从 UIImage/CGImage 转换为 Leptonica Pix 结构

发布于 2025-01-01 07:23:59 字数 2683 浏览 0 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2025-01-08 07:23:59

尝试以下代码:

CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([picture CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);

Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [picture CGImage];

myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerPixel([picture CGImage]) ;
myPix->wpl = CGImageGetBytesPerRow (myCGImage)/4 ;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;

pixEndianByteSwap(myPix);

NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));

Try following code:

CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([picture CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);

Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [picture CGImage];

myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerPixel([picture CGImage]) ;
myPix->wpl = CGImageGetBytesPerRow (myCGImage)/4 ;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;

pixEndianByteSwap(myPix);

NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));
倾城月光淡如水﹏ 2025-01-08 07:23:59

一旦您拥有 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. ...

Once you have a CGImage, you can get most of that information directly from the 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. ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文