1 /* Image support for the NeXT/Open/GNUstep and MacOSX window system.
2 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
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)
28 /* This should be the first include, as it may set up #defines affecting
29 interpretation of even the system includes. */
34 #include "dispextern.h"
38 extern Lisp_Object QCfile, QCdata;
42 int image_trace_num = 0;
43 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
44 __FILE__, __LINE__, ++image_trace_num)
50 /* ==========================================================================
52 C interface. This allows easy calling from C files. We could just
53 compile everything as Objective-C, but that might mean slower
54 compilation and possible difficulties on some platforms..
56 ========================================================================== */
59 ns_image_from_XBM (unsigned char *bits, int width, int height)
61 NSTRACE (ns_image_from_XBM);
62 return [[EmacsImage alloc] initFromXBM: bits
63 width: width height: height
68 ns_image_for_XPM (int width, int height, int depth)
70 NSTRACE (ns_image_for_XPM);
71 return [[EmacsImage alloc] initForXPMWithDepth: depth
72 width: width height: height];
76 ns_image_from_file (Lisp_Object file)
78 NSTRACE (ns_image_from_bitmap_file);
79 return [EmacsImage allocInitFromFile: file];
83 ns_load_image (struct frame *f, struct image *img,
84 Lisp_Object spec_file, Lisp_Object spec_data)
86 EmacsImage *eImg = nil;
89 NSTRACE (ns_load_image);
91 if (STRINGP (spec_file))
93 eImg = [EmacsImage allocInitFromFile: spec_file];
95 else if (STRINGP (spec_data))
99 data = [NSData dataWithBytes: SDATA (spec_data)
100 length: SBYTES (spec_data)];
101 eImg = [[EmacsImage alloc] initWithData: data];
102 [eImg setPixmapData];
107 add_to_log ("Unable to load image %s", img->spec, Qnil);
112 img->width = size.width;
113 img->height = size.height;
115 /* 4) set img->pixmap = emacsimage */
122 ns_image_width (void *img)
124 return [(id)img size].width;
128 ns_image_height (void *img)
130 return [(id)img size].height;
134 ns_get_pixel (void *img, int x, int y)
136 return [(EmacsImage *)img getPixelAtX: x Y: y];
140 ns_put_pixel (void *img, int x, int y, unsigned long argb)
142 unsigned char alpha = (argb >> 24) & 0xFF;
145 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
146 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
150 ns_set_alpha (void *img, int x, int y, unsigned char a)
152 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
156 /* ==========================================================================
158 Class supporting bitmaps and images of various sorts.
160 ========================================================================== */
162 @implementation EmacsImage
164 static EmacsImage *ImageList = nil;
166 + allocInitFromFile: (Lisp_Object)file
168 EmacsImage *image = ImageList;
172 /* look for an existing image of the same name */
173 while (image != nil &&
174 [[image name] compare: [NSString stringWithUTF8String: SDATA (file)]]
176 image = [image imageListNext];
184 /* Search bitmap-file-path for the file, if appropriate. */
185 found = x_find_image_file (file);
186 if (!STRINGP (found))
189 image = [[EmacsImage alloc] initByReferencingFile:
190 [NSString stringWithUTF8String: SDATA (found)]];
192 imgRep = [image bestRepresentationForDevice: nil];
199 /* The next two lines cause the DPI of the image to be ignored.
200 This seems to be the behavior users expect. */
201 [image setScalesWhenResized: YES];
202 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
204 [image setName: [NSString stringWithUTF8String: SDATA (file)]];
206 ImageList = [image imageListSetNext: ImageList];
219 - imageListSetNext: (id)arg
228 return imageListNext;
242 [stippleMask release];
245 ImageList = imageListNext;
248 while (list != nil && [list imageListNext] != self)
249 list = [list imageListNext];
250 [list imageListSetNext: imageListNext];
257 - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
260 return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0];
264 - initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h
265 flip: (BOOL)flip length: (int)length;
267 int bpr = (w + 7) / 8;
268 unsigned char *planes[5];
270 [self initWithSize: NSMakeSize (w, h)];
272 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
273 pixelsWide: w pixelsHigh: h
274 bitsPerSample: 8 samplesPerPixel: 4
275 hasAlpha: YES isPlanar: YES
276 colorSpaceName: NSCalibratedRGBColorSpace
277 bytesPerRow: w bitsPerPixel: 0];
279 [bmRep getBitmapDataPlanes: planes];
281 /* pull bits out to set the (bytewise) alpha mask */
283 unsigned char *s = bits;
284 unsigned char *alpha = planes[3];
285 unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
287 unsigned char c, bitPat;
289 for (j = 0; j < h; j++)
290 for (i = 0; i < bpr; i++)
294 unsigned char s1, s2;
295 while (*s++ != 'x' && s < bits + length);
296 if (s >= bits + length)
301 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
304 c = hexchar (s1) * 0x10 + hexchar (s2);
309 bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255;
312 *alpha++ = (bitPat & 0x80) ? 0xff : 0;
318 [self addRepresentation: bmRep];
320 memset (planes[0], 0, w*h);
321 memset (planes[1], 0, w*h);
322 memset (planes[2], 0, w*h);
323 [self setXBMColor: [NSColor blackColor]];
328 /* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
329 is used as a mask, so we just memset the entire array. */
330 - setXBMColor: (NSColor *)color
332 NSSize s = [self size];
333 int len = (int) s.width * s.height;
334 unsigned char *planes[5];
338 if (bmRep == nil || color == nil)
341 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
342 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
346 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
348 [bmRep getBitmapDataPlanes: planes];
350 /* we used to just do this, but Cocoa seems to have a bug when rendering
351 an alpha-masked image onto a dark background where it bloats the mask */
352 /* memset (planes[0..2], r, g, b*0xff, len); */
354 int i, len = s.width*s.height;
355 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
356 for (i =0; i<len; i++)
357 if (planes[3][i] != 0)
369 - initForXPMWithDepth: (int)depth width: (int)width height: (int)height
371 NSSize s = {width, height};
374 [self initWithSize: s];
376 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
377 pixelsWide: width pixelsHigh: height
378 /* keep things simple for now */
379 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
380 hasAlpha: YES isPlanar: YES
381 colorSpaceName: NSCalibratedRGBColorSpace
382 bytesPerRow: width bitsPerPixel: 0];
384 [bmRep getBitmapDataPlanes: pixmapData];
386 memset (pixmapData[i], 0, width*height);
387 [self addRepresentation: bmRep];
392 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
393 - (void) setPixmapData
398 reps = [[self representations] objectEnumerator];
399 while (rep = (NSImageRep *) [reps nextObject])
401 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
403 bmRep = (NSBitmapImageRep *) rep;
404 onTiger = [bmRep respondsToSelector: @selector (colorAtX:y:)];
406 if ([bmRep numberOfPlanes] >= 3)
407 [bmRep getBitmapDataPlanes: pixmapData];
409 /* The next two lines cause the DPI of the image to be ignored.
410 This seems to be the behavior users expect. */
411 [self setScalesWhenResized: YES];
412 [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
420 /* note; this and next work only for image created with initForXPMWithDepth,
421 initFromSkipXBM, or where setPixmapData was called successfully */
423 - (unsigned long) getPixelAtX: (int)x Y: (int)y
428 /* this method is faster but won't work for bitmaps */
429 if (pixmapData[0] != NULL)
431 int loc = x + y * [self size].width;
432 return (pixmapData[3][loc] << 24) /* alpha */
433 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
434 | (pixmapData[2][loc]);
438 NSColor *color = [bmRep colorAtX: x y: y];
440 [color getRed: &r green: &g blue: &b alpha: &a];
441 return ((int)(a * 255.0) << 24)
442 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
443 | ((int)(b * 255.0));
449 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
450 green: (unsigned char)g blue: (unsigned char)b
451 alpha:(unsigned char)a;
456 if (pixmapData[0] != NULL)
458 int loc = x + y * [self size].width;
459 pixmapData[0][loc] = r;
460 pixmapData[1][loc] = g;
461 pixmapData[2][loc] = b;
462 pixmapData[3][loc] = a;
467 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
468 blue: (b/255.0) alpha: (a/255.0)]
473 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
478 if (pixmapData[0] != NULL)
480 int loc = x + y * [self size].width;
482 pixmapData[3][loc] = a;
486 NSColor *color = [bmRep colorAtX: x y: y];
487 color = [color colorWithAlphaComponent: (a / 255.0)];
488 [bmRep setColor: color atX: x y: y];
492 /* returns a pattern color, which is cached here */
493 - (NSColor *)stippleMask
495 if (stippleMask == nil)
496 stippleMask = [[NSColor colorWithPatternImage: self] retain];