The root of this question is: What is the bit-by-bit format of the OpenCV IplImage->imageData property?
Background: I'm using Python's ctypes to allow pythonic access to a low-level C library that uses OpenCV. I've been able to get almost all the functions accessible from python, but I'm stuck on this one that demands the data of the old OpenCV struct known as IplImage, specifically the imageData property. I can't figure out how IplImage->imageData is organized versus how python's cv2.cv.LoadImage's iplimage type, which has ostensibly the same data as the C struct, but it appears to be organized differently.
So for example, I have a 4-pixel image that is 2x2 pixels. Top left pixel is 100% RED. Top right pixel is 100% GREEN. Bottom left pixel is 100% BLUE, Bottom right pixel is 100% white.
In python the information looks like this:
import cv2
img = cv2.cv.LoadImage('rgbw.png')
pixels = []
for ch in img.tostring():
pixels.append(ord(ch))
print pixels
[0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255]
Which makes sense to me: The first three values [0, 0, 255] represent B:0, G:0, R:255, the red pixel. The second, is green, the third is the lower-left, blue, and the last lower right is white.
I marshal this into the library and it library behaves fine, but it doesn't appear to "see" anything in the imageData (I get a return code that means "I saw nothing" when clearly this data is comprehensible when I pass it into the library using the C api directly.
So of course I suspect the C IplImage->imageData has the data organized completely differently, so I look in the debugger and find to my surprise that not only is the data different, but I can't understand it: here it is, starting with a cvLoadImage("rgbw.png") assigning it to an IplImage struct called 'image'.
Breakpoint 1, main (argc=2, argv=0x7fffffffe418) at IplImageInfo.cpp:44
44 printf("imageData %s\n", image->imageData);
(gdb) x/16ub image->imageData
0x618c90: 0 0 255 0 255 0 0 0
0x618c98: 255 0 0 255 255 255 0 0
(gdb)
So comparing it byte-by-byte, adding zeros for comparison's sake:
Python:
000 000 255 | 000 255 000 | 255 000 000 | 255 255 255
C: (printing the first 16 bytes, not 12, which is what I'd expect, see below)
000 000 255 | 000 255 000 | 000 000 255 | 000 000 255 | 255 255 000 | 000
Notice the first six bytes are the same in both. But then, what's going on? We have another TWO RED pixels, then ... a Cyan pixel? Another thing, this file is 12 bytes in size (4 pixels, 3 bytes each). When I print out the image->imageSize property from C, I get 16, not 12. So something is rotten I don't get it. Clearly there's something wrong with my model of what's in imageData. Can you explain it?