Toby Toby - 1 year ago 151
C++ Question

Convert Leptonica Pix Object to QPixmap ( or other image object )

I'm using the Leptonica Library to process some pictures. After that I want to show them in my QT GUI. Leptonica is using their own format Pix for the images, while QT is using their own format QPixmap. At the moment the only way for me is to save the pictures after processing as a file ( like bmp ) and then load them again with a QT function call. Now I want to convert them in my code, so I dont need to take the detour with saving them on the filesystem. Any ideas how to do this?

Best Regards

// edit:

Okay as already suggested I tried to convert the PIX* to a QImage.
The PIX* is defined like this:

struct Pix
l_uint32 w; /* width in pixels */
l_uint32 h; /* height in pixels */
l_uint32 d; /* depth in bits */
l_uint32 wpl; /* 32-bit words/line */
l_uint32 refcount; /* reference count (1 if no clones) */
l_int32 xres; /* image res (ppi) in x direction */
/* (use 0 if unknown) */
l_int32 yres; /* image res (ppi) in y direction */
/* (use 0 if unknown) */
l_int32 informat; /* input file format, IFF_* */
char *text; /* text string associated with pix */
struct PixColormap *colormap; /* colormap (may be null) */
l_uint32 *data; /* the image data */

while QImage offers me a method like this:

QImage ( const uchar * data,
int width,
int height,
int bytesPerLine,
Format format )

I assume I cant just copy the data from the PIX to the QImage when calling the constructor. I guess I need to fill the QImage Pixel by Pixel, but actually I dont know how? Do I need to loop through all the coordinates? How do I regard the bit depth? Any ideas here?

Answer Source

Well I could solve the problem this way:

Leptonica offers a function

l_int32     pixWriteMemBmp (l_uint8 **pdata, size_t *psize, PIX *pix)

With this function you can write into the memory instead of a filestream. Still ( in this example ) the Bmp Header and format persists ( there are the same functions for other image formats too ).

The corresponding function from QT is this one:

bool QImage::loadFromData ( const uchar * data, int len, const char * format = 0 )

Since the the Header persits I just need to pass the data ptr and the size to the loadFromData function and QT does the rest.

So all together it would be like this:

PIX *m_pix;
FILE * pFile;
pFile = fopen( "PathToFile", "r" );
m_pix = pixReadStreamBmp(pFile); // If other file format use the according function
// Now we have a Pix object from leptonica

l_uint8* ptr_memory;
size_t len;
pixWriteMemBmp(&ptr_memory, &size, m_pix);
// Now we have the picture somewhere in the memory

QImage testimage;
QPixmap pixmap;
testimage.loadFromData((uchar *)ptr_memory,len);
// Now we have the image as a pixmap in Qt

This actually works for me, tho I don't know if there is a way to do this backwards so easy. ( If there is, please let me know )

Best Regards

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download