00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 static PIXEL firstdiff_x[3][3] = { { -1.0, 0.0, 1.0 },
00016 { -2.0, 0.0, 2.0 },
00017 { -1.0, 0.0, 1.0 } };
00018
00019 static PIXEL firstdiff_y[3][3] = { { 1.0, 2.0, 1.0 },
00020 { 0.0, 0.0, 0.0 },
00021 {-1.0, -2.0, -1.0 } };
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 void TREERINGMAG (
00032 IMAGE *in,
00033 double sigma,
00034 int size,
00035 IMAGE **out
00036
00037
00038 ) { register short i, j, k, n;
00039 char msg[SLEN];
00040 IMAGE *gaussian, *smoothed;
00041 IMAGE *FoG_mag, *FoG_dir;
00042 PIXEL *psf=0, factor;
00043 PIXEL theta;
00044 int sectorval;
00045
00046
00047 int progmeter;
00048 double progress;
00049 double localpercent;
00050 double localprogress;
00051 int progcount, progtotal, progincr;
00052 int cancel=0;
00053
00054 progmeter = ProgMeter_Create("Compute Tree Ring Magnitudes");
00055 progress = 0.0;
00056
00057
00058
00059
00060 if (NAMES) {
00061 MESSAGE('I',"");
00062 MESSAGE('I',"TREERINGMAG");
00063 MESSAGE('I',"");
00064 sprintf(msg," Input image: %s",in->text);
00065 MESSAGE('I',msg);
00066 sprintf(msg," Gaussian smoothing filter size: %d",size);
00067 MESSAGE('I',msg);
00068 sprintf(msg," Standard Deviation: %f",sigma);
00069 MESSAGE('I',msg);
00070 MESSAGE('I'," ...............");
00071 }
00072
00073
00074 if (!CHECKIMG(in)) {
00075 MESSAGE('E'," Can't identify image.");
00076 goto the_end;
00077 } else if (in->nbnd > 1) {
00078 MESSAGE('E'," Image must be single-band.");
00079 goto the_end;
00080 }
00081
00082
00083
00084 if (!CHECKIMG(*out)) GETMEM(in->nbnd,in->nlin,in->npix,out);
00085 if (!*out) goto the_end;
00086
00087
00088 if (size > 0) {
00089
00090 CREATEGAUSS(size, sigma, &gaussian);
00091 if (CHECKIMG(gaussian)) {
00092 sprintf(gaussian->text, "%dx%d Gaussian -- sigma=%f", size,size,sigma);
00093 } else {
00094 MESSAGE('E'," Can't identify gaussian filter.");
00095 goto the_end;
00096 }
00097 if (progmeter) {
00098 progress = 10.0;
00099 cancel = ProgMeter_Update(progmeter,progress);
00100 if (cancel != 0) goto the_end;
00101 }
00102
00103
00104 if (!(psf = (PIXEL *) malloc(size*size*sizeof(PIXEL)))) {
00105 MESSAGE('E'," Memory allocation failed.");
00106 goto the_end;
00107 }
00108 for (n=0, j=0, factor=0.0; j<size; j++) {
00109 for (k=0; k<size; k++) {
00110 psf[n] = gaussian->data[0][j][k];
00111 factor += psf[n++];
00112 }
00113 }
00114 for (n=0, j=0; j<size; j++) {
00115 for (k=0; k<size; k++) {
00116 psf[n++] /= factor;
00117 }
00118 }
00119 SCONVL (in, psf, size, size, &smoothed);
00120 } else {
00121 IMGCOPY(in,&smoothed);
00122 }
00123
00124 if (progmeter) {
00125 progress = 30.0;
00126 cancel = ProgMeter_Update(progmeter,progress);
00127 if (cancel != 0) goto the_end;
00128 }
00129
00130
00131 if (CHECKIMG(smoothed)) {
00132 sprintf(smoothed->text, "Smoothed image");
00133 } else {
00134 MESSAGE('E'," Can't identify smoothed image.");
00135 goto the_end;
00136 }
00137
00138
00139 GRADIENT(smoothed,(PIXEL *)firstdiff_y,(PIXEL *)firstdiff_x,3,&FoG_mag,&FoG_dir);
00140
00141 if (progmeter) {
00142 progress = 70.0;
00143 cancel = ProgMeter_Update(progmeter,progress);
00144 if (cancel != 0) goto the_end;
00145 }
00146
00147
00148
00149 if (progmeter) {
00150 localpercent = 0.3;
00151 localprogress = 0.0;
00152 progcount = 0;
00153 progtotal = in->npix * in->nlin;
00154 progincr = rint((double)progtotal/8.0);
00155 }
00156 for (j=0; j<in->nlin; j++) {
00157 for (k=0; k<in->npix; k++) {
00158
00159 if (progmeter) {
00160 progcount++;
00161 if ((progcount%progincr) == 0) {
00162 localprogress = ((double)progcount/(double)progtotal)*100.0;
00163 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00164 if (cancel != 0) goto the_end;
00165 }
00166 }
00167
00168 if (((k>0)&&(FoG_mag->data[0][j][k-1] > FoG_mag->data[0][j][k]))
00169 || ((k<in->npix-1)&&(FoG_mag->data[0][j][k+1] > FoG_mag->data[0][j][k]))) {
00170
00171 (*out)->data[0][j][k] = 0.0;
00172 } else if ((k<in->npix-1)&&(k>0)
00173 &&(smoothed->data[0][j][k+1] < smoothed->data[0][j][k-1])) {
00174
00175 (*out)->data[0][j][k] = 0.0;
00176 } else {
00177 (*out)->data[0][j][k] = FoG_mag->data[0][j][k];
00178 }
00179 }
00180 }
00181
00182
00183 the_end:
00184 if (psf) free(psf);
00185 if (CHECKIMG(gaussian)) RELIMG(&gaussian);
00186 if (CHECKIMG(smoothed)) RELIMG(&smoothed);
00187 if (CHECKIMG(FoG_mag)) RELIMG(&FoG_mag);
00188 if (CHECKIMG(FoG_dir)) RELIMG(&FoG_dir);
00189
00190
00191 if (progmeter) {
00192 ProgMeter_Destroy(progmeter);
00193 }
00194
00195 if (cancel != 0) {
00196 if (*out) RELMEM(*out);
00197 *out = NULL;
00198 }
00199 }