Commit | Line | Data |
---|---|---|
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 | |
5 | This file is part of GNU Emacs. | |
6 | ||
32d235f8 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
edfda783 | 8 | it under the terms of the GNU General Public License as published by |
32d235f8 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
edfda783 AR |
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 | |
32d235f8 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
edfda783 | 19 | |
32d235f8 | 20 | /* |
edfda783 AR |
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) | |
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 | ||
37 | extern Lisp_Object QCfile, QCdata; | |
38 | ||
39 | /* call tracing */ | |
40 | #if 0 | |
41 | int 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 | ||
57 | void * | |
58 | ns_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 | ||
66 | void * | |
67 | ns_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 | ||
74 | void * | |
75 | ns_image_from_file (Lisp_Object file) | |
76 | { | |
77 | NSTRACE (ns_image_from_bitmap_file); | |
78 | return [EmacsImage allocInitFromFile: file]; | |
79 | } | |
80 | ||
81 | int | |
82 | ns_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 | ||
118 | int | |
119 | ns_image_width (void *img) | |
120 | { | |
121 | return [(id)img size].width; | |
122 | } | |
123 | ||
124 | int | |
125 | ns_image_height (void *img) | |
126 | { | |
127 | return [(id)img size].height; | |
128 | } | |
129 | ||
130 | unsigned long | |
131 | ns_get_pixel (void *img, int x, int y) | |
132 | { | |
133 | return [(EmacsImage *)img getPixelAtX: x Y: y]; | |
134 | } | |
135 | ||
136 | void | |
137 | ns_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 | ||
146 | void | |
147 | ns_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 | ||
161 | static EmacsImage *ImageList = nil; | |
162 | ||
163 | + allocInitFromFile: (Lisp_Object)file | |
164 | { | |
165 | EmacsImage *image = ImageList; | |
4c7077c3 | 166 | NSImageRep *imgRep; |
edfda783 AR |
167 | Lisp_Object found; |
168 | ||
169 | /* look for an existing image of the same name */ | |
170 | while (image != nil && | |
171 | [[image name] compare: [NSString stringWithUTF8String: SDATA (file)]] | |
172 | != NSOrderedSame) | |
173 | image = [image imageListNext]; | |
174 | ||
175 | if (image != nil) | |
176 | { | |
177 | [image reference]; | |
178 | return image; | |
179 | } | |
180 | ||
181 | /* Search bitmap-file-path for the file, if appropriate. */ | |
182 | found = x_find_image_file (file); | |
183 | if (!STRINGP (found)) | |
184 | return nil; | |
185 | ||
186 | image = [[EmacsImage alloc] initByReferencingFile: | |
187 | [NSString stringWithUTF8String: SDATA (found)]]; | |
188 | ||
4c7077c3 AR |
189 | imgRep = [image bestRepresentationForDevice: nil]; |
190 | if (imgRep == nil) | |
edfda783 AR |
191 | { |
192 | [image release]; | |
193 | return nil; | |
194 | } | |
195 | ||
4c7077c3 AR |
196 | /* The next two lines cause the DPI of the image to be ignored. |
197 | This seems to be the behavior users expect. */ | |
198 | [image setScalesWhenResized: YES]; | |
199 | [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])]; | |
200 | ||
edfda783 AR |
201 | [image setName: [NSString stringWithUTF8String: SDATA (file)]]; |
202 | [image reference]; | |
203 | ImageList = [image imageListSetNext: ImageList]; | |
204 | ||
205 | return image; | |
206 | } | |
207 | ||
208 | ||
209 | - reference | |
210 | { | |
211 | refCount++; | |
212 | return self; | |
213 | } | |
214 | ||
215 | ||
216 | - imageListSetNext: (id)arg | |
217 | { | |
218 | imageListNext = arg; | |
219 | return self; | |
220 | } | |
221 | ||
222 | ||
223 | - imageListNext | |
224 | { | |
225 | return imageListNext; | |
226 | } | |
227 | ||
228 | ||
229 | - (void)dealloc | |
230 | { | |
231 | id list = ImageList; | |
232 | ||
233 | if (refCount > 1) | |
234 | { | |
235 | refCount--; | |
236 | return; | |
237 | } | |
238 | ||
239 | [stippleMask release]; | |
240 | ||
241 | if (list == self) | |
242 | ImageList = imageListNext; | |
243 | else | |
244 | { | |
245 | while (list != nil && [list imageListNext] != self) | |
246 | list = [list imageListNext]; | |
247 | [list imageListSetNext: imageListNext]; | |
248 | } | |
249 | ||
250 | [super dealloc]; | |
251 | } | |
252 | ||
253 | ||
254 | - initFromXBM: (unsigned char *)bits width: (int)w height: (int)h | |
255 | flip: (BOOL)flip | |
256 | { | |
257 | return [self initFromSkipXBM: bits width: w height: h flip: flip length: 0]; | |
258 | } | |
259 | ||
260 | ||
261 | - initFromSkipXBM: (unsigned char *)bits width: (int)w height: (int)h | |
262 | flip: (BOOL)flip length: (int)length; | |
263 | { | |
264 | int bpr = (w + 7) / 8; | |
265 | unsigned char *planes[5]; | |
266 | ||
267 | [self initWithSize: NSMakeSize (w, h)]; | |
268 | ||
269 | bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL | |
270 | pixelsWide: w pixelsHigh: h | |
271 | bitsPerSample: 8 samplesPerPixel: 4 | |
272 | hasAlpha: YES isPlanar: YES | |
273 | colorSpaceName: NSCalibratedRGBColorSpace | |
274 | bytesPerRow: w bitsPerPixel: 0]; | |
275 | ||
276 | [bmRep getBitmapDataPlanes: planes]; | |
277 | { | |
278 | /* pull bits out to set the (bytewise) alpha mask */ | |
279 | int i, j, k; | |
280 | unsigned char *s = bits; | |
281 | unsigned char *alpha = planes[3]; | |
282 | unsigned char swt[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, | |
283 | 3, 11, 7, 15}; | |
284 | unsigned char c, bitPat; | |
285 | ||
286 | for (j = 0; j < h; j++) | |
287 | for (i = 0; i < bpr; i++) | |
288 | { | |
289 | if (length) | |
290 | { | |
291 | unsigned char s1, s2; | |
292 | while (*s++ != 'x' && s < bits + length); | |
293 | if (s >= bits + length) | |
294 | { | |
295 | [bmRep release]; | |
296 | return nil; | |
297 | } | |
298 | #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10) | |
299 | s1 = *s++; | |
300 | s2 = *s++; | |
301 | c = hexchar (s1) * 0x10 + hexchar (s2); | |
302 | } | |
303 | else | |
304 | c = *s++; | |
305 | ||
306 | bitPat = flip ? swt[c >> 4] | (swt[c & 0xf] << 4) : c ^ 255; | |
307 | for (k =0; k<8; k++) | |
308 | { | |
309 | *alpha++ = (bitPat & 0x80) ? 0xff : 0; | |
310 | bitPat <<= 1; | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | [self addRepresentation: bmRep]; | |
316 | ||
317 | bzero (planes[0], w*h); | |
318 | bzero (planes[1], w*h); | |
319 | bzero (planes[2], w*h); | |
320 | [self setXBMColor: [NSColor blackColor]]; | |
321 | return self; | |
322 | } | |
323 | ||
324 | ||
325 | /* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha | |
326 | is used as a mask, so we just memset the entire array. */ | |
327 | - setXBMColor: (NSColor *)color | |
328 | { | |
329 | NSSize s = [self size]; | |
330 | int len = (int) s.width * s.height; | |
331 | unsigned char *planes[5]; | |
332 | float r, g, b, a; | |
333 | NSColor *rgbColor; | |
334 | ||
335 | if (bmRep == nil || color == nil) | |
336 | return; | |
337 | ||
338 | if ([color colorSpaceName] != NSCalibratedRGBColorSpace) | |
339 | rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace]; | |
340 | else | |
341 | rgbColor = color; | |
342 | ||
343 | [rgbColor getRed: &r green: &g blue: &b alpha: &a]; | |
344 | ||
345 | [bmRep getBitmapDataPlanes: planes]; | |
346 | ||
347 | /* we used to just do this, but Cocoa seems to have a bug when rendering | |
348 | an alpha-masked image onto a dark background where it bloats the mask */ | |
349 | /* memset (planes[0..2], r, g, b*0xff, len); */ | |
350 | { | |
351 | int i, len = s.width*s.height; | |
352 | int rr = r * 0xff, gg = g * 0xff, bb = b * 0xff; | |
353 | for (i =0; i<len; i++) | |
354 | if (planes[3][i] != 0) | |
355 | { | |
356 | planes[0][i] = rr; | |
357 | planes[1][i] = gg; | |
358 | planes[2][i] = bb; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | ||
364 | - initForXPMWithDepth: (int)depth width: (int)width height: (int)height | |
365 | { | |
366 | NSSize s = {width, height}; | |
367 | int i; | |
368 | ||
369 | [self initWithSize: s]; | |
370 | ||
371 | bmRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL | |
372 | pixelsWide: width pixelsHigh: height | |
373 | /* keep things simple for now */ | |
374 | bitsPerSample: 8 samplesPerPixel: 4 /*RGB+A*/ | |
375 | hasAlpha: YES isPlanar: YES | |
376 | colorSpaceName: NSCalibratedRGBColorSpace | |
377 | bytesPerRow: width bitsPerPixel: 0]; | |
378 | ||
379 | [bmRep getBitmapDataPlanes: pixmapData]; | |
380 | for (i =0; i<4; i++) | |
381 | bzero (pixmapData[i], width*height); | |
382 | [self addRepresentation: bmRep]; | |
383 | return self; | |
384 | } | |
385 | ||
386 | ||
387 | /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */ | |
388 | - (void) setPixmapData | |
389 | { | |
390 | NSEnumerator *reps; | |
391 | NSImageRep *rep; | |
392 | ||
393 | reps = [[self representations] objectEnumerator]; | |
394 | while (rep = (NSImageRep *) [reps nextObject]) | |
395 | { | |
396 | if ([rep respondsToSelector: @selector (getBitmapDataPlanes:)]) | |
397 | { | |
398 | bmRep = (NSBitmapImageRep *) rep; | |
399 | onTiger = [bmRep respondsToSelector: @selector (colorAtX:y:)]; | |
400 | ||
401 | if ([bmRep numberOfPlanes] >= 3) | |
402 | [bmRep getBitmapDataPlanes: pixmapData]; | |
403 | break; | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | ||
409 | /* note; this and next work only for image created with initForXPMWithDepth, | |
410 | initFromSkipXBM, or where setPixmapData was called successfully */ | |
411 | /* return ARGB */ | |
412 | - (unsigned long) getPixelAtX: (int)x Y: (int)y | |
413 | { | |
414 | if (bmRep == nil) | |
415 | return 0; | |
416 | ||
417 | /* this method is faster but won't work for bitmaps */ | |
418 | if (pixmapData[0] != NULL) | |
419 | { | |
420 | int loc = x + y * [self size].width; | |
421 | return (pixmapData[3][loc] << 24) /* alpha */ | |
422 | | (pixmapData[0][loc] << 16) | (pixmapData[1][loc] << 8) | |
423 | | (pixmapData[2][loc]); | |
424 | } | |
425 | else if (onTiger) | |
426 | { | |
427 | NSColor *color = [bmRep colorAtX: x y: y]; | |
428 | float r, g, b, a; | |
429 | [color getRed: &r green: &g blue: &b alpha: &a]; | |
430 | return ((int)(a * 255.0) << 24) | |
431 | | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8) | |
432 | | ((int)(b * 255.0)); | |
433 | ||
434 | } | |
435 | return 0; | |
436 | } | |
437 | ||
438 | - (void) setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r | |
439 | green: (unsigned char)g blue: (unsigned char)b | |
440 | alpha:(unsigned char)a; | |
441 | { | |
442 | if (bmRep == nil) | |
443 | return; | |
444 | ||
445 | if (pixmapData[0] != NULL) | |
446 | { | |
447 | int loc = x + y * [self size].width; | |
448 | pixmapData[0][loc] = r; | |
449 | pixmapData[1][loc] = g; | |
450 | pixmapData[2][loc] = b; | |
451 | pixmapData[3][loc] = a; | |
452 | } | |
453 | else if (onTiger) | |
454 | { | |
455 | [bmRep setColor: | |
d3810c21 AR |
456 | [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0) |
457 | blue: (b/255.0) alpha: (a/255.0)] | |
edfda783 AR |
458 | atX: x y: y]; |
459 | } | |
460 | } | |
461 | ||
462 | - (void) setAlphaAtX: (int) x Y: (int) y to: (unsigned char) a | |
463 | { | |
464 | if (bmRep == nil) | |
465 | return; | |
466 | ||
467 | if (pixmapData[0] != NULL) | |
468 | { | |
469 | int loc = x + y * [self size].width; | |
470 | ||
471 | pixmapData[3][loc] = a; | |
472 | } | |
473 | else if (onTiger) | |
474 | { | |
475 | NSColor *color = [bmRep colorAtX: x y: y]; | |
476 | color = [color colorWithAlphaComponent: (a / 255.0)]; | |
477 | [bmRep setColor: color atX: x y: y]; | |
478 | } | |
479 | } | |
480 | ||
481 | /* returns a pattern color, which is cached here */ | |
482 | - (NSColor *)stippleMask | |
483 | { | |
484 | if (stippleMask == nil) | |
485 | stippleMask = [[NSColor colorWithPatternImage: self] retain]; | |
486 | return stippleMask; | |
487 | } | |
488 | ||
489 | @end | |
0ae1e5e5 | 490 | |
2f8e74bb | 491 | // arch-tag: 6b310280-6892-4e5e-8f34-41c4d384874f |