Page 1 of 2

gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 7:05 pm
by tiboy08
Hi,

I have a problem when loading a gflBitmap (from a standard unsigned char * buffer) using the gflLoadBitmapFromMemory function. The function call returns the error code 6 ("Unknown format") only under linux, under windows there is no problem ... I post the tested code below :
#include <iostream>
#include <libgfl.h>

void PgmRead(int & sx, int & sy, unsigned char ** buffer, const char * filename){

FILE * fic = fopen(filename, "rb");
fscanf(fic, "P5\n");
fscanf(fic, "%d %d\n", &sx, &sy);
fscanf(fic, "255\n");
(*buffer) = new unsigned char[sx*sy];
fread((*buffer), sizeof(unsigned char), sx*sy, fic);
fclose(fic);

}

int main(int argc, char ** argv){

gflLibraryInit();

unsigned char * image = 0;
int sx, sy;
PgmRead(sx, sy, &image, "test.pgm");

GFL_LOAD_PARAMS load_params;
gflGetDefaultLoadParams(&load_params);
load_params.FormatIndex = gflGetFormatIndexByName("raw");
load_params.Origin = GFL_TOP_LEFT;
load_params.Width = sx;
load_params.Height = sy;
load_params.Offset = 0;
load_params.ChannelOrder = GFL_CORDER_INTERLEAVED;
load_params.ChannelType = GFL_CTYPE_GREYSCALE;

GFL_BITMAP * bitmap = 0;
bitmap = gflAllockBitmap(GFL_GREY, sx, sy, 1, NULL);

std::cout << gflGetErrorString(gflLoadBitmapFromMemory(image, sx*sy, &bitmap, &load_params, NULL)) << std::endl;

gflLibraryExit();

return 0;

}
returns "Unknown format". Can you please give me some information about this problem ??

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 8:19 pm
by xnview
Why you don't load ppm data and use gflGetFormatIndexByName("ppm")?

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 8:52 pm
by tiboy08
Because, in my program (not in this little test code), the greyscale buffer is computed during by a previous step and is already in memory ....

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 8:54 pm
by xnview
tiboy08 wrote:Because, in my program (not in this little test code), the greyscale buffer is computed during by a previous step and is already in memory ....
So perhaps the better way is to create GFL_BITMAP structure, and to copy data only

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 8:57 pm
by tiboy08
Ok, so i create an GFL_BITMAP instance using the gflAllockBitmap function, then operate a memcpy on the GFL_BITMAP::Data ??

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 8:59 pm
by xnview
tiboy08 wrote:Ok, so i create an GFL_BITMAP instance using the gflAllockBitmap function, then operate a memcpy on the GFL_BITMAP::Data ??
yes, more simple :)

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 9:00 pm
by tiboy08
ok thank you, i will try this now ....

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 9:09 pm
by tiboy08
Ok, seems to work correctly now. Thank you !! I will contact you in case of further issues.

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Tue Feb 15, 2011 9:48 pm
by tiboy08
Ok, so the first problem is solved. But it remains one, i work on a image compression program so this is what I need to do :

1) compress an unsigned char * buffer in PNG format into a memory buffer (using GFL_BITMAP & gflSaveBitmapIntoMemory) (seems to work)
2) From the PNG bitstream, retrieve the original unsigned char * buffer using (gflLoadBitmapFromMemory) (produces segfault)

This is the resulting C++ code :
int main(int argc, char * argv[]){

gflLibraryInit();

unsigned char * image = 0;
unsigned char * imagePNG = 0;
unsigned int imagePNGSize = 0;
unsigned char * image_decomp = 0;

int sx, sy;
PgmRead(sx, sy, &image, "test.pgm");

GFL_SAVE_PARAMS save_params;
gflGetDefaultSaveParams(&save_params);
save_params.FormatIndex = gflGetFormatIndexByName("png");
save_params.Compression = GFL_NO_COMPRESSION;
save_params.CompressionLevel = 7;

GFL_LOAD_PARAMS load_params2;
gflGetDefaultLoadParams(&load_params2);
load_params2.FormatIndex = gflGetFormatIndexByName("png");
load_params2.Origin = GFL_TOP_LEFT;
load_params2.Width = sx;
load_params2.Height = sy;
load_params2.Offset = 0;
load_params2.ChannelOrder = GFL_CORDER_INTERLEAVED;
load_params2.ChannelType = GFL_CTYPE_GREYSCALE;


//Creating a bitmap for PNG compression into memory
GFL_BITMAP * bitmap = 0;
bitmap = gflAllockBitmap(GFL_GREY, sx, sy, 1, NULL);
bitmap->Data = new GFL_UINT8[sx*sy];
memcpy(bitmap->Data, image, sx*sy*sizeof(unsigned char));
gflSaveBitmapIntoMemory(&imagePNG, &imagePNGSize, bitmap, &save_params);

//Retrieve an unsigned char * buffer from the PNG bitstream
GFL_BITMAP * bitmap2 = 0;
bitmap2 = gflAllockBitmap(GFL_GREY, sx, sy, 1, NULL);
gflLoadBitmapFromMemory(imagePNG, imagePNGSize, &bitmap2, &load_params2, NULL); // ---> produces segfault

image_decomp = new unsigned char[sx*sy];
memcpy(image_decomp, bitmap2->Data, sx*sy*sizeof(unsigned char));

gflLibraryExit();

return 0;

}

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Wed Feb 16, 2011 7:42 am
by xnview
No need to allocate bitmap before to load it

Code: Select all

bitmap2 = gflAllockBitmap(GFL_GREY, sx, sy, 1, NULL);
Do you use the last version?

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Wed Feb 16, 2011 1:45 pm
by tiboy08
Ok for the allocation, still resulting in seg fault. I use the version 3.40 of the library, is there a newer ?? Or is this bug not present in older versions ??

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Thu Feb 17, 2011 4:07 pm
by tiboy08
any clue ??

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Thu Feb 17, 2011 9:01 pm
by xnview
I've tried, but no crash

One thing

Code: Select all

bitmap = gflAllockBitmap(GFL_GREY, sx, sy, 1, NULL);
bitmap->Data = new GFL_UINT8[sx*sy];
memcpy(bitmap->Data, image, sx*sy*sizeof(unsigned char));
gflSaveBitmapIntoMemory(&imagePNG, &imagePNGSize, bitmap, &save_params);
No need to make a new, gflAllockBitmap allocate data

Could you send me PgmRead & test.pgm?

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Thu Feb 17, 2011 10:37 pm
by tiboy08
Here is my code for the test program with the .pgm test file ... Have you made the test under windows or linux ?? I'm using ubuntu 10.04 32bits ...
test_GFL.tar.gz
(219.02 KiB) Downloaded 153 times

Re: gflLoadBitmapFromMemory problem under linux ??

Posted: Thu Feb 17, 2011 10:43 pm
by tiboy08
Here is the stack trace from my debugger ...
stack-trace.jpeg
stack-trace.jpeg (46.21 KiB) Viewed 3034 times