Frameworks/AIUtilities Framework/Source/AIImageAdditions.m
author Evan Schoenberg
Thu, 14 Nov 2019 16:23:07 -0500
branchadium-1.5.10.5
changeset 5998 c1c33c0ba53a
parent 5732 2273fd749cbd
permissions -rw-r--r--
Corrected image rep casting, fixing several warnings
3679
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     1
/* 
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     2
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     3
 * with this source distribution.
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     4
 * 
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     6
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     7
 * or (at your option) any later version.
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     8
 * 
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    10
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    11
 * Public License for more details.
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    12
 * 
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    13
 * You should have received a copy of the GNU General Public License along with this program; if not,
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    14
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
f4294bb53b0f Bring consistency to file headings, this is not exhaustive. Evan and the rest.
Frank Dowsett <wixardy@adium.im>
parents: 3071
diff changeset
    15
 */
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    16
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    17
#import "AIImageAdditions.h"
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    18
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    19
@interface NSImage (AIImageAdditions_PRIVATE)
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    20
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    21
- (NSBitmapImageRep *)bitmapRep;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    22
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    23
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    24
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    25
@implementation NSImage (AIImageAdditions)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    26
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    27
+ (NSImage *)imageNamed:(NSString *)name forClass:(Class)inClass loadLazily:(BOOL)flag
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    28
{
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    29
	NSBundle *ownerBundle;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    30
    NSString *imagePath;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    31
    NSImage	*image;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    32
	
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    33
    // Get the bundle
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    34
    ownerBundle = [NSBundle bundleForClass:inClass];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    35
	
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    36
    // Open the image
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    37
    imagePath = [ownerBundle pathForImageResource:name];   
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    38
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    39
	if(flag) {
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    40
		image = [[NSImage alloc] initByReferencingFile:imagePath];
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    41
	} else {
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    42
		image = [[NSImage alloc] initWithContentsOfFile:imagePath];
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    43
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    44
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    45
    return [image autorelease];	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    46
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    47
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    48
// Returns an image from the owners bundle with the specified name
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    49
+ (NSImage *)imageNamed:(NSString *)name forClass:(Class)inClass
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    50
{	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    51
	return [self imageNamed:name forClass:inClass loadLazily:NO];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    52
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    53
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    54
+ (NSImage *)imageForSSL
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    55
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    56
	static NSImage *SSLIcon = nil;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    57
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    58
	if (!SSLIcon) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    59
		NSBundle *securityInterfaceFramework = [NSBundle bundleWithIdentifier:@"com.apple.securityinterface"];
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    60
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    61
		if (!securityInterfaceFramework) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    62
			securityInterfaceFramework = [NSBundle bundleWithPath:@"/System/Library/Frameworks/SecurityInterface.framework"];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    63
		}
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    64
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    65
		SSLIcon = [[NSImage alloc] initByReferencingFile:[securityInterfaceFramework pathForImageResource:@"CertSmallStd"]];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    66
	}
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    67
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    68
	return SSLIcon;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    69
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    70
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    71
+ (AIBitmapImageFileType)fileTypeOfData:(NSData *)inData
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    72
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    73
	const char *data = [inData bytes];
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2806
diff changeset
    74
	NSUInteger len = [inData length];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    75
	AIBitmapImageFileType fileType = AIUnknownFileType;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    76
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    77
	if (len >= 4) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    78
		if (!strncmp((char *)data, "GIF8", 4))
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    79
			fileType = AIGIFFileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    80
		else if (!strncmp((char *)data, "\xff\xd8\xff", 3)) /* 4th may be e0 through ef */
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    81
			fileType = AIJPEGFileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    82
		else if (!strncmp((char *)data, "\x89PNG", 4))
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    83
			fileType = AIPNGFileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    84
		else if (!strncmp((char *)data, "MM", 2) ||
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    85
				 !strncmp((char *)data, "II", 2))
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    86
			fileType = AITIFFFileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    87
		else if (!strncmp((char *)data, "BM", 2))
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    88
			fileType = AIBMPFileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    89
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    90
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    91
	return fileType;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    92
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    93
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    94
+ (NSString *)extensionForBitmapImageFileType:(AIBitmapImageFileType)inFileType
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    95
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    96
	NSString *extension = nil;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
    97
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    98
	switch (inFileType) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    99
		case AIUnknownFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   100
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   101
		case AITIFFFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   102
			extension = @"tif";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   103
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   104
		case AIBMPFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   105
			extension = @"bmp";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   106
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   107
		case AIGIFFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   108
			extension = @"gif";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   109
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   110
		case AIJPEGFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   111
			extension = @"jpg";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   112
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   113
		case AIPNGFileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   114
			extension = @"png";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   115
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   116
		case AIJPEG2000FileType:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   117
			extension = @"jp2";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   118
			break;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   119
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   120
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   121
	return extension;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   122
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   123
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   124
// Create and return an opaque bitmap image rep, replacing transparency with [NSColor whiteColor]
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   125
- (NSBitmapImageRep *)opaqueBitmapImageRep
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   126
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   127
	NSImage				*tempImage = nil;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   128
	NSBitmapImageRep	*imageRep = nil;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   129
	NSSize				size = [self size];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   130
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   131
	// Work with a temporary image so we don't modify self
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   132
	tempImage = [[[NSImage allocWithZone:[self zone]] initWithSize:size] autorelease];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   133
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   134
	// Lock before drawing to the temporary image
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   135
	[tempImage lockFocus];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   136
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   137
	// Fill with a white background
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   138
	[[NSColor whiteColor] set];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   139
	NSRectFill(NSMakeRect(0, 0, size.width, size.height));
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   140
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   141
	// Draw the image
5732
2273fd749cbd -compositeToPoint is deprecated, change to -drawAtPoint. There are still some in PSMTabs that are being warned about.
Frank Dowsett <wixardy@adium.im>
parents: 3785
diff changeset
   142
	[self drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   143
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   144
	// We're done drawing
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   145
	[tempImage unlockFocus];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   146
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   147
	// Find an NSBitmapImageRep from the temporary image
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   148
	for (NSImageRep *rep in tempImage.representations) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   149
		if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   150
			imageRep = (NSBitmapImageRep *)rep;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   151
		}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   152
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   153
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   154
	// Make one if necessary
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   155
	if (!imageRep) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   156
		imageRep = [NSBitmapImageRep imageRepWithData:[tempImage TIFFRepresentation]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   157
    }
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   158
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   159
	// 10.6 behavior: Drawing into a new image copies the display's color profile in.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   160
	// Remove the color profile so we don't bloat the image size.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   161
	[imageRep setProperty:NSImageColorSyncProfileData withValue:nil];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   162
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   163
	return imageRep;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   164
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   165
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   166
- (NSBitmapImageRep *)largestBitmapImageRep
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   167
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   168
	// Find the biggest image
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   169
	NSBitmapImageRep *bestRep = nil;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   170
	Class NSBitmapImageRepClass = [NSBitmapImageRep class];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   171
	CGFloat maxWidth = 0.0f;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   172
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   173
	for (NSImageRep *rep in [self representations]) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   174
		if ([rep isKindOfClass:NSBitmapImageRepClass]) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   175
			CGFloat thisWidth = [rep size].width;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   176
			
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   177
			if (thisWidth >= maxWidth) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   178
				// Cast explanation: GCC warns about us returning an NSImageRep here, presumably because it could be some other kind of NSImageRep if we don't check the class.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   179
				// Fortunately, we have such a check. This cast silences the warning.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   180
				bestRep = (NSBitmapImageRep *)rep;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   181
				maxWidth = thisWidth;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   182
			}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   183
		}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   184
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   185
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   186
	// We don't already have one, so forge one from our TIFF representation.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   187
	if (!bestRep) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   188
		bestRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   189
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   190
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   191
	return bestRep;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   192
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   193
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   194
- (NSData *)JPEGRepresentation
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   195
{	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   196
	return [self JPEGRepresentationWithCompressionFactor:1.0f];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   197
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   198
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   199
- (NSData *)JPEGRepresentationWithCompressionFactor:(float)compressionFactor
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   200
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   201
	/* JPEG does not support transparency, but NSImage does. We need to create a non-transparent NSImage
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   202
	 * before creating our representation or transparent parts will become black. White is preferable.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   203
	 */
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   204
	return ([[self opaqueBitmapImageRep] representationUsingType:NSJPEGFileType 
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   205
													  properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:(float)compressionFactor] 
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   206
																							 forKey:NSImageCompressionFactor]]);	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   207
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   208
- (NSData *)JPEGRepresentationWithMaximumByteSize:(NSUInteger)maxByteSize
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   209
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   210
	/* JPEG does not support transparency, but NSImage does. We need to create a non-transparent NSImage
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   211
	 * before creating our representation or transparent parts will become black. White is preferable.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   212
	 */
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   213
	NSBitmapImageRep *opaqueBitmapImageRep = [self opaqueBitmapImageRep];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   214
	NSData *data = nil;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   215
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   216
	for (float compressionFactor = 0.99f; compressionFactor > 0.4f; compressionFactor -= 0.01f) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   217
		data = [opaqueBitmapImageRep representationUsingType:NSJPEGFileType 
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   218
												  properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:compressionFactor] 
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   219
																						 forKey:NSImageCompressionFactor]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   220
		if (data && ([data length] <= maxByteSize)) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   221
			break;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   222
		} else {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   223
			data = nil;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   224
		}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   225
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   226
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   227
	return data;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   228
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   229
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   230
- (NSData *)PNGRepresentation
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   231
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   232
	/* PNG is easy; it supports everything TIFF does, and NSImage's PNG support is great. */
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   233
	NSBitmapImageRep *bitmapRep =  [self largestBitmapImageRep];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   234
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   235
    return ([bitmapRep representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]]);
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   236
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   237
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   238
- (NSData *)GIFRepresentation
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   239
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   240
	// GIF requires special treatment, as Apple doesn't allow you to save animations.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   241
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   242
	NSMutableData *GIFRepresentation = nil;
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   243
	NSImageRep *imageRep = [[self representations] objectAtIndex:0];
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   244
    NSBitmapImageRep *bitmap = nil;
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   245
    
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   246
	if (imageRep && [imageRep isKindOfClass:[NSBitmapImageRep class]]) {
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   247
        bitmap = (NSBitmapImageRep *)bitmap;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   248
		unsigned frameCount = [[bitmap valueForProperty:NSImageFrameCount] intValue];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   249
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   250
		if (!frameCount) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   251
			frameCount = 1;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   252
		}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   253
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   254
		NSSize size = [self size];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   255
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   256
		if (size.width > 0 && size.height > 0) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   257
			NSMutableArray *images = [NSMutableArray array];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   258
			
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   259
			for (unsigned i = 0; i < frameCount; i++) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   260
				// Set current frame
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   261
				[bitmap setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithUnsignedInt:i]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   262
				// Add frame representation
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   263
                [images addObject:[NSBitmapImageRep imageRepWithData:[bitmap representationUsingType:NSGIFFileType
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   264
                                                                                          properties:[NSDictionary dictionary]]]];
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   265
			}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   266
			
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   267
			GIFRepresentation = [NSMutableData dataWithData:[NSBitmapImageRep representationOfImageRepsInArray:images
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   268
																									 usingType:NSGIFFileType
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   269
																									properties:[self GIFPropertiesForRepresentation:bitmap]]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   270
			
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   271
			// Write GIF Extension Blocks
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   272
			[self writeGIFExtensionBlocksInData:GIFRepresentation forRepresenation:bitmap];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   273
		}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   274
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   275
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   276
	return GIFRepresentation;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   277
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   278
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   279
- (NSData *)BMPRepresentation
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   280
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   281
	/* BMP does not support transparency, but NSImage does. We need to create a non-transparent NSImage
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   282
	 * before creating our representation or transparent parts will become black. White is preferable.
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   283
	 */
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   284
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   285
    return ([[self opaqueBitmapImageRep] representationUsingType:NSBMPFileType properties:@{}]);
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   286
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   287
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   288
/*!
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   289
 * @brief Returns a GIF representation for GIFs, and PNG represenation for all other types
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   290
 */
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   291
- (NSData *)bestRepresentationByType
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   292
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   293
	NSData *data = nil;
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   294
	NSImageRep *bitmap = nil;
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   295
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   296
	if ((bitmap = [[self representations] objectAtIndex:0]) &&
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   297
		[bitmap isKindOfClass:[NSBitmapImageRep class]] &&
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   298
		([[(NSBitmapImageRep *)bitmap valueForProperty:NSImageFrameCount] intValue] > 1)) {
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   299
		data = [self GIFRepresentation];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   300
	} else {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   301
		data = [self PNGRepresentation];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   302
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   303
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   304
	return data;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   305
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   306
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   307
- (NSBitmapImageRep *)getBitmap
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   308
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   309
	[self lockFocus];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   310
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   311
	NSSize size = [self size];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   312
	NSRect rect = NSMakeRect(0.0f, 0.0f, size.width, size.height);
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   313
	NSBitmapImageRep *bm = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:rect] autorelease];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   314
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   315
	[self unlockFocus];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   316
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   317
	return bm;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   318
}
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   319
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   320
/*!
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   321
 * @brief Retrieve an image rep with a maximum size
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   322
 *
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   323
 * Returns the NSData of an image representation.
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   324
 *
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   325
 * @param fileType The NSBitmapImageFileType to be outputted for sizing
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   326
 * @param maximumSize The maximum size in bytes for the image
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   327
 *
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   328
 * @return the NSData representation using fileType
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   329
 */
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   330
- (NSData *)representationWithFileType:(NSBitmapImageFileType)fileType maximumFileSize:(NSUInteger)maximumSize
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   331
{
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   332
	NSBitmapImageRep *imageRep = [self largestBitmapImageRep];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   333
	
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   334
	// If no rep is found, return nil.
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   335
	if (!imageRep)
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   336
		return nil;
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   337
	
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   338
	NSData *data = [imageRep representationUsingType:fileType properties:@{}];
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   339
	
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   340
	// If no maximum size, return the base representation.
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   341
	if (!maximumSize)
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   342
		return data;
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   343
	
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   344
	// Ratio of height/width
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   345
	CGFloat ratio = (CGFloat)imageRep.pixelsHigh / (CGFloat)imageRep.pixelsWide;
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   346
	
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   347
	// Loop until we're small enough to fit into our max size
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   348
	while (data.length > maximumSize) {
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   349
		// New width/height using our ratio
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   350
		NSUInteger width = (imageRep.pixelsWide - 100);
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   351
		NSUInteger height = ((CGFloat)imageRep.pixelsWide - 100.0f)*ratio;
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   352
		
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   353
		// Create a new rep with the lowered size
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   354
		NSBitmapImageRep *newImageRep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   355
																				 pixelsWide:width
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   356
																				 pixelsHigh:height
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   357
																			  bitsPerSample:imageRep.bitsPerSample
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   358
																			samplesPerPixel:imageRep.samplesPerPixel
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   359
																				   hasAlpha:imageRep.hasAlpha
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   360
																				   isPlanar:imageRep.isPlanar
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   361
																			 colorSpaceName:NSCalibratedRGBColorSpace
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   362
																				bytesPerRow:imageRep.bytesPerRow
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   363
																			   bitsPerPixel:imageRep.bitsPerPixel] autorelease];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   364
		
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   365
		// Draw the old rep into the new rep
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   366
		[NSGraphicsContext saveGraphicsState];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   367
		[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:newImageRep]];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   368
		[imageRep drawInRect:NSMakeRect(0, 0, width, height)];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   369
		[NSGraphicsContext restoreGraphicsState];
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   370
		
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   371
		// Override the old rep
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   372
		imageRep = newImageRep;
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   373
		
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   374
		// Grab a new representation
