Thread: How to read out the RGB values of an image?

  1. #1
    Registered User
    Join Date
    May 2006

    How to read out the RGB values of an image?

    how do I read out the RGB values of an image (jpeg, png, gif, tiff) on Linux ?
    I'd like to pass the location of an image to a function that reads out the RGB values of each pixel,
    calculates the total sum, then returns r_total, g_total, b_total as array.
    I understand I need two for loops for width and height, but I don't know how to access the data.
    Could someone provide an example please?
    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Look up each file format.

    Start with simple windows BMP files, they're dead easy to decode into pixel RGB values.

    Finding an image library or two for the other file formats (especially jpeg) is probably a good idea unless you really want to roll your own code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    to roll your own code for jpeg is like rolling your own rope to hang yourself, oops thats mpeg
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Mar 2006
    pure bitmap files like BMP are uncompressed, so it's bloody straightforward. Most other file formats like PNG and JPEG are compressed/encoded, though, so direct RGB access becomes a little chore. To make it easy, get an API like libPNG.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    May 2006
    I'd maybe try using SDL with the SDL_Image library. It can read the most popular file formats like the ones you listed.

    Grab SDL here:

    and SDL_Image here:

  6. #6
    Registered User
    Join Date
    May 2006
    Hey, thanks for the replies.
    I checked out SDL, and it seems useful.
    So far I've managed to create an SDL_surface, load the image and
    get the width and height, but I'm not sure how to get the RGB with
    SDL_GetRGB. I don't know how to pass the pixel position to it and
    how to retrieve the rgb values.
    Could someone give me a hint or two?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help required to read PPM image files
    By fatality123 in forum C Programming
    Replies: 2
    Last Post: 09-30-2008, 10:50 AM
  2. HotSpot image controls (web)
    By novacain in forum C# Programming
    Replies: 0
    Last Post: 06-25-2008, 04:27 AM
  3. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM