Notes about elpa.gnu.org for maintainers.
[bpt/emacs.git] / src / nsimage.m
CommitLineData
edfda783 1/* Image support for the NeXT/Open/GNUstep and MacOSX window system.
114f9c96 2 Copyright (C) 1989, 1992, 1993, 1994, 2005, 2006, 2008, 2009, 2010
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
99 data = [NSData dataWithBytes: SDATA (spec_data)
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 &&
174 [[image name] compare: [NSString stringWithUTF8String: SDATA (file)]]
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:
190 [NSString stringWithUTF8String: SDATA (found)]];
191
4c7077c3
AR
192 imgRep = [image bestRepresentationForDevice: nil];
193 if (imgRep == nil)
edfda783
AR
194 {
195 [image release];
196 return nil;
197 }
198
4c7077c3
AR
199 /* The next two lines cause the DPI of the image to be ignored.
200 This seems to be the behavior users expect. */
201 [image setScalesWhenResized: YES];
202 [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
203
edfda783
AR
204 [image setName: [NSString stringWithUTF8String: SDATA (file)]];
205 [image reference];
206 ImageList = [image imageListSetNext: ImageList];
207
208 return image;
209}
210
211
212- reference
213{
214 refCount++;
215 return self;
216}
217
218
219- imageListSetNext: (id)arg
220{
221 imageListNext = arg;
222 return self;
223}
224
225
226- imageListNext
227{
228 return imageListNext;
229}
230
231
232- (void)dealloc
233{
234 id list = ImageList;
235
236 if (refCount > 1)
237 {
238 refCount--;
239 return;
240 }
241
242 [stippleMask release];
243
244 if (list == self)
245 ImageList = imageListNext;
246 else
247 {
248 while (list != nil && [list imageListNext] != self)
249 list = [list imageListNext];
250 [list imageListSetNext: imageListNext];
251 }
252
253 [super dealloc];
254}
255
256
257- initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
258 flip: (BOOL)flip
259{
260 return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0];
261}
262
263
264- initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h
265 flip: (BOOL)flip length: (int)length;
266{
267 int bpr = (w + 7) / 8;
268 unsigned char *planes[5];
269
270 [self initWithSize: NSMakeSize (w, h)];
271
272 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
273 pixelsWide: w pixelsHigh: h
274 bitsPerSample: 8 samplesPerPixel: 4
275 hasAlpha: YES isPlanar: YES
276 colorSpaceName: NSCalibratedRGBColorSpace
277 bytesPerRow: w bitsPerPixel: 0];
278
279 [bmRep getBitmapDataPlanes: planes];
280 {
281 /* pull bits out to set the (bytewise) alpha mask */
282 int i, j, k;
283 unsigned char *s = bits;
284 unsigned char *alpha = planes[3];
285 unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
286 3, 11, 7, 15};
287 unsigned char c, bitPat;
288
289 for (j = 0; j < h; j++)
290 for (i = 0; i < bpr; i++)
291 {
292 if (length)
293 {
294 unsigned char s1, s2;
295 while (*s++ != 'x' && s < bits + length);
296 if (s >= bits + length)
297 {
298 [bmRep release];
299 return nil;
300 }
301#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
302 s1 = *s++;
303 s2 = *s++;
304 c = hexchar (s1) * 0x10 + hexchar (s2);
305 }
306 else
307 c = *s++;
308
309 bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255;
310 for (k =0; k<8; k++)
311 {
312 *alpha++ = (bitPat & 0x80) ? 0xff : 0;
313 bitPat <<= 1;
314 }
315 }
316 }
317
318 [self addRepresentation: bmRep];
319
72af86bd
AS
320 memset (planes[0], 0, w*h);
321 memset (planes[1], 0, w*h);
322 memset (planes[2], 0, w*h);
edfda783
AR
323 [self setXBMColor: [NSColor blackColor]];
324 return self;
325}
326
327
328/* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
329 is used as a mask, so we just memset the entire array. */
330- setXBMColor: (NSColor *)color
331{
332 NSSize s = [self size];
333 int len = (int) s.width * s.height;
334 unsigned char *planes[5];
31e88bd8 335 CGFloat r, g, b, a;
edfda783
AR
336 NSColor *rgbColor;
337
338 if (bmRep == nil || color == nil)
339 return;
340
341 if ([color colorSpaceName] != NSCalibratedRGBColorSpace)
342 rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
343 else
344 rgbColor = color;
345
346 [rgbColor getRed: &r green: &g blue: &b alpha: &a];
347
348 [bmRep getBitmapDataPlanes: planes];
349
350 /* we used to just do this, but Cocoa seems to have a bug when rendering
351 an alpha-masked image onto a dark background where it bloats the mask */
352 /* memset (planes[0..2], r, g, b*0xff, len); */
353 {
354 int i, len = s.width*s.height;
355 int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff;
356 for (i =0; i<len; i++)
357 if (planes[3][i] != 0)
358 {
359 planes[0][i] = rr;
360 planes[1][i] = gg;
361 planes[2][i] = bb;
362 }
363 }
364}
365
366
367- initForXPMWithDepth: (int)depth width: (int)width height: (int)height
368{
369 NSSize s = {width, height};
370 int i;
371
372 [self initWithSize: s];
373
374 bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
375 pixelsWide: width pixelsHigh: height
376 /* keep things simple for now */
377 bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/
378 hasAlpha: YES isPlanar: YES
379 colorSpaceName: NSCalibratedRGBColorSpace
380 bytesPerRow: width bitsPerPixel: 0];
381
382 [bmRep getBitmapDataPlanes: pixmapData];
383 for (i =0; i<4; i++)
72af86bd 384 memset (pixmapData[i], 0, width*height);
edfda783
AR
385 [self addRepresentation: bmRep];
386 return self;
387}
388
389
390/* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
391- (void) setPixmapData
392{
393 NSEnumerator *reps;
394 NSImageRep *rep;
395
396 reps = [[self representations] objectEnumerator];
397 while (rep = (NSImageRep *) [reps nextObject])
398 {
399 if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)])
400 {
401 bmRep = (NSBitmapImageRep *) rep;
402 onTiger = [bmRep respondsToSelector: @selector (colorAtX:y:)];
403
404 if ([bmRep numberOfPlanes] >= 3)
405 [bmRep getBitmapDataPlanes: pixmapData];
e0d2e69a
AR
406
407 /* The next two lines cause the DPI of the image to be ignored.
408 This seems to be the behavior users expect. */
409 [self setScalesWhenResized: YES];
410 [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
411
edfda783
AR
412 break;
413 }
414 }
415}
416
417
418/* note; this and next work only for image created with initForXPMWithDepth,
419 initFromSkipXBM, or where setPixmapData was called successfully */
420/* return ARGB */
421- (unsigned long) getPixelAtX: (int)x Y: (int)y
422{
423 if (bmRep == nil)
424 return 0;
425
426 /* this method is faster but won't work for bitmaps */
427 if (pixmapData[0] != NULL)
428 {
429 int loc = x + y * [self size].width;
430 return (pixmapData[3][loc] << 24) /* alpha */
431 | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8)
432 | (pixmapData[2][loc]);
433 }
434 else if (onTiger)
435 {
436 NSColor *color = [bmRep colorAtX: x y: y];
31e88bd8 437 CGFloat r, g, b, a;
edfda783
AR
438 [color getRed: &r green: &g blue: &b alpha: &a];
439 return ((int)(a * 255.0) << 24)
440 | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
441 | ((int)(b * 255.0));
442
443 }
444 return 0;
445}
446
447- (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
448 green: (unsigned char)g blue: (unsigned char)b
449 alpha:(unsigned char)a;
450{
451 if (bmRep == nil)
452 return;
453
454 if (pixmapData[0] != NULL)
455 {
456 int loc = x + y * [self size].width;
457 pixmapData[0][loc] = r;
458 pixmapData[1][loc] = g;
459 pixmapData[2][loc] = b;
460 pixmapData[3][loc] = a;
461 }
462 else if (onTiger)
463 {
464 [bmRep setColor:
d3810c21
AR
465 [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
466 blue: (b/255.0) alpha: (a/255.0)]
edfda783
AR
467 atX: x y: y];
468 }
469}
470
471- (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a
472{
473 if (bmRep == nil)
474 return;
475
476 if (pixmapData[0] != NULL)
477 {
478 int loc = x + y * [self size].width;
479
480 pixmapData[3][loc] = a;
481 }
482 else if (onTiger)
483 {
484 NSColor *color = [bmRep colorAtX: x y: y];
485 color = [color colorWithAlphaComponent: (a / 255.0)];
486 [bmRep setColor: color atX: x y: y];
487 }
488}
489
490/* returns a pattern color, which is cached here */
491- (NSColor *)stippleMask
492{
493 if (stippleMask == nil)
494 stippleMask = [[NSColor colorWithPatternImage: self] retain];
495 return stippleMask;
496}
497
498@end
0ae1e5e5 499
2f8e74bb 500// arch-tag: 6b310280-6892-4e5e-8f34-41c4d384874f