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