1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Drawing
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="GDK Basics" href="cha-gdk.html">
<link rel="PREVIOUS" title="Graphics Contexts" href=
"sec-gc.html">
<link rel="NEXT" title="GDK Resource Management" href=
"sec-gdkresourcemgmt.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="sec-gc.html"><font color="#0000ff" size="2">
<b><<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="sec-gdkresourcemgmt.html"><font color=
"#0000ff" size="2"><b>Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z132">Drawing</a>
</h1>
<p>
Once you understand drawables, colors, visuals, graphics
contexts, and fonts, actually drawing is very simple. This
section is a quick summary of the GDK drawing routines.
Remember that drawing is a server-side operation; for
example, if you ask to draw a line, Xlib will send the
line's endpoints to the server, and the server will do the
actual drawing using the specified GC (the GC is also a
server-side resource). Often this is an important
performance consideration.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z133">Points</a>
</h2>
<p>
You can draw a single point with <tt class="FUNCTION">
gdk_draw_point()</tt>, or multiple points with <tt class=
"FUNCTION">gdk_draw_points()</tt> (<a href=
"z132.html#FL-GDKPOINTS">Figure 17</a>). The point is
drawn in the current foreground color. Multiple points
are given as an array. A <span class="STRUCTNAME">
GdkPoint</span> looks like this:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
typedef struct _GdkPoint GdkPoint;
struct _GdkPoint
{
gint16 x;
gint16 y;
};
</pre>
</td>
</tr>
</table>
<p>
Remember that X coordinates start in the top left corner,
are relative to the drawable, and may not overflow a
signed sixteen-bit integer.
</p>
<div class="FIGURE">
<a name="FL-GDKPOINTS"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKPOINTS.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_point</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_points</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, GdkPoint* <tt
class="PARAMETER"><i>points</i></tt>, gint <tt class=
"PARAMETER"><i>npoints</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 17. Drawing Points</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z134">Lines</a>
</h2>
<p>
To draw a single line, pass its endpoints as arguments to
<tt class="FUNCTION">gdk_draw_line()</tt> (<a href=
"z132.html#FL-GDKLINES">Figure 18</a>). To draw connected
lines, you pass a list of points to <tt class="FUNCTION">
gdk_draw_lines()</tt>; GDK will "connect the dots." To
draw multiple lines that aren't necessarily connected,
pass a list of segments to <tt class="FUNCTION">
gdk_draw_segments()</tt>; a <span class="STRUCTNAME">
GdkSegment</span> is:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
typedef struct _GdkSegment GdkSegment;
struct _GdkSegment
{
gint16 x1;
gint16 y1;
gint16 x2;
gint16 y2;
};
</pre>
</td>
</tr>
</table>
<p>
If lines or segments drawn in the same request meet at
their endpoints, they are joined with the join style from
the GC.
</p>
<div class="FIGURE">
<a name="FL-GDKLINES"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKLINES.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_line</tt></code>(GdkDrawable* <tt
class="PARAMETER"><i>drawable</i></tt>, GdkGC* <tt
class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>x1</i></tt>, gint <tt class=
"PARAMETER"><i>y1</i></tt>, gint <tt class=
"PARAMETER"><i>x2</i></tt>, gint <tt class=
"PARAMETER"><i>y2</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_lines</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, GdkPoint* <tt
class="PARAMETER"><i>points</i></tt>, gint <tt class=
"PARAMETER"><i>npoints</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_segments</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, GdkSegment* <tt
class="PARAMETER"><i>segments</i></tt>, gint <tt
class="PARAMETER"><i>nsegments</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 18. Drawing Lines</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z135">Rectangles</a>
</h2>
<p>
Rectangles are drawn with <tt class="FUNCTION">
gdk_draw_rectangle()</tt> (<a href=
"z132.html#FL-GDKRECT">Figure 19</a>). The <span class=
"STRUCTNAME">filled</span> argument indicates whether to
fill the rectangle; <span class="STRUCTNAME">TRUE</span>
means to fill it.
</p>
<div class="FIGURE">
<a name="FL-GDKRECT"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKRECT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gdk_draw_rectangle</tt></code>(GdkDrawable* <tt
class="PARAMETER"><i>drawable</i></tt>, GdkGC* <tt
class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>filled</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>, gint <tt class=
"PARAMETER"><i>width</i></tt>, gint <tt class=
"PARAMETER"><i>height</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 19. Drawing Rectangles</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z136">Arcs</a>
</h2>
<p>
<tt class="FUNCTION">gdk_draw_arc()</tt> draws an ellipse
or a portion of one. (<a href=
"z132.html#FL-GDKARC">Figure 20</a>). The arc can be
filled or unfilled; the third argument to the function
toggles fill. The fourth through seventh arguments
describe a rectangle; the ellipse is inscribed in this
rectangle. <span class="STRUCTNAME">angle1</span> is the
angle at which to start drawing; it is relative to the 3
o'clock position (that is, 0 radians). <span class=
"STRUCTNAME">angle2</span> is the distance to travel
around the arc; if positive, travel is counterclockwise,
otherwise travel is clockwise. Both <span class=
"STRUCTNAME">angle1</span> and <span class="STRUCTNAME">
angle2</span> are specified in sixty-fourths of a degree;
so, 360 degrees is given as <span class="STRUCTNAME">
360*64</span>. This allows more precise specification of
the arc's size and shape, without using floating point
numbers. <span class="STRUCTNAME">angle2</span> should
not exceed 360 degrees, since it is nonsensical to move
more than 360 degrees around the ellipse.
</p>
<p>
To draw a circle, draw from 0 to 360*64 inside a square:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gdk_draw_arc(drawable, gc, TRUE,
0, 0,
50, 50,
0, 360*64);
</pre>
</td>
</tr>
</table>
<p>
To draw half an ellipse, change the aspect ratio and
halve the span of the arc:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gdk_draw_arc(drawable, gc, TRUE,
0, 0,
100, 50,
0, 180*64);
</pre>
</td>
</tr>
</table>
<p>
Many X servers draw the edges of filled arcs in an
aesthetically unpleasing way; in particular, very small
circles may not look very circular. You can work around
this by also drawing the circle's outline.
</p>
<div class="FIGURE">
<a name="FL-GDKARC"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKARC.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_arc</tt></code>(GdkDrawable* <tt
class="PARAMETER"><i>drawable</i></tt>, GdkGC* <tt
class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>filled</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>, gint <tt class=
"PARAMETER"><i>width</i></tt>, gint <tt class=
"PARAMETER"><i>height</i></tt>, gint <tt class=
"PARAMETER"><i>angle1</i></tt>, gint <tt class=
"PARAMETER"><i>angle2</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 20. Drawing Arcs</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z137">Polygons</a>
</h2>
<p>
<tt class="FUNCTION">gdk_draw_polygon()</tt> draws a
filled or unfilled polygon (<a href=
"z132.html#FL-GDKPOLY">Figure 21</a>). Notice that <tt
class="FUNCTION">gdk_draw_lines()</tt> can also be used
to draw an unfilled polygon (there is no reason to prefer
one or the other). The arguments to <tt class="FUNCTION">
gdk_draw_polygon()</tt> are the same as those to <tt
class="FUNCTION">gdk_draw_lines()</tt>. The polygon does
not have to be convex. It may also be self-intersecting.
Self-intersecting polygons are filled with an "Even-Odd
Rule," which means regions with an odd number of polygon
areas overlapping them are not filled. That is, if the
polygon does not overlap itself, it is entirely filled;
if a region is overlapped once, it is not filled; if it's
overlapped twice, it is filled; and so on.
</p>
<div class="FIGURE">
<a name="FL-GDKPOLY"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKPOLY.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_polygon</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>filled</i></tt>, GdkPoint* <tt class=
"PARAMETER"><i>points</i></tt>, gint <tt class=
"PARAMETER"><i>npoints</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 21. Drawing Polygons</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z138">Text</a>
</h2>
<p>
There are two functions to draw strings; as an
optimization, <tt class="FUNCTION">gdk_draw_text()</tt>
takes the length of the string to draw as an argument.
<tt class="FUNCTION">gdk_draw_string()</tt> uses <tt
class="FUNCTION">strlen()</tt> to compute the string
length for you. Otherwise the two are identical. The
<span class="STRUCTNAME">x</span> and <span class=
"STRUCTNAME">y</span> coordinates specify the location of
the left side of the text's baseline. See <a href=
"sec-gdkfont.html">the section called <i>Fonts</i></a>
for more information on fonts and font metrics. Text is
drawn in the foreground color.
</p>
<p>
There is no way to draw scaled or rotated text with GDK.
<span class="STRUCTNAME">GnomeCanvasText</span> offers a
slow and low-quality way to render scaled and rotated
text (see <a href=
"sec-itemreference.html#SEC-GNOMECANVASTEXT">the section
called <i>Text Item</i> in the chapter called <i><tt
class="CLASSNAME">GnomeCanvas</tt></i></a>). If you need
high-quality scaling and rotating, you will need to use
additional libraries, such as <tt class="APPLICATION">
t1lib</tt> for Type 1 fonts or FreeType for True Type
fonts. Another possibility is the Display Postscript
extension to X (XDPS); the GNU Project is working on a
free implementation of XDPS. The Gnome project also has a
text solution in development, as part of the <tt class=
"APPLICATION">gnome-print</tt> library.
</p>
<div class="FIGURE">
<a name="FL-GDKTEXT"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKTEXT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_string</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkFont*
<tt class="PARAMETER"><i>font</i></tt>, GdkGC* <tt
class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>, const gchar* <tt class=
"PARAMETER"><i>text</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_text</tt></code>(GdkDrawable* <tt
class="PARAMETER"><i>drawable</i></tt>, GdkFont* <tt
class="PARAMETER"><i>font</i></tt>, GdkGC* <tt class=
"PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>, const gchar* <tt class=
"PARAMETER"><i>text</i></tt>, gint <tt class=
"PARAMETER"><i>text_length</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 22. Drawing Text</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z139">Pixmaps</a>
</h2>
<p>
<tt class="FUNCTION">gdk_draw_pixmap()</tt> copies a
region from a pixmap to another drawable (pixmap or
window). The source and destination drawables must have
the same depth and visual. If you pass <span class=
"STRUCTNAME">-1</span> for the width or height, the full
size of the source pixmap is substituted. The source can
actually be any drawable, including a window, but <tt
class="FUNCTION">gdk_window_copy_area()</tt> will make
your code clearer if the source is a window. <a href=
"z132.html#FL-GDKDRAWPIXMAP">Figure 23</a> shows <tt
class="FUNCTION">gdk_draw_pixmap()</tt>.
</p>
<div class="FIGURE">
<a name="FL-GDKDRAWPIXMAP"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKDRAWPIXMAP.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdk.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_draw_pixmap</tt></code>(GdkDrawable*
<tt class="PARAMETER"><i>drawable</i></tt>, GdkGC*
<tt class="PARAMETER"><i>gc</i></tt>, GdkDrawable*
<tt class="PARAMETER"><i>src</i></tt>, gint <tt
class="PARAMETER"><i>xsrc</i></tt>, gint <tt class=
"PARAMETER"><i>ysrc</i></tt>, gint <tt class=
"PARAMETER"><i>xdest</i></tt>, gint <tt class=
"PARAMETER"><i>ydest</i></tt>, gint <tt class=
"PARAMETER"><i>width</i></tt>, gint <tt class=
"PARAMETER"><i>height</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 23. Drawing Pixmaps</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="SEC-GDKRGB">RGB Buffers</a>
</h2>
<p>
GDK's GdkRGB module allows you to copy a client-side
buffer of image data to a drawable. If you need to
manipulate images extensively, or copy image data to the
server, this is the correct way to do it. You can't
directly manipulate a <span class="STRUCTNAME">
GdkPixmap</span> because a pixmap is a server-side
object. Copying image data to the server with <tt class=
"FUNCTION">gdk_draw_point()</tt> would be unbelievably
slow, since each point would require a server request
(probably more than one, since you will need to change
the GC for each point).
</p>
<p>
Internally, GdkRGB uses an object called <span class=
"STRUCTNAME">GdkImage</span> to rapidly copy image data
to the server in a single request. This is still somewhat
slow---sizeable data does have to be copied---but GdkRGB
is highly tuned and uses shared memory if the client and
server happen to be on the same machine. So it's the
fastest way to perform this task, given the X
architecture. It will also handle some tricky issues for
you (such as adapting to the colormaps and visuals
available on a given X server).
</p>
<p>
The GdkRGB functions are in a separate header, <tt class=
"FILENAME">gdk/gdkrgb.h</tt>. Before using any GdkRGB
functions, you must initialize the module with <tt class=
"FUNCTION">gdk_rgb_init()</tt> (<a href=
"z132.html#FL-GDKRGB">Figure 24</a>); this sets up the
visual and colormap GdkRGB will use, and some internal
data structures.
</p>
<p>
The drawable you intend to copy the RGB buffer to must
use GdkRGB's visual and colormap. If the drawable is a
part of a widget, the easiest way to ensure this is to
push the GdkRGB visual and colormap when you create the
widget:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
GtkWidget* widget;
gtk_widget_push_visual(gdk_rgb_get_visual());
gtk_widget_push_colormap(gdk_rgb_get_cmap());
widget = gtk_whatever_new();
gtk_widget_pop_visual();
gtk_widget_pop_colormap();
</pre>
</td>
</tr>
</table>
<p>
The current version of GTK+ will be better-behaved if you
do this when creating the toplevel window containing the
drawable, instead of when creating the drawable itself.
However, in principle you can do it for only the
drawable.
</p>
<p>
GdkRGB understands several kinds of image data, including
24- and 32-bit RGB data, 8-bit grayscale, and 8-bit
indexes into an array of RGB values (a client-side <span
class="STRUCTNAME">GdkRgbCmap</span>). This section
describes only the simplest, 24-bit RGB data; this kind
of buffer is rendered with <tt class="FUNCTION">
gdk_draw_rgb_image()</tt>. There are separate functions
to render the other buffer types, but all of them work in
essentially the same way.
</p>
<p>
A 24-bit RGB buffer is a one-dimensional array of bytes;
every byte triplet makes up a pixel (byte 0 is red, byte
1 is green, byte 2 is blue). Three numbers describe the
size of the array and the location of bytes within it:
</p>
<ul>
<li>
<p>
The <i class="FIRSTTERM">width</i> is the number of
pixels (byte triplets) per row of the image.
</p>
</li>
<li>
<p>
The <i class="FIRSTTERM">height</i> is the number of
rows in the image.
</p>
</li>
<li>
<p>
The <i class="FIRSTTERM">rowstride</i> is the number
of bytes between rows. That is, for a buffer with
rowstride <i class="EMPHASIS">r</i>, if row <i class=
"EMPHASIS">n</i> starts at array index <i class=
"EMPHASIS">i</i> row <i class="EMPHASIS">n+1</i>
starts at array index <i class="EMPHASIS">i+r</i>.
The rowstride is not necessarily three times the
buffer's width; GdkRGB is faster if both the source
pointer and the rowstride are aligned to a 4-byte
boundary. Specifying a rowstride allows you to use
padding to achieve this.
</p>
</li>
</ul>
<p>
The <span class="STRUCTNAME">x</span>, <span class=
"STRUCTNAME">y</span>, <span class="STRUCTNAME">
width</span>, and <span class="STRUCTNAME">height</span>
arguments to <tt class="FUNCTION">
gdk_rgb_draw_image()</tt> define a region of the target
drawable to copy the RGB buffer to. The RGB buffer must
have at least <span class="STRUCTNAME">width</span>
columns and <span class="STRUCTNAME">height</span> rows.
Row 0, column 0 of the RGB buffer will be copied to point
(<span class="STRUCTNAME">x</span>, <span class=
"STRUCTNAME">y</span>) on the drawable.
</p>
<p>
Dithering simulates a larger number of colors on displays
with a limited palette. Dithering only matters on 8- and
16-bit displays; 24-bit displays do not have a limited
palette. The <span class="STRUCTNAME">dither</span>
argument is an enumerated type; it has three possible
values:
</p>
<ul>
<li>
<p>
<span class="STRUCTNAME">GDK_RGB_DITHER_NONE</span>
specifies that no dithering will be done. It's
appropriate for text or line drawings with few
colors, but inappropriate for photographic images.
</p>
</li>
<li>
<p>
<span class="STRUCTNAME">GDK_RGB_DITHER_NORMAL</span>
specifies dithering on 8-bit displays, but not 16-bit
displays. This is usually the best
quality/performance tradeoff.
</p>
</li>
<li>
<p>
<span class="STRUCTNAME">GDK_RGB_DITHER_MAX</span>
specifies that dithering will always be done on 8-
and 16-bit displays. The quality gain on 16-bit
displays is probably not worth the speed
penalty.
</p>
</li>
</ul>
<p>
The <span class="STRUCTNAME">gc</span> argument to <tt
class="FUNCTION">gdk_draw_rgb_image()</tt> is simply
passed through to <tt class="FUNCTION">
gdk_draw_image()</tt> (recall that GdkRGB uses <span
class="STRUCTNAME">GdkImage</span> internally). The <span
class="STRUCTNAME">gc</span> components that make sense
are used (such as the clip mask, drawing function, and
subwindow mode).
</p>
<div class="FIGURE">
<a name="FL-GDKRGB"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GDKRGB.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <gdk/gdkrgb.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gdk_rgb_init</tt></code>(void);</code>
</p>
<p>
<code><code class="FUNCDEF">GdkColormap* <tt class=
"FUNCTION">gdk_rgb_get_cmap</tt></code>(void);</code>
</p>
<p>
<code><code class="FUNCDEF">GdkVisual* <tt class=
"FUNCTION">
gdk_rgb_get_visual</tt></code>(void);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gdk_draw_rgb_image</tt></code>(GdkDrawable* <tt
class="PARAMETER"><i>drawable</i></tt>, GdkGC* <tt
class="PARAMETER"><i>gc</i></tt>, gint <tt class=
"PARAMETER"><i>x</i></tt>, gint <tt class=
"PARAMETER"><i>y</i></tt>, gint <tt class=
"PARAMETER"><i>width</i></tt>, gint <tt class=
"PARAMETER"><i>height</i></tt>, GdkRGBDither <tt
class="PARAMETER"><i>dither</i></tt>, guchar* <tt
class="PARAMETER"><i>rgb_buf</i></tt>, gint <tt
class="PARAMETER"><i>rowstride</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 24. GdkRGB</b>
</p>
</div>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="sec-gc.html"><font color="#0000ff" size="2">
<b><<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="sec-gdkresourcemgmt.html"><font color=
"#0000ff" size="2"><b>Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Graphics
Contexts</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>GDK Resource
Management</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|