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

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    How to read out the RGB values of an image?

    Hey,
    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
    Location
    The edge of the known universe
    Posts
    39,660
    http://www.wotsit.org/
    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
    Posts
    768
    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

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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
    Posts
    34
    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:

    http://www.libsdl.org/index.php

    and SDL_Image here:

    http://www.libsdl.org/projects/SDL_image/

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    28
    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