问题描述
我想在我的iOS应用中使用Leptonica库来处理图像.
I want to use Leptonica library in my iOS app to process images.
有人知道如何从Leptonica的Pix
结构中的原始数据创建UIImage
:
Does anybody knows how can I create UIImage
from the raw data in Leptonica's Pix
structure:
/*-------------------------------------------------------------------------*
* Basic Pix *
*-------------------------------------------------------------------------*/
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 */
};
typedef struct Pix PIX;
?
谢谢!
推荐答案
首先,您可能要签出:
First, you might want to check out: Convert Leptonica Pix Object to QPixmap ( or other image object )
我们想要的是找到Pix和UIImage都支持的通用格式,从Pix转换为该通用格式,然后从该通用格式转换为UIImage.
What we want is to find common formats that both Pix and UIImage support, convert from Pix to that common format, and then convert from the common format to UIImage.
通过查看Leptonica库,看起来受支持的常见格式为GIF,JPEG,TIFF,BMP和PNG. JPEG将是有损的,并且GIF和PNG都将导致CPU的额外工作(当我们从Pix转换为UIImage时,将会有额外的编码/解码周期).由于这些原因,我在下面的示例中选择了TIFF.如果不起作用,我会选择PNG.
From looking at the Leptonica library, it looks like the common supported formats are GIF, JPEG, TIFF, BMP, and PNG. JPEG will be lossy, and GIF and PNG will both result in additional work by the CPU (there will be an additional encode/decode cycle when we convert from Pix to UIImage). For these reasons, I chose TIFF in the example below. If it doesn't work, I would go with PNG.
计划如下:
- 1)从Pix转换为字节缓冲区
- 2)提取字节缓冲区并将其存储到NSData中
- 3)将数据传递到NSImage
看起来pixWriteMem()函数是我们需要的#1(前提是对它的支持已编译到库中).
It looks like the pixWriteMem() function is what we need for #1 (provided that support for it was compiled into the library).
通过查看库中包含的示例代码,看起来我们负责释放pixWriteMem()的输出-因此,我们将YES传递给NSData的freeWhenDone:参数.
From looking at the example code included with the library, it looks like we are responsible for freeing the output of pixWriteMem() - hence, we will pass YES into NSData's freeWhenDone: argument.
类似这样的东西(警告:未经测试的代码):
Something like this (warning: untested code):
UIImage *GetImageFromPix(Pix *thePix)
{
UIImage *result = nil;
l_uint8 *bytes = NULL;
size_t size = 0;
if (0 == pixWriteMem(&bytes, &size, thePix, IFF_TIFF)) {
NSData *data = [[NSData alloc] initWithBytesNoCopy:bytes length:(NSUInteger)size freeWhenDone:YES];
result = [UIImage imageWithData:data];
[data release];
}
return result;
}
这篇关于从Leptonica的Pix结构创建UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!