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