1999-02-16 16:53:54 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2000-12-17 05:37:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
#include <glib-object.h>
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
#include "gimplut.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "pixel-region.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
|
|
|
|
GimpLut *
|
2000-12-29 23:22:01 +08:00
|
|
|
gimp_lut_new (void)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2001-05-15 19:25:25 +08:00
|
|
|
GimpLut *lut;
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
lut = g_new (GimpLut, 1);
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
lut->luts = NULL;
|
|
|
|
lut->nchannels = 0;
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
return lut;
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-11-13 09:02:27 +08:00
|
|
|
gimp_lut_free (GimpLut *lut)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
gint i;
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
for (i = 0; i < lut->nchannels; i++)
|
2000-12-29 23:22:01 +08:00
|
|
|
g_free (lut->luts[i]);
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
g_free (lut->luts);
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-11-13 09:02:27 +08:00
|
|
|
gimp_lut_setup (GimpLut *lut,
|
|
|
|
GimpLutFunc func,
|
|
|
|
void *user_data,
|
2001-12-03 21:44:59 +08:00
|
|
|
gint nchannels)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2001-12-02 22:59:30 +08:00
|
|
|
guint i, v;
|
2000-12-29 23:22:01 +08:00
|
|
|
gdouble val;
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
if (lut->luts)
|
2000-12-29 23:22:01 +08:00
|
|
|
{
|
|
|
|
for (i = 0; i < lut->nchannels; i++)
|
|
|
|
g_free (lut->luts[i]);
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
g_free (lut->luts);
|
|
|
|
}
|
2001-05-15 19:25:25 +08:00
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
lut->nchannels = nchannels;
|
2001-05-15 19:25:25 +08:00
|
|
|
lut->luts = g_new (guchar *, lut->nchannels);
|
2000-12-29 23:22:01 +08:00
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
for (i = 0; i < lut->nchannels; i++)
|
2000-12-29 23:22:01 +08:00
|
|
|
{
|
|
|
|
lut->luts[i] = g_new (guchar, 256);
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
for (v = 0; v < 256; v++)
|
2001-05-15 19:25:25 +08:00
|
|
|
{
|
|
|
|
/* to add gamma correction use func(v ^ g) ^ 1/g instead. */
|
2000-12-29 23:22:01 +08:00
|
|
|
val = 255.0 * func (user_data, lut->nchannels, i, v/255.0) + 0.5;
|
2001-05-15 19:25:25 +08:00
|
|
|
|
2001-12-02 22:59:30 +08:00
|
|
|
lut->luts[i][v] = CLAMP (val, 0, 255);
|
2000-12-29 23:22:01 +08:00
|
|
|
}
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-11-13 09:02:27 +08:00
|
|
|
gimp_lut_setup_exact (GimpLut *lut,
|
|
|
|
GimpLutFunc func,
|
|
|
|
void *user_data,
|
2001-12-03 21:44:59 +08:00
|
|
|
gint nchannels)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
gimp_lut_setup (lut, func, user_data, nchannels);
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_lut_process (GimpLut *lut,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR)
|
|
|
|
{
|
2001-12-02 22:59:30 +08:00
|
|
|
guint h, width, src_r_i, dest_r_i;
|
2000-12-29 23:22:01 +08:00
|
|
|
guchar *src, *dest;
|
|
|
|
guchar *lut0 = NULL, *lut1 = NULL, *lut2 = NULL, *lut3 = NULL;
|
1999-02-16 16:53:54 +08:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
h = srcPR->h;
|
|
|
|
src = srcPR->data;
|
|
|
|
dest = destPR->data;
|
|
|
|
width = srcPR->w;
|
|
|
|
src_r_i = srcPR->rowstride - (srcPR->bytes * srcPR->w);
|
1999-02-16 16:53:54 +08:00
|
|
|
dest_r_i = destPR->rowstride - (destPR->bytes * srcPR->w);
|
|
|
|
|
|
|
|
if (src_r_i == 0 && dest_r_i == 0)
|
2000-12-29 23:22:01 +08:00
|
|
|
{
|
|
|
|
width *= h;
|
|
|
|
h = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
while (h--)
|
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
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;
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-09 14:00:11 +08:00
|
|
|
void
|
|
|
|
gimp_lut_process_inline (GimpLut *lut,
|
|
|
|
PixelRegion *srcPR)
|
|
|
|
{
|
2001-12-02 22:59:30 +08:00
|
|
|
guint h, width, src_r_i;
|
2000-12-29 23:22:01 +08:00
|
|
|
guchar *src;
|
|
|
|
guchar *lut0 = NULL, *lut1 = NULL, *lut2 = NULL, *lut3 = NULL;
|
1999-04-09 14:00:11 +08:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
h = srcPR->h;
|
|
|
|
src = srcPR->data;
|
|
|
|
width = srcPR->w;
|
1999-04-09 14:00:11 +08:00
|
|
|
src_r_i = srcPR->rowstride - (srcPR->bytes * srcPR->w);
|
|
|
|
|
|
|
|
if (src_r_i == 0)
|
2000-12-29 23:22:01 +08:00
|
|
|
{
|
|
|
|
width *= h;
|
|
|
|
h = 1;
|
|
|
|
}
|
|
|
|
|
1999-04-09 14:00:11 +08:00
|
|
|
while (h--)
|
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
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;
|
1999-04-09 14:00:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
void
|
|
|
|
gimp_lut_process_2 (PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR,
|
|
|
|
GimpLut *lut)
|
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
gimp_lut_process (lut, srcPR, destPR);
|
1999-02-16 16:53:54 +08:00
|
|
|
}
|