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