]> code.delx.au - gnu-emacs/blob - src/nsimage.m
Merge from origin/emacs-24
[gnu-emacs] / src / nsimage.m
1 /* Image support for the NeXT/Open/GNUstep and MacOSX window system.
2 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2015 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /*
21 Originally by Carl Edman
22 Updated by Christian Limpach (chris@nice.ch)
23 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
24 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
25 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
26 */
27
28 /* This should be the first include, as it may set up #defines affecting
29 interpretation of even the system includes. */
30 #include <config.h>
31
32 #include "lisp.h"
33 #include "dispextern.h"
34 #include "nsterm.h"
35 #include "frame.h"
36
37 /* call tracing */
38 #if 0
39 int image_trace_num = 0;
40 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
41 __FILE__, __LINE__, ++image_trace_num)
42 #else
43 #define NSTRACE(x)
44 #endif
45
46
47 /* ==========================================================================
48
49 C interface. This allows easy calling from C files. We could just
50 compile everything as Objective-C, but that might mean slower
51 compilation and possible difficulties on some platforms..
52
53 ========================================================================== */
54
55 void *
56 ns_image_from_XBM (unsigned char *bits, int width, int height)
57 {
58 NSTRACE (ns_image_from_XBM);
59 return [[EmacsImage alloc] initFromXBM: bits
60 width: width height: height
61 flip: YES];
62 }
63
64 void *
65 ns_image_for_XPM (int width, int height, int depth)
66 {
67 NSTRACE (ns_image_for_XPM);
68 return [[EmacsImage alloc] initForXPMWithDepth: depth
69 width: width height: height];
70 }
71
72 void *
73 ns_image_from_file (Lisp_Object file)
74 {
75 NSTRACE (ns_image_from_bitmap_file);
76 return [EmacsImage allocInitFromFile: file];
77 }
78
79 bool
80 ns_load_image (struct frame *f, struct image *img,
81 Lisp_Object spec_file, Lisp_Object spec_data)
82 {
83 EmacsImage *eImg = nil;
84 NSSize size;
85
86 NSTRACE (ns_load_image);
87
88 if (STRINGP (spec_file))
89 {
90 eImg = [EmacsImage allocInitFromFile: spec_file];
91 }
92 else if (STRINGP (spec_data))
93 {
94 NSData *data;
95
96 data = [NSData dataWithBytes: SSDATA (spec_data)
97 length: SBYTES (spec_data)];
98 eImg = [[EmacsImage alloc] initWithData: data];
99 [eImg setPixmapData];
100 }
101
102 if (eImg == nil)
103 {
104 add_to_log ("Unable to load image %s", img->spec, Qnil);
105 return 0;
106 }
107
108 size = [eImg size];
109 img->width = size.width;
110 img->height = size.height;
111
112 /* 4) set img->pixmap = emacsimage */
113 img->pixmap = eImg;
114 return 1;
115 }
116
117
118 int
119 ns_image_width (void *img)
120 {
121 return [(id)img size].width;
122 }
123
124 int
125 ns_image_height (void *img)
126 {
127 return [(id)img size].height;
128 }
129
130 unsigned long
131 ns_get_pixel (void *img, int x, int y)
132 {
133 return [(EmacsImage *)img getPixelAtX: x Y: y];
134 }
135
136 void
137 ns_put_pixel (void *img, int x, int y, unsigned long argb)
138 {
139 unsigned char alpha = (argb >> 24) & 0xFF;
140 if (alpha == 0)
141 alpha = 0xFF;
142 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
143 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
144 }
145
146 void
147 ns_set_alpha (void *img, int x, int y, unsigned char a)
148 {
149 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
150 }
151
152
153 /* ==========================================================================
154
155 Class supporting bitmaps and images of various sorts.
156
157 ========================================================================== */
158
159 @implementation EmacsImage
160
161 + allocInitFromFile: (Lisp_Object)file
162 {
163 NSImageRep *imgRep;
164 Lisp_Object found;
165 EmacsImage *image;
166
167 /* Search bitmap-file-path for the file, if appropriate. */
168 found = x_find_image_file (file);
169 if (!STRINGP (found))
170 return nil;
171
172 image = [[EmacsImage alloc] initByReferencingFile:
173 [NSString stringWithUTF8String: SSDATA (found)]];
174
175 image->bmRep = nil;
176 #ifdef NS_IMPL_COCOA
177 imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
178 #else
179 imgRep = [image bestRepresentationForDevice: nil];
180 #endif
181 if (imgRep == nil)
182 {
183 [image release];
184 return nil;
185 }
186
187 /* The next two lines cause the DPI of the image to be ignored.
188 This seems to be the behavior users expect. */
189 [image setScalesWhenResized: YES];
190 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
191
192 [image setName: [NSString stringWithUTF8String: SSDATA (file)]];
193
194 return image;
195 }
196
197
198 - (void)dealloc
199 {
200 [stippleMask release];
201 [bmRep release];
202 [super dealloc];
203 }
204
205
206 - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
207 flip: (BOOL)flip
208 {
209 return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0];
210 }
211
212
213 - initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h
214 flip: (BOOL)flip length: (int)length;
215 {
216 int bpr = (w + 7) / 8;
217 unsigned char *planes[5];
218
219 [self initWithSize: NSMakeSize (w, h)];
220
221 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
222 pixelsWide: w pixelsHigh: h
223 bitsPerSample: 8 samplesPerPixel: 4
224 hasAlpha: YES isPlanar: YES
225 colorSpaceName: NSCalibratedRGBColorSpace
226 bytesPerRow: w bitsPerPixel: 0];
227
228 [bmRep getBitmapDataPlanes: planes];
229 {
230 /* pull bits out to set the (bytewise) alpha mask */
231 int i, j, k;
232 unsigned char *s = bits;
233 unsigned char *alpha = planes[3];
234 unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
235 3, 11, 7, 15};
236 unsigned char c, bitPat;
237
238 for (j = 0; j < h; j++)
239 for (i = 0; i < bpr; i++)
240 {
241 if (length)
242 {
243 unsigned char s1, s2;
244 while (*s++ != 'x' && s < bits + length);
245 if (s >= bits + length)
246 {
247 [bmRep release];
248 bmRep = nil;
249 return nil;
250 }
251 #define hexchar(x) ('0' <= (x) && (x) <= '9' ? (x) - '0' : (x) - 'a' + 10)
252 s1 = *s++;
253 s2 = *s++;
254 c = hexchar (s1) * 0x10 + hexchar (s2);
255 }
256 else
257 c = *s++;
258
259 bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255;
260 for (k =0; k<8; k++)
261 {
262 *alpha++ = (bitPat & 0x80) ? 0xff : 0;
263 bitPat <<= 1;
264 }
265 }
266 }
267
268 [self addRepresentation: bmRep];
269
270 memset (planes[0], 0, w*h);
271 memset (planes[1], 0, w*h);
272 memset (planes[2], 0, w*h);
273 [self setXBMColor: [NSColor blackColor]];
274 return self;
275 }
276
277
278 /* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
279 is used as a mask, so we just memset the entire array. */
280 - setXBMColor: (NSColor *)color
281 {
282 NSSize s = [self size];
283 unsigned char *planes[5];
284 EmacsCGFloat r, g, b, a;
285 NSColor *rgbColor;
286
287 if (bmRep == nil || color == nil)
288 return self;
289
290 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
291 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
292 else
293 rgbColor = color;
294
295 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
296
297 [bmRep getBitmapDataPlanes: planes];
298
299 /* we used to just do this, but Cocoa seems to have a bug when rendering
300 an alpha-masked image onto a dark background where it bloats the mask */
301 /* memset (planes[0..2], r, g, b*0xff, len); */
302 {
303 int i, len = s.width*s.height;
304 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
305 for (i =0; i<len; i++)
306 if (planes[3][i] != 0)
307 {
308 planes[0][i] = rr;
309 planes[1][i] = gg;
310 planes[2][i] = bb;
311 }
312 }
313
314 return self;
315 }
316
317
318 - initForXPMWithDepth: (int)depth width: (int)width height: (int)height
319 {
320 NSSize s = {width, height};
321 int i;
322
323 [self initWithSize: s];
324
325 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
326 pixelsWide: width pixelsHigh: height
327 /* keep things simple for now */
328 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
329 hasAlpha: YES isPlanar: YES
330 colorSpaceName: NSCalibratedRGBColorSpace
331 bytesPerRow: width bitsPerPixel: 0];
332
333 [bmRep getBitmapDataPlanes: pixmapData];
334 for (i =0; i<4; i++)
335 memset (pixmapData[i], 0, width*height);
336 [self addRepresentation: bmRep];
337 return self;
338 }
339
340
341 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
342 - (void) setPixmapData
343 {
344 NSEnumerator *reps;
345 NSImageRep *rep;
346
347 reps = [[self representations] objectEnumerator];
348 while ((rep = (NSImageRep *) [reps nextObject]))
349 {
350 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
351 {
352 NSBitmapImageRep *bmr = (NSBitmapImageRep *) rep;
353
354 if ([bmr numberOfPlanes] >= 3)
355 [bmr getBitmapDataPlanes: pixmapData];
356
357 /* The next two lines cause the DPI of the image to be ignored.
358 This seems to be the behavior users expect. */
359 [self setScalesWhenResized: YES];
360 [self setSize: NSMakeSize([bmr pixelsWide], [bmr pixelsHigh])];
361
362 break;
363 }
364 }
365 }
366
367
368 /* note; this and next work only for image created with initForXPMWithDepth,
369 initFromSkipXBM, or where setPixmapData was called successfully */
370 /* return ARGB */
371 - (unsigned long) getPixelAtX: (int)x Y: (int)y
372 {
373 if (bmRep == nil)
374 return 0;
375
376 /* this method is faster but won't work for bitmaps */
377 if (pixmapData[0] != NULL)
378 {
379 int loc = x + y * [self size].width;
380 return (pixmapData[3][loc] << 24) /* alpha */
381 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
382 | (pixmapData[2][loc]);
383 }
384 else
385 {
386 NSColor *color = [bmRep colorAtX: x y: y];
387 EmacsCGFloat r, g, b, a;
388 [color getRed: &r green: &g blue: &b alpha: &a];
389 return ((int)(a * 255.0) << 24)
390 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
391 | ((int)(b * 255.0));
392
393 }
394 }
395
396 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
397 green: (unsigned char)g blue: (unsigned char)b
398 alpha:(unsigned char)a;
399 {
400 if (bmRep == nil)
401 return;
402
403 if (pixmapData[0] != NULL)
404 {
405 int loc = x + y * [self size].width;
406 pixmapData[0][loc] = r;
407 pixmapData[1][loc] = g;
408 pixmapData[2][loc] = b;
409 pixmapData[3][loc] = a;
410 }
411 else
412 {
413 [bmRep setColor:
414 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
415 blue: (b/255.0) alpha: (a/255.0)]
416 atX: x y: y];
417 }
418 }
419
420 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
421 {
422 if (bmRep == nil)
423 return;
424
425 if (pixmapData[0] != NULL)
426 {
427 int loc = x + y * [self size].width;
428
429 pixmapData[3][loc] = a;
430 }
431 else
432 {
433 NSColor *color = [bmRep colorAtX: x y: y];
434 color = [color colorWithAlphaComponent: (a / 255.0)];
435 [bmRep setColor: color atX: x y: y];
436 }
437 }
438
439 /* returns a pattern color, which is cached here */
440 - (NSColor *)stippleMask
441 {
442 if (stippleMask == nil)
443 stippleMask = [[NSColor colorWithPatternImage: self] retain];
444 return stippleMask;
445 }
446
447 @end