How to Read Jpeg and Png Pixels in C++ on Linux

How do I read JPEG and PNG pixels in C++ on Linux?

There is no standard library in the C-standard to read the file-formats.

However, most programs, especially on the linux platform use the same library to decode the image-formats:

For jpeg it's libjpeg, for png it's libpng.

The chances that the libs are already installed is very high.

http://www.libpng.org

http://www.ijg.org

opening a jpeg or png image as pixel data in c or c++

To start with, there is no standard C++ library to process images. Secondly, jpeg and png image formats are compressed image formats that need some complex sort of decompressing algorithm. So to start work with images you have two ways:

  1. Using of simple image format like bmp. bmp is just pixel buffer
    with some light header, describing that pixel buffer format. You can
    write a program loading bmp in half an our on your own (and waste
    about an hour to understand the format itself). More on bmp:
    http://en.wikipedia.org/wiki/BMP_file_format.

  2. Using EXTERNAL
    library. That is, you don't have this library with your language or
    compiler package. You need to download and even install it
    sometimes. In general, to use external library you need to do five
    thighs: 1) Find image processing library in Internet. 2) Put xxx.lib file in your project folder. 3) Tell your linker you want to use xxx.lib 4) Add in your code #include "xxx.h". 5) Use library API to load image.

How to extract a pixel from a png file using c++?

I don't think you'll be able to get a pixel without decompressing at least the preceding part of the image. When you open a file, it will just point to the compressed data on disk. When you run it through a PNG library, it will be decompressed.

Your best bet, I think, and the canonical library for reading and writing PNGs, is libpng. It provides great low-level access to image data and metadata, and is probably going to be the fastest/most efficient option. It allows you to decompress the data row by row (and perhaps chunk by chunk--I can't remember), so you can probably decompress until you get your pixel, then toss the decompressed data and move to the next image.

The downside is that it's probably not as programmer friendly as some other options. The documentation is comprehensive to the point of being tedious, IMO, but there may be simpler tutorials out there. On the other hand, you'll probably need all the details in the manual to accomplish what the minimal decompression you're going for. It will also help explain the PNG file format so you can better manage data down to the chunk level.

Read and display gray scale images in C language.

This answer is an attempt to demonstrate how to develop with ImageMagick's MagickWand API with Xcode, and is based on the comments from the OP.

After installing ImageMagick, start a new Xcode command-line C project. Before writing any code, you need to tell llvm about the ImageMagick resources/libraries/etc.

There are many, many, ways to achieve this, but here's the quickest I can think of.

  • Navigate to top-level project's "Build Settings"
  • Under "All" (not "Basic") search for "Other Linker Flags"
  • Outside of Xcode, open up Terminal.app and enter the following

    • MagickWand-config --ldflags
  • Enter the output from Terminal.app as the value for "Other Linker Flags"

Other Linker Flags

  • Back in the settings search; enter "Other C Flags"
  • Back in Terminal.app run the following

    • MagickWand-config --cflags
  • Enter the resulting output as the value for "Other C Flags"

Other C Flags

Over in file main.c, you should notice Xcode picking-up MagickWand commands right away.

MagickWand in Xcode

Try the following (needs X11 installed) ...

#include <stdio.h>
#include <wand/MagickWand.h>

int main(int argc, const char * argv[]) {
MagickWandGenesis();
MagickWand * wand;
wand = NewMagickWand();
MagickReadImage(wand, "wizard:");
MagickQuantizeImage(wand, 255, GRAYColorspace, 0, MagickFalse, MagickTrue);
MagickDisplayImage(wand, ":0");
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}

... and build + run to verify.

Read and display gray scale images in C language

edit

To get the gray scale (0-255) value for each pixel, you can invoke a pixel iterator (see second example here), or export the values. Here is an example of dynamically populating a list of gray values by exporting...

    // Get image size from wand instance
size_t width = MagickGetImageWidth(wand);
size_t height = MagickGetImageHeight(wand);
size_t total_gray_pixels = width * height;

// Allocate memory to hold values (total pixels * size of data type)
unsigned char * blob = malloc(total_gray_pixels);

MagickExportImagePixels(wand, // Image instance
0, // Start X
0, // Start Y
width, // End X
height, // End Y
"I", // Value where "I" = intensity = gray value
CharPixel, // Storage type where "unsigned char == (0 ~ 255)
blob); // Destination pointer

// Dump to stdout
for (int i = 0; i < total_gray_pixels; i++ ) {
printf("Gray value @ %lux%lu = %d\n", i % width, i / height, (int)blob[i]);
}
/** Example output...
* Gray value @ 0x0 = 226
* Gray value @ 1x0 = 189
* Gray value @ 2x0 = 153
* Gray value @ 3x0 = 116
* Gray value @ 4x0 = 80
* ... etc
*/

How to open an image using C?

As others have mentioned, since images are binary files you have to know how to "interpret" the data in them after reading them. Luckily, for most formats you can find libraries that do it for you like libpng or libjpeg.



Related Topics



Leave a reply



Submit