]> Pileus Git - ~andy/gtk/blob - gdk/gdkimage.c
Translation updated by Ivar Smolin.
[~andy/gtk] / gdk / gdkimage.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30
31 #include "gdk.h"                /* For gdk_flush() */
32 #include "gdkimage.h"
33 #include "gdkprivate.h"
34 #include "gdkinternals.h"       /* For scratch_image code */
35 #include "gdkalias.h"
36
37 /**
38  * gdk_image_ref:
39  * @image: a #GdkImage
40  * 
41  * Deprecated function; use g_object_ref() instead.
42  * 
43  * Return value: the image
44  **/
45 GdkImage *
46 gdk_image_ref (GdkImage *image)
47 {
48   return (GdkImage *) g_object_ref (image);
49 }
50
51 /**
52  * gdk_image_unref:
53  * @image: a #GdkImage
54  * 
55  * Deprecated function; use g_object_unref() instead.
56  * 
57  **/
58 void
59 gdk_image_unref (GdkImage *image)
60 {
61   g_return_if_fail (GDK_IS_IMAGE (image));
62
63   g_object_unref (image);
64 }
65
66 /**
67  * gdk_image_get:
68  * @drawable: a #GdkDrawable
69  * @x: x coordinate in @window
70  * @y: y coordinate in @window
71  * @width: width of area in @window
72  * @height: height of area in @window
73  * 
74  * This is a deprecated wrapper for gdk_drawable_get_image();
75  * gdk_drawable_get_image() should be used instead. Or even better: in
76  * most cases gdk_pixbuf_get_from_drawable() is the most convenient
77  * choice.
78  * 
79  * Return value: a new #GdkImage or %NULL
80  **/
81 GdkImage*
82 gdk_image_get (GdkWindow *drawable,
83                gint       x,
84                gint       y,
85                gint       width,
86                gint       height)
87 {
88   g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
89   g_return_val_if_fail (x >= 0, NULL);
90   g_return_val_if_fail (y >= 0, NULL);
91   g_return_val_if_fail (width >= 0, NULL);
92   g_return_val_if_fail (height >= 0, NULL);
93   
94   return gdk_drawable_get_image (drawable, x, y, width, height);
95 }
96
97 /**
98  * gdk_image_set_colormap:
99  * @image: a #GdkImage
100  * @colormap: a #GdkColormap
101  * 
102  * Sets the colormap for the image to the given colormap.  Normally
103  * there's no need to use this function, images are created with the
104  * correct colormap if you get the image from a drawable. If you
105  * create the image from scratch, use the colormap of the drawable you
106  * intend to render the image to.
107  **/
108 void
109 gdk_image_set_colormap (GdkImage       *image,
110                         GdkColormap    *colormap)
111 {
112   g_return_if_fail (GDK_IS_IMAGE (image));
113   g_return_if_fail (GDK_IS_COLORMAP (colormap));
114
115   if (image->colormap != colormap)
116     {
117       if (image->colormap)
118         g_object_unref (image->colormap);
119
120       image->colormap = colormap;
121       g_object_ref (image->colormap);
122     }
123     
124 }
125
126 /**
127  * gdk_image_get_colormap:
128  * @image: a #GdkImage
129  * 
130  * Retrieves the colormap for a given image, if it exists.  An image
131  * will have a colormap if the drawable from which it was created has
132  * a colormap, or if a colormap was set explicitely with
133  * gdk_image_set_colormap().
134  * 
135  * Return value: colormap for the image
136  **/
137 GdkColormap *
138 gdk_image_get_colormap (GdkImage *image)
139 {
140   g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
141
142   return image->colormap;
143 }
144
145 /* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
146  * up between n_images different images. possible_n_images gives
147  * various divisors of N_REGIONS. The reason for allowing this
148  * flexibility is that we want to create as few images as possible,
149  * but we want to deal with the abberant systems that have a SHMMAX
150  * limit less than
151  *
152  * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
153  *
154  * (Are there any such?)
155  */
156 #define N_REGIONS 6
157 static const int possible_n_images[] = { 1, 2, 3, 6 };
158
159 /* We allocate one GdkScratchImageInfo structure for each
160  * depth where we are allocating scratch images. (Future: one
161  * per depth, per display)
162  */
163 typedef struct _GdkScratchImageInfo GdkScratchImageInfo;
164
165 struct _GdkScratchImageInfo {
166   gint depth;
167   
168   gint n_images;
169   GdkImage *static_image[N_REGIONS];
170   gint static_image_idx;
171
172   /* In order to optimize filling fractions, we simultaneously fill in up
173    * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
174    * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
175    * be tiled horizontally. One for images that are wider than
176    * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
177    * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
178    * that we tile in horizontal rows.
179    */
180   gint horiz_idx;
181   gint horiz_y;
182   gint vert_idx;
183   gint vert_x;
184   
185   /* tile_y1 and tile_y2 define the horizontal band into
186    * which we are tiling images. tile_x is the x extent to
187    * which that is filled
188    */
189   gint tile_idx;
190   gint tile_x;
191   gint tile_y1;
192   gint tile_y2;
193
194   GdkScreen *screen;
195 };
196
197 static GSList *scratch_image_infos = NULL;
198
199 static gboolean
200 allocate_scratch_images (GdkScratchImageInfo *info,
201                          gint                 n_images,
202                          gboolean             shared)
203 {
204   gint i;
205   
206   for (i = 0; i < n_images; i++)
207     {
208       info->static_image[i] = _gdk_image_new_for_depth (info->screen,
209                                                         shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
210                                                         NULL,
211                                                         GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images), 
212                                                         GDK_SCRATCH_IMAGE_HEIGHT,
213                                                         info->depth);
214       
215       if (!info->static_image[i])
216         {
217           gint j;
218           
219           for (j = 0; j < i; j++)
220             g_object_unref (info->static_image[j]);
221           
222           return FALSE;
223         }
224     }
225   
226   return TRUE;
227 }
228
229 static GdkScratchImageInfo *
230 scratch_image_info_for_depth (GdkScreen *screen,
231                               gint       depth)
232 {
233   GSList *tmp_list;
234   GdkScratchImageInfo *image_info;
235   gint i;
236
237   tmp_list = scratch_image_infos;
238   while (tmp_list)
239     {
240       image_info = tmp_list->data;
241       if (image_info->depth == depth && image_info->screen == screen)
242         return image_info;
243       
244       tmp_list = tmp_list->next;
245     }
246
247   image_info = g_new (GdkScratchImageInfo, 1);
248
249   image_info->depth = depth;
250   image_info->screen = screen;
251
252   /* Try to allocate as few possible shared images */
253   for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
254     {
255       if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
256         {
257           image_info->n_images = possible_n_images[i];
258           break;
259         }
260     }
261
262   /* If that fails, just allocate N_REGIONS normal images */
263   if (i == G_N_ELEMENTS (possible_n_images))
264     {
265       allocate_scratch_images (image_info, N_REGIONS, FALSE);
266       image_info->n_images = N_REGIONS;
267     }
268
269   image_info->static_image_idx = 0;
270
271   image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
272   image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
273   image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
274   image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
275
276   scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);
277
278   return image_info;
279 }
280
281 /* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
282    for performance evaluation. */
283
284 #undef NO_FLUSH
285
286 #ifdef VERBOSE
287 static gint sincelast;
288 #endif
289
290 static gint
291 alloc_scratch_image (GdkScratchImageInfo *image_info)
292 {
293   if (image_info->static_image_idx == N_REGIONS)
294     {
295 #ifndef NO_FLUSH
296       gdk_flush ();
297 #endif
298 #ifdef VERBOSE
299       g_print ("flush, %d puts since last flush\n", sincelast);
300       sincelast = 0;
301 #endif
302       image_info->static_image_idx = 0;
303
304       /* Mark all regions that we might be filling in as completely
305        * full, to force new tiles to be allocated for subsequent
306        * images
307        */
308       image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
309       image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
310       image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
311       image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
312     }
313   return image_info->static_image_idx++;
314 }
315
316 /**
317  * _gdk_image_get_scratch:
318  * @screen: a #GdkScreen
319  * @width: desired width
320  * @height: desired height
321  * @depth: depth of image 
322  * @x: X location within returned image of scratch image
323  * @y: Y location within returned image of scratch image
324  * 
325  * Allocates an image of size width/height, up to a maximum
326  * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
327  * suitable to use on @screen.
328  * 
329  * Return value: a scratch image. This must be used by a
330  *  call to gdk_image_put() before any other calls to
331  *  _gdk_image_get_scratch()
332  **/
333 GdkImage *
334 _gdk_image_get_scratch (GdkScreen   *screen,
335                         gint         width,                     
336                         gint         height,
337                         gint         depth,
338                         gint        *x,
339                         gint        *y)
340 {
341   GdkScratchImageInfo *image_info;
342   GdkImage *image;
343   gint idx;
344   
345   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
346
347   image_info = scratch_image_info_for_depth (screen, depth);
348
349   if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
350     {
351       if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
352         {
353           idx = alloc_scratch_image (image_info);
354           *x = 0;
355           *y = 0;
356         }
357       else
358         {
359           if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
360             {
361               image_info->horiz_idx = alloc_scratch_image (image_info);
362               image_info->horiz_y = 0;
363             }
364           idx = image_info->horiz_idx;
365           *x = 0;
366           *y = image_info->horiz_y;
367           image_info->horiz_y += height;
368         }
369     }
370   else
371     {
372       if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
373         {
374           if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
375             {
376               image_info->vert_idx = alloc_scratch_image (image_info);
377               image_info->vert_x = 0;
378             }
379           idx = image_info->vert_idx;
380           *x = image_info->vert_x;
381           *y = 0;
382           /* using 3 and -4 would be slightly more efficient on 32-bit machines
383              with > 1bpp displays */
384           image_info->vert_x += (width + 7) & -8;
385         }
386       else
387         {
388           if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
389             {
390               image_info->tile_y1 = image_info->tile_y2;
391               image_info->tile_x = 0;
392             }
393           if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
394             {
395               image_info->tile_idx = alloc_scratch_image (image_info);
396               image_info->tile_x = 0;
397               image_info->tile_y1 = 0;
398               image_info->tile_y2 = 0;
399             }
400           if (height + image_info->tile_y1 > image_info->tile_y2)
401             image_info->tile_y2 = height + image_info->tile_y1;
402           idx = image_info->tile_idx;
403           *x = image_info->tile_x;
404           *y = image_info->tile_y1;
405           image_info->tile_x += (width + 7) & -8;
406         }
407     }
408   image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
409   *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
410 #ifdef VERBOSE
411   g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
412   sincelast++;
413 #endif
414   return image;
415 }
416
417 GdkImage*
418 gdk_image_new (GdkImageType  type,
419                GdkVisual    *visual,
420                gint          width,
421                gint          height)
422 {
423   return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
424                                    visual, width, height, -1);
425 }
426
427 #define __GDK_IMAGE_C__
428 #include "gdkaliasdef.c"