blob: 59130cf01c8e4da2a4b527bd730dce2eae025a91 [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;
55 unsigned Size;
56}Section_t;
57
58extern int ExifSectionIndex;
59
60extern int DumpExifMap;
61
62#define MAX_DATE_COPIES 10
63
Wu-cheng Li574d52d2010-01-31 22:34:33 +080064typedef struct {
65 uint32_t num;
66 uint32_t denom;
67} rat_t;
68
The Android Open Source Project34a25642009-03-03 19:30:03 -080069//--------------------------------------------------------------------------
70// This structure stores Exif header image elements in a simple manner
71// Used to store camera data as extracted from the various ways that it can be
72// stored in an exif header
73typedef struct {
74 char FileName [PATH_MAX+1];
75 time_t FileDateTime;
76 unsigned FileSize;
77 char CameraMake [32];
78 char CameraModel [40];
79 char DateTime [20];
80 int Height, Width;
81 int Orientation;
82 int IsColor;
83 int Process;
84 int FlashUsed;
Wu-cheng Li574d52d2010-01-31 22:34:33 +080085 rat_t FocalLength;
The Android Open Source Project34a25642009-03-03 19:30:03 -080086 float ExposureTime;
87 float ApertureFNumber;
88 float Distance;
89 float CCDWidth;
90 float ExposureBias;
91 float DigitalZoomRatio;
92 int FocalLength35mmEquiv; // Exif 2.2 tag - usually not present.
93 int Whitebalance;
94 int MeteringMode;
95 int ExposureProgram;
96 int ExposureMode;
97 int ISOequivalent;
98 int LightSource;
Chih-Chung Changd6a02c32009-03-24 20:34:28 -070099 int DistanceRange;
100
101 char Comments[MAX_COMMENT_SIZE];
102 int CommentWidchars; // If nonzer, widechar comment, indicates number of chars.
The Android Open Source Project34a25642009-03-03 19:30:03 -0800103
104 unsigned ThumbnailOffset; // Exif offset to thumbnail
105 unsigned ThumbnailSize; // Size of thumbnail.
106 unsigned LargestExifOffset; // Last exif data referenced (to check if thumbnail is at end)
107
108 char ThumbnailAtEnd; // Exif header ends with the thumbnail
109 // (we can only modify the thumbnail if its at the end)
110 int ThumbnailSizeOffset;
111
112 int DateTimeOffsets[MAX_DATE_COPIES];
113 int numDateTimeTags;
114
115 int GpsInfoPresent;
116 char GpsLat[31];
117 char GpsLatRaw[31];
118 char GpsLatRef[2];
119 char GpsLong[31];
120 char GpsLongRaw[31];
121 char GpsLongRef[2];
122 char GpsAlt[20];
Wu-cheng Li1ed81972010-05-21 12:02:32 +0800123 rat_t GpsAltRaw;
124 char GpsAltRef;
Ray Chena39920c2010-01-15 13:58:10 -0800125 // gps-datestamp is 11 bytes ascii in EXIF 2.2
126 char GpsDateStamp[11];
127 char GpsTimeStamp[11];
Ray Chen434623a2010-03-10 14:59:44 -0800128 char GpsProcessingMethod[GPS_PROCESSING_METHOD_LEN + 1];
The Android Open Source Project34a25642009-03-03 19:30:03 -0800129}ImageInfo_t;
130
131
132
133#define EXIT_FAILURE 1
134#define EXIT_SUCCESS 0
135
136// jpgfile.c functions
137typedef enum {
138 READ_METADATA = 1,
139 READ_IMAGE = 2,
140 READ_ALL = 3
141}ReadMode_t;
142
143
144typedef struct {
145 unsigned short Tag; // tag value, i.e. TAG_MODEL
146 int Format; // format of data
147 char* Value; // value of data in string format
148 int DataLength; // length of string when format says Value is a string
149 int GpsTag; // bool - the tag is related to GPS info
150} ExifElement_t;
151
152
153typedef struct {
154 unsigned short Tag;
155 char * Desc;
156 int Format;
157 int DataLength; // Number of elements in Format. -1 means any length.
158} TagTable_t;
159
160
161// prototypes for jhead.c functions
162void ErrFatal(char * msg);
163void ErrNonfatal(char * msg, int a1, int a2);
164void FileTimeAsString(char * TimeStr);
165
166// Prototypes for exif.c functions.
167int Exif2tm(struct tm * timeptr, char * ExifTime);
168void process_EXIF (unsigned char * CharBuf, unsigned int length);
169int RemoveThumbnail(unsigned char * ExifSection);
170void ShowImageInfo(int ShowFileInfo);
171void ShowConciseImageInfo(void);
172const char * ClearOrientation(void);
173void PrintFormatNumber(void * ValuePtr, int Format, int ByteCount);
174double ConvertAnyFormat(void * ValuePtr, int Format);
175int Get16u(void * Short);
176unsigned Get32u(void * Long);
177int Get32s(void * Long);
178void Put32u(void * Value, unsigned PutValue);
179void create_EXIF(ExifElement_t* elements, int exifTagCount, int gpsTagCount);
180int TagNameToValue(const char* tagName);
181
182//--------------------------------------------------------------------------
183// Exif format descriptor stuff
184extern const int BytesPerFormat[];
185#define NUM_FORMATS 12
186
Ray Chena39920c2010-01-15 13:58:10 -0800187#define FMT_BYTE 1
The Android Open Source Project34a25642009-03-03 19:30:03 -0800188#define FMT_STRING 2
189#define FMT_USHORT 3
190#define FMT_ULONG 4
191#define FMT_URATIONAL 5
192#define FMT_SBYTE 6
193#define FMT_UNDEFINED 7
194#define FMT_SSHORT 8
195#define FMT_SLONG 9
196#define FMT_SRATIONAL 10
197#define FMT_SINGLE 11
198#define FMT_DOUBLE 12
199
200
201// makernote.c prototypes
202extern void ProcessMakerNote(unsigned char * DirStart, int ByteCount,
203 unsigned char * OffsetBase, unsigned ExifLength);
204
205// gpsinfo.c prototypes
Ray Chena39920c2010-01-15 13:58:10 -0800206void ProcessGpsInfo(unsigned char * ValuePtr, int ByteCount,
The Android Open Source Project34a25642009-03-03 19:30:03 -0800207 unsigned char * OffsetBase, unsigned ExifLength);
208int IsGpsTag(const char* tag);
209int GpsTagToFormatType(unsigned short tag);
210int GpsTagNameToValue(const char* tagName);
211TagTable_t* GpsTagToTagTableEntry(unsigned short tag);
212
213// iptc.c prototpyes
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700214void show_IPTC (unsigned char * CharBuf, unsigned int length);
215void ShowXmp(Section_t XmpSection);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800216
217// Prototypes for myglob.c module
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700218#ifdef _WIN32
219void MyGlob(const char * Pattern , void (*FileFuncParm)(const char * FileName));
220void SlashToNative(char * Path);
221#endif
222
223// Prototypes for paths.c module
224int EnsurePathExists(const char * FileName);
225void CatPath(char * BasePath, const char * FilePath);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800226
227// Prototypes from jpgfile.c
228int ReadJpegSections (FILE * infile, ReadMode_t ReadMode);
229void DiscardData(void);
230void DiscardAllButExif(void);
231int ReadJpegFile(const char * FileName, ReadMode_t ReadMode);
232int ReplaceThumbnail(const char * ThumbFileName);
233int SaveThumbnail(char * ThumbFileName);
234int RemoveSectionType(int SectionType);
235int RemoveUnknownSections(void);
236int WriteJpegFile(const char * FileName);
237Section_t * FindSection(int SectionType);
238Section_t * CreateSection(int SectionType, unsigned char * Data, int size);
239void ResetJpgfile(void);
240
241// Variables from jhead.c used by exif.c
242extern ImageInfo_t ImageInfo;
243extern int ShowTags;
244extern char* formatStr(int format);
245
246//--------------------------------------------------------------------------
247// JPEG markers consist of one or more 0xFF bytes, followed by a marker
248// code byte (which is not an FF). Here are the marker codes of interest
249// in this program. (See jdmarker.c for a more complete list.)
250//--------------------------------------------------------------------------
251
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700252#define M_SOF0 0xC0 // Start Of Frame N
253#define M_SOF1 0xC1 // N indicates which compression process
254#define M_SOF2 0xC2 // Only SOF0-SOF2 are now in common use
The Android Open Source Project34a25642009-03-03 19:30:03 -0800255#define M_SOF3 0xC3
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700256#define M_SOF5 0xC5 // NB: codes C4 and CC are NOT SOF markers
The Android Open Source Project34a25642009-03-03 19:30:03 -0800257#define M_SOF6 0xC6
258#define M_SOF7 0xC7
259#define M_SOF9 0xC9
260#define M_SOF10 0xCA
261#define M_SOF11 0xCB
262#define M_SOF13 0xCD
263#define M_SOF14 0xCE
264#define M_SOF15 0xCF
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700265#define M_SOI 0xD8 // Start Of Image (beginning of datastream)
266#define M_EOI 0xD9 // End Of Image (end of datastream)
267#define M_SOS 0xDA // Start Of Scan (begins compressed data)
268#define M_JFIF 0xE0 // Jfif marker
269#define M_EXIF 0xE1 // Exif marker. Also used for XMP data!
270#define M_XMP 0x10E1 // Not a real tag (same value in file as Exif!)
Ray Chena39920c2010-01-15 13:58:10 -0800271#define M_COM 0xFE // COMment
The Android Open Source Project34a25642009-03-03 19:30:03 -0800272#define M_DQT 0xDB
273#define M_DHT 0xC4
274#define M_DRI 0xDD
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700275#define M_IPTC 0xED // IPTC marker
The Android Open Source Project34a25642009-03-03 19:30:03 -0800276
277