Question

I would like to convert an Image from one format(.png) to other image format (.img format). I was able to retrieve and modify the rgba values for the same image format.Is there any additional thing I need to do to convert it to other image formats?

I created an empty bitmap and would like to draw the to image this bitmap.

CGImageRef cgimage = image.CGImage;

size_t width  = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);

size_t bytesPerRow = CGImageGetBytesPerRow(cgimage);
size_t bytesPerPixel = CGImageGetBitsPerPixel(cgimage);
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgimage);
size_t bytes_per_pixel = bytesPerPixel / bitsPerComponent;

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgimage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
memset(rawData, 0, height * width * 4);

CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);

Are there any functions in iOS to fill the output bitmap with modified rgba values.

Was it helpful?

Solution

It would look something like this:

UIImage *image = self.theImage;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:height * width * 4];
unsigned char *rawData = data.mutableBytes;
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;

If you want to manipulate the data, you iterate through byteIndex . Hope that's what you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top