I want to get a UIImage from Pix without doing any read/write to file operations. I am getting a CGBitmapContextCreate error.
Here’s the Pix structure:
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;
The full documentation can be found here: http://tpgit.github.com/Leptonica/pix_8h_source.html
Here’s my attempt at it:
- (UIImage *) getImageFromPix:(PIX *) thePix
{
CGColorSpaceRef colorSpace;
uint32_t *bitmapData;
size_t bitsPerComponent = thePix->d;
size_t width = thePix->w;
size_t height = thePix->h;
size_t bytesPerRow = thePix->wpl * 4;
colorSpace = CGColorSpaceCreateDeviceGray();
if(!colorSpace) {
NSLog(@"Error allocating color space Gray\n");
return NULL;
}
// Allocate memory for image data
bitmapData = thePix->data;
if(!bitmapData) {
NSLog(@"Error allocating memory for bitmap\n");
CGColorSpaceRelease(colorSpace);
return NULL;
}
//Create bitmap context
CGContextRef context = CGBitmapContextCreate(bitmapData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaNone);
if(!context) {
free(bitmapData);
NSLog(@"Bitmap context not created");
}
CGColorSpaceRelease(colorSpace);
CGImageRef imgRef = CGBitmapContextCreateImage(context);
UIImage * result = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);
CGContextRelease(context);
return result;
}
Here’s the error I am getting:
<Error>: CGBitmapContextCreate: unsupported parameter combination: 1 integer bits/component; 1 bits/pixel; 1-component color space; kCGImageAlphaNone; 16 bytes/row.
CGBitmapContextCreate()does not support every combination of pixel formats. There may be many Pix formats that it does not support. A full list of supported formats can be found in the Quartz 2D Programming Guide. The formats supported on iOS are fairly limited. You may need to convert what you are getting from Pix to one of supported formats. Its hard to do this in general but for your specific case if you always get 1 bits/pixel, 1 bits/component, noalpha channel, it should not be too hard to convert to the Gray format of 8 bits/pixel, 8 bits/component, no alpha channel that is supported on iOS.