blob: a3825e8f83545331696d9643d52b13aeff0b76d9 [file] [log] [blame]
The Android Open Source Project34a25642009-03-03 19:30:03 -08001//--------------------------------------------------------------------------
2// Include file for jhead program.
3//
Ray Chena39920c2010-01-15 13:58:10 -08004// This include file only defines stuff that goes across modules.
5// I like to keep the definitions for macros and structures as close to
6// where they get used as possible, so include files only get stuff that
The Android Open Source Project34a25642009-03-03 19:30:03 -08007// gets used in more than one file.
8//--------------------------------------------------------------------------
9#define _CRT_SECURE_NO_DEPRECATE 1
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <errno.h>
16#include <ctype.h>
Wu-cheng Li661f9632010-02-04 17:19:42 +080017#include <stdint.h>
The Android Open Source Project34a25642009-03-03 19:30:03 -080018
19//--------------------------------------------------------------------------
20
21#ifdef _WIN32
22 #include <sys/utime.h>
23#else
24 #include <utime.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <limits.h>
29#endif
30
31
32typedef unsigned char uchar;
33
34#ifndef TRUE
35 #define TRUE 1
36 #define FALSE 0
37#endif
38
Chih-Chung Changd6a02c32009-03-24 20:34:28 -070039#define MAX_COMMENT_SIZE 2000
Ray Chen434623a2010-03-10 14:59:44 -080040#define GPS_PROCESSING_METHOD_LEN 100
The Android Open Source Project34a25642009-03-03 19:30:03 -080041
42#ifdef _WIN32
43 #define PATH_MAX _MAX_PATH
Chih-Chung Changd6a02c32009-03-24 20:34:28 -070044 #define SLASH '\\'
45#else
46 #define SLASH '/'
The Android Open Source Project34a25642009-03-03 19:30:03 -080047#endif
48
Chih-Chung Changd6a02c32009-03-24 20:34:28 -070049
The Android Open Source Project34a25642009-03-03 19:30:03 -080050//--------------------------------------------------------------------------
51// This structure is used to store jpeg file sections in memory.
52typedef struct {
53 uchar * Data;
54 int Type;
Jeff Sharkey31b17e62013-08-21 11:27:36 -070055 unsigned Offset;
The Android Open Source Project34a25642009-03-03 19:30:03 -080056 unsigned Size;
57}Section_t;
58
59extern int ExifSectionIndex;
60
61extern int DumpExifMap;
62
63#define MAX_DATE_COPIES 10
64
Wang Kun22bfc1902010-11-24 13:27:17 +010065// Buffer size must large enough to hold maximum location string
66// containing six signed integers plus delimeters and terminator,
67// i.e.: 11 * 6 + 3(‘/’) + 2(’,’) + 1(\0) = 72
68#define MAX_BUF_SIZE 72
69
Wu-cheng Li574d52d2010-01-31 22:34:33 +080070typedef struct {
71 uint32_t num;
72 uint32_t denom;
73} rat_t;
74
The Android Open Source Project34a25642009-03-03 19:30:03 -080075//--------------------------------------------------------------------------
76// This structure stores Exif header image elements in a simple manner
77// Used to store camera data as extracted from the various ways that it can be
78// stored in an exif header
79typedef struct {
80 char FileName [PATH_MAX+1];
81 time_t FileDateTime;
82 unsigned FileSize;
83 char CameraMake [32];
84 char CameraModel [40];
85 char DateTime [20];
86 int Height, Width;
87 int Orientation;
88 int IsColor;
89 int Process;
90 int FlashUsed;
Wu-cheng Li574d52d2010-01-31 22:34:33 +080091 rat_t FocalLength;
The Android Open Source Project34a25642009-03-03 19:30:03 -080092 float ExposureTime;
93 float ApertureFNumber;
94 float Distance;
95 float CCDWidth;
96 float ExposureBias;
97 float DigitalZoomRatio;
98 int FocalLength35mmEquiv; // Exif 2.2 tag - usually not present.
99 int Whitebalance;
100 int MeteringMode;
101 int ExposureProgram;
102 int ExposureMode;
103 int ISOequivalent;
104 int LightSource;
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700105 int DistanceRange;
106
107 char Comments[MAX_COMMENT_SIZE];
108 int CommentWidchars; // If nonzer, widechar comment, indicates number of chars.
The Android Open Source Project34a25642009-03-03 19:30:03 -0800109
110 unsigned ThumbnailOffset; // Exif offset to thumbnail
111 unsigned ThumbnailSize; // Size of thumbnail.
112 unsigned LargestExifOffset; // Last exif data referenced (to check if thumbnail is at end)
113
114 char ThumbnailAtEnd; // Exif header ends with the thumbnail
115 // (we can only modify the thumbnail if its at the end)
116 int ThumbnailSizeOffset;
117
118 int DateTimeOffsets[MAX_DATE_COPIES];
119 int numDateTimeTags;
120
121 int GpsInfoPresent;
122 char GpsLat[31];
Wang Kun22bfc1902010-11-24 13:27:17 +0100123 char GpsLatRaw[MAX_BUF_SIZE];
The Android Open Source Project34a25642009-03-03 19:30:03 -0800124 char GpsLatRef[2];
125 char GpsLong[31];
Wang Kun22bfc1902010-11-24 13:27:17 +0100126 char GpsLongRaw[MAX_BUF_SIZE];
The Android Open Source Project34a25642009-03-03 19:30:03 -0800127 char GpsLongRef[2];
128 char GpsAlt[20];
Wu-cheng Li1ed81972010-05-21 12:02:32 +0800129 rat_t GpsAltRaw;
130 char GpsAltRef;
Ray Chena39920c2010-01-15 13:58:10 -0800131 // gps-datestamp is 11 bytes ascii in EXIF 2.2
132 char GpsDateStamp[11];
133 char GpsTimeStamp[11];
Ray Chen434623a2010-03-10 14:59:44 -0800134 char GpsProcessingMethod[GPS_PROCESSING_METHOD_LEN + 1];
The Android Open Source Project34a25642009-03-03 19:30:03 -0800135}ImageInfo_t;
136
137
138
139#define EXIT_FAILURE 1
140#define EXIT_SUCCESS 0
141
142// jpgfile.c functions
143typedef enum {
144 READ_METADATA = 1,
145 READ_IMAGE = 2,
146 READ_ALL = 3
147}ReadMode_t;
148
149
150typedef struct {
151 unsigned short Tag; // tag value, i.e. TAG_MODEL
152 int Format; // format of data
153 char* Value; // value of data in string format
154 int DataLength; // length of string when format says Value is a string
155 int GpsTag; // bool - the tag is related to GPS info
156} ExifElement_t;
157
158
159typedef struct {
160 unsigned short Tag;
161 char * Desc;
162 int Format;
163 int DataLength; // Number of elements in Format. -1 means any length.
164} TagTable_t;
165
166
167// prototypes for jhead.c functions
168void ErrFatal(char * msg);
169void ErrNonfatal(char * msg, int a1, int a2);
170void FileTimeAsString(char * TimeStr);
171
172// Prototypes for exif.c functions.
173int Exif2tm(struct tm * timeptr, char * ExifTime);
174void process_EXIF (unsigned char * CharBuf, unsigned int length);
175int RemoveThumbnail(unsigned char * ExifSection);
176void ShowImageInfo(int ShowFileInfo);
177void ShowConciseImageInfo(void);
178const char * ClearOrientation(void);
179void PrintFormatNumber(void * ValuePtr, int Format, int ByteCount);
180double ConvertAnyFormat(void * ValuePtr, int Format);
181int Get16u(void * Short);
182unsigned Get32u(void * Long);
183int Get32s(void * Long);
184void Put32u(void * Value, unsigned PutValue);
Angus Kong482486a2012-01-19 15:56:10 +0800185void create_EXIF(ExifElement_t* elements, int exifTagCount, int gpsTagCount, int hasDateTimeTag);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800186int TagNameToValue(const char* tagName);
Angus Kong482486a2012-01-19 15:56:10 +0800187int IsDateTimeTag(unsigned short tag);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800188
189//--------------------------------------------------------------------------
190// Exif format descriptor stuff
191extern const int BytesPerFormat[];
192#define NUM_FORMATS 12
193
Ray Chena39920c2010-01-15 13:58:10 -0800194#define FMT_BYTE 1
The Android Open Source Project34a25642009-03-03 19:30:03 -0800195#define FMT_STRING 2
196#define FMT_USHORT 3
197#define FMT_ULONG 4
198#define FMT_URATIONAL 5
199#define FMT_SBYTE 6
200#define FMT_UNDEFINED 7
201#define FMT_SSHORT 8
202#define FMT_SLONG 9
203#define FMT_SRATIONAL 10
204#define FMT_SINGLE 11
205#define FMT_DOUBLE 12
206
207
208// makernote.c prototypes
209extern void ProcessMakerNote(unsigned char * DirStart, int ByteCount,
210 unsigned char * OffsetBase, unsigned ExifLength);
211
212// gpsinfo.c prototypes
Ray Chena39920c2010-01-15 13:58:10 -0800213void ProcessGpsInfo(unsigned char * ValuePtr, int ByteCount,
The Android Open Source Project34a25642009-03-03 19:30:03 -0800214 unsigned char * OffsetBase, unsigned ExifLength);
215int IsGpsTag(const char* tag);
216int GpsTagToFormatType(unsigned short tag);
217int GpsTagNameToValue(const char* tagName);
218TagTable_t* GpsTagToTagTableEntry(unsigned short tag);
Tyler Luubd900942011-10-12 17:46:32 -0500219static const char ExifAsciiPrefix[] = { 0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0 };
The Android Open Source Project34a25642009-03-03 19:30:03 -0800220
221// iptc.c prototpyes
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700222void show_IPTC (unsigned char * CharBuf, unsigned int length);
223void ShowXmp(Section_t XmpSection);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800224
225// Prototypes for myglob.c module
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700226#ifdef _WIN32
227void MyGlob(const char * Pattern , void (*FileFuncParm)(const char * FileName));
228void SlashToNative(char * Path);
229#endif
230
231// Prototypes for paths.c module
232int EnsurePathExists(const char * FileName);
233void CatPath(char * BasePath, const char * FilePath);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800234
235// Prototypes from jpgfile.c
236int ReadJpegSections (FILE * infile, ReadMode_t ReadMode);
237void DiscardData(void);
238void DiscardAllButExif(void);
239int ReadJpegFile(const char * FileName, ReadMode_t ReadMode);
240int ReplaceThumbnail(const char * ThumbFileName);
Tyler Luu13714f22011-09-27 20:04:35 -0500241int ReplaceThumbnailFromBuffer(const char* Thumb, int ThumbLen);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800242int SaveThumbnail(char * ThumbFileName);
243int RemoveSectionType(int SectionType);
244int RemoveUnknownSections(void);
245int WriteJpegFile(const char * FileName);
246Section_t * FindSection(int SectionType);
247Section_t * CreateSection(int SectionType, unsigned char * Data, int size);
248void ResetJpgfile(void);
Tyler Luu24757b42011-08-24 20:53:34 -0500249int ReadJpegSectionsFromBuffer (unsigned char* buffer, unsigned int buffer_size, ReadMode_t ReadMode);
250int WriteJpegToBuffer(unsigned char* buffer, unsigned int buffer_size);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800251
252// Variables from jhead.c used by exif.c
253extern ImageInfo_t ImageInfo;
254extern int ShowTags;
255extern char* formatStr(int format);
256
257//--------------------------------------------------------------------------
258// JPEG markers consist of one or more 0xFF bytes, followed by a marker
259// code byte (which is not an FF). Here are the marker codes of interest
260// in this program. (See jdmarker.c for a more complete list.)
261//--------------------------------------------------------------------------
262
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700263#define M_SOF0 0xC0 // Start Of Frame N
264#define M_SOF1 0xC1 // N indicates which compression process
265#define M_SOF2 0xC2 // Only SOF0-SOF2 are now in common use
The Android Open Source Project34a25642009-03-03 19:30:03 -0800266#define M_SOF3 0xC3
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700267#define M_SOF5 0xC5 // NB: codes C4 and CC are NOT SOF markers
The Android Open Source Project34a25642009-03-03 19:30:03 -0800268#define M_SOF6 0xC6
269#define M_SOF7 0xC7
270#define M_SOF9 0xC9
271#define M_SOF10 0xCA
272#define M_SOF11 0xCB
273#define M_SOF13 0xCD
274#define M_SOF14 0xCE
275#define M_SOF15 0xCF
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700276#define M_SOI 0xD8 // Start Of Image (beginning of datastream)
277#define M_EOI 0xD9 // End Of Image (end of datastream)
278#define M_SOS 0xDA // Start Of Scan (begins compressed data)
279#define M_JFIF 0xE0 // Jfif marker
280#define M_EXIF 0xE1 // Exif marker. Also used for XMP data!
281#define M_XMP 0x10E1 // Not a real tag (same value in file as Exif!)
Ray Chena39920c2010-01-15 13:58:10 -0800282#define M_COM 0xFE // COMment
The Android Open Source Project34a25642009-03-03 19:30:03 -0800283#define M_DQT 0xDB
284#define M_DHT 0xC4
285#define M_DRI 0xDD
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700286#define M_IPTC 0xED // IPTC marker