how to draw a FIBITMAP

hi. im getting a FIBITMAP from a memorystream using freeimage. now i want to draw it to the screen, but i canot cast it to ofImage. how should i do it?
this is my code:
bufer comes from fread (buffer,1,lSize,pFile); // pfile is a jpg file

  
  
fJPEG((BYTE*)buffer);  
FIMEMORY* memBuff;  
FIBITMAP* img;  
void  testApp::fJPEG( BYTE* jpegBytes )  
{  
  
    
 memBuff= FreeImage_OpenMemory	(	jpegBytes,lSize	 )	;  
   
 img=FreeImage_LoadFromMemory	(	FIF_JPEG,memBuff, 0	 );	  
   
}  
  
  

im getting this error
error: conversion from ‘FIBITMAP*’ to non-scalar type ‘ofImage’ requested

my first thought is – can you get the pixels from the FIBITMAP and then use ofImage::setFromPixels(…) to upload that data into an ofImage?

  • zach

i can get width height bpp from my fibitmap, but im not finding any func to get pixels

the func in ofImage setFromPixels needs an unsigned char* as source of pixels. FIBITMAP is a void*. maybe i can usit directly. ill try and pos results.

hmm I dont’ think that’s a good idea to cast – the data needs to be gotten to in a different way. actually, there is a good code in ofImage to see how to get pixels out of the FIBITMAP* – see update() and especially:

FreeImage_ConvertToRawBits

ofImage has a lot of code that shows different functions of freeImage.

one thing to point out is that in relationship to the question – you might have better luck asking in the freeImage forums about how to use things like FIBITMAP, etc

http://sourceforge.net/forum/?group-id=11504

although we know abit about freeImage since of uses it, you’ll find better support and better info closer to the source…

  • zach

i’ve got it working, but still with some problems
the image is upside down and skewed.
this is the whole code

  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){	   
  
    
size_t result;  
 string fileName = ofToDataPath("bg.jpg");   
	pFile = fopen ( fileName.c_str(), "rb" );   
  if (pFile==NULL) {fputs ("File error ",stderr);  std::exit(1) ;}  
  
  // obtain file size:  
  fseek (pFile , 0 , SEEK_END);  
  lSize = ftell (pFile);  
  rewind (pFile);  
  
  // allocate memory to contain the whole file:  
  buffer = (char*) malloc (sizeof(char)*lSize);  
  if (buffer == NULL) {fputs ("Memory error",stderr); std::exit(2) ;}  
  
  // copy the file into the buffer:  
  result = fread (buffer,1,lSize,pFile);  
   
 for (int i=0; i<lSize;i++){  
// printf( "%i \n", (int)buffer[i]);  
  
  //cout <<(int) (buffer[i]&0xff) <<" \ "<<(int) (buffer[i])<< endl;  
    
   }   
  if (result != lSize) {fputs ("Reading error",stderr);  std::exit(3) ;}  
  
  /* the whole file is now loaded in the memory buffer. */  
  
  // terminate  
  fclose (pFile);  
 // free (buffer);  
   
dumpHDT(buffer);  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
ofImage dispImg;  
dispImg.clone(fJPEG((BYTE*)buffer));  
	dispImg.draw(0,0);  
}  
vector<int> dhtHeaders;  
  
vector< vector<int> > testApp::dumpHDT( char* data){  
  
	for (int i=0; i<lSize; i++){  
       if(data[i]==-1){ // find ff  
  
			if(data[i+1]==-60){ //find c4  
				if(int(data[i+4])>15){ // check fot dtc table  
				dhtHeaders.push_back( i);  
				}  
  
			}  
			if(data[i+1]==-38){ //check for next different marker (end of huffman marker)  
				break;  
			}  
		}  
	}  
	vector< vector<int> > result;  
	result.resize( dhtHeaders.size() );  
	unsigned char* unsigned_buffer = (unsigned char*) data;   
	for(int i=0; i<dhtHeaders.size() ; i++){  
    
		int16_t my_16_bit_int = unsigned_buffer[dhtHeaders[i]+2]*256+unsigned_buffer[dhtHeaders[i]+3]-19; //lenght of the huffman table vals at ffc4 marker data  
		vector<int> huffV;  
		for(int j=0; j< my_16_bit_int ; j++){  
        
			int16_t huffval = unsigned_buffer[dhtHeaders[i]+j+21]; // huffmant vals  
			//cout<<huffval<<endl;  
  
			//huffV[j].push_back((int)huffval);  
			//cout<<"kkk "<<result[i][j]<<endl;  
  
		}  
		//result[i].push_back(huffV);  
	}  
  
	return result;  
  
}  
  
ofImage  testApp::fJPEG( BYTE* jpegBytes )  
{  
  
  
    
 memBuff= FreeImage_OpenMemory	(	jpegBytes,lSize	 )	;  
   
 img=FreeImage_LoadFromMemory	(	FIF_JPEG,memBuff, 0	 );	  
 int w=FreeImage_GetWidth(img);  
 int h=FreeImage_GetHeight(img);  
 int bpp=FreeImage_GetBPP(img);  
 // cout<< w<<"|"<<h<<"|"<<bpp<<endl;  
  ofImage ofImg;  
  ofImg.allocate(w, h, OF_IMAGE_COLOR);  
  ofImg.setFromPixels(FreeImage_GetBits(img),w,h,OF_IMAGE_COLOR,0);  
   
   
return ofImg;  
   
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){   
	  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased  (int key){   
	  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(){  
  
}  
  
  

any idea?

the app is running now, but the image shows flipped horizontally and vertically . i also noticed that it consumes almost all my ram and the vmemory keeps increasing