1999-10-05 03:26:07 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
1999-11-20 10:49:40 +08:00
|
|
|
* Copyright (C) 1999 Manish Singh <yosh@gimp.org>
|
1999-10-05 03:26:07 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2002-10-20 18:14:17 +08:00
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2002-03-15 06:42:50 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
2002-10-21 22:15:02 +08:00
|
|
|
#include "libgimpmodule/gimpmodule.h"
|
2002-03-15 06:42:50 +08:00
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
2000-05-27 09:30:21 +08:00
|
|
|
|
2002-10-30 23:12:30 +08:00
|
|
|
#include "libgimp/libgimp-intl.h"
|
2000-05-05 21:29:46 +08:00
|
|
|
|
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
#define DEFAULT_GAMMA 1.0
|
|
|
|
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
#define CDISPLAY_TYPE_GAMMA (cdisplay_gamma_type)
|
|
|
|
#define CDISPLAY_GAMMA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_GAMMA, CdisplayGamma))
|
|
|
|
#define CDISPLAY_GAMMA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_GAMMA, CdisplayGammaClass))
|
|
|
|
#define CDISPLAY_IS_GAMMA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_GAMMA))
|
|
|
|
#define CDISPLAY_IS_GAMMA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_GAMMA))
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
typedef struct _CdisplayGamma CdisplayGamma;
|
|
|
|
typedef struct _CdisplayGammaClass CdisplayGammaClass;
|
|
|
|
|
|
|
|
struct _CdisplayGamma
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
GimpColorDisplay parent_instance;
|
|
|
|
|
|
|
|
gdouble gamma;
|
|
|
|
guchar *lookup;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkObject *adjustment;
|
1999-10-05 03:26:07 +08:00
|
|
|
};
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
struct _CdisplayGammaClass
|
2000-05-04 17:14:43 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
GimpColorDisplayClass parent_instance;
|
1999-10-05 03:26:07 +08:00
|
|
|
};
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
|
|
|
|
static GType cdisplay_gamma_get_type (GTypeModule *module);
|
|
|
|
static void cdisplay_gamma_class_init (CdisplayGammaClass *klass);
|
|
|
|
static void cdisplay_gamma_init (CdisplayGamma *gamma);
|
|
|
|
|
|
|
|
static void cdisplay_gamma_finalize (GObject *object);
|
|
|
|
|
|
|
|
static GimpColorDisplay * cdisplay_gamma_clone (GimpColorDisplay *display);
|
|
|
|
static void cdisplay_gamma_convert (GimpColorDisplay *display,
|
|
|
|
guchar *buf,
|
|
|
|
gint w,
|
|
|
|
gint h,
|
|
|
|
gint bpp,
|
|
|
|
gint bpl);
|
|
|
|
static void cdisplay_gamma_load_state (GimpColorDisplay *display,
|
|
|
|
GimpParasite *state);
|
|
|
|
static GimpParasite * cdisplay_gamma_save_state (GimpColorDisplay *display);
|
2002-10-23 22:55:07 +08:00
|
|
|
static GtkWidget * cdisplay_gamma_configure (GimpColorDisplay *display);
|
|
|
|
static void cdisplay_gamma_configure_reset (GimpColorDisplay *display);
|
2002-10-20 18:14:17 +08:00
|
|
|
|
|
|
|
static void gamma_create_lookup_table (CdisplayGamma *gamma);
|
2002-10-23 22:55:07 +08:00
|
|
|
static void gamma_configure_adj_callback (GtkAdjustment *adj,
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma);
|
|
|
|
|
|
|
|
|
2002-10-20 20:40:30 +08:00
|
|
|
static const GimpModuleInfo cdisplay_gamma_info =
|
2000-05-04 17:14:43 +08:00
|
|
|
{
|
2000-01-15 03:57:42 +08:00
|
|
|
N_("Gamma color display filter"),
|
1999-10-05 03:26:07 +08:00
|
|
|
"Manish Singh <yosh@gimp.org>",
|
2000-10-16 20:07:55 +08:00
|
|
|
"v0.2",
|
1999-10-05 03:26:07 +08:00
|
|
|
"(c) 1999, released under the GPL",
|
2000-10-16 20:07:55 +08:00
|
|
|
"October 14, 2000"
|
1999-10-05 03:26:07 +08:00
|
|
|
};
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
static GType cdisplay_gamma_type = 0;
|
|
|
|
static GimpColorDisplayClass *parent_class = NULL;
|
|
|
|
|
|
|
|
|
2002-10-20 20:40:30 +08:00
|
|
|
G_MODULE_EXPORT const GimpModuleInfo *
|
|
|
|
gimp_module_query (GTypeModule *module)
|
|
|
|
{
|
|
|
|
return &cdisplay_gamma_info;
|
|
|
|
}
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
G_MODULE_EXPORT gboolean
|
2002-10-20 20:40:30 +08:00
|
|
|
gimp_module_register (GTypeModule *module)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
cdisplay_gamma_get_type (module);
|
|
|
|
|
|
|
|
return TRUE;
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
static GType
|
|
|
|
cdisplay_gamma_get_type (GTypeModule *module)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
if (! cdisplay_gamma_type)
|
|
|
|
{
|
2002-12-18 23:37:56 +08:00
|
|
|
static const GTypeInfo display_info =
|
2002-10-20 18:14:17 +08:00
|
|
|
{
|
|
|
|
sizeof (CdisplayGammaClass),
|
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) cdisplay_gamma_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (CdisplayGamma),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) cdisplay_gamma_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
cdisplay_gamma_type =
|
|
|
|
g_type_module_register_type (module,
|
|
|
|
GIMP_TYPE_COLOR_DISPLAY,
|
|
|
|
"CdisplayGamma",
|
2002-12-18 23:37:56 +08:00
|
|
|
&display_info, 0);
|
2002-10-20 18:14:17 +08:00
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
return cdisplay_gamma_type;
|
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
static void
|
|
|
|
cdisplay_gamma_class_init (CdisplayGammaClass *klass)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
GObjectClass *object_class;
|
|
|
|
GimpColorDisplayClass *display_class;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
display_class = GIMP_COLOR_DISPLAY_CLASS (klass);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
object_class->finalize = cdisplay_gamma_finalize;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
display_class->name = _("Gamma");
|
|
|
|
display_class->help_page = "modules/gamma.html";
|
|
|
|
display_class->clone = cdisplay_gamma_clone;
|
|
|
|
display_class->convert = cdisplay_gamma_convert;
|
|
|
|
display_class->load_state = cdisplay_gamma_load_state;
|
|
|
|
display_class->save_state = cdisplay_gamma_save_state;
|
|
|
|
display_class->configure = cdisplay_gamma_configure;
|
|
|
|
display_class->configure_reset = cdisplay_gamma_configure_reset;
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
static void
|
|
|
|
cdisplay_gamma_init (CdisplayGamma *gamma)
|
2000-01-02 08:16:47 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
gint i;
|
2000-01-02 08:16:47 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
gamma->gamma = DEFAULT_GAMMA;
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma->lookup = g_new (guchar, 256);
|
2000-01-02 08:16:47 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
gamma->lookup[i] = i;
|
2000-01-02 08:16:47 +08:00
|
|
|
}
|
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
static void
|
2002-10-20 18:14:17 +08:00
|
|
|
cdisplay_gamma_finalize (GObject *object)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma = CDISPLAY_GAMMA (object);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
if (gamma->hbox)
|
|
|
|
gtk_widget_destroy (gamma->hbox);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
if (gamma->lookup)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
g_free (gamma->lookup);
|
|
|
|
gamma->lookup = NULL;
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
2002-10-20 18:14:17 +08:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
|
|
|
|
static GimpColorDisplay *
|
|
|
|
cdisplay_gamma_clone (GimpColorDisplay *display)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
|
|
|
CdisplayGamma *copy;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
|
|
|
|
|
|
|
copy = CDISPLAY_GAMMA (gimp_color_display_new (G_TYPE_FROM_INSTANCE (gamma)));
|
|
|
|
|
|
|
|
copy->gamma = gamma->gamma;
|
|
|
|
|
|
|
|
memcpy (copy->lookup, gamma->lookup, sizeof (guchar) * 256);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
return GIMP_COLOR_DISPLAY (copy);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-10-20 18:14:17 +08:00
|
|
|
cdisplay_gamma_convert (GimpColorDisplay *display,
|
|
|
|
guchar *buf,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint bpp,
|
|
|
|
gint bpl)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
|
|
|
gint i, j = height;
|
|
|
|
|
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
1999-10-05 10:16:59 +08:00
|
|
|
|
|
|
|
/* You will not be using the entire buffer most of the time.
|
|
|
|
* Hence, the simplistic code for this is as follows:
|
|
|
|
*
|
|
|
|
* for (j = 0; j < height; j++)
|
|
|
|
* {
|
|
|
|
* for (i = 0; i < width * bpp; i++)
|
|
|
|
* buf[i] = lookup[buf[i]];
|
|
|
|
* buf += bpl;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
width *= bpp;
|
|
|
|
bpl -= width;
|
|
|
|
|
|
|
|
while (j--)
|
|
|
|
{
|
|
|
|
i = width;
|
|
|
|
while (i--)
|
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
*buf = gamma->lookup[*buf];
|
1999-10-05 10:16:59 +08:00
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
buf += bpl;
|
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-10-20 18:14:17 +08:00
|
|
|
cdisplay_gamma_load_state (GimpColorDisplay *display,
|
|
|
|
GimpParasite *state)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
|
|
|
|
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2002-10-20 18:14:17 +08:00
|
|
|
memcpy (&gamma->gamma, gimp_parasite_data (state), sizeof (gdouble));
|
1999-10-05 03:26:07 +08:00
|
|
|
#else
|
2002-10-20 18:14:17 +08:00
|
|
|
{
|
|
|
|
guint32 buf[2], *data = gimp_parasite_data (state);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
buf[0] = g_ntohl (data[1]);
|
|
|
|
buf[1] = g_ntohl (data[0]);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
memcpy (&gamma->gamma, buf, sizeof (gdouble));
|
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
#endif
|
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma_create_lookup_table (gamma);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
static GimpParasite *
|
2002-10-20 18:14:17 +08:00
|
|
|
cdisplay_gamma_save_state (GimpColorDisplay *display)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
|
|
|
guint32 buf[2];
|
|
|
|
|
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
memcpy (buf, &gamma->gamma, sizeof (double));
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
guint32 tmp;
|
|
|
|
|
|
|
|
tmp = g_htonl (buf[0]);
|
1999-10-05 03:26:07 +08:00
|
|
|
buf[0] = g_htonl (buf[1]);
|
|
|
|
buf[1] = tmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
return gimp_parasite_new ("Display/Gamma", GIMP_PARASITE_PERSISTENT,
|
|
|
|
sizeof (double), &buf);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
static GtkWidget *
|
|
|
|
cdisplay_gamma_configure (GimpColorDisplay *display)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
|
|
|
GtkWidget *label;
|
2002-10-23 22:55:07 +08:00
|
|
|
GtkWidget *spinbutton;
|
2002-03-15 06:42:50 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
2000-01-03 15:49:19 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
if (gamma->hbox)
|
|
|
|
gtk_widget_destroy (gamma->hbox);
|
|
|
|
|
|
|
|
gamma->hbox = gtk_hbox_new (FALSE, 4);
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (gamma->hbox), (gpointer) &gamma->hbox);
|
|
|
|
|
|
|
|
label = gtk_label_new ( _("Gamma:"));
|
|
|
|
gtk_box_pack_start (GTK_BOX (gamma->hbox), label, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (label);
|
2000-05-04 17:14:43 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
spinbutton = gimp_spin_button_new (&gamma->adjustment,
|
|
|
|
gamma->gamma, 0.01, 10.0, 0.01, 0.1, 0.0,
|
|
|
|
0.1, 3);
|
|
|
|
gtk_box_pack_start (GTK_BOX (gamma->hbox), spinbutton, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (spinbutton);
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (gamma->adjustment), "value_changed",
|
|
|
|
G_CALLBACK (gamma_configure_adj_callback),
|
|
|
|
gamma);
|
|
|
|
|
|
|
|
return gamma->hbox;
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
1999-11-20 10:49:40 +08:00
|
|
|
|
|
|
|
static void
|
2002-10-23 22:55:07 +08:00
|
|
|
cdisplay_gamma_configure_reset (GimpColorDisplay *display)
|
1999-11-20 10:49:40 +08:00
|
|
|
{
|
2002-10-20 18:14:17 +08:00
|
|
|
CdisplayGamma *gamma;
|
1999-11-20 10:49:40 +08:00
|
|
|
|
2002-10-20 18:14:17 +08:00
|
|
|
gamma = CDISPLAY_GAMMA (display);
|
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
if (gamma->adjustment)
|
|
|
|
gtk_adjustment_set_value (GTK_ADJUSTMENT (gamma->adjustment), DEFAULT_GAMMA);
|
2002-10-20 18:14:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_create_lookup_table (CdisplayGamma *gamma)
|
|
|
|
{
|
|
|
|
gdouble one_over_gamma;
|
|
|
|
gdouble ind;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (gamma->gamma == 0.0)
|
|
|
|
gamma->gamma = 1.0;
|
|
|
|
|
|
|
|
one_over_gamma = 1.0 / gamma->gamma;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
ind = (gdouble) i / 255.0;
|
|
|
|
gamma->lookup[i] = (guchar) (gint) (255 * pow (ind, one_over_gamma));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-10-23 22:55:07 +08:00
|
|
|
gamma_configure_adj_callback (GtkAdjustment *adj,
|
|
|
|
CdisplayGamma *gamma)
|
2002-10-20 18:14:17 +08:00
|
|
|
{
|
2002-10-23 22:55:07 +08:00
|
|
|
gamma->gamma = adj->value;
|
2002-10-20 18:14:17 +08:00
|
|
|
|
|
|
|
gamma_create_lookup_table (gamma);
|
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
gimp_color_display_changed (GIMP_COLOR_DISPLAY (gamma));
|
1999-11-20 10:49:40 +08:00
|
|
|
}
|