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.