1
/* $Id: handy.c 22715 2007-06-06 08:44:52Z muks $
2
* These routines are useful for working with GIMP and need not be
3
* specific to plug-in-maze.
5
* Kevin Turner <acapnotic@users.sourceforge.net>
6
* http://gimp-plug-ins.sourceforge.net/maze/
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30
#include "libgimp/gimp.h"
35
/* get_colors Returns the current foreground and background colors in
36
nice little arrays. It works nicely for RGB and grayscale images,
37
however handling of indexed images is somewhat broken. Patches
41
get_colors (GimpDrawable *drawable,
48
gimp_context_get_foreground (&foreground);
49
gimp_context_get_background (&background);
51
fg[0] = fg[1] = fg[2] = fg[3] = 255;
52
bg[0] = bg[1] = bg[2] = bg[3] = 255;
54
switch ( gimp_drawable_type (drawable->drawable_id) )
58
gimp_rgb_get_uchar (&foreground, &fg[0], &fg[1], &fg[2]);
59
gimp_rgb_get_uchar (&background, &bg[0], &bg[1], &bg[2]);
62
case GIMP_GRAYA_IMAGE:
64
fg[0] = gimp_rgb_luminance_uchar (&foreground);
65
bg[0] = gimp_rgb_luminance_uchar (&background);
68
case GIMP_INDEXEDA_IMAGE:
69
case GIMP_INDEXED_IMAGE: /* FIXME: Should use current fg/bg colors. */
70
g_warning("maze: get_colors: Indexed image. Using colors 15 and 0.\n");
71
fg[0] = 15; /* As a plugin, I protest. *I* shouldn't be the */
72
bg[0] = 0; /* one who has to deal with this colormapcrap. */
80
/* Draws a solid color box in a GimpPixelRgn. */
81
/* Optimization assumptions:
82
* (Or, "Why Maze is Faster Than Checkerboard.")
84
* Assuming calling memcpy is faster than using loops.
85
* Row buffers are nice...
87
* Assume allocating memory for row buffers takes a significant amount
88
* of time. Assume drawbox will be called many times.
89
* Only allocate memory once.
91
* Do not assume the row buffer will always be the same size. Allow
92
* for reallocating to make it bigger if needed. However, I don't see
93
* reason to bother ever shrinking it again.
94
* (Under further investigation, assuming the row buffer never grows
95
* may be a safe assumption in this case.)
97
* Also assume that the program calling drawbox is short-lived, so
98
* memory leaks aren't of particular concern-- the memory allocated to
99
* the row buffer is never set free.
102
/* Further optimizations that could be made...
103
* Currently, the row buffer is re-filled with every call. However,
104
* plug-ins such as maze and checkerboard only use two colors, and
105
* for the most part, have rows of the same size with every call.
106
* We could keep a row of each color on hand so we wouldn't have to
107
* re-fill it every time... */
113
#include "libgimp/gimp.h"
119
drawbox( GimpPixelRgn *dest_rgn,
120
guint x, guint y, guint w, guint h,
123
const guint bpp = dest_rgn->bpp;
124
const guint x_min = x * bpp;
126
/* x_max = dest_rgn->bpp * MIN(dest_rgn->w, (x + w)); */
127
/* rowsize = x_max - x_min */
128
const guint rowsize = bpp * MIN(dest_rgn->w, (x + w)) - x_min;
130
/* The maximum [xy] value is that of the far end of the box, or
131
* the edge of the region, whichever comes first. */
132
const guint y_max = dest_rgn->rowstride * MIN(dest_rgn->h, (y + h));
134
static guint8 *rowbuf;
135
static guint high_size = 0;
139
/* Does the row buffer need to be (re)allocated? */
142
rowbuf = g_new (guint8, rowsize);
144
else if (rowsize > high_size)
146
rowbuf = g_renew (guint8, rowbuf, rowsize);
149
high_size = MAX(high_size, rowsize);
151
/* Fill the row buffer with the color. */
152
for (xx = 0; xx < rowsize; xx += bpp)
154
memcpy (&rowbuf[xx], clr, bpp);
157
/* Fill in the box in the region with rows... */
158
for (yy = dest_rgn->rowstride * y; yy < y_max; yy += dest_rgn->rowstride)
160
memcpy (&dest_rgn->data[yy + x_min], rowbuf, rowsize);