Mrinmoy Das
Mrinmoy Das

Reputation: 31

How to get Image matrix of an image using libjpeg

Does any function in libjpeg outputs an image matrix just like imread does in matlab?

Upvotes: 3

Views: 1647

Answers (1)

Throwback1986
Throwback1986

Reputation: 6005

From the libjpeg v9 release notes (http://www.ijg.org/files/):

Pixels are stored by scanlines, with each scanline running from left to right. The component values for each pixel are adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color. Each scanline is an array of data type JSAMPLE --- which is typically "unsigned char", unless you've changed jmorecfg.h. (You can also change the RGB pixel layout, say to B,G,R order, by modifying jmorecfg.h. But see the restrictions listed in that file before doing so.)

A 2-D array of pixels is formed by making a list of pointers to the starts of scanlines; so the scanlines need not be physically adjacent in memory. Even if you process just one scanline at a time, you must make a one-element pointer array to conform to this structure. Pointers to JSAMPLE rows are of type JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.

The library accepts or supplies one or more complete scanlines per call. It is not possible to process part of a row at a time. Scanlines are always processed top-to-bottom. You can process an entire image in one call if you have it all in memory, but usually it's simplest to process one scanline at a time.

The key section is:

A 2-D array of pixels is formed by making a list of pointers

This means that library does not natively return a "matrix" of pixels; rather, it returns rows (scanlines), and the user is responsible for arranging those scanlines into a "matrix". This is not too challenging, but a bit of work is needed. The following code snippet shows a working example of JPEG decompression:

while (decompressor.output_scanline < decompressor.output_height)
{
    pRow = &jpegInputBuffer[decompressor.output_scanline*rowStride];
    jpeg_read_scanlines(&decompressor, &pRow, 1);
}

Note that I've omitted a number of details (definition of pRow, rowStride, how the decompressor struct is loaded, etc) because those seem to be beyond the scope of the question.

Upvotes: 4

Related Questions