-
Notifications
You must be signed in to change notification settings - Fork 285
/
macosx.m
302 lines (247 loc) · 8.83 KB
/
macosx.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
#import <Foundation/Foundation.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSGraphicsContext.h>
#include "allegro5/allegro.h"
#include "allegro5/fshook.h"
#include "allegro5/allegro_image.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_image.h"
#include "iio.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
typedef float CGFloat;
#endif
ALLEGRO_DEBUG_CHANNEL("OSXIIO")
// Just to make sure it's never al_malloc.
#define apple_malloc malloc
static ALLEGRO_BITMAP *really_load_image(char *buffer, int size, int flags)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
ALLEGRO_BITMAP *bmp = NULL;
void *pixels = NULL;
/* Note: buffer is now owned (and later freed) by the data object. */
NSData *nsdata = [[NSData alloc] initWithBytesNoCopy:buffer length:size];
NSImage *image = [[NSImage alloc] initWithData:nsdata];
[nsdata release];
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA);
if (!image)
goto done;
/* Get the image representations */
NSArray *reps = [image representations];
NSImageRep *image_rep = [reps objectAtIndex: 0];
if (!image_rep) {
[image release];
goto done;
}
/* Get the actual size in pixels from the representation */
unsigned char *data[5];
// TODO: We should check it really is a bitmap representation.
NSBitmapImageRep *bitmap_rep = (NSBitmapImageRep *)image_rep;
[bitmap_rep getBitmapDataPlanes:data];
pixels = data[0];
int w = [image_rep pixelsWide];
int h = [image_rep pixelsHigh];
int bits = [bitmap_rep bitsPerPixel];
int samples = bits / 8;
ALLEGRO_DEBUG("Read image of size %dx%dx%d\n", w, h, bits);
/* Then create a bitmap out of the memory buffer. */
bmp = al_create_bitmap(w, h);
if (bmp) {
ALLEGRO_LOCKED_REGION *lock = al_lock_bitmap(bmp,
ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
int i, j;
{
for (i = 0; i < h; i++) {
uint8_t *data_row = (uint8_t *) lock->data + lock->pitch * i;
uint8_t *source_row = (uint8_t *) pixels + w * samples * i;
if (samples == 4) {
if (premul) {
for (j = 0; j < w; j++) {
int r, g, b, a;
r = source_row[j * 4 + 0];
g = source_row[j * 4 + 1];
b = source_row[j * 4 + 2];
a = source_row[j * 4 + 3];
data_row[j * 4 + 0] = r * a / 255;
data_row[j * 4 + 1] = g * a / 255;
data_row[j * 4 + 2] = b * a / 255;
data_row[j * 4 + 3] = a;
}
}
else
memcpy(data_row, source_row, w * 4);
}
else if (samples == 3) {
for (j = 0; j < w; j++) {
data_row[j * 4 + 0] = source_row[j * 3 + 0];
data_row[j * 4 + 1] = source_row[j * 3 + 1];
data_row[j * 4 + 2] = source_row[j * 3 + 2];
data_row[j * 4 + 3] = 255;
}
}
else if (samples == 2) {
for (j = 0; j < w; j++) {
int a = data_row[j * 4 + 3] = source_row[j * 2 + 1];
if (!premul)
a = 255;
data_row[j * 4 + 0] = source_row[j * 2 + 0] * a / 255;
data_row[j * 4 + 1] = source_row[j * 2 + 0] * a / 255;
data_row[j * 4 + 2] = source_row[j * 2 + 0] * a / 255;
}
}
else if (samples == 1) {
for (j = 0; j < w; j++) {
data_row[j * 4 + 0] = source_row[j];
data_row[j * 4 + 1] = source_row[j];
data_row[j * 4 + 2] = source_row[j];
data_row[j * 4 + 3] = 255;
}
}
}
}
al_unlock_bitmap(bmp);
}
[image release];
done:
[pool drain];
return bmp;
}
static ALLEGRO_BITMAP *_al_osx_load_image_f(ALLEGRO_FILE *f, int flags)
{
ALLEGRO_BITMAP *bmp;
ASSERT(f);
int64_t size = al_fsize(f);
if (size <= 0) {
// TODO: Read from stream until we have the whole image
ALLEGRO_ERROR("Couldn't determine file size.\n");
return NULL;
}
/* Note: This *MUST* be the Apple malloc and not any wrapper, as the
* buffer will be owned and freed by the NSData object not us.
*/
void *buffer = apple_malloc(size);
al_fread(f, buffer, size);
/* Really load the image now. */
bmp = really_load_image(buffer, size, flags);
return bmp;
}
static ALLEGRO_BITMAP *_al_osx_load_image(const char *filename, int flags)
{
ALLEGRO_FILE *fp;
ALLEGRO_BITMAP *bmp;
ASSERT(filename);
ALLEGRO_DEBUG("Using native loader to read %s\n", filename);
fp = al_fopen(filename, "rb");
if (!fp) {
ALLEGRO_ERROR("Unable open %s for reading.\n", filename);
return NULL;
}
bmp = _al_osx_load_image_f(fp, flags);
al_fclose(fp);
return bmp;
}
extern NSImage* NSImageFromAllegroBitmap(ALLEGRO_BITMAP* bmp);
bool _al_osx_save_image_f(ALLEGRO_FILE *f, const char *ident, ALLEGRO_BITMAP *bmp)
{
NSBitmapImageFileType type;
if (!strcmp(ident, ".bmp")) {
type = NSBMPFileType;
}
else if (!strcmp(ident, ".jpg") || !strcmp(ident, ".jpeg")) {
type = NSJPEGFileType;
}
else if (!strcmp(ident, ".gif")) {
type = NSGIFFileType;
}
else if (!strcmp(ident, ".tif") || !strcmp(ident, ".tiff")) {
type = NSTIFFFileType;
}
else if (!strcmp(ident, ".png")) {
type = NSPNGFileType;
}
else {
ALLEGRO_ERROR("Unsupported image format: %s.\n", ident);
return false;
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSImage *image = NSImageFromAllegroBitmap(bmp);
NSArray *reps = [image representations];
NSData *nsdata = [NSBitmapImageRep representationOfImageRepsInArray: reps usingType: type properties: [NSDictionary dictionary]];
size_t size = (size_t)[nsdata length];
bool ret = al_fwrite(f, [nsdata bytes], size) == size;
[image release];
[pool drain];
return ret;
}
bool _al_osx_save_image(const char *filename, ALLEGRO_BITMAP *bmp)
{
ALLEGRO_FILE *fp;
bool retsave = false;
bool retclose = false;
fp = al_fopen(filename, "wb");
if (fp) {
ALLEGRO_PATH *path = al_create_path(filename);
if (path) {
retsave = _al_osx_save_image_f(fp, al_get_path_extension(path), bmp);
al_destroy_path(path);
}
retclose = al_fclose(fp);
}
else {
ALLEGRO_ERROR("Unable open %s for writing.\n", filename);
}
return retsave && retclose;
}
bool _al_osx_save_png_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
return _al_osx_save_image_f(f, ".png", bmp);
}
bool _al_osx_save_jpg_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
return _al_osx_save_image_f(f, ".jpg", bmp);
}
bool _al_osx_save_tif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
return _al_osx_save_image_f(f, ".tif", bmp);
}
bool _al_osx_save_gif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
return _al_osx_save_image_f(f, ".gif", bmp);
}
bool _al_osx_register_image_loader(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bool success = false;
int num_types;
int i;
/* Get a list of all supported image types */
NSArray *file_types = [NSImage imageFileTypes];
num_types = [file_types count];
for (i = 0; i < num_types; i++) {
NSString *str = @".";
NSString *type_str = [str stringByAppendingString: [file_types objectAtIndex: i]];
const char *s = [type_str UTF8String];
/* Skip image types Allegro supports built-in */
if (!_al_stricmp(s, ".tga") || !_al_stricmp(s, ".bmp") || !_al_stricmp(s, ".pcx")) {
continue;
}
/* Unload previous loader, if any */
al_register_bitmap_loader(s, NULL);
al_register_bitmap_loader_f(s, NULL);
ALLEGRO_DEBUG("Registering native loader for bitmap type %s\n", s);
success |= al_register_bitmap_loader(s, _al_osx_load_image);
success |= al_register_bitmap_loader_f(s, _al_osx_load_image_f);
}
char const *extensions[] = { ".tif", ".tiff", ".gif", ".png", ".jpg", ".jpeg", NULL };
for (i = 0; extensions[i]; i++) {
ALLEGRO_DEBUG("Registering native saver for bitmap type %s\n", extensions[i]);
success |= al_register_bitmap_saver(extensions[i], _al_osx_save_image);
}
success |= al_register_bitmap_saver_f(".tif", _al_osx_save_tif_f);
success |= al_register_bitmap_saver_f(".tiff", _al_osx_save_tif_f);
success |= al_register_bitmap_saver_f(".gif", _al_osx_save_gif_f);
success |= al_register_bitmap_saver_f(".png", _al_osx_save_png_f);
success |= al_register_bitmap_saver_f(".jpg", _al_osx_save_jpg_f);
success |= al_register_bitmap_saver_f(".jpeg", _al_osx_save_jpg_f);
[pool drain];
return success;
}