handy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* $Id$
  2. * These routines are useful for working with the 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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include "config.h"
  25. #include <string.h>
  26. #include "libgimp/gimp.h"
  27. /* get_colors Returns the current foreground and background colors in
  28. nice little arrays. It works nicely for RGB and grayscale images,
  29. however handling of indexed images is somewhat broken. Patches
  30. appreciated. */
  31. void
  32. get_colors (GimpDrawable *drawable,
  33. guint8 *fg,
  34. guint8 *bg)
  35. {
  36. GimpRGB foreground;
  37. GimpRGB background;
  38. gimp_palette_get_foreground (&foreground);
  39. gimp_palette_get_background (&background);
  40. fg[0] = fg[1] = fg[2] = fg[3] = 255;
  41. bg[0] = bg[1] = bg[2] = bg[3] = 255;
  42. switch ( gimp_drawable_type (drawable->drawable_id) )
  43. {
  44. case GIMP_RGB_IMAGE:
  45. case GIMP_RGBA_IMAGE:
  46. gimp_rgb_get_uchar (&foreground, &fg[0], &fg[1], &fg[2]);
  47. gimp_rgb_get_uchar (&background, &bg[0], &bg[1], &bg[2]);
  48. break;
  49. case GIMP_GRAYA_IMAGE:
  50. case GIMP_GRAY_IMAGE:
  51. fg[0] = gimp_rgb_intensity_uchar (&foreground);
  52. bg[0] = gimp_rgb_intensity_uchar (&background);
  53. break;
  54. case GIMP_INDEXEDA_IMAGE:
  55. case GIMP_INDEXED_IMAGE: /* FIXME: Should use current fg/bg colors. */
  56. g_warning("maze: get_colors: Indexed image. Using colors 15 and 0.\n");
  57. fg[0] = 15; /* As a plugin, I protest. *I* shouldn't be the */
  58. bg[0] = 0; /* one who has to deal with this colormapcrap. */
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. /* Draws a solid color box in a GimpPixelRgn. */
  65. /* Optimization assumptions:
  66. * (Or, "Why Maze is Faster Than Checkerboard.")
  67. *
  68. * Assuming calling memcpy is faster than using loops.
  69. * Row buffers are nice...
  70. *
  71. * Assume allocating memory for row buffers takes a significant amount
  72. * of time. Assume drawbox will be called many times.
  73. * Only allocate memory once.
  74. *
  75. * Do not assume the row buffer will always be the same size. Allow
  76. * for reallocating to make it bigger if needed. However, I don't see
  77. * reason to bother ever shrinking it again.
  78. * (Under further investigation, assuming the row buffer never grows
  79. * may be a safe assumption in this case.)
  80. *
  81. * Also assume that the program calling drawbox is short-lived, so
  82. * memory leaks aren't of particular concern-- the memory allocated to
  83. * the row buffer is never set free.
  84. */
  85. /* Further optimizations that could be made...
  86. * Currently, the row buffer is re-filled with every call. However,
  87. * plug-ins such as maze and checkerboard only use two colors, and
  88. * for the most part, have rows of the same size with every call.
  89. * We could keep a row of each color on hand so we wouldn't have to
  90. * re-fill it every time... */
  91. void
  92. drawbox( GimpPixelRgn *dest_rgn,
  93. guint x, guint y, guint w, guint h,
  94. guint8 clr[4])
  95. {
  96. const guint bpp = dest_rgn->bpp;
  97. const guint x_min = x * bpp;
  98. /* x_max = dest_rgn->bpp * MIN(dest_rgn->w, (x + w)); */
  99. /* rowsize = x_max - x_min */
  100. const guint rowsize = bpp * MIN(dest_rgn->w, (x + w)) - x_min;
  101. /* The maximum [xy] value is that of the far end of the box, or
  102. * the edge of the region, whichever comes first. */
  103. const guint y_max = dest_rgn->rowstride * MIN(dest_rgn->h, (y + h));
  104. static guint8 *rowbuf;
  105. static guint high_size = 0;
  106. guint xx, yy;
  107. /* Does the row buffer need to be (re)allocated? */
  108. if (high_size == 0)
  109. {
  110. rowbuf = g_new (guint8, rowsize);
  111. }
  112. else if (rowsize > high_size)
  113. {
  114. rowbuf = g_renew (guint8, rowbuf, rowsize);
  115. }
  116. high_size = MAX(high_size, rowsize);
  117. /* Fill the row buffer with the color. */
  118. for (xx = 0; xx < rowsize; xx += bpp)
  119. {
  120. memcpy (&rowbuf[xx], clr, bpp);
  121. }
  122. /* Fill in the box in the region with rows... */
  123. for (yy = dest_rgn->rowstride * y; yy < y_max; yy += dest_rgn->rowstride)
  124. {
  125. memcpy (&dest_rgn->data[yy + x_min], rowbuf, rowsize);
  126. }
  127. }