pixel-surround.c 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

Michael Natterer's avatar
Michael Natterer committed
23
#include "core/core-types.h"
24

25 26
#include "core/gimpimage.h"

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 141 142 143 144 145 146 147 148 149 150
#include "pixel_region.h"
#include "pixel_surround.h"
#include "tile_manager.h"
#include "tile.h"


void
pixel_surround_init (PixelSurround *ps,
		     TileManager   *tm,
		     gint           w,
		     gint           h,
		     guchar         bg[MAX_CHANNELS])
{
  gint i;

  for (i = 0; i < MAX_CHANNELS; ++i)
    {
      ps->bg[i] = bg[i];
    }

  ps->tile       = NULL;
  ps->mgr        = tm;
  ps->bpp        = tile_manager_bpp (tm);
  ps->w          = w;
  ps->h          = h;
  /* make sure buffer is big enough */
  ps->buff_size  = w * h * ps->bpp;
  ps->buff       = g_malloc (ps->buff_size);
  ps->row_stride = 0;
}

guchar *
pixel_surround_lock (PixelSurround *ps,
		     gint           x,
		     gint           y)
{
  gint    i, j;
  guchar *k;
  guchar *ptr;

  ps->tile = tile_manager_get_tile (ps->mgr, x, y, TRUE, FALSE);

  i = x % TILE_WIDTH;
  j = y % TILE_HEIGHT;

  /* do we have the whole region? */
  if (ps->tile &&
      (i < (tile_ewidth(ps->tile) - ps->w)) &&
      (j < (tile_eheight(ps->tile) - ps->h)))
    {
      ps->row_stride = tile_ewidth (ps->tile) * ps->bpp;
      /* is this really the correct way? */
      return tile_data_pointer (ps->tile, i, j);
    }

  /* nope, do this the hard way (for now) */
  if (ps->tile)
    {
      tile_release (ps->tile, FALSE);
      ps->tile = 0;
    }

  /* copy pixels, one by one */
  /* no, this is not the best way, but it's much better than before */
  ptr = ps->buff;
  for (j = y; j < y+ps->h; ++j)
    {
      for (i = x; i < x+ps->w; ++i)
	{
	  Tile *tile = tile_manager_get_tile (ps->mgr, i, j, TRUE, FALSE);

	  if (tile)
	    {
	      guchar *buff = tile_data_pointer (tile,
						i % TILE_WIDTH,
						j % TILE_HEIGHT);

	      for (k = buff; k < buff+ps->bpp; ++k, ++ptr)
		{
		  *ptr = *k;
		}
	      tile_release (tile, FALSE);
	    }
	  else
	    {
	      for (k = ps->bg; k < ps->bg+ps->bpp; ++k, ++ptr)
		{
		  *ptr = *k;
		}
	    }
	}
    }
  ps->row_stride = ps->w * ps->bpp;

  return ps->buff;
}

gint
pixel_surround_rowstride (PixelSurround *ps)
{
  return ps->row_stride;
}

void
pixel_surround_release (PixelSurround *ps)
{
  /* always get new tile (for now), so release the old one */
  if (ps->tile)
    {
      tile_release (ps->tile, FALSE);
      ps->tile = 0;
    }
}

void
pixel_surround_clear (PixelSurround *ps)
{
  if (ps->buff)
    {
      g_free (ps->buff);
      ps->buff = 0;
      ps->buff_size = 0;
    }
}