gimplut.c 5.12 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 Spencer Kimball and Peter Mattis
 *
 * gimplut.c: Copyright (C) 1999 Jay Cox <jaycox@earthlink.net>
 *
 * 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.
 */

Sven Neumann's avatar
Sven Neumann committed
21 22 23 24
#include "config.h"

#include <stdio.h>

Michael Natterer's avatar
Michael Natterer committed
25
#include <glib.h>
Sven Neumann's avatar
Sven Neumann committed
26

Michael Natterer's avatar
Michael Natterer committed
27
#include "base-types.h"
Sven Neumann's avatar
Sven Neumann committed
28

29
#include "gimplut.h"
Michael Natterer's avatar
Michael Natterer committed
30
#include "pixel-region.h"
Sven Neumann's avatar
Sven Neumann committed
31

32 33

GimpLut *
34
gimp_lut_new (void)
35
{
Michael Natterer's avatar
Michael Natterer committed
36
  GimpLut *lut;
37

Michael Natterer's avatar
Michael Natterer committed
38
  lut = g_new (GimpLut, 1);
39

Michael Natterer's avatar
Michael Natterer committed
40 41
  lut->luts      = NULL;
  lut->nchannels = 0;
42

Michael Natterer's avatar
Michael Natterer committed
43
  return lut;
44 45 46
}

void
47
gimp_lut_free (GimpLut *lut)
48
{
49 50
  gint i;

51
  for (i = 0; i < lut->nchannels; i++)
52
    g_free (lut->luts[i]);
Michael Natterer's avatar
Michael Natterer committed
53

54
  g_free (lut->luts);
55 56 57
}

void
58 59 60
gimp_lut_setup (GimpLut     *lut, 
		GimpLutFunc  func,
		void        *user_data, 
61
		guint        nchannels)
62
{
63
  guint   i, v;
64 65
  gdouble val;

66
  if (lut->luts)
67 68 69
    {
      for (i = 0; i < lut->nchannels; i++)
	g_free (lut->luts[i]);
Michael Natterer's avatar
Michael Natterer committed
70

71 72
      g_free (lut->luts);
    }
Michael Natterer's avatar
Michael Natterer committed
73

74
  lut->nchannels = nchannels;
Michael Natterer's avatar
Michael Natterer committed
75
  lut->luts      = g_new (guchar *, lut->nchannels);
76

77
  for (i = 0; i < lut->nchannels; i++)
78 79
    {
      lut->luts[i] = g_new (guchar, 256);
Michael Natterer's avatar
Michael Natterer committed
80

81
      for (v = 0; v < 256; v++)
Michael Natterer's avatar
Michael Natterer committed
82 83
	{
	  /* to add gamma correction use func(v ^ g) ^ 1/g instead. */
84
	  val = 255.0 * func (user_data, lut->nchannels, i, v/255.0) + 0.5;
Michael Natterer's avatar
Michael Natterer committed
85

86
	  lut->luts[i][v] = CLAMP (val, 0, 255);
87
	}
88 89 90 91
    }
}

void
92 93 94
gimp_lut_setup_exact (GimpLut     *lut, 
		      GimpLutFunc  func,
		      void        *user_data, 
95
		      guint        nchannels)
96
{
97
  gimp_lut_setup (lut, func, user_data, nchannels);
98 99 100 101 102 103 104
}

