1/*
2    PPGNUstepGlue_TIFFRepresentations.m
3
4    Copyright 2014-2018 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for 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// Workarounds for two GNUstep issues with creating TIFF representations (NSData):
26// - Calling -[NSImage TIFFRepresentation] on (some) valid images causes a TIFF library error
27// - TIFF data written by GNUstep GUI 0.25.0 is incorrect (apparently fixed in GUI 0.25.1)
28
29#ifdef GNUSTEP
30
31#import <Cocoa/Cocoa.h>
32#import "NSObject_PPUtilities.h"
33#import "PPAppBootUtilities.h"
34#import "NSBitmapImageRep_PPUtilities.h"
35
36
37@implementation NSObject (PPGNUstepGlue_TIFFRepresentations)
38
39+ (void) ppGSGlue_TIFFRepresentations_InstallPatches
40{
41    macroSwizzleClassMethod(NSBitmapImageRep, TIFFRepresentationOfImageRepsInArray:,
42                                ppGSPatch_TIFFRepresentationOfImageRepsInArray:);
43
44    macroSwizzleInstanceMethod(NSBitmapImageRep, ppCompressedTIFFData,
45                                ppGSPatch_CompressedTIFFData);
46}
47
48+ (void) load
49{
50    macroPerformNSObjectSelectorAfterAppLoads(ppGSGlue_TIFFRepresentations_InstallPatches);
51}
52
53@end
54
55@implementation NSBitmapImageRep (PPGNUstepGlue_TIFFRepresentations)
56
57// PATCH: +[NSBitmapImageRep TIFFRepresentationOfImageRepsInArray:]
58//  Calling -[NSImage TIFFRepresentation] on some valid images causes a TIFF library error from
59// within +[NSBitmapImageRep TIFFRepresentationOfImageRepsInArray:]. Workaround is to patch
60// +[NSBitmapImageRep TIFFRepresentationOfImageRepsInArray:], and if the imageReps array just
61// contains a single bitmap (which should be the case for all NSImages in PikoPixel), call
62// -[NSBitmapImageRep TIFFRepresentation] instead (which seems to work correctly).
63
64+ (NSData *) ppGSPatch_TIFFRepresentationOfImageRepsInArray: (NSArray *) anArray
65{
66    if ([anArray count] == 1)
67    {
68        NSBitmapImageRep *bitmapRep = [anArray objectAtIndex: 0];
69
70        if ([bitmapRep isKindOfClass: [NSBitmapImageRep class]])
71        {
72            NSData *tiffData = [bitmapRep TIFFRepresentation];
73
74            if (tiffData)
75            {
76                return tiffData;
77            }
78        }
79    }
80
81    return [self ppGSPatch_TIFFRepresentationOfImageRepsInArray: anArray];
82}
83
84// PATCH: -[NSBitmapImageRep (PPUtilities) ppCompressedTIFFData]
85// GNUstep GUI 0.25.0 has issues reading back TIFF data that it wrote (OS X can't read the TIFF
86// data either), so patched to write PNG-format data instead. (This issue appears to have been
87// fixed in GUI 0.25.1).
88
89- (NSData *) ppGSPatch_CompressedTIFFData
90{
91    return [self ppCompressedPNGData];
92}
93
94@end
95
96#endif  // GNUSTEP
97
98