| 1 |
/* GDK - The GIMP Drawing Kit |
| 2 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
| 3 |
* Copyright (C) 1998-2002 Tor Lillqvist |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS |
| 23 |
* file for a list of people on the GTK+ Team. See the ChangeLog |
| 24 |
* files for a list of changes. These files are distributed with |
| 25 |
* GTK+ at ftp://ftp.gtk.org/pub/gtk/. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
#include "gdkimage.h" |
| 30 |
#include "gdkpixmap.h" |
| 31 |
#include "gdkscreen.h" /* gdk_screen_get_default() */ |
| 32 |
#include "gdkprivate-win32.h" |
| 33 |
|
| 34 |
static GList *image_list = NULL; |
| 35 |
static gpointer parent_class = NULL; |
| 36 |
|
| 37 |
static void gdk_win32_image_destroy (GdkImage *image); |
| 38 |
static void gdk_image_init (GdkImage *image); |
| 39 |
static void gdk_image_class_init (GdkImageClass *klass); |
| 40 |
static void gdk_image_finalize (GObject *object); |
| 41 |
|
| 42 |
GType |
| 43 |
gdk_image_get_type (void) |
| 44 |
{ |
| 45 |
static GType object_type = 0; |
| 46 |
|
| 47 |
if (!object_type) |
| 48 |
{ |
| 49 |
const GTypeInfo object_info = |
| 50 |
{ |
| 51 |
sizeof (GdkImageClass), |
| 52 |
(GBaseInitFunc) NULL, |
| 53 |
(GBaseFinalizeFunc) NULL, |
| 54 |
(GClassInitFunc) gdk_image_class_init, |
| 55 |
NULL, /* class_finalize */ |
| 56 |
NULL, /* class_data */ |
| 57 |
sizeof (GdkImage), |
| 58 |
0, /* n_preallocs */ |
| 59 |
(GInstanceInitFunc) gdk_image_init, |
| 60 |
}; |
| 61 |
|
| 62 |
object_type = g_type_register_static (G_TYPE_OBJECT, |
| 63 |
"GdkImage", |
| 64 |
&object_info, 0); |
| 65 |
} |
| 66 |
|
| 67 |
return object_type; |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
gdk_image_init (GdkImage *image) |
| 72 |
{ |
| 73 |
image->windowing_data = NULL; |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
gdk_image_class_init (GdkImageClass *klass) |
| 78 |
{ |
| 79 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 80 |
|
| 81 |
parent_class = g_type_class_peek_parent (klass); |
| 82 |
|
| 83 |
object_class->finalize = gdk_image_finalize; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
gdk_image_finalize (GObject *object) |
| 88 |
{ |
| 89 |
GdkImage *image = GDK_IMAGE (object); |
| 90 |
|
| 91 |
gdk_win32_image_destroy (image); |
| 92 |
|
| 93 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
_gdk_image_exit (void) |
| 98 |
{ |
| 99 |
GdkImage *image; |
| 100 |
|
| 101 |
while (image_list) |
| 102 |
{ |
| 103 |
image = image_list->data; |
| 104 |
gdk_win32_image_destroy (image); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Create a GdkImage _without_ an associated GdkPixmap. The caller is |
| 110 |
* responsible for creating a GdkPixmap object and making the association. |
| 111 |
*/ |
| 112 |
|
| 113 |
static GdkImage * |
| 114 |
_gdk_win32_new_image (GdkVisual *visual, |
| 115 |
gint width, |
| 116 |
gint height, |
| 117 |
gint depth, |
| 118 |
guchar *bits) |
| 119 |
{ |
| 120 |
GdkImage *image; |
| 121 |
|
| 122 |
image = g_object_new (gdk_image_get_type (), NULL); |
| 123 |
image->windowing_data = NULL; |
| 124 |
image->type = GDK_IMAGE_SHARED; |
| 125 |
image->visual = visual; |
| 126 |
image->byte_order = GDK_LSB_FIRST; |
| 127 |
image->width = width; |
| 128 |
image->height = height; |
| 129 |
image->depth = depth; |
| 130 |
image->bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_display_get_default (), depth); |
| 131 |
switch (depth) |
| 132 |
{ |
| 133 |
case 1: |
| 134 |
case 4: |
| 135 |
case 5: |
| 136 |
case 6: |
| 137 |
case 7: |
| 138 |
case 8: |
| 139 |
image->bpp = 1; |
| 140 |
break; |
| 141 |
case 15: |
| 142 |
case 16: |
| 143 |
image->bpp = 2; |
| 144 |
break; |
| 145 |
case 24: |
| 146 |
image->bpp = image->bits_per_pixel / 8; |
| 147 |
break; |
| 148 |
case 32: |
| 149 |
image->bpp = 4; |
| 150 |
break; |
| 151 |
default: |
| 152 |
g_warning ("_gdk_win32_new_image: depth=%d", image->depth); |
| 153 |
g_assert_not_reached (); |
| 154 |
} |
| 155 |
if (depth == 1) |
| 156 |
image->bpl = ((width - 1)/32 + 1)*4; |
| 157 |
else if (depth == 4) |
| 158 |
image->bpl = ((width - 1)/8 + 1)*4; |
| 159 |
else |
| 160 |
image->bpl = ((width*image->bpp - 1)/4 + 1)*4; |
| 161 |
image->mem = bits; |
| 162 |
|
| 163 |
return image; |
| 164 |
} |
| 165 |
|
| 166 |
GdkImage * |
| 167 |
gdk_image_new_bitmap (GdkVisual *visual, |
| 168 |
gpointer data, |
| 169 |
gint w, |
| 170 |
gint h) |
| 171 |
{ |
| 172 |
GdkPixmap *pixmap; |
| 173 |
GdkImage *image; |
| 174 |
guchar *bits; |
| 175 |
gint data_bpl = (w-1)/8 + 1; |
| 176 |
gint i; |
| 177 |
|
| 178 |
pixmap = gdk_pixmap_new (NULL, w, h, 1); |
| 179 |
|
| 180 |
if (pixmap == NULL) |
| 181 |
return NULL; |
| 182 |
|
| 183 |
GDK_NOTE (IMAGE, g_print ("gdk_image_new_bitmap: %dx%d=%p\n", |
| 184 |
w, h, GDK_PIXMAP_HBITMAP (pixmap))); |
| 185 |
|
| 186 |
bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits; |
| 187 |
image = _gdk_win32_new_image (visual, w, h, 1, bits); |
| 188 |
image->windowing_data = pixmap; |
| 189 |
|
| 190 |
if (data_bpl != image->bpl) |
| 191 |
{ |
| 192 |
for (i = 0; i < h; i++) |
| 193 |
memmove ((guchar *) image->mem + i*image->bpl, ((guchar *) data) + i*data_bpl, data_bpl); |
| 194 |
} |
| 195 |
else |
| 196 |
memmove (image->mem, data, data_bpl*h); |
| 197 |
|
| 198 |
return image; |
| 199 |
} |
| 200 |
|
| 201 |
void |
| 202 |
_gdk_windowing_image_init (void) |
| 203 |
{ |
| 204 |
/* Nothing needed AFAIK */ |
| 205 |
} |
| 206 |
|
| 207 |
GdkImage* |
| 208 |
_gdk_image_new_for_depth (GdkScreen *screen, |
| 209 |
GdkImageType type, |
| 210 |
GdkVisual *visual, |
| 211 |
gint width, |
| 212 |
gint height, |
| 213 |
gint depth) |
| 214 |
{ |
| 215 |
GdkPixmap *pixmap; |
| 216 |
GdkImage *image; |
| 217 |
guchar *bits; |
| 218 |
|
| 219 |
g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL); |
| 220 |
g_return_val_if_fail (visual || depth != -1, NULL); |
| 221 |
g_return_val_if_fail (screen == gdk_screen_get_default (), NULL); |
| 222 |
|
| 223 |
if (visual) |
| 224 |
depth = visual->depth; |
| 225 |
|
| 226 |
pixmap = gdk_pixmap_new (NULL, width, height, depth); |
| 227 |
|
| 228 |
if (pixmap == NULL) |
| 229 |
return NULL; |
| 230 |
|
| 231 |
GDK_NOTE (IMAGE, g_print ("_gdk_image_new_for_depth: %dx%dx%d=%p\n", |
| 232 |
width, height, depth, GDK_PIXMAP_HBITMAP (pixmap))); |
| 233 |
|
| 234 |
bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits; |
| 235 |
image = _gdk_win32_new_image (visual, width, height, depth, bits); |
| 236 |
image->windowing_data = pixmap; |
| 237 |
|
| 238 |
return image; |
| 239 |
} |
| 240 |
|
| 241 |
GdkImage* |
| 242 |
_gdk_win32_copy_to_image (GdkDrawable *drawable, |
| 243 |
GdkImage *image, |
| 244 |
gint src_x, |
| 245 |
gint src_y, |
| 246 |
gint dest_x, |
| 247 |
gint dest_y, |
| 248 |
gint width, |
| 249 |
gint height) |
| 250 |
{ |
| 251 |
GdkGC *gc; |
| 252 |
GdkScreen *screen = gdk_drawable_get_screen (drawable); |
| 253 |
|
| 254 |
g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL); |
| 255 |
g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL); |
| 256 |
|
| 257 |
GDK_NOTE (IMAGE, g_print ("_gdk_win32_copy_to_image: %p\n", |
| 258 |
GDK_DRAWABLE_HANDLE (drawable))); |
| 259 |
|
| 260 |
if (!image) |
| 261 |
image = _gdk_image_new_for_depth (screen, GDK_IMAGE_FASTEST, NULL, width, height, |
| 262 |
gdk_drawable_get_depth (drawable)); |
| 263 |
|
| 264 |
gc = gdk_gc_new ((GdkDrawable *) image->windowing_data); |
| 265 |
_gdk_win32_blit |
| 266 |
(FALSE, |
| 267 |
GDK_DRAWABLE_IMPL_WIN32 (GDK_PIXMAP_OBJECT (image->windowing_data)->impl), |
| 268 |
gc, drawable, src_x, src_y, dest_x, dest_y, width, height); |
| 269 |
g_object_unref (gc); |
| 270 |
|
| 271 |
return image; |
| 272 |
} |
| 273 |
|
| 274 |
guint32 |
| 275 |
gdk_image_get_pixel (GdkImage *image, |
| 276 |
gint x, |
| 277 |
gint y) |
| 278 |
{ |
| 279 |
guchar *pixelp; |
| 280 |
|
| 281 |
g_return_val_if_fail (image != NULL, 0); |
| 282 |
g_return_val_if_fail (x >= 0 && x < image->width, 0); |
| 283 |
g_return_val_if_fail (y >= 0 && y < image->height, 0); |
| 284 |
|
| 285 |
if (!(x >= 0 && x < image->width && y >= 0 && y < image->height)) |
| 286 |
return 0; |
| 287 |
|
| 288 |
if (image->depth == 1) |
| 289 |
return (((guchar *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0; |
| 290 |
|
| 291 |
if (image->depth == 4) |
| 292 |
{ |
| 293 |
pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1); |
| 294 |
if (x&1) |
| 295 |
return (*pixelp) & 0x0F; |
| 296 |
|
| 297 |
return (*pixelp) >> 4; |
| 298 |
} |
| 299 |
|
| 300 |
pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp; |
| 301 |
|
| 302 |
switch (image->bpp) |
| 303 |
{ |
| 304 |
case 1: |
| 305 |
return *pixelp; |
| 306 |
|
| 307 |
/* Windows is always LSB, no need to check image->byte_order. */ |
| 308 |
case 2: |
| 309 |
return pixelp[0] | (pixelp[1] << 8); |
| 310 |
|
| 311 |
case 3: |
| 312 |
return pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16); |
| 313 |
|
| 314 |
case 4: |
| 315 |
return pixelp[0] | (pixelp[1] << 8) | (pixelp[2] << 16); |
| 316 |
} |
| 317 |
g_assert_not_reached (); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
void |
| 322 |
gdk_image_put_pixel (GdkImage *image, |
| 323 |
gint x, |
| 324 |
gint y, |
| 325 |
guint32 pixel) |
| 326 |
{ |
| 327 |
guchar *pixelp; |
| 328 |
|
| 329 |
g_return_if_fail (image != NULL); |
| 330 |
g_return_if_fail (x >= 0 && x < image->width); |
| 331 |
g_return_if_fail (y >= 0 && y < image->height); |
| 332 |
|
| 333 |
if (!(x >= 0 && x < image->width && y >= 0 && y < image->height)) |
| 334 |
return; |
| 335 |
|
| 336 |
GdiFlush (); |
| 337 |
if (image->depth == 1) |
| 338 |
if (pixel & 1) |
| 339 |
((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7))); |
| 340 |
else |
| 341 |
((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7))); |
| 342 |
else if (image->depth == 4) |
| 343 |
{ |
| 344 |
pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1); |
| 345 |
|
| 346 |
if (x&1) |
| 347 |
{ |
| 348 |
*pixelp &= 0xF0; |
| 349 |
*pixelp |= (pixel & 0x0F); |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
*pixelp &= 0x0F; |
| 354 |
*pixelp |= (pixel << 4); |
| 355 |
} |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
pixelp = (guchar *) image->mem + y * image->bpl + x * image->bpp; |
| 360 |
|
| 361 |
/* Windows is always LSB, no need to check image->byte_order. */ |
| 362 |
switch (image->bpp) |
| 363 |
{ |
| 364 |
case 4: |
| 365 |
pixelp[3] = 0; |
| 366 |
case 3: |
| 367 |
pixelp[2] = ((pixel >> 16) & 0xFF); |
| 368 |
case 2: |
| 369 |
pixelp[1] = ((pixel >> 8) & 0xFF); |
| 370 |
case 1: |
| 371 |
pixelp[0] = (pixel & 0xFF); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
gdk_win32_image_destroy (GdkImage *image) |
| 378 |
{ |
| 379 |
GdkPixmap *pixmap; |
| 380 |
|
| 381 |
g_return_if_fail (GDK_IS_IMAGE (image)); |
| 382 |
|
| 383 |
pixmap = image->windowing_data; |
| 384 |
|
| 385 |
if (pixmap == NULL) /* This means that _gdk_image_exit() |
| 386 |
* destroyed the image already, and |
| 387 |
* now we're called a second time from |
| 388 |
* _finalize() |
| 389 |
*/ |
| 390 |
return; |
| 391 |
|
| 392 |
GDK_NOTE (IMAGE, g_print ("gdk_win32_image_destroy: %p\n", |
| 393 |
GDK_PIXMAP_HBITMAP (pixmap))); |
| 394 |
|
| 395 |
g_object_unref (pixmap); |
| 396 |
image->windowing_data = NULL; |
| 397 |
} |
| 398 |
|
| 399 |
gint |
| 400 |
_gdk_windowing_get_bits_for_depth (GdkDisplay *display, |
| 401 |
gint depth) |
| 402 |
{ |
| 403 |
g_return_val_if_fail (display == gdk_display_get_default (), 0); |
| 404 |
|
| 405 |
switch (depth) |
| 406 |
{ |
| 407 |
case 1: |
| 408 |
return 1; |
| 409 |
|
| 410 |
case 2: |
| 411 |
case 3: |
| 412 |
case 4: |
| 413 |
return 4; |
| 414 |
|
| 415 |
case 5: |
| 416 |
case 6: |
| 417 |
case 7: |
| 418 |
case 8: |
| 419 |
return 8; |
| 420 |
|
| 421 |
case 15: |
| 422 |
case 16: |
| 423 |
return 16; |
| 424 |
|
| 425 |
case 24: |
| 426 |
case 32: |
| 427 |
return 32; |
| 428 |
} |
| 429 |
g_assert_not_reached (); |
| 430 |
return 0; |
| 431 |
} |