diff options
author | Sven Weidauer <sven@5sw.de> | 2017-06-07 22:51:20 +0200 |
---|---|---|
committer | Sven Weidauer <sven@5sw.de> | 2017-06-07 22:51:20 +0200 |
commit | 1a142fdd96dc29eebad8175ffa0345f7562e0398 (patch) | |
tree | ea8b1c2de5e7868e6e872a134827b0c7e0206e78 /frontends/cocoa/bitmap.m | |
parent | 8ce6d02fbd4fcf3a660812a7b9e5a60f2beca807 (diff) | |
download | netsurf-1a142fdd96dc29eebad8175ffa0345f7562e0398.tar.gz netsurf-1a142fdd96dc29eebad8175ffa0345f7562e0398.tar.bz2 |
Add helpers for retain/release and use in bitmap.m
Diffstat (limited to 'frontends/cocoa/bitmap.m')
-rw-r--r-- | frontends/cocoa/bitmap.m | 69 |
1 files changed, 25 insertions, 44 deletions
diff --git a/frontends/cocoa/bitmap.m b/frontends/cocoa/bitmap.m index 597a5af79..554d5e0bf 100644 --- a/frontends/cocoa/bitmap.m +++ b/frontends/cocoa/bitmap.m @@ -31,6 +31,8 @@ #import "cocoa/plotter.h" #import "cocoa/bitmap.h" +#import "cocoa/arc.h" + #define BITS_PER_SAMPLE (8) #define SAMPLES_PER_PIXEL (4) #define BITS_PER_PIXEL (BITS_PER_SAMPLE * SAMPLES_PER_PIXEL) @@ -41,7 +43,6 @@ #define ALPHA_OFFSET (3) static CGImageRef cocoa_prepare_bitmap(void *bitmap); -//static NSMapTable *cocoa_get_bitmap_cache( void ); static inline NSMapTable *cocoa_get_bitmap_cache(void) { @@ -52,40 +53,32 @@ static inline NSMapTable *cocoa_get_bitmap_cache(void) return cache; } +static inline NSBitmapImageRep *get_bitmap(void *bitmap) { + NSCParameterAssert(bitmap); + return (__bridge NSBitmapImageRep *)bitmap; +} + static int bitmap_get_width(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsWide]; + return (int)get_bitmap(bitmap).pixelsWide; } static int bitmap_get_height(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return (int)[bmp pixelsHigh]; + return (int)get_bitmap(bitmap).pixelsHigh; } static bool bitmap_get_opaque(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp isOpaque]; + return get_bitmap(bitmap).isOpaque; } static void bitmap_destroy(void *bitmap) { NSCParameterAssert(NULL != 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; - bmp = nil; + cocoa_bitmap_modified(bitmap); + arc_release(bitmap); } static void *bitmap_create(int width, int height, unsigned int state) @@ -103,35 +96,27 @@ static void *bitmap_create(int width, int height, unsigned int state) bytesPerRow:BYTES_PER_PIXEL * width bitsPerPixel:BITS_PER_PIXEL]; - return (__bridge_retained void *)bmp; + return arc_retain(bmp); } static void bitmap_set_opaque(void *bitmap, bool opaque) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - [bmp setOpaque:opaque ? YES : NO]; + get_bitmap(bitmap).opaque = opaque ? YES : NO; } static unsigned char *bitmap_get_buffer(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitmapData]; + return get_bitmap(bitmap).bitmapData; } static size_t bitmap_get_rowstride(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bytesPerRow]; + return get_bitmap(bitmap).bytesPerRow; } static size_t bitmap_get_bpp(void *bitmap) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - return [bmp bitsPerPixel] / 8; + return get_bitmap(bitmap).bitsPerPixel / 8; } static bool bitmap_test_opaque(void *bitmap) @@ -159,11 +144,8 @@ static bool bitmap_test_opaque(void *bitmap) static bool bitmap_save(void *bitmap, const char *path, unsigned flags) { - NSCParameterAssert(NULL != bitmap); - NSBitmapImageRep *bmp = (__bridge NSBitmapImageRep *)bitmap; - - NSData *tiff = [bmp TIFFRepresentation]; - return [tiff writeToFile:[NSString stringWithUTF8String:path] atomically:YES]; + NSData *tiff = get_bitmap(bitmap).TIFFRepresentation; + return [tiff writeToFile:@(path) atomically:YES]; } void cocoa_bitmap_modified(void *bitmap) @@ -191,17 +173,16 @@ CGImageRef cocoa_get_cgimage(void *bitmap) static CGImageRef cocoa_prepare_bitmap(void *bitmap) { - NSCParameterAssert(NULL != bitmap); + NSBitmapImageRep *bmp = get_bitmap(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) + if (h <= 1) { return CGImageRetain(original); + } void *data = malloc(4 * w * h); |