ofImage bug with 8bpp

Hi Theo, Zach,

I think I found a bug in the ofImage class: When I’m loading an 8 bpp color image and after that another one within the same image object the update function starts an infinite loop by calling setImageType which calls update again etc…

You can easily test it like this:

  
  
	img.loadImage("frame001.bmp");  
    img.loadImage("frame002.bmp");  
  

…and printing something to the console from the update function within ofImage.

I think I found a workaround by changing the following in the ofImage::update function.

  
  
// 	convert to RGB -  
// 	this is a gif (or other) with 256 colors, not b&w.  
// new code --- taken from setImageType function:  
FIBITMAP * convertedBmp = FreeImage_ConvertTo24Bits(bmp);  
swap(convertedBmp);  
type = OF_IMAGE_COLOR;  
bpp = FreeImage_GetBPP(bmp);  
//	end new code  
//setImageType(OF_IMAGE_COLOR); // original code  
  

Rick

Hey Rick!

Thanks for finding this.
I was able to reproduce it with a 8bit color GIF.

The problem is that at that part in update() we have determined it is an 8bit color image which needs to be converted to 24bit rgb - but because it has OF_IMAGE_COLOR as its type (from the previous loadImage) when setImageType is called with newType set to OF_IMAGE_COLOR it sees that the type and newType are the same and dooesn’t bother to do the conversion. It then calls update which of course sets off the recursive loop!

To fix:
Set ‘type’ to OF_IMAGE_UNDEFINED in loadImage to clear the type from any previous images that have been loaded.
Also set type to OF_IMAGE_UNDEFINED in update when we are about to covert it from 8bit color to 24bit color.

  
//-------------------------  
void ofImage::loadImage(string fileName){  
  fileName = ofToDataPath(fileName);  
  bool bLoaded = false;  
  type = OF_IMAGE_UNDEFINED;  

  
//  convert to RGB -  
//  this is a gif (or other) with 256 colors, not b&w.			  
//  FIX: we set from OF_IMAGE_COLOR to   
//OF_IMAGE_UNDEFINED as setImageType only converts to rgb 24   
//  if the image type is not OF_IMAGE_COLOR   
type = OF_IMAGE_UNDEFINED;  
setImageType(OF_IMAGE_COLOR);  

Just added this 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

Cheers!
Theo