3 * Copyright (C) 2005 Imendio AB
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.
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.
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.
25 #include "gdkprivate-quartz.h"
27 static GObjectClass *parent_class;
30 _gdk_quartz_image_copy_to_image (GdkDrawable *drawable,
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);
44 screen = gdk_drawable_get_screen (drawable);
46 image = _gdk_image_new_for_depth (screen, GDK_IMAGE_FASTEST, NULL,
48 gdk_drawable_get_depth (drawable));
50 if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable))
52 GdkPixmapImplQuartz *pix_impl;
57 pix_impl = GDK_PIXMAP_IMPL_QUARTZ (drawable);
58 data = (guchar *)(pix_impl->data);
60 if (src_x + width > pix_impl->width || src_y + height > pix_impl->height)
62 g_warning ("Out of bounds copy-area for pixmap -> image conversion\n");
66 switch (gdk_drawable_get_depth (drawable))
69 bytes_per_row = pix_impl->width * 4;
70 for (y = 0; y < height; y++)
72 guchar *src = data + ((y + src_y) * bytes_per_row) + (src_x * 4);
74 for (x = 0; x < width; x++)
78 /* RGB24, 4 bytes per pixel, skip first. */
79 pixel = src[0] << 16 | src[1] << 8 | src[2];
82 gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel);
88 bytes_per_row = pix_impl->width * 4;
89 for (y = 0; y < height; y++)
91 guchar *src = data + ((y + src_y) * bytes_per_row) + (src_x * 4);
93 for (x = 0; x < width; x++)
97 /* ARGB32, 4 bytes per pixel. */
98 pixel = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
101 gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel);
106 case 1: /* TODO: optimize */
107 bytes_per_row = pix_impl->width;
108 for (y = 0; y < height; y++)
110 guchar *src = data + ((y + src_y) * bytes_per_row) + src_x;
112 for (x = 0; x < width; x++)
120 gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel);
126 g_warning ("Unsupported bit depth %d\n", gdk_drawable_get_depth (drawable));
130 else if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
133 NSBitmapImageRep *rep;
139 view = GDK_WINDOW_IMPL_QUARTZ (drawable)->view;
141 /* We return the image even if we can't copy to it. */
142 if (![view lockFocusIfCanDraw])
145 rect = NSMakeRect (src_x, src_y, width, height);
147 rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: rect];
150 data = [rep bitmapData];
153 for (y = 0; y < size.height; y++)
155 guchar *src = data + y * [rep bytesPerRow];
157 for (x = 0; x < size.width; x++)
161 if (image->byte_order == GDK_LSB_FIRST)
162 pixel = src[0] << 8 | src[1] << 16 |src[2] << 24;
164 pixel = src[0] << 16 | src[1] << 8 |src[2];
168 gdk_image_put_pixel (image, dest_x + x, dest_y + y, pixel);
179 gdk_image_finalize (GObject *object)
181 GdkImage *image = GDK_IMAGE (object);
185 G_OBJECT_CLASS (parent_class)->finalize (object);
189 gdk_image_class_init (GdkImageClass *klass)
191 GObjectClass *object_class = G_OBJECT_CLASS (klass);
193 parent_class = g_type_class_peek_parent (klass);
195 object_class->finalize = gdk_image_finalize;
199 gdk_image_get_type (void)
201 static GType object_type = 0;
205 static const GTypeInfo object_info =
207 sizeof (GdkImageClass),
208 (GBaseInitFunc) NULL,
209 (GBaseFinalizeFunc) NULL,
210 (GClassInitFunc) gdk_image_class_init,
211 NULL, /* class_finalize */
212 NULL, /* class_data */
215 (GInstanceInitFunc) NULL,
218 object_type = g_type_register_static (G_TYPE_OBJECT,
228 gdk_image_new_bitmap (GdkVisual *visual, gpointer data, gint width, gint height)
230 /* We don't implement this function because it's broken, deprecated and
231 * tricky to implement. */
232 g_warning ("This function is unimplemented");
238 _gdk_image_new_for_depth (GdkScreen *screen,
248 depth = visual->depth;
250 g_assert (depth == 24 || depth == 32);
252 image = g_object_new (gdk_image_get_type (), NULL);
254 image->visual = visual;
255 image->width = width;
256 image->height = height;
257 image->depth = depth;
259 image->byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
261 /* We only support images with bpp 4 */
263 image->bpl = image->width * image->bpp;
264 image->bits_per_pixel = image->bpp * 8;
266 image->mem = g_malloc (image->bpl * image->height);
267 memset (image->mem, 0x00, image->bpl * image->height);
273 gdk_image_get_pixel (GdkImage *image,
279 g_return_val_if_fail (image != NULL, 0);
280 g_return_val_if_fail (x >= 0 && x < image->width, 0);
281 g_return_val_if_fail (y >= 0 && y < image->height, 0);
283 ptr = image->mem + y * image->bpl + x * image->bpp;
285 return *(guint32 *)ptr;
289 gdk_image_put_pixel (GdkImage *image,
296 ptr = image->mem + y * image->bpl + x * image->bpp;
298 *(guint32 *)ptr = pixel;
302 _gdk_windowing_get_bits_for_depth (GdkDisplay *display,
305 if (depth == 24 || depth == 32)
308 g_assert_not_reached ();