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