将Leptonica Pix对象转换为QPixmap(或其他图像对象) [英] Convert Leptonica Pix Object to QPixmap ( or other image object )

查看:921
本文介绍了将Leptonica Pix对象转换为QPixmap(或其他图像对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Leptonica Library处理一些图片。之后我想在我的QT GUI中显示它们。 Leptonica使用自己的格式Pix作为图像,而QT使用他们自己的格式QPixmap。目前,唯一的方法是将处理后的图片保存为文件(如bmp),然后再通过QT函数调用再次加载。现在我想在我的代码中转换它们,所以我不需要绕道而行将它们保存在文件系统中。有任何想法怎么做?

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?

最好的问候

//编辑:

好的,我已经建议将PIX *转换为QImage。
PIX *的定义如下:

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

http://tpgit.github.com/Leptonica/pix_8h_source.html

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                    */
};

而QImage为我提供了这样的方法:

while QImage offers me a method like this:

http://developer.qt.nokia。 com / doc / qt-4.8 / qimage.html#QImage-7

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

我假设在调用构造函数时我无法将数据从PIX复制到QImage。我想我需要填充Pixel的QImage Pixel,但实际上我不知道怎么做?我需要遍历所有坐标吗?我如何看待钻头深度?这里有什么想法吗?

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?

推荐答案

我可以用这种方式解决问题:

Well I could solve the problem this way:

Leptonica提供函数

Leptonica offers a function

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

使用此功能,您可以写入内存而不是文件流。仍然(在这个例子中)Bmp标题和格式仍然存在(其他图像格式也有相同的功能)。

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 ).

QT的相应功能是这一个:

The corresponding function from QT is this one:

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

由于Header持续存在,我只需要将数据ptr和大小传递给loadFromData函数,QT完成剩下的工作。

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
fclose(pFile);
// 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);
pixmap.convertFromImage(testimage);
// 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 )

最好的问候

这篇关于将Leptonica Pix对象转换为QPixmap(或其他图像对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