]> 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 unsigned long fg, unsigned long bg)
58 {
59 NSTRACE (ns_image_from_XBM);
60 return [[EmacsImage alloc] initFromXBM: bits
61 width: width height: height
62 fg: fg bg: bg];
63 }
64
65 void *
66 ns_image_for_XPM (int width, int height, int depth)
67 {
68 NSTRACE (ns_image_for_XPM);
69 return [[EmacsImage alloc] initForXPMWithDepth: depth
70 width: width height: height];
71 }
72
73 void *
74 ns_image_from_file (Lisp_Object file)
75 {
76 NSTRACE (ns_image_from_bitmap_file);
77 return [EmacsImage allocInitFromFile: file];
78 }
79
80 bool
81 ns_load_image (struct frame *f, struct image *img,
82 Lisp_Object spec_file, Lisp_Object spec_data)
83 {
84 EmacsImage *eImg = nil;
85 NSSize size;
86
87 NSTRACE (ns_load_image);
88
89 if (STRINGP (spec_file))
90 {
91 eImg = [EmacsImage allocInitFromFile: spec_file];
92 }
93 else if (STRINGP (spec_data))
94 {
95 NSData *data;
96
97 data = [NSData dataWithBytes: SSDATA (spec_data)
98 length: SBYTES (spec_data)];
99 eImg = [[EmacsImage alloc] initWithData: data];
100 [eImg setPixmapData];
101 }
102
103 if (eImg == nil)
104 {
105 add_to_log ("Unable to load image %s", img->spec, Qnil);
106 return 0;
107 }
108
109 size = [eImg size];
110 img->width = size.width;
111 img->height = size.height;
112
113 /* 4) set img->pixmap = emacsimage */
114 img->pixmap = eImg;
115 return 1;
116 }
117
118
119 int
120 ns_image_width (void *img)
121 {
122 return [(id)img size].width;
123 }
124
125 int
126 ns_image_height (void *img)
127 {
128 return [(id)img size].height;
129 }
130
131 unsigned long
132 ns_get_pixel (void *img, int x, int y)
133 {
134 return [(EmacsImage *)img getPixelAtX: x Y: y];
135 }
136
137 void
138 ns_put_pixel (void *img, int x, int y, unsigned long argb)
139 {
140 unsigned char alpha = (argb >> 24) & 0xFF;
141 if (alpha == 0)
142 alpha = 0xFF;
143 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
144 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
145 }
146
147 void
148 ns_set_alpha (void *img, int x, int y, unsigned char a)
149 {
150 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
151 }
152
153
154 /* ==========================================================================
155
156 Class supporting bitmaps and images of various sorts.
157
158 ========================================================================== */
159
160 @implementation EmacsImage
161
162 + allocInitFromFile: (Lisp_Object)file
163 {
164 NSImageRep *imgRep;
165 Lisp_Object found;
166 EmacsImage *image;
167
168 /* Search bitmap-file-path for the file, if appropriate. */
169 found = x_find_image_file (file);
170 if (!STRINGP (found))
171 return nil;
172
173 image = [[EmacsImage alloc] initByReferencingFile:
174 [NSString stringWithUTF8String: SSDATA (found)]];
175
176 image->bmRep = nil;
177 #ifdef NS_IMPL_COCOA
178 imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
179 #else
180 imgRep = [image bestRepresentationForDevice: nil];
181 #endif
182 if (imgRep == nil)
183 {
184 [image release];
185 return nil;
186 }
187
188 /* The next two lines cause the DPI of the image to be ignored.
189 This seems to be the behavior users expect. */
190 #ifdef NS_IMPL_COCOA
191 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
192 [image setScalesWhenResized: YES];
193 #endif
194 #endif
195 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
196
197 [image setName: [NSString stringWithUTF8String: SSDATA (file)]];
198
199 return image;
200 }
201
202
203 - (void)dealloc
204 {
205 [stippleMask release];
206 [bmRep release];
207 [super dealloc];
208 }
209
210
211 - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
212 fg: (unsigned long)fg bg: (unsigned long)bg
213 {
214 unsigned char *planes[5];
215
216 [self initWithSize: NSMakeSize (w, h)];
217
218 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
219 pixelsWide: w pixelsHigh: h
220 bitsPerSample: 8 samplesPerPixel: 4
221 hasAlpha: YES isPlanar: YES
222 colorSpaceName: NSCalibratedRGBColorSpace
223 bytesPerRow: w bitsPerPixel: 0];
224
225 [bmRep getBitmapDataPlanes: planes];
226
227 if (fg == 0 && bg == 0)
228 bg = 0xffffff;
229
230 {
231 /* pull bits out to set the (bytewise) alpha mask */
232 int i, j, k;
233 unsigned char *s = bits;
234 unsigned char *rr = planes[0];
235 unsigned char *gg = planes[1];
236 unsigned char *bb = planes[2];
237 unsigned char *alpha = planes[3];
238 unsigned char fgr = (fg >> 16) & 0xff;
239 unsigned char fgg = (fg >> 8) & 0xff;
240 unsigned char fgb = fg & 0xff;
241 unsigned char bgr = (bg >> 16) & 0xff;
242 unsigned char bgg = (bg >> 8) & 0xff;
243 unsigned char bgb = bg & 0xff;
244 unsigned char c;
245
246 int idx = 0;
247 for (j = 0; j < h; ++j)
248 for (i = 0; i < w; )
249 {
250 c = *s++;
251 for (k = 0; i < w && k < 8; ++k, ++i)
252 {
253 *alpha++ = 0xff;
254 if (c & 1)
255 {
256 *rr++ = fgr;
257 *gg++ = fgg;
258 *bb++ = fgb;
259 }
260 else
261 {
262 *rr++ = bgr;
263 *gg++ = bgg;
264 *bb++ = bgb;
265 }
266 idx++;
267 c >>= 1;
268 }
269 }
270 }
271
272 xbm_fg = fg;
273 [self addRepresentation: bmRep];
274 return self;
275 }
276
277 /* Set color for a bitmap image. */
278 - setXBMColor: (NSColor *)color
279 {
280 NSSize s = [self size];
281 unsigned char *planes[5];
282 EmacsCGFloat r, g, b, a;
283 NSColor *rgbColor;
284
285 if (bmRep == nil || color == nil)
286 return self;
287
288 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
289 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
290 else
291 rgbColor = color;
292
293 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
294
295 [bmRep getBitmapDataPlanes: planes];
296
297 {
298 int i, len = s.width*s.height;
299 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
300 unsigned char fgr = (xbm_fg >> 16) & 0xff;
301 unsigned char fgg = (xbm_fg >> 8) & 0xff;
302 unsigned char fgb = xbm_fg & 0xff;
303
304 for (i = 0; i < len; ++i)
305 if (planes[0][i] == fgr && planes[1][i] == fgg && planes[2][i] == fgb)
306 {
307 planes[0][i] = rr;
308 planes[1][i] = gg;
309 planes[2][i] = bb;
310 }
311 xbm_fg = ((rr << 16) & 0xff) + ((gg << 8) & 0xff) + (bb & 0xff);
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 #ifdef NS_IMPL_COCOA
360 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
361 [self setScalesWhenResized: YES];
362 #endif
363 #endif
364 [self setSize: NSMakeSize([bmr pixelsWide], [bmr pixelsHigh])];
365
366 break;
367 }
368 }
369 }
370
371
372 /* note; this and next work only for image created with initForXPMWithDepth,
373 initFromSkipXBM, or where setPixmapData was called successfully */
374 /* return ARGB */
375 - (unsigned long) getPixelAtX: (int)x Y: (int)y
376 {
377 if (bmRep == nil)
378 return 0;
379
380 /* this method is faster but won't work for bitmaps */
381 if (pixmapData[0] != NULL)
382 {
383 int loc = x + y * [self size].width;
384 return (pixmapData[3][loc] << 24) /* alpha */
385 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
386 | (pixmapData[2][loc]);
387 }
388 else
389 {
390 NSColor *color = [bmRep colorAtX: x y: y];
391 EmacsCGFloat r, g, b, a;
392 [color getRed: &r green: &g blue: &b alpha: &a];
393 return ((int)(a * 255.0) << 24)
394 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
395 | ((int)(b * 255.0));
396
397 }
398 }
399
400 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
401 green: (unsigned char)g blue: (unsigned char)b
402 alpha:(unsigned char)a;
403 {
404 if (bmRep == nil)
405 return;
406
407 if (pixmapData[0] != NULL)
408 {
409 int loc = x + y * [self size].width;
410 pixmapData[0][loc] = r;
411 pixmapData[1][loc] = g;
412 pixmapData[2][loc] = b;
413 pixmapData[3][loc] = a;
414 }
415 else
416 {
417 [bmRep setColor:
418 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
419 blue: (b/255.0) alpha: (a/255.0)]
420 atX: x y: y];
421 }
422 }
423
424 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
425 {
426 if (bmRep == nil)
427 return;
428
429 if (pixmapData[0] != NULL)
430 {
431 int loc = x + y * [self size].width;
432
433 pixmapData[3][loc] = a;
434 }
435 else
436 {
437 NSColor *color = [bmRep colorAtX: x y: y];
438 color = [color colorWithAlphaComponent: (a / 255.0)];
439 [bmRep setColor: color atX: x y: y];
440 }
441 }
442
443 /* returns a pattern color, which is cached here */
444 - (NSColor *)stippleMask
445 {
446 if (stippleMask == nil)
447 stippleMask = [[NSColor colorWithPatternImage: self] retain];
448 return stippleMask;
449 }
450
451 @end