Get the RGB values of a pixel from an image in C++

Given an image (probably JPG, but we haven't decided what format), I'm trying to get all the RGB values of all the pixels in the image, using C++ on Unix.  I don't need to display the image, just retrieve the data.
I'm looking for the code to this in C++ or free libraries that can handle it.  Thanks.
LVL 2
mysdaaoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb_indiaCommented:
COLORREF array[width][height];

HBITMAP bm = (HBITMAP)LoadImage(NULL, bmName, IMAGE_BITMAP, 0 , 0, LR_LOADFROMFILE);
HDC temp = CreateCompatibleDC(hdc);
SelectObject(temp, bm);
for(int i = 0; i < width; i++)
{
     for(int j = 0; j < height; j++)
     {
          array[i][j] = GetPixel(other,i,j);
     }
}

That will give you an array of RGB values.  you can use GetRValue(), GetGValue() and GetBvalue() to find out the specific RGB values.  
0
mysdaaoAuthor Commented:
The compiler doesn't recognize most of those data types.
I think you've given me an example of how to do it in Visual C++.  I'm working on Unix here.
0
weareuProgrammerCommented:
yup thats MFC... Do you use g++ ? what compiler are you using on your Unix box?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

weareuProgrammerCommented:
checkout:
http://www.cs.technion.ac.il/Labs/Isl/Project/Projects_done/vsoft/

They have a cool library for C++ (4 unix g++ is recommended) to do image manipulation with... You can easily read images into containers and easily get color data 4 pixels...

Checkout the sample as well:
http://www.cs.technion.ac.il/Labs/Isl/Project/Projects_done/vsoft/example.html
0
mysdaaoAuthor Commented:
Yes, I am using G++ on Unix.
From the description, that library looks like exactly what I need.  But I can't access the source.  The repository directory is forbidden.  I've sent the people listed an e-mail, but since it says it was last modified in 2000, I don't know how long it'll take for a response, or if they'll care.
So I'm still looking for a solution.
0
pb_indiaCommented:
#include <iostream.h>

// This is the main include file required for the ISL library
#include <pictemp.h>

int main()
{
    Greylevel greyPix; // This defines a single variable of
      // standard data type for greylevel images
    RGBdata colorPix; // And this is for color pixel

    Picture<Greylevel> BWPicture; // Picture class is a template.
      // here we defined a new container for greylevel images
      // initially it is empty

    BWPicture << "lenna256.gif"; // Using operator "<<" we can read image
      // files directly into our container. Besides GIF, there are
      // some other file types that are supported.
     
    int sizeX, sizeY;

    BWPicture.getDims(sizeX, sizeY); // This method retrieves the size
      // of an image in the container;
      // Please mind the order of coordinates
    cout < <"Picture is "<< sizeY << "x" << sizeX << endl;

    greyPix = BWPicture(sizeY/2, sizeX/2); // By using  operator() we
      // can address specific pixel by its coordinates

    BWPicture[sizeY/2][sizeX/2] = 20; // Direct operations are also allowed

    BWPicture >> "copy_of_lenna.pgm"; // Using operator ">>" we can
      // write the images from the container to a file. Only ASCII
      // PPM/PGM  formats are available


    Picture<RGBdata> ColorPicture; // here we defined a new container
      // for color images

    ColorPicture << "lenna.rgb"; // Here we open a color image in SGI
      // standard format
   
    colorPix = ColorPiucture(10, 15); // This is the same as [10][15]
        
    colorPix.R = 255; // The color pixel has 3 fields of R, G, and B
    colorPix.G = colorPix.B = 0; // This makes our pixel red

    ColorPicture(10,15) = colorPix; // we can assign RGB in the same manner

    BWPicture = ColorPicture; // this will transform color image into
      // greylevel authomatically

    ColorPicture = BWPicture; // back assignments sets R=G=B

      // Library Class is able to use built-in cameras like this:
    Picture<CamData> VideoPicture; // Container for Camera images

    cout << "Cheese!" << endl; // get ready to shot
   
    VideoPicture.CamShot(); // Here the camera is activated and the
      // picture is transferred from the camera into the container
   
    VideoPicture >> "capture.pgm"; // And now save it to a disk
   
      // That's all for now
    return 0;
}



--------------------------------------------------------------------------------

After the program has been written to a file, you can compile it by using a C++ compiler (in UNIX case, g++ is recommended) by issuing the following command:

on a Sun computer:
> g++ -I/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include -I/usr/local/include/X11 example.cc -o example -L/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/lib -lisls -ljpeg -lm
This can be explained as follows:
-I flag tells the compiler where to look for the #include files
-L flag tells the compiler where to look for libraries during the linkage step
-lisls adds ISL library for the compilation
-ljpeg is required for working with JPG files
on a SGI computer:
> CC -n32 -ptall -I/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include -I/usr/local/include/X11 example.cc -o example -L/net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/lib -lisl -limage -lvl -ljpeg -lm
In this case, SGI must add more libraries in order to work with camera and images.
on a PC the compilation process is compiler-dependent. Details can be found on the Compilaton page
Note, that the library name is DIFFERENT for SUNs (isls) and for SGIs (isl). This is because the machine architecture is different between those two.

0
mysdaaoAuthor Commented:
pb_india:
Compiling it that way would only work if I was on their network.  It will look for the libraries in the included files in /net/emc2-32/www/public_html/Labs/Isl/Project/Projects_done/vsoft/dl/include which of course doesn't exist outside their network.  I would need to download pictemp.h and whatever other files and libraries are used, but I can't find a way to do that.
0
mysdaaoAuthor Commented:
I found a library myself that can do what I need.  Corona can read and write many image formats and get pixel information.
http://corona.sourceforge.net/
Thank you for trying to help me.
0
moduloCommented:
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.