gimp/plug-ins/common/file-gif-load.c

1418 lines
40 KiB
C
Raw Normal View History

/* GIF loading file filter for GIMP 2.x
* +-------------------------------------------------------------------+
* | Copyright Adam D. Moss, Peter Mattis, Spencer Kimball |
* +-------------------------------------------------------------------+
* Version 1.50.4 - 2003/06/03
1999-03-20 19:50:30 +00:00
* Adam D. Moss - <adam@gimp.org> <adam@foxbox.org>
*/
/* Copyright notice for old GIF code from which this plugin was long ago */
/* derived (David Koblas has kindly granted permission to relicense): */
/* +-------------------------------------------------------------------+ */
/* | Copyright 1990, 1991, 1993, David Koblas. (koblas@extra.com) | */
/* +-------------------------------------------------------------------+ */
/* Also...
* 'This filter uses code taken from the "giftopnm" and "ppmtogif" programs
* which are part of the "netpbm" package.'
1999-03-20 19:50:30 +00:00
*/
/* Additionally...
1999-03-20 19:50:30 +00:00
* "The Graphics Interchange Format(c) is the Copyright property of
* CompuServe Incorporated. GIF(sm) is a Service Mark property of
plug-ins/FractalExplorer/Dialogs.c 2003-11-15 Michael Natterer <mitch@gimp.org> * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/FractalExplorer.c * plug-ins/bmp/bmpread.c * plug-ins/bmp/bmpwrite.c * plug-ins/common/CEL.c * plug-ins/common/CML_explorer.c * plug-ins/common/animoptimize.c * plug-ins/common/bz2.c * plug-ins/common/convmatrix.c * plug-ins/common/curve_bend.c * plug-ins/common/dicom.c * plug-ins/common/gauss_iir.c * plug-ins/common/gauss_rle.c * plug-ins/common/gbr.c * plug-ins/common/gif.c * plug-ins/common/gifload.c * plug-ins/common/gih.c * plug-ins/common/grid.c * plug-ins/common/gtm.c * plug-ins/common/gz.c * plug-ins/common/hrz.c * plug-ins/common/jpeg.c * plug-ins/common/mail.c * plug-ins/common/mapcolor.c * plug-ins/common/pat.c * plug-ins/common/pcx.c * plug-ins/common/pix.c * plug-ins/common/png.c * plug-ins/common/pnm.c * plug-ins/common/ps.c * plug-ins/common/psd.c * plug-ins/common/psd_save.c * plug-ins/common/psp.c * plug-ins/common/sel_gauss.c * plug-ins/common/spheredesigner.c * plug-ins/common/sunras.c * plug-ins/common/svg.c * plug-ins/common/tga.c * plug-ins/common/tiff.c * plug-ins/common/wmf.c * plug-ins/common/xbm.c * plug-ins/common/xwd.c * plug-ins/faxg3/faxg3.c * plug-ins/fits/fits.c * plug-ins/flame/flame.c * plug-ins/gfig/gfig.c * plug-ins/gflare/gflare.c * plug-ins/gfli/gfli.c * plug-ins/gimpressionist/brush.c * plug-ins/gimpressionist/ppmtool.c * plug-ins/helpbrowser/domain.c * plug-ins/ifscompose/ifscompose.c * plug-ins/sgi/sgi.c * plug-ins/twain/twain.c * plug-ins/winsnap/winsnap.c * plug-ins/xjt/xjt.c: removed explicit newlines from messages. Made file open/save messages the same all over the place. Reduced number of translatable strings by adding some more "standard" messages. Removed plug-in names from messages. Added some random mnemonics. Unmarked some strings for translation and added some that were forgotten. General message cleanup. Removed trailing whitespace.
2003-11-15 13:53:33 +00:00
* CompuServe Incorporated."
1999-03-20 19:50:30 +00:00
*/
/*
* REVISION HISTORY
*
* 2003/06/03
* 1.50.04 - When initializing the LZW state, watch out for a completely
* bogus input_code_size [based on fix by Raphael Quinet]
* Also, fix a stupid old bug when clearing the code table between
* subimages. (Enables us to deal better with errors when the stream is
* corrupted pretty early in a subimage.) [adam]
* Minor-version-bump to distinguish between gimp1.2/1.4 branches.
*
* 2000/03/31
* 1.00.03 - Just mildly more useful comments/messages concerning frame
* disposals.
*
* 1999/11/20
* 1.00.02 - Fixed a couple of possible infinite loops where an
* error condition was not being checked. Also changed some g_message()s
* back to g_warning()s as they should be (don't get carried away with
* the user feedback fellahs, no-one wants to be told of every single
* corrupt byte and block in its own little window. :-( ).
*
* 1999/11/11
* 1.00.01 - Fixed an uninitialized variable which has been around
* forever... thanks to jrb@redhat.com for noticing that there
* was a problem somewhere!
*
* 1999/03/20
1999-03-20 19:50:30 +00:00
* 1.00.00 - GIF load-only code split from main GIF plugin.
*
* For previous revision information, please consult the comments
* in the 'gif' plugin.
*/
/*
* TODO (more *'s means more important!)
*
* - PDB stuff for comments
*
* - Remove unused colormap entries for GRAYSCALE images.
1999-03-20 19:50:30 +00:00
*/
configure.in removed tips files, AC_SUBST GIMP_PLUGINS and GIMP_MODULES so * configure.in * Makefile.am: removed tips files, AC_SUBST GIMP_PLUGINS and GIMP_MODULES so you can easily skip those parts of the build * acinclude.m4 * config.sub * config.guess * ltconfig * ltmain.sh: libtool 1.3.2 * app/fileops.c: shuffle #includes to avoid warning about MIN and MAX [ The following is a big i18n patch from David Monniaux <david.monniaux@ens.fr> ] * tips/gimp_conseils.fr.txt * tips/gimp_tips.txt * tips/Makefile.am * configure.in: moved tips to separate dir * po-plugins: new dir for plug-in translation files * configure.in: add po-plugins dir and POTFILES processing * app/boundary.c * app/brightness_contrast.c * app/by_color_select.c * app/color_balance.c * app/convert.c * app/curves.c * app/free_select.c * app/gdisplay.c * app/gimpimage.c * app/gimpunit.c * app/gradient.c * app/gradient_select.c * app/install.c * app/session.c: various i18n tweaks * app/tips_dialog.c: localize tips filename * libgimp/gimpunit.c * libgimp/gimpunitmenu.c: #include "config.h" * plug-ins/CEL * plug-ins/CML_explorer * plug-ins/Lighting * plug-ins/apply_lens * plug-ins/autostretch_hsv * plug-ins/blur * plug-ins/bmp * plug-ins/borderaverage * plug-ins/bumpmap * plug-ins/bz2 * plug-ins/checkerboard * plug-ins/colorify * plug-ins/compose * plug-ins/convmatrix * plug-ins/cubism * plug-ins/depthmerge * plug-ins/destripe * plug-ins/gif * plug-ins/gifload * plug-ins/jpeg * plug-ins/mail * plug-ins/oilify * plug-ins/png * plug-ins/print * plug-ins/ps * plug-ins/xbm * plug-ins/xpm * plug-ins/xwd: plug-in i18n stuff -Yosh
1999-05-29 16:35:47 +00:00
#include "config.h"
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-25 17:46:56 +00:00
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 14:37:00 +00:00
#include <errno.h>
1999-03-20 19:50:30 +00:00
#include <string.h>
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-25 17:46:56 +00:00
#include <glib/gstdio.h>
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-25 17:46:56 +00:00
#include <libgimp/gimp.h>
configure.in removed tips files, AC_SUBST GIMP_PLUGINS and GIMP_MODULES so * configure.in * Makefile.am: removed tips files, AC_SUBST GIMP_PLUGINS and GIMP_MODULES so you can easily skip those parts of the build * acinclude.m4 * config.sub * config.guess * ltconfig * ltmain.sh: libtool 1.3.2 * app/fileops.c: shuffle #includes to avoid warning about MIN and MAX [ The following is a big i18n patch from David Monniaux <david.monniaux@ens.fr> ] * tips/gimp_conseils.fr.txt * tips/gimp_tips.txt * tips/Makefile.am * configure.in: moved tips to separate dir * po-plugins: new dir for plug-in translation files * configure.in: add po-plugins dir and POTFILES processing * app/boundary.c * app/brightness_contrast.c * app/by_color_select.c * app/color_balance.c * app/convert.c * app/curves.c * app/free_select.c * app/gdisplay.c * app/gimpimage.c * app/gimpunit.c * app/gradient.c * app/gradient_select.c * app/install.c * app/session.c: various i18n tweaks * app/tips_dialog.c: localize tips filename * libgimp/gimpunit.c * libgimp/gimpunitmenu.c: #include "config.h" * plug-ins/CEL * plug-ins/CML_explorer * plug-ins/Lighting * plug-ins/apply_lens * plug-ins/autostretch_hsv * plug-ins/blur * plug-ins/bmp * plug-ins/borderaverage * plug-ins/bumpmap * plug-ins/bz2 * plug-ins/checkerboard * plug-ins/colorify * plug-ins/compose * plug-ins/convmatrix * plug-ins/cubism * plug-ins/depthmerge * plug-ins/destripe * plug-ins/gif * plug-ins/gifload * plug-ins/jpeg * plug-ins/mail * plug-ins/oilify * plug-ins/png * plug-ins/print * plug-ins/ps * plug-ins/xbm * plug-ins/xpm * plug-ins/xwd: plug-in i18n stuff -Yosh
1999-05-29 16:35:47 +00:00
#include "libgimp/stdplugins-intl.h"
1999-03-20 19:50:30 +00:00
#define LOAD_PROC "file-gif-load"
#define LOAD_THUMB_PROC "file-gif-load-thumb"
1999-03-20 19:50:30 +00:00
/* uncomment the line below for a little debugging info */
/* #define GIFDEBUG yesplease */
typedef struct _Gif Gif;
typedef struct _GifClass GifClass;
struct _Gif
{
GimpPlugIn parent_instance;
};
struct _GifClass
{
GimpPlugInClass parent_class;
};
#define GIF_TYPE (gif_get_type ())
#define GIF(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIF_TYPE, Gif))
GType gif_get_type (void) G_GNUC_CONST;
static GList * gif_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * gif_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * gif_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data);
static GimpValueArray * gif_load_thumb (GimpProcedure *procedure,
GFile *file,
gint size,
GimpProcedureConfig *config,
gpointer run_data);
static GimpImage * load_image (GFile *file,
gboolean thumbnail,
GError **error);
G_DEFINE_TYPE (Gif, gif, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (GIF_TYPE)
DEFINE_STD_SET_I18N
1999-03-20 19:50:30 +00:00
static guchar used_cmap[3][256];
static guint highest_used_index;
static gboolean promote_to_rgb = FALSE;
static guchar gimp_cmap[768];
static GimpParasite *comment_parasite = NULL;
1999-03-20 19:50:30 +00:00
static void
gif_class_init (GifClass *klass)
1999-03-20 19:50:30 +00:00
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
1999-03-20 19:50:30 +00:00
plug_in_class->query_procedures = gif_query_procedures;
plug_in_class->create_procedure = gif_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
1999-03-20 19:50:30 +00:00
static void
gif_init (Gif *gif)
1999-03-20 19:50:30 +00:00
{
}
1999-03-20 19:50:30 +00:00
static GList *
gif_query_procedures (GimpPlugIn *plug_in)
1999-03-20 19:50:30 +00:00
{
GList *list = NULL;
1999-03-20 19:50:30 +00:00
list = g_list_append (list, g_strdup (LOAD_THUMB_PROC));
list = g_list_append (list, g_strdup (LOAD_PROC));
2012-09-15 22:08:20 +02:00
return list;
}
static GimpProcedure *
gif_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
1999-03-20 19:50:30 +00:00
if (! strcmp (name, LOAD_PROC))
1999-03-20 19:50:30 +00:00
{
procedure = gimp_load_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
gif_load, NULL, NULL);
gimp_procedure_set_menu_label (procedure, _("GIF image"));
gimp_procedure_set_documentation (procedure,
"Loads files of Compuserve GIF "
"file format", NULL,
name);
gimp_procedure_set_attribution (procedure,
"Spencer Kimball, Peter Mattis, "
"Adam Moss, David Koblas",
"Spencer Kimball, Peter Mattis, "
"Adam Moss, David Koblas",
"1995-2006");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
"image/gif");
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"gif");
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
"0,string,GIF8");
gimp_load_procedure_set_thumbnail_loader (GIMP_LOAD_PROCEDURE (procedure),
LOAD_THUMB_PROC);
}
else if (! strcmp (name, LOAD_THUMB_PROC))
{
procedure = gimp_thumbnail_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
gif_load_thumb, NULL, NULL);
gimp_procedure_set_documentation (procedure,
"Loads only the first frame of a "
"GIF image, to be "
"used as a thumbnail",
"",
name);
gimp_procedure_set_attribution (procedure,
"Sven Neumann",
"Sven Neumann",
"2006");
}
1999-03-20 19:50:30 +00:00
return procedure;
}
static GimpValueArray *
gif_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpValueArray *return_vals;
GimpImage *image;
GError *error = NULL;
gegl_init (NULL, NULL);
image = load_image (file, FALSE, &error);
if (! image)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
/* The GIF format only tells you how many bits per pixel are in the
* image, not the actual number of used indices (D'OH!)
*
* So if we're not careful, repeated load/save of a transparent GIF
* without intermediate indexed->RGB->indexed pumps up the number
* of bits used, as we add an index each time for the transparent
* color. Ouch. We either do some heavier analysis at save-time,
* or trim down the number of GIMP colors at load-time. We do the
* latter for now.
*/
#ifdef GIFDEBUG
g_print ("GIF: Highest used index is %d\n", highest_used_index);
#endif
configure.in po-plug-ins/POTFILES.in plug-ins/common/Makefile.am 2000-01-25 Michael Natterer <mitch@gimp.org> * configure.in * po-plug-ins/POTFILES.in * plug-ins/common/Makefile.am * plug-ins/common/plugin-defs.pl * plug-ins/megawidget/*: removed. (There were only 3 functions left which were used by ~5 plugins, so I moved the resp. functions to the plugins). More preview stuff to come... * app/airbrush_blob.c * modules/colorsel_triangle.c * modules/colorsel_water.c: use G_PI instead of M_PI. * app/procedural_db.h * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c * tools/pdbgen/enums.pl: new PDB return value STATUS_CANCEL which indicates that "Cancel" was pressed in a plugin dialog. (Useful only for file load/save plugins). * app/fileops.[ch] * app/menus.c: changes to handle STATUS_CANCEL correctly. Did some code cleanup in fileops.[ch]. Pop up a warning if File->Save failed. * app/plug_in.c: return_val[0] is of type PDB_STATUS, not PDB_INT32. * libgimp/gimpmath.h: new constant G_MAXRAND which equals to RAND_MAX if it exists or to G_MAXINT otherwise. * libgimp/gimpwidgets.[ch]: new function gimp_random_seed_new() which creates a spinbutton and a "Time" toggle. Call the function which does the "set_sensitive" magic from the radio button callback. * plug-ins/[75 plugins]: - Return STATUS_CANCEL in all file load/save dialogs if "Cancel" was pressed. - Standardized the file plugins' "run" functions. - Use G_PI and G_MAXRAND everywhere. - Added tons of scales and spinbuttons instead of text entries. - Applied uniform packing/spacings all over the place. - Reorganized some UIs (stuff like moving the preview to the top left corner of the dialog). - Removed many ui helper functions and callbacks and use the stuff from libgimp instead. - I tried not to restrict the range of possible values when I replaced entries with spinbuttons/scales but may have failed, though in some cases. Please test ;-) - #include <libgimp/gimpmath.h> where appropriate and use it's constants. - Indentation, s/int/gint/ et.al., code cleanup. RFC: The plugins are definitely not useable with GIMP 1.0 any more, so shouldn't we remove all the remaining compatibility stuff ??? (like "#ifdef GIMP_HAVE_PARASITES")
2000-01-25 17:46:56 +00:00
if (! promote_to_rgb)
gimp_palette_set_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"),
gimp_cmap, highest_used_index * 3);
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
*flags = GIMP_METADATA_LOAD_ALL;
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static GimpValueArray *
gif_load_thumb (GimpProcedure *procedure,
GFile *file,
gint size,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpValueArray *return_vals;
GimpImage *image;
GError *error = NULL;
gegl_init (NULL, NULL);
image = load_image (file, TRUE, &error);
if (! image)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
if (! promote_to_rgb)
gimp_palette_set_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"), gimp_cmap, highest_used_index * 3);
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
GIMP_VALUES_SET_INT (return_vals, 2, gimp_image_get_width (image));
GIMP_VALUES_SET_INT (return_vals, 3, gimp_image_get_height (image));
gimp_value_array_truncate (return_vals, 4);
return return_vals;
1999-03-20 19:50:30 +00:00
}
#define MAXCOLORMAPSIZE 256
/* Max size of data buffer. Count is one byte so maximum read is 255. In
* GetCode we use 2 extra bytes, so let's set it to 260 to be safe. */
#define MAXDATABLOCKSIZE 260
1999-03-20 19:50:30 +00:00
#define CM_RED 0
#define CM_GREEN 1
#define CM_BLUE 2
#define MAX_LZW_BITS 12
1999-03-20 19:50:30 +00:00
#define INTERLACE 0x40
#define LOCALCOLORMAP 0x80
#define BitSet(byte, bit) (((byte) & (bit)) == (bit))
#define gif_message(...) (g_message("%s. %s", __VA_ARGS__))
1999-03-20 19:50:30 +00:00
#define ReadOK(file,buffer,len) (fread(buffer, len, 1, file) != 0)
#define LM_to_uint(a,b) (((b)<<8)|(a))
#define GRAYSCALE 1
#define COLOR 2
typedef guchar CMap[3][MAXCOLORMAPSIZE];
1999-03-20 19:50:30 +00:00
static struct
{
guint Width;
guint Height;
CMap ColorMap;
guint BitPixel;
guint ColorResolution;
guint Background;
guint AspectRatio;
1999-03-20 19:50:30 +00:00
/*
**
*/
gint GrayScale;
1999-03-20 19:50:30 +00:00
} GifScreen;
static struct
{
gint transparent;
gint delayTime;
gint inputFlag;
gint disposal;
gint num_loops;
} Gif89 = { -1, -1, -1, 0, -1 };
1999-03-20 19:50:30 +00:00
static void read_error (const gchar *error_type,
GimpImage *image,
GError **error);
static gboolean ReadColorMap (FILE *fd,
gint number,
CMap buffer,
gint *format);
static gint DoExtension (FILE *fd,
gint label);
static gint GetDataBlock (FILE *fd,
guchar *buf);
static gint GetCode (FILE *fd,
gint code_size,
gboolean flag);
static gint LZWReadByte (FILE *fd,
gint just_reset_LZW,
gint input_code_size);
2014-09-26 09:01:47 +05:30
static gboolean ReadImage (FILE *fd,
GFile *file,
gint len,
gint height,
CMap cmap,
gint ncols,
gint format,
gint interlace,
gint number,
guint leftpos,
guint toppos,
guint screenwidth,
guint screenheight,
GimpImage **image,
GError **error);
1999-03-20 19:50:30 +00:00
static GimpImage *
load_image (GFile *file,
gboolean thumbnail,
GError **error)
1999-03-20 19:50:30 +00:00
{
FILE *fd;
guchar buf[16];
guchar c;
CMap localColorMap;
gint grayScale;
gboolean useGlobalColormap;
gint bitPixel;
gint imageCount = 0;
GimpImage *image = NULL;
gboolean status;
gboolean saved_parasite = FALSE;
1999-03-20 19:50:30 +00:00
gimp_progress_init_printf (_("Opening '%s'"),
gimp_file_get_utf8_name (file));
fd = g_fopen (g_file_peek_path (file), "rb");
if (! fd)
1999-03-20 19:50:30 +00:00
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for reading: %s"),
gimp_file_get_utf8_name (file), g_strerror (errno));
return NULL;
1999-03-20 19:50:30 +00:00
}
if (! ReadOK (fd, buf, 6))
1999-03-20 19:50:30 +00:00
{
read_error (_("GIF magic code"), NULL, error);
fclose (fd);
return NULL;
1999-03-20 19:50:30 +00:00
}
if (strncmp ((gchar *) buf, "GIF", 3) != 0)
1999-03-20 19:50:30 +00:00
{
g_set_error (error, GIMP_PLUG_IN_ERROR, 0,
_("This is not a GIF file: incorrect magic code"));
fclose (fd);
return NULL;
1999-03-20 19:50:30 +00:00
}
2021-10-16 16:37:33 +03:00
if ((strncmp ((gchar *) buf + 3, "87a", 3) != 0) && (strncmp ((gchar *) buf + 3, "89a", 3) != 0))
1999-03-20 19:50:30 +00:00
{
g_set_error (error, GIMP_PLUG_IN_ERROR, 0,
_("Incorrect GIF version: not '87a' or '89a'"));
fclose (fd);
return NULL;
1999-03-20 19:50:30 +00:00
}
if (! ReadOK (fd, buf, 7))
1999-03-20 19:50:30 +00:00
{
read_error (_("screen descriptor"), NULL, error);
fclose (fd);
return NULL;
1999-03-20 19:50:30 +00:00
}
GifScreen.Width = LM_to_uint (buf[0], buf[1]);
GifScreen.Height = LM_to_uint (buf[2], buf[3]);
GifScreen.BitPixel = 2 << (buf[4] & 0x07);
1999-03-20 19:50:30 +00:00
GifScreen.ColorResolution = (((buf[4] & 0x70) >> 3) + 1);
GifScreen.Background = buf[5];
GifScreen.AspectRatio = buf[6];
1999-03-20 19:50:30 +00:00
if (BitSet (buf[4], LOCALCOLORMAP))
{
/* Global Colormap */
if (! ReadColorMap (fd, GifScreen.BitPixel, GifScreen.ColorMap,
&GifScreen.GrayScale))
{
read_error (_("global colormap"), NULL, error);
fclose (fd);
return NULL;
}
1999-03-20 19:50:30 +00:00
}
highest_used_index = GifScreen.BitPixel;
plug-ins/FractalExplorer/Dialogs.c 2003-11-15 Michael Natterer <mitch@gimp.org> * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/FractalExplorer.c * plug-ins/bmp/bmpread.c * plug-ins/bmp/bmpwrite.c * plug-ins/common/CEL.c * plug-ins/common/CML_explorer.c * plug-ins/common/animoptimize.c * plug-ins/common/bz2.c * plug-ins/common/convmatrix.c * plug-ins/common/curve_bend.c * plug-ins/common/dicom.c * plug-ins/common/gauss_iir.c * plug-ins/common/gauss_rle.c * plug-ins/common/gbr.c * plug-ins/common/gif.c * plug-ins/common/gifload.c * plug-ins/common/gih.c * plug-ins/common/grid.c * plug-ins/common/gtm.c * plug-ins/common/gz.c * plug-ins/common/hrz.c * plug-ins/common/jpeg.c * plug-ins/common/mail.c * plug-ins/common/mapcolor.c * plug-ins/common/pat.c * plug-ins/common/pcx.c * plug-ins/common/pix.c * plug-ins/common/png.c * plug-ins/common/pnm.c * plug-ins/common/ps.c * plug-ins/common/psd.c * plug-ins/common/psd_save.c * plug-ins/common/psp.c * plug-ins/common/sel_gauss.c * plug-ins/common/spheredesigner.c * plug-ins/common/sunras.c * plug-ins/common/svg.c * plug-ins/common/tga.c * plug-ins/common/tiff.c * plug-ins/common/wmf.c * plug-ins/common/xbm.c * plug-ins/common/xwd.c * plug-ins/faxg3/faxg3.c * plug-ins/fits/fits.c * plug-ins/flame/flame.c * plug-ins/gfig/gfig.c * plug-ins/gflare/gflare.c * plug-ins/gfli/gfli.c * plug-ins/gimpressionist/brush.c * plug-ins/gimpressionist/ppmtool.c * plug-ins/helpbrowser/domain.c * plug-ins/ifscompose/ifscompose.c * plug-ins/sgi/sgi.c * plug-ins/twain/twain.c * plug-ins/winsnap/winsnap.c * plug-ins/xjt/xjt.c: removed explicit newlines from messages. Made file open/save messages the same all over the place. Reduced number of translatable strings by adding some more "standard" messages. Removed plug-in names from messages. Added some random mnemonics. Unmarked some strings for translation and added some that were forgotten. General message cleanup. Removed trailing whitespace.
2003-11-15 13:53:33 +00:00
while (TRUE)
1999-03-20 19:50:30 +00:00
{
if (! ReadOK (fd, &c, 1))
{
read_error (_("image data"), image, error);
fclose (fd);
return image; /* will be NULL if failed on first image! */
}
1999-03-20 19:50:30 +00:00
if (c == ';')
{
/* GIF terminator */
fclose (fd);
return image;
}
1999-03-20 19:50:30 +00:00
if (c == '!')
{
/* Extension */
if (! ReadOK (fd, &c, 1))
{
read_error (_("extension data"), image, error);
fclose (fd);
return image; /* will be NULL if failed on first image! */
}
DoExtension (fd, c);
continue;
}
1999-03-20 19:50:30 +00:00
if (c != ',')
{
/* Not a valid start character */
g_printerr ("GIF: bogus character 0x%02x, ignoring.\n", (int) c);
continue;
}
1999-03-20 19:50:30 +00:00
++imageCount;
if (! ReadOK (fd, buf, 9))
{
read_error (_("frame info"), image, error);
fclose (fd);
return image; /* will be NULL if failed on first image! */
}
1999-03-20 19:50:30 +00:00
useGlobalColormap = !BitSet (buf[8], LOCALCOLORMAP);
bitPixel = 1 << ((buf[8] & 0x07) + 1);
if (! useGlobalColormap)
{
if (! ReadColorMap (fd, bitPixel, localColorMap, &grayScale))
{
read_error (_("local colormap"), image, error);
fclose (fd);
return image; /* will be NULL if failed on first image! */
}
highest_used_index = bitPixel;
status = ReadImage (fd, file, LM_to_uint (buf[4], buf[5]),
LM_to_uint (buf[6], buf[7]),
localColorMap, bitPixel,
grayScale,
BitSet (buf[8], INTERLACE), imageCount,
(guint) LM_to_uint (buf[0], buf[1]),
(guint) LM_to_uint (buf[2], buf[3]),
GifScreen.Width,
GifScreen.Height,
&image, error);
}
1999-03-20 19:50:30 +00:00
else
{
status = ReadImage (fd, file, LM_to_uint (buf[4], buf[5]),
LM_to_uint (buf[6], buf[7]),
GifScreen.ColorMap, GifScreen.BitPixel,
GifScreen.GrayScale,
BitSet (buf[8], INTERLACE), imageCount,
(guint) LM_to_uint (buf[0], buf[1]),
(guint) LM_to_uint (buf[2], buf[3]),
GifScreen.Width,
GifScreen.Height,
&image, error);
}
1999-03-20 19:50:30 +00:00
if (!status)
break;
if (GifScreen.AspectRatio != 0 && GifScreen.AspectRatio != 49)
{
/* Byte overflow is allowed per the GIF specification */
guint8 par = GifScreen.AspectRatio + 15;
if (par > 0)
{
gdouble image_xres;
gdouble image_yres;
gfloat ratio = par / 64.0;
g_message (_("Non-square pixels. Image might look squashed if "
"Dot for Dot mode is enabled."));
gimp_image_get_resolution (image, &image_xres, &image_yres);
if (ratio < 1)
image_xres = image_yres * (1 / ratio);
else
image_yres = image_xres * ratio;
gimp_image_set_resolution (image, image_xres, image_yres);
}
}
1999-03-20 19:50:30 +00:00
if (comment_parasite != NULL)
{
if (! thumbnail)
gimp_image_attach_parasite (image, comment_parasite);
gimp_parasite_free (comment_parasite);
comment_parasite = NULL;
}
1999-03-20 19:50:30 +00:00
/* If we are loading a thumbnail, we stop after the first frame. */
if (thumbnail)
break;
/* If there is more than one frame, we add a parasite so that
* we know to export as an animation on overwrite */
if (Gif89.num_loops > -1 && ! saved_parasite)
{
GimpParasite *parasite;
gchar *str;
str = g_strdup_printf ("%d", Gif89.num_loops);
parasite = gimp_parasite_new ("gif/animated",
GIMP_PARASITE_PERSISTENT,
strlen (str) + 1,
(gpointer) str);
g_free (str);
gimp_image_attach_parasite (image, parasite);
gimp_parasite_free (parasite);
saved_parasite = TRUE;
}
1999-03-20 19:50:30 +00:00
}
fclose (fd);
return image;
1999-03-20 19:50:30 +00:00
}
static void
read_error (const gchar *error_type, GimpImage *image, GError **error)
{
if (! image)
{
g_set_error (error, GIMP_PLUG_IN_ERROR, 0,
_("Error reading %s. %s"),
error_type, _("Image may be corrupt!"));
}
else
{
g_message (_("Error reading %s. %s"),
error_type, _("Image may be corrupt!"));
}
}
static gboolean
1999-03-20 19:50:30 +00:00
ReadColorMap (FILE *fd,
gint number,
CMap buffer,
gint *format)
1999-03-20 19:50:30 +00:00
{
guchar rgb[3];
gint flag;
gint i;
1999-03-20 19:50:30 +00:00
flag = TRUE;
for (i = 0; i < number; ++i)
{
if (! ReadOK (fd, rgb, sizeof (rgb)))
return FALSE;
1999-03-20 19:50:30 +00:00
buffer[CM_RED][i] = rgb[0];
buffer[CM_GREEN][i] = rgb[1];
buffer[CM_BLUE][i] = rgb[2];
flag &= (rgb[0] == rgb[1] && rgb[1] == rgb[2]);
}
*format = (flag) ? GRAYSCALE : COLOR;
return TRUE;
1999-03-20 19:50:30 +00:00
}
static gint
1999-03-20 19:50:30 +00:00
DoExtension (FILE *fd,
gint label)
1999-03-20 19:50:30 +00:00
{
static guchar buf[MAXDATABLOCKSIZE];
gint bufsize;
#ifdef GIFDEBUG
gchar *str;
#endif
1999-03-20 19:50:30 +00:00
switch (label)
{
case 0x01: /* Plain Text Extension */
#ifdef GIFDEBUG
1999-03-20 19:50:30 +00:00
str = "Plain Text Extension";
#endif
1999-03-20 19:50:30 +00:00
#ifdef notdef
if (GetDataBlock (fd, (guchar *) buf) == 0)
;
1999-03-20 19:50:30 +00:00
lpos = LM_to_uint (buf[0], buf[1]);
tpos = LM_to_uint (buf[2], buf[3]);
width = LM_to_uint (buf[4], buf[5]);
height = LM_to_uint (buf[6], buf[7]);
cellw = buf[8];
cellh = buf[9];
1999-03-20 19:50:30 +00:00
foreground = buf[10];
background = buf[11];
while (GetDataBlock (fd, (guchar *) buf) > 0)
{
PPM_ASSIGN (image[ypos][xpos],
cmap[CM_RED][v],
cmap[CM_GREEN][v],
cmap[CM_BLUE][v]);
++index;
}
1999-03-20 19:50:30 +00:00
return FALSE;
#else
break;
#endif
case 0xff: /* Application Extension */
#ifdef GIFDEBUG
1999-03-20 19:50:30 +00:00
str = "Application Extension";
#endif
/* Animation block */
if (GetDataBlock (fd, (guchar *) buf))
{
if (strncmp ((const gchar *) buf, "NETSCAPE2.0", 8) == 0)
{
if (GetDataBlock (fd, (guchar *) buf))
Gif89.num_loops = (buf[0] << 8) | buf[1];
}
}
1999-03-20 19:50:30 +00:00
break;
case 0xfe: /* Comment Extension */
#ifdef GIFDEBUG
1999-03-20 19:50:30 +00:00
str = "Comment Extension";
#endif
while ((bufsize = GetDataBlock (fd, (guchar *) buf)) > 0)
{
gchar *comment = (gchar *) buf;
/* Make sure we end with a NULL */
comment[bufsize] = '\0';
if (! g_utf8_validate (comment, -1, NULL))
continue;
if (comment_parasite)
gimp_parasite_free (comment_parasite);
plug-ins/FractalExplorer/Dialogs.c 2003-11-15 Michael Natterer <mitch@gimp.org> * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/FractalExplorer.c * plug-ins/bmp/bmpread.c * plug-ins/bmp/bmpwrite.c * plug-ins/common/CEL.c * plug-ins/common/CML_explorer.c * plug-ins/common/animoptimize.c * plug-ins/common/bz2.c * plug-ins/common/convmatrix.c * plug-ins/common/curve_bend.c * plug-ins/common/dicom.c * plug-ins/common/gauss_iir.c * plug-ins/common/gauss_rle.c * plug-ins/common/gbr.c * plug-ins/common/gif.c * plug-ins/common/gifload.c * plug-ins/common/gih.c * plug-ins/common/grid.c * plug-ins/common/gtm.c * plug-ins/common/gz.c * plug-ins/common/hrz.c * plug-ins/common/jpeg.c * plug-ins/common/mail.c * plug-ins/common/mapcolor.c * plug-ins/common/pat.c * plug-ins/common/pcx.c * plug-ins/common/pix.c * plug-ins/common/png.c * plug-ins/common/pnm.c * plug-ins/common/ps.c * plug-ins/common/psd.c * plug-ins/common/psd_save.c * plug-ins/common/psp.c * plug-ins/common/sel_gauss.c * plug-ins/common/spheredesigner.c * plug-ins/common/sunras.c * plug-ins/common/svg.c * plug-ins/common/tga.c * plug-ins/common/tiff.c * plug-ins/common/wmf.c * plug-ins/common/xbm.c * plug-ins/common/xwd.c * plug-ins/faxg3/faxg3.c * plug-ins/fits/fits.c * plug-ins/flame/flame.c * plug-ins/gfig/gfig.c * plug-ins/gflare/gflare.c * plug-ins/gfli/gfli.c * plug-ins/gimpressionist/brush.c * plug-ins/gimpressionist/ppmtool.c * plug-ins/helpbrowser/domain.c * plug-ins/ifscompose/ifscompose.c * plug-ins/sgi/sgi.c * plug-ins/twain/twain.c * plug-ins/winsnap/winsnap.c * plug-ins/xjt/xjt.c: removed explicit newlines from messages. Made file open/save messages the same all over the place. Reduced number of translatable strings by adding some more "standard" messages. Removed plug-in names from messages. Added some random mnemonics. Unmarked some strings for translation and added some that were forgotten. General message cleanup. Removed trailing whitespace.
2003-11-15 13:53:33 +00:00
comment_parasite = gimp_parasite_new ("gimp-comment",
GIMP_PARASITE_PERSISTENT,
strlen (comment) + 1, comment);
}
1999-03-20 19:50:30 +00:00
return TRUE;
break;
case 0xf9: /* Graphic Control Extension */
#ifdef GIFDEBUG
1999-03-20 19:50:30 +00:00
str = "Graphic Control Extension";
#endif
(void) GetDataBlock (fd, (guchar *) buf);
Gif89.disposal = (buf[0] >> 2) & 0x7;
1999-03-20 19:50:30 +00:00
Gif89.inputFlag = (buf[0] >> 1) & 0x1;
Gif89.delayTime = LM_to_uint (buf[1], buf[2]);
if ((buf[0] & 0x1) != 0)
Gif89.transparent = buf[3];
1999-03-20 19:50:30 +00:00
else
Gif89.transparent = -1;
1999-03-20 19:50:30 +00:00
while (GetDataBlock (fd, (guchar *) buf) > 0);
1999-03-20 19:50:30 +00:00
return FALSE;
break;
1999-03-20 19:50:30 +00:00
default:
#ifdef GIFDEBUG
str = (gchar *)buf;
#endif
sprintf ((gchar *)buf, "UNKNOWN (0x%02x)", label);
1999-03-20 19:50:30 +00:00
break;
}
#ifdef GIFDEBUG
g_print ("GIF: got a '%s'\n", str);
#endif
while (GetDataBlock (fd, (guchar *) buf) > 0);
1999-03-20 19:50:30 +00:00
return FALSE;
}
static gint ZeroDataBlock = FALSE;
1999-03-20 19:50:30 +00:00
static gint
GetDataBlock (FILE *fd,
guchar *buf)
1999-03-20 19:50:30 +00:00
{
guchar count;
1999-03-20 19:50:30 +00:00
if (! ReadOK (fd, &count, 1))
1999-03-20 19:50:30 +00:00
{
gif_message (_("Error reading data size"), _("Image may be corrupt!"));
1999-03-20 19:50:30 +00:00
return -1;
}
ZeroDataBlock = (count == 0);
1999-03-20 19:50:30 +00:00
if ((count != 0) && (! ReadOK (fd, buf, count)))
1999-03-20 19:50:30 +00:00
{
gif_message (_("Error reading data"), _("Image may be corrupt!"));
1999-03-20 19:50:30 +00:00
return -1;
}
return count;
}
static gint
GetCode (FILE *fd,
gint code_size,
gboolean flag)
1999-03-20 19:50:30 +00:00
{
static guchar buf[MAXDATABLOCKSIZE];
static gint curbit, lastbit, done, last_byte;
gint i, j, ret, count;
1999-03-20 19:50:30 +00:00
if (flag)
{
curbit = 0;
lastbit = 0;
done = FALSE;
1999-11-11 21:53:40 +00:00
last_byte = 2;
1999-03-20 19:50:30 +00:00
return 0;
}
while ((curbit + code_size) > lastbit)
1999-03-20 19:50:30 +00:00
{
if (done)
{
if (curbit >= lastbit)
{
gif_message (_("Not enough compressed data"), _("Image may be corrupt!"));
return -1;
}
return -2;
}
1999-03-20 19:50:30 +00:00
buf[0] = buf[last_byte - 2];
buf[1] = buf[last_byte - 1];
count = GetDataBlock (fd, &buf[2]);
if (count < 0)
{
return -1;
}
else if (count == 0)
{
done = TRUE;
}
1999-03-20 19:50:30 +00:00
last_byte = 2 + count;
curbit = (curbit - lastbit) + 16;
lastbit = (2 + count) * 8;
}
ret = 0;
for (i = curbit, j = 0; j < code_size; ++i, ++j)
ret |= ((buf[i / 8] & (1 << (i % 8))) != 0) << j;
curbit += code_size;
return ret;
}
static gint
LZWReadByte (FILE *fd,
gint just_reset_LZW,
gint input_code_size)
1999-03-20 19:50:30 +00:00
{
static gint fresh = FALSE;
gint code, incode;
static gint code_size, set_code_size;
static gint max_code, max_code_size;
static gint firstcode, oldcode;
static gint clear_code, end_code;
static gint table[2][(1 << MAX_LZW_BITS)];
#define STACK_SIZE ((1 << (MAX_LZW_BITS)) * 2)
static gint stack[STACK_SIZE], *sp;
gint i;
1999-03-20 19:50:30 +00:00
if (just_reset_LZW)
1999-03-20 19:50:30 +00:00
{
if (input_code_size > MAX_LZW_BITS || input_code_size <= 1)
{
gif_message (_("Value out of range for code size"), _("Image may be corrupt!"));
return -1;
}
1999-03-20 19:50:30 +00:00
set_code_size = input_code_size;
code_size = set_code_size + 1;
clear_code = 1 << set_code_size;
end_code = clear_code + 1;
1999-03-20 19:50:30 +00:00
max_code_size = 2 * clear_code;
max_code = clear_code + 2;
1999-03-20 19:50:30 +00:00
if ((code = GetCode (fd, 0, TRUE)) < 0)
{
return code;
}
1999-03-20 19:50:30 +00:00
fresh = TRUE;
sp = stack;
1999-03-20 19:50:30 +00:00
for (i = 0; i < clear_code; ++i)
{
table[0][i] = 0;
table[1][i] = i;
}
for (; i < (1 << MAX_LZW_BITS); ++i)
{
table[0][i] = 0;
table[1][i] = 0;
}
1999-03-20 19:50:30 +00:00
return 0;
}
else if (fresh)
{
fresh = FALSE;
do
{
firstcode = oldcode = GetCode (fd, code_size, FALSE);
}
1999-03-20 19:50:30 +00:00
while (firstcode == clear_code);
if (firstcode < 0)
{
return firstcode;
}
return firstcode & 255;
1999-03-20 19:50:30 +00:00
}
if (sp > stack)
return (*--sp) & 255;
1999-03-20 19:50:30 +00:00
while ((code = GetCode (fd, code_size, FALSE)) >= 0)
{
if (code == clear_code)
{
for (i = 0; i < clear_code; ++i)
{
table[0][i] = 0;
table[1][i] = i;
}
for (; i < (1 << MAX_LZW_BITS); ++i)
{
table[0][i] = 0;
table[1][i] = 0;
}
code_size = set_code_size + 1;
max_code_size = 2 * clear_code;
max_code = clear_code + 2;
sp = stack;
firstcode = oldcode = GetCode (fd, code_size, FALSE);
if (firstcode < 0)
{
return firstcode;
}
return firstcode & 255;
}
else if (code == end_code || code > max_code)
{
gint count;
guchar buf[MAXDATABLOCKSIZE];
gboolean extra_data = FALSE;
1999-03-20 19:50:30 +00:00
if (code != end_code)
{
gif_message (_("Invalid code, expecting end of data marker"),
_("Image may be corrupt!"));
code = -1;
}
else
{
code = -2;
}
if (ZeroDataBlock)
{
return code;
}
1999-03-20 19:50:30 +00:00
while ((count = GetDataBlock (fd, buf)) > 0)
{
extra_data = TRUE;
}
1999-03-20 19:50:30 +00:00
if (count != 0 || extra_data)
{
gif_message (_("Unexpected extra data"), _("Image may be corrupt!"));
}
return -1;
}
1999-03-20 19:50:30 +00:00
incode = code;
if (code == max_code)
{
if (sp < &(stack[STACK_SIZE]))
*sp++ = firstcode;
code = oldcode;
}
1999-03-20 19:50:30 +00:00
while (code >= clear_code && sp < &(stack[STACK_SIZE]))
{
if (code >= (1 << MAX_LZW_BITS))
{
gif_message (_("Invalid table entry"), _("Image is corrupt!"));
gif: perform bounds checking for table entries table is specified to have 2 x (1<<12) entries, this in turn is because table entries are specified to be smaller than (1<<12). Therefore we need to check that each entry is within the specified range. Without this check, it's possible to induce a buffer overflow read, see ASAN output below. However the value that we read is eventually clamped to a safe range before being returned, hence this overflow seems pretty useless: ==10977==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000010a21c0 at pc 0x00000056187b bp 0x7ffec915c550 sp 0x7ffec915c548 READ of size 4 at 0x0000010a21c0 thread T0 #0 0x56187a in LZWReadByte /home/ahunt/git/gimp/plug-ins/common/file-gif-load.c:913:19 #1 0x55fc4d in ReadImage /home/ahunt/git/gimp/plug-ins/common/file-gif-load.c:1183:15 #2 0x55e447 in load_image /home/ahunt/git/gimp/plug-ins/common/file-gif-load.c #3 0x561ef0 in LLVMFuzzerTestOneInput /home/ahunt/git/gimp/plug-ins/common/file-gif-load_fuzzer.c:79:17 #4 0x460e44 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:599:15 #5 0x46034a in fuzzer::Fuzzer::RunOne(unsigned char const*, unsigned long, bool, fuzzer::InputInfo*, bool, bool*) /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:505:3 #6 0x462067 in fuzzer::Fuzzer::MutateAndTestOne() /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:745:19 #7 0x462bf5 in fuzzer::Fuzzer::Loop(std::__Fuzzer::vector<fuzzer::SizedFile, fuzzer::fuzzer_allocator<fuzzer::SizedFile> >&) /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:883:5 #8 0x450ea6 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:906:6 #9 0x47ae82 in main /home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10 #10 0x7f307beb4349 in __libc_start_main (/lib64/libc.so.6+0x24349) #11 0x424259 in _start /home/abuild/rpmbuild/BUILD/glibc-2.26/csu/../sysdeps/x86_64/start.S:120 0x0000010a21c0 is located 1088 bytes to the right of global variable 'table' defined in 'file-gif-load.c:794:15' (0x1099d80) of size 32768 SUMMARY: AddressSanitizer: global-buffer-overflow /home/ahunt/git/gimp/plug-ins/common/file-gif-load.c:913:19 in LZWReadByte Shadow bytes around the buggy address: 0x00008020c3e0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c3f0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c400: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c410: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c420: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 =>0x00008020c430: f9 f9 f9 f9 f9 f9 f9 f9[f9]f9 f9 f9 f9 f9 f9 f9 0x00008020c440: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c450: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c460: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c470: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 0x00008020c480: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==10977==ABORTING ( crash-911d5feb07551dd0313d204db4883838139820f5 )
2021-09-10 19:15:17 +02:00
return -1;
}
*sp++ = table[1][code];
if (code == table[0][code])
{
gif_message (_("Circular table entry"), _("Image is corrupt!"));
return -1;
}
code = table[0][code];
}
1999-03-20 19:50:30 +00:00
if (sp < &(stack[STACK_SIZE]))
*sp++ = firstcode = table[1][code];
1999-03-20 19:50:30 +00:00
if ((code = max_code) < (1 << MAX_LZW_BITS))
{
table[0][code] = oldcode;
table[1][code] = firstcode;
++max_code;
if ((max_code >= max_code_size) &&
(max_code_size < (1 << MAX_LZW_BITS)))
{
max_code_size *= 2;
++code_size;
}
}
1999-03-20 19:50:30 +00:00
oldcode = incode;
if (sp > stack)
return (*--sp) & 255;
1999-03-20 19:50:30 +00:00
}
if (code < 0)
{
return code;
}
return code & 255;
1999-03-20 19:50:30 +00:00
}
static gboolean
ReadImage (FILE *fd,
GFile *file,
gint len,
gint height,
CMap cmap,
gint ncols,
gint format,
gint interlace,
gint number,
guint leftpos,
guint toppos,
guint screenwidth,
guint screenheight,
GimpImage **image,
GError **error)
1999-03-20 19:50:30 +00:00
{
static gint frame_number = 1;
1999-03-20 19:50:30 +00:00
GimpLayer *layer;
2012-09-15 22:08:20 +02:00
GeglBuffer *buffer;
guchar *dest, *temp;
guchar c;
gint xpos = 0, ypos = 0, pass = 0;
gint cur_progress, max_progress;
gint v;
gint i, j;
gchar *framename;
gchar *framename_ptr;
gboolean alpha_frame = FALSE;
static gint previous_disposal;
1999-03-20 19:50:30 +00:00
/* Guard against bogus frame size */
if (len < 1 || height < 1 ||
leftpos + len > screenwidth || toppos + height > screenheight)
{
if (frame_number == 1)
{
g_set_error (error, GIMP_PLUG_IN_ERROR, 0,
_("%s. %s"),
_("Invalid frame dimensions"), _("Image may be corrupt!"));
}
else
{
gif_message (_("Invalid frame dimensions"), _("Image may be corrupt!"));
}
return FALSE;
}
1999-03-20 19:50:30 +00:00
/*
** Initialize the Compression routines
*/
if (! ReadOK (fd, &c, 1))
1999-03-20 19:50:30 +00:00
{
read_error (_("image data"), *image, error);
return FALSE;
1999-03-20 19:50:30 +00:00
}
if (LZWReadByte (fd, TRUE, c) < 0)
1999-03-20 19:50:30 +00:00
{
read_error (_("compressed image data"), *image, error);
return FALSE;
1999-03-20 19:50:30 +00:00
}
if (frame_number == 1)
1999-03-20 19:50:30 +00:00
{
/* Guard against bogus logical screen size values */
if (screenwidth == 0)
{
g_printerr ("Invalid zero screenwidth changed to %d", len);
screenwidth = len;
}
if (screenheight == 0)
{
g_printerr ("Invalid zero screenheight changed to %d", height);
screenheight = height;
}
*image = gimp_image_new (screenwidth, screenheight, GIMP_INDEXED);
1999-03-20 19:50:30 +00:00
for (i = 0, j = 0; i < ncols; i++)
{
used_cmap[0][i] = gimp_cmap[j++] = cmap[0][i];
used_cmap[1][i] = gimp_cmap[j++] = cmap[1][i];
used_cmap[2][i] = gimp_cmap[j++] = cmap[2][i];
}
1999-03-20 19:50:30 +00:00
gimp_palette_set_colormap (gimp_image_get_palette (*image), babl_format ("R'G'B' u8"), gimp_cmap, ncols * 3);
1999-03-20 19:50:30 +00:00
if (Gif89.delayTime < 0)
framename = g_strdup (_("Background"));
1999-03-20 19:50:30 +00:00
else
framename = g_strdup_printf (_("Background (%d%s)"),
10 * Gif89.delayTime, "ms");
1999-03-20 19:50:30 +00:00
previous_disposal = Gif89.disposal;
if (Gif89.transparent == -1)
{
layer = gimp_layer_new (*image, framename,
len, height,
GIMP_INDEXED_IMAGE,
100,
gimp_image_get_default_new_layer_mode (*image));
}
1999-03-20 19:50:30 +00:00
else
{
layer = gimp_layer_new (*image, framename,
len, height,
GIMP_INDEXEDA_IMAGE,
100,
gimp_image_get_default_new_layer_mode (*image));
alpha_frame=TRUE;
}
g_free (framename);
1999-03-20 19:50:30 +00:00
}
else /* NOT FIRST FRAME */
{
gimp_progress_set_text_printf (_("Opening '%s' (frame %d)"),
gimp_file_get_utf8_name (file),
frame_number);
gimp_progress_pulse ();
/* If the colormap is now different, we have to promote to RGB! */
if (! promote_to_rgb)
{
for (i = 0; i < ncols; i++)
{
if ((used_cmap[0][i] != cmap[0][i]) ||
(used_cmap[1][i] != cmap[1][i]) ||
(used_cmap[2][i] != cmap[2][i]))
{
/* Everything is RGB(A) from now on... sigh. */
promote_to_rgb = TRUE;
plug-ins/FractalExplorer/Dialogs.c 2003-11-15 Michael Natterer <mitch@gimp.org> * plug-ins/FractalExplorer/Dialogs.c * plug-ins/FractalExplorer/FractalExplorer.c * plug-ins/bmp/bmpread.c * plug-ins/bmp/bmpwrite.c * plug-ins/common/CEL.c * plug-ins/common/CML_explorer.c * plug-ins/common/animoptimize.c * plug-ins/common/bz2.c * plug-ins/common/convmatrix.c * plug-ins/common/curve_bend.c * plug-ins/common/dicom.c * plug-ins/common/gauss_iir.c * plug-ins/common/gauss_rle.c * plug-ins/common/gbr.c * plug-ins/common/gif.c * plug-ins/common/gifload.c * plug-ins/common/gih.c * plug-ins/common/grid.c * plug-ins/common/gtm.c * plug-ins/common/gz.c * plug-ins/common/hrz.c * plug-ins/common/jpeg.c * plug-ins/common/mail.c * plug-ins/common/mapcolor.c * plug-ins/common/pat.c * plug-ins/common/pcx.c * plug-ins/common/pix.c * plug-ins/common/png.c * plug-ins/common/pnm.c * plug-ins/common/ps.c * plug-ins/common/psd.c * plug-ins/common/psd_save.c * plug-ins/common/psp.c * plug-ins/common/sel_gauss.c * plug-ins/common/spheredesigner.c * plug-ins/common/sunras.c * plug-ins/common/svg.c * plug-ins/common/tga.c * plug-ins/common/tiff.c * plug-ins/common/wmf.c * plug-ins/common/xbm.c * plug-ins/common/xwd.c * plug-ins/faxg3/faxg3.c * plug-ins/fits/fits.c * plug-ins/flame/flame.c * plug-ins/gfig/gfig.c * plug-ins/gflare/gflare.c * plug-ins/gfli/gfli.c * plug-ins/gimpressionist/brush.c * plug-ins/gimpressionist/ppmtool.c * plug-ins/helpbrowser/domain.c * plug-ins/ifscompose/ifscompose.c * plug-ins/sgi/sgi.c * plug-ins/twain/twain.c * plug-ins/winsnap/winsnap.c * plug-ins/xjt/xjt.c: removed explicit newlines from messages. Made file open/save messages the same all over the place. Reduced number of translatable strings by adding some more "standard" messages. Removed plug-in names from messages. Added some random mnemonics. Unmarked some strings for translation and added some that were forgotten. General message cleanup. Removed trailing whitespace.
2003-11-15 13:53:33 +00:00
/* Promote everything we have so far into RGB(A) */
1999-03-20 19:50:30 +00:00
#ifdef GIFDEBUG
g_print ("GIF: Promoting image to RGB...\n");
1999-03-20 19:50:30 +00:00
#endif
gimp_image_convert_rgb (*image);
break;
}
}
}
1999-03-20 19:50:30 +00:00
if (Gif89.delayTime < 0)
framename = g_strdup_printf (_("Frame %d"), frame_number);
1999-03-20 19:50:30 +00:00
else
framename = g_strdup_printf (_("Frame %d (%d%s)"),
frame_number, 10 * Gif89.delayTime, "ms");
1999-03-20 19:50:30 +00:00
switch (previous_disposal)
{
case 0x00:
break; /* 'don't care' */
case 0x01:
framename_ptr = framename;
framename = g_strconcat (framename, " (combine)", NULL);
g_free (framename_ptr);
break;
case 0x02:
framename_ptr = framename;
framename = g_strconcat (framename, " (replace)", NULL);
g_free (framename_ptr);
break;
case 0x03: /* Rarely-used, and unhandled by many
loaders/players (including GIMP: we treat as
'combine' mode). */
framename_ptr = framename;
framename = g_strconcat (framename, " (combine) (!)", NULL);
g_free (framename_ptr);
break;
case 0x04: /* I've seen a composite of this type. stvo_online_banner2.gif */
case 0x05:
case 0x06: /* I've seen a composite of this type. bn31.Gif */
case 0x07:
framename_ptr = framename;
framename = g_strconcat (framename, " (unknown disposal)", NULL);
g_free (framename_ptr);
g_message (_("GIF: Undocumented GIF composite type %d is "
"not handled. Animation might not play or "
"re-save perfectly."),
previous_disposal);
break;
default:
g_message (_("Unknown composite type %d. %s"),
previous_disposal, _("Image may be corrupt!"));
break;
}
1999-03-20 19:50:30 +00:00
previous_disposal = Gif89.disposal;
layer = gimp_layer_new (*image, framename,
len, height,
promote_to_rgb ?
GIMP_RGBA_IMAGE : GIMP_INDEXEDA_IMAGE,
100,
gimp_image_get_default_new_layer_mode (*image));
1999-03-20 19:50:30 +00:00
alpha_frame = TRUE;
g_free (framename);
1999-03-20 19:50:30 +00:00
}
frame_number++;
gimp_image_insert_layer (*image, layer, NULL, 0);
gimp_item_transform_translate (GIMP_ITEM (layer), (gint) leftpos, (gint) toppos);
1999-03-20 19:50:30 +00:00
cur_progress = 0;
max_progress = height;
if (len > (G_MAXSIZE / height / (alpha_frame ? (promote_to_rgb ? 4 : 2) : 1)))
{
g_message ("'%s' has a larger image size than GIMP can handle.",
gimp_file_get_utf8_name (file));
gimp_image_delete (*image);
*image = NULL;
return FALSE;
}
#ifdef GIFDEBUG
g_print ("GIF: reading %d by %d%s GIF image, ncols=%d\n",
len, height, interlace ? " interlaced" : "", ncols);
#endif
1999-03-20 19:50:30 +00:00
if (! alpha_frame && promote_to_rgb)
1999-03-20 19:50:30 +00:00
{
/* I don't see how one would easily construct a GIF in which
this could happen, but it's a mad mad world. */
g_set_error (error, GIMP_PLUG_IN_ERROR, 0,
_("Can't handle non-alpha RGB frames.\n"
"Please file a bug report at "
"https://gitlab.gnome.org/GNOME/gimp/issues"));
gimp_image_delete (*image);
*image = NULL;
return FALSE;
1999-03-20 19:50:30 +00:00
}
if (alpha_frame)
dest = (guchar *) g_malloc ((gsize)len * (gsize)height * (promote_to_rgb ? 4 : 2));
else
dest = (guchar *) g_malloc ((gsize)len * (gsize)height);
while ((v = LZWReadByte (fd, FALSE, c)) >= 0)
1999-03-20 19:50:30 +00:00
{
if (alpha_frame)
{
if (promote_to_rgb)
{
temp = dest + ( (ypos * len) + xpos ) * 4;
*(temp ) = (guchar) cmap[0][v];
*(temp+1) = (guchar) cmap[1][v];
*(temp+2) = (guchar) cmap[2][v];
*(temp+3) = (guchar) ((v == Gif89.transparent) ? 0 : 255);
}
else
{
temp = dest + ( (ypos * len) + xpos ) * 2;
*temp = (guchar) v;
*(temp+1) = (guchar) ((v == Gif89.transparent) ? 0 : 255);
}
}
1999-03-20 19:50:30 +00:00
else
{
1999-03-20 19:50:30 +00:00
temp = dest + (ypos * len) + xpos;
*temp = (guchar) v;
}
1999-03-20 19:50:30 +00:00
xpos++;
if (xpos == len)
{
xpos = 0;
if (interlace)
{
switch (pass)
{
case 0:
case 1:
ypos += 8;
break;
case 2:
ypos += 4;
break;
case 3:
ypos += 2;
break;
}
if (ypos >= height)
{
pass++;
switch (pass)
{
case 1:
ypos = 4;
break;
case 2:
ypos = 2;
break;
case 3:
ypos = 1;
break;
default:
goto fini;
}
}
}
else
{
ypos++;
}
1999-03-20 19:50:30 +00:00
if (frame_number == 1)
{
cur_progress++;
if ((cur_progress % 16) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
1999-03-20 19:50:30 +00:00
if (ypos >= height)
break;
1999-03-20 19:50:30 +00:00
}
/* v can be < 0 here, indicating an error or encountering an end_code (-2).
* At this point we have already safely handled any corruption, so we might
* as well load any partial data we can instead of returning false.
*/
fini:
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
2012-09-15 22:08:20 +02:00
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, len, height), 0,
NULL, dest, GEGL_AUTO_ROWSTRIDE);
1999-03-20 19:50:30 +00:00
g_free (dest);
2012-09-15 22:08:20 +02:00
g_object_unref (buffer);
gimp_progress_update (1.0);
1999-03-20 19:50:30 +00:00
{
gboolean first = TRUE;
/* Skip remaining compressed data if any. */
while (LZWReadByte (fd, FALSE, c) >= 0)
{
if (first)
{
g_message (_("Too much compressed data, ignoring extra..."));
first = FALSE;
}
}
}
return TRUE;
1999-03-20 19:50:30 +00:00
}