00001 #include "trees.h"
00002 #include <math.h>
00003 #include <sys/time.h>
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 void MOSAIC_BYTE_TRACKRINGS (
00014 IMAGE_BYTE *in,
00015 MOSAIC_INDEX *index,
00016 IMAGE_BYTE **iMag,
00017 IMAGE_BYTE **iDir
00018
00019 ) { register int i, j, k;
00020 struct timeval start, end, t1, t2;
00021 IMAGE *curframe=NULL;
00022 IMAGE *gradmag=NULL;
00023 IMAGE *graddir=NULL;
00024 IMAGE *mag=NULL;
00025 IMAGE *dir=NULL;
00026 LOCAL_INDEX *localindex = NULL;
00027
00028 int progmeter;
00029 double progress;
00030 double localpercent;
00031 double localprogress;
00032 int total, increment;
00033 int cancel=0;
00034
00035 progmeter = ProgMeter_Create("Compute Tree Ring Magnitudes");
00036 progress = 0.0;
00037
00038 *iMag = NULL;
00039 *iDir = NULL;
00040
00041
00042
00043 if (!CHECKIMG_BYTE(in)) {
00044 MESSAGE('E'," Can't identify image.");
00045 goto the_end;
00046 } else if (!CHECKIMG_MOSAIC_INDEX(index)) {
00047 MESSAGE('E'," Can't identify mosaic index.");
00048 goto the_end;
00049 } else if (in->npix != index->npix) {
00050 MESSAGE('E'," Size of mosaic index does not match size of mosaic.");
00051 goto the_end;
00052 } else if (in->nbnd > 1) {
00053 MESSAGE('E'," Only first band of input image will be used.");
00054 }
00055
00056
00057
00058 if (!CHECKIMG_BYTE(*iMag)) GETMEM_BYTE(1,in->nlin,in->npix,iMag);
00059 if (!(*iMag)) goto the_end;
00060
00061 if (!CHECKIMG_BYTE(*iDir)) GETMEM_BYTE(1,in->nlin,in->npix,iDir);
00062 if (!(*iDir)) goto the_end;
00063
00064
00065
00066 for (j=0; j<in->nlin; j++) {
00067 for (k=0; k<in->npix; k++) {
00068
00069 (*iMag)->data[0][j][k] = (PIXEL_BYTE)0;
00070 (*iDir)->data[0][j][k] = (PIXEL_BYTE)0;
00071 }
00072 }
00073
00074 if (progmeter) {
00075 localpercent = 1.0;
00076 localprogress = 0.0;
00077 total = index->nframe;
00078 increment = 1;
00079 }
00080
00081
00082 for (i=0; i<index->nframe; i++) {
00083
00084 if (progmeter) {
00085 if ((i%increment) == 0) {
00086 localprogress = ((double)i/(double)total)*100.0;
00087 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00088 if (cancel != 0) goto the_end;
00089 }
00090 }
00091
00092
00093 if (index->frame[i].endx < index->hstart)
00094 continue;
00095 if (index->frame[i].startx > index->hend)
00096 continue;
00097
00098 printf("Finding gradient in frame %d\n",i);
00099 gettimeofday(&start, NULL);
00100
00101
00102 gettimeofday(&t1, NULL);
00103 MOSAIC_BYTE_EXTRACT(in, index,
00104 index->frame[i].starty-6, index->frame[i].endy+6,
00105 index->frame[i].startx-6, index->frame[i].endx+6,
00106 (PIXEL)127.0, &curframe);
00107 gettimeofday(&t2, NULL);
00108 printf(" extract = %ld ms.\n", delay(t1, t2));
00109
00110
00111 COMPUTE_LOCAL_INDEX(index, i,
00112 index->frame[i].starty-6, index->frame[i].endy+6,
00113 index->frame[i].startx-6, index->frame[i].endx+6,
00114 &localindex);
00115
00116
00117
00118 gettimeofday(&t1, NULL);
00119 TREES_TREERINGMAG(curframe, localindex, 3.0, 5, &gradmag, &graddir);
00120 gettimeofday(&t2, NULL);
00121 printf(" treeringmag = %ld ms.\n", delay(t1, t2));
00122
00123 if (!CHECKIMG(gradmag) || !CHECKIMG(gradmag)) {
00124 MESSAGE('E'," Problem determining gradient magnitude and direction.");
00125 goto the_end;
00126 } else {
00127 sprintf(gradmag->text, "%s - Ring Magnitude", in->text);
00128 sprintf(graddir->text, "%s - Ring Direction", in->text);
00129 }
00130
00131
00132
00133
00134 gettimeofday(&t1, NULL);
00135 TRIM(gradmag,6,&mag);
00136 TRIM(graddir,6,&dir);
00137 gettimeofday(&t2, NULL);
00138 printf(" trim = %ld ms.\n", delay(t1, t2));
00139
00140
00141 if (!CHECKIMG(mag) || !CHECKIMG(dir)) {
00142 MESSAGE('E'," Problem trimming magnitude and direction images.");
00143 goto the_end;
00144 } else {
00145 sprintf(mag->text, "%s - Ring Magnitude", in->text);
00146 sprintf(dir->text, "%s - Ring Direction", in->text);
00147 }
00148
00149 if (CHECKIMG(gradmag)) RELIMG(&gradmag);
00150 gradmag = NULL;
00151 if (CHECKIMG(graddir)) RELIMG(&graddir);
00152 graddir = NULL;
00153
00154
00155
00156 gettimeofday(&t1, NULL);
00157 MOSAIC_BYTE_INSERT(mag, index, index->frame[i].starty, index->frame[i].startx, iMag);
00158 MOSAIC_BYTE_INSERT(dir, index, index->frame[i].starty, index->frame[i].startx, iDir);
00159 gettimeofday(&t2, NULL);
00160 printf(" insert frame = %ld ms.\n", delay(t1, t2));
00161
00162 if (CHECKIMG(mag)) RELIMG(&mag);
00163 if (CHECKIMG(dir)) RELIMG(&dir);
00164 mag = NULL;
00165 dir = NULL;
00166
00167 printf("Done finding gradient in frame %d\n",i);
00168
00169 gettimeofday(&end, NULL);
00170 printf("Time required for frame %d= %ld ms.\n\n", i,delay(start, end));
00171
00172
00173 if (CHECKIMG(curframe)) RELIMG(&curframe);
00174 curframe = NULL;
00175 if (localindex) localindex = NULL;
00176 }
00177
00178 if (progmeter) {
00179 cancel = ProgMeter_Update(progmeter,100.0);
00180 if (cancel != 0) goto the_end;
00181 }
00182
00183
00184 the_end:
00185
00186 if (progmeter) {
00187 ProgMeter_Destroy(progmeter);
00188 }
00189
00190
00191 if (CHECKIMG(curframe)) RELIMG(&curframe);
00192 if (CHECKIMG(mag)) RELIMG(&mag);
00193 if (CHECKIMG(dir)) RELIMG(&dir);
00194 if (CHECKIMG(gradmag)) RELIMG(&gradmag);
00195 if (CHECKIMG(graddir)) RELIMG(&graddir);
00196
00197 }