从 UIImage/CGImage 转换为 Leptonica Pix 结构
我想在我的 iOS 应用程序中使用 Leptonica 库进行图像处理,但在弄清楚如何从 UIImage
获取 Leptonica 的 Pix
结构时遇到了麻烦。我建议做的是类似以下的事情:
UIImage *image = [UIImage imageNamed:@"test.png"];
...
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *rasterData = CFDataGetBytePtr(data);
任何人都可以建议如何正确地将这些数据转换为Leptonica的Pix结构:
/*-------------------------------------------------------------------------*
* 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 */
};
UPD:
我正在这样做:
UIImage *image = [UIImage imageNamed:@"test.png"];
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [image CGImage];
myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerComponent(myCGImage);
myPix->wpl = CGImageGetBytesPerRow (myCGImage) / 4;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;
NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));
但是我得到的与原始图片有很大不同:
http://dl.dropbox.com/u/4409984/so/lept-orig.png
与
http://dl.dropbox.com/u/4409984/so/lept-res.png
我在做什么 错误的?
I want to use Leptonica library in my iOS application for image processing and have troubles figuring out how to get Leptonica's Pix
structure from UIImage
. What I suggest to do is something like the following:
UIImage *image = [UIImage imageNamed:@"test.png"];
...
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *rasterData = CFDataGetBytePtr(data);
Can anyone suggest how to correctly convert this data to 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 */
};
UPD:
I am doing like this:
UIImage *image = [UIImage imageNamed:@"test.png"];
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [image CGImage];
myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerComponent(myCGImage);
myPix->wpl = CGImageGetBytesPerRow (myCGImage) / 4;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;
NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));
But what I get is quite different from the original picture:
http://dl.dropbox.com/u/4409984/so/lept-orig.png
vs.
http://dl.dropbox.com/u/4409984/so/lept-res.png
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下代码:
Try following code:
一旦您拥有 CGImage,您就可以直接从 CGImage 获取大部分信息。
Once you have a CGImage, you can get most of that information directly from the CGImage.