gtk2/gdk/win32/gdkimage-win32.c
Tor Lillqvist bc1ec5c14a Massive changes. Too many to list here, but I'll try a summary:
2002-02-17  Tor Lillqvist  <tml@iki.fi>

	* gdk/win32/*.c: Massive changes. Too many to list here, but I'll
	try a summary:

	1) Unify GdkPixmap and GdkImage implementation: For each
	GdkPixmap, allocate a GdkImage, and vice versa.
	GdkPixmapImplWin32Data has a pointer to the GdkImage.
	GdkImage::windowing_data is a pointer to the GdkPixmap.

	This simplifies many pixmap and image related functions a lot, and
	reduces duplicated code snippets. For instance, there is only one
	place in gdk/win32 where CreateDIBSection() is called, in the
	function _gdk_win32_new_pixmap(). Converting a bitmap (GdkPixmap)
	to a Windows region is almost trivial, with the bitmap bits being
	readily accessible in the associated GdkImage.

	All blitting between GdkPixmaps, GdkWindows and GdkImages goes
	through handled the _gdk_win32_blit() function, which calls
	different functions to handle the cases of blitting from pixmaps,
	inside windows (scrolling), or from windows, which all require
	somewhat different handling.

	2) Support 256-color mode. This has long been very broken, now it
	works more or less OK. Keep the logical palette for each colormap
	as small as possible while allocating and freeing colors. Select
	and realize the logical palette associated with a GdkColormap into
	a DC before drawing or blitting.

	When the display is in 256-color mode, make it possible for the
	user to override the size of the palette(s) used with either the
	GDK_WIN32_MAX_COLORS environment variable, or a -max-colors
	command line option. It is possible to reduce the palette size all
	the way down to using just the 16 static colors (which causes the
	system visual to be of type GDK_VISUAL_STATIC_COLOR. This could
	possibly be useful if one desperately wants to avoid color
	flashing. (Note that in order for this to work properly, an as of
	yet not commited fix to gdkrgb.c is needed.)

	Handle the palette messages. On WM_PALETTECHANGED, call
	UpdateColors() for the given window hierarchy. Do this only if a
	window in some other top-level window hierarchy caused the palette
	change (realized a palette). Do this max five times in a row (an
	arbitrarily chosen limit), though, otherwise redraw by generating
	expose events. On WM_QUERYNEWPALETTE, cause a redraw of the whole
	window hierarchy by generating GDK_EXPOSE events.

	3) Code cleanup in general. For instance, remove the "emulated"
	X11 structs ColormapStruct, Visual and XStandardColormap. Use the
	new GDK_DEBUG_* flags for debugging output in the relevant source
	files. Remove the unused colormap hash table in gdkcolor-win32.c

	4) Plug some resource leaks.

2002-02-14  Tor Lillqvist  <tml@iki.fi>

	* gdk/win32/gdkdnd-win32.c (gdk_dropfiles_filter): Use
	g_filename_to_uri() to actually create legal URIs in the
	text/uri-list data.
2002-02-17 00:25:05 +00:00

418 lines
9.4 KiB
C

/* GDK - The GIMP Drawing Kit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
* Copyright (C) 1998-2002 Tor Lillqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "gdkimage.h"
#include "gdkpixmap.h"
#include "gdkprivate-win32.h"
static GList *image_list = NULL;
static gpointer parent_class = NULL;
static void gdk_win32_image_destroy (GdkImage *image);
static void gdk_image_init (GdkImage *image);
static void gdk_image_class_init (GdkImageClass *klass);
static void gdk_image_finalize (GObject *object);
GType
gdk_image_get_type (void)
{
static GType object_type = 0;
if (!object_type)
{
static const GTypeInfo object_info =
{
sizeof (GdkImageClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gdk_image_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GdkImage),
0, /* n_preallocs */
(GInstanceInitFunc) gdk_image_init,
};
object_type = g_type_register_static (G_TYPE_OBJECT,
"GdkImage",
&object_info, 0);
}
return object_type;
}
static void
gdk_image_init (GdkImage *image)
{
image->windowing_data = NULL;
}
static void
gdk_image_class_init (GdkImageClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gdk_image_finalize;
}
static void
gdk_image_finalize (GObject *object)
{
GdkImage *image = GDK_IMAGE (object);
gdk_win32_image_destroy (image);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
void
_gdk_image_exit (void)
{
GdkImage *image;
while (image_list)
{
image = image_list->data;
gdk_win32_image_destroy (image);
}
}
GdkImage *
_gdk_win32_setup_pixmap_image (GdkPixmap *pixmap,
GdkWindow *window,
gint width,
gint height,
gint depth,
guchar *bits)
{
GdkImage *image;
image = g_object_new (gdk_image_get_type (), NULL);
image->windowing_data = pixmap;
image->type = GDK_IMAGE_SHARED;
image->visual = gdk_drawable_get_visual (window);
image->byte_order = GDK_LSB_FIRST;
image->width = width;
image->height = height;
image->depth = depth;
switch (depth)
{
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
image->bpp = 1;
break;
case 15:
case 16:
image->bpp = 2;
break;
case 24:
image->bpp = 3;
break;
case 32:
image->bpp = 4;
break;
default:
g_warning ("_gdk_win32_setup_pixmap_image: depth=%d", image->depth);
g_assert_not_reached ();
}
if (depth == 1)
image->bpl = ((width - 1)/32 + 1)*4;
else if (depth == 4)
image->bpl = ((width - 1)/8 + 1)*4;
else
image->bpl = ((width*image->bpp - 1)/4 + 1)*4;
image->bits_per_pixel = image->depth;
image->mem = bits;
return image;
}
GdkImage *
gdk_image_new_bitmap (GdkVisual *visual,
gpointer data,
gint w,
gint h)
{
GdkPixmap *pixmap;
GdkImage *image;
gint data_bpl = (w-1)/8 + 1;
gint i;
pixmap = gdk_pixmap_new (NULL, w, h, 1);
if (pixmap == NULL)
return NULL;
image = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->image;
GDK_NOTE (IMAGE, g_print ("gdk_image_new_bitmap: %dx%d=%p\n",
w, h, GDK_PIXMAP_HBITMAP (pixmap)));
if (data_bpl != image->bpl)
{
for (i = 0; i < h; i++)
memmove ((guchar *) image->mem + i*image->bpl, ((guchar *) data) + i*data_bpl, data_bpl);
}
else
memmove (image->mem, data, data_bpl*h);
return image;
}
void
_gdk_windowing_image_init (void)
{
/* Nothing needed AFAIK */
}
GdkImage*
_gdk_image_new_for_depth (GdkImageType type,
GdkVisual *visual,
gint width,
gint height,
gint depth)
{
GdkPixmap *pixmap;
g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
g_return_val_if_fail (visual || depth != -1, NULL);
if (visual)
depth = visual->depth;
pixmap = gdk_pixmap_new (NULL, width, height, depth);
if (pixmap == NULL)
return NULL;
GDK_NOTE (IMAGE, g_print ("_gdk_image_new_for_depth: %dx%dx%d=%p\n",
width, height, depth, GDK_PIXMAP_HBITMAP (pixmap)));
return GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->image;
}
GdkImage*
gdk_image_new (GdkImageType type,
GdkVisual *visual,
gint width,
gint height)
{
return _gdk_image_new_for_depth (type, visual, width, height, -1);
}
GdkImage*
_gdk_win32_copy_to_image (GdkDrawable *drawable,
GdkImage *image,
gint src_x,
gint src_y,
gint dest_x,
gint dest_y,
gint width,
gint height)
{
GdkGC *gc;
g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL);
GDK_NOTE (IMAGE, g_print ("_gdk_win32_copy_to_image: %p\n",
GDK_DRAWABLE_HANDLE (drawable)));
if (!image)
image = _gdk_image_new_for_depth (GDK_IMAGE_FASTEST, NULL, width, height,
gdk_drawable_get_depth (drawable));
gc = gdk_gc_new ((GdkDrawable *) image->windowing_data);
_gdk_win32_blit
(FALSE,
GDK_DRAWABLE_IMPL_WIN32 (GDK_PIXMAP_OBJECT (image->windowing_data)->impl),
gc, drawable, src_x, src_y, dest_x, dest_y, width, height);
gdk_gc_unref (gc);
return image;
}
guint32
gdk_image_get_pixel (GdkImage *image,
gint x,
gint y)
{
guchar *pixelp;
g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
return 0;
if (image->depth == 1)
return (((guchar *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0;
if (image->depth == 4)
{
pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1);
if (x&1)
return (*pixelp) & 0x0F;
return (*pixelp) >> 4;
}
pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
switch (image->bpp)
{
case 1:
return *pixelp;
/* Windows is always LSB, no need to check image->byte_order. */
case 2:
return pixelp[0] | (pixelp[1] << 8);
case 3:
return pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
case 4:
return pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16);
}
g_assert_not_reached ();
return 0;
}
void
gdk_image_put_pixel (GdkImage *image,
gint x,
gint y,
guint32 pixel)
{
guchar *pixelp;
g_return_if_fail (image != NULL);
if (!(x >= 0 && x < image->width && y >= 0 && y < image->height))
return;
GdiFlush ();
if (image->depth == 1)
if (pixel & 1)
((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7)));
else
((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7)));
else if (image->depth == 4)
{
pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1);
if (x&1)
{
*pixelp &= 0xF0;
*pixelp |= (pixel & 0x0F);
}
else
{
*pixelp &= 0x0F;
*pixelp |= (pixel << 4);
}
}
else
{
pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp;
/* Windows is always LSB, no need to check image->byte_order. */
switch (image->bpp)
{
case 4:
pixelp[3] = 0;
case 3:
pixelp[2] = ((pixel >> 16) & 0xFF);
case 2:
pixelp[1] = ((pixel >> 8) & 0xFF);
case 1:
pixelp[0] = (pixel & 0xFF);
}
}
}
static void
gdk_win32_image_destroy (GdkImage *image)
{
GdkPixmap *pixmap;
g_return_if_fail (GDK_IS_IMAGE (image));
pixmap = image->windowing_data;
if (pixmap == NULL) /* This means that _gdk_image_exit()
* destroyed the image already, and
* now we're called a second time from
* _finalize()
*/
return;
GDK_NOTE (IMAGE, g_print ("gdk_win32_image_destroy: %p\n",
GDK_PIXMAP_HBITMAP (pixmap)));
gdk_pixmap_unref (pixmap);
image->windowing_data = NULL;
}
gint
_gdk_windowing_get_bits_for_depth (gint depth)
{
switch (depth)
{
case 1:
return 1;
case 2:
case 3:
case 4:
return 4;
case 5:
case 6:
case 7:
case 8:
return 8;
case 15:
case 16:
return 16;
case 24:
return 24;
case 32:
return 32;
}
g_assert_not_reached ();
return 0;
}