-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomIconGenerator.m
1361 lines (1141 loc) · 54 KB
/
CustomIconGenerator.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// CustomIconGenerator.m
// Add Folder Icons
//
// Created by Andrew Hodgkinson on 3/03/16.
// Copyright © 2016 Hipposoft. All rights reserved.
//
#import "CustomIconGenerator.h"
#import "Add_Folder_IconsAppDelegate.h"
#import "GlobalConstants.h"
#import "GlobalSemaphore.h"
#import "Icons.h"
#import "IconStyleManager.h"
#import "SlipCoverSupport.h"
#import "CaseGenerator.h"
/* Pre-computed locations inside a CANVAS_SIZE square canvas for cropped
* thumbnail icons for when there are between 1 and 4 icons available. See
* "GlobalConstants.h" for CANVAS_SIZE and "CustomIconGenerator.h" for other
* relevant constants throughout this implementation.
*
* The compuation takes the worst case of an icon with no border, shadow or
* rotation with image cropping enabled, so the painted image fills the whole
* canvas prior to scaling. The width, height and origins are calculated on
* that basis. At run-time, adjustments are made to these values when there
* is more than one thumbnail being included, which bring thumbnails closer
* together when shadows or rotation are included as the image in such cases
* is not filling the canvas and the gaps start to look too large. With
* rotation and shadows, a bit of overlap looks good.
*
* Ordering is important to make sure that when one icon overlaps another,
* it will overlap with its drop shadow part hanging above the next icon. So,
* start plotting in the bottom right corner and work anti-clockwise.
*/
static CGRect locations_os_x_10_6_to_10_9[ 4 ][ 4 ] =
{
{
{ .origin.x = 112, .origin.y = 76, .size.width = 288, .size.height = 288 }
},
{
{ .origin.x = 264, .origin.y = 112, .size.width = 216, .size.height = 216 },
{ .origin.x = 32, .origin.y = 112, .size.width = 216, .size.height = 216 }
},
{
{ .origin.x = 264, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 132, .origin.y = 264, .size.width = 248, .size.height = 248 }
},
{
{ .origin.x = 264, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 264, .origin.y = 264, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 264, .size.width = 248, .size.height = 248 }
}
};
static CGRect locations_os_x_10_10_or_later[ 4 ][ 4 ] =
{
{
{ .origin.x = 123, .origin.y = 90, .size.width = 268, .size.height = 276 }
},
{
{ .origin.x = 268, .origin.y = 134, .size.width = 216, .size.height = 216 },
{ .origin.x = 32, .origin.y = 134, .size.width = 216, .size.height = 216 }
},
{
{ .origin.x = 264, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 132, .origin.y = 264, .size.width = 248, .size.height = 248 }
},
{
{ .origin.x = 264, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 0, .size.width = 248, .size.height = 248 },
{ .origin.x = 264, .origin.y = 264, .size.width = 248, .size.height = 248 },
{ .origin.x = 0, .origin.y = 264, .size.width = 248, .size.height = 248 }
}
};
static CGRect (*locations)[4] = NULL; /* Initialised in the constructor */
@interface CustomIconGenerator()
- ( NSArray * ) allocFoundImagePathArray: ( NSError ** ) error;
- ( BOOL ) paintImageAt: ( CFStringRef ) fullPosixPath
intoRect: ( CGRect ) rect
usingContext: ( CGContextRef ) context
maintainingAspectRatio: ( BOOL ) maintainAspectRatio;
- ( CGImageRef ) allocCustomIconFrom: ( NSArray * ) chosenImages
withBackground: ( CGImageRef ) backgroundImage
errorsTo: ( NSError ** ) error;
- ( CGImageRef ) allocSlipCoverIcon: ( NSArray * ) chosenImages
errorsTo: ( NSError ** ) error;
@property CGImageRef backgroundImage;
@end
@implementation CustomIconGenerator
/******************************************************************************\
* -initWithIconStyle:forPOSIXPath:
*
* Initialise an instance of the icon generator based on the given IconStyle
* instance (retained by reference) and full POSIX path of the folder for which
* an icon is to be generated - source images for the icon are taken from this.
* Once initialised, you may want to modify other read/write properties before
* generating an icon.
*
* Any relevant user preferences values are frozen in when the instance is
* created, so changes to them will not affect the operation of this generator.
* If you pass an IconStyle instance that's still connected to CoreData and may
* be modified in the background by preferences changes, though, beware.
*
* In: ( IconStyle * ) theIconStyle
* IconStyle to use for the generated icon. Can be read back later via the
* "iconStyle" property.
*
* ( NSString * ) thePOSIXPath
* Full POSIX path of the folder to use for image enumeration and icon
* generation. Can be read back later by the "posixPath" property.
\******************************************************************************/
- ( instancetype ) initWithIconStyle: ( IconStyle * ) theIconStyle
forPOSIXPath: ( NSString * ) thePosixPath
{
if ( ( self = [ super init ] ) )
{
NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults ];
NSArray * filenames = [ defaults arrayForKey: @"coverArtFilenames" ];
/* The defaults system actually gives us an array of dictionarys of
* the single entry form "leafname = <foo>" - collect those into an
* array of the values of "<foo>". Include also a fallback in case
* all cover art filenames were deleted.
*/
filenames = [ filenames valueForKeyPath: @"leafname" ];
if ( [ filenames count ] == 0 )
{
filenames = @[ @"cover", @"folder" ];
}
_posixPath = thePosixPath;
_iconStyle = theIconStyle;
_coverArtFilenames = [ [ NSArray alloc ] initWithArray: filenames copyItems: YES ];
_useColourLabelsToIdentifyCoverArt = [ defaults boolForKey: @"colourLabelsIndicateCoverArt" ];
_makeBackgroundOpaque = NO;
_nonRandomImageSelectionForAPreview = NO;
if ( _iconStyle.usesSlipCover.boolValue == YES )
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
_slipCoverCase =
[
SlipCoverSupport findDefinitionFromName: [ _iconStyle slipCoverName ]
withinDefinitions: [ iconStyleManager slipCoverDefinitions ]
];
}
else
{
_slipCoverCase = nil;
}
_backgroundImage = [ ( Add_Folder_IconsAppDelegate * ) [ NSApp delegate ] standardFolderIcon ];
/* This is a lazy-initialised static variable defined towards the top
* of this source file.
*/
if ( locations == NULL )
{
if ( [ [ NSProcessInfo processInfo ] respondsToSelector: @selector( operatingSystemVersion ) ] )
{
/* The above interface only exists in OS X 10.10 or later and,
* happily, that's the version at which we change our position
* matrix for the thumbnails.
*/
locations = locations_os_x_10_10_or_later;
}
else
{
locations = locations_os_x_10_6_to_10_9;
}
}
}
return self;
}
/******************************************************************************\
* -allocFoundImagePathArray:
*
* Based on given folder and icon generation parameters, search the folder for
* suitable images to use as part of icon generation and return a pointer to a
* mutable array containing full POSIX paths (as NSString pointers) of the
* found image(s).
*
* Images may be enumerated from the given directory freely, or be constrainted
* by some of the settings in this instance's configured icon style. Results
* are always narrowed down to a collection no larger than the icon style's
* "maxImages" property. The order of results will always be random even if
* there were fewer images found than this maximum; results of this call thus
* may differ from call to call even if most other parameters are equal. This
* can be prevented by setting the nonRandomImageSelectionForAPreview property
* to YES prior to calling. Returned results are then drawn sequentially from
* the found pool, in order of enumeration.
*
* This function allows re-entrant callers from multiple threads using
* independent execution contexts, as it protects thread-sensitive sections
* using the global semaphore.
*
* In: ( NSError ** ) error
* Pointer to an NSError *, which is filled in with 'nil' if no errors
* occur, else a pointer to an initialised NSError describing the problem.
* You can pass 'nil' here if you don't care about errors.
*
* Out: Pointer to a caller-owned NSArray containing the paths of the images,
* or "nil" if either none were found or an error occurs.
\******************************************************************************/
- ( NSArray * ) allocFoundImagePathArray: ( NSError ** ) error
{
NSFileManager * fileMgr = [ [ NSFileManager alloc ] init ]; /* http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html */
NSString * enumPath = _posixPath;
NSString * currFile;
NSMutableArray * images = [ NSMutableArray arrayWithCapacity: 0 ];
NSMutableArray * chosenImages = nil;
BOOL failed = NO;
errno = 0;
if ( error ) *error = nil;
/* We consider ourselves in cover art mode if using that flag explicitly or
* if using SlipCover code for icon generation.
*/
BOOL onlyUseCoverArt = self.iconStyle.onlyUseCoverArt.boolValue |
self.iconStyle.usesSlipCover.boolValue;
/* Directory enumeration is needed in multiple image mode and may be needed
* in cover art mode; besides, this gives us a quick way to discover if the
* path to the folder seems to be invalid.
*/
NSDirectoryEnumerator * dirEnum = [ fileMgr enumeratorAtPath: enumPath ];
NSDictionary * dirAttrs = [ dirEnum directoryAttributes ];
if ( ! dirAttrs )
{
if ( error )
{
NSDictionary * dict =
@{
NSLocalizedDescriptionKey: @"Unable to generate icon",
NSLocalizedFailureReasonErrorKey: @"The folder was either not found or access to it was denied"
};
*error = [ NSError errorWithDomain: NSCocoaErrorDomain
code: NSFileReadNoPermissionError
userInfo: dict ];
}
__Require( dirAttrs, nothingToDo );
}
/* Look for image files. The search is exited early if a certain number
* of images have been found or the loop has been running for too long in
* multiple image mode.
*/
if ( onlyUseCoverArt )
{
/* Although we got hold of a directory enumerator earlier, we get
* a new one here as we're only interested in finding the right
* label colour or files with the right start to their names.
* Subdirectories are explicitly NOT scanned.
*/
NSString * found = nil;
NSURL * enumPathAsURL = [ [ NSURL alloc ] initFileURLWithPath: enumPath
isDirectory: YES ];
dirEnum = [ fileMgr enumeratorAtURL: enumPathAsURL
includingPropertiesForKeys: @[
NSURLIsDirectoryKey,
NSURLIsRegularFileKey,
NSURLLabelColorKey,
NSURLLabelNumberKey
]
options: NSDirectoryEnumerationSkipsHiddenFiles
errorHandler: nil ];
@try
{
/* Semaphore use rationale - see multiple image code below */
globalSemaphoreClaim();
for ( NSURL * theURL in dirEnum )
{
NSString * fullPath = [ theURL path ];
/* Extract the cached directory and color label data */
NSError * resourceError = nil;
NSDictionary * info =
[
theURL resourceValuesForKeys: @[
NSURLIsDirectoryKey,
NSURLIsRegularFileKey,
NSURLLabelColorKey,
NSURLLabelNumberKey
]
error: &resourceError
];
/* Assuming no errors, skip subdirectories and look for
* labelled, "cover" or "folder" leafname images.
*
* MAXIMUM_IMAGE_SIZE is not obeyed as we're looking for
* specific leafnames or a colour label in cover art
* mode and the implication is that the user wants us to
* use that image regardless. This is quite different
* from a generic directory scan.
*/
if ( resourceError == nil )
{
BOOL isDirectory;
BOOL isRegularFile;
NSColor * labelColour;
NSNumber * labelNumber;
isDirectory = [ info[ @"NSURLIsDirectoryKey" ] boolValue ];
isRegularFile = [ info[ @"NSURLIsRegularFileKey" ] boolValue ];
labelColour = info[ @"NSURLLabelColorKey" ];
labelNumber = info[ @"NSURLLabelNumberKey" ];
if ( isDirectory == YES )
{
[ dirEnum skipDescendants ];
}
else if ( isRegularFile == YES && isImageFile( fullPath ) == YES )
{
NSString * leaf = [ [ fullPath lastPathComponent ] stringByDeletingPathExtension ];
if (
self.useColourLabelsToIdentifyCoverArt &&
(
labelColour != nil ||
[ labelNumber integerValue ] > 0
)
)
{
found = fullPath;
break;
}
NSUInteger foundIndex =
[
self.coverArtFilenames indexOfObjectWithOptions: NSEnumerationConcurrent
passingTest: ^BOOL ( NSString * obj, NSUInteger idx, BOOL * stop )
{
if ( [ obj localizedCaseInsensitiveCompare: leaf ] == NSOrderedSame )
{
*stop = YES;
return YES;
}
else
{
return NO;
}
}
];
if ( foundIndex != NSNotFound )
{
found = fullPath;
break;
}
}
}
}
}
@catch ( id ignored )
{
if ( error )
{
NSDictionary * dict =
@{
NSLocalizedDescriptionKey: @"Unable to generate icon",
NSLocalizedFailureReasonErrorKey: @"An unexpected internal error occured while reading the folder contents"
};
*error = [ NSError errorWithDomain: NSPOSIXErrorDomain
code: errno
userInfo: dict ];
}
failed = YES;
}
@finally
{
globalSemaphoreRelease();
}
if ( found ) [ images addObject: found ];
}
else /* "if ( onlyUseCoverArt )" */
{
srandomdev(); /* Randomise the random number generator */
/* Directory scanning is timed to avoid excessively long / deep folder
* recursion holding up process completion. To keep this timer sane, only
* one scan is run at a time. This helps avoid excessive filesystem
* thrashing in passing (i.e. there's a very good chance that the code
* will complete more quickly when this section runs in series rather
* than if it attempts to run in parallel).
*/
@try
{
globalSemaphoreClaim();
clock_t startTime = clock();
while ( ( currFile = [ dirEnum nextObject ] ) )
{
NSDictionary * currAttrs = [ dirEnum fileAttributes ];
if ( ! currAttrs ) continue;
NSString * fileType = currAttrs[ NSFileType ];
NSString * fullPath = [
enumPath stringByAppendingPathComponent: currFile
];
/* Only interested in regular files or directories, nothing more. Skip
* directories that appear to have filename extensions because
* they're probably packaged formats of some kind (e.g. applications).
*/
if ( [ fileType isEqualToString: NSFileTypeRegular ] )
{
if ( isImageFile( fullPath ) == YES )
{
#ifdef MAXIMUM_IMAGE_SIZE
{
NSNumber * size = currAttrs[ NSFileSize ];
if ( size.unsignedLongLongValue > MAXIMUM_IMAGE_SIZE ) continue;
}
#endif
[ images addObject: fullPath ];
/* Once we have enough images, bail */
if (
MAXIMUM_IMAGES_FOUND != 0 &&
[ images count ] >= MAXIMUM_IMAGES_FOUND
)
break;
}
}
else if (
SKIP_PACKAGES
&& [ fileType isEqualToString: NSFileTypeDirectory ]
&& isLikeAPackage( fullPath )
)
{
[ dirEnum skipDescendents ];
}
/* If we've run for too long, bail */
if (
MAXIMUM_LOOP_TIME_TICKS != 0 &&
clock() - startTime >= MAXIMUM_LOOP_TIME_TICKS
)
break;
}
}
@catch ( id ignored )
{
if ( error )
{
NSDictionary * dict =
@{
NSLocalizedDescriptionKey: @"Unable to generate icon",
NSLocalizedFailureReasonErrorKey: @"An unexpected internal error occured while reading the folder contents"
};
*error = [ NSError errorWithDomain: NSPOSIXErrorDomain
code: errno
userInfo: dict ];
}
failed = YES;
}
@finally
{
globalSemaphoreRelease();
}
} /* "else" of "if ( onlyUseCoverArt )" */
/* If there are no images, exit; the standard folder icon will be used */
if ( [ images count ] == 0 ) __Require( false, nothingToDo );
/* Otherwise, choose up to four images at random */
NSUInteger maxImages = self.iconStyle.maxImages.unsignedIntegerValue;
if ( maxImages < 1 ) maxImages = 1;
else if ( maxImages > 4 ) maxImages = 4;
chosenImages = [ [ NSMutableArray alloc ] initWithCapacity: 0 ];
while ( [ images count ] > 0 && [ chosenImages count ] < maxImages )
{
/* Using random() in this way is fine - unlike rand(), all bits are
* considered sufficiently random in the generated integer.
*/
NSUInteger randomIndex;
if ( self.nonRandomImageSelectionForAPreview == YES ) randomIndex = 0;
else randomIndex = random() % [ images count ];
[ chosenImages addObject: images[ randomIndex ] ];
[ images removeObjectAtIndex: randomIndex ];
}
nothingToDo:
return chosenImages;
}
/******************************************************************************\
* -paintImageAt:intoRect:usingContext:maintainingAspectRatio:
*
* Paint an image found at the given fully specified POSIX-style file path
* into the given rectangle under the given context. The image is cropped to
* a square before being painted; if the target rectangle is not also square
* the image will be distorted when painted.
*
* Note the prevailing use of Core Foundation / Core Graphics types herein.
*
* In: ( CFStringRef ) fullPosixPath
* Full POSIX path of the image to load and paint;
*
* ( CGRect ) rect
* Rectangle into which the cropped image should be painted;
*
* ( CGContextRef ) context
* Core Graphics context for the painting operation;
*
* ( BOOL ) maintainAspectRatio
* If NO the image is cropped to a square to fill the parameter in 'rect'.
* If YES the image is scaled to fit, maintaing aspect ratio, so not all
* of the given rectangle will be painted upon for non-square images.
*
* Out: YES if successful, NO if failed.
\******************************************************************************/
- ( BOOL ) paintImageAt: ( CFStringRef ) fullPosixPath
intoRect: ( CGRect ) rect
usingContext: ( CGContextRef ) context
maintainingAspectRatio: ( BOOL ) maintainAspectRatio
{
BOOL success = YES;
size_t width, height;
/* Turn the POSIX path into a URL, the URL into an image source and the
* image source into an image object based on index 0 from the source
* file (i.e. for multi-page TIFFs, Icon files etc., take the first of
* however many sub-images are contained within).
*/
CGImageSourceRef imageSource = NULL;
CGImageRef image = NULL;
CFURLRef url = CFURLCreateWithFileSystemPath
(
kCFAllocatorDefault,
fullPosixPath,
kCFURLPOSIXPathStyle,
false
);
if ( url ) imageSource = CGImageSourceCreateWithURL( url, NULL );
if ( imageSource ) image = CGImageSourceCreateImageAtIndex( imageSource, 0, NULL );
if ( image )
{
width = CGImageGetWidth ( image );
height = CGImageGetHeight ( image );
/* EXIF rotation is in the metadata and the above ignores it, so there
* is more work to do still. If we used NSImage this would all go away,
* but tests of early NSImage-based code showed it was very much slower
* than CoreGraphics. Since calculating the correct top-or-middle
* cropping rectangles and persuading all the coordinate space
* transformations to work properly would be particularly thorny and
* open to mistakes for edge case image types, we simply recreate a new
* image in the transformed orientation at full size and continue to
* work with that later in the code.
*
* Allocation failures are ignored as correct orientation inside the
* thumbnail is not considered critical.
*
* The code is derived from:
*
* http://developer.apple.com/library/mac/#samplecode/MyPhoto/Listings/Step8_ImageView_m.html
* http://developer.apple.com/library/mac/#samplecode/CGRotation/Introduction/Intro.html
*/
NSDictionary * metadata = (__bridge_transfer NSDictionary * /* Toll-free bridge */ )
CGImageSourceCopyPropertiesAtIndex( imageSource, 0, NULL );
if ( metadata )
{
NSNumber * val;
CGFloat dpi, xdpi, ydpi;
int orientation;
val = metadata[ ( id ) kCGImagePropertyDPIWidth ];
dpi = val.floatValue;
xdpi = ( dpi == 0 ) ? 72.0 : dpi;
val = metadata[ ( id ) kCGImagePropertyDPIHeight ];
dpi = val.floatValue;
ydpi = ( dpi == 0 ) ? 72.0 : dpi;
val = metadata[ ( id ) kCGImagePropertyOrientation ];
orientation = val.intValue;
if ( orientation < 1 || orientation > 8 ) orientation = 1;
CGFloat x = ( ydpi > xdpi ) ? ydpi / xdpi : 1;
CGFloat y = ( xdpi > ydpi ) ? xdpi / ydpi : 1;
if ( x != 1.0 || y != 1.0 || orientation != 1 )
{
CGFloat w = x * width;
CGFloat h = y * height;
CGAffineTransform ctms[ 8 ] =
{
{ x, 0, 0, y, 0, 0 }, // 1 = row 0 top, col 0 lhs = normal
{ -x, 0, 0, y, w, 0 }, // 2 = row 0 top, col 0 rhs = flip horizontal
{ -x, 0, 0, -y, w, h }, // 3 = row 0 bot, col 0 rhs = rotate 180
{ x, 0, 0, -y, 0, h }, // 4 = row 0 bot, col 0 lhs = flip vertical
{ 0, -x, -y, 0, h, w }, // 5 = row 0 lhs, col 0 top = rot -90, flip vert
{ 0, -x, y, 0, 0, w }, // 6 = row 0 rhs, col 0 top = rot 90
{ 0, x, y, 0, 0, 0 }, // 7 = row 0 rhs, col 0 bot = rot 90, flip vert
{ 0, x, -y, 0, h, 0 } // 8 = row 0 lhs, col 0 bot = rotate -90
};
/* Create a context big enough to hold the image's actual pixel
* size, regardless of pixel aspect ratio, but accounting for a
* possible rotation at ±90 degrees (orientations 5-8).
*/
CGContextRef transformationContext;
size_t contextWidth, contextHeight;
if ( orientation <= 4 ) { contextWidth = width; contextHeight = height; }
else { contextWidth = height; contextHeight = width; }
transformationContext = CGBitmapContextCreate
(
NULL,
contextWidth,
contextHeight,
8, /* Bits per component */
contextWidth * 4, /* Bytes per row */
CGImageGetColorSpace( image ),
kCGImageAlphaPremultipliedFirst
);
if ( transformationContext != NULL )
{
CGContextConcatCTM( transformationContext, ctms[ orientation - 1 ] );
CGContextDrawImage( transformationContext, CGRectMake( 0, 0, width, height ), image );
/* Release the old image first to avoid accumulating lots
* of copies in RAM. Worse case, we end up with a NULL
* 'image' and no thumbnail plotted for this image.
*/
CFRelease( image );
image = CGBitmapContextCreateImage( transformationContext );
CFRelease( transformationContext );
if ( image )
{
width = CGImageGetWidth ( image );
height = CGImageGetHeight ( image );
}
}
}
}
if ( maintainAspectRatio == NO && image )
{
/* Create a sub-image based on a square crop of the original. If
* the image is wider than tall (landscape), use a center crop.
* If the image is taller than wide (portrait), use the top part
* of it - on average this works well for pictures of people.
*/
BOOL isSquare = NO;
CGRect cropRect;
CGImageRef croppedImage;
if ( width > height ) cropRect = CGRectMake( ( width - height ) / 2, 0, height, height );
else if ( height > width ) cropRect = CGRectMake( 0, 0, width, width ); /* Image *top* left is (0,0) */
else isSquare = YES;
if ( isSquare == NO )
{
croppedImage = CGImageCreateWithImageInRect( image, cropRect );
CFRelease( image );
image = croppedImage;
}
}
else
{
/* Adjust the plotting rectangle to avoid image cropping */
CGFloat rectWidth = rect.size.width;
CGFloat rectHeight = rect.size.height;
if ( width > height )
{
CGFloat scaledHeight = height * ( rectWidth / width );
CGFloat yOffset = ( rectHeight - scaledHeight ) / 2;
rect.origin.y += yOffset;
rect.size.height = scaledHeight;
}
else
{
CGFloat scaledWidth = width * ( rectHeight / height );
CGFloat xOffset = ( rectWidth - scaledWidth ) / 2;
rect.origin.x += xOffset;
rect.size.width = scaledWidth;
}
}
}
/* Check 'image' again in case cropping was attempted but failed */
if ( image ) CGContextDrawImage( context, rect, image );
/* Make sure everything is released */
if ( image ) CFRelease( image ); else success = NO;
if ( imageSource ) CFRelease( imageSource ); else success = NO;
if ( url ) CFRelease( url ); else success = NO;
return success;
}
/******************************************************************************\
* -allocCustomIconFrom:withBackground:errorsTo:
*
* Generate a folder icon with an array of images to be included as thumbnails,
* at CANVAS_SIZE x CANVAS_SIZE resolution (see the companion header file),
* using custom icon generation parameters. The caller is responsible
* for releasing the returned object when it is no longer needed.
*
* This function allows re-entrant callers from multiple threads using
* independent execution contexts.
*
* Note the prevailing use of Core Foundation / Core Graphics types herein.
* Even so, the caller must ensure that an autorelease pool is available.
*
* In: ( NSArray * ) chosenImages
* Pointer to an array of NSString pointers where each string gives the
* the full POSIX path of an image to be included within the icon. At
* one image must be present. If there are more than needed, then items
* at higher indicies will be ignored (e.g. if you provide four images but
* but the icon style uses cover art mode, only the first item in the
* array will be used). An appropriate array can be obtained from a call
* to "-allocFoundImagePathArray:".
*
* ( CGImageRef ) backgroundImage
* CGImageRef pointing to an icon to put underneath thumbnails if icon
* parameters say that this should be used - usually this is obtained by
* a call to "allocFolderIcon" from outside the application main thread.
* Use NULL for no background image;
*
* ( NSError ** )
* Pointer to an NSError * updated on exit to point to an initialised
* NSError if anything went wrong, else "nil". If you can't do anything
* about errors anyway, just pass "nil".
*
* Out: CGImageRef pointing to thumbnail image or NULL if there is an error, or
* if there is no need to assign a custom icon (no images in folder). If
* non-NULL, caller must CFRelease() the memory when finished.
\******************************************************************************/
- ( CGImageRef ) allocCustomIconFrom: ( NSArray * ) chosenImages
withBackground: ( CGImageRef ) backgroundImage
errorsTo: ( NSError ** ) error;
{
CGImageRef finalImage = NULL;
if ( error ) *error = nil;
/* We consider ourselves in cover art mode if using that flag explicitly.
* This method should never be called for Slip Cover icon styles.
*/
BOOL onlyUseCoverArt = self.iconStyle.onlyUseCoverArt.boolValue;
/**************************************************************************\
* Create layers representing thumbnails of the images
\**************************************************************************/
/* Get a graphics context for painting things. This is constructed as a
* bespoke bitmap context rather than using the one we could obtain from
* Quick Look because it opens up more possibilities later (there is
* direct control over alpha channel provision, though transparency is
* extremely problematic under Quick Look - most of the time, you don't
* get it, regardless of context).
*/
NSUInteger canvasSize = dpiValue( CANVAS_SIZE );
CGSize pixelSize = CGSizeMake( canvasSize, canvasSize );
CGRect pixelRect = CGRectMake( 0, 0, canvasSize, canvasSize );
CGContextRef context = NULL;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if ( colorSpace )
{
context = CGBitmapContextCreate
(
NULL, /* OS X 10.3 or later => CG allocates for us */
canvasSize,
canvasSize,
8, /* Bits per component */
canvasSize * 4, /* Bytes per row */
colorSpace,
kCGImageAlphaPremultipliedFirst
);
CGColorSpaceRelease( colorSpace );
}
if ( ! context ) return nil; // Note early exit!
CGContextSetShouldAntialias ( context, true );
CGContextSetInterpolationQuality ( context, kCGInterpolationHigh );
/* Create layers into which thumbnails will be drawn. These will be
* scaled down again when drawn into the final icon and will use anti-
* aliasing then, but we need to keep high quality settings on the
* layers to make sure that the rotated edges look good.
*/
NSUInteger count = [ chosenImages count ];
CFMutableArrayRef layers = CFArrayCreateMutable
(
kCFAllocatorDefault,
count,
NULL
);
for ( size_t index = 0; index < count; index ++ )
{
/* Ensure the array actually contains 'count' NULL items. Within the
* Grand Central processing loop below, used layers are written into
* the array at specific indices. Doing this means that the array can
* be written to by concurrent processes without locking. If we just
* tried to extend the array within the processing loop then we'd have
* to serialise the operation (as two threads attempting to extend the
* same array simultaneously could just corrupt the data structure).
*/
CFArrayAppendValue( layers, NULL );
}
dispatch_apply
(
count,
dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ),
^( size_t index )
{
NSString * currFile = chosenImages[ ( NSUInteger ) index ];
CGLayerRef layer = CGLayerCreateWithContext( context, pixelSize, NULL );
CGContextRef layerCtx = CGLayerGetContext( layer );
CGContextSetShouldAntialias ( layerCtx, true );
CGContextSetInterpolationQuality ( layerCtx, kCGInterpolationHigh );
/* To start with, set a thumbnail size to match the entire canvas.
* As things like shadows, rotation and border get applied, reduce
* these values as appropriate to make sure that the final scaled
* thumbnail fits within the canvas, even with any effects applied.
*/
CGFloat thumbSize = canvasSize;
/* Paint in a transparency layer using a shadow offset to the bottom
* and right by BLUR_OFFSET with a radius of BLUR_RADIUS.
*/
CGContextBeginTransparencyLayer( layerCtx, NULL );
CGContextTranslateCTM( layerCtx, canvasSize / 2, canvasSize / 2 );
if ( self.iconStyle.randomRotation.boolValue == YES )
{
CGContextRotateCTM( layerCtx, ( ( random() % 300 ) - 150 ) / 2000.0 );
thumbSize -= dpiValue( ROTATION_PAD );
}
if ( self.iconStyle.dropShadow.boolValue == YES )
{
/* Go for a symmetrical border-like drop shadow in multi-image
* mode, else for the larger icon-filling cover art mode,
* use a drop shadow modelled on the Finder's typical shadow
* style - offset downwards.
*/
if ( onlyUseCoverArt == NO )
{
CGContextSetShadow
(
layerCtx,
CGSizeMake( 0, dpiValue( -BLUR_OFFSET ) ),
dpiValue( BLUR_RADIUS )
);
/* "* 2" on the blur offset is basically a fudge factor.
* AFAICS just adding the radius and offset should give a
* correct adjustment to make room for the shadow but in
* practice it gets cut off at the bottom unless a bit of
* extra room is provided.
*/
thumbSize -= dpiValue( BLUR_RADIUS + BLUR_OFFSET * 2 );
}
else
{
/* The default shadow colour is much lighter than shown in e.g.
* the Finder's thumbnails for image files (probably because of
* colour space issues), so darken it by specifying an exact
* colour in generic RGB space (noting that we used device RGB
* when the outer graphics bitmap context was created).
*/
CGColorRef c = CGColorCreateGenericRGB( 0.3, 0.3, 0.3, 1 );
if ( c )
{
CGContextSetShadowWithColor
(
layerCtx,
CGSizeMake( 0, dpiValue( -( BLUR_OFFSET / 2 ) ) ),
dpiValue( ( BLUR_RADIUS / 3 ) * ( BLUR_OFFSET / 2 ) ),
c
);
CGColorRelease( c );
/* Unlike the case above, here just adding the blur
* radius and offset is sufficient for some reason.
*/
thumbSize -= dpiValue( BLUR_RADIUS * ( BLUR_OFFSET / 2 ) + ( BLUR_OFFSET / 2 ) );
}
}
}
/* Hide the border in cover art mode or if borders are disabled,
* putting the shadow beneath the image.
*/
if ( self.iconStyle.whiteBackground.boolValue == YES )
{
CGFloat borderSize = thumbSize;