PICTURE *makepic(int fileid, int x, int y, float xorig, float yorig, int items, int sampls, char *history) { PICTURE *pic = newpic(); fillhead(pic,fileid,x,y,xorig,yorig,items,sampls,history); if (newdata(pic)) return(NULL); return(pic); }
/* only header contents copied, data memory not allocated; used by copypic */ PICTURE *copyhead(PICTURE *pic, int samples, int fileid) { PICTURE *pic2 = newpic(); if (pic == NULL) { (void)fprintf(stderr,"copyhead given null pic\n"); return(NULL); } fillhead(pic2,fileid,pic->x,pic->y,pic->xorigin,pic->yorigin,pic->items,samples,pic->history); return(pic2); }
int fm_MITIFF_read(char *infile, unsigned char *image[], fmio_mihead *ginfo) { char *where="MITIFF_read"; TIFF *in; int i, status, size; short pmi; unsigned int fieldlen, currlen, nextlen, taglen; char *description, *o_description; char *currfield, *nextfield, *field, *pt; char *o_currfield, *o_nextfield, *o_field; char *fieldname[FMIO_FIELDS]={ "Satellite:", "Date and Time:", "SatDir:", "Channels:", "In this file:", "Xsize:", "Ysize:", "Map projection:", "TrueLat:", "GridRot:", "Xunit:", "Yunit:", "NPX:", "NPY:", "Ax:", "Ay:", "Bx:", "By:", "Calibration" }; /* * Open TIFF files and initialize IFD */ in=TIFFOpen(infile, "rc"); if (!in) { printf(" This is no TIFF file! \n"); return(FM_IO_ERR); } /* * Test whether this is a color palette image or not. If so another * function should be used. */ status = TIFFGetField(in, 262, &pmi); if (pmi == 3) { return(FM_IO_ERR); } description = (char *) malloc(1024*sizeof(char)); if (!description) fmerrmsg(where,"Memory allocation failed"); o_description = description; TIFFGetField(in, 270, &description); currfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char)); if (!currfield) fmerrmsg(where,"Memory allocation failed"); o_currfield = currfield; nextfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char)); if (!nextfield) fmerrmsg(where,"Memory allocation failed"); o_nextfield = nextfield; for (i=0; i<FMIO_FIELDS-1; i++) { pt = strstr(description, fieldname[i]); sprintf(currfield, "%s", pt); currlen = strlen(currfield); pt = strstr(description, fieldname[i+1]); sprintf(nextfield, "%s", pt); nextlen = strlen(nextfield); taglen = strlen(fieldname[i]); fieldlen = currlen-nextlen-taglen; field = (char *) calloc(fieldlen+1, sizeof(char)); if (!field) fmerrmsg(where,"Memory allocation failed"); o_field = field; currfield += taglen; strncpy(field, currfield, fieldlen); fillhead(field, fieldname[i], ginfo); free(o_field); } free(o_currfield); free(o_nextfield); free(o_description); /* * Read image data into matrix. */ TIFFGetField(in, 256, &ginfo->xsize); TIFFGetField(in, 257, &ginfo->ysize); size = ginfo->xsize*ginfo->ysize; /* * Memory allocated for image data in this function (*image) is freed * in function main process. */ if (ginfo->zsize > FMIO_MAXCHANNELS) { printf("\n\tNOT ENOUGH POINTERS AVAILABLE TO HOLD DATA!\n"); return(FM_IO_ERR); } for (i=0; i<ginfo->zsize; i++) { image[i] = (unsigned char *) malloc((size+1)*sizeof(char)); if (!image[i]) fmerrmsg(where,"Memory allocation failed"); status = TIFFReadRawStrip(in, 0, image[i], size); if (status == -1) return(FM_IO_ERR); if (TIFFReadDirectory(in) == 0) break; } if (ginfo->zsize != (i+1)) { printf("\n\tERROR READING MULTIPLE SUBFILES!\n"); return(FM_IO_ERR); } TIFFClose(in); return(FM_OK); }
int readhead(PICTURE *pic, FILE *fp) /* architecture independent binary reader. Reads (and prints) header into the *PICTURE structure with *data untouched */ { unsigned char firstmagic, secondmagic; char pline[132]; union { unsigned char charform[4]; int intform; } magic; int x,y,gmax; vmessage(); firstmagic = fgetc(fp); if ( firstmagic == 'P' ) { secondmagic = fgetc(fp); fgetc(fp); /* skips linefeed */ fgets(pline, 132, fp); if ( (pline[0] < 48) || (pline[0] > 57) ) { (void)fprintf(stderr,"Pixelmap Comment: %s",pline); fscanf(fp,"%d %d",&x,&y); } else sscanf(pline,"%d %d",&x,&y); fscanf(fp,"%d",&gmax); if (gmax != GMAX) { (void)fprintf(stderr,"Unexpected Pixelmap Greylevels: %d\n",gmax); return(-1); } fgetc(fp); /* skips linefeed */ switch (secondmagic) { case '2': /* call read asci ? */ magic.intform = PGM_ASCI_MAGIC; fillhead(pic,IMAGE_ID,x,y,0.f,0.f,x*y,1,"PGM ascii"); break; case '3': /* call read asci ? */ magic.intform = PPM_ASCI_MAGIC; fillhead(pic,IMAGE_ID,x,y,0.f,0.f,x*y,3,"PPM ascii"); break; case '5': /* .pgm file */ magic.intform = PGM_MAGIC; fillhead(pic,IMAGE_ID,x,y,0.f,0.f,x*y,1,"PGM"); break; case '6': /* .ppm file */ magic.intform = PPM_MAGIC; fillhead(pic,IMAGE_ID,x,y,0.f,0.f,x*y,3,"PPM"); break; case '7': /* .pam file */ magic.intform = PAM_MAGIC; fillhead(pic,IMAGE_ID,x,y,0.f,0.f,x*y,3,"PAM"); break; default: (void)fprintf(stderr, "Unexpected magic P%c: supported pixel maps are: P2,P3,P5,P6\n",secondmagic); return(-1); } } else /* .lvl format */ { magic.charform[0] = firstmagic; magic.charform[1] = fgetc(fp); magic.charform[2] = fgetc(fp); magic.charform[3] = fgetc(fp); fread(pic,HEADSZ,1,fp); switch (magic.intform) { case VIS_MAGIC: case VISA_MAGIC: break; case VIS_SWMAGIC: case VISA_SWMAGIC: swaphead(pic); break; default: (void)fprintf(stderr,"readhead: unrecognised magic int\n"); return(-1); } } strcat(pic->history,"|"); strcat(pic->history,PROGNAME); pic->magic = magic.intform; printhead(pic, pic->magic); return(0); /* success */ }
/* * PURPOSE: * To read DNMI/TIFF palette color files containing either classed satellite * imagery or radar imagery. * * RETURN VALUES: * 0 - Normal and correct ending * 2 - This is not a Palette-color image * * NOTE: * Requires access to libtiff. * * AUTHOR: * Øystein Godøy, DNMI/FOU, 21/07/1999 * MODIFICATION: * Øystein Godøy, DNMI/FOU, 27/03/2001 * Corrected some memory allocation and freeing problems connected to * strtok actually changing the string it operates on. */ int fm_MITIFF_read_imagepal(char *infile, unsigned char *image[], fmio_mihead *ginfo, fmio_mihead_pal *palinfo) { char *where="MITIFF_read_imagepal"; TIFF *in; int i, status, size; short pmi; unsigned int fieldlen, currlen, nextlen, taglen; uint16 *red, *green, *blue; char *description, *o_description; char *currfield, *nextfield, *field, *pt; char *o_currfield, *o_nextfield, *o_field; char *fieldname[FMIO_FIELDS]={ "Satellite:", "Date and Time:", "SatDir:", "Channels:", "In this file:", "Xsize:", "Ysize:", "Map projection:", "TrueLat:", "GridRot:", "Xunit:", "Yunit:", "NPX:", "NPY:", "Ax:", "Ay:", "Bx:", "By:", "COLOR INFO:" }; /* * Open TIFF files and initialize IFD */ in=TIFFOpen(infile, "rc"); if (!in) { printf(" This is no TIFF file! \n"); return(FM_IO_ERR); } /* * Test whether this is a color palette image or not. If so another * function should be used. */ status = TIFFGetField(in, 262, &pmi); if (pmi != 3) { return(FM_IO_ERR); } status = TIFFGetField(in, 320, &red, &green, &blue); if (status != 1) { return(FM_IO_ERR); } for (i=0; i<256; i++) { palinfo->cmap[0][i] = red[i]; palinfo->cmap[1][i] = green[i]; palinfo->cmap[2][i] = blue[i]; } description = (char *) malloc(FMIO_TIFFHEAD*sizeof(char)); if (!description) fmerrmsg(where,"Memory allocation failed"); o_description = description; TIFFGetField(in, 270, &description); /* * Lead through the filed tags defined, except for the last one which will * create a segmentation fault if it is used. This is processed after the * loop. */ currfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char)); if (!currfield) fmerrmsg(where,"Memory allocation failed"); o_currfield = currfield; nextfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char)); if (!nextfield) fmerrmsg(where,"Memory allocation failed"); o_nextfield = nextfield; for (i=0; i<FMIO_FIELDS-1; i++) { pt = strstr(description, fieldname[i]); sprintf(currfield, "%s", pt); currlen = strlen(currfield); pt = strstr(description, fieldname[i+1]); sprintf(nextfield, "%s", pt); nextlen = strlen(nextfield); taglen = strlen(fieldname[i]); fieldlen = currlen-nextlen-taglen; field = (char *) malloc((fieldlen+1)*sizeof(char)); if (!field) fmerrmsg(where,"Memory allocation failed"); o_field = field; currfield += taglen; strncpy(field, currfield, fieldlen); fillhead(field, fieldname[i], ginfo); free(o_field); } /* * The last part of the information header is treated as one single string * and is extracted as the remaining part and processed in a suitable way * later... */ pt = strstr(description, fieldname[FMIO_FIELDS-1]); sprintf(currfield, "%s", pt); currlen = strlen(currfield); nextlen = strlen(description); taglen = strlen(fieldname[FMIO_FIELDS-1]); /* fieldlen = nextlen-currlen-taglen; field = (char *) malloc((fieldlen+1)*sizeof(char)); */ fieldlen = nextlen-currlen-taglen; field = (char *) malloc((currlen+1)*sizeof(char)); if (!field) fmerrmsg(where,"Memory allocation failed"); o_field = field; /* * Beware here, in order to help C keep track of which memory to free * later, currfield should be reduced by taglen imediately or better a new * work string should be used, but for now this solution is chosen... */ currfield += taglen; /* strncpy(field, currfield, fieldlen); */ strncpy(field, currfield, currlen); currfield -= taglen; /* printf(" %d-%d-%d-%d\n",currlen,nextlen,taglen,fieldlen); printf("%s\n\n%s\n",currfield, field); printf("%d - %d\n",strlen(currfield),strlen(field)); */ fillhead_imagepal(field, fieldname[FMIO_FIELDS-1], palinfo); free(o_field); free(o_currfield); free(o_nextfield); free(o_description); /* * Read image data into matrix. */ TIFFGetField(in, 256, &ginfo->xsize); TIFFGetField(in, 257, &ginfo->ysize); size = ginfo->xsize*ginfo->ysize; /* * Memory allocated for image data in this function (*image) is freed * in function main process. */ if (ginfo->zsize > FMIO_MAXCHANNELS) { printf("\n\tNOT ENOUGH POINTERS AVAILABLE TO HOLD DATA!\n"); return(FM_IO_ERR); } for (i=0; i<ginfo->zsize; i++) { image[i] = (unsigned char *) malloc((size+1)*sizeof(char)); if (!image[i]) fmerrmsg(where,"Memory allocation failed"); status = TIFFReadRawStrip(in, 0, *image, size); if (status == -1) return(FM_IO_ERR); if (TIFFReadDirectory(in) == 0) break; } if (ginfo->zsize != (i+1)) { printf("\n\tERROR READING MULTIPLE SUBFILES!\n"); return(FM_IO_ERR); } TIFFClose(in); return(FM_OK); }