summaryrefslogtreecommitdiff
path: root/gdk/gdkimage.c
blob: 1ef49c996dff44583bf7c0732765079d94ef0db4 (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * 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 <stdlib.h>
#include <sys/types.h>

#include "gdk.h"		/* For gdk_flush() */
#include "gdkimage.h"
#include "gdkprivate.h"
#include "gdkinternals.h"	/* For scratch_image code */
#include "gdkalias.h"

/**
 * gdk_image_set_colormap:
 * @image: a #GdkImage
 * @colormap: a #GdkColormap
 * 
 * Sets the colormap for the image to the given colormap.  Normally
 * there's no need to use this function, images are created with the
 * correct colormap if you get the image from a drawable. If you
 * create the image from scratch, use the colormap of the drawable you
 * intend to render the image to.
 **/
void
gdk_image_set_colormap (GdkImage       *image,
                        GdkColormap    *colormap)
{
  g_return_if_fail (GDK_IS_IMAGE (image));
  g_return_if_fail (GDK_IS_COLORMAP (colormap));

  if (image->colormap != colormap)
    {
      if (image->colormap)
	g_object_unref (image->colormap);

      image->colormap = colormap;
      g_object_ref (image->colormap);
    }
}

/**
 * gdk_image_get_colormap:
 * @image: a #GdkImage
 * 
 * Retrieves the colormap for a given image, if it exists.  An image
 * will have a colormap if the drawable from which it was created has
 * a colormap, or if a colormap was set explicitely with
 * gdk_image_set_colormap().
 * 
 * Return value: colormap for the image
 **/
GdkColormap *
gdk_image_get_colormap (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);

  return image->colormap;
}

/**
 * gdk_image_get_image_type:
 * @image: a #GdkImage
 *
 * Determines the type of a given image.
 *
 * Return value: the #GdkImageType of the image
 *
 * Since: 2.22
 **/
GdkImageType
gdk_image_get_image_type (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->type;
}

/**
 * gdk_image_get_visual:
 * @image: a #GdkImage
 *
 * Determines the visual that was used to create the image.
 *
 * Return value: a #GdkVisual
 *
 * Since: 2.22
 **/
GdkVisual *
gdk_image_get_visual (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);

  return image->visual;
}

/**
 * gdk_image_get_byte_order:
 * @image: a #GdkImage
 *
 * Determines the byte order of the image.
 *
 * Return value: a #GdkVisual
 *
 * Since: 2.22
 **/
GdkByteOrder
gdk_image_get_byte_order (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->byte_order;
}

/**
 * gdk_image_get_width:
 * @image: a #GdkImage
 *
 * Determines the width of the image.
 *
 * Return value: the width
 *
 * Since: 2.22
 **/
gint
gdk_image_get_width (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->width;
}

/**
 * gdk_image_get_height:
 * @image: a #GdkImage
 *
 * Determines the height of the image.
 *
 * Return value: the height
 *
 * Since: 2.22
 **/
gint
gdk_image_get_height (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->height;
}

/**
 * gdk_image_get_depth:
 * @image: a #GdkImage
 *
 * Determines the depth of the image.
 *
 * Return value: the depth
 *
 * Since: 2.22
 **/
guint16
gdk_image_get_depth (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->depth;
}

/**
 * gdk_image_get_bytes_per_pixel:
 * @image: a #GdkImage
 *
 * Determines the number of bytes per pixel of the image.
 *
 * Return value: the bytes per pixel
 *
 * Since: 2.22
 **/
guint16
gdk_image_get_bytes_per_pixel (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->bpp;
}

/**
 * gdk_image_get_bytes_per_line:
 * @image: a #GdkImage
 *
 * Determines the number of bytes per line of the image.
 *
 * Return value: the bytes per line
 *
 * Since: 2.22
 **/
guint16
gdk_image_get_bytes_per_line (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->bpl;
}

/**
 * gdk_image_get_bits_per_pixel:
 * @image: a #GdkImage
 *
 * Determines the number of bits per pixel of the image.
 *
 * Return value: the bits per pixel
 *
 * Since: 2.22
 **/
