-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsample_main.c
325 lines (285 loc) · 9.85 KB
/
sample_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright (C) 2013 KLab Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* gcc:
* gcc -o exif sample_main.c exif.c
*
* Microsoft Visual C++:
* cl.exe /o exif sample_main.c exif.c
*/
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <stdio.h>
#include "exif.h"
// sample functions
int sample_removeExifSegment(const char *srcJpgFileName, const char *outJpgFileName);
int sample_removeSensitiveData(const char *srcJpgFileName, const char *outJpgFileName);
int sample_queryTagExists(const char *srcJpgFileName);
int sample_updateTagData(const char *srcJpgFileName, const char *outJpgFileName);
int sample_saveThumbnail(const char *srcJpgFileName, const char *outFileName);
// sample
int main(int ac, char *av[])
{
void **ifdArray;
TagNodeInfo *tag;
int i, result;
#ifdef _MSC_VER
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
#endif
#endif
if (ac < 2) {
printf("usage: %s <JPEG FileName> [-v]erbose\n", av[0]);
return 0;
}
// -v option
if (ac >= 3) {
if ((*av[2] == '-' || *av[2] == '/') && (*(av[2]+1) == 'v')) {
setVerbose(1);
}
}
// parse the JPEG header and create the pointer array of the IFD tables
ifdArray = createIfdTableArray(av[1], &result);
// check result status
switch (result) {
case 0: // no IFDs
printf("[%s] does not seem to contain the Exif segment.\n", av[1]);
break;
case ERR_READ_FILE:
printf("failed to open or read [%s].\n", av[1]);
break;
case ERR_INVALID_JPEG:
printf("[%s] is not a valid JPEG file.\n", av[1]);
break;
case ERR_INVALID_APP1HEADER:
printf("[%s] does not have valid Exif segment header.\n", av[1]);
break;
case ERR_INVALID_IFD:
printf("[%s] contains one or more IFD errors. use -v for details.\n", av[1]);
break;
default:
printf("[%s] createIfdTableArray: result=%d\n", av[1], result);
break;
}
if (!ifdArray) {
return 0;
}
// dump all IFD tables
for (i = 0; ifdArray[i] != NULL; i++) {
dumpIfdTable(ifdArray[i]);
}
// or dumpIfdTableArray(ifdArray);
printf("\n");
// get [Model] tag value from 0th IFD
tag = getTagInfo(ifdArray, IFD_0TH, TAG_Model);
if (tag) {
if (!tag->error) {
printf("0th IFD : Model = [%s]\n", tag->byteData);
}
freeTagInfo(tag);
}
// get [DateTimeOriginal] tag value from Exif IFD
tag = getTagInfo(ifdArray, IFD_EXIF, TAG_DateTimeOriginal);
if (tag) {
if (!tag->error) {
printf("Exif IFD : DateTimeOriginal = [%s]\n", tag->byteData);
}
freeTagInfo(tag);
}
// get [GPSLatitude] tag value from GPS IFD
tag = getTagInfo(ifdArray, IFD_GPS, TAG_GPSLatitude);
if (tag) {
if (!tag->error) {
printf("GPS IFD : GPSLatitude = ");
for (i = 0; i < (int)tag->count*2; i+=2) {
printf("%u/%u ", tag->numData[i], tag->numData[i+1]);
}
printf("\n");
}
freeTagInfo(tag);
}
// free IFD table array
freeIfdTableArray(ifdArray);
// sample function A: remove the Exif segment in a JPEG file
// result = sample_removeExifSegment(av[1], "removeExif.jpg");
// sample function B: remove sensitive Exif data in a JPEG file
// result = sample_removeSensitiveData(av[1], "removeSensitive.jpg");
// sample function C: check if "GPSLatitude" tag exists in GPS IFD
// result = sample_queryTagExists(av[1]);
// sample function D: Update the value of "Make" tag in 0th IFD
// result = sample_updateTagData(av[1], "updateTag.jpg");
// sample function E: Write Exif thumbnail data to file
// result = sample_saveThumbnail(av[1], "thumbnail.jpg");
return result;
}
/**
* sample_removeExifSegment()
*
* remove the Exif segment in a JPEG file
*
*/
int sample_removeExifSegment(const char *srcJpgFileName, const char *outJpgFileName)
{
int sts = removeExifSegmentFromJPEGFile(srcJpgFileName, outJpgFileName);
if (sts <= 0) {
printf("removeExifSegmentFromJPEGFile: ret=%d\n", sts);
}
return sts;
}
/**
* sample_removeSensitiveData()
*
* remove sensitive Exif data in a JPEG file
*
*/
int sample_removeSensitiveData(const char *srcJpgFileName, const char *outJpgFileName)
{
int sts, result;
void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result);
if (!ifdTableArray) {
printf("createIfdTableArray: ret=%d\n", result);
return result;
}
// remove GPS IFD and 1st IFD if exist
removeIfdTableFromIfdTableArray(ifdTableArray, IFD_GPS);
removeIfdTableFromIfdTableArray(ifdTableArray, IFD_1ST);
// remove tags if exist
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Make);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Model);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_DateTime);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_ImageDescription);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Software);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Artist);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_MakerNote);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_UserComment);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_DateTimeOriginal);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_DateTimeDigitized);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTime);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTimeOriginal);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTimeDigitized);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_ImageUniqueID);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_CameraOwnerName);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_BodySerialNumber);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensMake);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensModel);
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensSerialNumber);
// update the Exif segment
sts = updateExifSegmentInJPEGFile(srcJpgFileName, outJpgFileName, ifdTableArray);
if (sts < 0) {
printf("updateExifSegmentInJPEGFile: ret=%d\n", sts);
}
freeIfdTableArray(ifdTableArray);
return sts;
}
/**
* sample_queryTagExists()
*
* check if "GPSLatitude" tag exists in GPS IFD
*
*/
int sample_queryTagExists(const char *srcJpgFileName)
{
int sts, result;
void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result);
if (!ifdTableArray) {
printf("createIfdTableArray: ret=%d\n", result);
return result;
}
sts = queryTagNodeIsExist(ifdTableArray, IFD_GPS, TAG_GPSLatitude);
printf("GPSLatitude tag is %s in [%s]\n", (sts) ? "exists" : "not exists", srcJpgFileName);
freeIfdTableArray(ifdTableArray);
return sts;
}
/**
* sample_updateTagData()
*
* Update the value of "Make" tag in 0th IFD
*
*/
int sample_updateTagData(const char *srcJpgFileName, const char *outJpgFileName)
{
TagNodeInfo *tag;
int sts, result;
void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result);
if (ifdTableArray != NULL) {
if (queryTagNodeIsExist(ifdTableArray, IFD_0TH, TAG_Make)) {
removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Make);
}
} else { // Exif segment not exists
// create new IFD table
ifdTableArray = insertIfdTableToIfdTableArray(NULL, IFD_0TH, &result);
if (!ifdTableArray) {
printf("insertIfdTableToIfdTableArray: ret=%d\n", result);
return 0;
}
}
// create a tag info
tag = createTagInfo(TAG_Make, TYPE_ASCII, 6, &result);
if (!tag) {
printf("createTagInfo: ret=%d\n", result);
freeIfdTableArray(ifdTableArray);
return result;
}
// set tag data
strcpy((char*)tag->byteData, "ABCDE");
// insert to IFD table
insertTagNodeToIfdTableArray(ifdTableArray, IFD_0TH, tag);
freeTagInfo(tag);
// write file
sts = updateExifSegmentInJPEGFile(srcJpgFileName, outJpgFileName, ifdTableArray);
if (sts < 0) {
printf("updateExifSegmentInJPEGFile: ret=%d\n", sts);
}
freeIfdTableArray(ifdTableArray);
return sts;
}
/**
* sample_saveThumbnail()
*
* Write Exif thumbnail data to file
*
*/
int sample_saveThumbnail(const char *srcJpgFileName, const char *outFileName)
{
unsigned char *p;
unsigned int len;
FILE *fp;
int result;
void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result);
if (!ifdTableArray) {
printf("createIfdTableArray: ret=%d\n", result);
return result;
}
// try to get thumbnail data from 1st IFD
p = getThumbnailDataOnIfdTableArray(ifdTableArray, &len, &result);
if (!p) {
printf("getThumbnailDataOnIfdTableArray: ret=%d\n", result);
freeIfdTableArray(ifdTableArray);
return result;
}
// save thumbnail
fp = fopen(outFileName, "wb");
if (!fp) {
printf("failed to create [%s]\n", outFileName);
freeIfdTableArray(ifdTableArray);
return 0;
}
fwrite(p, 1, len, fp);
fclose(fp);
free(p);
freeIfdTableArray(ifdTableArray);
return 0;
}