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;
185 } /* gdk_image_new_bitmap() */
188 _gdk_windowing_image_init (void)
190 /* Nothing needed AFAIK */
194 gdk_image_new (GdkImageType type,
200 GdkImagePrivateWin32 *private;
203 BITMAPINFOHEADER bmiHeader;
205 WORD bmiIndices[256];
207 RGBQUAD bmiColors[256];
213 if (type == GDK_IMAGE_FASTEST || type == GDK_IMAGE_NORMAL)
214 type = GDK_IMAGE_SHARED;
216 GDK_NOTE (MISC, g_print ("gdk_image_new: %dx%d %s\n",
218 (type == GDK_IMAGE_SHARED ? "shared" :
221 image = g_object_new (gdk_image_get_type (), NULL);
222 private = IMAGE_PRIVATE_DATA (image);
225 image->visual = visual;
226 image->width = width;
227 image->height = height;
228 image->depth = visual->depth;
230 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
232 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
233 bmi.bmiHeader.biWidth = width;
234 bmi.bmiHeader.biHeight = -height;
235 bmi.bmiHeader.biPlanes = 1;
236 if (image->depth == 15)
237 bmi.bmiHeader.biBitCount = 16;
239 bmi.bmiHeader.biBitCount = image->depth;
240 if (image->depth == 16)
241 bmi.bmiHeader.biCompression = BI_BITFIELDS;
243 bmi.bmiHeader.biCompression = BI_RGB;
244 bmi.bmiHeader.biSizeImage = 0;
245 bmi.bmiHeader.biXPelsPerMeter =
246 bmi.bmiHeader.biYPelsPerMeter = 0;
247 bmi.bmiHeader.biClrUsed = 0;
248 bmi.bmiHeader.biClrImportant = 0;
250 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
252 iUsage = DIB_PAL_COLORS;
253 for (i = 0; i < 256; i++)
254 bmi.u.bmiIndices[i] = i;
258 iUsage = DIB_RGB_COLORS;
259 if (image->depth == 1)
261 bmi.u.bmiColors[0].rgbBlue =
262 bmi.u.bmiColors[0].rgbGreen =
263 bmi.u.bmiColors[0].rgbRed = 0x00;
264 bmi.u.bmiColors[0].rgbReserved = 0x00;
266 bmi.u.bmiColors[1].rgbBlue =
267 bmi.u.bmiColors[1].rgbGreen =
268 bmi.u.bmiColors[1].rgbRed = 0xFF;
269 bmi.u.bmiColors[1].rgbReserved = 0x00;
272 else if (image->depth == 16)
274 bmi.u.bmiMasks[0] = visual->red_mask;
275 bmi.u.bmiMasks[1] = visual->green_mask;
276 bmi.u.bmiMasks[2] = visual->blue_mask;
280 private->hbitmap = CreateDIBSection (gdk_display_hdc, (BITMAPINFO *) &bmi,
281 iUsage, &image->mem, NULL, 0);
283 if (private->hbitmap == NULL)
285 WIN32_GDI_FAILED ("CreateDIBSection");
290 switch (image->depth)
307 g_warning ("gdk_image_new: depth = %d", image->depth);
308 g_assert_not_reached ();
310 image->byte_order = GDK_LSB_FIRST;
311 if (image->depth == 1)
312 image->bpl = ((width-1)/32 + 1)*4;
314 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
316 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
317 (guint) private->hbitmap, image->mem, image->bpl));
323 _gdk_win32_get_image (GdkDrawable *drawable,
330 GdkImagePrivateWin32 *private;
331 GdkDrawableImplWin32 *impl;
334 BITMAPINFOHEADER bmiHeader;
336 WORD bmiIndices[256];
338 RGBQUAD bmiColors[256];
341 HGDIOBJ oldbitmap1, oldbitmap2;
346 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
348 GDK_NOTE (MISC, g_print ("_gdk_win32_get_image: %#x %dx%d@+%d+%d\n",
349 (guint) GDK_DRAWABLE_HANDLE (drawable),
350 width, height, x, y));
352 impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
354 image = g_object_new (gdk_image_get_type (), NULL);
355 private = IMAGE_PRIVATE_DATA (image);
357 image->type = GDK_IMAGE_SHARED;
358 image->visual = gdk_drawable_get_visual (drawable);
359 image->width = width;
360 image->height = height;
362 /* This function is called both to blit from a window and from
365 if (GDK_IS_PIXMAP (drawable))
367 if ((hdc = CreateCompatibleDC (NULL)) == NULL)
369 WIN32_GDI_FAILED ("CreateCompatibleDC");
373 if ((oldbitmap1 = SelectObject (hdc, impl->handle)) == NULL)
375 WIN32_GDI_FAILED ("SelectObject");
380 GetObject (impl->handle, sizeof (BITMAP), &bm);
382 g_print ("gdk_image_get: bmWidth:%ld bmHeight:%ld bmWidthBytes:%ld bmBitsPixel:%d\n",
383 bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, bm.bmBitsPixel));
384 image->depth = bm.bmBitsPixel;
385 if (image->depth <= 8)
387 iUsage = DIB_PAL_COLORS;
388 for (i = 0; i < 256; i++)
389 bmi.u.bmiIndices[i] = i;
392 iUsage = DIB_RGB_COLORS;
396 if ((hdc = GetDC (impl->handle)) == NULL)
398 WIN32_GDI_FAILED ("GetDC");
402 image->depth = gdk_visual_get_system ()->depth;
403 if (image->visual->type == GDK_VISUAL_PSEUDO_COLOR)
405 iUsage = DIB_PAL_COLORS;
406 for (i = 0; i < 256; i++)
407 bmi.u.bmiIndices[i] = i;
410 iUsage = DIB_RGB_COLORS;
413 if ((memdc = CreateCompatibleDC (hdc)) == NULL)
415 WIN32_GDI_FAILED ("CreateCompatibleDC");
416 if (GDK_IS_PIXMAP (drawable))
418 SelectObject (hdc, oldbitmap1);
423 ReleaseDC (impl->handle, hdc);
429 bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
430 bmi.bmiHeader.biWidth = width;
431 bmi.bmiHeader.biHeight = -height;
432 bmi.bmiHeader.biPlanes = 1;
433 bmi.bmiHeader.biBitCount = image->depth;
434 if (image->depth == 16)
436 bmi.bmiHeader.biCompression = BI_BITFIELDS;
437 if (image->visual == NULL)
439 /* XXX ??? Is it always this if depth==16 and a pixmap? Guess so. */
440 bmi.u.bmiMasks[0] = 0xf800;
441 bmi.u.bmiMasks[1] = 0x07e0;
442 bmi.u.bmiMasks[2] = 0x001f;
446 bmi.u.bmiMasks[0] = image->visual->red_mask;
447 bmi.u.bmiMasks[1] = image->visual->green_mask;
448 bmi.u.bmiMasks[2] = image->visual->blue_mask;
452 bmi.bmiHeader.biCompression = BI_RGB;
453 bmi.bmiHeader.biSizeImage = 0;
454 bmi.bmiHeader.biXPelsPerMeter =
455 bmi.bmiHeader.biYPelsPerMeter = 0;
456 bmi.bmiHeader.biClrUsed = 0;
457 bmi.bmiHeader.biClrImportant = 0;
459 if ((private->hbitmap = CreateDIBSection (hdc, (BITMAPINFO *) &bmi, iUsage,
460 &image->mem, NULL, 0)) == NULL)
462 WIN32_GDI_FAILED ("CreateDIBSection");
464 if (GDK_IS_PIXMAP (drawable))
466 SelectObject (hdc, oldbitmap1);
471 ReleaseDC (impl->handle, hdc);
477 if ((oldbitmap2 = SelectObject (memdc, private->hbitmap)) == NULL)
479 WIN32_GDI_FAILED ("SelectObject");
480 DeleteObject (private->hbitmap);
482 if (GDK_IS_PIXMAP (drawable))
484 SelectObject (hdc, oldbitmap1);
489 ReleaseDC (impl->handle, hdc);
495 if (!BitBlt (memdc, 0, 0, width, height, hdc, x, y, SRCCOPY))
497 WIN32_GDI_FAILED ("BitBlt");
498 SelectObject (memdc, oldbitmap2);
499 DeleteObject (private->hbitmap);
501 if (GDK_IS_PIXMAP (drawable))
503 SelectObject (hdc, oldbitmap1);
508 ReleaseDC (impl->handle, hdc);
514 if (SelectObject (memdc, oldbitmap2) == NULL)
515 WIN32_GDI_FAILED ("SelectObject");
517 if (!DeleteDC (memdc))
518 WIN32_GDI_FAILED ("DeleteDC");
520 if (GDK_IS_PIXMAP (drawable))
522 SelectObject (hdc, oldbitmap1);
527 ReleaseDC (impl->handle, hdc);
530 switch (image->depth)
547 g_warning ("gdk_image_get: image->depth = %d", image->depth);
548 g_assert_not_reached ();
550 image->byte_order = GDK_LSB_FIRST;
551 if (image->depth == 1)
552 image->bpl = ((width - 1)/32 + 1)*4;
554 image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
556 GDK_NOTE (MISC, g_print ("... = %#x mem = %p, bpl = %d\n",
557 (guint) private->hbitmap, image->mem, image->bpl));
563 gdk_image_get_pixel (GdkImage *image,
569 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
571 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
574 if (image->depth == 1)
575 pixel = (((char *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
578 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
586 /* Windows is always LSB, no need to check image->byte_order. */
588 pixel = pixelp[0] | (pixelp[1] << 8);
592 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
596 pixel = pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
605 gdk_image_put_pixel (GdkImage *image,
610 g_return_if_fail (image != NULL);
612 if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
615 if (image->depth == 1)
617 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
619 ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
622 guchar *pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
624 /* Windows is always LSB, no need to check image->byte_order. */
630 pixelp[2] = ((pixel >> 16) & 0xFF);
632 pixelp[1] = ((pixel >> 8) & 0xFF);
634 pixelp[0] = (pixel & 0xFF);
640 gdk_win32_image_destroy (GdkImage *image)
642 GdkImagePrivateWin32 *private;
644 g_return_if_fail (GDK_IS_IMAGE (image));
646 private = IMAGE_PRIVATE_DATA (image);
648 if (private == NULL) /* This means that gdk_image_exit() destroyed the
649 * image already, and now we're called a second
650 * time from _finalize()
654 GDK_NOTE (MISC, g_print ("gdk_win32_image_destroy: %#x\n",
655 (guint) private->hbitmap));
659 case GDK_IMAGE_SHARED:
660 if (!DeleteObject (private->hbitmap))
661 WIN32_GDI_FAILED ("DeleteObject");
665 g_assert_not_reached ();
669 image->windowing_data = NULL;