Commit e5943a63 authored by Manish Singh's avatar Manish Singh
Browse files

Recommitting, perserving history

parent 4d139659
This diff is collapsed.
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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.
*/
gint32 load_image (const gchar *filename,
GimpRunMode runmode,
gboolean preview);
#ifdef HAVE_EXIF
gint32 load_thumbnail_image (const gchar *filename,
gint *width,
gint *height);
#endif /* HAVE_EXIF */
This diff is collapsed.
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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.
*/
#define SCALE_WIDTH 125
/* if you are not compiling this from inside the gimp tree, you have to */
/* take care yourself if your JPEG library supports progressive mode */
/* #undef HAVE_PROGRESSIVE_JPEG if your library doesn't support it */
/* #define HAVE_PROGRESSIVE_JPEG if your library knows how to handle it */
/* See bugs #63610 and #61088 for a discussion about the quality settings */
#define DEFAULT_QUALITY 85
#define DEFAULT_SMOOTHING 0.0
#define DEFAULT_OPTIMIZE TRUE
#define DEFAULT_PROGRESSIVE FALSE
#define DEFAULT_BASELINE TRUE
#define DEFAULT_SUBSMP 0
#define DEFAULT_RESTART 0
#define DEFAULT_DCT 0
#define DEFAULT_PREVIEW FALSE
#define DEFAULT_EXIF TRUE
#define DEFAULT_THUMBNAIL FALSE
typedef struct
{
gdouble quality;
gdouble smoothing;
gboolean optimize;
gboolean progressive;
gboolean baseline;
gint subsmp;
gint restart;
gint dct;
gboolean preview;
gboolean save_exif;
gboolean save_thumbnail;
} JpegSaveVals;
JpegSaveVals jsvals;
gint32 orig_image_ID_global;
gint32 drawable_ID_global;
gboolean save_image (const gchar *filename,
gint32 image_ID,
gint32 drawable_ID,
gint32 orig_image_ID,
gboolean preview);
gboolean save_dialog (void);
#ifdef HAVE_EXIF
gint create_thumbnail (gint32 image_ID,
gint32 drawable_ID,
gchar **thumbnail_buffer);
#endif /* HAVE_EXIF */
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 "jpeg.h"
#include "jpeg-save.h"
#include "jpeg-load.h"
/* Declare local functions.
*/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
};
MAIN ()
static void
query (void)
{
static GimpParamDef load_args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to load" }
};
static GimpParamDef load_return_vals[] =
{
{ GIMP_PDB_IMAGE, "image", "Output image" }
};
#ifdef HAVE_EXIF
static GimpParamDef thumb_args[] =
{
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
{ GIMP_PDB_INT32, "thumb_size", "Preferred thumbnail size" }
};
static GimpParamDef thumb_return_vals[] =
{
{ GIMP_PDB_IMAGE, "image", "Thumbnail image" },
{ GIMP_PDB_INT32, "image_width", "Width of full-sized image" },
{ GIMP_PDB_INT32, "image_height", "Height of full-sized image" }
};
#endif /* HAVE_EXIF */
static GimpParamDef save_args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to save the image in" },
{ GIMP_PDB_FLOAT, "quality", "Quality of saved image (0 <= quality <= 1)" },
{ GIMP_PDB_FLOAT, "smoothing", "Smoothing factor for saved image (0 <= smoothing <= 1)" },
{ GIMP_PDB_INT32, "optimize", "Optimization of entropy encoding parameters (0/1)" },
{ GIMP_PDB_INT32, "progressive", "Enable progressive jpeg image loading - ignored if not compiled with HAVE_PROGRESSIVE_JPEG (0/1)" },
{ GIMP_PDB_STRING, "comment", "Image comment" },
{ GIMP_PDB_INT32, "subsmp", "The subsampling option number" },
{ GIMP_PDB_INT32, "baseline", "Force creation of a baseline JPEG (non-baseline JPEGs can't be read by all decoders) (0/1)" },
{ GIMP_PDB_INT32, "restart", "Frequency of restart markers (in rows, 0 = no restart markers)" },
{ GIMP_PDB_INT32, "dct", "DCT algorithm to use (speed/quality tradeoff)" }
};
gimp_install_procedure ("file_jpeg_load",
"loads files in the JPEG file format",
"loads files in the JPEG file format",
"Spencer Kimball, Peter Mattis & others",
"Spencer Kimball & Peter Mattis",
"1995-1999",
N_("JPEG image"),
NULL,
GIMP_PLUGIN,
G_N_ELEMENTS (load_args),
G_N_ELEMENTS (load_return_vals),
load_args, load_return_vals);
gimp_register_file_handler_mime ("file_jpeg_load", "image/jpeg");
gimp_register_magic_load_handler ("file_jpeg_load",
"jpg,jpeg,jpe",
"",
"6,string,JFIF,6,string,Exif");
#ifdef HAVE_EXIF
gimp_install_procedure ("file_jpeg_load_thumb",
"Loads a thumbnail from a JPEG image",
"Loads a thumbnail from a JPEG image (only if it exists)",
"S. Mukund <muks@mukund.org>, Sven Neumann <sven@gimp.org>",
"S. Mukund <muks@mukund.org>, Sven Neumann <sven@gimp.org>",
"November 15, 2004",
NULL,
NULL,
GIMP_PLUGIN,
G_N_ELEMENTS (thumb_args),
G_N_ELEMENTS (thumb_return_vals),
thumb_args, thumb_return_vals);
gimp_register_thumbnail_loader ("file_jpeg_load", "file_jpeg_load_thumb");
#endif /* HAVE_EXIF */
gimp_install_procedure ("file_jpeg_save",
"saves files in the JPEG file format",
"saves files in the lossy, widely supported JPEG format",
"Spencer Kimball, Peter Mattis & others",
"Spencer Kimball & Peter Mattis",
"1995-1999",
N_("JPEG image"),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (save_args), 0,
save_args, NULL);
gimp_register_file_handler_mime ("file_jpeg_save", "image/jpeg");
gimp_register_save_handler ("file_jpeg_save", "jpg,jpeg,jpe", "");
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[4];
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint32 image_ID;
gint32 drawable_ID;
gint32 orig_image_ID;
GimpParasite *parasite;
gboolean err;
GimpExportReturn export = GIMP_EXPORT_CANCEL;
run_mode = param[0].data.d_int32;
INIT_I18N ();
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
if (strcmp (name, "file_jpeg_load") == 0)
{
image_ID = load_image (param[1].data.d_string, run_mode, FALSE);
if (image_ID != -1)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_IMAGE;
values[1].data.d_image = image_ID;
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
#ifdef HAVE_EXIF
else if (strcmp (name, "file_jpeg_load_thumb") == 0)
{
if (nparams < 2)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
const gchar *filename = param[0].data.d_string;
gint width = 0;
gint height = 0;
gint32 image_ID;
image_ID = load_thumbnail_image (filename, &width, &height);
if (image_ID != -1)
{
*nreturn_vals = 4;
values[1].type = GIMP_PDB_IMAGE;
values[1].data.d_image = image_ID;
values[2].type = GIMP_PDB_INT32;
values[2].data.d_int32 = width;
values[3].type = GIMP_PDB_INT32;
values[3].data.d_int32 = height;
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
}
#endif /* HAVE_EXIF */
else if (strcmp (name, "file_jpeg_save") == 0)
{
image_ID = orig_image_ID = param[1].data.d_int32;
drawable_ID = param[2].data.d_int32;
/* eventually export the image */
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
case GIMP_RUN_WITH_LAST_VALS:
gimp_ui_init ("jpeg", FALSE);
export = gimp_export_image (&image_ID, &drawable_ID, "JPEG",
(GIMP_EXPORT_CAN_HANDLE_RGB |
GIMP_EXPORT_CAN_HANDLE_GRAY));
switch (export)
{
case GIMP_EXPORT_EXPORT:
{
gchar *tmp = g_filename_from_utf8 (_("Export Preview"), -1,
NULL, NULL, NULL);
if (tmp)
{
gimp_image_set_filename (image_ID, tmp);
g_free (tmp);
}
display_ID = -1;
}
break;
case GIMP_EXPORT_IGNORE:
break;
case GIMP_EXPORT_CANCEL:
values[0].data.d_status = GIMP_PDB_CANCEL;
return;
break;
}
break;
default:
break;
}
#ifdef HAVE_EXIF
exif_data_unref (exif_data);
exif_data = NULL;
#endif /* HAVE_EXIF */
g_free (image_comment);
image_comment = NULL;
parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
if (parasite)
{
image_comment = g_strndup (gimp_parasite_data (parasite),
gimp_parasite_data_size (parasite));
gimp_parasite_free (parasite);
}
#ifdef HAVE_EXIF
parasite = gimp_image_parasite_find (orig_image_ID, "exif-data");
if (parasite)
{
exif_data = exif_data_new_from_data (gimp_parasite_data (parasite),
gimp_parasite_data_size (parasite));
gimp_parasite_free (parasite);
}
#endif /* HAVE_EXIF */
jsvals.quality = DEFAULT_QUALITY;
jsvals.smoothing = DEFAULT_SMOOTHING;
jsvals.optimize = DEFAULT_OPTIMIZE;
jsvals.progressive = DEFAULT_PROGRESSIVE;
jsvals.baseline = DEFAULT_BASELINE;
jsvals.subsmp = DEFAULT_SUBSMP;
jsvals.restart = DEFAULT_RESTART;
jsvals.dct = DEFAULT_DCT;
jsvals.preview = DEFAULT_PREVIEW;
jsvals.save_exif = DEFAULT_EXIF;
jsvals.save_thumbnail = DEFAULT_THUMBNAIL;
#ifdef HAVE_EXIF
if (exif_data && (exif_data->data))
jsvals.save_thumbnail = TRUE;
#endif /* HAVE_EXIF */
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data ("file_jpeg_save", &jsvals);
/* load up the previously used values */
parasite = gimp_image_parasite_find (orig_image_ID,
"jpeg-save-options");
if (parasite)
{
const JpegSaveVals *save_vals = gimp_parasite_data (parasite);
jsvals.quality = save_vals->quality;
jsvals.smoothing = save_vals->smoothing;
jsvals.optimize = save_vals->optimize;
jsvals.progressive = save_vals->progressive;
jsvals.baseline = save_vals->baseline;
jsvals.subsmp = save_vals->subsmp;
jsvals.restart = save_vals->restart;
jsvals.dct = save_vals->dct;
jsvals.preview = save_vals->preview;
jsvals.save_exif = save_vals->save_exif;
jsvals.save_thumbnail = save_vals->save_thumbnail;
gimp_parasite_free (parasite);
}
if (jsvals.preview)
{
/* we freeze undo saving so that we can avoid sucking up
* tile cache with our unneeded preview steps. */
gimp_image_undo_freeze (image_ID);
undo_touched = TRUE;
}
/* prepare for the preview */
image_ID_global = image_ID;
orig_image_ID_global = orig_image_ID;
drawable_ID_global = drawable_ID;
/* First acquire information with a dialog */
err = save_dialog ();
if (undo_touched)
{
/* thaw undo saving and flush the displays to have them
* reflect the current shortcuts */
gimp_image_undo_thaw (image_ID);
gimp_displays_flush ();
}
if (!err)
status = GIMP_PDB_CANCEL;
break;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are there! */
/* pw - added two more progressive and comment */
/* sg - added subsampling, preview, baseline, restarts and DCT */
if (nparams != 14)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
/* Once the PDB gets default parameters, remove this hack */
if (param[5].data.d_float > 0.05)
{
jsvals.quality = 100.0 * param[5].data.d_float;
jsvals.smoothing = param[6].data.d_float;
jsvals.optimize = param[7].data.d_int32;
#ifdef HAVE_PROGRESSIVE_JPEG
jsvals.progressive = param[8].data.d_int32;
#endif /* HAVE_PROGRESSIVE_JPEG */
jsvals.baseline = param[11].data.d_int32;
jsvals.subsmp = param[10].data.d_int32;
jsvals.restart = param[12].data.d_int32;
jsvals.dct = param[13].data.d_int32;
/* free up the default -- wasted some effort earlier */
g_free (image_comment);
image_comment = g_strdup (param[9].data.d_string);
}
jsvals.preview = FALSE;
if (jsvals.quality < 0.0 || jsvals.quality > 100.0)
status = GIMP_PDB_CALLING_ERROR;
else if (jsvals.smoothing < 0.0 || jsvals.smoothing > 1.0)
status = GIMP_PDB_CALLING_ERROR;
else if (jsvals.subsmp < 0 || jsvals.subsmp > 2)
status = GIMP_PDB_CALLING_ERROR;
else if (jsvals.dct < 0 || jsvals.dct > 2)
status = GIMP_PDB_CALLING_ERROR;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Possibly retrieve data */
gimp_get_data ("file_jpeg_save", &jsvals);
parasite = gimp_image_parasite_find (orig_image_ID,
"jpeg-save-options");
if (parasite)
{
const JpegSaveVals *save_vals = gimp_parasite_data (parasite);
jsvals.quality = save_vals->quality;
jsvals.smoothing = save_vals->smoothing;
jsvals.optimize = save_vals->optimize;
jsvals.progressive = save_vals->progressive;
jsvals.baseline = save_vals->baseline;
jsvals.subsmp = save_vals->subsmp;
jsvals.restart = save_vals->restart;
jsvals.dct = save_vals->dct;
jsvals.preview = FALSE;
gimp_parasite_free (parasite);
}
break;
default:
break;
}
if (status == GIMP_PDB_SUCCESS)
{
if (save_image (param[3].data.d_string,
image_ID,
drawable_ID,
orig_image_ID,
FALSE))
{
/* Store mvals data */
gimp_set_data ("file_jpeg_save", &jsvals, sizeof (JpegSaveVals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
if (export == GIMP_EXPORT_EXPORT)
{
/* If the image was exported, delete the new display. */
/* This also deletes the image. */
if (display_ID != -1)
gimp_display_delete (display_ID);
else
gimp_image_delete (image_ID);
}
/* pw - now we need to change the defaults to be whatever
* was used to save this image. Dump the old parasites
* and add new ones. */
gimp_image_parasite_detach (orig_image_ID, "gimp-comment");
if (image_comment && strlen (image_comment))
{
parasite = gimp_parasite_new ("gimp-comment",
GIMP_PARASITE_PERSISTENT,
strlen (image_comment) + 1,
image_comment);
gimp_image_parasite_attach (orig_image_ID, parasite);
gimp_parasite_free (parasite);
}
gimp_image_parasite_detach (orig_image_ID, "jpeg-save-options");
parasite = gimp_parasite_new ("jpeg-save-options",
0, sizeof (jsvals), &jsvals);
gimp_image_parasite_attach (orig_image_ID, parasite);
gimp_parasite_free (parasite);
}
else
{
status = GIMP_PDB_CALLING_ERROR;
}
values[0].data.d_status = status;
}
/*
* Here's the routine that will replace the standard error_exit method:
*/
void
my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);