How to embed exif metadata in an image without copying the image?

I previously asked this question: How to write exif metadata for an image .

Now I have found a way to enter metadata. However, this leads to copying the image into memory. With large images and the need to have a copy in memory, this will have performance and possibly lead to a memory failure.

Is there a correct way to enter metadata without having to make a copy of the image? Perhaps it can be attached to the file after it is written to disk?

I would prefer my own implementations without resorting to a third-party library just for this, if at all possible.

+4
source share
3 answers

This question may require small or large code, depending on what you need. EXIF data is stored in the APP1 JPEG marker (FFE1). It is very similar to a TIFF file with a TIFF, IFD header and separate data tags. If you can create your own marker segment APP1, adding it or replacing it in a JPEG file is trivial. If you want to read metadata from an existing file, add new tags and then write them back, which may be more active. The hard part of EXIF โ€‹โ€‹data is those tags that require more than 4 bytes. Each TIFF tag is 12 bytes: 2-byte tag, 2-byte data type, 4-byte count, 4-byte data. If the data is not completely placed in 4 bytes of the tag, then the tag indicates the absolute offset to the file where to look for data. If existing data has tags with such data (for example, make, model, capture date, capture time, etc.), you will need to repackage this data by setting offsets, and then add your own. In a nutshell:

1) If you add the pre-prepared APP1 marker to a JPEG file, it is simple and requires a little code.

2) If you need to read existing metadata from a JPEG file, add your own and write them back, the code will be more active. This is not "difficult", but requires much more than just reading and writing data blocks.

Start by reading the TIFF 6.0 specification to understand the structure of tags and directories:

TIFF 6.0 Specification

Then take a look at the JPEG EXIF โ€‹โ€‹specification:

EXIF 2.2 Spec

+1
source

I would expect existing exif manipulator software to do this, but not tested.

References:

0
source

CGImageSourceRef can be used to retrieve image properties, including thumbnails without loading all image data into memory. Thus, the memory is not lost UIImage and NSData.

CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], NULL); 

Then save the CGImageDestinationRef by adding the original image and exif data.

 CGImageDestinationAddImageFromSource (destRef, imageSource, 0, (CFDictionaryRef)propertes );//exif BOOL success = CGImageDestinationFinalize(destRef); 
0
source

Source: https://habr.com/ru/post/1402771/


All Articles