forked from kimasendorf/ExtraFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XFProfile.m
424 lines (329 loc) · 11.3 KB
/
XFProfile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// XFProfile.m
// ExtraFile
//
// Created by Kim Asendorf on 06.05.11.
//
#import "XFProfile.h"
static OSErr profileIterate(CMProfileIterateData *info, void *refCon)
{
NSMutableArray* array = (NSMutableArray*)refCon;
XFProfile* prof = [XFProfile profileWithIterateData:info];
if (prof)
[array addObject:prof];
return noErr;
}
@implementation XFProfile
+ (NSArray*)arrayOfAllProfilesWithSpace:(OSType)space
{
CFIndex i, count;
NSArray* profArray = [XFProfile arrayOfAllProfiles];
NSMutableArray* profs = [NSMutableArray arrayWithCapacity:0];
count = [profArray count];
for (i=0; i<count; i++)
{
XFProfile* prof = (XFProfile*)[profArray objectAtIndex:i];
OSType pClass = [prof classType];
if ([prof spaceType]==space && [prof description] &&
(pClass==cmDisplayClass || pClass==cmOutputClass))
[profs addObject:prof];
}
[profArray release];
return profs;
}
+ (NSArray *)arrayOfAllProfiles
{
NSMutableArray* profs = [[NSMutableArray arrayWithCapacity:0] retain];
CMProfileIterateUPP iterUPP = NewCMProfileIterateUPP(profileIterate);
CMIterateColorSyncFolder(iterUPP, NULL, 0L, profs);
DisposeCMProfileIterateUPP(iterUPP);
return (NSArray*)profs;
}
+ (XFProfile *)profileDefaultRGB
{
NSString* path = [[[NSUserDefaultsController sharedUserDefaultsController] defaults]
objectForKey:@"DefaultRGBProfile"];
return [XFProfile profileWithPath:path];
}
+ (XFProfile *)profileDefaultGray
{
NSString* path = [[[NSUserDefaultsController sharedUserDefaultsController] defaults]
objectForKey:@"DefaultGrayProfile"];
return [XFProfile profileWithPath:path];
}
+ (XFProfile *)profileDefaultCMYK
{
NSString* path = [[[NSUserDefaultsController sharedUserDefaultsController] defaults]
objectForKey:@"DefaultCMYKProfile"];
return [XFProfile profileWithPath:path];
}
+ (XFProfile *)profileWithGenericRGB
{
return [[[XFProfile alloc] initWithGenericRGB] autorelease];
}
+ (XFProfile *)profileWithLinearRGB
{
return [[[XFProfile alloc] initWithLinearRGB] autorelease];
}
+ (XFProfile *)profileWithIterateData:(CMProfileIterateData *)data
{
return [[[XFProfile alloc] initWithIterateData:data] autorelease];
}
+ (XFProfile *)profileWithPath:(NSString *)path
{
return [[[XFProfile alloc] initWithPath:path] autorelease];
}
- (XFProfile *)initWithGenericRGB
{
xfLocation.locType = cmPathBasedProfile;
strcpy(xfLocation.u.pathLoc.path, "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc");
xfClass = cmDisplayClass;
xfSpace = cmRGBData;
if (CMOpenProfile(&xfRef, &xfLocation) == noErr)
{
return self;
}
else
{
[self autorelease];
return nil;
}
}
- (XFProfile *)initWithLinearRGB
{
static const UInt8 data[0x220] =
"\x00\x00\x02\x20\x61\x70\x70\x6c\x02\x20\x00\x00\x6d\x6e\x74\x72"
"\x52\x47\x42\x20\x58\x59\x5a\x20\x07\xd2\x00\x05\x00\x0d\x00\x0c"
"\x00\x00\x00\x00\x61\x63\x73\x70\x41\x50\x50\x4c\x00\x00\x00\x00"
"\x61\x70\x70\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3\x2d"
"\x61\x70\x70\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x0a\x72\x58\x59\x5a\x00\x00\x00\xfc\x00\x00\x00\x14"
"\x67\x58\x59\x5a\x00\x00\x01\x10\x00\x00\x00\x14\x62\x58\x59\x5a"
"\x00\x00\x01\x24\x00\x00\x00\x14\x77\x74\x70\x74\x00\x00\x01\x38"
"\x00\x00\x00\x14\x63\x68\x61\x64\x00\x00\x01\x4c\x00\x00\x00\x2c"
"\x72\x54\x52\x43\x00\x00\x01\x78\x00\x00\x00\x0e\x67\x54\x52\x43"
"\x00\x00\x01\x78\x00\x00\x00\x0e\x62\x54\x52\x43\x00\x00\x01\x78"
"\x00\x00\x00\x0e\x64\x65\x73\x63\x00\x00\x01\xb0\x00\x00\x00\x6d"
"\x63\x70\x72\x74\x00\x00\x01\x88\x00\x00\x00\x26\x58\x59\x5a\x20"
"\x00\x00\x00\x00\x00\x00\x74\x4b\x00\x00\x3e\x1d\x00\x00\x03\xcb"
"\x58\x59\x5a\x20\x00\x00\x00\x00\x00\x00\x5a\x73\x00\x00\xac\xa6"
"\x00\x00\x17\x26\x58\x59\x5a\x20\x00\x00\x00\x00\x00\x00\x28\x18"
"\x00\x00\x15\x57\x00\x00\xb8\x33\x58\x59\x5a\x20\x00\x00\x00\x00"
"\x00\x00\xf3\x52\x00\x01\x00\x00\x00\x01\x16\xcf\x73\x66\x33\x32"
"\x00\x00\x00\x00\x00\x01\x0c\x42\x00\x00\x05\xde\xff\xff\xf3\x26"
"\x00\x00\x07\x92\x00\x00\xfd\x91\xff\xff\xfb\xa2\xff\xff\xfd\xa3"
"\x00\x00\x03\xdc\x00\x00\xc0\x6c\x63\x75\x72\x76\x00\x00\x00\x00"
"\x00\x00\x00\x01\x01\x00\x00\x00\x74\x65\x78\x74\x00\x00\x00\x00"
"\x43\x6f\x70\x79\x72\x69\x67\x68\x74\x20\x41\x70\x70\x6c\x65\x20"
"\x43\x6f\x6d\x70\x75\x74\x65\x72\x20\x49\x6e\x63\x2e\x00\x00\x00"
"\x64\x65\x73\x63\x00\x00\x00\x00\x00\x00\x00\x13\x4c\x69\x6e\x65"
"\x61\x72\x20\x52\x47\x42\x20\x50\x72\x6f\x66\x69\x6c\x65\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
xfLocation.locType = cmBufferBasedProfile;
xfLocation.u.bufferLoc.buffer = (void*)data;
xfLocation.u.bufferLoc.size = 0x220;
xfClass = cmDisplayClass;
xfSpace = cmRGBData;
if (CMOpenProfile(&xfRef, &xfLocation) == noErr)
{
return self;
}
else
{
[self autorelease];
return nil;
}
}
- (XFProfile *)initWithIterateData:(CMProfileIterateData *)info
{
const size_t kMaxProfNameLen = 36;
xfLocation = info->location;
xfClass = info->header.profileClass;
xfSpace = info->header.dataColorSpace;
if (info->uniCodeNameCount > 1)
{
CFIndex numChars = info->uniCodeNameCount - 1;
if (numChars > kMaxProfNameLen)
numChars = kMaxProfNameLen;
xfName = [[NSString stringWithCharacters:info->uniCodeName length:numChars] retain];
}
return self;
}
- (XFProfile *)initWithPath:(NSString *)path
{
if (path)
{
xfPath = [path retain];
xfLocation.locType = cmPathBasedProfile;
strncpy(xfLocation.u.pathLoc.path, [path fileSystemRepresentation], 255);
CMAppleProfileHeader header;
if (noErr==CMGetProfileHeader([self ref], &header))
{
xfClass = header.cm2.profileClass;
xfSpace = header.cm2.dataColorSpace;
}
else
{
[self autorelease];
return nil;
}
}
return self;
}
- (void)dealloc
{
CMCloseProfile(xfRef);
CGColorSpaceRelease(xfColorspace);
[xfName release];
[xfPath release];
[super dealloc];
}
- (CMProfileRef)ref
{
if (xfRef == NULL)
(void) CMOpenProfile(&xfRef, &xfLocation);
return xfRef;
}
- (CMProfileLocation *)location
{
return &xfLocation;
}
- (OSType)classType
{
return xfClass;
}
- (OSType)spaceType
{
return xfSpace;
}
- (NSString *)description
{
if (xfName == nil)
CMCopyProfileDescriptionString([self ref], (CFStringRef *) &xfName);
return xfName;
}
- (NSString *)path
{
if (xfPath == nil)
{
/*
if (xfLocation.locType == cmFileBasedProfile)
{
FSRef fsref;
UInt8 path[1024];
if (FSpMakeFSRef(&(mLocation.u.fileLoc.spec), &fsref) == noErr &&
FSRefMakePath(&fsref, path, 1024) == noErr)
xfPath = [[NSString stringWithUTF8String:(const char *)path] retain];
}
else
*/
if (xfLocation.locType == cmPathBasedProfile)
{
xfPath = [[NSString stringWithUTF8String:xfLocation.u.pathLoc.path] retain];
}
}
return xfPath;
}
- (BOOL)isEqual:(id)obj
{
if ([obj isKindOfClass:[self class]])
return [[self path] isEqualToString:[obj path]];
return [super isEqual:obj];
}
- (CGColorSpaceRef)colorspace
{
if (xfColorspace == nil)
xfColorspace = CGColorSpaceCreateWithPlatformColorSpace([self ref]);
return xfColorspace;
}
- (NSData *)dataForCISoftproofTextureWithGridSize:(size_t) grid
{
NSData* nsdata = nil;
NCMConcatProfileSet* set = nil;
size_t count = (grid*grid*grid) * 4;
size_t size;
UInt8* data8 = nil;
CMWorldRef cw = nil;
CMProfileRef displayProf = nil;
XFProfile* linRGB = nil;
// profile for transform
linRGB = [XFProfile profileWithLinearRGB];
if (linRGB == nil)
goto bail;
// specify size of resulting data
size = count * sizeof(float);
nsdata = [NSMutableData dataWithLength:size];
if (nsdata == nil)
goto bail;
// now build our color world transform
size = offsetof(NCMConcatProfileSet, profileSpecs[3]);
set = (NCMConcatProfileSet *) calloc(1, size);
if (set==nil)
goto bail;
set->cmm = 0000;
set->flagsMask = 0xFFFFFFFF;
set->profileCount = 3;
set->flags = (cmBestMode) << 16 | cmGamutCheckingMask;
set->profileSpecs[0].profile = [linRGB ref];
set->profileSpecs[1].profile = [self ref];
set->profileSpecs[2].profile = [linRGB ref];
set->profileSpecs[0].renderingIntent = kUseProfileIntent;
set->profileSpecs[1].renderingIntent = kUseProfileIntent;
set->profileSpecs[2].renderingIntent = kUseProfileIntent;
set->profileSpecs[0].transformTag = kDeviceToPCS;
set->profileSpecs[1].transformTag = kPCSToPCS;
set->profileSpecs[2].transformTag = kPCSToDevice;
// Define a color world for color transformations among concatenated profiles.
if (NCWConcatColorWorld (&cw, set, nil, nil) != noErr)
goto bail;
size = count * sizeof(UInt8);
data8 = malloc(size);
// cmTextureRGBtoRGBX8 = RGB to 8-bit RGBx texture
if (CWFillLookupTexture (cw, grid, cmTextureRGBtoRGBX8, size, data8) != noErr)
goto bail;
float* dataPtr = (float*) [(NSMutableData *)nsdata mutableBytes];
if (dataPtr == nil)
goto bail;
size_t i;
for (i=0; i<count; i++)
dataPtr[i] = (float)data8[i]/255.0;
bail:
if (data8) free(data8);
if (displayProf) CMCloseProfile (displayProf);
if (cw) CWDisposeColorWorld(cw);
if (set) free(set);
return nsdata;
}
@end
//DEFAULT PROFILE
CGImageRef CGImageCreateCopyWithDefaultSpace(CGImageRef image)
{
if (image == nil)
return nil;
CGImageRef newImage = nil;
XFProfile* dfltRGB = [XFProfile profileDefaultRGB];
XFProfile* dfltGray = [XFProfile profileDefaultGray];
XFProfile* dfltCMYK = [XFProfile profileDefaultCMYK];
CGColorSpaceRef devRGB = CGColorSpaceCreateDeviceRGB();
CGColorSpaceRef devGray = CGColorSpaceCreateDeviceGray();
CGColorSpaceRef devCMYK = CGColorSpaceCreateDeviceCMYK();
if (dfltRGB && CFEqual(devRGB,CGImageGetColorSpace(image)))
newImage = CGImageCreateCopyWithColorSpace(image, [dfltRGB colorspace]);
if (dfltGray && CFEqual(devGray,CGImageGetColorSpace(image)))
newImage = CGImageCreateCopyWithColorSpace(image, [dfltGray colorspace]);
if (dfltCMYK && CFEqual(devCMYK,CGImageGetColorSpace(image)))
newImage = CGImageCreateCopyWithColorSpace(image, [dfltCMYK colorspace]);
if (newImage == nil)
newImage = CGImageRetain(image);
CFRelease(devRGB);
CFRelease(devGray);
CFRelease(devCMYK);
return newImage;
}