forked from zepouet/Xee-xCode-4.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XeeCGImage.m
149 lines (114 loc) · 3.27 KB
/
XeeCGImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#import "XeeCGImage.h"
// Reverse-engineered API calls. Evil!
CGAccessSessionRef CGAccessSessionCreate(CGDataProviderRef provider);
void *CGAccessSessionGetBytePointer(CGAccessSessionRef session);
size_t CGAccessSessionGetBytes(CGAccessSessionRef session,void *buffer,size_t bytes);
void CGAccessSessionRelease(CGAccessSessionRef session);
@implementation XeeCGImage
-(id)init
{
if(self=[super init])
{
imageref=NULL;
session=NULL;
}
return self;
}
-(id)initWithCGImage:(CGImageRef)cgimage
{
if(self=[super init])
{
imageref=NULL;
session=NULL;
if([self setCGImage:cgimage]) return self;
[self release];
}
return nil;
}
-(void)dealloc
{
if(session) CGAccessSessionRelease(session);
CGImageRelease(imageref);
[super dealloc];
}
-(BOOL)setCGImage:(CGImageRef)cgimage
{
if(!cgimage) return NO;
int pixelwidth=CGImageGetWidth(cgimage);
int pixelheight=CGImageGetHeight(cgimage);
int bprow=CGImageGetBytesPerRow(cgimage);
int bppixel=CGImageGetBitsPerPixel(cgimage);
int bpcomponent=CGImageGetBitsPerComponent(cgimage);
int bitmapinfo=CGImageGetBitmapInfo(cgimage); // only on 10.4
int alphainfo=CGImageGetAlphaInfo(cgimage);
CGColorSpaceRef colorspace=CGImageGetColorSpace(cgimage);
int components=CGColorSpaceGetNumberOfComponents(colorspace);
int mode;
switch(components)
{
case 1: mode=XeeGreyBitmap; break;
case 3: mode=XeeRGBBitmap; break;
default: return NO;
}
// Check for unsupported (non-host) byte ordering
int byteorder=bitmapinfo&kCGBitmapByteOrderMask;
#ifdef __BIG_ENDIAN__
if(byteorder==kCGBitmapByteOrder16Little||byteorder==kCGBitmapByteOrder32Little) return NO;
#else
if(byteorder==kCGBitmapByteOrder16Big||byteorder==kCGBitmapByteOrder32Big) return NO;
#endif
CGDataProviderRef provider=CGImageGetDataProvider(cgimage);
if(!provider) return NO;
CGAccessSessionRef newsession=CGAccessSessionCreate(provider);
if(!newsession) return NO;
BOOL shouldfree=NO;
void *pixeldata=CGAccessSessionGetBytePointer(newsession);
if(!pixeldata)
{
size_t bytes=pixelheight*bprow;
pixeldata=malloc(bytes);
if(!pixeldata)
{
CGAccessSessionRelease(newsession);
return NO;
}
/*size_t res=*/CGAccessSessionGetBytes(newsession,pixeldata,bytes);
// should check res, maybe?
CGAccessSessionRelease(newsession);
newsession=NULL;
shouldfree=YES;
}
int flags=0;
if(bitmapinfo&kCGBitmapFloatComponents) flags|=XeeBitmapFloatingPointFlag;
if(![self setData:pixeldata freeData:shouldfree width:pixelwidth height:pixelheight
bitsPerPixel:bppixel bitsPerComponent:bpcomponent bytesPerRow:bprow
mode:mode alphaType:alphainfo flags:flags])
{
if(newsession) CGAccessSessionRelease(newsession);
return NO;
}
CGImageRetain(cgimage);
CGImageRelease(imageref);
imageref=cgimage;
if(session) CGAccessSessionRelease(session);
session=newsession;
return YES;
}
-(void)invertImage
{
// Used to invert the colours of TIFF bitmap images.
if(bitspercomponent!=8) return; // TODO: should throw an exception?
for(int y=0;y<height;y++)
for(int i=0;i<bytesperrow;i++)
{
data[y*bytesperrow+i]^=0xff;
}
}
-(CGColorSpaceRef)createColorSpaceForCGImage
{
if(!imageref) return [super createColorSpaceForCGImage];
CGColorSpaceRef colorspace=CGImageGetColorSpace(imageref);
if(colorspace) CGColorSpaceRetain(colorspace);
return colorspace;
}
@end