1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gdkpixmap.h"
29 #include "gdkprivate-win32.h"
31 static GList *image_list = NULL;
32 static gpointer parent_class = NULL;
34 static void gdk_win32_image_destroy (GdkImage *image);
35 static void gdk_image_init (GdkImage *image);
36 static void gdk_image_class_init (GdkImageClass *klass);
37 static void gdk_image_finalize (GObject *object);
40 gdk_image_get_type (void)
42 static GType object_type = 0;
46 static const GTypeInfo object_info =
48 sizeof (GdkImageClass),
50 (GBaseFinalizeFunc) NULL,
51 (GClassInitFunc) gdk_image_class_init,
52 NULL, /* class_finalize */
53 NULL, /* class_data */
56 (GInstanceInitFunc) gdk_image_init,
59 object_type = g_type_register_static (G_TYPE_OBJECT,
68 gdk_image_init (GdkImage *image)
70 image->windowing_data = g_new0 (GdkImagePrivateWin32, 1);
74 gdk_image_class_init (GdkImageClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 parent_class = g_type_class_peek_parent (klass);
80 object_class->finalize = gdk_image_finalize;
84 gdk_image_finalize (GObject *object)
86 GdkImage *image = GDK_IMAGE (object);
88 gdk_win32_image_destroy (image);
90 G_OBJECT_CLASS (parent_class)->finalize (object);
94 _gdk_image_exit (void)
100 image = image_list->data;
101 gdk_win32_image_destroy (image);
106 gdk_image_new_bitmap (GdkVisual *visual,
111 * Desc: create a new bitmap image
116 GdkImagePrivateWin32 *private;
118 BITMAPINFOHEADER bmiHeader;
121 RGBQUAD bmiColors[2];
125 int bpl = (w-1)/8 + 1;
126 int bpl32 = ((w-1)/32 + 1)*4;
128 image = g_object_new (gdk_image_get_type (), NULL);
129 private = IMAGE_PRIVATE_DATA (image);
131 image->type = GDK_IMAGE_SHARED;
132 image->visual = visual;
136 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
138 GDK_NOTE (MISC, g_print ("gdk_image_new_bitmap: %dx%d\n", w, h));
140 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
141 bmi.bmiHeader.biWidth = w;
142 bmi.bmiHeader.biHeight = -h;
143 bmi.bmiHeader.biPlanes = 1;
144 bmi.bmiHeader.biBitCount = 1;
145 bmi.bmiHeader.biCompression = BI_RGB;
146 bmi.bmiHeader.biSizeImage = 0;
147 bmi.bmiHeader.biXPelsPerMeter =
148 bmi.bmiHeader.biYPelsPerMeter = 0;
149 bmi.bmiHeader.biClrUsed = 0;
150 bmi.bmiHeader.biClrImportant = 0;
152 bmi.u.bmiColors[0].rgbBlue =
153 bmi.u.bmiColors[0].rgbGreen =
154 bmi.u.bmiColors[0].rgbRed = 0x00;
155 bmi.u.bmiColors[0].rgbReserved = 0x00;
157 bmi.u.bmiColors[1].rgbBlue =
158 bmi.u.bmiColors[1].rgbGreen =
159 bmi.u.bmiColors[1].rgbRed = 0xFF;
160 bmi.u.bmiColors[1].rgbReserved = 0x00;
162 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
163 DIB_RGB_COLORS, &bits, NULL, 0);
166 /* Win32 expects scanlines in DIBs to be 32 bit aligned */
168 for (i = 0; i < h; i++)
169 memmove (bits + i*bpl32, ((char *) data) + i*bpl, bpl);
172 memmove (bits, data, bpl*h);
175 image->byte_order = GDK_MSB_FIRST;
177 image->bits_per_pixel = 1;
180 /* free data right now, in contrast to the X11 version we have made
181 * our own copy. Use free, it was malloc()ed.
185 } /* gdk_image_new_bitmap() */
188 _gdk_windowing_image_init (void)
190 /* Nothing needed AFAIK */
194 _gdk_image_new_for_depth (GdkImageType type,
201 GdkImagePrivateWin32 *private;
202 Visual *xvisual = NULL;
204 BITMAPINFOHEADER bmiHeader;
206 WORD bmiIndices[256];
208 RGBQUAD bmiColors[256];
214 g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
215 g_return_val_if_fail (visual || depth != -1, NULL);
218 depth = visual->depth;
220 if (type == GDK_IMAGE_FASTEST || type == GDK_IMAGE_NORMAL)
221 type = GDK_IMAGE_SHARED;
223 GDK_NOTE (MISC, g_print ("gdk_image_new: %dx%d %s\n",
225 (type == GDK_IMAGE_SHARED ? "shared" :
228 image = g_object_new (gdk_image_get_type (), NULL);
229 private = IMAGE_PRIVATE_DATA (image);
232 image->visual = visual;
233 image->width = width;
234 image->height = height;
235 image->depth = depth;
238 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
240 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
241 bmi.bmiHeader.biWidth = width;
242 bmi.bmiHeader.biHeight = -height;
243 bmi.bmiHeader.biPlanes = 1;
244 if (image->depth == 15)
245 bmi.bmiHeader.biBitCount = 16;
247 bmi.bmiHeader.biBitCount = image->depth;
248 if (image->depth == 16)
249 bmi.bmiHeader.biCompression = BI_BITFIELDS;
251 bmi.bmiHeader.biCompression = BI_RGB;
252 bmi.bmiHeader.biSizeImage = 0;
253 bmi.bmiHeader.biXPelsPerMeter =
254 bmi.bmiHeader.biYPelsPerMeter = 0;
255 bmi.bmiHeader.biClrUsed = 0;
256 bmi.bmiHeader.biClrImportant = 0;
258 if ( (image->visual && image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
259 || (8 == image->depth))
261 iUsage = DIB_PAL_COLORS;
262 for (i = 0; i < 256; i++)
263 bmi.u.bmiIndices[i] = i;
267 iUsage = DIB_RGB_COLORS;
268 if (image->depth == 1)
270 bmi.u.bmiColors[0].rgbBlue =
271 bmi.u.bmiColors[0].rgbGreen =
272 bmi.u.bmiColors[0].rgbRed = 0x00;
273 bmi.u.bmiColors[0].rgbReserved = 0x00;
275 bmi.u.bmiColors[1].rgbBlue =
276 bmi.u.bmiColors[1].rgbGreen =
277 bmi.u.bmiColors[1].rgbRed = 0xFF;
278 bmi.u.bmiColors[1].rgbReserved = 0x00;
281 else if (image->depth == 16)
283 bmi.u.bmiMasks[0] = visual ? visual->red_mask : 0x0000F800;
284 bmi.u.bmiMasks[1] = visual ? visual->green_mask : 0x000007E0;
285 bmi.u.bmiMasks[2] = visual ? visual->blue_mask : 0x0000001F;
289 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
290 iUsage, &image->mem, NULL, 0);
292 if (private->hbitmap == NULL)
294 WIN32_GDI_FAILED ("CreateDIBSection");
299 switch (image->depth)
316 g_warning ("gdk_image_new: depth = %d", image->depth);
317 g_assert_not_reached ();
319 image->bits_per_pixel = image->depth;
320 image->byte_order = GDK_LSB_FIRST;
321 if (image->depth == 1)
322 image->bpl = ((width-1)/32 + 1)*4;
324 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
326 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
327 (guint) private->hbitmap, image->mem, image->bpl));
333 gdk_image_new (GdkImageType type,
338 return _gdk_image_new_for_depth (type, visual, width, height, -1);
343 _gdk_win32_get_image (GdkDrawable *drawable,
350 GdkImagePrivateWin32 *private;
351 GdkDrawableImplWin32 *impl;
354 BITMAPINFOHEADER bmiHeader;
356 WORD bmiIndices[256];
358 RGBQUAD bmiColors[256];
361 HGDIOBJ oldbitmap1 = NULL, oldbitmap2;
366 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
368 GDK_NOTE (MISC, g_print ("_gdk_win32_get_image: %#x %dx%d@+%d+%d\n",
369 (guint) GDK_DRAWABLE_IMPL_WIN32 (drawable)->handle,
370 width, height, x, y));
372 impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
374 image = g_object_new (gdk_image_get_type (), NULL);
375 private = IMAGE_PRIVATE_DATA (image);
377 image->type = GDK_IMAGE_SHARED;
378 image->visual = gdk_drawable_get_visual (drawable);
379 image->width = width;
380 image->height = height;
382 /* This function is called both to blit from a window and from
385 if (GDK_IS_PIXMAP_IMPL_WIN32 (drawable))
387 if ((hdc = CreateCompatibleDC (NULL)) == NULL)
389 WIN32_GDI_FAILED ("CreateCompatibleDC");
393 if ((oldbitmap1 = SelectObject (hdc, impl->handle)) == NULL)
395 WIN32_GDI_FAILED ("SelectObject");
400 GetObject (impl->handle, sizeof (BITMAP), &bm);
402 g_print ("gdk_image_get: bmWidth:%ld bmHeight:%ld bmWidthBytes:%ld bmBitsPixel:%d\n",
403 bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, bm.bmBitsPixel));
404 image->depth = bm.bmBitsPixel;
405 if (image->depth <= 8)
407 iUsage = DIB_PAL_COLORS;
408 for (i = 0; i < 256; i++)
409 bmi.u.bmiIndices[i] = i;
412 iUsage = DIB_RGB_COLORS;
416 if ((hdc = GetDC (impl->handle)) == NULL)
418 WIN32_GDI_FAILED ("GetDC");
422 image->depth = gdk_visual_get_system ()->depth;
423 if (image->visual && image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
425 iUsage = DIB_PAL_COLORS;
426 for (i = 0; i < 256; i++)
427 bmi.u.bmiIndices[i] = i;
430 iUsage = DIB_RGB_COLORS;
433 if ((memdc = CreateCompatibleDC (hdc)) == NULL)
435 WIN32_GDI_FAILED ("CreateCompatibleDC");
436 if (GDK_IS_PIXMAP_IMPL_WIN32 (drawable))
438 SelectObject (hdc, oldbitmap1);
440 WIN32_GDI_FAILED ("DeleteDC");
444 if (!ReleaseDC (impl->handle, hdc))
445 WIN32_GDI_FAILED ("ReleaseDC");
451 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
452 bmi.bmiHeader.biWidth = width;
453 bmi.bmiHeader.biHeight = -height;
454 bmi.bmiHeader.biPlanes = 1;
455 bmi.bmiHeader.biBitCount = image->depth;
456 if (image->depth == 16)
458 bmi.bmiHeader.biCompression = BI_BITFIELDS;
459 if (image->visual == NULL)
461 /* XXX ??? Is it always this if depth==16 and a pixmap? Guess so. */
462 bmi.u.bmiMasks[0] = 0xf800;
463 bmi.u.bmiMasks[1] = 0x07e0;
464 bmi.u.bmiMasks[2] = 0x001f;
468 bmi.u.bmiMasks[0] = image->visual->red_mask;
469 bmi.u.bmiMasks[1] = image->visual->green_mask;
470 bmi.u.bmiMasks[2] = image->visual->blue_mask;
474 bmi.bmiHeader.biCompression = BI_RGB;
475 bmi.bmiHeader.biSizeImage = 0;
476 bmi.bmiHeader.biXPelsPerMeter =
477 bmi.bmiHeader.biYPelsPerMeter = 0;
478 bmi.bmiHeader.biClrUsed = 0;
479 bmi.bmiHeader.biClrImportant = 0;
481 if ((private->hbitmap = CreateDIBSection (hdc, (BITMAPINFO *) &bmi, iUsage,
482 &image->mem, NULL, 0)) == NULL)
484 WIN32_GDI_FAILED ("CreateDIBSection");
486 if (GDK_IS_PIXMAP (drawable))
488 SelectObject (hdc, oldbitmap1);
493 ReleaseDC (impl->handle, hdc);
499 if ((oldbitmap2 = SelectObject (memdc, private->hbitmap)) == NULL)
501 WIN32_GDI_FAILED ("SelectObject");
502 DeleteObject (private->hbitmap);
504 if (GDK_IS_PIXMAP (drawable))
506 SelectObject (hdc, oldbitmap1);
511 ReleaseDC (impl->handle, hdc);
517 if (!BitBlt (memdc, 0, 0, width, height, hdc, x, y, SRCCOPY))
519 WIN32_GDI_FAILED ("BitBlt");
520 SelectObject (memdc, oldbitmap2);
521 DeleteObject (private->hbitmap);
523 if (GDK_IS_PIXMAP (drawable))
525 SelectObject (hdc, oldbitmap1);
530 ReleaseDC (impl->handle, hdc);
536 if (SelectObject (memdc, oldbitmap2) == NULL)
537 WIN32_GDI_FAILED ("SelectObject");
539 if (!DeleteDC (memdc))
540 WIN32_GDI_FAILED ("DeleteDC");
542 if (GDK_IS_PIXMAP_IMPL_WIN32 (drawable))
544 SelectObject (hdc, oldbitmap1);
549 ReleaseDC (impl->handle, hdc);
552 switch (image->depth)
569 g_warning ("_gdk_win32_get_image: image->depth = %d", image->depth);
570 g_assert_not_reached ();
572 image->bits_per_pixel = image->depth;
573 image->byte_order = GDK_LSB_FIRST;
574 if (image->depth == 1)
575 image->bpl = ((width - 1)/32 + 1)*4;
577 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
579 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
580 (guint) private->hbitmap, image->mem, image->bpl));
586 _gdk_win32_copy_to_image (GdkDrawable *drawable,
595 GdkImagePrivateWin32 *private;
596 GdkDrawableImplWin32 *impl;
599 gboolean ok = TRUE, is_window_impl;
600 HBITMAP holdbmp = NULL;
601 HDC src_dc = NULL, dest_dc = NULL;
603 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
604 g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL);
606 visual = gdk_drawable_get_visual (drawable);
607 impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
608 is_window_impl = GDK_IS_WINDOW_IMPL_WIN32 (drawable);
610 if (!image && !is_window_impl)
611 return _gdk_win32_get_image (drawable, src_x, src_y, width, height);
614 image = _gdk_image_new_for_depth (GDK_IMAGE_FASTEST, visual, width, height, -1);
616 private = IMAGE_PRIVATE_DATA (image);
619 src_dc = GetDC (impl->handle);
622 gc = gdk_gc_new (drawable);
623 src_dc = gdk_win32_hdc_get (drawable, gc, 0);
629 dest_dc = CreateCompatibleDC (NULL);
635 holdbmp = SelectObject (dest_dc, private->hbitmap);
639 if (ok && !BitBlt (dest_dc, dest_x, dest_y, width, height,
640 src_dc, src_x, src_y, SRCCOPY))
641 WIN32_GDI_FAILED ("BitBlt");
645 if (src_dc && !ReleaseDC (impl->handle, src_dc))
646 WIN32_GDI_FAILED ("ReleaseDC");
650 gdk_win32_hdc_release (drawable, gc, 0);
657 SelectObject (dest_dc, holdbmp);
658 if (!DeleteDC (dest_dc))
659 WIN32_GDI_FAILED ("DeleteDC");
661 g_return_val_if_fail (ok, image);
667 gdk_image_get_pixel (GdkImage *image,
673 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
675 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
678 if (image->depth == 1)
679 pixel = (((char *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
682 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
690 /* Windows is always LSB, no need to check image->byte_order. */
692 pixel = pixelp[0] | (pixelp[1] << 8);
696 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
700 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
704 g_warning ("gdk_image_get_pixel(): bpp = %d", image->bpp);
705 g_assert_not_reached ();
713 gdk_image_put_pixel (GdkImage *image,
718 g_return_if_fail (image != NULL);
720 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
723 if (image->depth == 1)
725 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
727 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
730 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
732 /* Windows is always LSB, no need to check image->byte_order. */
738 pixelp[2] = ((pixel >> 16) & 0xFF);
740 pixelp[1] = ((pixel >> 8) & 0xFF);
742 pixelp[0] = (pixel & 0xFF);
748 gdk_win32_image_destroy (GdkImage *image)
750 GdkImagePrivateWin32 *private;
752 g_return_if_fail (GDK_IS_IMAGE (image));
754 private = IMAGE_PRIVATE_DATA (image);
756 if (private == NULL) /* This means that _gdk_image_exit() destroyed the
757 * image already, and now we're called a second
758 * time from _finalize()
762 GDK_NOTE (MISC, g_print ("gdk_win32_image_destroy: %#x\n",
763 (guint) private->hbitmap));
767 case GDK_IMAGE_SHARED:
768 if (!DeleteObject (private->hbitmap))
769 WIN32_GDI_FAILED ("DeleteObject");
773 g_assert_not_reached ();
777 image->windowing_data = NULL;
781 _gdk_windowing_get_bits_for_depth (gint depth)
783 if ((1 == depth) || (8 == depth) || (16 == depth) ||
784 (24 == depth) || (32 == depth))
786 else if (15 == depth)
789 g_assert_not_reached ();