]> code.delx.au - gnu-emacs/blob - src/nsimage.m
9eaeefebcd87cdb63b11b9b28c5007cc57a20a40
[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 #include "coding.h"
37
38 /* call tracing */
39 #if 0
40 int image_trace_num = 0;
41 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
42 __FILE__, __LINE__, ++image_trace_num)
43 #else
44 #define NSTRACE(x)
45 #endif
46
47
48 /* ==========================================================================
49
50 C interface. This allows easy calling from C files. We could just
51 compile everything as Objective-C, but that might mean slower
52 compilation and possible difficulties on some platforms..
53
54 ========================================================================== */
55
56 void *
57 ns_image_from_XBM (unsigned char *bits, int width, int height,
58 unsigned long fg, unsigned long bg)
59 {
60 NSTRACE (ns_image_from_XBM);
61 return [[EmacsImage alloc] initFromXBM: bits
62 width: width height: height
63 fg: fg bg: bg];
64 }
65
66 void *
67 ns_image_for_XPM (int width, int height, int depth)
68 {
69 NSTRACE (ns_image_for_XPM);
70 return [[EmacsImage alloc] initForXPMWithDepth: depth
71 width: width height: height];
72 }
73
74 void *
75 ns_image_from_file (Lisp_Object file)
76 {
77 NSTRACE (ns_image_from_bitmap_file);
78 return [EmacsImage allocInitFromFile: file];
79 }
80
81 bool
82 ns_load_image (struct frame *f, struct image *img,
83 Lisp_Object spec_file, Lisp_Object spec_data)
84 {
85 EmacsImage *eImg = nil;
86 NSSize size;
87
88 NSTRACE (ns_load_image);
89
90 if (STRINGP (spec_file))
91 {
92 eImg = [EmacsImage allocInitFromFile: spec_file];
93 }
94 else if (STRINGP (spec_data))
95 {
96 NSData *data;
97
98 data = [NSData dataWithBytes: SSDATA (spec_data)
99 length: SBYTES (spec_data)];
100 eImg = [[EmacsImage alloc] initWithData: data];
101 [eImg setPixmapData];
102 }
103
104 if (eImg == nil)
105 {
106 add_to_log ("Unable to load image %s", img->spec);
107 return 0;
108 }
109
110 size = [eImg size];
111 img->width = size.width;
112 img->height = size.height;
113
114 /* 4) set img->pixmap = emacsimage */
115 img->pixmap = eImg;
116 return 1;
117 }
118
119
120 int
121 ns_image_width (void *img)
122 {
123 return [(id)img size].width;
124 }
125
126 int
127 ns_image_height (void *img)
128 {
129 return [(id)img size].height;
130 }
131
132 unsigned long
133 ns_get_pixel (void *img, int x, int y)
134 {
135 return [(EmacsImage *)img getPixelAtX: x Y: y];
136 }
137
138 void
139 ns_put_pixel (void *img, int x, int y, unsigned long argb)
140 {
141 unsigned char alpha = (argb >> 24) & 0xFF;
142 if (alpha == 0)
143 alpha = 0xFF;
144 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
145 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
146 }
147
148 void
149 ns_set_alpha (void *img, int x, int y, unsigned char a)
150 {
151 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
152 }
153
154
155 /* ==========================================================================
156
157 Class supporting bitmaps and images of various sorts.
158
159 ========================================================================== */
160
161 @implementation EmacsImage
162
163 + allocInitFromFile: (Lisp_Object)file
164 {
165 NSImageRep *imgRep;
166 Lisp_Object found;
167 EmacsImage *image;
168
169 /* Search bitmap-file-path for the file, if appropriate. */
170 found = x_find_image_file (file);
171 if (!STRINGP (found))
172 return nil;
173 found = ENCODE_FILE (found);
174
175 image = [[EmacsImage alloc] initByReferencingFile:
176 [NSString stringWithUTF8String: SSDATA (found)]];
177
178 image->bmRep = nil;
179 #ifdef NS_IMPL_COCOA
180 imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
181 #else
182 imgRep = [image bestRepresentationForDevice: nil];
183 #endif
184 if (imgRep == nil)
185 {
186 [image release];
187 return nil;
188 }
189
190 /* The next two lines cause the DPI of the image to be ignored.
191 This seems to be the behavior users expect. */
192 #ifdef NS_IMPL_COCOA
193 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
194 [image setScalesWhenResized: YES];
195 #endif
196 #endif
197 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
198
199 [image setName: [NSString stringWithUTF8String: SSDATA (file)]];
200
201 return image;
202 }
203
204
205 - (void)dealloc
206 {
207 [stippleMask release];
208 [bmRep release];
209 [super dealloc];
210 }
211
212
213 - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
214 fg: (unsigned long)fg bg: (unsigned long)bg
215 {
216 unsigned char *planes[5];
217
218 [self initWithSize: NSMakeSize (w, h)];
219
220 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
221 pixelsWide: w pixelsHigh: h
222 bitsPerSample: 8 samplesPerPixel: 4
223 hasAlpha: YES isPlanar: YES
224 colorSpaceName: NSCalibratedRGBColorSpace
225 bytesPerRow: w bitsPerPixel: 0];
226
227 [bmRep getBitmapDataPlanes: planes];
228
229 if (fg == 0 && bg == 0)
230 bg = 0xffffff;
231
232 {
233 /* pull bits out to set the (bytewise) alpha mask */
234 int i, j, k;
235 unsigned char *s = bits;
236 unsigned char *rr = planes[0];
237 unsigned char *gg = planes[1];
238 unsigned char *bb = planes[2];
239 unsigned char *alpha = planes[3];
240 unsigned char fgr = (fg >> 16) & 0xff;
241 unsigned char fgg = (fg >> 8) & 0xff;
242 unsigned char fgb = fg & 0xff;
243 unsigned char bgr = (bg >> 16) & 0xff;
244 unsigned char bgg = (bg >> 8) & 0xff;
245 unsigned char bgb = bg & 0xff;
246 unsigned char c;
247
248 int idx = 0;
249 for (j = 0; j < h; ++j)
250 for (i = 0; i < w; )
251 {
252 c = *s++;
253 for (k = 0; i < w && k < 8; ++k, ++i)
254 {
255 *alpha++ = 0xff;
256 if (c & 1)
257 {
258 *rr++ = fgr;
259 *gg++ = fgg;
260 *bb++ = fgb;
261 }
262 else
263 {
264 *rr++ = bgr;
265 *gg++ = bgg;
266 *bb++ = bgb;
267 }
268 idx++;
269 c >>= 1;
270 }
271 }
272 }
273
274 xbm_fg = fg;
275 [self addRepresentation: bmRep];
276 return self;
277 }
278
279 /* Set color for a bitmap image. */
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 {
300 int i, len = s.width*s.height;
301 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
302 unsigned char fgr = (xbm_fg >> 16) & 0xff;
303 unsigned char fgg = (xbm_fg >> 8) & 0xff;
304 unsigned char fgb = xbm_fg & 0xff;
305
306 for (i = 0; i < len; ++i)
307 if (planes[0][i] == fgr && planes[1][i] == fgg && planes[2][i] == fgb)
308 {
309 planes[0][i] = rr;
310 planes[1][i] = gg;
311 planes[2][i] = bb;
312 }
313 xbm_fg = ((rr << 16) & 0xff) + ((gg << 8) & 0xff) + (bb & 0xff);
314 }
315
316 return self;
317 }
318
319
320 - initForXPMWithDepth: (int)depth width: (int)width height: (int)height
321 {
322 NSSize s = {width, height};
323 int i;
324
325 [self initWithSize: s];
326
327 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
328 pixelsWide: width pixelsHigh: height
329 /* keep things simple for now */
330 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
331 hasAlpha: YES isPlanar: YES
332 colorSpaceName: NSCalibratedRGBColorSpace
333 bytesPerRow: width bitsPerPixel: 0];
334
335 [bmRep getBitmapDataPlanes: pixmapData];
336 for (i =0; i<4; i++)
337 memset (pixmapData[i], 0, width*height);
338 [self addRepresentation: bmRep];
339 return self;
340 }
341
342
343 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
344 - (void) setPixmapData
345 {
346 NSEnumerator *reps;
347 NSImageRep *rep;
348
349 reps = [[self representations] objectEnumerator];
350 while ((rep = (NSImageRep *) [reps nextObject]))
351 {
352 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
353 {
354 NSBitmapImageRep *bmr = (NSBitmapImageRep *) rep;
355
356 if ([bmr numberOfPlanes] >= 3)
357 [bmr getBitmapDataPlanes: pixmapData];
358
359 /* The next two lines cause the DPI of the image to be ignored.
360 This seems to be the behavior users expect. */
361 #ifdef NS_IMPL_COCOA
362 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
363 [self setScalesWhenResized: YES];
364 #endif
365 #endif
366 [self setSize: NSMakeSize([bmr pixelsWide], [bmr pixelsHigh])];
367
368 break;
369 }
370 }
371 }
372
373
374 /* note; this and next work only for image created with initForXPMWithDepth,
375 initFromSkipXBM, or where setPixmapData was called successfully */
376 /* return ARGB */
377 - (unsigned long) getPixelAtX: (int)x Y: (int)y
378 {
379 if (bmRep == nil)
380 return 0;
381
382 /* this method is faster but won't work for bitmaps */
383 if (pixmapData[0] != NULL)
384 {
385 int loc = x + y * [self size].width;
386 return (pixmapData[3][loc] << 24) /* alpha */
387 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
388 | (pixmapData[2][loc]);
389 }
390 else
391 {
392 NSColor *color = [bmRep colorAtX: x y: y];
393 EmacsCGFloat r, g, b, a;
394 [color getRed: &r green: &g blue: &b alpha: &a];
395 return ((int)(a * 255.0) << 24)
396 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
397 | ((int)(b * 255.0));
398
399 }
400 }
401
402 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
403 green: (unsigned char)g blue: (unsigned char)b
404 alpha:(unsigned char)a;
405 {
406 if (bmRep == nil)
407 return;
408
409 if (pixmapData[0] != NULL)
410 {
411 int loc = x + y * [self size].width;
412 pixmapData[0][loc] = r;
413 pixmapData[1][loc] = g;
414 pixmapData[2][loc] = b;
415 pixmapData[3][loc] = a;
416 }
417 else
418 {
419 [bmRep setColor:
420 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
421 blue: (b/255.0) alpha: (a/255.0)]
422 atX: x y: y];
423 }
424 }
425
426 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
427 {
428 if (bmRep == nil)
429 return;
430
431 if (pixmapData[0] != NULL)
432 {
433 int loc = x + y * [self size].width;
434
435 pixmapData[3][loc] = a;
436 }
437 else
438 {
439 NSColor *color = [bmRep colorAtX: x y: y];
440 color = [color colorWithAlphaComponent: (a / 255.0)];
441 [bmRep setColor: color atX: x y: y];
442 }
443 }
444
445 /* returns a pattern color, which is cached here */
446 - (NSColor *)stippleMask
447 {
448 if (stippleMask == nil)
449 stippleMask = [[NSColor colorWithPatternImage: self] retain];
450 return stippleMask;
451 }
452
453 @end