| 1 |
/* gdkimage-quartz.c |
| 2 |
* |
| 3 |
* Copyright (C) 2005 Imendio AB |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gdk.h" |
| 24 |
#include "gdkimage.h" |
| 25 |
#include "gdkprivate-quartz.h" |
| 26 |
|
| 27 |
static GObjectClass *parent_class; |
| 28 |
|
| 29 |
GdkImage * |
| 30 |
_gdk_quartz_image_copy_to_image (GdkDrawable *drawable, |
| 31 |
GdkImage *image, |
| 32 |
gint src_x, |
| 33 |
gint src_y, |
| 34 |
gint dest_x, |
| 35 |
gint dest_y, |
| 36 |
gint width, |
| 37 |
gint height) |
| 38 |
{ |
| 39 |
GdkScreen *screen; |
| 40 |
|
| 41 |
g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_QUARTZ (drawable), NULL); |
| 42 |
g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL); |
| 43 |
|
| 44 |
screen = gdk_drawable_get_screen (drawable); |
| 45 |
if (!image) |
| 46 |
image = _gdk_image_new_for_depth (screen, GDK_IMAGE_FASTEST, NULL, |
| 47 |
width, height, |
| 48 |
gdk_drawable_get_depth (drawable)); |
| 49 |
|
| 50 |
if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable)) |
| 51 |
{ |
| 52 |
GdkPixmapImplQuartz *pix_impl; |
| 53 |
gint bytes_per_row; |
| 54 |
guchar *data; |
| 55 |
int x, y; |
| 56 |
|
| 57 |
pix_impl = GDK_PIXMAP_IMPL_QUARTZ (drawable); |
| 58 |
data = (guchar *)(pix_impl->data); |
| 59 |
|
| 60 |
if (src_x + width > pix_impl->width || src_y + height > pix_impl->height) |
| 61 |
{ |
| 62 |
g_warning ("Out of bounds copy-area for pixmap -> image conversion\n"); |
| 63 |
return image; |
| 64 |
} |
| 65 |
|
| 66 |
switch (gdk_drawable_get_depth (drawable)) |
| 67 |
{ |
| 68 |
case 24: |
| 69 |
bytes_per_row = pix_impl->width * 4; |
| 70 |
for (y = 0; y < height; y++) |
| 71 |
{ |
| 72 |
guchar *src = data + ((y + src_y) * bytes_per_row) + (src_x * 4); |
| 73 |
|
| 74 |
for (x = 0; x < width; x++) |
| 75 |
{ |
| 76 |
gint32 pixel; |
| 77 |
|
| 78 |
/* RGB24, 4 bytes per pixel, skip first. */ |
| 79 |
pixel = src[0] << 16 | src[1] << 8 | src[2]; |
| 80 |
src += 4; |
| 81 |
|
| 82 |
gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel); |
| 83 |
} |
| 84 |
} |
| 85 |
break; |
| 86 |
|
| 87 |
case 32: |
| 88 |
bytes_per_row = pix_impl->width * 4; |
| 89 |
for (y = 0; y < height; y++) |
| 90 |
{ |
| 91 |
guchar *src = data + ((y + src_y) * bytes_per_row) + (src_x * 4); |
| 92 |
|
| 93 |
for (x = 0; x < width; x++) |
| 94 |
{ |
| 95 |
gint32 pixel; |
| 96 |
|
| 97 |
/* ARGB32, 4 bytes per pixel. */ |
| 98 |
pixel = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]; |
| 99 |
src += 4; |
| 100 |
|
| 101 |
gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel); |
| 102 |
} |
| 103 |
} |
| 104 |
break; |
| 105 |
|
| 106 |
case 1: /* TODO: optimize */ |
| 107 |
bytes_per_row = pix_impl->width; |
| 108 |
for (y = 0; y < height; y++) |
| 109 |
{ |
| 110 |
guchar *src = data + ((y + src_y) * bytes_per_row) + src_x; |
| 111 |
|
| 112 |
for (x = 0; x < width; x++) |
| 113 |
{ |
| 114 |
gint32 pixel; |
| 115 |
|
| 116 |
/* 8 bits */ |
| 117 |
pixel = src[0]; |
| 118 |
src++; |
| 119 |
|
| 120 |
gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel); |
| 121 |
} |
| 122 |
} |
| 123 |
break; |
| 124 |
|
| 125 |
default: |
| 126 |
g_warning ("Unsupported bit depth %d\n", gdk_drawable_get_depth (drawable)); |
| 127 |
return image; |
| 128 |
} |
| 129 |
} |
| 130 |
else if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable)) |
| 131 |
{ |
| 132 |
GdkQuartzView *view; |
| 133 |
NSBitmapImageRep *rep; |
| 134 |
guchar *data; |
| 135 |
int x, y; |
| 136 |
NSSize size; |
| 137 |
NSBitmapFormat format; |
| 138 |
gboolean has_alpha; |
| 139 |
gint bpp; |
| 140 |
gint r_byte = 0; |
| 141 |
gint g_byte = 1; |
| 142 |
gint b_byte = 2; |
| 143 |
gint a_byte = 3; |
| 144 |
gboolean le_image_data = FALSE; |
| 145 |
|
| 146 |
if (GDK_WINDOW_IMPL_QUARTZ (drawable) == GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (_gdk_root)->impl)) |
| 147 |
{ |
| 148 |
/* Special case for the root window. */ |
| 149 |
CGRect rect = CGRectMake (src_x, src_y, width, height); |
| 150 |
CGImageRef root_image_ref = CGWindowListCreateImage (rect, |
| 151 |
kCGWindowListOptionOnScreenOnly, |
| 152 |
kCGNullWindowID, |
| 153 |
kCGWindowImageDefault); |
| 154 |
|
| 155 |
/* HACK: the NSBitmapImageRep does not copy and convert |
| 156 |
* CGImageRef's data so it matches what NSBitmapImageRep can |
| 157 |
* express in its API (which is RGBA and ARGB, premultiplied |
| 158 |
* and unpremultiplied), it only references the CGImageRef. |
| 159 |
* Therefore we need to do the host byte swapping ourselves. |
| 160 |
*/ |
| 161 |
if (CGImageGetBitmapInfo (root_image_ref) & kCGBitmapByteOrder32Little) |
| 162 |
{ |
| 163 |
r_byte = 3; |
| 164 |
g_byte = 2; |
| 165 |
b_byte = 1; |
| 166 |
a_byte = 0; |
| 167 |
|
| 168 |
le_image_data = TRUE; |
| 169 |
} |
| 170 |
|
| 171 |
rep = [[NSBitmapImageRep alloc] initWithCGImage: root_image_ref]; |
| 172 |
CGImageRelease (root_image_ref); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
NSRect rect = NSMakeRect (src_x, src_y, width, height); |
| 177 |
view = GDK_WINDOW_IMPL_QUARTZ (drawable)->view; |
| 178 |
|
| 179 |
/* We return the image even if we can't copy to it. */ |
| 180 |
if (![view lockFocusIfCanDraw]) |
| 181 |
return image; |
| 182 |
|
| 183 |
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: rect]; |
| 184 |
[view unlockFocus]; |
| 185 |
} |
| 186 |
|
| 187 |
data = [rep bitmapData]; |
| 188 |
size = [rep size]; |
| 189 |
format = [rep bitmapFormat]; |
| 190 |
has_alpha = [rep hasAlpha]; |
| 191 |
bpp = [rep bitsPerPixel] / 8; |
| 192 |
|
| 193 |
/* MORE HACK: AlphaFirst seems set for le_image_data, which is |
| 194 |
* technically correct, but really apple, are you kidding, it's |
| 195 |
* in fact ABGR, not ARGB as promised in NSBitmapImageRep's API. |
| 196 |
*/ |
| 197 |
if (!le_image_data && (format & NSAlphaFirstBitmapFormat)) |
| 198 |
{ |
| 199 |
r_byte = 1; |
| 200 |
g_byte = 2; |
| 201 |
b_byte = 3; |
| 202 |
a_byte = 0; |
| 203 |
} |
| 204 |
|
| 205 |
for (y = 0; y < size.height; y++) |
| 206 |
{ |
| 207 |
guchar *src = data + y * [rep bytesPerRow]; |
| 208 |
|
| 209 |
for (x = 0; x < size.width; x++) |
| 210 |
{ |
| 211 |
guchar r = src[r_byte]; |
| 212 |
guchar g = src[g_byte]; |
| 213 |
guchar b = src[b_byte]; |
| 214 |
gint32 pixel; |
| 215 |
|
| 216 |
if (has_alpha) |
| 217 |
{ |
| 218 |
guchar alpha = src[a_byte]; |
| 219 |
|
| 220 |
/* unpremultiply if alpha > 0 */ |
| 221 |
if (! (format & NSAlphaNonpremultipliedBitmapFormat) && alpha) |
| 222 |
{ |
| 223 |
r = r * 255 / alpha; |
| 224 |
g = g * 255 / alpha; |
| 225 |
b = b * 255 / alpha; |
| 226 |
} |
| 227 |
|
| 228 |
if (image->byte_order == GDK_MSB_FIRST) |
| 229 |
pixel = alpha | b << 8 | g << 16 | r << 24; |
| 230 |
else |
| 231 |
pixel = alpha << 24 | b << 16 | g << 8 | r; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
if (image->byte_order == GDK_MSB_FIRST) |
| 236 |
pixel = b | g << 8 | r << 16; |
| 237 |
else |
| 238 |
pixel = b << 16 | g << 8 | r; |
| 239 |
} |
| 240 |
|
| 241 |
src += bpp; |
| 242 |
|
| 243 |
gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
[rep release]; |
| 248 |
} |
| 249 |
|
| 250 |
return image; |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
gdk_image_finalize (GObject *object) |
| 255 |
{ |
| 256 |
GdkImage *image = GDK_IMAGE (object); |
| 257 |
|
| 258 |
g_free (image->mem); |
| 259 |
|
| 260 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
gdk_image_class_init (GdkImageClass *klass) |
| 265 |
{ |
| 266 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 267 |
|
| 268 |
parent_class = g_type_class_peek_parent (klass); |
| 269 |
|
| 270 |
object_class->finalize = gdk_image_finalize; |
| 271 |
} |
| 272 |
|
| 273 |
GType |
| 274 |
gdk_image_get_type (void) |
| 275 |
{ |
| 276 |
static GType object_type = 0; |
| 277 |
|
| 278 |
if (!object_type) |
| 279 |
{ |
| 280 |
const GTypeInfo object_info = |
| 281 |
{ |
| 282 |
sizeof (GdkImageClass), |
| 283 |
(GBaseInitFunc) NULL, |
| 284 |
(GBaseFinalizeFunc) NULL, |
| 285 |
(GClassInitFunc) gdk_image_class_init, |
| 286 |
NULL, /* class_finalize */ |
| 287 |
NULL, /* class_data */ |
| 288 |
sizeof (GdkImage), |
| 289 |
0, /* n_preallocs */ |
| 290 |
(GInstanceInitFunc) NULL, |
| 291 |
}; |
| 292 |
|
| 293 |
object_type = g_type_register_static (G_TYPE_OBJECT, |
| 294 |
"GdkImage", |
| 295 |
&object_info, |
| 296 |
0); |
| 297 |
} |
| 298 |
|
| 299 |
return object_type; |
| 300 |
} |
| 301 |
|
| 302 |
GdkImage * |
| 303 |
gdk_image_new_bitmap (GdkVisual *visual, gpointer data, gint width, gint height) |
| 304 |
{ |
| 305 |
/* We don't implement this function because it's broken, deprecated and |
| 306 |
* tricky to implement. */ |
| 307 |
g_warning ("This function is unimplemented"); |
| 308 |
|
| 309 |
return NULL; |
| 310 |
} |
| 311 |
|
| 312 |
GdkImage* |
| 313 |
_gdk_image_new_for_depth (GdkScreen *screen, |
| 314 |
GdkImageType type, |
| 315 |
GdkVisual *visual, |
| 316 |
gint width, |
| 317 |
gint height, |
| 318 |
gint depth) |
| 319 |
{ |
| 320 |
GdkImage *image; |
| 321 |
|
| 322 |
if (visual) |
| 323 |
depth = visual->depth; |
| 324 |
|
| 325 |
g_assert (depth == 24 || depth == 32); |
| 326 |
|
| 327 |
image = g_object_new (gdk_image_get_type (), NULL); |
| 328 |
image->type = type; |
| 329 |
image->visual = visual; |
| 330 |
image->width = width; |
| 331 |
image->height = height; |
| 332 |
image->depth = depth; |
| 333 |
|
| 334 |
image->byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? GDK_LSB_FIRST : GDK_MSB_FIRST; |
| 335 |
|
| 336 |
/* We only support images with bpp 4 */ |
| 337 |
image->bpp = 4; |
| 338 |
image->bpl = image->width * image->bpp; |
| 339 |
image->bits_per_pixel = image->bpp * 8; |
| 340 |
|
| 341 |
image->mem = g_malloc (image->bpl * image->height); |
| 342 |
memset (image->mem, 0x00, image->bpl * image->height); |
| 343 |
|
| 344 |
return image; |
| 345 |
} |
| 346 |
|
| 347 |
guint32 |
| 348 |
gdk_image_get_pixel (GdkImage *image, |
| 349 |
gint x, |
| 350 |
gint y) |
| 351 |
{ |
| 352 |
guchar *ptr; |
| 353 |
|
| 354 |
g_return_val_if_fail (image != NULL, 0); |
| 355 |
g_return_val_if_fail (x >= 0 && x < image->width, 0); |
| 356 |
g_return_val_if_fail (y >= 0 && y < image->height, 0); |
| 357 |
|
| 358 |
ptr = image->mem + y * image->bpl + x * image->bpp; |
| 359 |
|
| 360 |
return *(guint32 *)ptr; |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
gdk_image_put_pixel (GdkImage *image, |
| 365 |
gint x, |
| 366 |
gint y, |
| 367 |
guint32 pixel) |
| 368 |
{ |
| 369 |
guchar *ptr; |
| 370 |
|
| 371 |
ptr = image->mem + y * image->bpl + x * image->bpp; |
| 372 |
|
| 373 |
*(guint32 *)ptr = pixel; |
| 374 |
} |
| 375 |
|
| 376 |
gint |
| 377 |
_gdk_windowing_get_bits_for_depth (GdkDisplay *display, |
| 378 |
gint depth) |
| 379 |
{ |
| 380 |
if (depth == 24 || depth == 32) |
| 381 |
return 32; |
| 382 |
else |
| 383 |
g_assert_not_reached (); |
| 384 |
|
| 385 |
return 0; |
| 386 |
} |