Sumit Kumar
Sumit Kumar

Reputation: 422

Coverting a Byte array to CGImage

I have a grayscale values of an image in an unsigned char array and I want to convert that into CGImage so that I can use it to display it in iOS via UIImage. Each value of unsigned char array is pixel value of grayscale image. I am using the following code but image is not displayed.

-(void)viewDidLoad
{
[super viewDidLoad];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"rectange4obs1pro" ofType:@"pgm"];
NSFileManager *fm = [NSFileManager defaultManager];
/////////////// read file ///////////
FILE *file = fopen([filename UTF8String], "rb");
if (file == NULL) {
    NSLog(@"File Not Found");
    return;
}
char name[20], secondline[10];
int w=0, h=0, colors=0;
fscanf(file, "%s", name);
fscanf(file, "%s", secondline);
fscanf(file, "%d %d %d", &w, &h, &colors);
printf("%d %d\n", w, h);
unsigned char * imagedata = (unsigned char *) malloc(sizeof(unsigned char)*w*h);
fseek(file, 1, SEEK_CUR);
int i=0;
while (!feof(file)) {
    imagedata[i] = getc(file);
    i++;
}
////////////////// end read file /////////////

CGColorSpaceRef colorSpace= CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext=CGBitmapContextCreate(imagedata, w, h, 8, w, colorSpace,  kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CFRelease(colorSpace);
free(imagedata);
CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);

UIImage * newimage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
[imageview setImage:newimage];
}

The message I am getting on terminal is:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipLast; 100 bytes/row. Jan 31 15:25:50 sumits-mbp.bsb.igloonet check_visibility_image[4210] : CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Upvotes: 2

Views: 1080

Answers (1)

Sumit Kumar
Sumit Kumar

Reputation: 422

This code is working now, I got the issue.

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"10CamsHGallery" ofType:@"pgm"];
NSFileManager *fm = [NSFileManager defaultManager];
/////////////// read file ///////////
FILE *file = fopen([filename UTF8String], "rb");
if (file == NULL) {
    NSLog(@"File Not Found");
    return;
}
char name[20], secondline[10];
int w=0, h=0, colors=0;
fscanf(file, "%s", name);
fscanf(file, "%s", secondline);
fscanf(file, "%d %d %d", &w, &h, &colors);
//printf("%d %d\n", w, h);
unsigned char * imagedata = (unsigned char *) malloc(sizeof(unsigned char)*w*h);
fseek(file, 1, SEEK_CUR);
int i=0;
while (!feof(file)) {
    int val = getc(file);
    imagedata[i] = val;
    i++;
}
////////////////// end read file /////////////

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CFDataRef rgbData = CFDataCreate(NULL, imagedata, w * h);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
CGImageRef rgbImageRef = CGImageCreate(w, h, 8, 8, w , colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
CFRelease(rgbData);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);
UIImage * newimage = [UIImage imageWithCGImage:rgbImageRef];
imageview.frame = CGRectMake(imageview.frame.origin.x,imageview.frame.origin.y, w, h);
[imageview setImage:newimage];
CGImageRelease(rgbImageRef);

}

Upvotes: 0

Related Questions