problem with getPixels()/setFromPixels() in grayscaleImage

Hi all!

I have a problem trying to set the pixels of a grayscale ofImage to the pixels in a ofxCvGrayscaleImage. Basically, ofImage.setFromPixels doesn’t seem to work for me

Here’s some code that shows my problem (see the comments):

in testApp.h

  
  
ofxCvGrayscaleImage imgCv;   
ofImage             img;  
ofTexture           tx;  
  
ofxCvColorImage     imgColCv;  
ofImage             imgCol;  
  
int w, h;  
  

in testApp.cpp

  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    w = 160;  
    h = 120;  
  
    imgCv.allocate(w, h);  
    img.allocate(w, h, OF_IMAGE_GRAYSCALE);  
    tx.allocate(w, h, GL_LUMINANCE);  
  
    imgColCv.allocate(w, h);  
    imgCol.allocate(w, h, OF_IMAGE_COLOR);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
    // I set my gray image to a whiteish color  
    imgCv.set(223);  
  
    // this setFromPixels doesn't seem to work.  
    img.setFromPixels(imgCv.getPixels(), w, h, OF_IMAGE_GRAYSCALE);  
  
    // but this does work, so I guess the problem is in setFromPixels.  
    tx.loadData(imgCv.getPixels(), w, h, GL_LUMINANCE);  
  
    // this also works ok.  
    imgColCv.set(255,0,23);  
  
    imgCol.setFromPixels(imgColCv.getPixels(), w, h, OF_IMAGE_COLOR);  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    imgCv.draw(0,0);  
    img.draw(w, 0); // this is not right. should be whiteish too.  
    tx.draw(w*2, 0);  
  
    imgColCv.draw(0, h);  
    imgCol.draw(w, h);  
  
}  
  

And this is the result I am getting.

So if I’m not mistaken, ofxCvGray.getPixels() works because the texture shows ok. and setFromPixels also works with color images…

I won’t dare to mention the word bug because I’m sure it’s some dumbness that I’m doing here…

I’m on Win / of_CB_0.05 FAT
zipped-src

Any help appreciated.

Thanks a lot!

Hey - I found this bug too a while back.
The problem is that freeimage can’t distinguish between a color image with a palette and a grayscale image as they are both one channel.

We had a ‘fix’ in for that but it looks like it got ‘unfixed’ with our latest update.

You can see the diff here:
http://www.openframeworks.cc/admin/inde-…-fImage.cpp

Line 183 had the fix in it:
Can you check if that works for you?

just change in the update function:

  
        bool bNoPallette                = (FreeImage_GetColorType(bmp) != FIC_PALETTE);   

to

  
        bool bNoPallette                = (FreeImage_GetColorType(bmp) != FIC_PALETTE) || type == OF_IMAGE_GRAYSCALE;   

sorry about that!
Theo

more on this.

I create 2 ofImages. allocate on imgGray and load an image in imgLoaded
then try to set pixels of imgGray from imgLoaded (not using ofxCv…)

  
  
imgGray.allocate(w,h, OF_IMAGE_GRAYSCALE);  
imgLoaded.loadImage("a.gif");  
imgLoaded.setImageType(OF_IMAGE_GRAYSCALE);  
  
imgGray.setFromPixels(imgLoaded.getPixels(), w, h, OF_IMAGE_GRAYSCALE);  
  

I get a black image.

again, if I do the same using color images it works.

At this point I’m having a look at ofImage code, but I’d be super grateful for any pointers (as in help, not * :slight_smile: )

EDIT: oops, seems we wrote at the same time. will leave it here for reference

I’m going to try your suggestion. Thanks a lot, Theo!

yes indeed, it ‘refixes’.

thanks a lot.
yay!

cool - we will have to ‘refix’ this back into the svn :wink:

Added this fix to the svn:
http://svn.openframeworks.cc/browser/di-…-rev=0&sc=1

download latest openFrameworks as zip
http://www.openframeworks.cc/downloads/-…-eworks.zip

Theo