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