]> Pileus Git - ~andy/gtk/blob - docs/reference/gdk/tmpl/rgb.sgml
fixed the example code (return TRUE from the expose_event handler).
[~andy/gtk] / docs / reference / gdk / tmpl / rgb.sgml
1 <!-- ##### SECTION Title ##### -->
2 GdkRGB
3
4 <!-- ##### SECTION Short_Description ##### -->
5 renders RGB, grayscale, or indexed image data to a #GdkDrawable
6
7 <!-- ##### SECTION Long_Description ##### -->
8
9 <para>
10 GdkRGB is a low-level module which renders RGB, grayscale, and indexed
11 colormap images to a #GdkDrawable. It does this as efficiently as
12 possible, handling issues such as colormaps, visuals, dithering,
13 temporary buffers, and so on. Most code should use the higher-level
14 #GdkPixbuf features in place of this module; for example,
15 gdk_pixbuf_render_to_drawable() uses GdkRGB in its implementation.
16 </para>
17
18 <para>
19 GdkRGB allocates a color cube to use when rendering images.  You can
20 set the threshold for installing colormaps with
21 gdk_rgb_set_min_colors(). The default is 5x5x5 (125). If a colorcube
22 of this size or larger can be allocated in the default colormap, then
23 that's done. Otherwise, GdkRGB creates its own private colormap.
24 Setting it to 0 means that it always tries to use the default
25 colormap, and setting it to 216 means that it always creates a private
26 one if it cannot allocate the 6x6x6 colormap in the default. If you
27 always want a private colormap (to avoid consuming too many colormap
28 entries for other apps, say), you can use 
29 <literal>gdk_rgb_set_install(TRUE)</literal>.
30 Setting the value greater than 216 exercises a bug in older versions
31 of GdkRGB. Note, however, that setting it to 0 doesn't let you get
32 away with ignoring the colormap and visual - a colormap is always
33 created in grayscale and direct color modes, and the visual is changed
34 in cases where a "better" visual than the default is available.
35 </para>
36
37 <example>
38 <title>A simple example program using GdkRGB.</title>
39 <programlisting>
40 &num;include &lt;gtk/gtk.h&gt;
41
42 &num;define IMAGE_WIDTH 256
43 &num;define IMAGE_HEIGHT        256
44
45 guchar rgbbuf[IMAGE_WIDTH * IMAGE_HEIGHT * 3];
46
47 gboolean on_darea_expose (GtkWidget *widget,
48                           GdkEventExpose *event,
49                           gpointer user_data);
50
51 int
52 main (int argc, char *argv[])
53 {
54   GtkWidget *window, *darea;
55   gint x, y;
56   guchar *pos;
57
58   gtk_init (&amp;argc, &amp;argv);
59
60   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
61   darea = gtk_drawing_area_new (<!-- -->);
62   gtk_widget_set_size_request (darea, IMAGE_WIDTH, IMAGE_HEIGHT);
63   gtk_container_add (GTK_CONTAINER (window), darea);
64   gtk_signal_connect (GTK_OBJECT (darea), "expose-event",
65                       GTK_SIGNAL_FUNC (on_darea_expose), NULL);
66   gtk_widget_show_all (window);
67
68   /* Set up the RGB buffer. */
69   pos = rgbbuf;
70   for (y = 0; y &lt; IMAGE_HEIGHT; y++)
71     {
72       for (x = 0; x &lt; IMAGE_WIDTH; x++)
73         {
74           *pos++ = x - x % 32;                  /* Red. */
75           *pos++ = (x / 32) * 4 + y - y % 32;   /* Green. */
76           *pos++ = y - y % 32;                  /* Blue. */
77         }
78     }
79
80   gtk_main (<!-- -->);
81   return 0;
82 }
83
84
85 gboolean
86 on_darea_expose (GtkWidget *widget,
87                  GdkEventExpose *event,
88                  gpointer user_data)
89 {
90   gdk_draw_rgb_image (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
91                       0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
92                       GDK_RGB_DITHER_MAX, rgbbuf, IMAGE_WIDTH * 3);
93
94   return TRUE;
95 }
96 </programlisting>
97 </example>
98
99 <!-- ##### SECTION See_Also ##### -->
100 <para>
101 <variablelist>
102
103 <varlistentry>
104 <term>#GdkColor</term>
105 <listitem><para>The underlying GDK mechanism for allocating
106 colors.</para></listitem>
107 </varlistentry>
108
109 <varlistentry>
110 <term>#GdkPixbuf and gdk_pixbuf_render_to_drawable()</term>
111 <listitem><para>Higher-level image handling.</para></listitem>
112 </varlistentry>
113
114 </variablelist>
115
116 </para>
117
118 <!-- ##### FUNCTION gdk_rgb_init ##### -->
119 <para>
120 This function no longer does anything at all. It's completely useless
121 (and harmless).
122 </para>
123
124
125
126 <!-- ##### FUNCTION gdk_draw_rgb_image ##### -->
127 <para>
128 Draws an RGB image in the drawable. This is the core GdkRGB
129 function, and likely the only one you will need to use.
130 </para>
131
132 <para>
133 The @rowstride parameter allows for lines to be aligned more flexibly.
134 For example, lines may be allocated to begin on 32-bit boundaries,
135 even if the width of the rectangle is odd. Rowstride is also useful
136 when drawing a subrectangle of a larger image in memory. Finally, to
137 replicate the same line a number of times, the trick of setting
138 @rowstride to 0 is allowed.
139 </para>
140
141 <para>
142 In general, for 0 &lt;= i &lt; @width and 0 &lt;= j &lt; height,
143 the pixel (x + i, y + j) is colored with red value @rgb_buf[@j *
144 @rowstride + @i * 3], green value @rgb_buf[@j * @rowstride + @i * 3 +
145 1], and blue value @rgb_buf[@j * @rowstride + @i * 3 + 2].
146 </para>
147
148 @drawable: The #GdkDrawable to draw in (usually a #GdkWindow).
149 @gc: The graphics context (all GDK drawing operations require one; its
150 contents are ignored).
151 @x: The x coordinate of the top-left corner in the drawable.
152 @y: The y coordinate of the top-left corner in the drawable.
153 @width: The width of the rectangle to be drawn.
154 @height: The height of the rectangle to be drawn.
155 @dith: A #GdkRgbDither value, selecting the desired dither mode.
156 @rgb_buf: The pixel data, represented as packed 24-bit data.
157 @rowstride: The number of bytes from the start of one row in @rgb_buf to the
158 start of the next.
159
160
161 <!-- ##### FUNCTION gdk_draw_rgb_image_dithalign ##### -->
162 <para>
163 Draws an RGB image in the drawable, with an adjustment for dither alignment.
164 </para>
165
166 <para>
167 This function is useful when drawing dithered images into a window
168 that may be scrolled. Pixel (x, y) will be drawn dithered as if its
169 actual location is (x + @xdith, y + @ydith). Thus, if you draw an
170 image into a window using zero dither alignment, then scroll up one
171 pixel, subsequent draws to the window should have @ydith = 1.
172 </para>
173
174 <para>
175 Setting the dither alignment correctly allows updating of small parts
176 of the screen while avoiding visible "seams" between the different
177 dither textures.
178 </para>
179
180 @drawable: The #GdkDrawable to draw in (usually a #GdkWindow).
181 @gc: The graphics context.
182 @x: The x coordinate of the top-left corner in the drawable.
183 @y: The y coordinate of the top-left corner in the drawable.
184 @width: The width of the rectangle to be drawn.
185 @height: The height of the rectangle to be drawn.
186 @dith: A #GdkRgbDither value, selecting the desired dither mode.
187 @rgb_buf: The pixel data, represented as packed 24-bit data.
188 @rowstride: The number of bytes from the start of one row in @rgb_buf to the
189 start of the next.
190 @xdith: An x offset for dither alignment.
191 @ydith: A y offset for dither alignment.
192
193
194 <!-- ##### FUNCTION gdk_draw_indexed_image ##### -->
195 <para>
196 Draws an indexed image in the drawable, using a #GdkRgbCmap to assign
197 actual colors to the color indices.
198 </para>
199
200 @drawable: The #GdkDrawable to draw in (usually a #GdkWindow).
201 @gc: The graphics context.
202 @x: The x coordinate of the top-left corner in the drawable.
203 @y: The y coordinate of the top-left corner in the drawable.
204 @width: The width of the rectangle to be drawn.
205 @height: The height of the rectangle to be drawn.
206 @dith: A #GdkRgbDither value, selecting the desired dither mode.
207 @buf: The pixel data, represented as 8-bit color indices.
208 @rowstride: The number of bytes from the start of one row in @buf to the
209 start of the next.
210 @cmap: The #GdkRgbCmap used to assign colors to the color indices.
211
212
213 <!-- ##### FUNCTION gdk_draw_gray_image ##### -->
214 <para>
215 Draws a grayscale image in the drawable.
216
217 </para>
218
219 @drawable: The #GdkDrawable to draw in (usually a #GdkWindow).
220 @gc: The graphics context.
221 @x: The x coordinate of the top-left corner in the drawable.
222 @y: The y coordinate of the top-left corner in the drawable.
223 @width: The width of the rectangle to be drawn.
224 @height: The height of the rectangle to be drawn.
225 @dith: A #GdkRgbDither value, selecting the desired dither mode.
226 @buf: The pixel data, represented as 8-bit gray values.
227 @rowstride: The number of bytes from the start of one row in @buf to the
228 start of the next.
229
230
231 <!-- ##### FUNCTION gdk_draw_rgb_32_image ##### -->
232 <para>
233 Draws a padded RGB image in the drawable. The image is stored as one
234 pixel per 32-bit word. It is laid out as a red byte, a green byte, a
235 blue byte, and a padding byte.
236 </para>
237
238 <para>
239 It's unlikely that this function will give significant performance
240 gains in practice. In my experience, the performance gain from having
241 pixels aligned to 32-bit boundaries is cancelled out by the increased
242 memory bandwidth.
243 </para>
244
245 @drawable: The #GdkDrawable to draw in (usually a #GdkWindow).
246 @gc: The graphics context.
247 @x: The x coordinate of the top-left corner in the drawable.
248 @y: The y coordinate of the top-left corner in the drawable.
249 @width: The width of the rectangle to be drawn.
250 @height: The height of the rectangle to be drawn.
251 @dith: A #GdkRgbDither value, selecting the desired dither mode.
252 @buf: The pixel data, represented as padded 32-bit data.
253 @rowstride: The number of bytes from the start of one row in @buf to the
254 start of the next.
255
256
257 <!-- ##### FUNCTION gdk_draw_rgb_32_image_dithalign ##### -->
258 <para>
259
260 </para>
261
262 @drawable: 
263 @gc: 
264 @x: 
265 @y: 
266 @width: 
267 @height: 
268 @dith: 
269 @buf: 
270 @rowstride: 
271 @xdith: 
272 @ydith: 
273
274
275 <!-- ##### ENUM GdkRgbDither ##### -->
276 <para>
277 Selects whether or not GdkRGB applies dithering
278 to the image on display. There are three values:
279 </para>
280
281 <itemizedlist>
282
283 <listitem>
284 <para>
285 %GDK_RGB_DITHER_NONE: Never use dithering.
286 </para>
287 </listitem>
288
289 <listitem>
290 <para>
291 %GDK_RGB_DITHER_NORMAL: Use dithering in 8 bits per pixel (and below)
292 only.
293 </para>
294 </listitem>
295
296 <listitem>
297 <para>
298 %GDK_RGB_DITHER_MAX: Use dithering in 16 bits per pixel and below.
299 </para>
300 </listitem>
301
302 </itemizedlist>
303
304 <para>
305 Since GdkRGB currently only handles images with 8 bits per component,
306 dithering on 24 bit per pixel displays is a moot point.
307 </para>
308
309 @GDK_RGB_DITHER_NONE: 
310 @GDK_RGB_DITHER_NORMAL: 
311 @GDK_RGB_DITHER_MAX: 
312
313 <!-- ##### FUNCTION gdk_rgb_cmap_new ##### -->
314 <para>
315 Creates a new #GdkRgbCmap structure. The cmap maps color indexes to
316 RGB colors. If @n_colors is less than 256, then images containing
317 color values greater than or equal to @n_colors will produce undefined
318 results, including possibly segfaults.
319 </para>
320
321 @colors: The colors, represented as 0xRRGGBB integer values.
322 @n_colors: The number of colors in the cmap.
323 @Returns: The newly created #GdkRgbCmap
324
325
326 <!-- ##### FUNCTION gdk_rgb_cmap_free ##### -->
327 <para>
328 Frees the memory associated with a #GdkRgbCmap created by gdk_rgb_cmap_new().
329 </para>
330
331 @cmap: The #GdkRgbCmap to free.
332
333
334 <!-- ##### STRUCT GdkRgbCmap ##### -->
335 <para>
336 A private data structure which maps color indices to actual RGB
337 colors. This is used only for gdk_draw_indexed_image().
338 </para>
339
340 @colors: 
341 @n_colors: 
342
343 <!-- ##### FUNCTION gdk_rgb_gc_set_foreground ##### -->
344 <para>
345 Sets the foreground color in @gc to the specified color (or the
346 closest approximation, in the case of limited visuals).
347 </para>
348
349 @gc: The #GdkGC to modify.
350 @rgb: The color, represented as a 0xRRGGBB integer value.
351
352
353 <!-- ##### FUNCTION gdk_rgb_gc_set_background ##### -->
354 <para>
355 Sets the background color in @gc to the specified color (or the
356 closest approximation, in the case of limited visuals).
357 </para>
358
359 @gc: The #GdkGC to modify.
360 @rgb: The color, represented as a 0xRRGGBB integer value.
361
362
363 <!-- ##### FUNCTION gdk_rgb_xpixel_from_rgb ##### -->
364 <para>
365 Finds the X pixel closest in color to the @rgb color specified. This
366 value may be used to set the <structfield>pixel</structfield> field of
367 a #GdkColor struct.
368 </para>
369
370 @rgb: The color, represented as a 0xRRGGBB integer value.
371 @Returns: The X pixel value.
372
373
374 <!-- ##### FUNCTION gdk_rgb_find_color ##### -->
375 <para>
376
377 </para>
378
379 @colormap: 
380 @color: 
381
382
383 <!-- ##### FUNCTION gdk_rgb_set_install ##### -->
384 <para>
385 If @install is %TRUE, directs GdkRGB to always install a new "private"
386 colormap rather than trying to find a best fit with the colors already
387 allocated. Ordinarily, GdkRGB will install a colormap only if a
388 sufficient cube cannot be allocated.
389 </para>
390
391 <para>
392 A private colormap has more colors, leading to better quality display,
393 but also leads to the dreaded "colormap flashing" effect.
394 </para>
395
396 @install: %TRUE to set install mode.
397
398
399 <!-- ##### FUNCTION gdk_rgb_set_min_colors ##### -->
400 <para>
401 Sets the minimum number of colors for the color cube. Generally,
402 GdkRGB tries to allocate the largest color cube it can. If it can't
403 allocate a color cube at least as large as @min_colors, it installs a
404 private colormap.
405 </para>
406
407 @min_colors: The minimum number of colors accepted.
408
409
410 <!-- ##### FUNCTION gdk_rgb_get_visual ##### -->
411 <para>
412 </para>
413
414 @Returns: 
415
416
417 <!-- ##### FUNCTION gdk_rgb_get_colormap ##### -->
418 <para>
419
420 </para>
421
422 @Returns: 
423
424
425 <!-- ##### MACRO gdk_rgb_get_cmap ##### -->
426 <para>
427 Gets the colormap set by GdkRGB. This colormap and the corresponding
428 visual should be used when creating windows that will be drawn in by GdkRGB.
429 </para>
430
431 @Returns: The #GdkColormap set by GdkRGB.
432
433
434 <!-- ##### FUNCTION gdk_rgb_ditherable ##### -->
435 <para>
436 Determines whether the visual is ditherable. This function may be
437 useful for presenting a user interface choice to the user about which
438 dither mode is desired; if the display is not ditherable, it may make
439 sense to gray out or hide the corresponding UI widget.
440 </para>
441
442 @Returns: %TRUE if the visual is ditherable.
443
444
445 <!-- ##### FUNCTION gdk_rgb_set_verbose ##### -->
446 <para>
447 Sets the "verbose" flag. This is generally only useful for debugging.
448 </para>
449
450 @verbose: %TRUE if verbose messages are desired.
451
452