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