|
27 | 27 |
|
28 | 28 | #include "exif.h" |
29 | 29 |
|
| 30 | +// sample functions |
| 31 | +int sample_removeExifSegment(const char *srcJpgFileName, const char *outJpgFileName); |
| 32 | +int sample_removeSensitiveData(const char *srcJpgFileName, const char *outJpgFileName); |
| 33 | +int sample_queryTagExists(const char *srcJpgFileName); |
| 34 | +int sample_updateTagData(const char *srcJpgFileName, const char *outJpgFileName); |
| 35 | +int sample_saveThumbnail(const char *srcJpgFileName, const char *outFileName); |
| 36 | + |
30 | 37 | // sample |
31 | 38 | int main(int ac, char *av[]) |
32 | 39 | { |
@@ -123,9 +130,196 @@ int main(int ac, char *av[]) |
123 | 130 | // free IFD table array |
124 | 131 | freeIfdTableArray(ifdArray); |
125 | 132 |
|
126 | | - // remove the Exif segment from a JPEG file |
127 | | - result = removeExifSegmentFromJPEGFile(av[1], "_noexif.jpg"); |
128 | | - printf("removeExifSegmentFromJPEGFile: result=%d\n", result); |
129 | 133 |
|
| 134 | + // sample function A: remove the Exif segment in a JPEG file |
| 135 | + // result = sample_removeExifSegment(av[1], "removeExif.jpg"); |
| 136 | + |
| 137 | + // sample function B: remove sensitive Exif data in a JPEG file |
| 138 | + // result = sample_removeSensitiveData(av[1], "removeSensitive.jpg"); |
| 139 | + |
| 140 | + // sample function C: check if "GPSLatitude" tag exists in GPS IFD |
| 141 | + // result = sample_queryTagExists(av[1]); |
| 142 | + |
| 143 | + // sample function D: Update the value of "Make" tag in 0th IFD |
| 144 | + // result = sample_updateTagData(av[1], "updateTag.jpg"); |
| 145 | + |
| 146 | + // sample function E: Write Exif thumbnail data to file |
| 147 | + // result = sample_saveThumbnail(av[1], "thumbnail.jpg"); |
| 148 | + |
| 149 | + return result; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * sample_removeExifSegment() |
| 154 | + * |
| 155 | + * remove the Exif segment in a JPEG file |
| 156 | + * |
| 157 | + */ |
| 158 | +int sample_removeExifSegment(const char *srcJpgFileName, const char *outJpgFileName) |
| 159 | +{ |
| 160 | + int sts = removeExifSegmentFromJPEGFile(srcJpgFileName, outJpgFileName); |
| 161 | + if (sts <= 0) { |
| 162 | + printf("removeExifSegmentFromJPEGFile: ret=%d\n", sts); |
| 163 | + } |
| 164 | + return sts; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * sample_removeSensitiveData() |
| 169 | + * |
| 170 | + * remove sensitive Exif data in a JPEG file |
| 171 | + * |
| 172 | + */ |
| 173 | +int sample_removeSensitiveData(const char *srcJpgFileName, const char *outJpgFileName) |
| 174 | +{ |
| 175 | + int sts, result; |
| 176 | + void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result); |
| 177 | + |
| 178 | + if (!ifdTableArray) { |
| 179 | + printf("createIfdTableArray: ret=%d\n", result); |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + // remove GPS IFD and 1st IFD if exist |
| 184 | + removeIfdTableFromIfdTableArray(ifdTableArray, IFD_GPS); |
| 185 | + removeIfdTableFromIfdTableArray(ifdTableArray, IFD_1ST); |
| 186 | + |
| 187 | + // remove tags if exist |
| 188 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Make); |
| 189 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Model); |
| 190 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_DateTime); |
| 191 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_ImageDescription); |
| 192 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Software); |
| 193 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Artist); |
| 194 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_MakerNote); |
| 195 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_UserComment); |
| 196 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_DateTimeOriginal); |
| 197 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_DateTimeDigitized); |
| 198 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTime); |
| 199 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTimeOriginal); |
| 200 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_SubSecTimeDigitized); |
| 201 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_ImageUniqueID); |
| 202 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_CameraOwnerName); |
| 203 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_BodySerialNumber); |
| 204 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensMake); |
| 205 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensModel); |
| 206 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_EXIF, TAG_LensSerialNumber); |
| 207 | + |
| 208 | + // update the Exif segment |
| 209 | + sts = updateExifSegmentInJPEGFile(srcJpgFileName, outJpgFileName, ifdTableArray); |
| 210 | + if (sts < 0) { |
| 211 | + printf("updateExifSegmentInJPEGFile: ret=%d\n", sts); |
| 212 | + } |
| 213 | + freeIfdTableArray(ifdTableArray); |
| 214 | + return sts; |
| 215 | +} |
| 216 | + |
| 217 | +/** |
| 218 | + * sample_queryTagExists() |
| 219 | + * |
| 220 | + * check if "GPSLatitude" tag exists in GPS IFD |
| 221 | + * |
| 222 | + */ |
| 223 | +int sample_queryTagExists(const char *srcJpgFileName) |
| 224 | +{ |
| 225 | + int sts, result; |
| 226 | + void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result); |
| 227 | + if (!ifdTableArray) { |
| 228 | + printf("createIfdTableArray: ret=%d\n", result); |
| 229 | + return result; |
| 230 | + } |
| 231 | + |
| 232 | + sts = queryTagNodeIsExist(ifdTableArray, IFD_GPS, TAG_GPSLatitude); |
| 233 | + printf("GPSLatitude tag is %s in [%s]\n", (sts) ? "exists" : "not exists", srcJpgFileName); |
| 234 | + |
| 235 | + freeIfdTableArray(ifdTableArray); |
| 236 | + return sts; |
| 237 | +} |
| 238 | + |
| 239 | +/** |
| 240 | + * sample_updateTagData() |
| 241 | + * |
| 242 | + * Update the value of "Make" tag in 0th IFD |
| 243 | + * |
| 244 | + */ |
| 245 | +int sample_updateTagData(const char *srcJpgFileName, const char *outJpgFileName) |
| 246 | +{ |
| 247 | + TagNodeInfo *tag; |
| 248 | + int sts, result; |
| 249 | + void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result); |
| 250 | + |
| 251 | + if (ifdTableArray != NULL) { |
| 252 | + if (queryTagNodeIsExist(ifdTableArray, IFD_0TH, TAG_Make)) { |
| 253 | + removeTagNodeFromIfdTableArray(ifdTableArray, IFD_0TH, TAG_Make); |
| 254 | + } |
| 255 | + } else { // Exif segment not exists |
| 256 | + // create new IFD table |
| 257 | + ifdTableArray = insertIfdTableToIfdTableArray(NULL, IFD_0TH, &result); |
| 258 | + if (!ifdTableArray) { |
| 259 | + printf("insertIfdTableToIfdTableArray: ret=%d\n", result); |
| 260 | + return 0; |
| 261 | + } |
| 262 | + } |
| 263 | + // create a tag info |
| 264 | + tag = createTagInfo(TAG_Make, TYPE_ASCII, 6, &result); |
| 265 | + if (!tag) { |
| 266 | + printf("createTagInfo: ret=%d\n", result); |
| 267 | + freeIfdTableArray(ifdTableArray); |
| 268 | + return result; |
| 269 | + } |
| 270 | + // set tag data |
| 271 | + strcpy((char*)tag->byteData, "ABCDE"); |
| 272 | + // insert to IFD table |
| 273 | + insertTagNodeToIfdTableArray(ifdTableArray, IFD_0TH, tag); |
| 274 | + freeTagInfo(tag); |
| 275 | + |
| 276 | + // write file |
| 277 | + sts = updateExifSegmentInJPEGFile(srcJpgFileName, outJpgFileName, ifdTableArray); |
| 278 | + |
| 279 | + if (sts < 0) { |
| 280 | + printf("updateExifSegmentInJPEGFile: ret=%d\n", sts); |
| 281 | + } |
| 282 | + freeIfdTableArray(ifdTableArray); |
| 283 | + return sts; |
| 284 | +} |
| 285 | + |
| 286 | +/** |
| 287 | + * sample_saveThumbnail() |
| 288 | + * |
| 289 | + * Write Exif thumbnail data to file |
| 290 | + * |
| 291 | + */ |
| 292 | +int sample_saveThumbnail(const char *srcJpgFileName, const char *outFileName) |
| 293 | +{ |
| 294 | + unsigned char *p; |
| 295 | + unsigned int len; |
| 296 | + FILE *fp; |
| 297 | + int result; |
| 298 | + |
| 299 | + void **ifdTableArray = createIfdTableArray(srcJpgFileName, &result); |
| 300 | + if (!ifdTableArray) { |
| 301 | + printf("createIfdTableArray: ret=%d\n", result); |
| 302 | + return result; |
| 303 | + } |
| 304 | + |
| 305 | + // try to get thumbnail data from 1st IFD |
| 306 | + p = getThumbnailDataOnIfdTableArray(ifdTableArray, &len, &result); |
| 307 | + if (!p) { |
| 308 | + printf("getThumbnailDataOnIfdTableArray: ret=%d\n", result); |
| 309 | + freeIfdTableArray(ifdTableArray); |
| 310 | + return result; |
| 311 | + } |
| 312 | + // save thumbnail |
| 313 | + fp = fopen(outFileName, "wb"); |
| 314 | + if (!fp) { |
| 315 | + printf("failed to create [%s]\n", outFileName); |
| 316 | + freeIfdTableArray(ifdTableArray); |
| 317 | + return 0; |
| 318 | + } |
| 319 | + fwrite(p, 1, len, fp); |
| 320 | + fclose(fp); |
| 321 | + |
| 322 | + free(p); |
| 323 | + freeIfdTableArray(ifdTableArray); |
130 | 324 | return 0; |
131 | 325 | } |
0 commit comments