In window.el tell whether functions operate on valid, live or any windows.
[bpt/emacs.git] / src / nsimage.m
CommitLineData
edfda783 1/* Image support for the NeXT/Open/GNUstep and MacOSX window system.
acaf905b 2 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2012
32d235f8 3 Free Software Foundation, Inc.
edfda783
AR
4
5This file is part of GNU Emacs.
6
32d235f8 7GNU Emacs is free software: you can redistribute it and/or modify
edfda783 8it under the terms of the GNU General Public License as published by
32d235f8
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
edfda783
AR
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
32d235f8 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
edfda783 19
32d235f8 20/*
edfda783
AR
21Originally by Carl Edman
22Updated by Christian Limpach (chris@nice.ch)
23OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
24MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
25GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
edfda783
AR
26*/
27
5a06864f
AR
28/* This should be the first include, as it may set up #defines affecting
29 interpretation of even the system includes. */
08a494a3 30#include <config.h>
b024548b 31#include <setjmp.h>
5a06864f 32
edfda783
AR
33#include "lisp.h"
34#include "dispextern.h"
35#include "nsterm.h"
36#include "frame.h"
37
38extern Lisp_Object QCfile, QCdata;
39
40/* call tracing */
41#if 0
42int image_trace_num = 0;
43#define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
44 __FILE__, __LINE__, ++image_trace_num)
45#else
46#define NSTRACE(x)
47#endif
48
49
50/* ==========================================================================
51
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..
55
56 ========================================================================== */
57
58void *
59ns_image_from_XBM (unsigned char *bits, int width, int height)
60{
61 NSTRACE (ns_image_from_XBM);
62 return [[EmacsImage alloc] initFromXBM: bits
63 width: width height: height
64 flip: YES];
65}
66
67void *
68ns_image_for_XPM (int width, int height, int depth)
69{
70 NSTRACE (ns_image_for_XPM);
71 return [[EmacsImage alloc] initForXPMWithDepth: depth
72 width: width height: height];
73}
74
75void *
76ns_image_from_file (Lisp_Object file)
77{
78 NSTRACE (ns_image_from_bitmap_file);
79 return [EmacsImage allocInitFromFile: file];
80}
81
82int
83ns_load_image (struct frame *f, struct image *img,
84 Lisp_Object spec_file, Lisp_Object spec_data)
85{
7574650a 86 EmacsImage *eImg = nil;
edfda783
AR
87 NSSize size;
88
3d608a86
J
89 NSTRACE (ns_load_image);
90
7574650a 91 if (STRINGP (spec_file))
edfda783
AR
92 {
93 eImg = [EmacsImage allocInitFromFile: spec_file];
94 }
7574650a 95 else if (STRINGP (spec_data))
edfda783 96 {
7574650a
AS
97 NSData *data;
98
0dc8cf50 99 data = [NSData dataWithBytes: SSDATA (spec_data)
7574650a 100 length: SBYTES (spec_data)];
edfda783
AR
101 eImg = [[EmacsImage alloc] initWithData: data];
102 [eImg setPixmapData];
103 }
104
105 if (eImg == nil)
106 {
107 add_to_log ("Unable to load image %s", img->spec, Qnil);
108 return 0;
109 }
110
111 size = [eImg size];
112 img->width = size.width;
113 img->height = size.height;
114
115 /* 4) set img->pixmap = emacsimage */
116 img->pixmap = eImg;
117 return 1;
118}
119
120
121int
122ns_image_width (void *img)
123{
124 return [(id)img size].width;
125}
126
127int
128ns_image_height (void *img)
129{
130 return [(id)img size].height;
131}
132
133unsigned long
134ns_get_pixel (void *img, int x, int y)
135{
136 return [(EmacsImage *)img getPixelAtX: x Y: y];
137}
138
139void
140ns_put_pixel (void *img, int x, int y, unsigned long argb)
141{
142 unsigned char alpha = (argb >> 24) & 0xFF;
143 if (alpha == 0)
144 alpha = 0xFF;
145 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
146 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
147}
148
149void
150ns_set_alpha (void *img, int x, int y, unsigned char a)
151{
152 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
153}
154
155
156/* ==========================================================================
157
158 Class supporting bitmaps and images of various sorts.
159
160 ========================================================================== */
161
162@implementation EmacsImage
163
164static EmacsImage *ImageList = nil;
165
166+ allocInitFromFile: (Lisp_Object)file
167{
168 EmacsImage *image = ImageList;
4c7077c3 169 NSImageRep *imgRep;
edfda783
AR
170 Lisp_Object found;
171
172 /* look for an existing image of the same name */
173 while (image != nil &&
0dc8cf50 174 [[image name] compare: [NSString stringWithUTF8String: SSDATA (file)]]
edfda783
AR
175 != NSOrderedSame)
176 image = [image imageListNext];
177
178 if (image != nil)
179 {
180 [image reference];
181 return image;
182 }
183
184 /* Search bitmap-file-path for the file, if appropriate. */
185 found = x_find_image_file (file);
186 if (!STRINGP (found))
187 return nil;
188
189 image = [[EmacsImage alloc] initByReferencingFile:
0dc8cf50 190 [NSString stringWithUTF8String: SSDATA (found)]];
edfda783 191
4393663b
JD
192#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
193 imgRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
194#else
4c7077c3 195 imgRep = [image bestRepresentationForDevice: nil];
4393663b 196#endif
4c7077c3 197 if (imgRep == nil)
edfda783
AR
198 {
199 [image release];
200 return nil;
201 }
202
4c7077c3
AR
203 /* The next two lines cause the DPI of the image to be ignored.
204 This seems to be the behavior users expect. */
205 [image setScalesWhenResized: YES];
206 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
207
0dc8cf50 208 [image setName: [NSString stringWithUTF8String: SSDATA (file)]];
edfda783
AR
209 [image reference];
210 ImageList = [image imageListSetNext: ImageList];
211
212 return image;
213}
214
215
216- reference
217{
218 refCount++;
219 return self;
220}
221
222
223- imageListSetNext: (id)arg
224{
225 imageListNext = arg;
226 return self;
227}
228
229
230- imageListNext
231{
232 return imageListNext;
233}
234
235
236- (void)dealloc
237{
238 id list = ImageList;
239
240 if (refCount > 1)
241 {
242 refCount--;
243 return;
244 }
245
246 [stippleMask release];
247
248 if (list == self)
249 ImageList = imageListNext;
250 else
251 {
252 while (list != nil && [list imageListNext] != self)
253 list = [list imageListNext];
254 [list imageListSetNext: imageListNext];
255 }
256
257 [super dealloc];
258}
259
260
261- initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
262 flip: (BOOL)flip
263{
264 return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0];
265}
266
267
268- initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h
269 flip: (BOOL)flip length: (int)length;
270{
271 int bpr = (w + 7) / 8;
272 unsigned char *planes[5];
273
274 [self initWithSize: NSMakeSize (w, h)];
275
276 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
277 pixelsWide: w pixelsHigh: h
278 bitsPerSample: 8 samplesPerPixel: 4
279 hasAlpha: YES isPlanar: YES
280 colorSpaceName: NSCalibratedRGBColorSpace
281 bytesPerRow: w bitsPerPixel: 0];
282
283 [bmRep getBitmapDataPlanes: planes];
284 {
285 /* pull bits out to set the (bytewise) alpha mask */
286 int i, j, k;
287 unsigned char *s = bits;
288 unsigned char *alpha = planes[3];
289 unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
290 3, 11, 7, 15};
291 unsigned char c, bitPat;
292
293 for (j = 0; j < h; j++)
294 for (i = 0; i < bpr; i++)
295 {
296 if (length)
297 {
298 unsigned char s1, s2;
299 while (*s++ != 'x' && s < bits + length);
300 if (s >= bits + length)
301 {
302 [bmRep release];
303 return nil;
304 }
305#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
306 s1 = *s++;
307 s2 = *s++;
308 c = hexchar (s1) * 0x10 + hexchar (s2);
309 }
310 else
311 c = *s++;
312
313 bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255;
314 for (k =0; k<8; k++)
315 {
316 *alpha++ = (bitPat & 0x80) ? 0xff : 0;
317 bitPat <<= 1;
318 }
319 }
320 }
321
322 [self addRepresentation: bmRep];
323
72af86bd
AS
324 memset (planes[0], 0, w*h);
325 memset (planes[1], 0, w*h);
326 memset (planes[2], 0, w*h);
edfda783
AR
327 [self setXBMColor: [NSColor blackColor]];
328 return self;
329}
330
331
332/* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
333 is used as a mask, so we just memset the entire array. */
334- setXBMColor: (NSColor *)color
335{
336 NSSize s = [self size];
edfda783 337 unsigned char *planes[5];
31e88bd8 338 CGFloat r, g, b, a;
edfda783
AR
339 NSColor *rgbColor;
340
341 if (bmRep == nil || color == nil)
3fdebbf9 342 return self;
edfda783
AR
343
344 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
345 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
346 else
347 rgbColor = color;
348
349 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
350
351 [bmRep getBitmapDataPlanes: planes];
352
353 /* we used to just do this, but Cocoa seems to have a bug when rendering
354 an alpha-masked image onto a dark background where it bloats the mask */
355 /* memset (planes[0..2], r, g, b*0xff, len); */
356 {
357 int i, len = s.width*s.height;
358 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
359 for (i =0; i<len; i++)
360 if (planes[3][i] != 0)
361 {
362 planes[0][i] = rr;
363 planes[1][i] = gg;
364 planes[2][i] = bb;
365 }
366 }
3fdebbf9
AR
367
368 return self;
edfda783
AR
369}
370
371
372- initForXPMWithDepth: (int)depth width: (int)width height: (int)height
373{
374 NSSize s = {width, height};
375 int i;
376
377 [self initWithSize: s];
378
379 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
380 pixelsWide: width pixelsHigh: height
381 /* keep things simple for now */
382 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
383 hasAlpha: YES isPlanar: YES
384 colorSpaceName: NSCalibratedRGBColorSpace
385 bytesPerRow: width bitsPerPixel: 0];
386
387 [bmRep getBitmapDataPlanes: pixmapData];
388 for (i =0; i<4; i++)
72af86bd 389 memset (pixmapData[i], 0, width*height);
edfda783
AR
390 [self addRepresentation: bmRep];
391 return self;
392}
393
394
395/* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
396- (void) setPixmapData
397{
398 NSEnumerator *reps;
399 NSImageRep *rep;
400
401 reps = [[self representations] objectEnumerator];
0dc8cf50 402 while ((rep = (NSImageRep *) [reps nextObject]))
edfda783
AR
403 {
404 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
405 {
406 bmRep = (NSBitmapImageRep *) rep;
407 onTiger = [bmRep respondsToSelector: @selector (colorAtX:y:)];
408
409 if ([bmRep numberOfPlanes] >= 3)
410 [bmRep getBitmapDataPlanes: pixmapData];
e0d2e69a
AR
411
412 /* The next two lines cause the DPI of the image to be ignored.
413 This seems to be the behavior users expect. */
414 [self setScalesWhenResized: YES];
415 [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
416
edfda783
AR
417 break;
418 }
419 }
420}
421
422
423/* note; this and next work only for image created with initForXPMWithDepth,
424 initFromSkipXBM, or where setPixmapData was called successfully */
425/* return ARGB */
426- (unsigned long) getPixelAtX: (int)x Y: (int)y
427{
428 if (bmRep == nil)
429 return 0;
430
431 /* this method is faster but won't work for bitmaps */
432 if (pixmapData[0] != NULL)
433 {
434 int loc = x + y * [self size].width;
435 return (pixmapData[3][loc] << 24) /* alpha */
436 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
437 | (pixmapData[2][loc]);
438 }
439 else if (onTiger)
440 {
441 NSColor *color = [bmRep colorAtX: x y: y];
31e88bd8 442 CGFloat r, g, b, a;
edfda783
AR
443 [color getRed: &r green: &g blue: &b alpha: &a];
444 return ((int)(a * 255.0) << 24)
445 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
446 | ((int)(b * 255.0));
447
448 }
449 return 0;
450}
451
452- (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
453 green: (unsigned char)g blue: (unsigned char)b
454 alpha:(unsigned char)a;
455{
456 if (bmRep == nil)
457 return;
458
459 if (pixmapData[0] != NULL)
460 {
461 int loc = x + y * [self size].width;
462 pixmapData[0][loc] = r;
463 pixmapData[1][loc] = g;
464 pixmapData[2][loc] = b;
465 pixmapData[3][loc] = a;
466 }
467 else if (onTiger)
468 {
469 [bmRep setColor:
d3810c21
AR
470 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
471 blue: (b/255.0) alpha: (a/255.0)]
edfda783
AR
472 atX: x y: y];
473 }
474}
475
476- (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
477{
478 if (bmRep == nil)
479 return;
480
481 if (pixmapData[0] != NULL)
482 {
483 int loc = x + y * [self size].width;
484
485 pixmapData[3][loc] = a;
486 }
487 else if (onTiger)
488 {
489 NSColor *color = [bmRep colorAtX: x y: y];
490 color = [color colorWithAlphaComponent: (a / 255.0)];
491 [bmRep setColor: color atX: x y: y];
492 }
493}
494
495/* returns a pattern color, which is cached here */
496- (NSColor *)stippleMask
497{
498 if (stippleMask == nil)
499 stippleMask = [[NSColor colorWithPatternImage: self] retain];
500 return stippleMask;
501}
502
503@end
0ae1e5e5 504