int main(int argc,char**argv){ Image *dst,*src,*dst3; char filename[256]; src=ImageRead(argv[1]); dst=ImageAlloc(src->W,src->H); dst3=ImageAlloc(src->W*3,src->H*3); Process(dst,src); sprintf(filename,"p1_%s.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src,100); sprintf(filename,"p2_%s_r00.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src, 10); sprintf(filename,"p2_%s_0g0.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src, 1); sprintf(filename,"p2_%s_00b.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src,110); sprintf(filename,"p2_%s_rg0.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src, 11); sprintf(filename,"p2_%s_0gb.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src,101); sprintf(filename,"p2_%s_r0b.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process2(dst,src,111); sprintf(filename,"p2_%s_rgb.%s",argv[2],argv[3]); ImageWrite(filename,dst); memset(filename,'0',sizeof(filename)); Process3(dst3,src,3); sprintf(filename,"p3_%s.%s",argv[2],argv[3]); ImageWrite(filename,dst3); memset(filename,'0',sizeof(filename)); return 0; }
/*---------------------------------------------------------------------- Parameters: image - the image to write fname - the name of the file to write to. Description: write a hips image to file 'fname' ----------------------------------------------------------------------*/ int ImageWriteFrames(IMAGE *image, const char*fname, int start, int nframes) { IMAGE *tmp_image ; tmp_image = ImageAlloc(image->rows, image->cols,image->pixel_format,nframes); ImageCopyFrames(image, tmp_image, start, nframes, 0) ; ImageWrite(tmp_image, fname) ; ImageFree(&tmp_image) ; return(NO_ERROR); }
Image*ImageRead(const char*name){//画像ファイルを読み込む関数 int W,H; Image*im; FILE*fp; char msg[256]; int flag = 0; flag = swith_extention_read(name,msg); if(flag == 1) fp = fopen(name,"rb"); else if(flag == 2) fp = popen(msg,"r"); fscanf(fp,"%*s%d%d%*s%*c",&W,&H); im=ImageAlloc(W,H); fread(im->data,1,W*H*3,fp); if(flag == 1) fclose(fp); else if(flag == 2) pclose(fp); return im; }
/*----------------------------------------------------- Parameters: Returns value: Description read an image from file and convert it to the specified format. ------------------------------------------------------*/ IMAGE * ImageReadType(const char*fname, int pixel_format) { IMAGE *Itmp, *I ; Itmp = ImageRead(fname) ; if (!Itmp) ErrorReturn(NULL, (ERROR_NO_FILE, "ImageReadType(%s, %d): could not read image", fname, pixel_format)) ; if (Itmp->pixel_format != pixel_format) { I = ImageAlloc(Itmp->rows, Itmp->cols, pixel_format, Itmp->num_frame) ; ImageCopy(Itmp, I) ; ImageFree(&Itmp) ; } else I = Itmp ; return(I) ; }
/*---------------------------------------------------------------------- Parameters: Description: ----------------------------------------------------------------------*/ IMAGE * KernelImageToSeq(KIMAGE *kimage) { IMAGE *image ; int num_frame, row, col, rows, cols, krow, kcol, krows, kcols, pix_per_frame, row0, col0, drow, dcol, npix, col_dif, row_dif ; KERNEL *kernel ; float *fsrc, *fdst, *fbase ; krows = kimage->krows ; kcols = kimage->kcols ; rows = kimage->rows ; cols = kimage->cols ; pix_per_frame = krows * kcols ; num_frame = rows * cols ; image = ImageAlloc(krows, kcols, PFFLOAT, num_frame) ; if (!image) { fprintf(stderr, "KernelImageToSeq: could not allocate sequence\n") ; return(NULL) ; } for (row = 0 ; row < rows ; row++) { for (col = 0 ; col < cols ; col++) { kernel = KIMAGEpix(kimage, row, col) ; row0 = kernel->row0 ; col0 = kernel->col0 ; fbase = IMAGEFpix(image, 0, 0) + ((row * cols) + col) * pix_per_frame ; memset((char *)fbase, 0, pix_per_frame * sizeof(float)) ; /* copy kernel into Image frame, changing coordinate systems so that the kernel is centered in the frame. This involves truncating some of the kernel. */ for (krow = 0 ; krow < krows ; krow++) { row_dif = (row0+krows/2) - row ; col_dif = (col0+kcols/2) - col ; /* calculate destination location. The point (row,col) should map to the center of the Image image. */ drow = krow + row_dif ; dcol = col_dif ; if (dcol < 0) /* destination before start of image */ { kcol = -dcol ; dcol = 0 ; } else kcol = 0 ; npix = kcols - abs(col_dif) ; fsrc = kernel->weights[krow] + kcol ; fdst = fbase + drow * kcols + dcol ; if (drow >= 0 && (drow < krows) && (npix > 0)) memmove((char *)fdst, (char *)fsrc, npix*sizeof(float)) ; } } } return(image) ; }
static IMAGE * TiffReadImage(const char*fname, int frame0) { IMAGE *I ; TIFF *tif = TIFFOpen(fname, "r"); int type = PFBYTE; // just make compiler happy int width, height, ret, row; short nsamples, bits_per_sample; int nframe,frame; byte *iptr ; tdata_t *buf; short photometric; int photometricInt; short fillorder; short compression; int compressionInt; short orientation; short resunit ; #if 0// we used to translate RGB image into grey scale //unsigned char *buffer; int skip; int i; float r, g, b, y; float *pf; #endif int scanlinesize, extra_samples; int index = 0; float xres, yres, res ; if (!tif) return(NULL) ; /* Find out how many frames we have */ nframe = 1 ; // note that TIFFOpen reads the 1st directory while (TIFFReadDirectory(tif)) nframe++; // some tif image just cannot be handled if (nframe == 0) nframe = 1; /* Go back to the beginning */ TIFFSetDirectory(tif,0); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGEWIDTH, &width); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGELENGTH, &height); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric); // fill order is LSB or MSB TIFFReadScanLine() handles it automatically ret = TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compression); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &resunit); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_XRESOLUTION, &xres); if (ret == 0) xres = resunit == 1 ? .1 : (10*2.54) ; ret = TIFFGetFieldDefaulted(tif, TIFFTAG_YRESOLUTION, &yres); if (ret == 0) yres = resunit == 1 ? .1 : (10*2.54) ; // orientation // #define TIFFTAG_ORIENTATION 274 /* +image orientation */ // ORIENTATION_TOPLEFT 1 /* row 0 top, col 0 lhs */ // ORIENTATION_TOPRIGHT 2 /* row 0 top, col 0 rhs */ // ORIENTATION_BOTRIGHT 3 /* row 0 bottom, col 0 rhs */ // ORIENTATION_BOTLEFT 4 /* row 0 bottom, col 0 lhs */ // ORIENTATION_LEFTTOP 5 /* row 0 lhs, col 0 top */ // ORIENTATION_RIGHTTOP 6 /* row 0 rhs, col 0 top */ // ORIENTATION_RIGHTBOT 7 /* row 0 rhs, col 0 bottom */ // ORIENTATION_LEFTBOT 8 /* row 0 lhs, col 0 bottom */ ret = TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &orientation); if (DIAG_VERBOSE_ON) { fprintf(stderr, "\ntiff info\n"); fprintf(stderr, " size: (%d, %d)\n", width, height); fprintf(stderr, "samples/pixel: %d\n", nsamples); fprintf(stderr, " bits/sample: %d\n", bits_per_sample); fprintf(stderr, " orientation: %d\n", orientation); photometricInt = photometric; // used in 'case' statement to avoid // gcc warning 'case value out of range' switch (photometricInt) { case PHOTOMETRIC_MINISWHITE: fprintf(stderr, " photometric: min value is white.\n"); break; case PHOTOMETRIC_MINISBLACK: fprintf(stderr, " photometric: min value is black.\n"); break; case PHOTOMETRIC_RGB: fprintf(stderr, " photometric: RGB color model.\n"); break; case PHOTOMETRIC_PALETTE: fprintf(stderr, " photometric: use palette.\n"); break; case PHOTOMETRIC_MASK: fprintf(stderr, " photometric: $holdout mask.\n"); break; case PHOTOMETRIC_SEPARATED: fprintf(stderr, " photometric: color separations.\n"); break; case PHOTOMETRIC_YCBCR: fprintf(stderr, " photometric: YCbCr6 CCIR 601.\n"); break; case PHOTOMETRIC_CIELAB: fprintf(stderr, " photometric: 1976 CIE L*a*b* \n"); break; case PHOTOMETRIC_ITULAB: fprintf(stderr, " photometric: ITU L*a*b* \n"); break; case PHOTOMETRIC_LOGL: fprintf(stderr, " photometric: CIE Log2(L) \n"); break; case PHOTOMETRIC_LOGLUV: fprintf(stderr, " photometric: CIE Log2(L) (u',v') \n"); break; default: fprintf(stderr, " photometric: unknown type\n"); break; } compressionInt = compression; // used in 'case' statement to avoid // gcc warning 'case value out of range' // we are not supporting compression at this time switch (compressionInt) { case COMPRESSION_NONE: fprintf(stderr, " compression: no compression\n"); break; case COMPRESSION_LZW: fprintf(stderr, " compression: Lempel-Ziv & Welch\n"); break; case COMPRESSION_JPEG: fprintf(stderr, " compression: JPEG DCT compression\n"); break; case COMPRESSION_PACKBITS: fprintf(stderr, " compression: Macintosh RLE\n"); break; default: fprintf(stderr, " compression: %d see /usr/include/tiff.h for meaning\n", compression); break; } } extra_samples = 0 ; switch (nsamples) { case 1: switch (bits_per_sample) /* not valid - I don't know why */ { default: case 8: type = PFBYTE; break; case 16: type = PFSHORT; break; case 32: type = PFFLOAT; break; case 64: type = PFDOUBLE; break; } break; case 4: extra_samples = 1 ; nsamples = 3 ; // no break case 3: switch (bits_per_sample) { default: case 8: type = PFRGB; break; } break; default: ErrorExit(ERROR_BADPARM, "IMAGE: nsamples=%d, bits_per_sample=%d. only grey scale or RGB image is supported\n", nsamples,bits_per_sample); } // nsamples not handled here if (nsamples != 1 && nsamples != 3) ErrorExit(ERROR_BADPARM, "IMAGE: nsamples = %d. only grey scale or RGB image is supported\n", nsamples ); // type can be grey scale or RGB if (frame0 < 0) I = ImageAlloc(height, width, type, nframe) ; else I = ImageAlloc(height, width, type, 1) ; res = (xres+yres)/2 ; switch (resunit) { case RESUNIT_CENTIMETER: // 3 - cm case RESUNIT_NONE: // 1 - no units I->sizepix = 100 / res ; I->xsize = 100.0 / xres ; // mm I->ysize = 100.0 / yres ; // mm break ; default: case RESUNIT_INCH: // 2 - inches I->sizepix = 2.54 / res ; I->xsize = 10.0*2.54 / xres ; // mm I->ysize = 10.0*2.54 / yres ; // mm break ; } iptr = I->image; for (frame=0;frame<nframe;frame++) { int planar_config, fillorder ; TIFFSetDirectory(tif,frame); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planar_config); if (planar_config == PLANARCONFIG_SEPARATE) ErrorReturn(NULL, (ERROR_UNSUPPORTED, "TiffReadImage: PLANARCONFIG_SEPARATE unsupported")) ; // else planar_config == PLANARCONFIG_CONTIG ret = TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGEWIDTH, &width); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_IMAGELENGTH, &height); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples); ret = TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,&bits_per_sample); scanlinesize = TIFFScanlineSize(tif); for (row=0;row<height;row++) { // get the pointer at the first column of a row // note that the orientation is column, row switch (orientation) { case ORIENTATION_TOPLEFT: index = height-row-1; break; case ORIENTATION_BOTLEFT: index = row; break; default: ErrorExit(ERROR_BADPARM, "IMAGE: orientation = %d. we support only topleft or botleft\n", orientation); } if (nsamples == 1) { switch (bits_per_sample) { default: case 8: buf = (tdata_t *)IMAGEpix(I,0,index); break; case 16: buf = (tdata_t *)IMAGESpix(I,0,index); break; case 32: buf = (tdata_t *)IMAGEFpix(I,0,index); break; case 64: buf = (tdata_t *)IMAGEDpix(I,0,index); break; } if (TIFFReadScanline(tif, buf, row, 0) < 0) // row must be sequentially read for compressed data ErrorReturn(NULL, (ERROR_BADFILE, "TiffReadImage: TIFFReadScanline returned error")); if (bits_per_sample == 1) // unpack bitmap { unsigned char *bitmap, bitmask ; unsigned int byte_, col, bit, b ; bitmap = (unsigned char *)calloc(scanlinesize, sizeof(unsigned char)) ; memmove(bitmap, buf, scanlinesize) ; for (col = b = 0 ; b < scanlinesize ; b++, col += 8) { byte_ = bitmap[b] ; if (byte_ > 0) DiagBreak() ; if (fillorder == FILLORDER_LSB2MSB) { for (bitmask = 0x01, bit = 0 ; bit < 8 ; bit++) { if (col+bit== Gx && index == Gy) DiagBreak() ; *IMAGEpix(I,col+bit,index) = ((byte_ & bitmask) > 0) ; bitmask = bitmask << 1 ; } } else // fillorder == FILLORDER_MSB2LSB { for (bitmask = 0x01<<7, bit = 0 ; bit < 8 ; bit++) { if (col+bit== Gx && index == Gy) DiagBreak() ; *IMAGEpix(I,col+bit,index) = ((byte_ & bitmask) > 0) ; bitmask = bitmask >> 1 ; } } } free(bitmap) ; } } else if (nsamples == 4) // RGB model + alpha { int s ; unsigned char *ipix ; ipix = (unsigned char *)calloc(scanlinesize, sizeof(unsigned char)) ; buf = (tdata_t *)ipix ; switch (bits_per_sample) { default: case 8: // buf = (tdata_t*) IMAGERGBpix(I, 0, index); if (TIFFReadScanline(tif, buf, row, 0) < 0) // row must be sequentially read for compressed data ErrorReturn(NULL, (ERROR_BADFILE, "TiffReadImage: TIFFReadScanline returned error")); } for (s = 0 ; s < width ; s++) { unsigned char *opix ; opix = IMAGERGBpix(I, s, index) ; *opix++ = *ipix ; *opix++ = *(ipix+1) ; *opix++ = *(ipix+2) ; ipix += nsamples ; } free(buf) ; } else if (nsamples == 3) // RGB model { switch (bits_per_sample) { default: case 8: buf = (tdata_t*) IMAGERGBpix(I, 0, index); if (TIFFReadScanline(tif, buf, row, 0) < 0) // row must be sequentially read for compressed data ErrorReturn(NULL, (ERROR_BADFILE, "TiffReadImage: TIFFReadScanline returned error")); } #if 0 //////////////////////////////////////////////////////////// // we used to translate into the grey value // now translate it into Y value // RGB range 0 to 1.0 // then YIQ is // Y = 0.299 0.587 0.114 R // I 0.596 -0.275 -0.321 G // Q 0.212 -0.523 0.311 B // andd use Y for grey scale (this is color tv signal into bw tv switch (bits_per_sample) { default: case 8: skip = 3; // for (i = 0; i < width; ++i) { r = (float) buf[i*skip]; g = (float) buf[i*skip+1]; b = (float) buf[i*skip+2]; y = (0.299*r + 0.587*g + 0.114*b); *IMAGEpix(I, i, row) = (unsigned char) y; } break; // 3 bytes at a time case 32: skip = 12; // 3x4 bytes at a time for (i=0; i < width ; ++i) { pf = (float *) &buf[i*skip]; r = *pf; pf = (float *) &buf[i*skip+4]; g = *pf; pf = (float *) &buf[i*skip+8]; b = *pf; y = (0.299*r + 0.587*g + 0.114*b); *IMAGEFpix(I, i, row) = y; } case 64: ErrorExit(ERROR_BADPARM, "At this time we don't support RGB double valued tiff.\n"); } free(buffer); #endif } }