im having a massive memory leak
its the fJPEG( BYTE* jpegBytes ) func
here goes the code:
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofSetFrameRate(30);
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(){
//dispImg.clone(fJPEG((BYTE*)buffer));
fJPEG((BYTE*)buffer).draw(0,0,1024,768);
}
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.push_back(huffval);
cout<<"kkk "<<huffV[j]<<endl;
}
result[i]=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;
ofImg.setFromPixels(FreeImage_GetBits(img),FreeImage_GetWidth(img),FreeImage_GetHeight(img),OF_IMAGE_COLOR,0);
return ofImg;
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
//cout<<ofToDataPath("bg.jpg")<<endl;
FreeImage_Save(FIF_JPEG, img, "../../../data/prueba.jpg", JPEG_QUALITYSUPERB);
}
//--------------------------------------------------------------
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(){
}