1
/* $Id: handy.c,v 1.13 2004/09/22 18:43:08 mitch Exp $
1
/* $Id: handy.c 21431 2006-11-24 11:10:10Z neo $
2
2
* These routines are useful for working with the GIMP and need not be
3
3
* specific to plug-in-maze.
30
30
#include "libgimp/gimp.h"
32
35
/* get_colors Returns the current foreground and background colors in
33
36
nice little arrays. It works nicely for RGB and grayscale images,
34
37
however handling of indexed images is somewhat broken. Patches
38
get_colors (GimpDrawable *drawable,
41
get_colors (GimpDrawable *drawable,
42
45
GimpRGB foreground;
43
46
GimpRGB background;
53
56
case GIMP_RGB_IMAGE:
54
57
case GIMP_RGBA_IMAGE:
55
gimp_rgb_get_uchar (&foreground, &fg[0], &fg[1], &fg[2]);
56
gimp_rgb_get_uchar (&background, &bg[0], &bg[1], &bg[2]);
58
gimp_rgb_get_uchar (&foreground, &fg[0], &fg[1], &fg[2]);
59
gimp_rgb_get_uchar (&background, &bg[0], &bg[1], &bg[2]);
59
62
case GIMP_GRAYA_IMAGE:
60
63
case GIMP_GRAY_IMAGE:
61
fg[0] = gimp_rgb_intensity_uchar (&foreground);
62
bg[0] = gimp_rgb_intensity_uchar (&background);
64
fg[0] = gimp_rgb_luminance_uchar (&foreground);
65
bg[0] = gimp_rgb_luminance_uchar (&background);
65
68
case GIMP_INDEXEDA_IMAGE:
77
80
/* Draws a solid color box in a GimpPixelRgn. */
78
81
/* Optimization assumptions:
79
82
* (Or, "Why Maze is Faster Than Checkerboard.")
81
84
* Assuming calling memcpy is faster than using loops.
82
85
* Row buffers are nice...
84
* Assume allocating memory for row buffers takes a significant amount
87
* Assume allocating memory for row buffers takes a significant amount
85
88
* of time. Assume drawbox will be called many times.
86
89
* Only allocate memory once.
88
91
* Do not assume the row buffer will always be the same size. Allow
89
* for reallocating to make it bigger if needed. However, I don't see
92
* for reallocating to make it bigger if needed. However, I don't see
90
93
* reason to bother ever shrinking it again.
91
94
* (Under further investigation, assuming the row buffer never grows
92
95
* may be a safe assumption in this case.)
94
97
* Also assume that the program calling drawbox is short-lived, so
95
* memory leaks aren't of particular concern-- the memory allocated to
98
* memory leaks aren't of particular concern-- the memory allocated to
96
99
* the row buffer is never set free.
103
106
* We could keep a row of each color on hand so we wouldn't have to
104
107
* re-fill it every time... */
107
drawbox( GimpPixelRgn *dest_rgn,
108
guint x, guint y, guint w, guint h,
113
#include "libgimp/gimp.h"
119
drawbox( GimpPixelRgn *dest_rgn,
120
guint x, guint y, guint w, guint h,
111
123
const guint bpp = dest_rgn->bpp;
114
126
/* x_max = dest_rgn->bpp * MIN(dest_rgn->w, (x + w)); */
115
127
/* rowsize = x_max - x_min */
116
128
const guint rowsize = bpp * MIN(dest_rgn->w, (x + w)) - x_min;
118
130
/* The maximum [xy] value is that of the far end of the box, or
119
131
* the edge of the region, whichever comes first. */
120
132
const guint y_max = dest_rgn->rowstride * MIN(dest_rgn->h, (y + h));
122
134
static guint8 *rowbuf;
123
static guint high_size = 0;
135
static guint high_size = 0;
127
139
/* Does the row buffer need to be (re)allocated? */
130
142
rowbuf = g_new (guint8, rowsize);
132
else if (rowsize > high_size)
144
else if (rowsize > high_size)
134
146
rowbuf = g_renew (guint8, rowbuf, rowsize);
137
149
high_size = MAX(high_size, rowsize);
139
151
/* Fill the row buffer with the color. */
140
for (xx = 0; xx < rowsize; xx += bpp)
152
for (xx = 0; xx < rowsize; xx += bpp)
142
154
memcpy (&rowbuf[xx], clr, bpp);
145
157
/* Fill in the box in the region with rows... */
146
158
for (yy = dest_rgn->rowstride * y; yy < y_max; yy += dest_rgn->rowstride)