加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 从UIImage / CGImage转换为Leptonica Pix结构

发布时间:2020-12-16 06:58:45 所属栏目:百科 来源:网络整理
导读:我想在我的iOS应用程序中使用Leptonica库进行图像处理,并且弄清楚如何从UI Image获取Leptonica的Pix结构.我建议做的事情如下: UIImage *image = [UIImage imageNamed:@"test.png"];...CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider
我想在我的iOS应用程序中使用Leptonica库进行图像处理,并且弄清楚如何从UI Image获取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

我究竟做错了什么?

解决方法

一旦有了CGImage,就可以直接从CGImage中获取大部分信息.

CGImageRef myCGImage = [image CGImage];
struct Pix myPix;
myPix.w = CGImageGetWidth (myCGImage);
myPix.h = CGImageGetHeight (myCGImage);
myPix.d = CGImageGetBitsPerComponent (myCGImage);
myPix.wpl = CGImageGetBytesPerRow (myCGImage) / 4;
... etc. ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读