diff options
Diffstat (limited to 'frontends/cocoa/bitmap.m')
-rw-r--r-- | frontends/cocoa/bitmap.m | 285 |
1 files changed, 143 insertions, 142 deletions
diff --git a/frontends/cocoa/bitmap.m b/frontends/cocoa/bitmap.m index 30127e360..597a5af79 100644 --- a/frontends/cocoa/bitmap.m +++ b/frontends/cocoa/bitmap.m @@ -40,243 +40,244 @@ #define BLUE_OFFSET (2) #define ALPHA_OFFSET (3) -static CGImageRef cocoa_prepare_bitmap( void *bitmap ); +static CGImageRef cocoa_prepare_bitmap(void *bitmap); //static NSMapTable *cocoa_get_bitmap_cache( void ); -static inline NSMapTable *cocoa_get_bitmap_cache( void ) +static inline NSMapTable *cocoa_get_bitmap_cache(void) { - static NSMapTable *cache = nil; - if (cache == nil) { - cache = NSCreateMapTable( NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0 ); - } - return cache; + static NSMapTable *cache = nil; + if (cache == nil) { + cache = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0); + } + return cache; } static int bitmap_get_width(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsWide]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return (int)[bmp pixelsWide]; } static int bitmap_get_height(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsHigh]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return (int)[bmp pixelsHigh]; } static bool bitmap_get_opaque(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp isOpaque]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return [bmp isOpaque]; } static void bitmap_destroy(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); + NSCParameterAssert(NULL != bitmap); - NSMapTable *cache = cocoa_get_bitmap_cache(); - CGImageRef image = NSMapGet( cache, bitmap ); - if (NULL != image) { - CGImageRelease( image ); - NSMapRemove( cache, bitmap ); - } + NSMapTable *cache = cocoa_get_bitmap_cache(); + CGImageRef image = NSMapGet(cache, bitmap); + if (NULL != image) { + CGImageRelease(image); + NSMapRemove(cache, bitmap); + } - NSBitmapImageRep *bmp = (__bridge_transfer NSBitmapImageRep *)bitmap; + NSBitmapImageRep *bmp = (__bridge_transfer NSBitmapImageRep *)bitmap; bmp = nil; } static void *bitmap_create(int width, int height, unsigned int state) { - NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes: NULL - pixelsWide: width - pixelsHigh: height - bitsPerSample: BITS_PER_SAMPLE - samplesPerPixel: SAMPLES_PER_PIXEL - hasAlpha: YES - isPlanar: NO - colorSpaceName: NSDeviceRGBColorSpace - bitmapFormat: NSAlphaNonpremultipliedBitmapFormat - bytesPerRow: BYTES_PER_PIXEL * width - bitsPerPixel: BITS_PER_PIXEL]; - - return (__bridge_retained void *)bmp; + NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:width + pixelsHigh:height + bitsPerSample:BITS_PER_SAMPLE + samplesPerPixel:SAMPLES_PER_PIXEL + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace + bitmapFormat:NSAlphaNonpremultipliedBitmapFormat + bytesPerRow:BYTES_PER_PIXEL * width + bitsPerPixel:BITS_PER_PIXEL]; + + return (__bridge_retained void *)bmp; } static void bitmap_set_opaque(void *bitmap, bool opaque) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - [bmp setOpaque: opaque ? YES : NO]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + [bmp setOpaque:opaque ? YES : NO]; } static unsigned char *bitmap_get_buffer(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitmapData]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return [bmp bitmapData]; } static size_t bitmap_get_rowstride(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bytesPerRow]; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return [bmp bytesPerRow]; } static size_t bitmap_get_bpp(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitsPerPixel] / 8; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + return [bmp bitsPerPixel] / 8; } static bool bitmap_test_opaque(void *bitmap) { - NSCParameterAssert( bitmap_get_bpp( bitmap ) == BYTES_PER_PIXEL ); + NSCParameterAssert(bitmap_get_bpp(bitmap) == BYTES_PER_PIXEL); - unsigned char *buf = bitmap_get_buffer( bitmap ); + unsigned char *buf = bitmap_get_buffer(bitmap); - const size_t height = bitmap_get_height( bitmap ); - const size_t width = bitmap_get_width( bitmap ); + const size_t height = bitmap_get_height(bitmap); + const size_t width = bitmap_get_width(bitmap); - const size_t line_step = bitmap_get_rowstride( bitmap ) - BYTES_PER_PIXEL * width; + const size_t line_step = bitmap_get_rowstride(bitmap) - BYTES_PER_PIXEL * width; - for (size_t y = 0; y < height; y++) { - for (size_t x = 0; x < height; x++) { - if (buf[ALPHA_OFFSET] != 0xFF) return false; - buf += BYTES_PER_PIXEL; - } - buf += line_step; - } + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < height; x++) { + if (buf[ALPHA_OFFSET] != 0xFF) + return false; + buf += BYTES_PER_PIXEL; + } + buf += line_step; + } - return true; + return true; } static bool bitmap_save(void *bitmap, const char *path, unsigned flags) { - NSCParameterAssert( NULL != bitmap ); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - NSData *tiff = [bmp TIFFRepresentation]; - return [tiff writeToFile: [NSString stringWithUTF8String: path] atomically: YES]; + NSData *tiff = [bmp TIFFRepresentation]; + return [tiff writeToFile:[NSString stringWithUTF8String:path] atomically:YES]; } void cocoa_bitmap_modified(void *bitmap) { - NSMapTable *cache = cocoa_get_bitmap_cache(); - CGImageRef image = NSMapGet( cache, bitmap ); - if (NULL != image) { - CGImageRelease( image ); - NSMapRemove( cache, bitmap ); - } + NSMapTable *cache = cocoa_get_bitmap_cache(); + CGImageRef image = NSMapGet(cache, bitmap); + if (NULL != image) { + CGImageRelease(image); + NSMapRemove(cache, bitmap); + } } -CGImageRef cocoa_get_cgimage( void *bitmap ) +CGImageRef cocoa_get_cgimage(void *bitmap) { - NSMapTable *cache = cocoa_get_bitmap_cache(); + NSMapTable *cache = cocoa_get_bitmap_cache(); - CGImageRef result = NSMapGet( cache, bitmap ); - if (NULL == result) { - result = cocoa_prepare_bitmap( bitmap ); - NSMapInsertKnownAbsent( cache, bitmap, result ); - } + CGImageRef result = NSMapGet(cache, bitmap); + if (NULL == result) { + result = cocoa_prepare_bitmap(bitmap); + NSMapInsertKnownAbsent(cache, bitmap, result); + } - return result; + return result; } - -static CGImageRef cocoa_prepare_bitmap( void *bitmap ) +static CGImageRef cocoa_prepare_bitmap(void *bitmap) { - NSCParameterAssert( NULL != bitmap ); + NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; + NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - size_t w = [bmp pixelsWide]; - size_t h = [bmp pixelsHigh]; + size_t w = [bmp pixelsWide]; + size_t h = [bmp pixelsHigh]; - CGImageRef original = [bmp CGImage]; + CGImageRef original = [bmp CGImage]; - if (h <= 1) return CGImageRetain( original ); + if (h <= 1) + return CGImageRetain(original); - void *data = malloc( 4 * w * h ); + void *data = malloc(4 * w * h); - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGContextRef context = CGBitmapContextCreate( data, w, h, BITS_PER_SAMPLE, - BYTES_PER_PIXEL * w, colorSpace, - [bmp isOpaque] ? kCGImageAlphaNoneSkipLast - : kCGImageAlphaPremultipliedLast ); - CGColorSpaceRelease( colorSpace ); + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGContextRef context = CGBitmapContextCreate(data, w, h, BITS_PER_SAMPLE, + BYTES_PER_PIXEL * w, colorSpace, + [bmp isOpaque] ? kCGImageAlphaNoneSkipLast + : kCGImageAlphaPremultipliedLast); + CGColorSpaceRelease(colorSpace); - CGContextTranslateCTM( context, 0.0, h ); - CGContextScaleCTM( context, 1.0, -1.0 ); + CGContextTranslateCTM(context, 0.0, h); + CGContextScaleCTM(context, 1.0, -1.0); - CGRect rect = CGRectMake( 0, 0, w, h ); - CGContextClearRect( context, rect ); - CGContextDrawImage( context, rect, original ); + CGRect rect = CGRectMake(0, 0, w, h); + CGContextClearRect(context, rect); + CGContextDrawImage(context, rect, original); - CGImageRef result = CGBitmapContextCreateImage( context ); + CGImageRef result = CGBitmapContextCreateImage(context); - CGContextRelease( context ); - free( data ); + CGContextRelease(context); + free(data); - return result; + return result; } static nserror bitmap_render(struct bitmap *bitmap, struct hlcache_handle *content) { - int bwidth = bitmap_get_width( bitmap ); - int bheight = bitmap_get_height( bitmap ); + int bwidth = bitmap_get_width(bitmap); + int bheight = bitmap_get_height(bitmap); - struct redraw_context ctx = { - .interactive = false, - .background_images = true, - .plot = &cocoa_plotters - }; + struct redraw_context ctx = { + .interactive = false, + .background_images = true, + .plot = &cocoa_plotters + }; - CGColorSpaceRef cspace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ); - CGContextRef bitmapContext = CGBitmapContextCreate( bitmap_get_buffer( bitmap ), - bwidth, bheight, - bitmap_get_bpp( bitmap ) * 8 / 4, - bitmap_get_rowstride( bitmap ), - cspace, kCGImageAlphaNoneSkipLast ); - CGColorSpaceRelease( cspace ); + CGColorSpaceRef cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); + CGContextRef bitmapContext = CGBitmapContextCreate(bitmap_get_buffer(bitmap), + bwidth, bheight, + bitmap_get_bpp(bitmap) * 8 / 4, + bitmap_get_rowstride(bitmap), + cspace, kCGImageAlphaNoneSkipLast); + CGColorSpaceRelease(cspace); - int width = MIN( content_get_width( content ), 1024 ); - int height = ((width * bheight) + bwidth / 2) / bwidth; + int width = MIN(content_get_width(content), 1024); + int height = ((width * bheight) + bwidth / 2) / bwidth; - CGContextTranslateCTM( bitmapContext, 0, bheight ); - CGContextScaleCTM( bitmapContext, (CGFloat)bwidth / width, -(CGFloat)bheight / height ); + CGContextTranslateCTM(bitmapContext, 0, bheight); + CGContextScaleCTM(bitmapContext, (CGFloat)bwidth / width, -(CGFloat)bheight / height); - [NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: bitmapContext flipped: YES]]; + [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapContext flipped:YES]]; - content_scaled_redraw( content, width, height, &ctx ); + content_scaled_redraw(content, width, height, &ctx); - [NSGraphicsContext setCurrentContext: nil]; - CGContextRelease( bitmapContext ); + [NSGraphicsContext setCurrentContext:nil]; + CGContextRelease(bitmapContext); - cocoa_bitmap_modified( bitmap ); + cocoa_bitmap_modified(bitmap); - return true; + return true; } static struct gui_bitmap_table bitmap_table = { - .create = bitmap_create, - .destroy = bitmap_destroy, - .set_opaque = bitmap_set_opaque, - .get_opaque = bitmap_get_opaque, - .test_opaque = bitmap_test_opaque, - .get_buffer = bitmap_get_buffer, - .get_rowstride = bitmap_get_rowstride, - .get_width = bitmap_get_width, - .get_height = bitmap_get_height, - .get_bpp = bitmap_get_bpp, - .save = bitmap_save, - .modified = cocoa_bitmap_modified, - .render = bitmap_render, + .create = bitmap_create, + .destroy = bitmap_destroy, + .set_opaque = bitmap_set_opaque, + .get_opaque = bitmap_get_opaque, + .test_opaque = bitmap_test_opaque, + .get_buffer = bitmap_get_buffer, + .get_rowstride = bitmap_get_rowstride, + .get_width = bitmap_get_width, + .get_height = bitmap_get_height, + .get_bpp = bitmap_get_bpp, + .save = bitmap_save, + .modified = cocoa_bitmap_modified, + .render = bitmap_render, }; struct gui_bitmap_table *cocoa_bitmap_table = &bitmap_table; |