-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpccImageUtilsOSX.h
More file actions
273 lines (212 loc) · 10 KB
/
gui.cpccImageUtilsOSX.h
File metadata and controls
273 lines (212 loc) · 10 KB
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
/* *****************************************
* File: cpccImageUtilsOSX.h
* Purpose: Portable (cross-platform), light-weight library
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2014 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* http://www.24hsoftware.com/portable-cpp-filesystem-library
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#if defined __OBJC__
#else
#error Must be compiled as obj-c++
#endif
#pragma once
// //////////////////////////////////////////////////////
// cpccImageUtilsOSX
// //////////////////////////////////////////////////////
class cpccImageUtilsOSX
{
public:
static void removeRepresentations(NSImage * aImagePtr)
{
/*
An NSImage returns an NSArray of its representations in response to a representations message.
Each representation is a kind of NSImageRep object:
NSEPSImageRep An image that can be recreated from EPS data that's either stored by the object or at a known location in the file system.
NSBitmapImageRep An image that can be recreated from bitmap or TIFF data.
NSCustomImageRep An image that can be redrawn by a method defined in the application.
NSCachedImageRep An image that has been rendered in an off-screen cache from data or instructions that are no longer available.
The image in the cache provides the only data from which the image can be reproduced.
addRepresentation:
Adds imageRep to the receiver's list of representations. After invoking this method, you may need to explicitly
set features of the new representation, such as size, number of colors, and so on. This is true in particular if
the NSImage has multiple image representations to choose from.
See NSImageRep and its subclasses for the methods you use to complete initialization.
Any representation that's added by this method is retained by the NSImage.
Note that representations can't be shared among NSImages.
*/
while ([[aImagePtr representations] count]>0)
[aImagePtr removeRepresentation: [[aImagePtr representations] objectAtIndex:0]];
}
static void logRepresentations(NSImage * aImagePtr)
{
if (!aImagePtr)
{
warningLog().add("logRepresentations() called with null image");
}
int nRepresentations = (int) [[aImagePtr representations] count];
NSString *classname;
for (int i=0; i<nRepresentations; i++)
{
classname = [[[aImagePtr representations] objectAtIndex:i] className ];
infoLog().addf("Representation #%i has class:%s", i,
(char *) [classname UTF8String] );
}
}
/*
static NSBitmapImageRep * getRepresentationOfNSImage(NSImage * aImagePtr, const int index=0)
{
//infoLog().add("cpccImageMacBase.getBmpRepresentation() entering");
if (!aImagePtr)
return NULL;
int nRepresentations = [[aImagePtr representations] count];
if (!(nRepresentations>0))
{ warningLog().add("getBmpRepresentation no representations");
return NULL;
}
else
{
logRepresentations(aImagePtr);
}
// You can use [NSBitmapImageRep imageRepWithData:[myImage TIFFRepresentation]] to get a bitmap rep
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBitmapImageRep/isPlanar
// https://discussions.apple.com/thread/1774118
if (index<nRepresentations)
return [[aImagePtr representations] objectAtIndex:index];
errorLog().addf("getBmpRepresentationOfNSImage() asked for representation %i, but the count is %i", index, nRepresentations);
return NULL;
// http://www.cocoabuilder.com/archive/cocoa/185814-nsimage-representations.html
// return [ NSBitmapImageRep imageRepWithData:[m_imagePtr TIFFRepresentation]];
}
*/
/*
static NSBitmapImageRep * getBmpRepresentationOfNSImage(NSImage * aImagePtr)
{
//infoLog().add("cpccImageMacBase.getBmpRepresentation() entering");
if (!aImagePtr)
return NULL;
int nRepresentations = [[aImagePtr representations] count];
if (!(nRepresentations>0))
{ warningLog().add("getBmpRepresentationOfNSImage no representations");
return NULL;
}
else
{
logRepresentations(aImagePtr);
}
for (int i=0; i<nRepresentations ; i++)
if ([[[aImagePtr representations] objectAtIndex:i] class ] == [NSBitmapImageRep class])
return [[aImagePtr representations] objectAtIndex:i];
warningLog().add("getBmpRepresentationOfNSImage no BmpRepresentation found");
return NULL;
}
*/
static NSBitmapImageRep * createBmpScreenshotOfLockedImage(const int aWidth, const int aHeight)
{
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0,0, aWidth, aHeight)];
return rep;
}
static NSBitmapImageRep* createBitmapRepFromNSImage(NSImage *aImagePtr)
{
if (!aImagePtr)
return nil;
if (!aImagePtr.TIFFRepresentation)
return nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithData: [aImagePtr TIFFRepresentation]];
#pragma clang diagnostic pop
return [bitmapImageRep autorelease];
}
static NSBitmapImageRep * createBlankBmpRepresentation(const int aWidth, const int aHeight, const bool aHasAlpha)
{
infoLog().add("cpccImageMacBase.createBmpRepresentation()");
if(aWidth < 1 || aHeight < 1)
return NULL;
/*
If planes is NULL or an array of NULL pointers, this method allocates enough memory
to hold the image described by the other arguments. You can then obtain pointers to
this memory (with the getPixel:atX:y: or bitmapData method) and fill in the image data.
In this case, the allocated memory will belong to the object and will be freed when it’s freed.
*/
// see initForXPMWithDepth
// http://web.mit.edu/Emacs/source/emacs/src/nsimage.m
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: aWidth
pixelsHigh: aHeight
bitsPerSample: 8
samplesPerPixel: aHasAlpha ? 4 : 3
hasAlpha: aHasAlpha
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bitmapFormat: 0 // RGBA
bytesPerRow: 0 // 0 == autodetect
bitsPerPixel: 0 // 0 == autodetect
// bitsPerPixel: 32 // debug retina
];
if (!rep)
warningLog().add("cpccImageMacBase.createBmpRepresentation() gave null rep");
return rep;
}
static void drawBmpRepresentation(const int x, const int y, const NSBitmapImageRep * aRep)
{
// return; // debug retina: the flow does not pass from here
if (!aRep)
return;
[NSGraphicsContext saveGraphicsState];
[aRep drawInRect:NSMakeRect(x, y, aRep.size.width, aRep.size.height)];
[NSGraphicsContext restoreGraphicsState];
}
private:
static void drawNSImageInNSView_notUsed(int x, int y, NSImage *img, NSView *aView)
{
if (!img)
return;
if (!aView)
return;
[aView lockFocus];
/*
If you want to ensure that a specific image representation is used,
you can use the drawRepresentation:inRect: method of NSImage
*/
[img drawAtPoint:NSMakePoint(x, y) fromRect:NSZeroRect operation: NSCompositeSourceOver fraction: 1.0f];
[aView unlockFocus];
}
public:
static NSImage *loadImageToNSImage(const cpcc_char* aFullPathFilename)
{
if (!aFullPathFilename)
return NULL;
NSString * tmpFilename = [[NSString alloc] initWithUTF8String:aFullPathFilename] ;
NSImage * tmpImg = [[NSImage alloc]init];;
[tmpImg initWithContentsOfFile:tmpFilename];
[tmpFilename release];
if (! [tmpImg isValid])
{
errorLog().addf("loadImageToNSImage() initWithContentsOfFile failed:%s", aFullPathFilename);
return NULL;
}
infoLog().addf("loadImageToNSImage(%s)",aFullPathFilename);
return tmpImg;
}
static NSBitmapImageRep *loadImageToNSBitmapImageRep(const cpcc_char *aFullPathFilename)
{
NSImage * tmpImg = loadImageToNSImage(aFullPathFilename);
if (tmpImg==NULL)
return NULL;
if (!tmpImg.TIFFRepresentation)
return NULL;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
// you have to retain tmpImg to retain the result
return [NSBitmapImageRep imageRepWithData:[tmpImg TIFFRepresentation] ];
#pragma clang diagnostic pop
}
};