00001 #include <stdio.h>
00002 #include "sadie.h"
00003 #include "mosaic.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 void MOSAIC_IMPORT (
00023 MOSAIC_INDEX *index,
00024 char *name,
00025 int jbgn,
00026 int kbgn,
00027 int jinc,
00028 int kinc,
00029 int jend,
00030 int kend,
00031 IMAGE **out
00032
00033 ) {
00034 register int j, k;
00035 char msg[SLEN];
00036 int lines, pixels;
00037 int outj, outk, nlin, npix;
00038 int nbytes;
00039 PIXEL_BYTE *column=NULL, *ptr=NULL;
00040 FILE *fp=NULL;
00041
00042
00043
00044
00045
00046
00047 if (!CHECKIMG_MOSAIC_INDEX(index)) {
00048 MESSAGE('E'," Can't identify mosaic index.");
00049 goto the_end;
00050 } else if (0 > jbgn || jend >= index->nlin) {
00051 MESSAGE('E'," Image to be sampled must be located completely inside input image in the line dimension.");
00052 goto the_end;
00053 } else if (0 > kbgn || kend >= index->npix) {
00054 MESSAGE('E'," Image to be sampled must be located completely inside input image in the pixel dimension.");
00055 goto the_end;
00056 } else if (jinc <= 0) {
00057 MESSAGE('E'," Line subsample increment must be greater than zero.");
00058 goto the_end;
00059 } else if (kinc <= 0) {
00060 MESSAGE('E'," Pixel subsample increment must be greater than zero.");
00061 goto the_end;
00062 } else if (jbgn > jend) {
00063 MESSAGE('E'," Number of lines to be spanned must be greater than zero.");
00064 goto the_end;
00065 } else if (kbgn > kend) {
00066 MESSAGE('E'," Number of pixels/line to be spanned must be greater than zero.");
00067 goto the_end;
00068 }
00069
00070
00071 if (!(fp=fopen(name,"r"))) {
00072 sprintf(msg," Can't open file %s.",name);
00073 MESSAGE('E',msg);
00074 goto the_end;
00075 }
00076
00077
00078 fseek(fp, (long) 512, SEEK_SET);
00079
00080
00081
00082 lines = max(1,(jend-jbgn+1)/jinc);
00083 pixels = max(1,(kend-kbgn+1)/kinc);
00084 if (!CHECKIMG(*out)) GETMEM(1,lines,pixels,out);
00085 if (!*out) goto the_end;
00086 (*out)->nbit = 8;
00087
00088
00089 nbytes = (int)index->height_max;
00090 if ((column=(PIXEL_BYTE *)malloc((long)index->height_max * sizeof(PIXEL_BYTE))) == NULL) {
00091 MESSAGE('E'," Memory request failed.");
00092 goto the_end;
00093 }
00094 if (fseek(fp,(long)(nbytes * (long)kbgn),SEEK_CUR) != 0) {
00095 sprintf(msg," Can't skip to column %d in file %s.",kbgn,name);
00096 MESSAGE('E',msg);
00097 goto the_end;
00098 }
00099 for (k = kbgn + (kinc-1), outk=0; outk<pixels; k += kinc, outk++) {
00100 if (fseek(fp,(long)(nbytes * (long)(kinc - 1)),SEEK_CUR) != 0) {
00101 sprintf(msg," Can't column subsample by %d in file %s.",kinc,name);
00102 MESSAGE('E',msg);
00103 goto the_end;
00104 }
00105 if (fread(column,nbytes,1,fp) != 1) {
00106 sprintf(msg," Can't read image data from file %s.",name);
00107 MESSAGE('E',msg);
00108 goto the_end;
00109 }
00110 for (j = jbgn, ptr=column, outj=0; outj < lines; j+=jinc, outj++) {
00111 if ((j < index->voffset[k]) || (j > index->voffset[k] + index->vheight[k])) {
00112 (*out)->data[0][outj][outk] = (PIXEL) 0.0;
00113 } else {
00114 (*out)->data[0][outj][outk] = (PIXEL) *(ptr + (j - index->voffset[k]));
00115 }
00116 }
00117 }
00118
00119
00120 if (NAMES) {
00121 RANGE(*out);
00122 MESSAGE('I',"");
00123 MESSAGE('I',"MOSAIC_IMPORT");
00124 MESSAGE('I',"");
00125 sprintf(msg," Input file: %s",name);
00126 MESSAGE('I',msg);
00127 MESSAGE('I'," Input format: 8-bit BIP in MOSAIC_IMAGE format");
00128 MESSAGE('I',msg);
00129 sprintf(msg," Number of lines, pixels: %d, %d",nlin,npix);
00130 MESSAGE('I',msg);
00131 sprintf(msg," Number of bits/pixel/band: %d",8);
00132 MESSAGE('I',msg);
00133 sprintf(msg," Subsampled from line, pixel: %d, %d",jbgn+1,kbgn+1);
00134 MESSAGE('I',msg);
00135 sprintf(msg," Subsampled to line, pixel: %d, %d",jend+1,kend+1);
00136 MESSAGE('I',msg);
00137 sprintf(msg," Subsample increment: lines, pixels: %d, %d",jinc,kinc);
00138 MESSAGE('I',msg);
00139 MESSAGE('I'," ...............");
00140 }
00141
00142
00143 the_end:
00144 if (fp) {
00145 if (fclose(fp)) {
00146 sprintf(msg," Can't close file %s.",name);
00147 MESSAGE('W',msg);
00148 }
00149 }
00150 if (column) free(column);
00151 if (TIMES) TIMING(T_EXIT);
00152 }