Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / src / nsimage.m
1 /* Image support for the NeXT/Open/GNUstep and MacOSX window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 2005, 2006, 2008, 2009, 2010, 2011, 2012
3 Free Software 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 #include <setjmp.h>
32
33 #include "lisp.h"
34 #include "dispextern.h"
35 #include "nsterm.h"
36 #include "frame.h"
37
38 extern Lisp_Object QCfile, QCdata;
39
40 /* call tracing */
41 #if 0
42 int 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
58 void *
59 ns_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
67 void *
68 ns_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
75 void *
76 ns_image_from_file (Lisp_Object file)
77 {
78 NSTRACE (ns_image_from_bitmap_file);
79 return [EmacsImage allocInitFromFile: file];
80 }
81
82 int
83 ns_load_image (struct frame *f, struct image *img,
84 Lisp_Object spec_file, Lisp_Object spec_data)
85 {
86 NSTRACE (ns_load_image);
87
88 EmacsImage *eImg;
89 NSSize size;
90
91 if (NILP (spec_data))
92 {
93 eImg = [EmacsImage allocInitFromFile: spec_file];
94 }
95 else
96 {
97 NSData *data = [NSData dataWithBytes: SDATA (spec_data)
98 length: SBYTES (spec_data)];
99 eImg = [[EmacsImage alloc] initWithData: data];
100 [eImg setPixmapData];
101 }
102
103 if (eImg == nil)
104 {
105 add_to_log ("Unable to load image %s", img->spec, Qnil);
106 return 0;
107 }
108
109 size = [eImg size];
110 img->width = size.width;
111 img->height = size.height;
112
113 /* 4) set img->pixmap = emacsimage */
114 img->pixmap = eImg;
115 return 1;
116 }
117
118
119 int
120 ns_image_width (void *img)
121 {
122 return [(id)img size].width;
123 }
124
125 int
126 ns_image_height (void *img)
127 {
128 return [(id)img size].height;
129 }
130
131 unsigned long
132 ns_get_pixel (void *img, int x, int y)
133 {
134 return [(EmacsImage *)img getPixelAtX: x Y: y];
135 }
136
137 void
138 ns_put_pixel (void *img, int x, int y, unsigned long argb)
139 {
140 unsigned char alpha = (argb >> 24) & 0xFF;
141 if (alpha == 0)
142 alpha = 0xFF;
143 [(EmacsImage *)img setPixelAtX: x Y: y toRed: (argb >> 16) & 0xFF
144 green: (argb >> 8) & 0xFF blue: (argb & 0xFF) alpha: alpha];
145 }
146
147 void
148 ns_set_alpha (void *img, int x, int y, unsigned char a)
149 {
150 [(EmacsImage *)img setAlphaAtX: x Y: y to: a];
151 }
152
153
154 /* ==========================================================================
155
156 Class supporting bitmaps and images of various sorts.
157
158 ========================================================================== */
159
160 @implementation EmacsImage
161
162 static EmacsImage *ImageList = nil;
163
164 + allocInitFromFile: (Lisp_Object)file
165 {
166 EmacsImage *image = ImageList;
167 NSImageRep *imgRep;
168 Lisp_Object found;
169
170 /* look for an existing image of the same name */
171 while (image != nil &&
172 [[image name] compare: [NSString stringWithUTF8String: SDATA (file)]]
173 != NSOrderedSame)
174 image = [image imageListNext];
175
176 if (image != nil)
177 {
178 [image reference];
179 return image;
180 }
181
182 /* Search bitmap-file-path for the file, if appropriate. */
183 found = x_find_image_file (file);
184 if (!STRINGP (found))
185 return nil;
186
187 image = [[EmacsImage alloc] initByReferencingFile:
188 [NSString stringWithUTF8String: SDATA (found)]];
189
190 imgRep = [image bestRepresentationForDevice: nil];
191 if (imgRep == nil)
192 {
193 [image release];
194 return nil;
195 }
196
197 /* The next two lines cause the DPI of the image to be ignored.
198 This seems to be the behavior users expect. */
199 [image setScalesWhenResized: YES];
200 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
201
202 [image setName: [NSString stringWithUTF8String: SDATA (file)]];
203 [image reference];
204 ImageList = [image imageListSetNext: ImageList];
205
206 return image;
207 }
208
209
210 - reference
211 {
212 refCount++;
213 return self;
214 }
215
216
217 - imageListSetNext: (id)arg
218 {
219 imageListNext = arg;
220 return self;
221 }
222
223
224 - imageListNext
225 {
226 return imageListNext;
227 }
228
229
230 - (void)dealloc
231 {
232 id list = ImageList;
233
234 if (refCount > 1)
235 {
236 refCount--;
237 return;
238 }
239
240 [stippleMask release];
241
242 if (list == self)
243 ImageList = imageListNext;
244 else
245 {
246 while (list != nil && [list imageListNext] != self)
247 list = [list imageListNext];
248 [list imageListSetNext: imageListNext];
249 }
250
251 [super dealloc];
252 }
253
254
255 - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
256 flip: (BOOL)flip
257 {
258 return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0];
259 }
260
261
262 - initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h
263 flip: (BOOL)flip length: (int)length;
264 {
265 int bpr = (w + 7) / 8;
266 unsigned char *planes[5];
267
268 [self initWithSize: NSMakeSize (w, h)];
269
270 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
271 pixelsWide: w pixelsHigh: h
272 bitsPerSample: 8 samplesPerPixel: 4
273 hasAlpha: YES isPlanar: YES
274 colorSpaceName: NSCalibratedRGBColorSpace
275 bytesPerRow: w bitsPerPixel: 0];
276
277 [bmRep getBitmapDataPlanes: planes];
278 {
279 /* pull bits out to set the (bytewise) alpha mask */
280 int i, j, k;
281 unsigned char *s = bits;
282 unsigned char *alpha = planes[3];
283 unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
284 3, 11, 7, 15};
285 unsigned char c, bitPat;
286
287 for (j = 0; j < h; j++)
288 for (i = 0; i < bpr; i++)
289 {
290 if (length)
291 {
292 unsigned char s1, s2;
293 while (*s++ != 'x' && s < bits + length);
294 if (s >= bits + length)
295 {
296 [bmRep release];
297 return nil;
298 }
299 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
300 s1 = *s++;
301 s2 = *s++;
302 c = hexchar (s1) * 0x10 + hexchar (s2);
303 }
304 else
305 c = *s++;
306
307 bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255;
308 for (k =0; k<8; k++)
309 {
310 *alpha++ = (bitPat & 0x80) ? 0xff : 0;
311 bitPat <<= 1;
312 }
313 }
314 }
315
316 [self addRepresentation: bmRep];
317
318 bzero (planes[0], w*h);
319 bzero (planes[1], w*h);
320 bzero (planes[2], w*h);
321 [self setXBMColor: [NSColor blackColor]];
322 return self;
323 }
324
325
326 /* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
327 is used as a mask, so we just memset the entire array. */
328 - setXBMColor: (NSColor *)color
329 {
330 NSSize s = [self size];
331 int len = (int) s.width * s.height;
332 unsigned char *planes[5];
333 CGFloat r, g, b, a;
334 NSColor *rgbColor;
335
336 if (bmRep == nil || color == nil)
337 return;
338
339 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
340 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
341 else
342 rgbColor = color;
343
344 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
345
346 [bmRep getBitmapDataPlanes: planes];
347
348 /* we used to just do this, but Cocoa seems to have a bug when rendering
349 an alpha-masked image onto a dark background where it bloats the mask */
350 /* memset (planes[0..2], r, g, b*0xff, len); */
351 {
352 int i, len = s.width*s.height;
353 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
354 for (i =0; i<len; i++)
355 if (planes[3][i] != 0)
356 {
357 planes[0][i] = rr;
358 planes[1][i] = gg;
359 planes[2][i] = bb;
360 }
361 }
362 }
363
364
365 - initForXPMWithDepth: (int)depth width: (int)width height: (int)height
366 {
367 NSSize s = {width, height};
368 int i;
369
370 [self initWithSize: s];
371
372 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
373 pixelsWide: width pixelsHigh: height
374 /* keep things simple for now */
375 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
376 hasAlpha: YES isPlanar: YES
377 colorSpaceName: NSCalibratedRGBColorSpace
378 bytesPerRow: width bitsPerPixel: 0];
379
380 [bmRep getBitmapDataPlanes: pixmapData];
381 for (i =0; i<4; i++)
382 bzero (pixmapData[i], width*height);
383 [self addRepresentation: bmRep];
384 return self;
385 }
386
387
388 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
389 - (void) setPixmapData
390 {
391 NSEnumerator *reps;
392 NSImageRep *rep;
393
394 reps = [[self representations] objectEnumerator];
395 while (rep = (NSImageRep *) [reps nextObject])
396 {
397 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
398 {
399 bmRep = (NSBitmapImageRep *) rep;
400 onTiger = [bmRep respondsToSelector: @selector (colorAtX:y:)];
401
402 if ([bmRep numberOfPlanes] >= 3)
403 [bmRep getBitmapDataPlanes: pixmapData];
404
405 /* The next two lines cause the DPI of the image to be ignored.
406 This seems to be the behavior users expect. */
407 [self setScalesWhenResized: YES];
408 [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
409
410 break;
411 }
412 }
413 }
414
415
416 /* note; this and next work only for image created with initForXPMWithDepth,
417 initFromSkipXBM, or where setPixmapData was called successfully */
418 /* return ARGB */
419 - (unsigned long) getPixelAtX: (int)x Y: (int)y
420 {
421 if (bmRep == nil)
422 return 0;
423
424 /* this method is faster but won't work for bitmaps */
425 if (pixmapData[0] != NULL)
426 {
427 int loc = x + y * [self size].width;
428 return (pixmapData[3][loc] << 24) /* alpha */
429 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
430 | (pixmapData[2][loc]);
431 }
432 else if (onTiger)
433 {
434 NSColor *color = [bmRep colorAtX: x y: y];
435 CGFloat r, g, b, a;
436 [color getRed: &r green: &g blue: &b alpha: &a];
437 return ((int)(a * 255.0) << 24)
438 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
439 | ((int)(b * 255.0));
440
441 }
442 return 0;
443 }
444
445 - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
446 green: (unsigned char)g blue: (unsigned char)b
447 alpha:(unsigned char)a;
448 {
449 if (bmRep == nil)
450 return;
451
452 if (pixmapData[0] != NULL)
453 {
454 int loc = x + y * [self size].width;
455 pixmapData[0][loc] = r;
456 pixmapData[1][loc] = g;
457 pixmapData[2][loc] = b;
458 pixmapData[3][loc] = a;
459 }
460 else if (onTiger)
461 {
462 [bmRep setColor:
463 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
464 blue: (b/255.0) alpha: (a/255.0)]
465 atX: x y: y];
466 }
467 }
468
469 - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
470 {
471 if (bmRep == nil)
472 return;
473
474 if (pixmapData[0] != NULL)
475 {
476 int loc = x + y * [self size].width;
477
478 pixmapData[3][loc] = a;
479 }
480 else if (onTiger)
481 {
482 NSColor *color = [bmRep colorAtX: x y: y];
483 color = [color colorWithAlphaComponent: (a / 255.0)];
484 [bmRep setColor: color atX: x y: y];
485 }
486 }
487
488 /* returns a pattern color, which is cached here */
489 - (NSColor *)stippleMask
490 {
491 if (stippleMask == nil)
492 stippleMask = [[NSColor colorWithPatternImage: self] retain];
493 return stippleMask;
494 }
495
496 @end
497
498 // arch-tag: 6b310280-6892-4e5e-8f34-41c4d384874f