image_map.c 9.79 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17 18 19 20 21 22 23 24 25 26 27 28 29
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "appenv.h"
#include "drawable.h"
#include "errors.h"
#include "gdisplay.h"
#include "gimage.h"
#include "gimage_mask.h"
#include "image_map.h"
#include "tile_manager.h"

30 31
#include "tile_manager_pvt.h"

32 33
#include "libgimp/gimpintl.h"

Elliot Lee's avatar
Elliot Lee committed
34 35 36 37 38 39 40 41 42
#define WAITING 0
#define WORKING 1

#define WORK_DELAY 1

/*  Local structures  */
typedef struct _ImageMap
{
  GDisplay *        gdisp;
43
  GimpDrawable *    drawable;
Elliot Lee's avatar
Elliot Lee committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  TileManager *     undo_tiles;
  ImageMapApplyFunc apply_func;
  void *            user_data;
  PixelRegion       srcPR, destPR;
  void *            pr;
  int               state;
  gint              idle;
} _ImageMap;


/**************************/
/*  Function definitions  */

static gint
image_map_do (gpointer data)
{
  _ImageMap *_image_map;
  GImage *gimage;
  PixelRegion shadowPR;
  int x, y, w, h;

  _image_map = (_ImageMap *) data;

67
  if (! (gimage = drawable_gimage ( (_image_map->drawable))))
Elliot Lee's avatar
Elliot Lee committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    {
      _image_map->state = WAITING;
      return FALSE;
    }

  /*  Process the pixel regions and apply the image mapping  */
  (* _image_map->apply_func) (&_image_map->srcPR, &_image_map->destPR, _image_map->user_data);

  x = _image_map->destPR.x;
  y = _image_map->destPR.y;
  w = _image_map->destPR.w;
  h = _image_map->destPR.h;

  /*  apply the results  */
  pixel_region_init (&shadowPR, gimage->shadow, x, y, w, h, FALSE);
83
  gimage_apply_image (gimage, _image_map->drawable, &shadowPR,
Manish Singh's avatar
Manish Singh committed
84
		      FALSE, OPAQUE_OPACITY, REPLACE_MODE, NULL, x, y);
Elliot Lee's avatar
Elliot Lee committed
85 86 87 88

  /*  display the results  */
  if (_image_map->gdisp)
    {
89
      drawable_update ( (_image_map->drawable), x, y, w, h);
90
      gdisplay_flush_now (_image_map->gdisp);
Elliot Lee's avatar
Elliot Lee committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    }

  _image_map->pr = pixel_regions_process (_image_map->pr);

  if (_image_map->pr == NULL)
    {
      _image_map->state = WAITING;
      gdisplays_flush ();
      return FALSE;
    }
  else
    return TRUE;
}

ImageMap
image_map_create (void *gdisp_ptr,
107
		  GimpDrawable *drawable)
Elliot Lee's avatar
Elliot Lee committed
108 109 110 111 112
{
  _ImageMap *_image_map;

  _image_map = (_ImageMap *) g_malloc (sizeof (_ImageMap));
  _image_map->gdisp = (GDisplay *) gdisp_ptr;
113
  _image_map->drawable = drawable;
Elliot Lee's avatar
Elliot Lee committed
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
  _image_map->undo_tiles = NULL;
  _image_map->state = WAITING;

  return (ImageMap) _image_map;
}

