NiVeR
NiVeR

Reputation: 1

Splitting an jpeg image in blocks in C++

Can anyone tell me how can an JPEG image be divided in 8 x 8 blocks in C++.

Thanks.

Upvotes: 0

Views: 2054

Answers (2)

smocking
smocking

Reputation: 3709

Ah, the die-hard approach. My heart goes out to you. Expect to learn a lot, but be forewarned that you will lose time, blood and pain doing so.

The Compression FAQ has some details on how JPEG works. A good starting point is Part 2: Subject 75: Introduction to JPEG.

In a nutshell, for a typical JPEG file, you will have to reverse the encoding steps 6 to 4:

  1. (6) extract the appropriate headers and image data from the JFIF container
  2. (5) reverse the Huffman coding
  3. (4) reverse the quantization

You should then be left with 8x8 blocks you could feed into an appropriate inverse DCT.

Wikipedia has some details on the JFIF format as well as Huffman tables and structure of the JPEG data within the JFIF.

I'm assuming you're looking to play with JPEG to learn about it? Because access to the raw encoded blocks is almost certainly not necessary if you have some practical application.

EDIT after seeing comments: If you just want to get a part of a very large JPEG without reading/decompressing the whole file, you could use ImageMagick's stream command. It allows you to get a subimage without reading the whole file. Use like e.g. stream -extract 8x8+16+16 large.jpeg block.rgb to get a 8x8 block starting at (16,16).

Upvotes: 3

Tony The Lion
Tony The Lion

Reputation: 63190

You have to decompress the image, use the turbojpg library (it's very fast), which will give you an array of unsigned char as RGB (or RGBA). Now you have an uncompressed image, which has a byte value for R G and B respectively.

You can from here, go and make a simple for loop that will go through 3*8 char blocks and copy them, using memcpy to some other memory location.

You have to keep in mind that the array returned from the turbojpg library is a one dimensional linear array of bytes. So the scanlines are stored one after the other. Take this into account when creating your blocks, cause depending on your needs, you'll have to traverse the array differently.

Upvotes: 1

Related Questions