guint16
gdk_image_get_bits_per_pixel (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);

  return image->bits_per_pixel;
}

/**
 * gdk_image_get_pixels:
 * @image: a #GdkImage
 *
 * Returns a pointer to the pixel data of the image.
 *
 * Returns: the pixel data of the image
 *
 * Since: 2.22
 */
gpointer
gdk_image_get_pixels (GdkImage *image)
{
  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);

  return image->mem;
}

/* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
 * up between n_images different images. possible_n_images gives
 * various divisors of N_REGIONS. The reason for allowing this
 * flexibility is that we want to create as few images as possible,
 * but we want to deal with the abberant systems that have a SHMMAX
 * limit less than
 *
 * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
 *
 * (Are there any such?)
 */
#define N_REGIONS 6
static const int possible_n_images[] = { 1, 2, 3, 6 };

/* We allocate one GdkScratchImageInfo structure for each
 * depth where we are allocating scratch images. (Future: one
 * per depth, per display)
 */
typedef struct _GdkScratchImageInfo GdkScratchImageInfo;

struct _GdkScratchImageInfo {
  gint depth;
  
  gint n_images;
  GdkImage *static_image[N_REGIONS];
  gint static_image_idx;

  /* In order to optimize filling fractions, we simultaneously fill in up
   * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
   * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
   * be tiled horizontally. One for images that are wider than
   * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
   * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
   * that we tile in horizontal rows.
   */
  gint horiz_idx;
  gint horiz_y;
  gint vert_idx;
  gint vert_x;
  
  /* tile_y1 and tile_y2 define the horizontal band into
   * which we are tiling images. tile_x is the x extent to
   * which that is filled
   */
  gint tile_idx;
  gint tile_x;
  gint tile_y1;
  gint tile_y2;

  GdkScreen *screen;
};

static GSList *scratch_image_infos = NULL;

static gboolean
allocate_scratch_images (GdkScratchImageInfo *info,
			 gint                 n_images,
			 gboolean             shared)
{
  gint i;
  
  for (i = 0; i < n_images; i++)
    {
      info->static_image[i] = _gdk_image_new_for_depth (info->screen,
							shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
							NULL,
							GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images), 
							GDK_SCRATCH_IMAGE_HEIGHT,
							info->depth);
      
      if (!info->static_image[i])
	{
	  gint j;
	  
	  for (j = 0; j < i; j++)
	    g_object_unref (info->static_image[j]);
	  
	  return FALSE;
	}
    }
  
  return TRUE;
}

static void
scratch_image_info_display_closed (GdkDisplay          *display,
                                   gboolean             is_error,
                                   GdkScratchImageInfo *image_info)
{
  gint i;

  g_signal_handlers_disconnect_by_func (display,
                                        scratch_image_info_display_closed,
                                        image_info);

  scratch_image_infos = g_slist_remove (scratch_image_infos, image_info);

  for (i = 0; i < image_info->n_images; i++)
    g_object_unref (image_info->static_image[i]);

  g_free (image_info);
}

static GdkScratchImageInfo *
scratch_image_info_for_depth (GdkScreen *screen,
			      gint       depth)
{
  GSList *tmp_list;
  GdkScratchImageInfo *image_info;
  gint i;

  tmp_list = scratch_image_infos;
  while (tmp_list)
    {
      image_info = tmp_list->data;
      if (image_info->depth == depth && image_info->screen == screen)
	return image_info;
      
      tmp_list = tmp_list->next;
    }

  image_info = g_new (GdkScratchImageInfo, 1);

  image_info->depth = depth;
  image_info->screen = screen;

  g_signal_connect (gdk_screen_get_display (screen), "closed",
                    G_CALLBACK (scratch_image_info_display_closed),
                    image_info);

  /* Try to allocate as few possible shared images */
  for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
    {
      if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
	{
	  image_info->n_images = possible_n_images[i];
	  break;
	}
    }

  /* If that fails, just allocate N_REGIONS normal images */
  if (i == G_N_ELEMENTS (possible_n_images))
    {
      allocate_scratch_images (image_info, N_REGIONS, FALSE);
      image_info->n_images = N_REGIONS;
    }

  image_info->static_image_idx = 0;

  image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
  image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
  image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
  image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;

  scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);

  return image_info;
}

/* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
   for performance evaluation. */

#undef NO_FLUSH

#ifdef VERBOSE
static gint sincelast;
#endif

static gint
alloc_scratch_image (GdkScratchImageInfo *image_info)
{
  if (image_info->static_image_idx == N_REGIONS)
    {
#ifndef NO_FLUSH
      gdk_flush ();
#endif
#ifdef VERBOSE
      g_print ("flush, %d puts since last flush\n", sincelast);
      sincelast = 0;
#endif
      image_info->static_image_idx = 0;

      /* Mark all regions that we might be filling in as completely
       * full, to force new tiles to be allocated for subsequent
       * images
       */
      image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
      image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
      image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
      image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
    }
  return image_info->static_image_idx++;
}

/**
 * _gdk_image_get_scratch:
 * @screen: a #GdkScreen
 * @width: desired width
 * @height: desired height
 * @depth: depth of image 
 * @x: X location within returned image of scratch image
 * @y: Y location within returned image of scratch image
 * 
 * Allocates an image of size width/height, up to a maximum
 * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
 * suitable to use on @screen.
 * 
 * Return value: a scratch image. This must be used by a
 *  call to gdk_image_put() before any other calls to
 *  _gdk_image_get_scratch()
 **/
GdkImage *
_gdk_image_get_scratch (GdkScreen   *screen,
			gint	     width,			
			gint	     height,
			gint	     depth,
			gint	    *x,
			gint	    *y)
{
  GdkScratchImageInfo *image_info;
  GdkImage *image;
  gint idx;
  
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  image_info = scratch_image_info_for_depth (screen, depth);

  if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
    {
      if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
	{
	  idx = alloc_scratch_image (image_info);
	  *x = 0;
	  *y = 0;
	}
      else
	{
	  if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
	    {
	      image_info->horiz_idx = alloc_scratch_image (image_info);
	      image_info->horiz_y = 0;
	    }
	  idx = image_info->horiz_idx;
	  *x = 0;
	  *y = image_info->horiz_y;
	  image_info->horiz_y += height;
	}
    }
  else
    {
      if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
	{
	  if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
	    {
	      image_info->vert_idx = alloc_scratch_image (image_info);
	      image_info->vert_x = 0;
	    }
	  idx = image_info->vert_idx;
	  *x = image_info->vert_x;
	  *y = 0;
	  /* using 3 and -4 would be slightly more efficient on 32-bit machines
	     with > 1bpp displays */
	  image_info->vert_x += (width + 7) & -8;
	}
      else
	{
	  if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
	    {
	      image_info->tile_y1 = image_info->tile_y2;
	      image_info->tile_x = 0;
	    }
	  if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
	    {
	      image_info->tile_idx = alloc_scratch_image (image_info);
	      image_info->tile_x = 0;
	      image_info->tile_y1 = 0;
	      image_info->tile_y2 = 0;
	    }
	  if (height + image_info->tile_y1 > image_info->tile_y2)
	    image_info->tile_y2 = height + image_info->tile_y1;
	  idx = image_info->tile_idx;
	  *x = image_info->tile_x;
	  *y = image_info->tile_y1;
	  image_info->tile_x += (width + 7) & -8;
	}
    }
  image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
  *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
#ifdef VERBOSE
  g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
  sincelast++;
#endif
  return image;
}

GdkImage*
gdk_image_new (GdkImageType  type,
	       GdkVisual    *visual,
	       gint          width,
	       gint          height)
{
  return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
				   visual, width, height, -1);
}

#define __GDK_IMAGE_C__
#include "gdkaliasdef.c"