//-------------------------------------------------------------------------- // GPS Lat/Long extraction helper function //-------------------------------------------------------------------------- void CExifParse::GetLatLong( const unsigned int Format, const unsigned char* ValuePtr, const int ComponentSize, char *latLongString) { if (Format != FMT_URATIONAL) { ErrNonfatal("Illegal number format %d for GPS Lat/Long", Format, 0); } else { double Values[3]; for (unsigned a=0; a<3 ;a++) { Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format); } if (Values[0] < 0 || Values[0] > 180 || Values[1] < 0 || Values[1] >= 60 || Values[2] < 0 || Values[2] >= 60) { // Ignore invalid values (DMS format expected) ErrNonfatal("Invalid Lat/Long value", 0, 0); latLongString[0] = 0; } else { char latLong[30]; sprintf(latLong, "%3.0fd %2.0f' %5.2f\"", Values[0], Values[1], Values[2]); strcat(latLongString, latLong); } } }
//-------------------------------------------------------------------------- // GPS Lat/Long extraction helper function //-------------------------------------------------------------------------- void CExifParse::GetLatLong( const unsigned int Format, const unsigned char* ValuePtr, const int ComponentSize, char *latLongString) { if (Format != FMT_URATIONAL) { ErrNonfatal("Illegal number format %d for GPS Lat/Long", Format, 0); } else { double Values[3]; for (unsigned a=0; a<3 ;a++) { Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format); } char latLong[30]; sprintf(latLong, "%3.0fd %2.0f' %5.2f\"", Values[0], Values[1], Values[2]); strcat(latLongString, latLong); } }
//-------------------------------------------------------------------------- // Process one of the nested EXIF directories. //-------------------------------------------------------------------------- void CExifParse::ProcessDir(const unsigned char* const DirStart, const unsigned char* const OffsetBase, const unsigned ExifLength, int NestingLevel) { if (NestingLevel > 4) { ErrNonfatal("Maximum directory nesting exceeded (corrupt exif header)", 0,0); return; } char IndentString[25]; memset(IndentString, ' ', 25); IndentString[NestingLevel * 4] = '\0'; int NumDirEntries = Get16((const void*)DirStart, m_MotorolaOrder); const unsigned char* const DirEnd = DIR_ENTRY_ADDR(DirStart, NumDirEntries); if (DirEnd+4 > (OffsetBase+ExifLength)) { if (DirEnd+2 == OffsetBase+ExifLength || DirEnd == OffsetBase+ExifLength) { // Version 1.3 of jhead would truncate a bit too much. // This also caught later on as well. } else { ErrNonfatal("Illegally sized directory", 0,0); return; } } for (int de=0;de<NumDirEntries;de++) { int Tag, Format, Components; unsigned char* ValuePtr; int ByteCount; const unsigned char* const DirEntry = DIR_ENTRY_ADDR(DirStart, de); Tag = Get16(DirEntry, m_MotorolaOrder); Format = Get16(DirEntry+2, m_MotorolaOrder); Components = Get32(DirEntry+4, m_MotorolaOrder); if (Format <= 0 || Format > NUM_FORMATS) { ErrNonfatal("Illegal number format %d for tag %04x", Format, Tag); continue; } if ((unsigned)Components > 0x10000) { ErrNonfatal("Illegal number of components %d for tag %04x", Components, Tag); continue; } ByteCount = Components * BytesPerFormat[Format - 1]; if (ByteCount > 4) { unsigned OffsetVal; OffsetVal = (unsigned)Get32(DirEntry+8, m_MotorolaOrder); // If its bigger than 4 bytes, the dir entry contains an offset. if (OffsetVal+ByteCount > ExifLength) { // Bogus pointer offset and / or bytecount value ErrNonfatal("Illegal value pointer for tag %04x", Tag,0); continue; } ValuePtr = (unsigned char*)(const_cast<unsigned char*>(OffsetBase)+OffsetVal); if (OffsetVal > m_LargestExifOffset) { m_LargestExifOffset = OffsetVal; } } else { // 4 bytes or less and value is in the dir entry itself ValuePtr = (unsigned char*)(const_cast<unsigned char*>(DirEntry)+8); } // Extract useful components of tag switch(Tag) { case TAG_DESCRIPTION: { int length = max(ByteCount, 0); length = min(length, MAX_COMMENT); strncpy(m_ExifInfo->Description, (char *)ValuePtr, length); m_ExifInfo->Description[length] = '\0'; break; } case TAG_MAKE: { int space = sizeof(m_ExifInfo->CameraMake); if (space > 0) { strncpy(m_ExifInfo->CameraMake, (char *)ValuePtr, space - 1); m_ExifInfo->CameraMake[space - 1] = '\0'; } break; } case TAG_MODEL: { int space = sizeof(m_ExifInfo->CameraModel); if (space > 0) { strncpy(m_ExifInfo->CameraModel, (char *)ValuePtr, space - 1); m_ExifInfo->CameraModel[space - 1] = '\0'; } break; } // case TAG_SOFTWARE: strncpy(m_ExifInfo->Software, ValuePtr, 5); break; case TAG_FOCALPLANEXRES: m_FocalPlaneXRes = ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_OFFSET: m_ExifInfo->ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_LENGTH: m_ExifInfo->ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_MAKER_NOTE: continue; break; case TAG_DATETIME_ORIGINAL: { int space = sizeof(m_ExifInfo->DateTime); if (space > 0) { strncpy(m_ExifInfo->DateTime, (char *)ValuePtr, space - 1); m_ExifInfo->DateTime[space - 1] = '\0'; // If we get a DATETIME_ORIGINAL, we use that one. m_DateFound = true; } break; } case TAG_DATETIME_DIGITIZED: case TAG_DATETIME: { if (!m_DateFound) { // If we don't already have a DATETIME_ORIGINAL, use whatever // time fields we may have. int space = sizeof(m_ExifInfo->DateTime); if (space > 0) { strncpy(m_ExifInfo->DateTime, (char *)ValuePtr, space - 1); m_ExifInfo->DateTime[space - 1] = '\0'; } } break; } case TAG_USERCOMMENT: { // The UserComment allows comments without the charset limitations of ImageDescription. // Therefore the UserComment field is prefixed by a CharacterCode field (8 Byte): // - ASCII: 'ASCII\0\0\0' // - Unicode: 'UNICODE\0' // - JIS X208-1990: 'JIS\0\0\0\0\0' // - Unknown: '\0\0\0\0\0\0\0\0' (application specific) m_ExifInfo->CommentsCharset = EXIF_COMMENT_CHARSET_UNKNOWN; const int EXIF_COMMENT_CHARSET_LENGTH = 8; if (ByteCount >= EXIF_COMMENT_CHARSET_LENGTH) { // As some implementations use spaces instead of \0 for the padding, // we're not so strict and check only the prefix. if (memcmp(ValuePtr, "ASCII", 5) == 0) m_ExifInfo->CommentsCharset = EXIF_COMMENT_CHARSET_ASCII; else if (memcmp(ValuePtr, "UNICODE", 7) == 0) m_ExifInfo->CommentsCharset = EXIF_COMMENT_CHARSET_UNICODE; else if (memcmp(ValuePtr, "JIS", 3) == 0) m_ExifInfo->CommentsCharset = EXIF_COMMENT_CHARSET_JIS; int length = ByteCount - EXIF_COMMENT_CHARSET_LENGTH; length = min(length, MAX_COMMENT); memcpy(m_ExifInfo->Comments, ValuePtr + EXIF_COMMENT_CHARSET_LENGTH, length); m_ExifInfo->Comments[length] = '\0'; // FixComment(comment); // Ensure comment is printable } } break; case TAG_XP_COMMENT: { // The XP user comment field is always unicode (UCS-2) encoded m_ExifInfo->XPCommentsCharset = EXIF_COMMENT_CHARSET_UNICODE; size_t length = min(ByteCount, MAX_COMMENT); memcpy(m_ExifInfo->XPComment, ValuePtr, length); m_ExifInfo->XPComment[length] = '\0'; } break; case TAG_FNUMBER: // Simplest way of expressing aperture, so I trust it the most. // (overwrite previously computd value if there is one) m_ExifInfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: // More relevant info always comes earlier, so only use this field if we don't // have appropriate aperture information yet. if (m_ExifInfo->ApertureFNumber == 0) { m_ExifInfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0)*0.5); } break; case TAG_FOCALLENGTH: // Nice digital cameras actually save the focal length as a function // of how far they are zoomed in. m_ExifInfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: // Inidcates the distacne the autofocus camera is focused to. // Tends to be less accurate as distance increases. { float distance = (float)ConvertAnyFormat(ValuePtr, Format); m_ExifInfo->Distance = distance; } break; case TAG_EXPOSURETIME: { // Simplest way of expressing exposure time, so I trust it most. // (overwrite previously computd value if there is one) float expTime = (float)ConvertAnyFormat(ValuePtr, Format); if (expTime) m_ExifInfo->ExposureTime = expTime; } break; case TAG_SHUTTERSPEED: // More complicated way of expressing exposure time, so only use // this value if we don't already have it from somewhere else. if (m_ExifInfo->ExposureTime == 0) { m_ExifInfo->ExposureTime = (float)(1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0))); } break; case TAG_FLASH: m_ExifInfo->FlashUsed = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_ORIENTATION: m_ExifInfo->Orientation = (int)ConvertAnyFormat(ValuePtr, Format); if (m_ExifInfo->Orientation < 0 || m_ExifInfo->Orientation > 8) { ErrNonfatal("Undefined rotation value %d", m_ExifInfo->Orientation, 0); m_ExifInfo->Orientation = 0; } break; case TAG_EXIF_IMAGELENGTH: case TAG_EXIF_IMAGEWIDTH: // Use largest of height and width to deal with images that have been // rotated to portrait format. { int a = (int)ConvertAnyFormat(ValuePtr, Format); if (m_ExifImageWidth < a) m_ExifImageWidth = a; } break; case TAG_FOCALPLANEUNITS: switch((int)ConvertAnyFormat(ValuePtr, Format)) { // According to the information I was using, 2 means meters. // But looking at the Cannon powershot's files, inches is the only // sensible value. case 1: m_FocalPlaneUnits = 25.4; break; // inch case 2: m_FocalPlaneUnits = 25.4; break; case 3: m_FocalPlaneUnits = 10; break; // centimeter case 4: m_FocalPlaneUnits = 1; break; // millimeter case 5: m_FocalPlaneUnits = .001; break; // micrometer } break; case TAG_EXPOSURE_BIAS: m_ExifInfo->ExposureBias = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: m_ExifInfo->Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_LIGHT_SOURCE: //Quercus: 17-1-2004 Added LightSource, some cams return this, whitebalance or both m_ExifInfo->LightSource = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_METERING_MODE: m_ExifInfo->MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_PROGRAM: m_ExifInfo->ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_INDEX: if (m_ExifInfo->ISOequivalent == 0) { // Exposure index and ISO equivalent are often used interchangeably, // so we will do the same. // http://photography.about.com/library/glossary/bldef_ei.htm m_ExifInfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); } break; case TAG_ISO_EQUIVALENT: m_ExifInfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if (m_ExifInfo->ISOequivalent < 50) m_ExifInfo->ISOequivalent *= 200; // Fixes strange encoding on some older digicams. break; case TAG_EXPOSURE_MODE: m_ExifInfo->ExposureMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_DIGITALZOOMRATIO: m_ExifInfo->DigitalZoomRatio = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXIF_OFFSET: case TAG_INTEROP_OFFSET: { const unsigned char* const SubdirStart = OffsetBase + (unsigned)Get32(ValuePtr, m_MotorolaOrder); if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength) { ErrNonfatal("Illegal exif or interop ofset directory link",0,0); } else { ProcessDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1); } continue; } break; case TAG_GPSINFO: { const unsigned char* const SubdirStart = OffsetBase + (unsigned)Get32(ValuePtr, m_MotorolaOrder); if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength) { ErrNonfatal("Illegal GPS directory link",0,0); } else { ProcessGpsInfo(SubdirStart, ByteCount, OffsetBase, ExifLength); } continue; } break; case TAG_FOCALLENGTH_35MM: // The focal length equivalent 35 mm is a 2.2 tag (defined as of April 2002) // if its present, use it to compute equivalent focal length instead of // computing it from sensor geometry and actual focal length. m_ExifInfo->FocalLength35mmEquiv = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; } } // In addition to linking to subdirectories via exif tags, // there's also a potential link to another directory at the end of each // directory. this has got to be the result of a committee! unsigned Offset; if (DIR_ENTRY_ADDR(DirStart, NumDirEntries) + 4 <= OffsetBase+ExifLength) { Offset = (unsigned)Get32(DirStart+2+12*NumDirEntries, m_MotorolaOrder); if (Offset) { const unsigned char* const SubdirStart = OffsetBase + Offset; if (SubdirStart > OffsetBase+ExifLength || SubdirStart < OffsetBase) { if (SubdirStart > OffsetBase && SubdirStart < OffsetBase+ExifLength+20) { // Jhead 1.3 or earlier would crop the whole directory! // As Jhead produces this form of format incorrectness, // I'll just let it pass silently } else { ErrNonfatal("Illegal subdirectory link",0,0); } } else { if (SubdirStart <= OffsetBase+ExifLength) { ProcessDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1); } } if (Offset > m_LargestExifOffset) { m_LargestExifOffset = Offset; } } } else { // The exif header ends before the last next directory pointer. } if (m_ExifInfo->ThumbnailOffset) { m_ExifInfo->ThumbnailAtEnd = false; if (m_ExifInfo->ThumbnailOffset <= ExifLength) { if (m_ExifInfo->ThumbnailSize > ExifLength - m_ExifInfo->ThumbnailOffset) { // If thumbnail extends past exif header, only save the part that // actually exists. Canon's EOS viewer utility will do this - the // thumbnail extracts ok with this hack. m_ExifInfo->ThumbnailSize = ExifLength - m_ExifInfo->ThumbnailOffset; } } } }
/*-------------------------------------------------------------------------- Process one of the nested EXIF directories. --------------------------------------------------------------------------*/ bool CxImageJPG::CxExifInfo::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const pInfo, unsigned char ** const LastExifRefdP ) { int de; int a; int NumDirEntries; unsigned ThumbnailOffset = 0; unsigned ThumbnailSize = 0; NumDirEntries = Get16u(DirStart); if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)){ strcpy(m_szLastError,"Illegally sized directory"); return false; } for (de=0;de<NumDirEntries;de++){ int Tag, Format, Components; unsigned char * ValuePtr; /* This actually can point to a variety of things; it must be cast to other types when used. But we use it as a byte-by-byte cursor, so we declare it as a pointer to a generic byte here. */ int ByteCount; unsigned char * DirEntry; DirEntry = DirStart+2+12*de; Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { /* (-1) catches illegal zero case as unsigned underflows to positive large. */ strcpy(m_szLastError,"Illegal format code in EXIF dir"); return false; } ByteCount = Components * BytesPerFormat[Format]; if (ByteCount > 4){ unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); /* If its bigger than 4 bytes, the dir entry contains an offset.*/ if (OffsetVal+ByteCount > ExifLength){ /* Bogus pointer offset and / or bytecount value */ strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return false; } ValuePtr = OffsetBase+OffsetVal; }else{ /* 4 bytes or less and value is in the dir entry itself */ ValuePtr = DirEntry+8; } if (*LastExifRefdP < ValuePtr+ByteCount){ /* Keep track of last byte in the exif header that was actually referenced. That way, we know where the discardable thumbnail data begins. */ *LastExifRefdP = ValuePtr+ByteCount; } /* Extract useful components of tag */ switch(Tag){ case TAG_MAKE: strncpy(pInfo->CameraMake, (char*)ValuePtr, 31); break; case TAG_MODEL: strncpy(pInfo->CameraModel, (char*)ValuePtr, 39); break; case TAG_DATETIME_ORIGINAL: strncpy(pInfo->DateTime, (char*)ValuePtr, 19); break; case TAG_USERCOMMENT: /* Olympus has this padded with trailing spaces. Remove these first. */ for (a=ByteCount;;){ a--; if (((char*)ValuePtr)[a] == ' '){ ((char*)ValuePtr)[a] = '\0'; }else{ break; } if (a == 0) break; } /* Copy the comment */ if (memcmp(ValuePtr, "ASCII",5) == 0){ for (a=5;a<10;a++){ char c; c = ((char*)ValuePtr)[a]; if (c != '\0' && c != ' '){ strncpy(pInfo->Comments, (char*)ValuePtr+a, 199); break; } } }else{ strncpy(pInfo->Comments, (char*)ValuePtr, 199); } break; case TAG_FNUMBER: /* Simplest way of expressing aperture, so I trust it the most. (overwrite previously computd value if there is one) */ pInfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: /* More relevant info always comes earlier, so only use this field if we don't have appropriate aperture information yet. */ if (pInfo->ApertureFNumber == 0){ pInfo->ApertureFNumber = (float) exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5); } break; case TAG_FOCALLENGTH: /* Nice digital cameras actually save the focal length as a function of how farthey are zoomed in. */ pInfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: /* Inidcates the distacne the autofocus camera is focused to. Tends to be less accurate as distance increases. */ pInfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURETIME: /* Simplest way of expressing exposure time, so I trust it most. (overwrite previously computd value if there is one) */ pInfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SHUTTERSPEED: /* More complicated way of expressing exposure time, so only use this value if we don't already have it from somewhere else. */ if (pInfo->ExposureTime == 0){ pInfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2))); } break; case TAG_FLASH: if (ConvertAnyFormat(ValuePtr, Format)){ pInfo->FlashUsed = 1; } break; case TAG_EXIF_IMAGELENGTH: case TAG_EXIF_IMAGEWIDTH: /* Use largest of height and width to deal with images that have been rotated to portrait format. */ a = (int)ConvertAnyFormat(ValuePtr, Format); if (ExifImageWidth < a) ExifImageWidth = a; break; case TAG_FOCALPLANEXRES: FocalplaneXRes = ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEUNITS: switch((int)ConvertAnyFormat(ValuePtr, Format)){ case 1: FocalplaneUnits = 25.4; break; /* 1 inch */ case 2: /* According to the information I was using, 2 means meters. But looking at the Cannon powershot's files, inches is the only sensible value. */ FocalplaneUnits = 25.4; break; case 3: FocalplaneUnits = 10; break; /* 1 centimeter*/ case 4: FocalplaneUnits = 1; break; /* 1 millimeter*/ case 5: FocalplaneUnits = .001; break; /* 1 micrometer*/ } break; /* Remaining cases contributed by: Volker C. Schoech ([email protected]) */ case TAG_EXPOSURE_BIAS: pInfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: pInfo->Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_METERING_MODE: pInfo->MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_PROGRAM: pInfo->ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_ISO_EQUIVALENT: pInfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if ( pInfo->ISOequivalent < 50 ) pInfo->ISOequivalent *= 200; break; case TAG_COMPRESSION_LEVEL: pInfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_OFFSET: ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_LENGTH: ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; } if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET){ unsigned char * SubdirStart; SubdirStart = OffsetBase + Get32u(ValuePtr); if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength){ strcpy(m_szLastError,"Illegal subdirectory link"); return false; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, pInfo, LastExifRefdP); continue; } } { /* In addition to linking to subdirectories via exif tags, there's also a potential link to another directory at the end of each directory. This has got to be the result of a committee! */ unsigned char * SubdirStart; unsigned Offset; Offset = Get16u(DirStart+2+12*NumDirEntries); if (Offset){ SubdirStart = OffsetBase + Offset; if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength){ strcpy(m_szLastError,"Illegal subdirectory link"); return false; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, pInfo, LastExifRefdP); } } if (ThumbnailSize && ThumbnailOffset){ if (ThumbnailSize + ThumbnailOffset <= ExifLength){ /* The thumbnail pointer appears to be valid. Store it. */ pInfo->ThumbnailPointer = OffsetBase + ThumbnailOffset; pInfo->ThumbnailSize = ThumbnailSize; } } return true; }
//-------------------------------------------------------------------------- // Process GPS info directory //-------------------------------------------------------------------------- void ProcessGpsInfo(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength) { int de; unsigned a; int NumDirEntries; NumDirEntries = Get16u(DirStart); #define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry)) if (ShowTags){ xprintf("(dir has %d entries)\n",NumDirEntries); } ImageInfo.GpsInfoPresent = TRUE; strcpy(ImageInfo.GpsLat, "? ?"); strcpy(ImageInfo.GpsLong, "? ?"); ImageInfo.GpsAlt[0] = 0; for (de=0;de<NumDirEntries;de++){ unsigned Tag, Format, Components; unsigned char * ValuePtr; int ComponentSize; unsigned ByteCount; unsigned char * DirEntry; DirEntry = DIR_ENTRY_ADDR(DirStart, de); if (DirEntry+12 > OffsetBase+ExifLength){ ErrNonfatal("GPS info directory goes past end of exif",0,0); return; } Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { // (-1) catches illegal zero case as unsigned underflows to positive large. ErrNonfatal("Illegal number format %d for Exif gps tag %04x", Format, Tag); continue; } ComponentSize = BytesPerFormat[Format]; ByteCount = Components * ComponentSize; if (ByteCount > 4){ unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); // If its bigger than 4 bytes, the dir entry contains an offset. if (OffsetVal+ByteCount > ExifLength){ // Bogus pointer offset and / or bytecount value ErrNonfatal("Illegal value pointer for Exif gps tag %04x", Tag,0); continue; } ValuePtr = OffsetBase+OffsetVal; }else{ // 4 bytes or less and value is in the dir entry itself ValuePtr = DirEntry+8; } switch(Tag){ char FmtString[21]; char TempString[50]; double Values[3]; case TAG_GPS_LAT_REF: ImageInfo.GpsLat[0] = ValuePtr[0]; break; case TAG_GPS_LONG_REF: ImageInfo.GpsLong[0] = ValuePtr[0]; break; case TAG_GPS_LAT: case TAG_GPS_LONG: if (Format != FMT_URATIONAL){ ErrNonfatal("Inappropriate format (%d) for Exif GPS coordinates!", Format, 0); } strcpy(FmtString, "%0.0fd %0.0fm %0.0fs"); for (a=0;a<3;a++){ int den, digits; den = Get32s(ValuePtr+4+a*ComponentSize); digits = 0; while (den > 1 && digits <= 6){ den = den / 10; digits += 1; } if (digits > 6) digits = 6; FmtString[1+a*7] = (char)('2'+digits+(digits ? 1 : 0)); FmtString[3+a*7] = (char)('0'+digits); Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format); } sprintf(TempString, FmtString, Values[0], Values[1], Values[2]); if (Tag == TAG_GPS_LAT){ strncpy(ImageInfo.GpsLat+2, TempString, 29); }else{ strncpy(ImageInfo.GpsLong+2, TempString, 29); } break; case TAG_GPS_ALT_REF: ImageInfo.GpsAlt[0] = (char)(ValuePtr[0] ? '-' : ' '); break; case TAG_GPS_ALT: sprintf(ImageInfo.GpsAlt + 1, "%.2fm", ConvertAnyFormat(ValuePtr, Format)); break; } if (ShowTags){ // Show tag value. if (Tag < MAX_GPS_TAG){ xprintf(" GPS%s =", GpsTags[Tag]); }else{ // Show unknown tag xprintf(" Illegal GPS tag %04x=", Tag); } switch(Format){ case FMT_UNDEFINED: // Undefined is typically an ascii string. case FMT_STRING: // String arrays printed without function call (different from int arrays) { xprintf("\""); for (a=0;a<ByteCount;a++){ int ZeroSkipped = 0; if (ValuePtr[a] >= 32){ if (ZeroSkipped){ xprintf("?"); ZeroSkipped = 0; } putchar(ValuePtr[a]); }else{ if (ValuePtr[a] == 0){ ZeroSkipped = 1; } } } xprintf("\"\n"); } break; default: // Handle arrays of numbers later (will there ever be?) for (a=0;;){ PrintFormatNumber(ValuePtr+a*ComponentSize, Format, ByteCount); if (++a >= Components) break; xprintf(", "); } xprintf("\n"); } } } }
bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP ) { int de, a, NumDirEntries; unsigned ThumbnailOffset = 0; unsigned ThumbnailSize = 0; NumDirEntries = Get16u(DirStart); if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)) { strcpy(m_szLastError,"Illegally sized directory"); return 0; } for (de=0;de<NumDirEntries;de++) { int Tag, Format, Components; unsigned char * ValuePtr; int BytesCount; unsigned char * DirEntry; DirEntry = DirStart+2+12*de; Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0; } BytesCount = Components * BytesPerFormat[Format]; if (BytesCount > 4) { unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); if (OffsetVal+BytesCount > ExifLength) { strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0; } ValuePtr = OffsetBase+OffsetVal; } else ValuePtr = DirEntry+8; if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount; switch(Tag) { case TAG_MAKE: strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31); break; case TAG_MODEL: strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39); break; case TAG_EXIF_VERSION: strncpy(m_exifinfo->Version,(char*)ValuePtr, 4); break; case TAG_DATETIME_ORIGINAL: strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19); break; case TAG_USERCOMMENT: for (a=BytesCount;;) { a--; if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0'; else break; if (a == 0) break; } if (memcmp(ValuePtr, "ASCII",5) == 0) { for (a=5;a<10;a++) { char c; c = ((char*)ValuePtr)[a]; if (c != '\0' && c != ' ') { strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199); break; } } } else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199); break; case TAG_FNUMBER: m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: if (m_exifinfo->ApertureFNumber == 0) { //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5); } break; case TAG_BRIGHTNESS: m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALLENGTH: m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURETIME: m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SHUTTERSPEED: if (m_exifinfo->ExposureTime == 0) { //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2))); } break; case TAG_FLASH: if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire"); else strcpy(m_exifinfo->FlashUsed,"not fired"); break; case TAG_ORIENTATION: m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format); switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 1: strcpy(m_exifinfo->Orientation,"Top-Left"); break; case 2: strcpy(m_exifinfo->Orientation,"Top-Right"); break; case 3: strcpy(m_exifinfo->Orientation,"Bottom-Right"); break; case 4: strcpy(m_exifinfo->Orientation,"Bottom-Left"); break; case 5: strcpy(m_exifinfo->Orientation,"Left-Top"); break; case 6: strcpy(m_exifinfo->Orientation,"Right-Top"); break; case 7: strcpy(m_exifinfo->Orientation,"Right-Bottom"); break; case 8: strcpy(m_exifinfo->Orientation,"Left-Bottom"); break; default: strcpy(m_exifinfo->Orientation,"Undefined"); break; } break; case TAG_EXIF_IMAGELENGTH: case TAG_EXIF_IMAGEWIDTH: a = (int)ConvertAnyFormat(ValuePtr, Format); if (ExifImageWidth < a) ExifImageWidth = a; break; case TAG_FOCALPLANEXRES: m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEYRES: m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_RESOLUTIONUNIT: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 2: strcpy(m_exifinfo->ResolutionUnit,"inches"); break; case 3: strcpy(m_exifinfo->ResolutionUnit,"centimeters"); break; default: strcpy(m_exifinfo->ResolutionUnit,"reserved"); } break; case TAG_FOCALPLANEUNITS: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 1: m_exifinfo->FocalplaneUnits = 1.0f; break; case 2: m_exifinfo->FocalplaneUnits = 1.0f; break; case 3: m_exifinfo->FocalplaneUnits = 0.3937007874f; break; case 4: m_exifinfo->FocalplaneUnits = 0.03937007874f; break; case 5: m_exifinfo->FocalplaneUnits = 0.00003937007874f; } break; case TAG_EXPOSURE_BIAS: m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 0: strcpy(m_exifinfo->LightSource,"unknown"); break; case 1: strcpy(m_exifinfo->LightSource,"Daylight"); break; case 2: strcpy(m_exifinfo->LightSource,"Fluorescent"); break; case 3: strcpy(m_exifinfo->LightSource,"Tungsten"); break; case 17: strcpy(m_exifinfo->LightSource,"Standard light A"); break; case 18: strcpy(m_exifinfo->LightSource,"Standard light B"); break; case 19: strcpy(m_exifinfo->LightSource,"Standard light C"); break; case 20: strcpy(m_exifinfo->LightSource,"D55"); break; case 21: strcpy(m_exifinfo->LightSource,"D65"); break; case 22: strcpy(m_exifinfo->LightSource,"D75"); break; default: strcpy(m_exifinfo->LightSource,"other"); break; } break; case TAG_METERING_MODE: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 0: strcpy(m_exifinfo->MeteringMode,"unknown"); break; case 1: strcpy(m_exifinfo->MeteringMode,"Average"); break; case 2: strcpy(m_exifinfo->MeteringMode,"Center-Weighted-Average"); break; case 3: strcpy(m_exifinfo->MeteringMode,"Spot"); break; case 4: strcpy(m_exifinfo->MeteringMode,"MultiSpot"); break; case 5: strcpy(m_exifinfo->MeteringMode,"Pattern"); break; case 6: strcpy(m_exifinfo->MeteringMode,"Partial"); break; default: strcpy(m_exifinfo->MeteringMode,"other"); break; } break; case TAG_EXPOSURE_PROGRAM: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 0: strcpy(m_exifinfo->ExposureProgram,"not defined"); break; case 1: strcpy(m_exifinfo->ExposureProgram,"Manual"); break; case 2: strcpy(m_exifinfo->ExposureProgram,"Normal program"); break; case 3: strcpy(m_exifinfo->ExposureProgram,"Aperture priority"); break; case 4: strcpy(m_exifinfo->ExposureProgram,"Shutter priority"); break; case 5: strcpy(m_exifinfo->ExposureProgram,"Creative program"); break; case 6: strcpy(m_exifinfo->ExposureProgram,"Action program"); break; case 7: strcpy(m_exifinfo->ExposureProgram,"Portrait mode"); break; case 8: strcpy(m_exifinfo->ExposureProgram,"Landscape mode"); break; default: strcpy(m_exifinfo->ExposureProgram,"reserved"); break; } break; case TAG_ISO_EQUIVALENT: m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200; break; case TAG_COMPRESSION_LEVEL: m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_XRESOLUTION: m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_YRESOLUTION: m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_OFFSET: ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_LENGTH: ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; } if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET) { unsigned char * SubdirStart; SubdirStart = OffsetBase + Get32u(ValuePtr); if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength) { strcpy(m_szLastError,"Illegal subdirectory link"); return 0; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP); continue; } } unsigned char * SubdirStart; unsigned Offset; Offset = Get16u(DirStart+2+12*NumDirEntries); if (Offset) { SubdirStart = OffsetBase + Offset; if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength) { strcpy(m_szLastError,"Illegal subdirectory link"); return 0; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP); } if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate) { if (ThumbnailSize + ThumbnailOffset <= ExifLength) { if(FILE *tf = fopen(THUMBNAILTMPFILE, "w")) { fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf); fclose(tf); m_exifinfo->Thumnailstate = 2; } } } return 1; }
/*-------------------------------------------------------------------------- Process one of the nested EXIF directories. --------------------------------------------------------------------------*/ bool CExifRead::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned int ExifLength, unsigned char ** const LastExifRefdP ) { int de; int a; int NumDirEntries; unsigned ThumbnailOffset = 0; unsigned ThumbnailSize = 0; NumDirEntries = Get16u(DirStart); if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)) { #ifdef GetErrorCode strcpy(m_szLastError,"Illegally sized directory"); #endif return 0; } for (de=0;de<NumDirEntries;de++){ int Tag, Format, Components; unsigned char * ValuePtr; /* This actually can point to a variety of things; it must be cast to other types when used. But we use it as a unsigned char-by-unsigned char cursor, so we declare it as a pointer to a generic unsigned char here. */ int BytesCount; unsigned char * DirEntry; DirEntry = DirStart+2+12*de; Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { #ifdef GetErrorCode /* (-1) catches illegal zero case as unsigned underflows to positive large */ strcpy(m_szLastError,"Illegal format code in EXIF dir"); #endif return 0; } BytesCount = Components * BytesPerFormat[Format]; if (BytesCount > 4){ unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); /* If its bigger than 4 unsigned chars, the dir entry contains an offset.*/ if (OffsetVal+BytesCount > ExifLength) { #ifdef GetErrorCode /* Bogus pointer offset and / or unsigned charcount value */ strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); #endif return 0; } ValuePtr = OffsetBase+OffsetVal; } else { /* 4 unsigned chars or less and value is in the dir entry itself */ ValuePtr = DirEntry+8; } if (*LastExifRefdP < ValuePtr+BytesCount) { /* Keep track of last unsigned char in the exif header that was actually referenced. That way, we know where the discardable thumbnail data begins. */ *LastExifRefdP = ValuePtr+BytesCount; } /* Extract useful components of tag */ switch(Tag) { case TAG_MAKE: if ( m_flags & EXIF_CAMERA_MAKER) strncpy(m_pExifInfo->CameraMake, (char*)ValuePtr, 31); break; case TAG_MODEL: if ( m_flags & EXIF_CAMERA_MODEL) strncpy_s(m_pExifInfo->CameraModel,40, (char*)ValuePtr, 39); break; case TAG_EXIF_VERSION: if ( m_flags & EXIF_VERSION) strncpy_s(m_pExifInfo->Version,5,(char*)ValuePtr, 4); break; case TAG_DATETIME_ORIGINAL: if ( m_flags & EXIF_DATA_TIME) strncpy_s(m_pExifInfo->DateTime,20, (char*)ValuePtr, 19); break; case TAG_USERCOMMENT: if ( m_flags & EXIF_USER_COMMENT) { // Olympus has this padded with trailing spaces. Remove these first. for ( a=BytesCount; ;) { a--; if (((char*)ValuePtr)[a] == ' ') { ((char*)ValuePtr)[a] = '\0'; } else { break; } if (a == 0) break; } /* Copy the comment */ if (memcmp(ValuePtr, "ASCII",5) == 0) { for (a=5;a<10;a++) { char c; c = ((char*)ValuePtr)[a]; if (c != '\0' && c != ' ') { strncpy_s(m_pExifInfo->Comments,MAX_COMMENT, (char*)ValuePtr+a, 199); break; } } }else { strncpy_s(m_pExifInfo->Comments,MAX_COMMENT, (char*)ValuePtr, 199); } } break; case TAG_FNUMBER: /* Simplest way of expressing aperture, so I trust it the most. (overwrite previously computd value if there is one) */ if ( m_flags & EXIF_APERTUREF_NUMBER) m_pExifInfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: /* More relevant info always comes earlier, so only use this field if we don't have appropriate aperture information yet. */ if ((m_flags & EXIF_APERTUREF_NUMBER) && m_pExifInfo->ApertureFNumber == 0) { m_pExifInfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0)*0.5); } break; case TAG_BRIGHTNESS: if ( m_flags & EXIF_BRIGHTNESS) m_pExifInfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALLENGTH: /* Nice digital cameras actually save the focal length as a function of how farthey are zoomed in. */ if ( m_flags & EXIF_FOCAL_LENGTH) m_pExifInfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: /* Inidcates the distacne the autofocus camera is focused to. Tends to be less accurate as distance increases. */ if ( m_flags & EXIF_DISTANCE) m_pExifInfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURETIME: /* Simplest way of expressing exposure time, so I trust it most. (overwrite previously computd value if there is one) */ if ( m_flags & EXIF_EXPOSURE_TIME) m_pExifInfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SHUTTERSPEED: /* More complicated way of expressing exposure time, so only use this value if we don't already have it from somewhere else. */ if (( m_flags & EXIF_EXPOSURE_TIME) && m_pExifInfo->ExposureTime == 0) { m_pExifInfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0))); } break; case TAG_FLASH: if ( m_flags & EXIF_FLASH_USED) { if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) { m_pExifInfo->FlashUsed = 1; } else { m_pExifInfo->FlashUsed = 0; } } break; case TAG_ORIENTATION: if ( m_flags & EXIF_ORIENTATION) { m_pExifInfo->Orientation = (int)ConvertAnyFormat(ValuePtr, Format); if (m_pExifInfo->Orientation < 1 || m_pExifInfo->Orientation > 8) { #ifdef GetErrorCode strcpy(m_szLastError,"Undefined rotation value"); #endif m_pExifInfo->Orientation = 0; } } break; case TAG_EXIF_IMAGELENGTH: case TAG_EXIF_IMAGEWIDTH: /* Use largest of height and width to deal with images that have been rotated to portrait format. */ a = (int)ConvertAnyFormat(ValuePtr, Format); if (m_ExifImageWidth < a) m_ExifImageWidth = a; break; case TAG_FOCALPLANEXRES: if ( m_flags & EXIF_FOCALPLANE_XRES) m_pExifInfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEYRES: if ( m_flags & EXIF_FOCALPLANE_YRES) m_pExifInfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_RESOLUTIONUNIT: if ( m_flags & EXIF_RESOLUTION_UNITS) { switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 1: m_pExifInfo->ResolutionUnit = 1.0f; break; /* 1 inch */ case 2: m_pExifInfo->ResolutionUnit = 1.0f; break; case 3: m_pExifInfo->ResolutionUnit = 0.3937007874f; break; /* 1 centimeter*/ case 4: m_pExifInfo->ResolutionUnit = 0.03937007874f; break; /* 1 millimeter*/ case 5: m_pExifInfo->ResolutionUnit = 0.00003937007874f; /* 1 micrometer*/ } } break; case TAG_FOCALPLANEUNITS: if ( m_flags & EXIF_FOCALPLANE_UNITS) { switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 1: m_pExifInfo->FocalplaneUnits = 1.0f; break; /* 1 inch */ case 2: m_pExifInfo->FocalplaneUnits = 1.0f; break; case 3: m_pExifInfo->FocalplaneUnits = 0.3937007874f; break; /* 1 centimeter*/ case 4: m_pExifInfo->FocalplaneUnits = 0.03937007874f; break; /* 1 millimeter*/ case 5: m_pExifInfo->FocalplaneUnits = 0.00003937007874f; /* 1 micrometer*/ } } break; // Remaining cases contributed by: Volker C. Schoech <schoech(at)gmx(dot)de> case TAG_EXPOSURE_BIAS: if ( m_flags & EXIF_EXPOSURE_BIAS) m_pExifInfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: if ( m_flags & EXIF_WHITE_BALANCE) m_pExifInfo->Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_METERING_MODE: if ( m_flags & EXIF_METERING_MODE) m_pExifInfo->MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_PROGRAM: if ( m_flags & EXIF_EXPOSURE_PROGRAM) m_pExifInfo->ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_ISO_EQUIVALENT: if ( m_flags & EXIF_ISO_EAUIVALENT) { m_pExifInfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if ( m_pExifInfo->ISOequivalent < 50 ) m_pExifInfo->ISOequivalent *= 200; } break; case TAG_COMPRESSION_LEVEL: if ( m_flags & EXIF_COMPRESSION_LEVEL) m_pExifInfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_XRESOLUTION: if ( m_flags & EXIF_XRESOLUTION) m_pExifInfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_YRESOLUTION: if ( m_flags & EXIF_YRESOLUTION) m_pExifInfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_OFFSET: ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_LENGTH: ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; } if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET ) { unsigned char * SubdirStart; SubdirStart = OffsetBase + Get32u(ValuePtr); if ( SubdirStart < OffsetBase || SubdirStart > OffsetBase + ExifLength ) { #ifdef GetErrorCode strcpy(m_szLastError,"Illegal subdirectory link"); #endif return 0; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, LastExifRefdP); continue; } } { /* In addition to linking to subdirectories via exif tags, there's also a potential link to another directory at the end of each directory. This has got to be the result of a committee! */ unsigned char * SubdirStart; unsigned Offset; Offset = Get32u(DirStart+2+12*NumDirEntries); if (Offset > 0) { SubdirStart = OffsetBase + Offset; if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength) { #ifdef GetErrorCode strcpy(m_szLastError,"Illegal subdirectory link"); #endif return 0; } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, LastExifRefdP); } } if (ThumbnailSize && ThumbnailOffset) { if (ThumbnailSize + ThumbnailOffset <= ExifLength) { if ( m_pExifInfo->pThumbnailPointer != NULL) { delete m_pExifInfo->pThumbnailPointer; } /* The thumbnail pointer appears to be valid. Store it. */ m_pExifInfo->pThumbnailPointer = OffsetBase + ThumbnailOffset; m_pExifInfo->ThumbnailOffset = ThumbnailOffset; m_pExifInfo->ThumbnailSize = ThumbnailSize; } } return 1; }
//-------------------------------------------------------------------------- // Process GPS info directory //-------------------------------------------------------------------------- void ProcessGpsInfo(unsigned char * DirStart, int ByteCountUnused, unsigned char * OffsetBase, unsigned ExifLength) { int de; unsigned a; int NumDirEntries; NumDirEntries = Get16u(DirStart); #define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry)) if (ShowTags){ printf("(dir has %d entries)\n",NumDirEntries); } ImageInfo.GpsInfoPresent = TRUE; strcpy(ImageInfo.GpsLat, "? ?"); strcpy(ImageInfo.GpsLong, "? ?"); ImageInfo.GpsAlt[0] = 0; for (de=0;de<NumDirEntries;de++){ unsigned Tag, Format, Components; unsigned char * ValuePtr; int ComponentSize; unsigned ByteCount; unsigned char * DirEntry; DirEntry = DIR_ENTRY_ADDR(DirStart, de); if (DirEntry+12 > OffsetBase+ExifLength){ ErrNonfatal("GPS info directory goes past end of exif",0,0); return; } Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { // (-1) catches illegal zero case as unsigned underflows to positive large. ErrNonfatal("Illegal number format %d for tag %04x", Format, Tag); continue; } ComponentSize = BytesPerFormat[Format]; ByteCount = Components * ComponentSize; #ifdef SUPERDEBUG printf("GPS tag %x format %s #components %d componentsize %d bytecount %d", Tag, formatStr(Format), Components, ComponentSize, ByteCount); #endif if (ByteCount > 4){ unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); // If its bigger than 4 bytes, the dir entry contains an offset. if (OffsetVal+ByteCount > ExifLength){ // Bogus pointer offset and / or bytecount value ErrNonfatal("Illegal value pointer for tag %04x", Tag,0); continue; } ValuePtr = OffsetBase+OffsetVal; }else{ // 4 bytes or less and value is in the dir entry itself ValuePtr = DirEntry+8; } switch(Tag){ char FmtString[21]; char TempString[56]; double Values[3]; case TAG_GPS_LAT_REF: ImageInfo.GpsLat[0] = ValuePtr[0]; ImageInfo.GpsLatRef[0] = ValuePtr[0]; ImageInfo.GpsLatRef[1] = '\0'; break; case TAG_GPS_LONG_REF: ImageInfo.GpsLong[0] = ValuePtr[0]; ImageInfo.GpsLongRef[0] = ValuePtr[0]; ImageInfo.GpsLongRef[1] = '\0'; break; case TAG_GPS_LAT: case TAG_GPS_LONG: if (Format != FMT_URATIONAL){ ErrNonfatal("Inappropriate format (%d) for GPS coordinates!", Format, 0); } strcpy(FmtString, "%0.0fd %0.0fm %0.0fs"); for (a=0;a<3;a++){ int den, digits; den = Get32s(ValuePtr+4+a*ComponentSize); digits = 0; while (den > 1 && digits <= 6){ den = den / 10; digits += 1; } if (digits > 6) digits = 6; FmtString[1+a*7] = (char)('2'+digits+(digits ? 1 : 0)); FmtString[3+a*7] = (char)('0'+digits); Values[a] = ConvertAnyFormat(ValuePtr+a*ComponentSize, Format); } sprintf(TempString, FmtString, Values[0], Values[1], Values[2]); if (Tag == TAG_GPS_LAT){ strncpy(ImageInfo.GpsLat+2, TempString, 29); }else{ strncpy(ImageInfo.GpsLong+2, TempString, 29); } sprintf(TempString, "%d/%d,%d/%d,%d/%d", Get32s(ValuePtr), Get32s(4+(char*)ValuePtr), Get32s(8+(char*)ValuePtr), Get32s(12+(char*)ValuePtr), Get32s(16+(char*)ValuePtr), Get32s(20+(char*)ValuePtr)); if (Tag == TAG_GPS_LAT){ strncpy(ImageInfo.GpsLatRaw, TempString, 31); }else{ strncpy(ImageInfo.GpsLongRaw, TempString, 31); } break; case TAG_GPS_ALT_REF: ImageInfo.GpsAlt[0] = (char)(ValuePtr[0] ? '-' : ' '); ImageInfo.GpsAltRef = (char)ValuePtr[0]; break; case TAG_GPS_ALT: sprintf(ImageInfo.GpsAlt + 1, "%.2fm", ConvertAnyFormat(ValuePtr, Format)); ImageInfo.GpsAltRaw.num = Get32u(ValuePtr); ImageInfo.GpsAltRaw.denom = Get32u(4+(char *)ValuePtr); break; case TAG_GPS_TIMESTAMP: snprintf(ImageInfo.GpsTimeStamp, sizeof(ImageInfo.GpsTimeStamp), "%d:%d:%d", (int) ConvertAnyFormat(ValuePtr, Format), (int) ConvertAnyFormat(ValuePtr + 8, Format), (int) ConvertAnyFormat(ValuePtr + 16, Format) ); break; case TAG_GPS_DATESTAMP: strncpy(ImageInfo.GpsDateStamp, (char*)ValuePtr, sizeof(ImageInfo.GpsDateStamp)); break; case TAG_GPS_PROCESSING_METHOD: if (ByteCount > EXIF_ASCII_PREFIX_LEN && memcmp(ValuePtr, ExifAsciiPrefix, EXIF_ASCII_PREFIX_LEN) == 0) { int length = ByteCount < GPS_PROCESSING_METHOD_LEN + EXIF_ASCII_PREFIX_LEN ? ByteCount - EXIF_ASCII_PREFIX_LEN : GPS_PROCESSING_METHOD_LEN; memcpy(ImageInfo.GpsProcessingMethod, (char*)(ValuePtr + EXIF_ASCII_PREFIX_LEN), length); ImageInfo.GpsProcessingMethod[length] = 0; } else { ALOGW("Unsupported encoding for GPSProcessingMethod"); } break; } if (ShowTags){ // Show tag value. if (Tag < MAX_GPS_TAG){ printf(" %s =", GpsTags[Tag].Desc); }else{ // Show unknown tag printf(" Illegal GPS tag %04x=", Tag); } switch(Format){ case FMT_UNDEFINED: // Undefined is typically an ascii string. case FMT_STRING: // String arrays printed without function call (different from int arrays) { printf("\""); for (a=0;a<ByteCount;a++){ int ZeroSkipped = 0; if (ValuePtr[a] >= 32){ if (ZeroSkipped){ printf("?"); ZeroSkipped = 0; } putchar(ValuePtr[a]); }else{ if (ValuePtr[a] == 0){ ZeroSkipped = 1; } } } printf("\"\n"); } break; default: // Handle arrays of numbers later (will there ever be?) for (a=0;;){ PrintFormatNumber(ValuePtr+a*ComponentSize, Format, ByteCount); if (++a >= Components) break; printf(", "); } printf("\n"); } } } }
//-------------------------------------------------------------------------- // Process one of the nested EXIF directories. //-------------------------------------------------------------------------- void ExifData::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, unsigned NestingLevel) { int de; int a; int NumDirEntries; unsigned ThumbnailOffset = 0; unsigned ThumbnailSize = 0; if ( NestingLevel > 4) throw FatalError("Maximum directory nesting exceeded (corrupt exif header)"); NumDirEntries = Get16u(DirStart); #define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry)) { unsigned char * DirEnd; DirEnd = DIR_ENTRY_ADDR(DirStart, NumDirEntries); if (DirEnd+4 > (OffsetBase+ExifLength)) { if (DirEnd+2 == OffsetBase+ExifLength || DirEnd == OffsetBase+ExifLength) { // Version 1.3 of jhead would truncate a bit too much. // This also caught later on as well. } else { // Note: Files that had thumbnails trimmed with jhead 1.3 or earlier // might trigger this. throw FatalError("Illegally sized directory"); } } if (DirEnd < LastExifRefd) LastExifRefd = DirEnd; } for (de=0; de<NumDirEntries; de++) { int Tag, Format, Components; unsigned char * ValuePtr; unsigned ByteCount; char * DirEntry; DirEntry = (char *)DIR_ENTRY_ADDR(DirStart, de); Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { // (-1) catches illegal zero case as unsigned underflows to positive large. throw FatalError("Illegal format code in EXIF dir"); } if ((unsigned)Components > 0x10000) { throw FatalError("Illegal number of components for tag"); continue; } ByteCount = Components * BytesPerFormat[Format]; if (ByteCount > 4) { unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); // If its bigger than 4 bytes, the dir entry contains an offset. if (OffsetVal+ByteCount > ExifLength) { // Bogus pointer offset and / or bytecount value //printf("Offset %d bytes %d ExifLen %d\n",OffsetVal, ByteCount, ExifLength); throw FatalError("Illegal pointer offset value in EXIF"); } ValuePtr = OffsetBase+OffsetVal; } else { // 4 bytes or less and value is in the dir entry itself ValuePtr = (unsigned char *)DirEntry+8; } if (LastExifRefd < ValuePtr+ByteCount) { // Keep track of last byte in the exif header that was actually referenced. // That way, we know where the discardable thumbnail data begins. LastExifRefd = ValuePtr+ByteCount; } // Extract useful components of tag switch(Tag) { case TAG_MAKE: ExifData::CameraMake = QString::fromLatin1((const char*)ValuePtr, 31); break; case TAG_MODEL: ExifData::CameraModel = QString::fromLatin1((const char*)ValuePtr, 39); break; case TAG_ORIENTATION: Orientation = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_DATETIME_ORIGINAL: DateTime = QString::fromLatin1((const char*)ValuePtr, 19); break; case TAG_USERCOMMENT: // Olympus has this padded with trailing spaces. Remove these first. for (a=ByteCount;;) { a--; if ((ValuePtr)[a] == ' ') { (ValuePtr)[a] = '\0'; } else { break; } if (a == 0) break; } // Copy the comment if (memcmp(ValuePtr, "ASCII",5) == 0) { for (a=5; a<10; a++) { int c; c = (ValuePtr)[a]; if (c != '\0' && c != ' ') { UserComment = QString::fromLatin1((const char*)(a+ValuePtr), 199); break; } } } else { UserComment = QString::fromLatin1((const char*)ValuePtr, 199); } break; case TAG_FNUMBER: // Simplest way of expressing aperture, so I trust it the most. // (overwrite previously computd value if there is one) ExifData::ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: // More relevant info always comes earlier, so only use this field if we don't // have appropriate aperture information yet. if (ExifData::ApertureFNumber == 0) { ExifData::ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0)*0.5); } break; case TAG_FOCALLENGTH: // Nice digital cameras actually save the focal length as a function // of how far they are zoomed in. ExifData::FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: // Inidcates the distacne the autofocus camera is focused to. // Tends to be less accurate as distance increases. ExifData::Distance = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURETIME: // Simplest way of expressing exposure time, so I trust it most. // (overwrite previously computd value if there is one) ExifData::ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SHUTTERSPEED: // More complicated way of expressing exposure time, so only use // this value if we don't already have it from somewhere else. if (ExifData::ExposureTime == 0) { ExifData::ExposureTime = (float)(1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0))); } break; case TAG_FLASH: ExifData::FlashUsed = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXIF_IMAGELENGTH: ExifImageLength = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXIF_IMAGEWIDTH: ExifImageWidth = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEXRES: FocalplaneXRes = ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEUNITS: switch((int)ConvertAnyFormat(ValuePtr, Format)) { case 1: FocalplaneUnits = 25.4; break; // inch case 2: // According to the information I was using, 2 means meters. // But looking at the Cannon powershot's files, inches is the only // sensible value. FocalplaneUnits = 25.4; break; case 3: FocalplaneUnits = 10; break; // centimeter case 4: FocalplaneUnits = 1; break; // milimeter case 5: FocalplaneUnits = .001; break; // micrometer } break; // Remaining cases contributed by: Volker C. Schoech ([email protected]) case TAG_EXPOSURE_BIAS: ExifData::ExposureBias = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: ExifData::Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_METERING_MODE: ExifData::MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_PROGRAM: ExifData::ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_ISO_EQUIVALENT: ExifData::ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if ( ExifData::ISOequivalent < 50 ) ExifData::ISOequivalent *= 200; break; case TAG_COMPRESSION_LEVEL: ExifData::CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_OFFSET: ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; case TAG_THUMBNAIL_LENGTH: ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format); break; } if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET) { unsigned char * SubdirStart; SubdirStart = OffsetBase + Get32u(ValuePtr); if (SubdirStart <= OffsetBase || SubdirStart >= OffsetBase+ExifLength) { throw FatalError("Illegal subdirectory link"); } ProcessExifDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1); continue; } } { // In addition to linking to subdirectories via exif tags, // there's also a potential link to another directory at the end of each // directory. this has got to be the result of a comitee! unsigned char * SubdirStart; unsigned Offset; if (DIR_ENTRY_ADDR(DirStart, NumDirEntries) + 4 <= OffsetBase+ExifLength) { Offset = Get32u(DIR_ENTRY_ADDR(DirStart, NumDirEntries)); // There is at least one jpeg from an HP camera having an Offset of almost MAXUINT. // Adding OffsetBase to it produces an overflow, so compare with ExifLength here. // See http://bugs.kde.org/show_bug.cgi?id=54542 if (Offset && Offset < ExifLength) { SubdirStart = OffsetBase + Offset; if (SubdirStart > OffsetBase+ExifLength) { if (SubdirStart < OffsetBase+ExifLength+20) { // Jhead 1.3 or earlier would crop the whole directory! // As Jhead produces this form of format incorrectness, // I'll just let it pass silently kdDebug(7034) << "Thumbnail removed with Jhead 1.3 or earlier\n"; } else { throw FatalError("Illegal subdirectory link 2"); } } else { if (SubdirStart <= OffsetBase+ExifLength) { ProcessExifDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1); } } } } else { // The exif header ends before the last next directory pointer. } } if (ThumbnailSize && ThumbnailOffset) { if (ThumbnailSize + ThumbnailOffset < ExifLength) { // The thumbnail pointer appears to be valid. Store it. Thumbnail.loadFromData(OffsetBase + ThumbnailOffset, ThumbnailSize, "JPEG"); } } }
//-------------------------------------------------------------------------- // Process one of the nested EXIF directories. //-------------------------------------------------------------------------- static void ProcessExifDir(char * DirStart, char * OffsetBase, unsigned ExifLength) { int de; int a; int NumDirEntries; NumDirEntries = Get16u(DirStart); if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)){ ErrExit("Illegally sized directory"); } if (ShowTags){ printf("Directory with %d entries\n",NumDirEntries); } for (de=0;de<NumDirEntries;de++){ int Tag, Format, Components; char * ValuePtr; int ByteCount; char * DirEntry; DirEntry = DirStart+2+12*de; Tag = Get16u(DirEntry); Format = Get16u(DirEntry+2); Components = Get32u(DirEntry+4); if ((Format-1) >= NUM_FORMATS) { // (-1) catches illegal zero case as unsigned underflows to positive large. ErrExit("Illegal format code in EXIF dir"); } ByteCount = Components * BytesPerFormat[Format]; if (ByteCount > 4){ unsigned OffsetVal; OffsetVal = Get32u(DirEntry+8); // If its bigger than 4 bytes, the dir entry contains an offset. if (OffsetVal+ByteCount > ExifLength){ // Bogus pointer offset and / or bytecount value printf("Offset %d bytes %d ExifLen %d\n",OffsetVal, ByteCount, ExifLength); ErrExit("Illegal pointer offset value in EXIF"); } ValuePtr = OffsetBase+OffsetVal; }else{ // 4 bytes or less and value is in the dir entry itself ValuePtr = DirEntry+8; } if (LastExifRefd < ValuePtr+ByteCount){ // Keep track of last byte in the exif header that was actually referenced. // That way, we know where the discardable thumbnail data begins. LastExifRefd = ValuePtr+ByteCount; } if (ShowTags){ // Show tag name for (a=0;;a++){ if (TagTable[a].Tag == 0){ printf(" Unknown Tag %04x Value = ", Tag); break; } if (TagTable[a].Tag == Tag){ printf(" %s = ",TagTable[a].Desc); break; } } // Show tag value. switch(Format){ case FMT_UNDEFINED: // Undefined is typically an ascii string. case FMT_STRING: // String arrays printed without function call (different from int arrays) printf("\""); for (a=0;a<ByteCount;a++){ if (isprint((ValuePtr)[a])){ putchar((ValuePtr)[a]); } } printf("\"\n"); break; default: // Handle arrays of numbers later (will there ever be?) PrintFormatNumber(ValuePtr, Format); } } // Extract useful components of tag switch(Tag){ case TAG_MAKE: strncpy(ImageInfo.CameraMake, ValuePtr, 31); break; case TAG_MODEL: strncpy(ImageInfo.CameraModel, ValuePtr, 39); break; case TAG_DATETIME_ORIGINAL: strncpy(ImageInfo.DateTime, ValuePtr, 19); break; case TAG_USERCOMMENT: // Olympus has this padded with trailing spaces. Remove these first. for (a=ByteCount;;){ a--; if ((ValuePtr)[a] == ' '){ (ValuePtr)[a] = '\0'; }else{ break; } if (a == 0) break; } // Copy the comment if (memcmp(ValuePtr, "ASCII",5) == 0){ for (a=5;a<10;a++){ int c; c = (ValuePtr)[a]; if (c != '\0' && c != ' '){ strncpy(ImageInfo.Comments, a+ValuePtr, 199); break; } } }else{ strncpy(ImageInfo.Comments, ValuePtr, 199); } break; case TAG_FNUMBER: // Simplest way of expressing aperture, so I trust it the most. // (overwrite previously computd value if there is one) ImageInfo.ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_APERTURE: case TAG_MAXAPERTURE: // More relevant info always comes earlier, so only use this field if we don't // have appropriate aperture information yet. if (ImageInfo.ApertureFNumber == 0){ ImageInfo.ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5); } break; case TAG_FOCALLENGTH: // Nice digital cameras actually save the focal length as a function // of how farthey are zoomed in. ImageInfo.FocalLength = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SUBJECT_DISTANCE: // Inidcates the distacne the autofocus camera is focused to. // Tends to be less accurate as distance increases. ImageInfo.Distance = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURETIME: // Simplest way of expressing exposure time, so I trust it most. // (overwrite previously computd value if there is one) ImageInfo.ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_SHUTTERSPEED: // More complicated way of expressing exposure time, so only use // this value if we don't already have it from somewhere else. if (ImageInfo.ExposureTime == 0){ ImageInfo.ExposureTime = (float)(1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2))); } break; case TAG_FLASH: if (ConvertAnyFormat(ValuePtr, Format)){ ImageInfo.FlashUsed = 1; } break; case TAG_EXIF_IMAGELENGTH: case TAG_EXIF_IMAGEWIDTH: // Use largest of height and width to deal with images that have been // rotated to portrait format. a = (int)ConvertAnyFormat(ValuePtr, Format); if (ExifImageWidth < a) ExifImageWidth = a; break; case TAG_FOCALPLANEXRES: FocalplaneXRes = ConvertAnyFormat(ValuePtr, Format); break; case TAG_FOCALPLANEUNITS: switch((int)ConvertAnyFormat(ValuePtr, Format)){ case 1: FocalplaneUnits = 25.4; break; // inch case 2: // According to the information I was using, 2 means meters. // But looking at the Cannon powershot's files, inches is the only // sensible value. FocalplaneUnits = 25.4; break; case 3: FocalplaneUnits = 10; break; // centimeter case 4: FocalplaneUnits = 1; break; // milimeter case 5: FocalplaneUnits = .001; break; // micrometer } break; // Remaining cases contributed by: Volker C. Schoech ([email protected]) case TAG_EXPOSURE_BIAS: ImageInfo.ExposureBias = (float)ConvertAnyFormat(ValuePtr, Format); break; case TAG_WHITEBALANCE: ImageInfo.Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_METERING_MODE: ImageInfo.MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_EXPOSURE_PROGRAM: ImageInfo.ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format); break; case TAG_ISO_EQUIVALENT: ImageInfo.ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format); if ( ImageInfo.ISOequivalent < 80 ) ImageInfo.ISOequivalent *= 200; break; case TAG_COMPRESSION_LEVEL: ImageInfo.CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format); break; } if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET){ char * SubdirStart; SubdirStart = OffsetBase + Get32u(ValuePtr); if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength){ ErrExit("Illegal subdirectory link"); } ProcessExifDir(SubdirStart, OffsetBase, ExifLength); continue; } } }