void
gimp_lut_process (GimpLut     *lut,
		  PixelRegion *srcPR,
		  PixelRegion *destPR)
{
105
  guint   h, width, src_r_i, dest_r_i;
106 107
  guchar *src, *dest;
  guchar *lut0 = NULL, *lut1 = NULL, *lut2 = NULL, *lut3 = NULL;
108 109 110 111 112 113 114 115 116 117

  if (lut->nchannels > 0)
    lut0 = lut->luts[0];
  if (lut->nchannels > 1)
    lut1 = lut->luts[1];
  if (lut->nchannels > 2)
    lut2 = lut->luts[2];
  if (lut->nchannels > 3)
    lut3 = lut->luts[3];

Michael Natterer's avatar
Michael Natterer committed
118 119 120 121 122
  h        = srcPR->h;
  src      = srcPR->data;
  dest     = destPR->data;
  width    = srcPR->w;
  src_r_i  =  srcPR->rowstride  - (srcPR->bytes  * srcPR->w);
123 124 125
  dest_r_i = destPR->rowstride - (destPR->bytes * srcPR->w);

  if (src_r_i == 0 && dest_r_i == 0)
126 127 128 129 130
    {
      width *= h;
      h = 1;
    }

131 132
  while (h--)
    {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
      switch (lut->nchannels)
	{
	case 1:
	  while (width--)
	    {
	      *dest = lut0[*src];
	      src++;
	      dest++;
	    }
	  break;
	case 2:
	  while (width--)
	    {
	      dest[0] = lut0[src[0]];
	      dest[1] = lut1[src[1]];
	      src  += 2;
	      dest += 2;
	    }
	  break;
	case 3:
	  while (width--)
	    {
	      dest[0] = lut0[src[0]];
	      dest[1] = lut1[src[1]];
	      dest[2] = lut2[src[2]];
	      src  += 3;
	      dest += 3;
	    }
	  break;
	case 4:
	  while (width--)
	    {
	      dest[0] = lut0[src[0]];
	      dest[1] = lut1[src[1]];
	      dest[2] = lut2[src[2]];
	      dest[3] = lut3[src[3]];
	      src  += 4;
	      dest += 4;
	    }
	  break;
	default:
	  g_warning ("gimplut: Error: nchannels = %d\n", lut->nchannels);
	}

      width = srcPR->w;
      src  += src_r_i;
      dest += dest_r_i;
180 181 182
    }
}

183 184 185 186
void
gimp_lut_process_inline (GimpLut     *lut,
			 PixelRegion *srcPR)
{
187
  guint   h, width, src_r_i;
188 189
  guchar *src;
  guchar *lut0 = NULL, *lut1 = NULL, *lut2 = NULL, *lut3 = NULL;
190 191 192 193 194 195 196 197 198 199

  if (lut->nchannels > 0)
    lut0 = lut->luts[0];
  if (lut->nchannels > 1)
    lut1 = lut->luts[1];
  if (lut->nchannels > 2)
    lut2 = lut->luts[2];
  if (lut->nchannels > 3)
    lut3 = lut->luts[3];

Michael Natterer's avatar
Michael Natterer committed
200 201 202
  h       = srcPR->h;
  src     = srcPR->data;
  width   = srcPR->w;
203 204 205
  src_r_i =  srcPR->rowstride  - (srcPR->bytes  * srcPR->w);

  if (src_r_i == 0)
206 207 208 209 210
    {
      width *= h;
      h = 1;
    }

211 212
  while (h--)
    {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      switch (lut->nchannels)
	{
	case 1:
	  while (width--)
	    {
	      *src = lut0[*src];
	      src++;
	    }
	  break;
	case 2:
	  while (width--)
	    {
	      src[0] = lut0[src[0]];
	      src[1] = lut1[src[1]];
	      src  += 2;
	    }
	  break;
	case 3:
	  while (width--)
	    {
	      src[0] = lut0[src[0]];
	      src[1] = lut1[src[1]];
	      src[2] = lut2[src[2]];
	      src  += 3;
	    }
	  break;
	case 4:
	  while (width--)
	    {
	      src[0] = lut0[src[0]];
	      src[1] = lut1[src[1]];
	      src[2] = lut2[src[2]];
	      src[3] = lut3[src[3]];
	      src  += 4;
	    }
	  break;
	default:
	  g_warning ("gimplut: Error: nchannels = %d\n", lut->nchannels);
	}
      width = srcPR->w;
      src  += src_r_i;
254 255 256
    }
}

257 258 259 260 261
void
gimp_lut_process_2 (PixelRegion *srcPR,
		    PixelRegion *destPR,
		    GimpLut     *lut)
{
262
  gimp_lut_process (lut, srcPR, destPR);
263
}