2

I need to import a PNG and display it on screen in a Motif application. For reasons best known to myself, I don't want to use any more libraries than I need to, and I'd like to stick with just Motif and pnglib.

I've been battling with this for a couple of days now, and I'd like to put aside my pride and ask for some help. This screenshot shows the problem:

https://s3.amazonaws.com/gtrebol264929/pnglib_fail.png

The window on the right shows what the image should look like, the window on the left is my Motif application showing what it looks like in my app. Clearly I've got the image data OK, as the basic concept of the picture can be seen. But also clearly I've messed up how I get the pixel data from pnglib into an XImage. Below is my code:

char * xdata = malloc(width * height * (channels + 1));
memset(xdata,100,width * height * channels);    

int colc = 0;
int bytec = 0;
while (colc < width) {
    int rowc = 0;
    while(rowc < height) {
        png_byte * row = png.row_pointers[rowc];
        memcpy(&xdata[bytec],&row[colc],1);
        bytec += 4;

        rowc += 1;
    }
    colc += 1;
}



XImage * img = XCreateImage(display, CopyFromParent, depth * channels, ZPixmap, 0, xdata, width, height, 32, bytes_per_line);
printf("PNG %ix%i (depth: %i x %i) img: %p\n",width,height,depth,channels,img);


XPutImage (display, win, gc, img, 0, 0, 0, 0, width, height); // 0, 0, 0, 0 are src x,y and dst x,y

png.row_pointers is the pixel data from pnglib.

I'm pretty sure I've just misunderstood how the pixel data is stored, but I can't quite work out what I've done wrong. Any help is very much appreciated.

All the best

Garry

Garry
  • 623
  • 1
  • 7
  • 11
  • 1
    The picture looks like the rows and columns are the wrong way round. – parkydr Apr 03 '13 at 19:42
  • Thank you, I have resolved that now, and now I have this: https://s3.amazonaws.com/gtrebol264929/pnglib_better.png maybe I'm reading too many bytes per pixel or something? Any help appreciated. – Garry Apr 04 '13 at 10:28
  • 1
    The rows look right now, but the pixels are too wide and the colour is off. Looks like a png pixel is double the bytes of an X pixel. – parkydr Apr 04 '13 at 11:10
  • Yes, thank you, I swapped around the Red and Blue channels, and colour was corrected. Then I made sure I was copying all channels for each pixel, and boom, it worked! Thanks very much for the help. – Garry Apr 04 '13 at 20:15
  • 1
    It could be useful if you answer your question with the solution you found. – Ivan Nov 01 '13 at 12:14

0 Answers0