1 /* Image support for the NeXT/Open/GNUstep and macOS window system.
2 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2017 Free Software
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 (at
10 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 macOS/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. */
33 #include "dispextern.h"
40 /* ==========================================================================
42 C interface. This allows easy calling from C files. We could just
43 compile everything as Objective-C, but that might mean slower
44 compilation and possible difficulties on some platforms..
46 ========================================================================== */
49 ns_image_from_XBM (char *bits, int width, int height,
50 unsigned long fg, unsigned long bg)
52 NSTRACE ("ns_image_from_XBM");
53 return [[EmacsImage alloc] initFromXBM: (unsigned char *) bits
54 width: width height: height
59 ns_image_for_XPM (int width, int height, int depth)
61 NSTRACE ("ns_image_for_XPM");
62 return [[EmacsImage alloc] initForXPMWithDepth: depth
63 width: width height: height];
67 ns_image_from_file (Lisp_Object file)
69 NSTRACE ("ns_image_from_file");
70 return [EmacsImage allocInitFromFile: file];
74 ns_load_image (struct frame *f, struct image *img,
75 Lisp_Object spec_file, Lisp_Object spec_data)
77 EmacsImage *eImg = nil;
80 NSTRACE ("ns_load_image");
82 if (STRINGP (spec_file))
84 eImg = [EmacsImage allocInitFromFile: spec_file];
86 else if (STRINGP (spec_data))
90 data = [NSData dataWithBytes: SSDATA (spec_data)
91 length: SBYTES (spec_data)];
92 eImg = [[EmacsImage alloc] initWithData: data];
98 add_to_log ("Unable to load image %s", img->spec);
103 img->width = size.width;
104 img->height = size.height;
106 /* 4) set img->pixmap = emacsimage */
113 ns_image_width (void *img)
115 return [(id)img size].width;
119 ns_image_height (void *img)
121 return [(id)img size].height;
125 ns_get_pixel (void *img, int x, int y)
127 return [(EmacsImage *)img getPixelAtX: x Y: y];
131 ns_put_pixel (void *img, int x, int y, unsigned long argb)
133 unsigned char alpha = (argb >> 24) & 0xFF;
136 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
137 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
141 ns_set_alpha (void *img, int x, int y, unsigned char a)
143 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
147 /* ==========================================================================
149 Class supporting bitmaps and images of various sorts.
151 ========================================================================== */
153 @implementation EmacsImage
155 + (instancetype)allocInitFromFile: (Lisp_Object)file
161 /* Search bitmap-file-path for the file, if appropriate. */
162 found = x_find_image_file (file);
163 if (!STRINGP (found))
165 found = ENCODE_FILE (found);
167 image = [[EmacsImage alloc] initByReferencingFile:
168 [NSString stringWithUTF8String: SSDATA (found)]];
172 imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
174 imgRep = [image bestRepresentationForDevice: nil];
182 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
184 [image setName: [NSString stringWithUTF8String: SSDATA (file)]];
192 [stippleMask release];
198 /* Create image from monochrome bitmap. If both FG and BG are 0
199 (black), set the background to white and make it transparent. */
200 - (instancetype)initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
201 fg: (unsigned long)fg bg: (unsigned long)bg
203 unsigned char *planes[5];
204 unsigned char bg_alpha = 0xff;
206 [self initWithSize: NSMakeSize (w, h)];
208 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
209 pixelsWide: w pixelsHigh: h
210 bitsPerSample: 8 samplesPerPixel: 4
211 hasAlpha: YES isPlanar: YES
212 colorSpaceName: NSCalibratedRGBColorSpace
213 bytesPerRow: w bitsPerPixel: 0];
215 [bmRep getBitmapDataPlanes: planes];
217 if (fg == 0 && bg == 0)
224 /* pull bits out to set the (bytewise) alpha mask */
226 unsigned char *s = bits;
227 unsigned char *rr = planes[0];
228 unsigned char *gg = planes[1];
229 unsigned char *bb = planes[2];
230 unsigned char *alpha = planes[3];
231 unsigned char fgr = (fg >> 16) & 0xff;
232 unsigned char fgg = (fg >> 8) & 0xff;
233 unsigned char fgb = fg & 0xff;
234 unsigned char bgr = (bg >> 16) & 0xff;
235 unsigned char bgg = (bg >> 8) & 0xff;
236 unsigned char bgb = bg & 0xff;
240 for (j = 0; j < h; ++j)
244 for (k = 0; i < w && k < 8; ++k, ++i)
267 [self addRepresentation: bmRep];
271 /* Set color for a bitmap image. */
272 - (instancetype)setXBMColor: (NSColor *)color
274 NSSize s = [self size];
275 unsigned char *planes[5];
276 EmacsCGFloat r, g, b, a;
279 if (bmRep == nil || color == nil)
282 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
283 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
287 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
289 [bmRep getBitmapDataPlanes: planes];
292 int i, len = s.width*s.height;
293 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
294 unsigned char fgr = (xbm_fg >> 16) & 0xff;
295 unsigned char fgg = (xbm_fg >> 8) & 0xff;
296 unsigned char fgb = xbm_fg & 0xff;
298 for (i = 0; i < len; ++i)
299 if (planes[0][i] == fgr && planes[1][i] == fgg && planes[2][i] == fgb)
305 xbm_fg = ((rr << 16) & 0xff0000) + ((gg << 8) & 0xff00) + (bb & 0xff);
312 - (instancetype)initForXPMWithDepth: (int)depth width: (int)width height: (int)height
314 NSSize s = {width, height};
317 [self initWithSize: s];
319 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
320 pixelsWide: width pixelsHigh: height
321 /* keep things simple for now */
322 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
323 hasAlpha: YES isPlanar: YES
324 colorSpaceName: NSCalibratedRGBColorSpace
325 bytesPerRow: width bitsPerPixel: 0];
327 [bmRep getBitmapDataPlanes: pixmapData];
329 memset (pixmapData[i], 0, width*height);
330 [self addRepresentation: bmRep];
335 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
336 - (void) setPixmapData
341 reps = [[self representations] objectEnumerator];
342 while ((rep = (NSImageRep *) [reps nextObject]))
344 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
346 NSBitmapImageRep *bmr = (NSBitmapImageRep *) rep;
348 if ([bmr numberOfPlanes] >= 3)
349 [bmr getBitmapDataPlanes: pixmapData];
351 [self setSize: NSMakeSize([bmr pixelsWide], [bmr pixelsHigh])];
359 /* note; this and next work only for image created with initForXPMWithDepth,
360 initFromSkipXBM, or where setPixmapData was called successfully */
362 - (unsigned long) getPixelAtX: (int)x Y: (int)y
367 /* this method is faster but won't work for bitmaps */
368 if (pixmapData[0] != NULL)
370 int loc = x + y * [self size].width;
371 return (pixmapData[3][loc] << 24) /* alpha */
372 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
373 | (pixmapData[2][loc]);
377 NSColor *color = [bmRep colorAtX: x y: y];
378 EmacsCGFloat r, g, b, a;
379 [color getRed: &r green: &g blue: &b alpha: &a];
380 return ((int)(a * 255.0) << 24)
381 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
382 | ((int)(b * 255.0));
387 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
388 green: (unsigned char)g blue: (unsigned char)b
389 alpha:(unsigned char)a
394 if (pixmapData[0] != NULL)
396 int loc = x + y * [self size].width;
397 pixmapData[0][loc] = r;
398 pixmapData[1][loc] = g;
399 pixmapData[2][loc] = b;
400 pixmapData[3][loc] = a;
405 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
406 blue: (b/255.0) alpha: (a/255.0)]
411 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
416 if (pixmapData[0] != NULL)
418 int loc = x + y * [self size].width;
420 pixmapData[3][loc] = a;
424 NSColor *color = [bmRep colorAtX: x y: y];
425 color = [color colorWithAlphaComponent: (a / 255.0)];
426 [bmRep setColor: color atX: x y: y];
430 /* returns a pattern color, which is cached here */
431 - (NSColor *)stippleMask
433 if (stippleMask == nil)
434 stippleMask = [[NSColor colorWithPatternImage: self] retain];