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