void
image_map_apply (ImageMap           image_map,
		 ImageMapApplyFunc  apply_func,
		 void              *user_data)
{
  _ImageMap *_image_map;
  int x1, y1, x2, y2;

  _image_map = (_ImageMap *) image_map;
  _image_map->apply_func = apply_func;
  _image_map->user_data = user_data;

  /*  If we're still working, remove the timer  */
  if (_image_map->state == WORKING)
    {
      gtk_idle_remove (_image_map->idle);
      pixel_regions_process_stop (_image_map->pr);
      _image_map->pr = NULL;
    }

  /*  Make sure the drawable is still valid  */
141
  if (! drawable_gimage ( (_image_map->drawable)))
Elliot Lee's avatar
Elliot Lee committed
142 143 144
    return;

  /*  The application should occur only within selection bounds  */
145
  drawable_mask_bounds ( (_image_map->drawable), &x1, &y1, &x2, &y2);
Elliot Lee's avatar
Elliot Lee committed
146 147 148 149 150

  /*  If undo tiles don't exist, or change size, (re)allocate  */
  if (!_image_map->undo_tiles ||
      _image_map->undo_tiles->x != x1 ||
      _image_map->undo_tiles->y != y1 ||
151 152
      _image_map->undo_tiles->width != (x2 - x1) ||
      _image_map->undo_tiles->height != (y2 - y1))
Elliot Lee's avatar
Elliot Lee committed
153 154 155 156 157 158
    {
      /*  If undo tiles exist, copy them to the drawable*/
      if (_image_map->undo_tiles)
	{
	  /*  Copy from the drawable to the tiles  */
	  pixel_region_init (&_image_map->srcPR, _image_map->undo_tiles, 0, 0,
159 160
			     _image_map->undo_tiles->width,
			     _image_map->undo_tiles->height,
Elliot Lee's avatar
Elliot Lee committed
161
			     FALSE);
162
	  pixel_region_init (&_image_map->destPR, drawable_data ( (_image_map->drawable)),
Elliot Lee's avatar
Elliot Lee committed
163
			     _image_map->undo_tiles->x, _image_map->undo_tiles->y,
164 165
			     _image_map->undo_tiles->width,
			     _image_map->undo_tiles->height,
Elliot Lee's avatar
Elliot Lee committed
166 167 168 169 170 171 172
			     TRUE);

	  copy_region (&_image_map->srcPR, &_image_map->destPR);
	}

      /*  If either the extents changed or the tiles don't exist, allocate new  */
      if (!_image_map->undo_tiles ||
173 174
	  _image_map->undo_tiles->width != (x2 - x1) ||
	  _image_map->undo_tiles->height != (y2 - y1))
Elliot Lee's avatar
Elliot Lee committed
175 176 177 178 179 180 181
	{
	  /*  Destroy old tiles--If they exist  */
	  if (_image_map->undo_tiles != NULL)
	    tile_manager_destroy (_image_map->undo_tiles);

	  /*  Allocate new tiles  */
	  _image_map->undo_tiles = tile_manager_new ((x2 - x1), (y2 - y1),
182
						     drawable_bytes ( (_image_map->drawable)));
Elliot Lee's avatar
Elliot Lee committed
183 184 185
	}

      /*  Copy from the image to the new tiles  */
186
      pixel_region_init (&_image_map->srcPR, drawable_data ( (_image_map->drawable)),
Elliot Lee's avatar
Elliot Lee committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
			 x1, y1, (x2 - x1), (y2 - y1), FALSE);
      pixel_region_init (&_image_map->destPR, _image_map->undo_tiles,
			 0, 0, (x2 - x1), (y2 - y1), TRUE);

      copy_region (&_image_map->srcPR, &_image_map->destPR);

      /*  Set the offsets  */
      _image_map->undo_tiles->x = x1;
      _image_map->undo_tiles->y = y1;
    }

  /*  Configure the src from the drawable data  */
  pixel_region_init (&_image_map->srcPR, _image_map->undo_tiles,
		     0, 0, (x2 - x1), (y2 - y1), FALSE);

  /*  Configure the dest as the shadow buffer  */
203
  pixel_region_init (&_image_map->destPR, drawable_shadow ( (_image_map->drawable)),
Elliot Lee's avatar
Elliot Lee committed
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
		     x1, y1, (x2 - x1), (y2 - y1), TRUE);

  /*  Apply the image transformation to the pixels  */
  _image_map->pr = pixel_regions_register (2, &_image_map->srcPR, &_image_map->destPR);

  /*  Start the intermittant work procedure  */
  _image_map->state = WORKING;
  _image_map->idle = gtk_idle_add (image_map_do, image_map);
}

void
image_map_commit (ImageMap image_map)
{
  _ImageMap *_image_map;
  int x1, y1, x2, y2;

  _image_map = (_ImageMap *) image_map;

  if (_image_map->state == WORKING)
    {
      gtk_idle_remove (_image_map->idle);

      /*  Finish the changes  */
      while (image_map_do (image_map)) ;
    }

  /*  Make sure the drawable is still valid  */
231
  if (! drawable_gimage ( (_image_map->drawable)))
Elliot Lee's avatar
Elliot Lee committed
232 233 234 235 236 237 238
    return;

  /*  Register an undo step  */
  if (_image_map->undo_tiles)
    {
      x1 = _image_map->undo_tiles->x;
      y1 = _image_map->undo_tiles->y;
239 240
      x2 = _image_map->undo_tiles->x + _image_map->undo_tiles->width;
      y2 = _image_map->undo_tiles->y + _image_map->undo_tiles->height;
241
      drawable_apply_image ( (_image_map->drawable), x1, y1, x2, y2, _image_map->undo_tiles, FALSE);
Elliot Lee's avatar
Elliot Lee committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    }

  g_free (_image_map);
}

