Frameworks/AIUtilities Framework/Source/AIImageAdditions.m
branchadium-1.5.10.5
changeset 5998 c1c33c0ba53a
parent 5732 2273fd749cbd
equal deleted inserted replaced
5997:d88e6711d0e0 5998:c1c33c0ba53a
   230 - (NSData *)PNGRepresentation
   230 - (NSData *)PNGRepresentation
   231 {
   231 {
   232 	/* PNG is easy; it supports everything TIFF does, and NSImage's PNG support is great. */
   232 	/* PNG is easy; it supports everything TIFF does, and NSImage's PNG support is great. */
   233 	NSBitmapImageRep *bitmapRep =  [self largestBitmapImageRep];
   233 	NSBitmapImageRep *bitmapRep =  [self largestBitmapImageRep];
   234 
   234 
   235 	return ([bitmapRep representationUsingType:NSPNGFileType properties:nil]);
   235     return ([bitmapRep representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]]);
   236 }
   236 }
   237 
   237 
   238 - (NSData *)GIFRepresentation
   238 - (NSData *)GIFRepresentation
   239 {
   239 {
   240 	// GIF requires special treatment, as Apple doesn't allow you to save animations.
   240 	// GIF requires special treatment, as Apple doesn't allow you to save animations.
   241 	
   241 	
   242 	NSMutableData *GIFRepresentation = nil;
   242 	NSMutableData *GIFRepresentation = nil;
   243 	NSBitmapImageRep *bitmap = [[self representations] objectAtIndex:0];
   243 	NSImageRep *imageRep = [[self representations] objectAtIndex:0];
   244 	
   244     NSBitmapImageRep *bitmap = nil;
   245 	if (bitmap && [bitmap isKindOfClass:[NSBitmapImageRep class]]) {
   245     
       
   246 	if (imageRep && [imageRep isKindOfClass:[NSBitmapImageRep class]]) {
       
   247         bitmap = (NSBitmapImageRep *)bitmap;
   246 		unsigned frameCount = [[bitmap valueForProperty:NSImageFrameCount] intValue];
   248 		unsigned frameCount = [[bitmap valueForProperty:NSImageFrameCount] intValue];
   247 		
   249 		
   248 		if (!frameCount) {
   250 		if (!frameCount) {
   249 			frameCount = 1;
   251 			frameCount = 1;
   250 		}
   252 		}
   256 			
   258 			
   257 			for (unsigned i = 0; i < frameCount; i++) {
   259 			for (unsigned i = 0; i < frameCount; i++) {
   258 				// Set current frame
   260 				// Set current frame
   259 				[bitmap setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithUnsignedInt:i]];
   261 				[bitmap setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithUnsignedInt:i]];
   260 				// Add frame representation
   262 				// Add frame representation
   261 				[images addObject:[NSBitmapImageRep imageRepWithData:[bitmap representationUsingType:NSGIFFileType properties:nil]]];
   263                 [images addObject:[NSBitmapImageRep imageRepWithData:[bitmap representationUsingType:NSGIFFileType
       
   264                                                                                           properties:[NSDictionary dictionary]]]];
   262 			}
   265 			}
   263 			
   266 			
   264 			GIFRepresentation = [NSMutableData dataWithData:[NSBitmapImageRep representationOfImageRepsInArray:images
   267 			GIFRepresentation = [NSMutableData dataWithData:[NSBitmapImageRep representationOfImageRepsInArray:images
   265 																									 usingType:NSGIFFileType
   268 																									 usingType:NSGIFFileType
   266 																									properties:[self GIFPropertiesForRepresentation:bitmap]]];
   269 																									properties:[self GIFPropertiesForRepresentation:bitmap]]];
   277 {
   280 {
   278 	/* BMP does not support transparency, but NSImage does. We need to create a non-transparent NSImage
   281 	/* BMP does not support transparency, but NSImage does. We need to create a non-transparent NSImage
   279 	 * before creating our representation or transparent parts will become black. White is preferable.
   282 	 * before creating our representation or transparent parts will become black. White is preferable.
   280 	 */
   283 	 */
   281 
   284 
   282 	return ([[self opaqueBitmapImageRep] representationUsingType:NSBMPFileType properties:nil]);
   285     return ([[self opaqueBitmapImageRep] representationUsingType:NSBMPFileType properties:@{}]);
   283 }
   286 }
   284 
   287 
   285 /*!
   288 /*!
   286  * @brief Returns a GIF representation for GIFs, and PNG represenation for all other types
   289  * @brief Returns a GIF representation for GIFs, and PNG represenation for all other types
   287  */
   290  */
   288 - (NSData *)bestRepresentationByType
   291 - (NSData *)bestRepresentationByType
   289 {
   292 {
   290 	NSData *data = nil;
   293 	NSData *data = nil;
   291 	NSBitmapImageRep *bitmap = nil;
   294 	NSImageRep *bitmap = nil;
   292 	
   295 	
   293 	if ((bitmap = [[self representations] objectAtIndex:0]) &&
   296 	if ((bitmap = [[self representations] objectAtIndex:0]) &&
   294 		[bitmap isKindOfClass:[NSBitmapImageRep class]] &&
   297 		[bitmap isKindOfClass:[NSBitmapImageRep class]] &&
   295 		([[bitmap valueForProperty:NSImageFrameCount] intValue] > 1)) {
   298 		([[(NSBitmapImageRep *)bitmap valueForProperty:NSImageFrameCount] intValue] > 1)) {
   296 		data = [self GIFRepresentation];
   299 		data = [self GIFRepresentation];
   297 	} else {
   300 	} else {
   298 		data = [self PNGRepresentation];
   301 		data = [self PNGRepresentation];
   299 	}
   302 	}
   300 	
   303 	
   330 	
   333 	
   331 	// If no rep is found, return nil.
   334 	// If no rep is found, return nil.
   332 	if (!imageRep)
   335 	if (!imageRep)
   333 		return nil;
   336 		return nil;
   334 	
   337 	
   335 	NSData *data = [imageRep representationUsingType:fileType properties:nil];
   338 	NSData *data = [imageRep representationUsingType:fileType properties:@{}];
   336 	
   339 	
   337 	// If no maximum size, return the base representation.
   340 	// If no maximum size, return the base representation.
   338 	if (!maximumSize)
   341 	if (!maximumSize)
   339 		return data;
   342 		return data;
   340 	
   343 	
   367 		
   370 		
   368 		// Override the old rep
   371 		// Override the old rep
   369 		imageRep = newImageRep;
   372 		imageRep = newImageRep;
   370 		
   373 		
   371 		// Grab a new representation
   374 		// Grab a new representation
   372 		data = [imageRep representationUsingType:fileType properties:nil];
   375 		data = [imageRep representationUsingType:fileType properties:@{}];
   373 	}
   376 	}
   374 	
   377 	
   375 	return data;
   378 	return data;
   376 }
   379 }
   377 
   380