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