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.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#include <libgimp/color_display.h>
|
|
|
|
#include <libgimp/gimpmodule.h>
|
|
|
|
#include <libgimp/parasite.h>
|
2000-05-05 21:29:46 +08:00
|
|
|
#include <libgimp/gimpmath.h>
|
2000-05-04 17:14:43 +08:00
|
|
|
#include <libgimp/gimpui.h>
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
#include "app/dialog_handler.h"
|
|
|
|
|
2000-01-09 01:20:50 +08:00
|
|
|
#include "modregister.h"
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
#include "libgimp/gimpintl.h"
|
|
|
|
|
|
|
|
|
2000-01-15 03:57:42 +08:00
|
|
|
#define COLOR_DISPLAY_NAME _("Gamma")
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
typedef struct _GammaContext GammaContext;
|
|
|
|
|
|
|
|
struct _GammaContext
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GFunc ok_func;
|
|
|
|
gpointer ok_data;
|
|
|
|
GFunc cancel_func;
|
|
|
|
gpointer cancel_data;
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
gdouble gamma;
|
|
|
|
guchar *lookup;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
GtkWidget *shell;
|
|
|
|
GtkWidget *spinner;
|
|
|
|
};
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
static gpointer gamma_new (int type);
|
|
|
|
static gpointer gamma_clone (gpointer cd_ID);
|
|
|
|
static void gamma_create_lookup_table (GammaContext *context);
|
|
|
|
static void gamma_destroy (gpointer cd_ID);
|
|
|
|
static void gamma_convert (gpointer cd_ID,
|
|
|
|
guchar *buf,
|
|
|
|
int w,
|
|
|
|
int h,
|
|
|
|
int bpp,
|
|
|
|
int bpl);
|
|
|
|
static void gamma_load (gpointer cd_ID,
|
|
|
|
GimpParasite *state);
|
|
|
|
static GimpParasite * gamma_save (gpointer cd_ID);
|
|
|
|
static void gamma_configure_ok_callback (GtkWidget *widget,
|
|
|
|
gpointer data);
|
|
|
|
static void gamma_configure_cancel_callback (GtkWidget *widget,
|
|
|
|
gpointer data);
|
|
|
|
static void gamma_configure (gpointer cd_ID,
|
|
|
|
GFunc ok_func,
|
|
|
|
gpointer ok_data,
|
|
|
|
GFunc cancel_func,
|
|
|
|
gpointer cancel_data);
|
|
|
|
static void gamma_configure_cancel (gpointer cd_ID);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-04 17:14:43 +08:00
|
|
|
static GimpColorDisplayMethods methods =
|
|
|
|
{
|
1999-10-05 03:26:07 +08:00
|
|
|
NULL,
|
|
|
|
gamma_new,
|
2000-01-02 08:16:47 +08:00
|
|
|
gamma_clone,
|
1999-10-05 03:26:07 +08:00
|
|
|
gamma_convert,
|
|
|
|
gamma_destroy,
|
|
|
|
NULL,
|
|
|
|
gamma_load,
|
|
|
|
gamma_save,
|
1999-11-20 10:49:40 +08:00
|
|
|
gamma_configure,
|
|
|
|
gamma_configure_cancel
|
1999-10-05 03:26:07 +08:00
|
|
|
};
|
|
|
|
|
2000-05-04 17:14:43 +08:00
|
|
|
static GimpModuleInfo info =
|
|
|
|
{
|
1999-10-05 03:26:07 +08:00
|
|
|
NULL,
|
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>",
|
|
|
|
"v0.1",
|
|
|
|
"(c) 1999, released under the GPL",
|
|
|
|
"October 3, 1999"
|
|
|
|
};
|
|
|
|
|
|
|
|
G_MODULE_EXPORT GimpModuleStatus
|
|
|
|
module_init (GimpModuleInfo **inforet)
|
|
|
|
{
|
2000-01-09 01:20:50 +08:00
|
|
|
#ifndef __EMX__
|
1999-10-05 03:26:07 +08:00
|
|
|
if (gimp_color_display_register (COLOR_DISPLAY_NAME, &methods))
|
2000-01-09 01:20:50 +08:00
|
|
|
#else
|
|
|
|
if (mod_color_display_register (COLOR_DISPLAY_NAME, &methods))
|
|
|
|
#endif
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
|
|
|
*inforet = &info;
|
|
|
|
return GIMP_MODULE_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return GIMP_MODULE_UNLOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT void
|
2000-05-27 06:28:40 +08:00
|
|
|
module_unload (void *shutdown_data,
|
|
|
|
void (*completed_cb) (void *),
|
|
|
|
void *completed_data)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2000-05-05 21:29:46 +08:00
|
|
|
#ifndef __EMX__
|
1999-10-05 03:26:07 +08:00
|
|
|
gimp_color_display_unregister (COLOR_DISPLAY_NAME);
|
2000-01-09 01:20:50 +08:00
|
|
|
#else
|
|
|
|
mod_color_display_unregister (COLOR_DISPLAY_NAME);
|
|
|
|
#endif
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gamma_new (int type)
|
|
|
|
{
|
|
|
|
int i;
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-01-03 15:49:19 +08:00
|
|
|
context = g_new0 (GammaContext, 1);
|
1999-10-07 07:48:01 +08:00
|
|
|
context->gamma = 1.0;
|
1999-10-05 03:26:07 +08:00
|
|
|
context->lookup = g_new (guchar, 256);
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
context->lookup[i] = i;
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2000-01-02 08:16:47 +08:00
|
|
|
static gpointer
|
|
|
|
gamma_clone (gpointer cd_ID)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *src_context = cd_ID;
|
|
|
|
GammaContext *context;
|
2000-01-02 08:16:47 +08:00
|
|
|
|
|
|
|
context = gamma_new (0);
|
|
|
|
context->gamma = src_context->gamma;
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
memcpy (context->lookup, src_context->lookup, sizeof (guchar) * 256);
|
2000-01-02 08:16:47 +08:00
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
static void
|
|
|
|
gamma_create_lookup_table (GammaContext *context)
|
|
|
|
{
|
2000-05-05 21:29:46 +08:00
|
|
|
gdouble one_over_gamma;
|
|
|
|
gdouble ind;
|
|
|
|
gint i;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
if (context->gamma == 0.0)
|
|
|
|
context->gamma = 1.0;
|
|
|
|
|
|
|
|
one_over_gamma = 1.0 / context->gamma;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
ind = (double) i / 255.0;
|
2000-05-27 06:28:40 +08:00
|
|
|
context->lookup[i] = (guchar) (gint) (255 * pow (ind, one_over_gamma));
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_destroy (gpointer cd_ID)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = cd_ID;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
if (context->shell)
|
2000-05-05 21:29:46 +08:00
|
|
|
{
|
|
|
|
dialog_unregister (context->shell);
|
|
|
|
gtk_widget_destroy (context->shell);
|
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
g_free (context->lookup);
|
|
|
|
g_free (context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_convert (gpointer cd_ID,
|
|
|
|
guchar *buf,
|
2000-05-05 21:29:46 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint bpp,
|
|
|
|
gint bpl)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
|
|
|
guchar *lookup = ((GammaContext *) cd_ID)->lookup;
|
2000-05-05 21:29:46 +08:00
|
|
|
gint i, j = height;
|
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--)
|
|
|
|
{
|
|
|
|
*buf = lookup[*buf];
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
buf += bpl;
|
|
|
|
}
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-05-27 06:28:40 +08:00
|
|
|
gamma_load (gpointer cd_ID,
|
|
|
|
GimpParasite *state)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = cd_ID;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2000-05-27 06:28:40 +08:00
|
|
|
memcpy (&context->gamma, gimp_parasite_data (state), sizeof (gdouble));
|
1999-10-05 03:26:07 +08:00
|
|
|
#else
|
2000-05-27 06:28:40 +08:00
|
|
|
guint32 buf[2], *data = gimp_parasite_data (state);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
buf[0] = g_ntohl (data[1]);
|
|
|
|
buf[1] = g_ntohl (data[0]);
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
memcpy (&context->gamma, buf, sizeof (gdouble));
|
1999-10-05 03:26:07 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
gamma_create_lookup_table (context);
|
|
|
|
}
|
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
static GimpParasite *
|
1999-10-05 03:26:07 +08:00
|
|
|
gamma_save (gpointer cd_ID)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = cd_ID;
|
1999-10-05 03:26:07 +08:00
|
|
|
guint32 buf[2];
|
|
|
|
|
|
|
|
memcpy (buf, &context->gamma, sizeof (double));
|
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
{
|
|
|
|
guint32 tmp = g_htonl (buf[0]);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_configure_ok_callback (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = data;
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-27 06:28:40 +08:00
|
|
|
context->gamma =
|
|
|
|
gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (context->spinner));
|
1999-10-05 03:26:07 +08:00
|
|
|
gamma_create_lookup_table (context);
|
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
dialog_unregister (context->shell);
|
2000-01-03 15:49:19 +08:00
|
|
|
gtk_widget_destroy (GTK_WIDGET (context->shell));
|
|
|
|
context->shell = NULL;
|
|
|
|
|
|
|
|
if (context->ok_func)
|
|
|
|
context->ok_func (context, context->ok_data);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_configure_cancel_callback (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = data;
|
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
dialog_unregister (context->shell);
|
2000-01-03 15:49:19 +08:00
|
|
|
gtk_widget_destroy (GTK_WIDGET (context->shell));
|
|
|
|
context->shell = NULL;
|
|
|
|
|
|
|
|
if (context->cancel_func)
|
|
|
|
context->cancel_func (context, context->cancel_data);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-01-03 15:49:19 +08:00
|
|
|
gamma_configure (gpointer cd_ID,
|
|
|
|
GFunc ok_func,
|
|
|
|
gpointer ok_data,
|
|
|
|
GFunc cancel_func,
|
|
|
|
gpointer cancel_data)
|
1999-10-05 03:26:07 +08:00
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = cd_ID;
|
1999-10-05 03:26:07 +08:00
|
|
|
GtkWidget *hbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkObject *adjustment;
|
|
|
|
|
|
|
|
if (!context->shell)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
context->ok_func = ok_func;
|
|
|
|
context->ok_data = ok_data;
|
|
|
|
context->cancel_func = cancel_func;
|
|
|
|
context->cancel_data = cancel_data;
|
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
context->shell =
|
|
|
|
gimp_dialog_new (_("Gamma"), "gamma",
|
|
|
|
gimp_standard_help_func, "modules/gamma.html",
|
|
|
|
GTK_WIN_POS_MOUSE,
|
|
|
|
FALSE, TRUE, FALSE,
|
|
|
|
|
|
|
|
_("OK"), gamma_configure_ok_callback,
|
|
|
|
cd_ID, NULL, NULL, TRUE, FALSE,
|
|
|
|
_("Cancel"), gamma_configure_cancel_callback,
|
|
|
|
cd_ID, NULL, NULL, FALSE, TRUE,
|
|
|
|
|
|
|
|
NULL);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-05 21:29:46 +08:00
|
|
|
dialog_register (context->shell);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
2000-05-04 17:14:43 +08:00
|
|
|
hbox = gtk_hbox_new (FALSE, 2);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
|
1999-10-05 03:26:07 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (context->shell)->vbox),
|
|
|
|
hbox, FALSE, FALSE, 0);
|
|
|
|
|
2000-01-03 21:01:51 +08:00
|
|
|
label = gtk_label_new ( _("Gamma:"));
|
1999-10-05 03:26:07 +08:00
|
|
|
gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
|
2000-05-04 17:14:43 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
|
1999-10-05 03:26:07 +08:00
|
|
|
|
|
|
|
adjustment = gtk_adjustment_new (1.0, 0.01, 10.0, 0.01, 0.1, 0.0);
|
|
|
|
context->spinner = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment),
|
|
|
|
0.1, 3);
|
|
|
|
gtk_widget_set_usize (context->spinner, 100, 0);
|
2000-05-04 17:14:43 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), context->spinner, FALSE, FALSE, 0);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
2000-05-04 17:14:43 +08:00
|
|
|
|
|
|
|
gtk_widget_show_all (context->shell);
|
1999-10-05 03:26:07 +08:00
|
|
|
}
|
1999-11-20 10:49:40 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gamma_configure_cancel (gpointer cd_ID)
|
|
|
|
{
|
2000-01-03 15:49:19 +08:00
|
|
|
GammaContext *context = cd_ID;
|
1999-11-20 10:49:40 +08:00
|
|
|
|
|
|
|
if (context->shell)
|
2000-05-05 21:29:46 +08:00
|
|
|
{
|
|
|
|
dialog_unregister (context->shell);
|
|
|
|
gtk_widget_destroy (context->shell);
|
|
|
|
context->shell = NULL;
|
|
|
|
}
|
2000-01-03 15:49:19 +08:00
|
|
|
|
|
|
|
if (context->cancel_func)
|
|
|
|
context->cancel_func (context, context->cancel_data);
|
1999-11-20 10:49:40 +08:00
|
|
|
}
|