pixel-surround.c 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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"

Michael Natterer's avatar
Michael Natterer committed
21
#include <glib.h>
22

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

Michael Natterer's avatar
Michael Natterer committed
25 26 27
#include "pixel-region.h"
#include "pixel-surround.h"
#include "tile-manager.h"
28 29 30 31 32 33
#include "tile.h"


void
pixel_surround_init (PixelSurround *ps,
		     TileManager   *tm,
34 35
		     guint          w,
		     guint          h,
36 37
		     guchar         bg[MAX_CHANNELS])
{
38
  guint i;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

  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,
58 59
		     guint          x,
		     guint          y)
60
{
61
  guint   i, j;
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
  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;
}

122
guint
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
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;
    }
}