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);
46 gdk_image_get_type (void)
48 static GType object_type = 0;
52 static const GTypeInfo object_info =
54 sizeof (GdkImageClass),
56 (GBaseFinalizeFunc) NULL,
57 (GClassInitFunc) gdk_image_class_init,
58 NULL, /* class_finalize */
59 NULL, /* class_data */
62 (GInstanceInitFunc) gdk_image_init,
65 object_type = g_type_register_static (G_TYPE_OBJECT,
74 gdk_image_init (GdkImage *image)
76 image->windowing_data = g_new0 (GdkImagePrivateWin32, 1);
80 gdk_image_class_init (GdkImageClass *klass)
82 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84 parent_class = g_type_class_peek_parent (klass);
86 object_class->finalize = gdk_image_finalize;
90 gdk_image_finalize (GObject *object)
92 GdkImage *image = GDK_IMAGE (object);
94 gdk_win32_image_destroy (image);
96 G_OBJECT_CLASS (parent_class)->finalize (object);
100 gdk_image_exit (void)
106 image = image_list->data;
107 gdk_win32_image_destroy (image);
112 gdk_image_new_bitmap (GdkVisual *visual,
117 * Desc: create a new bitmap image
122 GdkImagePrivateWin32 *private;
124 BITMAPINFOHEADER bmiHeader;
127 RGBQUAD bmiColors[2];
131 int bpl = (w-1)/8 + 1;
132 int bpl32 = ((w-1)/32 + 1)*4;
134 image = g_object_new (gdk_image_get_type (), NULL);
135 private = IMAGE_PRIVATE_DATA (image);
137 image->type = GDK_IMAGE_SHARED;
138 image->visual = visual;
142 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
144 GDK_NOTE (MISC, g_print ("gdk_image_new_bitmap: %dx%d\n", w, h));
146 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
147 bmi.bmiHeader.biWidth = w;
148 bmi.bmiHeader.biHeight = -h;
149 bmi.bmiHeader.biPlanes = 1;
150 bmi.bmiHeader.biBitCount = 1;
151 bmi.bmiHeader.biCompression = BI_RGB;
152 bmi.bmiHeader.biSizeImage = 0;
153 bmi.bmiHeader.biXPelsPerMeter =
154 bmi.bmiHeader.biYPelsPerMeter = 0;
155 bmi.bmiHeader.biClrUsed = 0;
156 bmi.bmiHeader.biClrImportant = 0;
158 bmi.u.bmiColors[0].rgbBlue =
159 bmi.u.bmiColors[0].rgbGreen =
160 bmi.u.bmiColors[0].rgbRed = 0x00;
161 bmi.u.bmiColors[0].rgbReserved = 0x00;
163 bmi.u.bmiColors[1].rgbBlue =
164 bmi.u.bmiColors[1].rgbGreen =
165 bmi.u.bmiColors[1].rgbRed = 0xFF;
166 bmi.u.bmiColors[1].rgbReserved = 0x00;
168 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
169 DIB_RGB_COLORS, &bits, NULL, 0);
172 /* Win32 expects scanlines in DIBs to be 32 bit aligned */
174 for (i = 0; i < h; i++)
175 memmove (bits + i*bpl32, ((char *) data) + i*bpl, bpl);
178 memmove (bits, data, bpl*h);
181 image->byte_order = GDK_MSB_FIRST;
183 image->bits_per_pixel = 1;
186 } /* gdk_image_new_bitmap() */
189 _gdk_windowing_image_init (void)
191 /* Nothing needed AFAIK */
195 gdk_image_new (GdkImageType type,
201 GdkImagePrivateWin32 *private;
204 BITMAPINFOHEADER bmiHeader;
206 WORD bmiIndices[256];
208 RGBQUAD bmiColors[256];
214 if (type == GDK_IMAGE_FASTEST || type == GDK_IMAGE_NORMAL)
215 type = GDK_IMAGE_SHARED;
217 GDK_NOTE (MISC, g_print ("gdk_image_new: %dx%d %s\n",
219 (type == GDK_IMAGE_SHARED ? "shared" :
222 image = g_object_new (gdk_image_get_type (), NULL);
223 private = IMAGE_PRIVATE_DATA (image);
226 image->visual = visual;
227 image->width = width;
228 image->height = height;
229 image->depth = visual->depth;
231 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
233 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
234 bmi.bmiHeader.biWidth = width;
235 bmi.bmiHeader.biHeight = -height;
236 bmi.bmiHeader.biPlanes = 1;
237 if (image->depth == 15)
238 bmi.bmiHeader.biBitCount = 16;
240 bmi.bmiHeader.biBitCount = image->depth;
241 if (image->depth == 16)
242 bmi.bmiHeader.biCompression = BI_BITFIELDS;
244 bmi.bmiHeader.biCompression = BI_RGB;
245 bmi.bmiHeader.biSizeImage = 0;
246 bmi.bmiHeader.biXPelsPerMeter =
247 bmi.bmiHeader.biYPelsPerMeter = 0;
248 bmi.bmiHeader.biClrUsed = 0;
249 bmi.bmiHeader.biClrImportant = 0;
251 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
253 iUsage = DIB_PAL_COLORS;
254 for (i = 0; i < 256; i++)
255 bmi.u.bmiIndices[i] = i;
259 iUsage = DIB_RGB_COLORS;
260 if (image->depth == 1)
262 bmi.u.bmiColors[0].rgbBlue =
263 bmi.u.bmiColors[0].rgbGreen =
264 bmi.u.bmiColors[0].rgbRed = 0x00;
265 bmi.u.bmiColors[0].rgbReserved = 0x00;
267 bmi.u.bmiColors[1].rgbBlue =
268 bmi.u.bmiColors[1].rgbGreen =
269 bmi.u.bmiColors[1].rgbRed = 0xFF;
270 bmi.u.bmiColors[1].rgbReserved = 0x00;
273 else if (image->depth == 16)
275 bmi.u.bmiMasks[0] = visual->red_mask;
276 bmi.u.bmiMasks[1] = visual->green_mask;
277 bmi.u.bmiMasks[2] = visual->blue_mask;
281 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
282 iUsage, &image->mem, NULL, 0);
284 if (private->hbitmap == NULL)
286 WIN32_GDI_FAILED ("CreateDIBSection");
291 switch (image->depth)
308 g_warning ("gdk_image_new: depth = %d", image->depth);
309 g_assert_not_reached ();
311 image->bits_per_pixel = image->depth;
312 image->byte_order = GDK_LSB_FIRST;
313 if (image->depth == 1)
314 image->bpl = ((width-1)/32 + 1)*4;
316 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
318 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
319 (guint) private->hbitmap, image->mem, image->bpl));
325 _gdk_win32_get_image (GdkDrawable *drawable,
332 GdkImagePrivateWin32 *private;
333 GdkDrawableImplWin32 *impl;
336 BITMAPINFOHEADER bmiHeader;
338 WORD bmiIndices[256];
340 RGBQUAD bmiColors[256];
343 HGDIOBJ oldbitmap1, oldbitmap2;
348 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
350 GDK_NOTE (MISC, g_print ("_gdk_win32_get_image: %#x %dx%d@+%d+%d\n",
351 (guint) GDK_DRAWABLE_HANDLE (drawable),
352 width, height, x, y));
354 impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
356 image = g_object_new (gdk_image_get_type (), NULL);
357 private = IMAGE_PRIVATE_DATA (image);
359 image->type = GDK_IMAGE_SHARED;
360 image->visual = gdk_drawable_get_visual (drawable);
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_IMPL_WIN32 (drawable))
369 if ((hdc = CreateCompatibleDC (NULL)) == NULL)
371 WIN32_GDI_FAILED ("CreateCompatibleDC");
375 if ((oldbitmap1 = SelectObject (hdc, impl->handle)) == NULL)
377 WIN32_GDI_FAILED ("SelectObject");
382 GetObject (impl->handle, sizeof (BITMAP), &bm);
384 g_print ("gdk_image_get: bmWidth:%ld bmHeight:%ld bmWidthBytes:%ld 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 (impl->handle)) == 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 (drawable))
420 SelectObject (hdc, oldbitmap1);
425 ReleaseDC (impl->handle, 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 (drawable))
468 SelectObject (hdc, oldbitmap1);
473 ReleaseDC (impl->handle, hdc);
479 if ((oldbitmap2 = SelectObject (memdc, private->hbitmap)) == NULL)
481 WIN32_GDI_FAILED ("SelectObject");
482 DeleteObject (private->hbitmap);
484 if (GDK_IS_PIXMAP (drawable))
486 SelectObject (hdc, oldbitmap1);
491 ReleaseDC (impl->handle, 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 (drawable))
505 SelectObject (hdc, oldbitmap1);
510 ReleaseDC (impl->handle, 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_IMPL_WIN32 (drawable))
524 SelectObject (hdc, oldbitmap1);
529 ReleaseDC (impl->handle, hdc);
532 switch (image->depth)
549 g_warning ("gdk_image_get: image->depth = %d", image->depth);
550 g_assert_not_reached ();
552 image->bits_per_pixel = image->depth;
553 image->byte_order = GDK_LSB_FIRST;
554 if (image->depth == 1)
555 image->bpl = ((width - 1)/32 + 1)*4;
557 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
559 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
560 (guint) private->hbitmap, image->mem, image->bpl));
566 gdk_image_get_pixel (GdkImage *image,
572 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
574 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
577 if (image->depth == 1)
578 pixel = (((char *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
581 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
589 /* Windows is always LSB, no need to check image->byte_order. */
591 pixel = pixelp[0] | (pixelp[1] << 8);
595 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
599 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
608 gdk_image_put_pixel (GdkImage *image,
613 g_return_if_fail (image != NULL);
615 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
618 if (image->depth == 1)
620 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
622 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
625 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
627 /* Windows is always LSB, no need to check image->byte_order. */
633 pixelp[2] = ((pixel >> 16) & 0xFF);
635 pixelp[1] = ((pixel >> 8) & 0xFF);
637 pixelp[0] = (pixel & 0xFF);
643 gdk_win32_image_destroy (GdkImage *image)
645 GdkImagePrivateWin32 *private;
647 g_return_if_fail (GDK_IS_IMAGE (image));
649 private = IMAGE_PRIVATE_DATA (image);
651 if (private == NULL) /* This means that gdk_image_exit() destroyed the
652 * image already, and now we're called a second
653 * time from _finalize()
657 GDK_NOTE (MISC, g_print ("gdk_win32_image_destroy: %#x\n",
658 (guint) private->hbitmap));
662 case GDK_IMAGE_SHARED:
663 if (!DeleteObject (private->hbitmap))
664 WIN32_GDI_FAILED ("DeleteObject");
668 g_assert_not_reached ();
672 image->windowing_data = NULL;