Create Leptonica PIX struct from UIImage

无人久伴 提交于 2019-12-25 15:50:50

问题


I want to create a PIX data structure from a UIImage. The struct of 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;


struct PixColormap
 {
     void            *array;     /* colormap table (array of RGBA_QUAD)     */
     l_int32          depth;     /* of pix (1, 2, 4 or 8 bpp)               */
     l_int32          nalloc;    /* number of color entries allocated       */
     l_int32          n;         /* number of color entries used            */
};

How can I do this in iOS?


回答1:


You can create a CGBitmapContext and draw the image into it. You need to use the RGB color space to create the context. You may need to experiment with kCGBitmapByteOrder32Little and kCGBitmapByteOrder32Big in the bitmapInfo argument to get the R, G, and B components in the correct order.

Then you can initialize your struct Pix from that, using functions like CGBitmapContextGetWidth, CGBitmapContextGetBytesPerRow, and CGBitmapContextGetData. The xres, yres, informat, text, and colormap elements don't correspond to any attributes of CGBitmapContext or UIImage, so you should probably set them to 0 or NULL.

Take a look at the CGBitmapContext Reference for help with CGBitmapContext.

Also look at the Quartz 2D Programming Guide for help creating a bitmap context, and for help drawing your image into the context.




回答2:


You will start with a UIImage, then get a CGImageRef from that using "CGImage". With a CGImageRef you can query it for the width, height, depth, wpl (which will you will get as bytesPerRow from the image, then modify by dividing by the pixel size). To get the data you need to render it into a context, get the point, then copy that data into a NSData object. The size will be the number of rows by the bytesPerRow value.



来源:https://stackoverflow.com/questions/11800177/create-leptonica-pix-struct-from-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!