summaryrefslogtreecommitdiff
path: root/gdk/win32/gdkimage-win32.c
blob: 7ed740db068b95a105bd6a2b96694bdc0c311083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* 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 <config.h>
#include "gdkimage.h"
#include "gdkpixmap.h"
#include "gdkscreen.h" /* gdk_screen_get_default() */
#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);
    }
}

/*
 * Create a GdkImage _without_ an associated GdkPixmap. The caller is
 * responsible for creating a GdkPixmap object and making the association.
 */

static GdkImage *
_gdk_win32_new_image (GdkVisual *visual,
		      gint       width,
		      gint       height,
		      gint       depth,
		      guchar    *bits)
{
  GdkImage *image;

  image = g_object_new (gdk_image_get_type (), NULL);
  image->windowing_data = NULL;
  image->type = GDK_IMAGE_SHARED;
  image->visual = visual;
  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_new_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;
  guchar *bits;
  gint data_bpl = (w-1)/8 + 1;
  gint i;

  pixmap = gdk_pixmap_new (NULL, w, h, 1);

  if (pixmap == NULL)
    return NULL;

  GDK_NOTE (IMAGE, g_print ("gdk_image_new_bitmap: %dx%d=%p\n",
			    w, h, GDK_PIXMAP_HBITMAP (pixmap)));

  bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits;
  image = _gdk_win32_new_image (visual, w, h, 1, bits);
  image->windowing_data = 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 (GdkScreen    *screen,
			  GdkImageType  type,
			  GdkVisual    *visual,
			  gint          width,
			  gint          height,
			  gint          depth)
{
  GdkPixmap *pixmap;
  GdkImage *image;
  guchar *bits;

  g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
  g_return_val_if_fail (visual || depth != -1, NULL);
  g_return_val_if_fail (screen == gdk_screen_get_default (), 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)));
  
  bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits;
  image = _gdk_win32_new_image (visual, width, height, depth, bits);
  image->windowing_data = pixmap;
  
  return image;
}

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;
  GdkScreen *screen = gdk_drawable_get_screen (drawable);
  
  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 (screen, 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 (image != NULL, 0);
  g_return_val_if_fail (x >= 0 && x < image->width, 0);
  g_return_val_if_fail (y >= 0 && y < image->height, 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);
  g_return_if_fail (x >= 0 && x < image->width);
  g_return_if_fail (y >= 0 && y < image->height);

  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 (GdkDisplay *display,
                                   gint        depth)
{
  g_return_val_if_fail (display == gdk_display_get_default (), 0);

  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;
}