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/.
29 #include <sys/types.h>
31 #include "gdk.h" /* For gdk_flush() */
33 #include "gdkprivate.h"
34 #include "gdkinternals.h" /* For scratch_image code */
40 * Deprecated function; use g_object_ref() instead.
42 * Return value: the image
45 gdk_image_ref (GdkImage *image)
47 return (GdkImage *) g_object_ref (image);
54 * Deprecated function; use g_object_unref() instead.
58 gdk_image_unref (GdkImage *image)
60 g_return_if_fail (GDK_IS_IMAGE (image));
62 g_object_unref (image);
67 * @drawable: a #GdkDrawable
68 * @x: x coordinate in @window
69 * @y: y coordinate in @window
70 * @width: width of area in @window
71 * @height: height of area in @window
73 * This is a deprecated wrapper for gdk_drawable_get_image();
74 * gdk_drawable_get_image() should be used instead. Or even better: in
75 * most cases gdk_pixbuf_get_from_drawable() is the most convenient
78 * Return value: a new #GdkImage or %NULL
81 gdk_image_get (GdkWindow *drawable,
87 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
88 g_return_val_if_fail (x >= 0, NULL);
89 g_return_val_if_fail (y >= 0, NULL);
90 g_return_val_if_fail (width >= 0, NULL);
91 g_return_val_if_fail (height >= 0, NULL);
93 return gdk_drawable_get_image (drawable, x, y, width, height);
97 * gdk_image_set_colormap:
99 * @colormap: a #GdkColormap
101 * Sets the colormap for the image to the given colormap. Normally
102 * there's no need to use this function, images are created with the
103 * correct colormap if you get the image from a drawable. If you
104 * create the image from scratch, use the colormap of the drawable you
105 * intend to render the image to.
108 gdk_image_set_colormap (GdkImage *image,
109 GdkColormap *colormap)
111 g_return_if_fail (GDK_IS_IMAGE (image));
112 g_return_if_fail (GDK_IS_COLORMAP (colormap));
114 if (image->colormap != colormap)
117 g_object_unref (image->colormap);
119 image->colormap = colormap;
120 g_object_ref (image->colormap);
126 * gdk_image_get_colormap:
127 * @image: a #GdkImage
129 * Retrieves the colormap for a given image, if it exists. An image
130 * will have a colormap if the drawable from which it was created has
131 * a colormap, or if a colormap was set explicitely with
132 * gdk_image_set_colormap().
134 * Return value: colormap for the image
137 gdk_image_get_colormap (GdkImage *image)
139 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
141 return image->colormap;
144 /* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
145 * up between n_images different images. possible_n_images gives
146 * various divisors of N_REGIONS. The reason for allowing this
147 * flexibility is that we want to create as few images as possible,
148 * but we want to deal with the abberant systems that have a SHMMAX
151 * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
153 * (Are there any such?)
156 static const int possible_n_images[] = { 1, 2, 3, 6 };
158 /* We allocate one GdkScratchImageInfo structure for each
159 * depth where we are allocating scratch images. (Future: one
160 * per depth, per display)
162 typedef struct _GdkScratchImageInfo GdkScratchImageInfo;
164 struct _GdkScratchImageInfo {
168 GdkImage *static_image[N_REGIONS];
169 gint static_image_idx;
171 /* In order to optimize filling fractions, we simultaneously fill in up
172 * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
173 * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
174 * be tiled horizontally. One for images that are wider than
175 * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
176 * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
177 * that we tile in horizontal rows.
184 /* tile_y1 and tile_y2 define the horizontal band into
185 * which we are tiling images. tile_x is the x extent to
186 * which that is filled
196 static GSList *scratch_image_infos = NULL;
199 allocate_scratch_images (GdkScratchImageInfo *info,
205 for (i = 0; i < n_images; i++)
207 info->static_image[i] = _gdk_image_new_for_depth (info->screen,
208 shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
210 GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images),
211 GDK_SCRATCH_IMAGE_HEIGHT,
214 if (!info->static_image[i])
218 for (j = 0; j < i; j++)
219 g_object_unref (info->static_image[i]);
228 static GdkScratchImageInfo *
229 scratch_image_info_for_depth (GdkScreen *screen,
233 GdkScratchImageInfo *image_info;
236 tmp_list = scratch_image_infos;
239 image_info = tmp_list->data;
240 if (image_info->depth == depth && image_info->screen == screen)
243 tmp_list = tmp_list->next;
246 image_info = g_new (GdkScratchImageInfo, 1);
248 image_info->depth = depth;
249 image_info->screen = screen;
251 /* Try to allocate as few possible shared images */
252 for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
254 if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
256 image_info->n_images = possible_n_images[i];
261 /* If that fails, just allocate N_REGIONS normal images */
262 if (i == G_N_ELEMENTS (possible_n_images))
264 allocate_scratch_images (image_info, N_REGIONS, FALSE);
265 image_info->n_images = N_REGIONS;
268 image_info->static_image_idx = 0;
270 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
271 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
272 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
273 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
275 scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);
280 /* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
281 for performance evaluation. */
286 static gint sincelast;
290 alloc_scratch_image (GdkScratchImageInfo *image_info)
292 if (image_info->static_image_idx == N_REGIONS)
298 g_print ("flush, %d puts since last flush\n", sincelast);
301 image_info->static_image_idx = 0;
303 /* Mark all regions that we might be filling in as completely
304 * full, to force new tiles to be allocated for subsequent
307 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
308 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
309 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
310 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
312 return image_info->static_image_idx++;
316 * _gdk_image_get_scratch:
317 * @screen: a #GdkScreen
318 * @width: desired width
319 * @height: desired height
320 * @depth: depth of image
321 * @x: X location within returned image of scratch image
322 * @y: Y location within returned image of scratch image
324 * Allocates an image of size width/height, up to a maximum
325 * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
326 * suitable to use on @screen.
328 * Return value: a scratch image. This must be used by a
329 * call to gdk_image_put() before any other calls to
330 * _gdk_image_get_scratch()
333 _gdk_image_get_scratch (GdkScreen *screen,
340 GdkScratchImageInfo *image_info;
344 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
346 image_info = scratch_image_info_for_depth (screen, depth);
348 if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
350 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
352 idx = alloc_scratch_image (image_info);
358 if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
360 image_info->horiz_idx = alloc_scratch_image (image_info);
361 image_info->horiz_y = 0;
363 idx = image_info->horiz_idx;
365 *y = image_info->horiz_y;
366 image_info->horiz_y += height;
371 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
373 if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
375 image_info->vert_idx = alloc_scratch_image (image_info);
376 image_info->vert_x = 0;
378 idx = image_info->vert_idx;
379 *x = image_info->vert_x;
381 /* using 3 and -4 would be slightly more efficient on 32-bit machines
382 with > 1bpp displays */
383 image_info->vert_x += (width + 7) & -8;
387 if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
389 image_info->tile_y1 = image_info->tile_y2;
390 image_info->tile_x = 0;
392 if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
394 image_info->tile_idx = alloc_scratch_image (image_info);
395 image_info->tile_x = 0;
396 image_info->tile_y1 = 0;
397 image_info->tile_y2 = 0;
399 if (height + image_info->tile_y1 > image_info->tile_y2)
400 image_info->tile_y2 = height + image_info->tile_y1;
401 idx = image_info->tile_idx;
402 *x = image_info->tile_x;
403 *y = image_info->tile_y1;
404 image_info->tile_x += (width + 7) & -8;
407 image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
408 *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
410 g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
417 gdk_image_new (GdkImageType type,
422 return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
423 visual, width, height, -1);