1

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. 
2
  • Right - you want to draw 3 colors and alpha, but you specified a gray colorspace (ie one "color"). Try it with CGColorSpaceCreateDeviceRGB(). Oh, and you need alpha too since you have alpha in your PIX. You don't need alpha but later when you draw iOS will make it alpha for you - taking up space and CPU - so better to do it now. Commented Aug 3, 2012 at 23:06
  • I actually do only want it in one color. How can I do that? Commented Aug 6, 2012 at 14:30

1 Answer 1

2

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, no alpha 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.

Sign up to request clarification or add additional context in comments.

1 Comment

1 bpp, 1 bpc, no alpha means each bit represents a pixel value, either on or off. 8 bpp, 8 bpc, no alpha means each byte represent a gray pixel value from 0 to 255. So your translated bitmap will be 8 times larger than your original. You need to step through each bit of the original bitmap and if it is set you write 255 into the corresponding byte of the translated bitmap, otherwise 0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.