1 /* Image support for the NeXT/Open/GNUstep and MacOSX window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 2005, 2006, 2008,
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
32 #include "dispextern.h"
36 extern Lisp_Object QCfile
, QCdata
;
40 int image_trace_num
= 0;
41 #define NSTRACE
(x
) fprintf (stderr
, "
%s:%d: [%d] " #x "\n", \
42 __FILE__
, __LINE__
, ++image_trace_num
)
48 /* ==========================================================================
50 C interface. This allows easy calling from C files. We could just
51 compile everything as Objective-C, but that might mean slower
52 compilation and possible difficulties on some platforms..
54 ========================================================================== */
57 ns_image_from_XBM
(unsigned
char *bits
, int width
, int height
)
59 NSTRACE
(ns_image_from_XBM
);
60 return [[EmacsImage alloc
] initFromXBM
: bits
61 width
: width height
: height
66 ns_image_for_XPM
(int width
, int height
, int depth
)
68 NSTRACE
(ns_image_for_XPM
);
69 return [[EmacsImage alloc
] initForXPMWithDepth
: depth
70 width
: width height
: height
];
74 ns_image_from_file
(Lisp_Object file
)
76 NSTRACE
(ns_image_from_bitmap_file
);
77 return [EmacsImage allocInitFromFile
: file
];
81 ns_load_image
(struct frame
*f
, struct image *img
,
82 Lisp_Object spec_file
, Lisp_Object spec_data
)
84 NSTRACE
(ns_load_image
);
91 eImg
= [EmacsImage allocInitFromFile
: spec_file
];
95 NSData
*data
= [NSData dataWithBytes
: XSTRING
(spec_data
)->data
96 length: SBYTES
(spec_data
)];
97 eImg
= [[EmacsImage alloc
] initWithData
: data
];
103 add_to_log
("Unable to
load image %s", img->spec, Qnil);
108 img
->width
= size.width
;
109 img
->height
= size.height
;
111 /* 4) set img->pixmap = emacsimage */
118 ns_image_width
(void
*img
)
120 return [(id
)img
size].width
;
124 ns_image_height
(void
*img
)
126 return [(id
)img
size].height
;
130 ns_get_pixel
(void
*img
, int x
, int y
)
132 return [(EmacsImage
*)img getPixelAtX
: x Y
: y
];
136 ns_put_pixel
(void
*img
, int x
, int y
, unsigned
long argb
)
138 unsigned
char alpha
= (argb
>> 24) & 0xFF;
141 [(EmacsImage
*)img setPixelAtX
: x Y
: y toRed
: (argb
>> 16) & 0xFF
142 green
: (argb
>> 8) & 0xFF blue
: (argb
& 0xFF) alpha
: alpha
];
146 ns_set_alpha
(void
*img
, int x
, int y
, unsigned
char a
)
148 [(EmacsImage
*)img setAlphaAtX
: x Y
: y to
: a
];
152 /* ==========================================================================
154 Class supporting bitmaps and images of various sorts.
156 ========================================================================== */
158 @implementation EmacsImage
160 static EmacsImage
*ImageList
= nil
;
162 + allocInitFromFile
: (Lisp_Object
)file
164 EmacsImage
*image = ImageList
;
167 /* look for an existing image of the same name */
168 while (image != nil
&&
169 [[image name
] compare
: [NSString stringWithUTF8String
: SDATA
(file
)]]
171 image = [image imageListNext
];
179 /* Search bitmap-file-path for the file, if appropriate. */
180 found
= x_find_image_file
(file
);
181 if (!STRINGP
(found
))
184 image = [[EmacsImage alloc
] initByReferencingFile
:
185 [NSString stringWithUTF8String
: SDATA
(found
)]];
187 if ([image bestRepresentationForDevice
: nil
] == nil
)
193 [image setName
: [NSString stringWithUTF8String
: SDATA
(file
)]];
195 ImageList
= [image imageListSetNext
: ImageList
];
208 - imageListSetNext
: (id
)arg
217 return imageListNext
;
231 [stippleMask release
];
234 ImageList
= imageListNext
;
237 while (list
!= nil
&& [list imageListNext
] != self
)
238 list
= [list imageListNext
];
239 [list imageListSetNext
: imageListNext
];
246 - initFromXBM
: (unsigned
char *)bits width
: (int
)w height
: (int
)h
249 return [self initFromSkipXBM
: bits width
: w height
: h flip
: flip
length: 0];
253 - initFromSkipXBM
: (unsigned
char *)bits width
: (int
)w height
: (int
)h
254 flip
: (BOOL
)flip
length: (int
)length;
256 int bpr
= (w
+ 7) / 8;
257 unsigned
char *planes
[5];
259 [self initWithSize
: NSMakeSize
(w
, h
)];
261 bmRep
= [[NSBitmapImageRep alloc
] initWithBitmapDataPlanes
: NULL
262 pixelsWide
: w pixelsHigh
: h
263 bitsPerSample
: 8 samplesPerPixel
: 4
264 hasAlpha
: YES isPlanar
: YES
265 colorSpaceName
: NSCalibratedRGBColorSpace
266 bytesPerRow
: w bitsPerPixel
: 0];
268 [bmRep getBitmapDataPlanes
: planes
];
270 /* pull bits out to set the (bytewise) alpha mask */
272 unsigned
char *s
= bits
;
273 unsigned
char *alpha
= planes
[3];
274 unsigned
char swt
[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
276 unsigned
char c
, bitPat
;
278 for (j
= 0; j
< h
; j
++)
279 for (i
= 0; i
< bpr
; i
++)
283 unsigned
char s1
, s2
;
284 while (*s
++ != 'x' && s
< bits
+ length);
285 if (s
>= bits
+ length)
290 #define hexchar
(x
) (isdigit
(x
) ? x
- '0' : x
- 'a' + 10)
293 c
= hexchar
(s1
) * 0x10 + hexchar
(s2
);
298 bitPat
= flip ? swt
[c
>> 4] |
(swt
[c
& 0xf] << 4) : c ^
255;
301 *alpha
++ = (bitPat
& 0x80) ?
0xff : 0;
307 [self addRepresentation
: bmRep
];
309 bzero
(planes
[0], w
*h
);
310 bzero
(planes
[1], w
*h
);
311 bzero
(planes
[2], w
*h
);
312 [self setXBMColor
: [NSColor blackColor
]];
317 /* Set color for a bitmap image (see initFromSkipXBM). Note that the alpha
318 is used as a mask, so we just memset the entire array. */
319 - setXBMColor
: (NSColor
*)color
321 NSSize s
= [self
size];
322 int len
= (int
) s.width
* s.height
;
323 unsigned
char *planes
[5];
327 if (bmRep
== nil || color
== nil
)
330 if ([color colorSpaceName
] != NSCalibratedRGBColorSpace
)
331 rgbColor
= [color colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
335 [rgbColor getRed
: &r green
: &g blue
: &b alpha
: &a
];
337 [bmRep getBitmapDataPlanes
: planes
];
339 /* we used to just do this, but Cocoa seems to have a bug when rendering
340 an alpha-masked image onto a dark background where it bloats the mask */
341 /* memset (planes[0..2], r, g, b*0xff, len); */
343 int i
, len
= s.width
*s.height
;
344 int rr
= r
* 0xff, gg
= g
* 0xff, bb
= b
* 0xff;
345 for (i
=0; i
<len
; i
++)
346 if (planes
[3][i
] != 0)
356 - initForXPMWithDepth
: (int
)depth width
: (int
)width height
: (int
)height
358 NSSize s
= {width
, height
};
361 [self initWithSize
: s
];
363 bmRep
= [[NSBitmapImageRep alloc
] initWithBitmapDataPlanes
: NULL
364 pixelsWide
: width pixelsHigh
: height
365 /* keep things simple for now */
366 bitsPerSample
: 8 samplesPerPixel
: 4 /*RGB+A*/
367 hasAlpha
: YES isPlanar
: YES
368 colorSpaceName
: NSCalibratedRGBColorSpace
369 bytesPerRow
: width bitsPerPixel
: 0];
371 [bmRep getBitmapDataPlanes
: pixmapData
];
373 bzero
(pixmapData
[i
], width
*height
);
374 [self addRepresentation
: bmRep
];
379 /* attempt to pull out pixmap data from a BitmapImageRep; returns NO if fails */
380 - (void
) setPixmapData
385 reps
= [[self representations
] objectEnumerator
];
386 while (rep
= (NSImageRep
*) [reps nextObject
])
388 if ([rep respondsToSelector
: @selector
(getBitmapDataPlanes
:)])
390 bmRep
= (NSBitmapImageRep
*) rep
;
391 onTiger
= [bmRep respondsToSelector
: @selector
(colorAtX
:y
:)];
393 if ([bmRep numberOfPlanes
] >= 3)
394 [bmRep getBitmapDataPlanes
: pixmapData
];
401 /* note; this and next work only for image created with initForXPMWithDepth,
402 initFromSkipXBM, or where setPixmapData was called successfully */
404 - (unsigned
long) getPixelAtX
: (int
)x Y
: (int
)y
409 /* this method is faster but won't work for bitmaps */
410 if (pixmapData
[0] != NULL)
412 int loc
= x
+ y
* [self
size].width
;
413 return (pixmapData
[3][loc
] << 24) /* alpha */
414 |
(pixmapData
[0][loc
] << 16) |
(pixmapData
[1][loc
] << 8)
415 |
(pixmapData
[2][loc
]);
419 NSColor
*color
= [bmRep colorAtX
: x y
: y
];
421 [color getRed
: &r green
: &g blue
: &b alpha
: &a
];
422 return ((int
)(a
* 255.0) << 24)
423 |
((int
)(r
* 255.0) << 16) |
((int
)(g
* 255.0) << 8)
424 |
((int
)(b
* 255.0));
430 - (void
) setPixelAtX
: (int
)x Y
: (int
)y toRed
: (unsigned
char)r
431 green
: (unsigned
char)g blue
: (unsigned
char)b
432 alpha
:(unsigned
char)a
;
437 if (pixmapData
[0] != NULL)
439 int loc
= x
+ y
* [self
size].width
;
440 pixmapData
[0][loc
] = r
;
441 pixmapData
[1][loc
] = g
;
442 pixmapData
[2][loc
] = b
;
443 pixmapData
[3][loc
] = a
;
448 [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: a
]
453 - (void
) setAlphaAtX
: (int
) x Y
: (int
) y to
: (unsigned
char) a
458 if (pixmapData
[0] != NULL)
460 int loc
= x
+ y
* [self
size].width
;
462 pixmapData
[3][loc
] = a
;
466 NSColor
*color
= [bmRep colorAtX
: x y
: y
];
467 color
= [color colorWithAlphaComponent
: (a
/ 255.0)];
468 [bmRep setColor
: color atX
: x y
: y
];
472 /* returns a pattern color, which is cached here */
473 - (NSColor
*)stippleMask
475 if (stippleMask
== nil
)
476 stippleMask
= [[NSColor colorWithPatternImage
: self
] retain
];