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/.
28 #include <sys/types.h>
30 #include "gdk.h" /* For gdk_flush() */
32 #include "gdkprivate.h"
33 #include "gdkinternals.h" /* For scratch_image code */
39 * Deprecated function; use g_object_ref() instead.
41 * Return value: the image
44 gdk_image_ref (GdkImage *image)
46 return (GdkImage *) g_object_ref (G_OBJECT (image));
53 * Deprecated function; use g_object_unref() instead.
57 gdk_image_unref (GdkImage *image)
59 g_return_if_fail (GDK_IS_IMAGE (image));
61 g_object_unref (G_OBJECT (image));
66 * @drawable: a #GdkDrawable
67 * @x: x coordinate in @window
68 * @y: y coordinate in @window
69 * @width: width of area in @window
70 * @height: height of area in @window
72 * This is a deprecated wrapper for gdk_drawable_get_image();
73 * gdk_drawable_get_image() should be used instead. Or even better: in
74 * most cases gdk_pixbuf_get_from_drawable() is the most convenient
77 * Return value: a new #GdkImage or %NULL
80 gdk_image_get (GdkWindow *drawable,
86 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
87 g_return_val_if_fail (x >= 0, NULL);
88 g_return_val_if_fail (y >= 0, NULL);
89 g_return_val_if_fail (width >= 0, NULL);
90 g_return_val_if_fail (height >= 0, NULL);
92 return gdk_drawable_get_image (drawable, x, y, width, height);
96 * gdk_image_set_colormap:
98 * @colormap: a #GdkColormap
100 * Sets the colormap for the image to the given colormap. Normally
101 * there's no need to use this function, images are created with the
102 * correct colormap if you get the image from a drawable. If you
103 * create the image from scratch, use the colormap of the drawable you
104 * intend to render the image to.
107 gdk_image_set_colormap (GdkImage *image,
108 GdkColormap *colormap)
110 g_return_if_fail (GDK_IS_IMAGE (image));
111 g_return_if_fail (GDK_IS_COLORMAP (colormap));
113 if (image->colormap != colormap)
116 g_object_unref (G_OBJECT (image->colormap));
118 image->colormap = colormap;
119 g_object_ref (G_OBJECT (image->colormap));
125 * gdk_image_get_colormap:
126 * @image: a #GdkImage
128 * Retrieves the colormap for a given image, if it exists. An image
129 * will have a colormap if the drawable from which it was created has
130 * a colormap, or if a colormap was set explicitely with
131 * gdk_image_set_colormap().
133 * Return value: colormap for the image
136 gdk_image_get_colormap (GdkImage *image)
138 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
140 return image->colormap;
143 /* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
144 * up between n_images different images. possible_n_images gives
145 * various divisors of N_REGIONS. The reason for allowing this
146 * flexibility is that we want to create as few images as possible,
147 * but we want to deal with the abberant systems that have a SHMMAX
150 * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
152 * (Are there any such?)
155 static const int possible_n_images[] = { 1, 2, 3, 6 };
157 /* We allocate one GdkScratchImageInfo structure for each
158 * depth where we are allocating scratch images. (Future: one
159 * per depth, per display)
161 typedef struct _GdkScratchImageInfo GdkScratchImageInfo;
163 struct _GdkScratchImageInfo {
167 GdkImage *static_image[N_REGIONS];
168 gint static_image_idx;
170 /* In order to optimize filling fractions, we simultaneously fill in up
171 * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
172 * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
173 * be tiled horizontally. One for images that are wider than
174 * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
175 * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
176 * that we tile in horizontal rows.
183 /* tile_y1 and tile_y2 define the horizontal band into
184 * which we are tiling images. tile_x is the x extent to
185 * which that is filled
195 static GSList *scratch_image_infos = NULL;
198 allocate_scratch_images (GdkScratchImageInfo *info,
204 for (i = 0; i < n_images; i++)
206 info->static_image[i] = _gdk_image_new_for_depth (info->screen,
207 shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
209 GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images),
210 GDK_SCRATCH_IMAGE_HEIGHT,
213 if (!info->static_image[i])
217 for (j = 0; j < i; j++)
218 gdk_image_unref (info->static_image[i]);
227 static GdkScratchImageInfo *
228 scratch_image_info_for_depth (GdkScreen *screen,
232 GdkScratchImageInfo *image_info;
235 tmp_list = scratch_image_infos;
238 image_info = tmp_list->data;
239 if (image_info->depth == depth && image_info->screen == screen)
242 tmp_list = tmp_list->next;
245 image_info = g_new (GdkScratchImageInfo, 1);
247 image_info->depth = depth;
248 image_info->screen = screen;
250 /* Try to allocate as few possible shared images */
251 for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
253 if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
255 image_info->n_images = possible_n_images[i];
260 /* If that fails, just allocate N_REGIONS normal images */
261 if (i == G_N_ELEMENTS (possible_n_images))
263 allocate_scratch_images (image_info, N_REGIONS, FALSE);
264 image_info->n_images = N_REGIONS;
267 image_info->static_image_idx = 0;
269 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
270 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
271 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
272 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
274 scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);
279 /* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
280 for performance evaluation. */
285 static gint sincelast;
289 alloc_scratch_image (GdkScratchImageInfo *image_info)
291 if (image_info->static_image_idx == N_REGIONS)
297 g_print ("flush, %d puts since last flush\n", sincelast);
300 image_info->static_image_idx = 0;
302 /* Mark all regions that we might be filling in as completely
303 * full, to force new tiles to be allocated for subsequent
306 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
307 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
308 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
309 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
311 return image_info->static_image_idx++;
315 * _gdk_image_get_scratch:
316 * @screen: a #GdkScreen
317 * @width: desired width
318 * @height: desired height
319 * @depth: depth of image
320 * @x: X location within returned image of scratch image
321 * @y: Y location within returned image of scratch image
323 * Allocates an image of size width/height, up to a maximum
324 * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
325 * suitable to use on @screen.
327 * Return value: a scratch image. This must be used by a
328 * call to gdk_image_put() before any other calls to
329 * _gdk_image_get_scratch()
332 _gdk_image_get_scratch (GdkScreen *screen,
339 GdkScratchImageInfo *image_info;
343 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
345 image_info = scratch_image_info_for_depth (screen, depth);
347 if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
349 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
351 idx = alloc_scratch_image (image_info);
357 if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
359 image_info->horiz_idx = alloc_scratch_image (image_info);
360 image_info->horiz_y = 0;
362 idx = image_info->horiz_idx;
364 *y = image_info->horiz_y;
365 image_info->horiz_y += height;
370 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
372 if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
374 image_info->vert_idx = alloc_scratch_image (image_info);
375 image_info->vert_x = 0;
377 idx = image_info->vert_idx;
378 *x = image_info->vert_x;
380 /* using 3 and -4 would be slightly more efficient on 32-bit machines
381 with > 1bpp displays */
382 image_info->vert_x += (width + 7) & -8;
386 if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
388 image_info->tile_y1 = image_info->tile_y2;
389 image_info->tile_x = 0;
391 if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
393 image_info->tile_idx = alloc_scratch_image (image_info);
394 image_info->tile_x = 0;
395 image_info->tile_y1 = 0;
396 image_info->tile_y2 = 0;
398 if (height + image_info->tile_y1 > image_info->tile_y2)
399 image_info->tile_y2 = height + image_info->tile_y1;
400 idx = image_info->tile_idx;
401 *x = image_info->tile_x;
402 *y = image_info->tile_y1;
403 image_info->tile_x += (width + 7) & -8;
406 image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
407 *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
409 g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
416 gdk_image_new (GdkImageType type,
421 return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
422 visual, width, height, -1);