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 /* free data right now, in contrast to the X11 version we have made
187 * our own copy. Use free, it was malloc()ed.
191 } /* gdk_image_new_bitmap() */
194 _gdk_windowing_image_init (void)
196 /* Nothing needed AFAIK */
200 gdk_image_new (GdkImageType type,
206 GdkImagePrivateWin32 *private;
209 BITMAPINFOHEADER bmiHeader;
211 WORD bmiIndices[256];
213 RGBQUAD bmiColors[256];
219 if (type == GDK_IMAGE_FASTEST || type == GDK_IMAGE_NORMAL)
220 type = GDK_IMAGE_SHARED;
222 GDK_NOTE (MISC, g_print ("gdk_image_new: %dx%d %s\n",
224 (type == GDK_IMAGE_SHARED ? "shared" :
227 image = g_object_new (gdk_image_get_type (), NULL);
228 private = IMAGE_PRIVATE_DATA (image);
231 image->visual = visual;
232 image->width = width;
233 image->height = height;
234 image->depth = visual->depth;
236 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
238 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
239 bmi.bmiHeader.biWidth = width;
240 bmi.bmiHeader.biHeight = -height;
241 bmi.bmiHeader.biPlanes = 1;
242 if (image->depth == 15)
243 bmi.bmiHeader.biBitCount = 16;
245 bmi.bmiHeader.biBitCount = image->depth;
246 if (image->depth == 16)
247 bmi.bmiHeader.biCompression = BI_BITFIELDS;
249 bmi.bmiHeader.biCompression = BI_RGB;
250 bmi.bmiHeader.biSizeImage = 0;
251 bmi.bmiHeader.biXPelsPerMeter =
252 bmi.bmiHeader.biYPelsPerMeter = 0;
253 bmi.bmiHeader.biClrUsed = 0;
254 bmi.bmiHeader.biClrImportant = 0;
256 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
258 iUsage = DIB_PAL_COLORS;
259 for (i = 0; i < 256; i++)
260 bmi.u.bmiIndices[i] = i;
264 iUsage = DIB_RGB_COLORS;
265 if (image->depth == 1)
267 bmi.u.bmiColors[0].rgbBlue =
268 bmi.u.bmiColors[0].rgbGreen =
269 bmi.u.bmiColors[0].rgbRed = 0x00;
270 bmi.u.bmiColors[0].rgbReserved = 0x00;
272 bmi.u.bmiColors[1].rgbBlue =
273 bmi.u.bmiColors[1].rgbGreen =
274 bmi.u.bmiColors[1].rgbRed = 0xFF;
275 bmi.u.bmiColors[1].rgbReserved = 0x00;
278 else if (image->depth == 16)
280 bmi.u.bmiMasks[0] = visual->red_mask;
281 bmi.u.bmiMasks[1] = visual->green_mask;
282 bmi.u.bmiMasks[2] = visual->blue_mask;
286 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
287 iUsage, &image->mem, NULL, 0);
289 if (private->hbitmap == NULL)
291 WIN32_GDI_FAILED ("CreateDIBSection");
296 switch (image->depth)
313 g_warning ("gdk_image_new: depth = %d", image->depth);
314 g_assert_not_reached ();
316 image->bits_per_pixel = image->depth;
317 image->byte_order = GDK_LSB_FIRST;
318 if (image->depth == 1)
319 image->bpl = ((width-1)/32 + 1)*4;
321 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
323 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
324 (guint) private->hbitmap, image->mem, image->bpl));
330 _gdk_win32_get_image (GdkDrawable *drawable,
337 GdkImagePrivateWin32 *private;
338 GdkDrawableImplWin32 *impl;
341 BITMAPINFOHEADER bmiHeader;
343 WORD bmiIndices[256];
345 RGBQUAD bmiColors[256];
348 HGDIOBJ oldbitmap1, oldbitmap2;
353 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
355 GDK_NOTE (MISC, g_print ("_gdk_win32_get_image: %#x %dx%d@+%d+%d\n",
356 (guint) GDK_DRAWABLE_IMPL_WIN32 (drawable)->handle,
357 width, height, x, y));
359 impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
361 image = g_object_new (gdk_image_get_type (), NULL);
362 private = IMAGE_PRIVATE_DATA (image);
364 image->type = GDK_IMAGE_SHARED;
365 image->visual = gdk_drawable_get_visual (drawable);
366 image->width = width;
367 image->height = height;
369 /* This function is called both to blit from a window and from
372 if (GDK_IS_PIXMAP_IMPL_WIN32 (drawable))
374 if ((hdc = CreateCompatibleDC (NULL)) == NULL)
376 WIN32_GDI_FAILED ("CreateCompatibleDC");
380 if ((oldbitmap1 = SelectObject (hdc, impl->handle)) == NULL)
382 WIN32_GDI_FAILED ("SelectObject");
387 GetObject (impl->handle, sizeof (BITMAP), &bm);
389 g_print ("gdk_image_get: bmWidth:%ld bmHeight:%ld bmWidthBytes:%ld bmBitsPixel:%d\n",
390 bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, bm.bmBitsPixel));
391 image->depth = bm.bmBitsPixel;
392 if (image->depth <= 8)
394 iUsage = DIB_PAL_COLORS;
395 for (i = 0; i < 256; i++)
396 bmi.u.bmiIndices[i] = i;
399 iUsage = DIB_RGB_COLORS;
403 if ((hdc = GetDC (impl->handle)) == NULL)
405 WIN32_GDI_FAILED ("GetDC");
409 image->depth = gdk_visual_get_system ()->depth;
410 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
412 iUsage = DIB_PAL_COLORS;
413 for (i = 0; i < 256; i++)
414 bmi.u.bmiIndices[i] = i;
417 iUsage = DIB_RGB_COLORS;
420 if ((memdc = CreateCompatibleDC (hdc)) == NULL)
422 WIN32_GDI_FAILED ("CreateCompatibleDC");
423 if (GDK_IS_PIXMAP (drawable))
425 SelectObject (hdc, oldbitmap1);
430 ReleaseDC (impl->handle, hdc);
436 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
437 bmi.bmiHeader.biWidth = width;
438 bmi.bmiHeader.biHeight = -height;
439 bmi.bmiHeader.biPlanes = 1;
440 bmi.bmiHeader.biBitCount = image->depth;
441 if (image->depth == 16)
443 bmi.bmiHeader.biCompression = BI_BITFIELDS;
444 if (image->visual == NULL)
446 /* XXX ??? Is it always this if depth==16 and a pixmap? Guess so. */
447 bmi.u.bmiMasks[0] = 0xf800;
448 bmi.u.bmiMasks[1] = 0x07e0;
449 bmi.u.bmiMasks[2] = 0x001f;
453 bmi.u.bmiMasks[0] = image->visual->red_mask;
454 bmi.u.bmiMasks[1] = image->visual->green_mask;
455 bmi.u.bmiMasks[2] = image->visual->blue_mask;
459 bmi.bmiHeader.biCompression = BI_RGB;
460 bmi.bmiHeader.biSizeImage = 0;
461 bmi.bmiHeader.biXPelsPerMeter =
462 bmi.bmiHeader.biYPelsPerMeter = 0;
463 bmi.bmiHeader.biClrUsed = 0;
464 bmi.bmiHeader.biClrImportant = 0;
466 if ((private->hbitmap = CreateDIBSection (hdc, (BITMAPINFO *) &bmi, iUsage,
467 &image->mem, NULL, 0)) == NULL)
469 WIN32_GDI_FAILED ("CreateDIBSection");
471 if (GDK_IS_PIXMAP (drawable))
473 SelectObject (hdc, oldbitmap1);
478 ReleaseDC (impl->handle, hdc);
484 if ((oldbitmap2 = SelectObject (memdc, private->hbitmap)) == NULL)
486 WIN32_GDI_FAILED ("SelectObject");
487 DeleteObject (private->hbitmap);
489 if (GDK_IS_PIXMAP (drawable))
491 SelectObject (hdc, oldbitmap1);
496 ReleaseDC (impl->handle, hdc);
502 if (!BitBlt (memdc, 0, 0, width, height, hdc, x, y, SRCCOPY))
504 WIN32_GDI_FAILED ("BitBlt");
505 SelectObject (memdc, oldbitmap2);
506 DeleteObject (private->hbitmap);
508 if (GDK_IS_PIXMAP (drawable))
510 SelectObject (hdc, oldbitmap1);
515 ReleaseDC (impl->handle, hdc);
521 if (SelectObject (memdc, oldbitmap2) == NULL)
522 WIN32_GDI_FAILED ("SelectObject");
524 if (!DeleteDC (memdc))
525 WIN32_GDI_FAILED ("DeleteDC");
527 if (GDK_IS_PIXMAP_IMPL_WIN32 (drawable))
529 SelectObject (hdc, oldbitmap1);
534 ReleaseDC (impl->handle, hdc);
537 switch (image->depth)
554 g_warning ("gdk_image_get: image->depth = %d", image->depth);
555 g_assert_not_reached ();
557 image->bits_per_pixel = image->depth;
558 image->byte_order = GDK_LSB_FIRST;
559 if (image->depth == 1)
560 image->bpl = ((width - 1)/32 + 1)*4;
562 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
564 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
565 (guint) private->hbitmap, image->mem, image->bpl));
571 gdk_image_get_pixel (GdkImage *image,
577 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
579 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
582 if (image->depth == 1)
583 pixel = (((char *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
586 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
594 /* Windows is always LSB, no need to check image->byte_order. */
596 pixel = pixelp[0] | (pixelp[1] << 8);
600 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
604 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
613 gdk_image_put_pixel (GdkImage *image,
618 g_return_if_fail (image != NULL);
620 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
623 if (image->depth == 1)
625 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
627 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
630 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
632 /* Windows is always LSB, no need to check image->byte_order. */
638 pixelp[2] = ((pixel >> 16) & 0xFF);
640 pixelp[1] = ((pixel >> 8) & 0xFF);
642 pixelp[0] = (pixel & 0xFF);
648 gdk_win32_image_destroy (GdkImage *image)
650 GdkImagePrivateWin32 *private;
652 g_return_if_fail (GDK_IS_IMAGE (image));
654 private = IMAGE_PRIVATE_DATA (image);
656 if (private == NULL) /* This means that gdk_image_exit() destroyed the
657 * image already, and now we're called a second
658 * time from _finalize()
662 GDK_NOTE (MISC, g_print ("gdk_win32_image_destroy: %#x\n",
663 (guint) private->hbitmap));
667 case GDK_IMAGE_SHARED:
668 if (!DeleteObject (private->hbitmap))
669 WIN32_GDI_FAILED ("DeleteObject");
673 g_assert_not_reached ();
677 image->windowing_data = NULL;