crash solution?: force ofImage to load grayscale images = 24

Hi - my test app is an image viewer that does face tracking. It loads images from a user specified directory and then uses the ofxCvHaarTracker addon to find faces. (this is not the final app - I’m just trying to build a piece at a time).

–the source images are various sizes, types and bit depths.
–running on OSX10.58, Xcode 3.1.2, OF v62.

Ok- the functionality is that the user can press the arrow keys to step through a list of images in the directory they have specified. I use the code below (at bottom) to clear and then reallocate memory (is this right?? I have no idea :oops: ). It *seems* like this is working ok. I was getting crashes though on specific images - that were grayscale. Looking through the ofImage code it seems like grayscale images were not being converted to 24 bit and then the openCV/ofxCvColorImage code would choke at ‘setFromPixels’.

So… I changed the ofImage code to force grayscale images to be converted to 24 bit as a crazy test and so far that seems to have eliminated the crashes I was seeing.

My question (finally…): is this kosher? Can I just change that?? Should ofImages always be forced to 24 bit or have some flag in ‘loadImage’ to as an option?

Anyway - all of the of openCV examples I have seen are video-based with consistent images sizes/types so perhaps this hasn’t come up before.

–sTOD

—changed ofImage code (I guess the ‘if (bPallete)’ test is no longer needed if all 8 bit images are to be converted to 24 bit).

  
  
		switch (bpp){  
			case 8:  
				if (bPallette) {  
					FIBITMAP 	* bmpTemp =		FreeImage_ConvertTo24Bits(bmp);  
					if (bmp != NULL)			FreeImage_Unload(bmp);  
					bmp							= bmpTemp;  
					bpp							= FreeImage_GetBPP(bmp);  
				} else {  
					// do nothing we are grayscale  
//*********************************************  
//*********************************************  
//*********************************************  
  
					ofLog(OF_LOG_ERROR,"grayscale image");  
					// added- 2.5.11 --st  
					FIBITMAP 	* bmpTemp =		FreeImage_ConvertTo24Bits(bmp);  
					if (bmp != NULL)			FreeImage_Unload(bmp);  
					bmp							= bmpTemp;  
					bpp							= FreeImage_GetBPP(bmp);  
  
//*********************************************  
//*********************************************  
//*********************************************  
				}  
				break;  
			case 24:  
				// do nothing we are color  
				break;  
			case 32:  
				// do nothing we are colorAlpha  
				break;  
			default:  
				FIBITMAP 	* bmpTemp =		FreeImage_ConvertTo24Bits(bmp);  
				if (bmp != NULL)			FreeImage_Unload(bmp);  
				bmp							= bmpTemp;  
				bpp							= FreeImage_GetBPP(bmp);  
		}  
  

– code for loading arbitrary image. Is the clearing/reallocating correct?

  
  
void testApp::displayImage(){  
   
		cImage.clear();  
		cImagePath = imageList[imageIndex];  
		cout << cImagePath << endl;  
		cImage.loadImage(imageList[imageIndex]);  
  		  
		WIDTH = cImage.getWidth();  
		HEIGHT = cImage.getHeight();  
  
		SAMPLE_WIDTH = (WIDTH /2);  
		SAMPLE_HEIGHT = (HEIGHT / 2);  
		  
		//cout << WIDTH << " " << HEIGHT << " " << SAMPLE_WIDTH << " " << SAMPLE_HEIGHT << endl;  
		if (firstLoad == false){  
			// free memory before new image  
			colorLargeImage.clear();  
			colorSmallImage.clear();  
			grayLargeImage.clear();  
			graySmallImage.clear();  
		}  
		firstLoad = false;  
		colorLargeImage.allocate( WIDTH, HEIGHT );  
		colorSmallImage.allocate( WIDTH /2, HEIGHT /2 );  
		grayLargeImage.allocate( WIDTH, HEIGHT );  
		graySmallImage.allocate( WIDTH /2, HEIGHT /2 );  
		  
		sourceToSampleScale = WIDTH / (WIDTH / 2);  
		sampleToSourceScale	= (float(WIDTH) / 2) / (float)WIDTH;  
		  
		colorLargeImage.setFromPixels( cImage.getPixels(), cImage.getWidth(), cImage.getHeight() );  
		colorSmallImage.scaleIntoMe( colorLargeImage );  
		grayLargeImage	= colorLargeImage;  
		graySmallImage	= colorSmallImage;  
		  
		haarTracker.clearHaarItems();  
		newImage = true;  
}  

hi you dont’ need to modify ofImage, just call:

  
img.setImageType(OF_IMAGE_COLOR)  

on every image, images that are 8bit will be converted to 24bit