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