/** Write all known exif tags @param tif TIFF handle @param md_model Metadata model from where to load the tags @param dib Image being written @return Returns TRUE if successful, returns FALSE otherwise */ BOOL tiff_write_exif_tags(TIFF *tif, TagLib::MDMODEL md_model, FIBITMAP *dib) { char defaultKey[16]; // only EXIF_MAIN so far if(md_model != TagLib::EXIF_MAIN) { return FALSE; } if(FreeImage_GetMetadataCount(FIMD_EXIF_MAIN, dib) == 0) { return FALSE; } TagLib& tag_lib = TagLib::instance(); for (int fi = 0, nfi = (int)tif->tif_nfields; nfi > 0; nfi--, fi++) { const TIFFField *fld = tif->tif_fields[fi]; const uint32 tag_id = TIFFFieldTag(fld); if(skip_write_field(tif, tag_id)) { // skip tags that are already handled by the LibTIFF writing process continue; } FITAG *tag = NULL; // get the tag key const char *key = tag_lib.getTagFieldName(TagLib::EXIF_MAIN, (WORD)tag_id, defaultKey); if(FreeImage_GetMetadata(FIMD_EXIF_MAIN, dib, key, &tag)) { FREE_IMAGE_MDTYPE tag_type = FreeImage_GetTagType(tag); TIFFDataType tif_tag_type = TIFFFieldDataType(fld); // check for identical formats // (enum value are the sames between FREE_IMAGE_MDTYPE and TIFFDataType types) if((int)tif_tag_type != (int)tag_type) { // skip tag or _TIFFmemcpy will fail continue; } // type of storage may differ (e.g. rationnal array vs float array type) if((unsigned)_TIFFDataSize(tif_tag_type) != FreeImage_TagDataWidth(tag_type)) { // skip tag or _TIFFmemcpy will fail continue; } if(tag_type == FIDT_ASCII) { TIFFSetField(tif, tag_id, FreeImage_GetTagValue(tag)); } else { TIFFSetField(tif, tag_id, FreeImage_GetTagCount(tag), FreeImage_GetTagValue(tag)); } } } return TRUE; }
int main(int argc, char* argv[]) { TIFF *tiff; int arg_index; if (argc < 2) usage(); tiff = TIFFOpen(argv[argc-1], "r+"); if (tiff == NULL) return 2; for( arg_index = 1; arg_index < argc-1; arg_index++ ) { if (strcmp(argv[arg_index],"-d") == 0 && arg_index < argc-2) { arg_index++; if( TIFFSetDirectory(tiff, atoi(argv[arg_index]) ) != 1 ) { fprintf( stderr, "Failed to set directory=%s\n", argv[arg_index] ); return 6; } arg_index++; } if (strcmp(argv[arg_index],"-sd") == 0 && arg_index < argc-2) { arg_index++; if( TIFFSetSubDirectory(tiff, atoi(argv[arg_index]) ) != 1 ) { fprintf( stderr, "Failed to set sub directory=%s\n", argv[arg_index] ); return 7; } arg_index++; } /* Add unset option to tiffset -- Zach Baker ([email protected]) 11/14/2012 */ if (strcmp(argv[arg_index],"-u") == 0 && arg_index < argc-2) { const TIFFField *fip; const char *tagname; arg_index++; tagname = argv[arg_index]; fip = GetField(tiff, tagname); if (!fip) return 3; if (TIFFUnsetField(tiff, TIFFFieldTag(fip)) != 1) { fprintf(stderr, "Failed to unset %s\n", TIFFFieldName(fip)); } arg_index++; } else if (strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3) { const TIFFField *fip; const char *tagname; arg_index++; tagname = argv[arg_index]; fip = GetField(tiff, tagname); if (!fip) return 3; arg_index++; if (TIFFFieldDataType(fip) == TIFF_ASCII) { if (TIFFSetField(tiff, TIFFFieldTag(fip), argv[arg_index]) != 1) fprintf( stderr, "Failed to set %s=%s\n", TIFFFieldName(fip), argv[arg_index] ); } else if (TIFFFieldWriteCount(fip) > 0 || TIFFFieldWriteCount(fip) == TIFF_VARIABLE) { int ret = 1; short wc; if (TIFFFieldWriteCount(fip) == TIFF_VARIABLE) wc = atoi(argv[arg_index++]); else wc = TIFFFieldWriteCount(fip); if (argc - arg_index < wc) { fprintf( stderr, "Number of tag values is not enough. " "Expected %d values for %s tag, got %d\n", wc, TIFFFieldName(fip), argc - arg_index); return 4; } if (wc > 1) { int i, size; void *array; switch (TIFFFieldDataType(fip)) { /* * XXX: We can't use TIFFDataWidth() * to determine the space needed to store * the value. For TIFF_RATIONAL values * TIFFDataWidth() returns 8, but we use 4-byte * float to represent rationals. */ case TIFF_BYTE: case TIFF_ASCII: case TIFF_SBYTE: case TIFF_UNDEFINED: default: size = 1; break; case TIFF_SHORT: case TIFF_SSHORT: size = 2; break; case TIFF_LONG: case TIFF_SLONG: case TIFF_FLOAT: case TIFF_IFD: case TIFF_RATIONAL: case TIFF_SRATIONAL: size = 4; break; case TIFF_DOUBLE: size = 8; break; } array = _TIFFmalloc(wc * size); if (!array) { fprintf(stderr, "No space for %s tag\n", tagname); return 4; } switch (TIFFFieldDataType(fip)) { case TIFF_BYTE: for (i = 0; i < wc; i++) ((uint8 *)array)[i] = atoi(argv[arg_index+i]); break; case TIFF_SHORT: for (i = 0; i < wc; i++) ((uint16 *)array)[i] = atoi(argv[arg_index+i]); break; case TIFF_SBYTE: for (i = 0; i < wc; i++) ((int8 *)array)[i] = atoi(argv[arg_index+i]); break; case TIFF_SSHORT: for (i = 0; i < wc; i++) ((int16 *)array)[i] = atoi(argv[arg_index+i]); break; case TIFF_LONG: for (i = 0; i < wc; i++) ((uint32 *)array)[i] = atol(argv[arg_index+i]); break; case TIFF_SLONG: case TIFF_IFD: for (i = 0; i < wc; i++) ((uint32 *)array)[i] = atol(argv[arg_index+i]); break; case TIFF_DOUBLE: for (i = 0; i < wc; i++) ((double *)array)[i] = atof(argv[arg_index+i]); break; case TIFF_RATIONAL: case TIFF_SRATIONAL: case TIFF_FLOAT: for (i = 0; i < wc; i++) ((float *)array)[i] = (float)atof(argv[arg_index+i]); break; default: break; } if (TIFFFieldPassCount(fip)) { ret = TIFFSetField(tiff, TIFFFieldTag(fip), wc, array); } else if (TIFFFieldTag(fip) == TIFFTAG_PAGENUMBER || TIFFFieldTag(fip) == TIFFTAG_HALFTONEHINTS || TIFFFieldTag(fip) == TIFFTAG_YCBCRSUBSAMPLING || TIFFFieldTag(fip) == TIFFTAG_DOTRANGE) { if (TIFFFieldDataType(fip) == TIFF_BYTE) { ret = TIFFSetField(tiff, TIFFFieldTag(fip), ((uint8 *)array)[0], ((uint8 *)array)[1]); } else if (TIFFFieldDataType(fip) == TIFF_SHORT) { ret = TIFFSetField(tiff, TIFFFieldTag(fip), ((uint16 *)array)[0], ((uint16 *)array)[1]); } } else { ret = TIFFSetField(tiff, TIFFFieldTag(fip), array); } _TIFFfree(array); } else { switch (TIFFFieldDataType(fip)) { case TIFF_BYTE: case TIFF_SHORT: case TIFF_SBYTE: case TIFF_SSHORT: ret = TIFFSetField(tiff, TIFFFieldTag(fip), atoi(argv[arg_index++])); break; case TIFF_LONG: case TIFF_SLONG: case TIFF_IFD: ret = TIFFSetField(tiff, TIFFFieldTag(fip), atol(argv[arg_index++])); break; case TIFF_DOUBLE: ret = TIFFSetField(tiff, TIFFFieldTag(fip), atof(argv[arg_index++])); break; case TIFF_RATIONAL: case TIFF_SRATIONAL: case TIFF_FLOAT: ret = TIFFSetField(tiff, TIFFFieldTag(fip), (float)atof(argv[arg_index++])); break; default: break; } } if (ret != 1) fprintf(stderr, "Failed to set %s\n", TIFFFieldName(fip)); arg_index += wc; } } else if (strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3) { FILE *fp; const TIFFField *fip; char *text; size_t len; arg_index++; fip = GetField(tiff, argv[arg_index]); if (!fip) return 3; if (TIFFFieldDataType(fip) != TIFF_ASCII) { fprintf( stderr, "Only ASCII tags can be set from file. " "%s is not ASCII tag.\n", TIFFFieldName(fip) ); return 5; } arg_index++; fp = fopen( argv[arg_index], "rt" ); if(fp == NULL) { perror( argv[arg_index] ); continue; } text = (char *) malloc(1000000); len = fread( text, 1, 999999, fp ); text[len] = '\0'; fclose( fp ); if(TIFFSetField( tiff, TIFFFieldTag(fip), text ) != 1) { fprintf(stderr, "Failed to set %s from file %s\n", TIFFFieldName(fip), argv[arg_index]); } _TIFFfree( text ); arg_index++; } else { fprintf(stderr, "Unrecognised option: %s\n", argv[arg_index]); usage(); } } TIFFRewriteDirectory(tiff); TIFFClose(tiff); return 0; }
/** Read a single Exif tag @param tif TIFF handle @param tag_id TIFF Tag ID @param dib Image being read @param md_model Metadata model where to store the tag @return Returns TRUE if successful, returns FALSE otherwise */ static BOOL tiff_read_exif_tag(TIFF *tif, uint32 tag_id, FIBITMAP *dib, TagLib::MDMODEL md_model) { uint32 value_count = 0; int mem_alloc = 0; void *raw_data = NULL; if(tag_id == TIFFTAG_EXIFIFD) { // Exif IFD offset - skip this tag // md_model should be EXIF_MAIN, the Exif IFD is processed later using the EXIF_EXIF metadata model return TRUE; } if((tag_id == TIFFTAG_GPSIFD) && (md_model == TagLib::EXIF_MAIN)) { // Exif GPS IFD offset - skip this tag // should be processed in another way ... return TRUE; } TagLib& tagLib = TagLib::instance(); // get the tag key - use NULL to avoid reading GeoTIFF tags const char *key = tagLib.getTagFieldName(md_model, (WORD)tag_id, NULL); if(key == NULL) { return TRUE; } const TIFFField *fip = TIFFFieldWithTag(tif, tag_id); if(fip == NULL) { return TRUE; } if(TIFFFieldPassCount(fip)) { // a count value is required for 'TIFFGetField' if (TIFFFieldReadCount(fip) != TIFF_VARIABLE2) { // a count is required, it will be of type uint16 uint16 value_count16 = 0; if(TIFFGetField(tif, tag_id, &value_count16, &raw_data) != 1) { // stop, ignore error return TRUE; } value_count = value_count16; } else { // a count is required, it will be of type uint32 uint32 value_count32 = 0; if(TIFFGetField(tif, tag_id, &value_count32, &raw_data) != 1) { // stop, ignore error return TRUE; } value_count = value_count32; } } else { // determine count if (TIFFFieldReadCount(fip) == TIFF_VARIABLE || TIFFFieldReadCount(fip) == TIFF_VARIABLE2) { value_count = 1; } else if (TIFFFieldReadCount(fip) == TIFF_SPP) { uint16 spp; TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); value_count = spp; } else { value_count = TIFFFieldReadCount(fip); } // access fields as pointers to data // (### determining this is NOT robust... and hardly can be. It is implemented looking the _TIFFVGetField code) if(TIFFFieldTag(fip) == TIFFTAG_TRANSFERFUNCTION) { // reading this tag cause a bug probably located somewhere inside libtiff return TRUE; } if ((TIFFFieldDataType(fip) == TIFF_ASCII || TIFFFieldReadCount(fip) == TIFF_VARIABLE || TIFFFieldReadCount(fip) == TIFF_VARIABLE2 || TIFFFieldReadCount(fip) == TIFF_SPP || value_count > 1) && TIFFFieldTag(fip) != TIFFTAG_PAGENUMBER && TIFFFieldTag(fip) != TIFFTAG_HALFTONEHINTS && TIFFFieldTag(fip) != TIFFTAG_YCBCRSUBSAMPLING && TIFFFieldTag(fip) != TIFFTAG_DOTRANGE && TIFFFieldTag(fip) != TIFFTAG_BITSPERSAMPLE //<- these two are tricky - && TIFFFieldTag(fip) != TIFFTAG_COMPRESSION //<- they are defined as TIFF_VARIABLE but in reality return a single value ) { if(TIFFGetField(tif, tag_id, &raw_data) != 1) { // stop, ignore error return TRUE; } } else { int value_size = 0; // access fields as values // Note: // For TIFF_RATIONAL values, TIFFDataWidth() returns 8, but LibTIFF use internaly 4-byte float to represent rationals. { TIFFDataType tag_type = TIFFFieldDataType(fip); switch(tag_type) { case TIFF_RATIONAL: case TIFF_SRATIONAL: value_size = 4; break; default: value_size = TIFFDataWidth(tag_type); break; } } raw_data = _TIFFmalloc(value_size * value_count); mem_alloc = 1; int ok = FALSE; // ### if value_count > 1, tag is PAGENUMBER or HALFTONEHINTS or YCBCRSUBSAMPLING or DOTRANGE, // all off which are value_count == 2 (see tif_dirinfo.c) switch(value_count) { case 1: ok = TIFFGetField(tif, tag_id, raw_data); break; case 2: ok = TIFFGetField(tif, tag_id, raw_data, (BYTE*)(raw_data) + value_size*1); break; /* # we might need more in the future: case 3: ok = TIFFGetField(tif, tag_id, raw_data, (BYTE*)(raw_data) + value_size*1, (BYTE*)(raw_data) + value_size*2); break; */ default: FreeImage_OutputMessageProc(FIF_TIFF, "Unimplemented variable number of parameters for Tiff Tag %s", TIFFFieldName(fip)); break; } if(ok != 1) { _TIFFfree(raw_data); return TRUE; } } } // build FreeImage tag from Tiff Tag data we collected FITAG *fitag = FreeImage_CreateTag(); if(!fitag) { if(mem_alloc) { _TIFFfree(raw_data); } return FALSE; } FreeImage_SetTagID(fitag, (WORD)tag_id); FreeImage_SetTagKey(fitag, key); switch(TIFFFieldDataType(fip)) { case TIFF_BYTE: FreeImage_SetTagType(fitag, FIDT_BYTE); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_UNDEFINED: FreeImage_SetTagType(fitag, FIDT_UNDEFINED); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_SBYTE: FreeImage_SetTagType(fitag, FIDT_SBYTE); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_SHORT: FreeImage_SetTagType(fitag, FIDT_SHORT); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_SSHORT: FreeImage_SetTagType(fitag, FIDT_SSHORT); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_LONG: FreeImage_SetTagType(fitag, FIDT_LONG); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_IFD: FreeImage_SetTagType(fitag, FIDT_IFD); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_SLONG: FreeImage_SetTagType(fitag, FIDT_SLONG); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_RATIONAL: { // LibTIFF converts rational to floats : reconvert floats to rationals DWORD *rvalue = (DWORD*)malloc(2 * value_count * sizeof(DWORD)); for(uint32 i = 0; i < value_count; i++) { float *fv = (float*)raw_data; FIRational rational(fv[i]); rvalue[2*i] = rational.getNumerator(); rvalue[2*i+1] = rational.getDenominator(); } FreeImage_SetTagType(fitag, FIDT_RATIONAL); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, rvalue); free(rvalue); } break; case TIFF_SRATIONAL: { // LibTIFF converts rational to floats : reconvert floats to rationals LONG *rvalue = (LONG*)malloc(2 * value_count * sizeof(LONG)); for(uint32 i = 0; i < value_count; i++) { float *fv = (float*)raw_data; FIRational rational(fv[i]); rvalue[2*i] = rational.getNumerator(); rvalue[2*i+1] = rational.getDenominator(); } FreeImage_SetTagType(fitag, FIDT_RATIONAL); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, rvalue); free(rvalue); } break; case TIFF_FLOAT: FreeImage_SetTagType(fitag, FIDT_FLOAT); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_DOUBLE: FreeImage_SetTagType(fitag, FIDT_DOUBLE); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_LONG8: // BigTIFF 64-bit unsigned integer FreeImage_SetTagType(fitag, FIDT_LONG8); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_IFD8: // BigTIFF 64-bit unsigned integer (offset) FreeImage_SetTagType(fitag, FIDT_IFD8); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_SLONG8: // BigTIFF 64-bit signed integer FreeImage_SetTagType(fitag, FIDT_SLONG8); FreeImage_SetTagLength(fitag, TIFFDataWidth( TIFFFieldDataType(fip) ) * value_count); FreeImage_SetTagCount(fitag, value_count); FreeImage_SetTagValue(fitag, raw_data); break; case TIFF_ASCII: default: { size_t length = 0; if(!mem_alloc && (TIFFFieldDataType(fip) == TIFF_ASCII) && (TIFFFieldReadCount(fip) == TIFF_VARIABLE)) { // when metadata tag is of type ASCII and it's value is of variable size (TIFF_VARIABLE), // tiff_read_exif_tag function gives length of 1 so all strings are truncated ... // ... try to avoid this by using an explicit calculation for 'length' length = strlen((char*)raw_data) + 1; } else { // remember that raw_data = _TIFFmalloc(value_size * value_count); const int value_size = TIFFDataWidth( TIFFFieldDataType(fip) ); length = value_size * value_count; } FreeImage_SetTagType(fitag, FIDT_ASCII); FreeImage_SetTagLength(fitag, (DWORD)length); FreeImage_SetTagCount(fitag, (DWORD)length); FreeImage_SetTagValue(fitag, raw_data); } break; } const char *description = tagLib.getTagDescription(md_model, (WORD)tag_id); if(description) { FreeImage_SetTagDescription(fitag, description); } // store the tag FreeImage_SetMetadata(tagLib.getFreeImageModel(md_model), dib, FreeImage_GetTagKey(fitag), fitag); // destroy the tag FreeImage_DeleteTag(fitag); if(mem_alloc) { _TIFFfree(raw_data); } return TRUE; }