Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding big junks of custom data to jpg image file

I wonder if there is an obvious and elegant way to add additional data to a jpeg while keeping it readable for standard image viewers. More precisely I would like to embed a picture of the backside of a (scanned) photo into it. Old photos often have personal messages written on the back, may it be the date or some notes. Sure you could use EXIF and add some text, but an actuall image of the back is more preferable. Sure I could also save 2 files xyz.jpg and xyz_back.jpg, or arrange both images side by side, always visible in one picture, but that's not what I'm looking for. It is possible and has been done, like on Samsung Note 2 and 3 you can add handwritten notes to the photos you've taken as a image. Or some smartphones allow to embed voice recordings to the image files while preserving the readability of those files on other devices.

like image 685
Angry Coder Avatar asked Nov 09 '15 12:11

Angry Coder


2 Answers

An interesting question. There are file formats that support multiple images per file (multipage TIFF comes to mind) but JPEG doesn't support this natively.

One feature of the JPEG file format is the concept of APP segments. These are regions of the JPEG file that can contain arbitrary information (as a sequence of bytes). Exif is actually stored in one of these segments, and is identified by a preamble.

Take a look at this page: http://www.sno.phy.queensu.ca/~phil/exiftool/#JPEG

You'll see many segments there that start with APP such as APP0 (which can store JFIF data), APP1 (which can contain Exif) and so forth.

There's nothing stopping you storing data in one of these segments. Conformant JPEG readers will ignore this unrecognised data, but you could write software to store/retrieve data from within there. It's even possible to embed another JPEG file within such a segment! There's no precedent I know for doing this however.

Another option would be to include the second image as the thumbnail of the first. Normally thumbnails are very small, but you could store a second image as the thumbnail of the first. Some software might replace or remove this though.

In general I think using two files and a naming convention would be the simplest and least confusing, but you do have options.

like image 167
Drew Noakes Avatar answered Sep 28 '22 19:09

Drew Noakes


There are two ways you can do this.

1) Use and Application Marker (APP0–APPF)—the preferred method

2) Use a Comment Marker (COM)

If you use an APPn marker:

1) Do not make it the first APPn in the file. Every known JPEG file format expects some kind of format specific APPn marker right after the SOI marker. Make sure that your marker is not there.

2) Place a unique application identifier (null terminated string) at the start of the data (something done by convention).

All kinds of applications store additional data this way.

One issue is that the length field is only 16-bits (Big Endian format). If you have a lot of data, you will have to split it across multiple markers.

If you use a COM marker, make sure it comes after the first APPn marker in the file. However, I would discourage using a COM marker for something like this as it might choke applications that try to display the contents.

like image 45
user3344003 Avatar answered Sep 28 '22 20:09

user3344003