diff options
Diffstat (limited to 'cocoa')
-rw-r--r-- | cocoa/apple_image.m | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/cocoa/apple_image.m b/cocoa/apple_image.m index 063b54d10..9ea227b11 100644 --- a/cocoa/apple_image.m +++ b/cocoa/apple_image.m @@ -44,10 +44,12 @@ static bool apple_image_redraw(struct content *c, int x, int y, static nserror apple_image_clone(const struct content *old, struct content **newc); static content_type apple_image_content_type(lwc_string *mime_type); +static bool apple_image_process_data(struct content *c, const char *data, unsigned int size); static const content_handler apple_image_content_handler = { .create = apple_image_create, .data_complete = apple_image_convert, + .process_data = apple_image_process_data, .destroy = apple_image_destroy, .redraw = apple_image_redraw, .clone = apple_image_clone, @@ -55,6 +57,9 @@ static const content_handler apple_image_content_handler = { .no_share = false }; +static NSBitmapImageRep *ImageRepForContent( struct content *c ); +static NSData *DataForContent( struct content * c); + static lwc_string **apple_image_mime_types = NULL; static size_t types_count = 0; static size_t types_capacity = 0; @@ -165,22 +170,21 @@ nserror apple_image_create(const content_handler *handler, bool apple_image_convert(struct content *c) { - unsigned long size; - const char *bytes = content__get_source_data(c, &size); - - NSData *data = [NSData dataWithBytesNoCopy: (char *)bytes length: size freeWhenDone: NO]; - NSBitmapImageRep *image = [[NSBitmapImageRep imageRepWithData: data] retain]; - - if (image == nil) { + NSBitmapImageRep *image = ImageRepForContent( c ); + if (image == nil) return false; + + NSData *data = DataForContent( c ); + + NSInteger rc = [image incrementalLoadFromData: data complete: YES]; + if (rc != NSImageRepLoadStatusCompleted) { union content_msg_data msg_data; - msg_data.error = "cannot decode image"; + msg_data.error = "invalid image data"; content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } c->width = [image pixelsWide]; c->height = [image pixelsHigh]; - c->bitmap = (void *)image; char title[100]; snprintf( title, sizeof title, "Image (%dx%d)", c->width, c->height ); @@ -254,4 +258,45 @@ bool apple_image_redraw(struct content *c, int x, int y, flags); } +bool apple_image_process_data(struct content *c, const char *newData, unsigned int newSize) +{ + NSBitmapImageRep *image = ImageRepForContent( c ); + if (image == nil) return false; + + NSData *data = DataForContent( c ); + + NSInteger rc = [image incrementalLoadFromData: data complete: NO]; + if (rc == NSImageRepLoadStatusInvalidData) { + union content_msg_data msg_data; + msg_data.error = "invalid data"; + content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + return false; + } + + return true; +} + +NSData *DataForContent( struct content * c) +{ + unsigned long sz = 0; + const char *bytes = content__get_source_data(c, &sz ); + return [NSData dataWithBytesNoCopy: (char *)bytes length: sz freeWhenDone: NO]; +} + +NSBitmapImageRep *ImageRepForContent( struct content *c ) +{ + NSBitmapImageRep *image = (NSBitmapImageRep *)c->bitmap; + if (image == nil) { + image = [[NSBitmapImageRep alloc] initForIncrementalLoad]; + if (image == nil) { + union content_msg_data msg_data; + msg_data.error = "not enough memory for image"; + content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + } + c->bitmap = (void *)image; + } + return image; +} + + #endif /* WITH_APPLE_IMAGE */ |