1/*
2    PPDocument_NativeFileIcon.m
3
4    Copyright 2013-2018 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for Mac OS X and GNUstep.
8    PikoPixel is a graphical application for drawing & editing pixel-art images.
9
10    PikoPixel is free software: you can redistribute it and/or modify it under
11    the terms of the GNU Affero General Public License as published by the
12    Free Software Foundation, either version 3 of the License, or (at your
13    option) any later version approved for PikoPixel by its copyright holder (or
14    an authorized proxy).
15
16    PikoPixel is distributed in the hope that it will be useful, but WITHOUT ANY
17    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18    FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19    details.
20
21    You should have received a copy of the GNU Affero General Public License
22    along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#import "PPDocument_NativeFileIcon.h"
26
27#import "PPGeometry.h"
28#import "NSBitmapImageRep_PPUtilities.h"
29#import "NSImage_PPUtilities.h"
30
31
32#define kMinDimensionForFileIcon    256
33
34
35@implementation PPDocument (NativeFileIcon)
36
37- (NSImage *) nativeFileIconImage
38{
39    float maxCanvasDimension, scalingFactor;
40    NSSize iconSize, scaledImageSize;
41    NSPoint scaledImageOrigin;
42    NSBitmapImageRep *iconBitmap;
43    NSImage *iconImage;
44
45    // File icon image needs to be square (non-square file icons are distorted on 10.7+)
46    // and contain transparency (opaque icons have display issues on 10.4/10.5)
47
48    if (NSIsEmptyRect(_canvasFrame))
49    {
50        goto ERROR;
51    }
52
53    maxCanvasDimension = MAX(_canvasFrame.size.width, _canvasFrame.size.height);
54
55    if (maxCanvasDimension < kMinDimensionForFileIcon)
56    {
57        scalingFactor = ceilf(((float) kMinDimensionForFileIcon) / maxCanvasDimension);
58    }
59    else
60    {
61        scalingFactor = 1.0f;
62    }
63
64    iconSize =
65        PPGeometry_SizeScaledByFactorAndRoundedToIntegerValues(
66                                            NSMakeSize(maxCanvasDimension, maxCanvasDimension),
67                                            scalingFactor);
68
69    scaledImageSize =
70        PPGeometry_SizeScaledByFactorAndRoundedToIntegerValues(_canvasFrame.size, scalingFactor);
71
72    scaledImageOrigin = PPGeometry_OriginPointForCenteringSizeInSize(scaledImageSize, iconSize);
73
74    iconBitmap = [NSBitmapImageRep ppImageBitmapOfSize: iconSize];
75
76    if (!iconBitmap)
77        goto ERROR;
78
79    [iconBitmap ppScaledCopyFromImageBitmap: _mergedVisibleLayersBitmap
80                inRect: _canvasFrame
81                toPoint: scaledImageOrigin
82                scalingFactor: scalingFactor];
83
84    if (PPGeometry_RectIsSquare(_canvasFrame)
85        && ![_mergedVisibleLayersBitmap ppImageBitmapHasTransparentPixels])
86    {
87        // workaround for 10.4/10.5 issue where setting a file's icon to a completely-opaque
88        // image results in a blank icon: if the image is square (leaves no transparent edges
89        // when scaled & centered on the icon) and completely opaque, then force transparency
90        // by decreasing the alpha value of one pixel (top-left corner)
91
92        PPImageBitmapPixel *iconPixel = (PPImageBitmapPixel *) [iconBitmap bitmapData];
93
94        if (!iconPixel)
95            goto ERROR;
96
97        macroImagePixelComponent_Alpha(iconPixel) = kMaxImagePixelComponentValue - 1;
98    }
99
100    iconImage = [NSImage ppImageWithBitmap: iconBitmap];
101
102    if (!iconImage)
103        goto ERROR;
104
105    return iconImage;
106
107ERROR:
108    return nil;
109}
110
111@end
112