~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/maze/handy.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
 *
5
 
 * Kevin Turner <acapnotic@users.sourceforge.net>
6
 
 * http://gimp-plug-ins.sourceforge.net/maze/
7
 
 */
8
 
 
9
 
/*
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.
14
 
 *
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.
19
 
 *
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.
23
 
 *
24
 
 */
25
 
 
26
 
#include "config.h"
27
 
 
28
 
#include <string.h>
29
 
 
30
 
#include "libgimp/gimp.h"
31
 
 
32
 
#include "maze.h"
33
 
 
34
 
 
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
38
 
   appreciated. */
39
 
 
40
 
void
41
 
get_colors (GimpDrawable *drawable,
42
 
            guint8       *fg,
43
 
            guint8       *bg)
44
 
{
45
 
  GimpRGB foreground;
46
 
  GimpRGB background;
47
 
 
48
 
  gimp_context_get_foreground (&foreground);
49
 
  gimp_context_get_background (&background);
50
 
 
51
 
  fg[0] = fg[1] = fg[2] = fg[3] = 255;
52
 
  bg[0] = bg[1] = bg[2] = bg[3] = 255;
53
 
 
54
 
  switch ( gimp_drawable_type (drawable->drawable_id) )
55
 
    {
56
 
    case GIMP_RGB_IMAGE:
57
 
    case GIMP_RGBA_IMAGE:
58
 
      gimp_rgb_get_uchar (&foreground, &fg[0], &fg[1], &fg[2]);
59
 
      gimp_rgb_get_uchar (&background, &bg[0], &bg[1], &bg[2]);
60
 
      break;
61
 
 
62
 
    case GIMP_GRAYA_IMAGE:
63
 
    case GIMP_GRAY_IMAGE:
64
 
      fg[0] = gimp_rgb_luminance_uchar (&foreground);
65
 
      bg[0] = gimp_rgb_luminance_uchar (&background);
66
 
      break;
67
 
 
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.   */
73
 
        break;
74
 
 
75
 
    default:
76
 
      break;
77
 
    }
78
 
}
79
 
 
80
 
/* Draws a solid color box in a GimpPixelRgn. */
81
 
/* Optimization assumptions:
82
 
 * (Or, "Why Maze is Faster Than Checkerboard.")
83
 
 *
84
 
 * Assuming calling memcpy is faster than using loops.
85
 
 * Row buffers are nice...
86
 
 *
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.
90
 
 *
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.)
96
 
 *
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.
100
 
 */
101
 
 
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...  */
108
 
 
109
 
#include "config.h"
110
 
 
111
 
#include <stdlib.h>
112
 
 
113
 
#include "libgimp/gimp.h"
114
 
 
115
 
#include "maze.h"
116
 
 
117
 
 
118
 
void
119
 
drawbox( GimpPixelRgn *dest_rgn,
120
 
         guint x, guint y, guint w, guint h,
121
 
         guint8 clr[4])
122
 
{
123
 
     const guint bpp = dest_rgn->bpp;
124
 
     const guint x_min = x * bpp;
125
 
 
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;
129
 
 
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));
133
 
 
134
 
     static guint8 *rowbuf;
135
 
     static guint high_size = 0;
136
 
 
137
 
     guint xx, yy;
138
 
 
139
 
     /* Does the row buffer need to be (re)allocated? */
140
 
     if (high_size == 0)
141
 
       {
142
 
         rowbuf = g_new (guint8, rowsize);
143
 
       }
144
 
     else if (rowsize > high_size)
145
 
       {
146
 
         rowbuf = g_renew (guint8, rowbuf, rowsize);
147
 
       }
148
 
 
149
 
     high_size = MAX(high_size, rowsize);
150
 
 
151
 
     /* Fill the row buffer with the color. */
152
 
     for (xx = 0; xx < rowsize; xx += bpp)
153
 
       {
154
 
         memcpy (&rowbuf[xx], clr, bpp);
155
 
       }
156
 
 
157
 
     /* Fill in the box in the region with rows... */
158
 
     for (yy = dest_rgn->rowstride * y; yy < y_max; yy += dest_rgn->rowstride)
159
 
       {
160
 
         memcpy (&dest_rgn->data[yy + x_min], rowbuf, rowsize);
161
 
       }
162
 
}