Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join 2 jpegs together losslessly without decoding using a hex editor?

Tags:

jpeg

I am trying to write a program (prob in java) to join a number of jpegs together losslessly without decoding them first.

I thought I'd start simple and try and append 2 jpegs of the same size compressed with the same settings one above the other using a hex editor.

First I extract the image data of jpeg B and append it to jpeg A. By modifying the dimensions specified in the headers I get a new recognizable picture (jpeg A + jpeg B appended in the y axis) which can be diplayed. However, although the image data from jpeg B is clearly recognizable it seems to have lost a lot of colour information and is clearly incorrect.

So my question is what steps am I missing out here? I don't think there are any other dimension specific header values I need to change, so maybe I need to huffman decode the image data from both jpegs, then append them together and then reencode the lot?

I've spent some time reading up on jpeg specs and headers etc but to be honest I'm out of my depth and could really do with a pointer or two!

Thanks a lot for any help.


Thanks for all the suggestions. Yes this is definitely possible, I should have mentioned jpegtran in my original question. I am basically trying to replicate this aspect of jpegtran functionality but use it in my own program. I guess I should look at the jpegtran source but I know nothing about C and not very much about programming in general so reverse engineering source code is easier said than done!

like image 455
joinJpegs Avatar asked Mar 04 '09 07:03

joinJpegs


People also ask

What is JPEG decompression?

Decompression of JPEG image involves extracting the pixel values using 2-dimensional Inverse Discrete Cosine Transform and de-quantization. The pixel values are in zigzag format in JPEG so they need to be extracted out in the normal format. JPEG header has discrete quantization tables and Huffman tables encoded in it.

What is JPEG codec?

The JPEG standard specifies the codec, which defines how an image is compressed into a stream of bytes and decompressed back into an image, but not the file format used to contain that stream. The Exif and JFIF standards define the commonly used file formats for interchange of JPEG-compressed images.

What is the principle behind JPEG encoding?

The JPEG compression is a block based compression. The data reduction is done by the subsampling of the color information, the quantization of the DCT-coefficients and the Huffman-Coding (reorder and coding). The user can control the amount of image quality loss due to the data reduction by setting (or chose presets).


1 Answers

This is very much doable. I did it on a lot of Google map image tiles to join those and form a poster size image. There is a package for Unix called JPEG Tools for doing exactly this. The program is called jpegjoin. Pure C source, with Windows binaries available. When compiled it creates a command line application which when run joins two jpeg images loselessly among many other things. It does NOT de-compress any image, just merges the compressed data together and fixes the header accordingly. I used it to merge 100 images to create 50 strips and then merged those strips again to create a large image.

More information can be found at http://en.wikipedia.org/wiki/Lossy_compression#Lossless_editing

Source code

Source code for the underlying jpegtran library can be found here. An example script to mimic jpegjoin is here.

like image 128
CDR Avatar answered Oct 21 '22 21:10

CDR