void
image_map_abort (ImageMap image_map)
{
  _ImageMap *_image_map;
  PixelRegion srcPR, destPR;

  _image_map = (_ImageMap *) image_map;

  if (_image_map->state == WORKING)
    {
      gtk_idle_remove (_image_map->idle);
      pixel_regions_process_stop (_image_map->pr);
      _image_map->pr = NULL;
    }

  /*  Make sure the drawable is still valid  */
263
  if (! drawable_gimage ( (_image_map->drawable)))
Elliot Lee's avatar
Elliot Lee committed
264 265 266 267 268 269 270
    return;

  /*  restore the original image  */
  if (_image_map->undo_tiles)
    {
      /*  Copy from the drawable to the tiles  */
      pixel_region_init (&srcPR, _image_map->undo_tiles, 0, 0,
271 272
			 _image_map->undo_tiles->width,
			 _image_map->undo_tiles->height,
Elliot Lee's avatar
Elliot Lee committed
273
			 FALSE);
274
      pixel_region_init (&destPR, drawable_data ( (_image_map->drawable)),
Elliot Lee's avatar
Elliot Lee committed
275
			 _image_map->undo_tiles->x, _image_map->undo_tiles->y,
276 277
			 _image_map->undo_tiles->width,
			 _image_map->undo_tiles->height,
Elliot Lee's avatar
Elliot Lee committed
278 279 280 281 282
			 TRUE);

      /* if the user has changed the image depth get out quickly */
      if (destPR.bytes != srcPR.bytes) 
	{
Sven Neumann's avatar
Sven Neumann committed
283
	  g_message ("image depth change, unable to restore original image");
Elliot Lee's avatar
Elliot Lee committed
284 285 286 287 288 289 290 291
	  tile_manager_destroy (_image_map->undo_tiles);
	  g_free (_image_map);
	  return;
	}
      
      copy_region (&srcPR, &destPR);

      /*  Update the area  */
292
      drawable_update ( (_image_map->drawable),
Elliot Lee's avatar
Elliot Lee committed
293
		       _image_map->undo_tiles->x, _image_map->undo_tiles->y,
294 295
		       _image_map->undo_tiles->width,
		       _image_map->undo_tiles->height);
Elliot Lee's avatar
Elliot Lee committed
296 297 298 299 300 301 302

      /*  Free the undo_tiles tile manager  */
      tile_manager_destroy (_image_map->undo_tiles);
    }

  g_free (_image_map);
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

unsigned char *
image_map_get_color_at (ImageMap image_map, int x, int y)
{
  Tile *tile;
  unsigned char *src;
  unsigned char *dest;
  _ImageMap *_image_map;

  if(!image_map)
    return NULL;

  _image_map = (_ImageMap *) image_map;

  /* Check if done damage to original image */
  if(!_image_map->undo_tiles)
    return (gimp_drawable_get_color_at(_image_map->drawable,x,y));

  if (!image_map ||
      (!gimp_drawable_gimage(_image_map->drawable) && 
Marc Lehmann's avatar
Marc Lehmann committed
323
       gimp_drawable_is_indexed(_image_map->drawable)) ||
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
324 325 326
      x < 0 || y < 0 ||
      x >= _image_map->undo_tiles->width ||
      y >= _image_map->undo_tiles->height)
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  {
    return NULL;
  }

  dest = g_new(unsigned char, 5);

  tile = tile_manager_get_tile (_image_map->undo_tiles, x, y,
				TRUE, FALSE);

  src = tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT);

  gimp_image_get_color (gimp_drawable_gimage(_image_map->drawable),
			gimp_drawable_type (_image_map->drawable), dest, src);

  if(TYPE_HAS_ALPHA(gimp_drawable_type (_image_map->drawable)))
    dest[3] = src[gimp_drawable_bytes (_image_map->drawable) - 1];
  else
    dest[3] = 255;
Marc Lehmann's avatar
Marc Lehmann committed
345
  if (gimp_drawable_is_indexed(_image_map->drawable))
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
346 347 348 349 350 351 352
    dest[4] = src[0];
  else
    dest[4] = 0;
  tile_release (tile, FALSE);
  return dest;
}