2

I created a binary file after taking a snapshot using X11 xGetImage and stored contents of data field in a binary file(The file has been zipped. Please uncompress it). Now, i was playing around a bit with CImg and learning its usage.

First Question

So, i tried CImg on this binary data using load_rgba function

CImgDisplay *disp;
CImg <float>img1,img2; //I don't why,it only works with float, with int it gives gray colour and with unsigned int it gives black foreground
img2 = img1.load_rgba("imagedata",1366,768);  //1366 X 768 is the dimension of my image that i got from X11
disp = new CImgDisplay(1024,768,"window");
disp->display(img2);

Now, i can see the image in the window, but there is a loss of quality. so i tried to have a look at the code and found that

at line 34318

assign(dimw,dimh,1,4); // the depth is assigned to 1. which i believe is the culprit, however i would like confirm it

and why it only works when float is passed for template??

Second Question Now i thought,to use CImg by first reading the file myself and then handing over the pointer of buffer to Cimg using this code

int main() {
    char *data;
    int size = 1366*768*4;   //1366 X 768 is the dimension of my image that i got from X11 and 4 is number of bits per pixel
    ifstream file ("imagedata", ios::in|ios::binary|ios::ate);
    data = new char[size];
    file.read (data, size);

    CImgDisplay *disp;
    CImg <float>img3(data,1366,768,1,4);
    disp = new CImgDisplay(1024,768,"window");
    disp->display(img3);
    getchar();
    return 0;   
}

Running this code on same imagedata(as in first case), all i get is a black window. Moreover setting 4th parameter(ie depth(z)) result in segmentation fault

What am i doing wrong here?

Ayush choubey
  • 606
  • 6
  • 23
  • With regards to Q1 - why are you reading the binary file as floats? Depending on the options passed to getXImage you should have either 8, 16 or 32 bit unsigned integers. – Andy Apr 22 '14 at 19:03
  • Good call, I forgot to add this in question, actually i started with int, but that resulted in gray image, then somewhere while googling, i found this way of passing float in template and it did work and i have no clue why. I would suggest, if you can please use the code and the sample image that i attached in the link, so that you can actually see, what actually has happened and may be evaluate it more closely.....thanks – Ayush choubey Apr 22 '14 at 19:15
  • I don't have CImg setup, hence I'm just commenting here. If you are reading a 32 bit image from X and reading that into a 4 byte CImg float, you will see loss of quality via rounding error. Were you using *unsigned* integers earlier or *signed* integers? – Andy Apr 22 '14 at 19:24
  • But that resulted in gray box in CImgDisplay and after your comment i tried with unsigned int and that resulted in pitch black image.... any idea – Ayush choubey Apr 22 '14 at 19:33
  • Yeah, print out the value of a pixel or two and make sure the value matches what you'd expect it to. Alternatively, create a 'test image' where you set specific values and types to specific locations. Primitive, but a good way to figure out what you are dealing with. – Andy Apr 22 '14 at 19:43

1 Answers1

0

Second Question Now i thought,to use CImg by first reading the file myself and then handing over the pointer of buffer to Cimg using this code

CImg data format: http://cimg.eu/reference/group__cimg__storage.html

If you want to init CImg object from buffer manually you should set up this arrays by yourself.

This is an example for jpeg image, for other formats you can check sources (functions names: _load_png, _load_jpeg etc.):

https://github.com/EyalAr/lwip/blob/master/src/decoder/jpeg_decoder.cpp

Nazar Sakharenko
  • 1,007
  • 6
  • 11