Skip to content

Instantly share code, notes, and snippets.

@mntone
Created February 25, 2019 07:02
Show Gist options
  • Save mntone/51725d09f1122bda846cc189edab77b6 to your computer and use it in GitHub Desktop.
Save mntone/51725d09f1122bda846cc189edab77b6 to your computer and use it in GitHub Desktop.
CGImage (RGB) from webp
#import <Foundation/Foundation.h>
#import <ImageIO/ImageIO.h>
#import <UIKit/UIKit.h>
#include "../libwebp/src/webp/types.h"
#include "../libwebp/src/webp/decode.h"
#include "../libwebp/src/webp/demux.h"
#import "WebPImageSource.h"
CGImageRef create_webp_rgb(size_t width, size_t height, const uint8_t *ptr, size_t length, CGColorSpaceRef colorSpace) {
const size_t bytesPerPixel = 3;
const size_t bytesPerRow = align16(bytesPerPixel * width);
const size_t size = bytesPerRow * height;
const CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little;
uint8_t *data = (uint8_t *)malloc(size);
if (!data) {
return NULL;
}
if (!WebPDecodeRGBInto(ptr, length, data, size, (int)bytesPerRow)) {
return NULL;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, size, bufferFree);
CGImageRef image = CGImageCreate(width, height, 8, 8 * bytesPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment