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/.
30 #include "gdkpixmap.h"
31 #include "gdkinternals.h"
32 #include "gdkprivate-win32.h"
33 #include "gdkdrawable-win32.h"
34 #include "gdkwindow-win32.h"
35 #include "gdkpixmap-win32.h"
37 static GList *image_list = NULL;
38 static gpointer parent_class = NULL;
40 static void gdk_win32_image_destroy (GdkImage *image);
41 static void gdk_image_init (GdkImage *image);
42 static void gdk_image_class_init (GdkImageClass *klass);
43 static void gdk_image_finalize (GObject *object);
45 #define PRIVATE_DATA(image) ((GdkImagePrivateWin32 *) GDK_IMAGE (image)->windowing_data)
48 gdk_image_get_type (void)
50 static GType object_type = 0;
54 static const GTypeInfo object_info =
56 sizeof (GdkImageClass),
58 (GBaseFinalizeFunc) NULL,
59 (GClassInitFunc) gdk_image_class_init,
60 NULL, /* class_finalize */
61 NULL, /* class_data */
64 (GInstanceInitFunc) gdk_image_init,
67 object_type = g_type_register_static (G_TYPE_OBJECT,
76 gdk_image_init (GdkImage *image)
78 image->windowing_data = g_new0 (GdkImagePrivateWin32, 1);
82 gdk_image_class_init (GdkImageClass *klass)
84 GObjectClass *object_class = G_OBJECT_CLASS (klass);
86 parent_class = g_type_class_peek_parent (klass);
88 object_class->finalize = gdk_image_finalize;
92 gdk_image_finalize (GObject *object)
94 GdkImage *image = GDK_IMAGE (object);
96 gdk_win32_image_destroy (image);
98 G_OBJECT_CLASS (parent_class)->finalize (object);
102 gdk_image_exit (void)
108 image = image_list->data;
109 gdk_win32_image_destroy (image);
114 gdk_image_new_bitmap (GdkVisual *visual,
119 * Desc: create a new bitmap image
124 GdkImagePrivateWin32 *private;
126 BITMAPINFOHEADER bmiHeader;
129 RGBQUAD bmiColors[2];
133 int bpl = (w-1)/8 + 1;
134 int bpl32 = ((w-1)/32 + 1)*4;
136 image = g_object_new (gdk_image_get_type (), NULL);
137 private = PRIVATE_DATA (image);
139 image->type = GDK_IMAGE_SHARED;
140 image->visual = visual;
144 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
146 GDK_NOTE (MISC, g_print ("gdk_image_new_bitmap: %dx%d\n", w, h));
148 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
149 bmi.bmiHeader.biWidth = w;
150 bmi.bmiHeader.biHeight = -h;
151 bmi.bmiHeader.biPlanes = 1;
152 bmi.bmiHeader.biBitCount = 1;
153 bmi.bmiHeader.biCompression = BI_RGB;
154 bmi.bmiHeader.biSizeImage = 0;
155 bmi.bmiHeader.biXPelsPerMeter =
156 bmi.bmiHeader.biYPelsPerMeter = 0;
157 bmi.bmiHeader.biClrUsed = 0;
158 bmi.bmiHeader.biClrImportant = 0;
160 bmi.u.bmiColors[0].rgbBlue =
161 bmi.u.bmiColors[0].rgbGreen =
162 bmi.u.bmiColors[0].rgbRed = 0x00;
163 bmi.u.bmiColors[0].rgbReserved = 0x00;
165 bmi.u.bmiColors[1].rgbBlue =
166 bmi.u.bmiColors[1].rgbGreen =
167 bmi.u.bmiColors[1].rgbRed = 0xFF;
168 bmi.u.bmiColors[1].rgbReserved = 0x00;
170 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
171 DIB_RGB_COLORS, &bits, NULL, 0);
174 /* Win32 expects scanlines in DIBs to be 32 bit aligned */
176 for (i = 0; i < h; i++)
177 memmove (bits + i*bpl32, ((char *) data) + i*bpl, bpl);
180 memmove (bits, data, bpl*h);
183 image->byte_order = GDK_MSB_FIRST;
187 } /* gdk_image_new_bitmap() */
190 _gdk_windowing_image_init (void)
192 /* Nothing needed AFAIK */
196 gdk_image_new (GdkImageType type,
202 GdkImagePrivateWin32 *private;
205 BITMAPINFOHEADER bmiHeader;
207 WORD bmiIndices[256];
209 RGBQUAD bmiColors[256];
215 if (type == GDK_IMAGE_FASTEST || type == GDK_IMAGE_NORMAL)
216 type = GDK_IMAGE_SHARED;
218 GDK_NOTE (MISC, g_print ("gdk_image_new: %dx%d %s\n",
220 (type == GDK_IMAGE_SHARED ? "shared" :
221 (type == GDK_IMAGE_SHARED_PIXMAP ? "shared_pixmap" :
224 image = g_object_new (gdk_image_get_type (), NULL);
225 private = PRIVATE_DATA (image);
228 image->visual = visual;
229 image->width = width;
230 image->height = height;
231 image->depth = visual->depth;
233 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
235 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
236 bmi.bmiHeader.biWidth = width;
237 bmi.bmiHeader.biHeight = -height;
238 bmi.bmiHeader.biPlanes = 1;
239 if (image->depth == 15)
240 bmi.bmiHeader.biBitCount = 16;
242 bmi.bmiHeader.biBitCount = image->depth;
243 if (image->depth == 16)
244 bmi.bmiHeader.biCompression = BI_BITFIELDS;
246 bmi.bmiHeader.biCompression = BI_RGB;
247 bmi.bmiHeader.biSizeImage = 0;
248 bmi.bmiHeader.biXPelsPerMeter =
249 bmi.bmiHeader.biYPelsPerMeter = 0;
250 bmi.bmiHeader.biClrUsed = 0;
251 bmi.bmiHeader.biClrImportant = 0;
253 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
255 iUsage = DIB_PAL_COLORS;
256 for (i = 0; i < 256; i++)
257 bmi.u.bmiIndices[i] = i;
261 iUsage = DIB_RGB_COLORS;
262 if (image->depth == 1)
264 bmi.u.bmiColors[0].rgbBlue =
265 bmi.u.bmiColors[0].rgbGreen =
266 bmi.u.bmiColors[0].rgbRed = 0x00;
267 bmi.u.bmiColors[0].rgbReserved = 0x00;
269 bmi.u.bmiColors[1].rgbBlue =
270 bmi.u.bmiColors[1].rgbGreen =
271 bmi.u.bmiColors[1].rgbRed = 0xFF;
272 bmi.u.bmiColors[1].rgbReserved = 0x00;
275 else if (image->depth == 16)
277 bmi.u.bmiMasks[0] = visual->red_mask;
278 bmi.u.bmiMasks[1] = visual->green_mask;
279 bmi.u.bmiMasks[2] = visual->blue_mask;
283 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
284 iUsage, &image->mem, NULL, 0);
286 if (private->hbitmap == NULL)
288 WIN32_GDI_FAILED ("CreateDIBSection");
293 switch (image->depth)
310 g_warning ("gdk_image_new: depth = %d", image->depth);
311 g_assert_not_reached ();
313 image->byte_order = GDK_LSB_FIRST;
314 if (image->depth == 1)
315 image->bpl = ((width-1)/32 + 1)*4;
317 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
319 GDK_NOTE (MISC, g_print ("... = %#x mem = %#x, bpl = %d\n",
320 private->hbitmap, image->mem, image->bpl));
326 gdk_image_get (GdkWindow *window,
333 GdkImagePrivateWin32 *private;
336 BITMAPINFOHEADER bmiHeader;
338 WORD bmiIndices[256];
340 RGBQUAD bmiColors[256];
343 HGDIOBJ oldbitmap1, oldbitmap2;
348 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
350 if (GDK_WINDOW_DESTROYED (window))
353 GDK_NOTE (MISC, g_print ("gdk_image_get: %#x %dx%d@+%d+%d\n",
354 GDK_DRAWABLE_HANDLE (window), width, height, x, y));
356 image = g_object_new (gdk_image_get_type (), NULL);
357 private = PRIVATE_DATA (image);
359 image->type = GDK_IMAGE_SHARED;
360 image->visual = gdk_drawable_get_visual (window);
361 image->width = width;
362 image->height = height;
364 /* This function is called both to blit from a window and from
367 if (GDK_IS_PIXMAP (window))
369 if ((hdc = CreateCompatibleDC (NULL)) == NULL)
371 WIN32_GDI_FAILED ("CreateCompatibleDC");
375 if ((oldbitmap1 = SelectObject (hdc, GDK_PIXMAP_HBITMAP (window))) == NULL)
377 WIN32_GDI_FAILED ("SelectObject");
382 GetObject (GDK_PIXMAP_HBITMAP (window), sizeof (BITMAP), &bm);
384 g_print ("gdk_image_get: bmWidth = %d, bmHeight = %d, bmWidthBytes = %d, bmBitsPixel = %d\n",
385 bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, bm.bmBitsPixel));
386 image->depth = bm.bmBitsPixel;
387 if (image->depth <= 8)
389 iUsage = DIB_PAL_COLORS;
390 for (i = 0; i < 256; i++)
391 bmi.u.bmiIndices[i] = i;
394 iUsage = DIB_RGB_COLORS;
398 if ((hdc = GetDC (GDK_WINDOW_HWND (window))) == NULL)
400 WIN32_GDI_FAILED ("GetDC");
404 image->depth = gdk_visual_get_system ()->depth;
405 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
407 iUsage = DIB_PAL_COLORS;
408 for (i = 0; i < 256; i++)
409 bmi.u.bmiIndices[i] = i;
412 iUsage = DIB_RGB_COLORS;
415 if ((memdc = CreateCompatibleDC (hdc)) == NULL)
417 WIN32_GDI_FAILED ("CreateCompatibleDC");
418 if (GDK_IS_PIXMAP (window))
420 SelectObject (hdc, oldbitmap1);
425 ReleaseDC (GDK_WINDOW_HWND (window), hdc);
431 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
432 bmi.bmiHeader.biWidth = width;
433 bmi.bmiHeader.biHeight = -height;
434 bmi.bmiHeader.biPlanes = 1;
435 bmi.bmiHeader.biBitCount = image->depth;
436 if (image->depth == 16)
438 bmi.bmiHeader.biCompression = BI_BITFIELDS;
439 if (image->visual == NULL)
441 /* XXX ??? Is it always this if depth==16 and a pixmap? Guess so. */
442 bmi.u.bmiMasks[0] = 0xf800;
443 bmi.u.bmiMasks[1] = 0x07e0;
444 bmi.u.bmiMasks[2] = 0x001f;
448 bmi.u.bmiMasks[0] = image->visual->red_mask;
449 bmi.u.bmiMasks[1] = image->visual->green_mask;
450 bmi.u.bmiMasks[2] = image->visual->blue_mask;
454 bmi.bmiHeader.biCompression = BI_RGB;
455 bmi.bmiHeader.biSizeImage = 0;
456 bmi.bmiHeader.biXPelsPerMeter =
457 bmi.bmiHeader.biYPelsPerMeter = 0;
458 bmi.bmiHeader.biClrUsed = 0;
459 bmi.bmiHeader.biClrImportant = 0;
461 if ((private->hbitmap = CreateDIBSection (hdc, (BITMAPINFO *) &bmi, iUsage,
462 &image->mem, NULL, 0)) == NULL)
464 WIN32_GDI_FAILED ("CreateDIBSection");
466 if (GDK_IS_PIXMAP (window))
468 SelectObject (hdc, oldbitmap1);
473 ReleaseDC (GDK_WINDOW_HWND (window), hdc);
479 if ((oldbitmap2 = SelectObject (memdc, private->hbitmap)) == NULL)
481 WIN32_GDI_FAILED ("SelectObject");
482 DeleteObject (private->hbitmap);
484 if (GDK_IS_PIXMAP (window))
486 SelectObject (hdc, oldbitmap1);
491 ReleaseDC (GDK_WINDOW_HWND (window), hdc);
497 if (!BitBlt (memdc, 0, 0, width, height, hdc, x, y, SRCCOPY))
499 WIN32_GDI_FAILED ("BitBlt");
500 SelectObject (memdc, oldbitmap2);
501 DeleteObject (private->hbitmap);
503 if (GDK_IS_PIXMAP (window))
505 SelectObject (hdc, oldbitmap1);
510 ReleaseDC (GDK_WINDOW_HWND (window), hdc);
516 if (SelectObject (memdc, oldbitmap2) == NULL)
517 WIN32_GDI_FAILED ("SelectObject");
519 if (!DeleteDC (memdc))
520 WIN32_GDI_FAILED ("DeleteDC");
522 if (GDK_IS_PIXMAP (window))
524 SelectObject (hdc, oldbitmap1);
529 ReleaseDC (GDK_WINDOW_HWND (window), hdc);
532 switch (image->depth)
549 g_warning ("gdk_image_get: image->depth = %d", image->depth);
550 g_assert_not_reached ();
552 image->byte_order = GDK_LSB_FIRST;
553 if (image->depth == 1)
554 image->bpl = ((width - 1)/32 + 1)*4;
556 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
558 GDK_NOTE (MISC, g_print ("... = %#x mem = %#x, bpl = %d\n",
559 private->hbitmap, image->mem, image->bpl));
565 gdk_image_get_pixel (GdkImage *image,
571 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
573 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
576 if (image->depth == 1)
577 pixel = (((char *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
580 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
588 /* Windows is always LSB, no need to check image->byte_order. */
590 pixel = pixelp[0] | (pixelp[1] << 8);
594 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
598 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
607 gdk_image_put_pixel (GdkImage *image,
612 g_return_if_fail (image != NULL);
614 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
617 if (image->depth == 1)
619 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
621 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
624 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
626 /* Windows is always LSB, no need to check image->byte_order. */
632 pixelp[2] = ((pixel >> 16) & 0xFF);
634 pixelp[1] = ((pixel >> 8) & 0xFF);
636 pixelp[0] = (pixel & 0xFF);
642 gdk_win32_image_destroy (GdkImage *image)
644 GdkImagePrivateWin32 *private;
646 g_return_if_fail (GDK_IS_IMAGE (image));
648 private = PRIVATE_DATA (image);
650 if (private == NULL) /* This means that gdk_image_exit() destroyed the
651 * image already, and now we're called a second
652 * time from _finalize()
656 GDK_NOTE (MISC, g_print ("gdk_win32_image_destroy: %#x%s\n",
658 (image->type == GDK_IMAGE_SHARED_PIXMAP ?
659 " (shared pixmap)" : "")));
663 case GDK_IMAGE_SHARED_PIXMAP:
664 break; /* The Windows bitmap has already been
665 * (or will be) deleted when freeing
666 * the corresponding pixmap.
669 case GDK_IMAGE_SHARED:
670 if (!DeleteObject (private->hbitmap))
671 WIN32_GDI_FAILED ("DeleteObject");
675 g_assert_not_reached ();
679 image->windowing_data = NULL;