5998
c1c33c0ba53a Corrected image rep casting, fixing several warnings
Evan Schoenberg
parents: 5732
diff changeset
   375
		data = [imageRep representationUsingType:fileType properties:@{}];
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   376
	}
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   377
	
2416
0eb4e7fbf3ba It helps to *not* return nil here.
Zachary West <zacw@adium.im>
parents: 2415
diff changeset
   378
	return data;
2415
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   379
}
bbed84b1bd5c Add an image method for returning an image representation with a maximum size.
Zachary West <zacw@adium.im>
parents: 2103
diff changeset
   380
3782
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   381
- (void)writeGIFExtensionBlocksInData:(NSMutableData *)data forRepresenation:(NSBitmapImageRep *)bitmap
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   382
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   383
	// GIF Application Extension Block - 0x21FF0B
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   384
	const char *GIFApplicationExtensionBlock = "\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50\x45\x32\x2E\x30\x03\x01\x00\x00\x00";
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   385
	// GIF Graphic Control Extension Block - 0x21F904
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   386
	NSData *GIFGraphicControlExtensionBlock = [NSData dataWithBytes:"\x00\x21\xF9\x04" length:4];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   387
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   388
	NSRange blockRange;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   389
    NSUInteger blockLocation = [data length];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   390
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   391
	unsigned frameCount = [[bitmap valueForProperty:NSImageFrameCount] intValue];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   392
	unsigned frameDuration;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   393
	unsigned i = 0;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   394
	
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   395
	while (!NSEqualRanges(blockRange = [data rangeOfData:GIFGraphicControlExtensionBlock options:NSDataSearchBackwards range:NSMakeRange(0, blockLocation)], NSMakeRange(NSNotFound, 0))) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   396
		// Set current frame
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   397
		[bitmap setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithUnsignedInt:i++]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   398
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   399
		// Frame Duration flag, 1/100 sec
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   400
		frameDuration = [[bitmap valueForProperty:NSImageCurrentFrameDuration] floatValue] * 100;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   401
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   402
        blockLocation = blockRange.location;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   403
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   404
		// Replace bytes in Graphic Extension Block
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   405
		[data replaceBytesInRange:NSMakeRange(NSMaxRange(blockRange) + 1, 2) withBytes:&frameDuration length:2];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   406
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   407
		// Write Application Extension Block
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   408
		if (i == frameCount) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   409
            [data replaceBytesInRange:NSMakeRange(blockRange.location + 1, 0) withBytes:GIFApplicationExtensionBlock length:strlen(GIFApplicationExtensionBlock) + 3];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   410
        }
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   411
		
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   412
		frameDuration = 0;
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   413
    }
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   414
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   415
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   416
- (NSDictionary *)GIFPropertiesForRepresentation:(NSBitmapImageRep *)bitmap
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   417
{
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   418
	NSNumber *frameCount = [bitmap valueForProperty:NSImageFrameCount];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   419
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   420
	if (!frameCount) {
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   421
		frameCount = [NSNumber numberWithUnsignedInt:1];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   422
	}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   423
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   424
	// Setting NSImageLoopCount & NSImageCurrentFrameDuration through - NSDictionary *properties - is not allowed!
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   425
	return [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:frameCount,
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   426
																			[NSNumber numberWithUnsignedInt:0],
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   427
																			nil]
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   428
										forKeys:[NSArray arrayWithObjects:NSImageFrameCount,
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   429
																			NSImageCurrentFrame,
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   430
																			nil]];
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   431
}
e660e9081fb6 Added ability to resize animated gifs that are too large. refs #9885
Adrian Godoroja <robotive@me.com>
parents: 3679
diff changeset
   432
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   433
@end