| 1 |
// |
| 2 |
// NSImage+data.m |
| 3 |
// ImageSplitter |
| 4 |
// |
| 5 |
// Created by hiroshi tomioka on 13/02/14. |
| 6 |
// Copyright (c) 2013年 hiroshi tomioka. All rights reserved. |
| 7 |
// |
| 8 |
|
| 9 |
#import "NSImage+data.h" |
| 10 |
|
| 11 |
@implementation NSImage (data) |
| 12 |
|
| 13 |
//画像ファイルのチェック |
| 14 |
+ (BOOL)isImageFile:(NSString*)filePath |
| 15 |
{ |
| 16 |
BOOL isImageFile = NO; |
| 17 |
LSItemInfoRecord info; |
| 18 |
CFStringRef uti = NULL; |
| 19 |
@try { |
| 20 |
|
| 21 |
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, |
| 22 |
(CFStringRef)filePath, kCFURLPOSIXPathStyle, FALSE); |
| 23 |
|
| 24 |
if (LSCopyItemInfoForURL(url, kLSRequestExtension | |
| 25 |
kLSRequestTypeCreator, &info) == noErr) |
| 26 |
{ |
| 27 |
if (info.extension != NULL) |
| 28 |
{ |
| 29 |
uti = |
| 30 |
UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, |
| 31 |
info.extension, kUTTypeData); |
| 32 |
CFRelease(info.extension); |
| 33 |
} |
| 34 |
|
| 35 |
if (uti == NULL) |
| 36 |
{ |
| 37 |
CFStringRef typeString = |
| 38 |
UTCreateStringForOSType(info.filetype); |
| 39 |
if ( typeString != NULL) |
| 40 |
{ |
| 41 |
uti = |
| 42 |
UTTypeCreatePreferredIdentifierForTag(kUTTagClassOSType, |
| 43 |
typeString, kUTTypeData); |
| 44 |
CFRelease(typeString); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if (uti != NULL) |
| 49 |
{ |
| 50 |
CFArrayRef supportedTypes = |
| 51 |
CGImageSourceCopyTypeIdentifiers(); |
| 52 |
CFIndex i, typeCount = CFArrayGetCount(supportedTypes); |
| 53 |
|
| 54 |
for (i = 0; i < typeCount; i++) |
| 55 |
{ |
| 56 |
if (UTTypeConformsTo(uti, |
| 57 |
(CFStringRef)CFArrayGetValueAtIndex(supportedTypes, i))) |
| 58 |
{ |
| 59 |
isImageFile = YES; |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
@catch (NSException *exception) { |
| 68 |
|
| 69 |
} |
| 70 |
@finally { |
| 71 |
} |
| 72 |
|
| 73 |
return isImageFile; |
| 74 |
} |
| 75 |
|
| 76 |
- (NSImage*)trimImageByRect:(NSRect)trimRect { |
| 77 |
NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation] ]; |
| 78 |
NSSize pointSize = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh); |
| 79 |
|
| 80 |
[self setSize:pointSize]; |
| 81 |
|
| 82 |
NSRect newRect = NSZeroRect; |
| 83 |
newRect.size = trimRect.size; |
| 84 |
NSImage *newImage = [[[NSImage alloc] initWithSize:newRect.size] autorelease]; |
| 85 |
|
| 86 |
[newImage lockFocus]; |
| 87 |
[NSGraphicsContext saveGraphicsState]; |
| 88 |
|
| 89 |
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; |
| 90 |
|
| 91 |
[self drawAtPoint:NSZeroPoint fromRect:trimRect operation:NSCompositeSourceOver fraction:1.0]; |
| 92 |
[NSGraphicsContext restoreGraphicsState]; |
| 93 |
[newImage unlockFocus]; |
| 94 |
|
| 95 |
return newImage; |
| 96 |
} |
| 97 |
|
| 98 |
- (NSImage*)fitImageInSize:(NSSize)fitSize { |
| 99 |
NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation] ]; |
| 100 |
NSSize pointSize = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh); |
| 101 |
|
| 102 |
[self setSize:pointSize]; |
| 103 |
|
| 104 |
NSRect originalRect = NSZeroRect; |
| 105 |
originalRect.size = pointSize; |
| 106 |
NSImage *newImage = [[[NSImage alloc] initWithSize:fitSize] autorelease]; |
| 107 |
|
| 108 |
[newImage lockFocus]; |
| 109 |
[NSGraphicsContext saveGraphicsState]; |
| 110 |
|
| 111 |
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; |
| 112 |
|
| 113 |
|
| 114 |
double scale = MIN(fitSize.width / originalRect.size.width, fitSize.height / originalRect.size.height); |
| 115 |
|
| 116 |
|
| 117 |
NSRect newRect = NSMakeRect((fitSize.width - originalRect.size.width * scale) / 2.0, (fitSize.height - originalRect.size.height * scale) / 2.0, originalRect.size.width * scale, originalRect.size.height * scale); |
| 118 |
|
| 119 |
[self drawInRect:newRect fromRect:originalRect operation:NSCompositeSourceOver fraction:1.0]; |
| 120 |
|
| 121 |
// NSZeroPoint fromRect:trimRect operation:NSCompositeSourceOver fraction:1.0]; |
| 122 |
[NSGraphicsContext restoreGraphicsState]; |
| 123 |
[newImage unlockFocus]; |
| 124 |
|
| 125 |
return newImage; |
| 126 |
} |
| 127 |
|
| 128 |
- (NSData*)JPEGDataWithQuality:(float)quality |
| 129 |
{ |
| 130 |
NSBitmapImageRep *bitmapImageRep; |
| 131 |
NSData *newData; |
| 132 |
NSDictionary *properties; |
| 133 |
|
| 134 |
bitmapImageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]]; |
| 135 |
properties = [NSDictionary |
| 136 |
dictionaryWithObject:[NSNumber numberWithFloat:quality] |
| 137 |
forKey:NSImageCompressionFactor]; |
| 138 |
newData = [bitmapImageRep representationUsingType:NSJPEGFileType |
| 139 |
properties:properties]; |
| 140 |
return newData; |
| 141 |
} |
| 142 |
|
| 143 |
- (NSData*)JPEG2000DataWithQuality:(float)quality |
| 144 |
{ |
| 145 |
NSBitmapImageRep *bitmapImageRep; |
| 146 |
NSData *newData; |
| 147 |
NSDictionary *properties; |
| 148 |
|
| 149 |
bitmapImageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]]; |
| 150 |
properties = [NSDictionary |
| 151 |
dictionaryWithObject:[NSNumber numberWithFloat:quality] |
| 152 |
forKey:NSImageCompressionFactor]; |
| 153 |
newData = [bitmapImageRep representationUsingType:NSJPEG2000FileType |
| 154 |
properties:properties]; |
| 155 |
return newData; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
- (NSData*)PNGDataWithInterlaced:(BOOL)interlaced |
| 160 |
{ |
| 161 |
NSBitmapImageRep *bitmapImageRep; |
| 162 |
NSData *newData; |
| 163 |
NSDictionary *properties; |
| 164 |
|
| 165 |
bitmapImageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]]; |
| 166 |
properties = [NSDictionary |
| 167 |
dictionaryWithObject:[NSNumber numberWithBool:interlaced] |
| 168 |
forKey:NSImageInterlaced]; |
| 169 |
newData = [bitmapImageRep representationUsingType:NSPNGFileType |
| 170 |
properties:properties]; |
| 171 |
return newData; |
| 172 |
} |
| 173 |
|
| 174 |
- (NSData*)BMPData |
| 175 |
{ |
| 176 |
NSBitmapImageRep *bitmapImageRep; |
| 177 |
NSData *newData; |
| 178 |
|
| 179 |
bitmapImageRep = [NSBitmapImageRep imageRepWithData:[self TIFFRepresentation]]; |
| 180 |
newData = [bitmapImageRep representationUsingType:NSBMPFileType |
| 181 |
properties:nil]; |
| 182 |
return newData; |
| 183 |
} |
| 184 |
|
| 185 |
@end |