2006-04-13 19:16:13 +08:00
|
|
|
/* GIF saving file filter for GIMP
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
2003-06-17 01:37:11 +08:00
|
|
|
* Copyright
|
1998-03-16 18:05:41 +08:00
|
|
|
* - Adam D. Moss
|
|
|
|
* - Peter Mattis
|
|
|
|
* - Spencer Kimball
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
1998-03-16 18:05:41 +08:00
|
|
|
* Based around original GIF code by David Koblas.
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
|
|
|
*
|
2003-06-17 01:37:11 +08:00
|
|
|
* Version 4.1.0 - 2003-06-16
|
1998-03-16 18:05:41 +08:00
|
|
|
* Adam D. Moss - <adam@gimp.org> <adam@foxbox.org>
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This filter uses code taken from the "giftopnm" and "ppmtogif" programs
|
|
|
|
* which are part of the "netpbm" package.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* "The Graphics Interchange Format(c) is the Copyright property of
|
|
|
|
* CompuServe Incorporated. GIF(sm) is a Service Mark property of
|
2003-11-06 23:27:05 +08:00
|
|
|
* CompuServe Incorporated."
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2003-05-13 22:59:03 +08:00
|
|
|
/* Copyright notice for GIF code from which this plugin was long ago */
|
|
|
|
/* derived (David Koblas has granted permission to relicense): */
|
|
|
|
/* +-------------------------------------------------------------------+ */
|
|
|
|
/* | Copyright 1990, 1991, 1993, David Koblas. (koblas@extra.com) | */
|
|
|
|
/* +-------------------------------------------------------------------+ */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-05-30 00:35:47 +08:00
|
|
|
#include "config.h"
|
2000-01-07 06:26:10 +08:00
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
#include <errno.h>
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
2000-01-07 06:26:10 +08:00
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
2000-01-07 06:26:10 +08:00
|
|
|
|
1999-05-30 00:35:47 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-05-01 05:03:44 +08:00
|
|
|
|
2005-08-14 06:52:41 +08:00
|
|
|
#define SAVE_PROC "file-gif-save"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-gif-save"
|
2011-04-09 02:31:34 +08:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-gif-save"
|
2005-08-14 06:52:41 +08:00
|
|
|
|
|
|
|
|
1998-10-10 02:01:35 +08:00
|
|
|
/* uncomment the line below for a little debugging info */
|
|
|
|
/* #define GIFDEBUG yesplease */
|
|
|
|
|
|
|
|
|
2010-02-13 19:23:39 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DISPOSE_STORE_VALUE_COLUMN,
|
|
|
|
DISPOSE_STORE_LABEL_COLUMN
|
|
|
|
};
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DISPOSE_UNSPECIFIED,
|
|
|
|
DISPOSE_COMBINE,
|
|
|
|
DISPOSE_REPLACE
|
|
|
|
};
|
1998-10-10 02:01:35 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2005-08-30 02:45:02 +08:00
|
|
|
gint interlace;
|
|
|
|
gint save_comment;
|
|
|
|
gint loop;
|
|
|
|
gint default_delay;
|
|
|
|
gint default_dispose;
|
|
|
|
gboolean always_use_default_delay;
|
|
|
|
gboolean always_use_default_dispose;
|
2010-02-16 05:47:19 +08:00
|
|
|
gboolean as_animation;
|
1997-11-25 06:05:25 +08:00
|
|
|
} GIFSaveVals;
|
|
|
|
|
|
|
|
|
|
|
|
/* Declare some local functions.
|
|
|
|
*/
|
2008-08-18 05:30:34 +08:00
|
|
|
static void query (void);
|
|
|
|
static void run (const gchar *name,
|
2006-04-13 19:16:13 +08:00
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals);
|
2008-08-18 05:30:34 +08:00
|
|
|
|
|
|
|
static gboolean save_image (const gchar *filename,
|
2006-04-13 19:16:13 +08:00
|
|
|
gint32 image_ID,
|
|
|
|
gint32 drawable_ID,
|
2008-08-18 05:30:34 +08:00
|
|
|
gint32 orig_image_ID,
|
|
|
|
GError **error);
|
2003-07-02 19:07:41 +08:00
|
|
|
|
2008-08-18 05:30:34 +08:00
|
|
|
static GimpPDBStatusType sanity_check (const gchar *filename,
|
2012-12-26 16:41:20 +08:00
|
|
|
gint32 *image_ID,
|
2008-08-18 05:30:34 +08:00
|
|
|
GError **error);
|
2006-04-13 19:16:13 +08:00
|
|
|
static gboolean bad_bounds_dialog (void);
|
1999-10-04 02:54:54 +08:00
|
|
|
|
2004-05-19 23:28:01 +08:00
|
|
|
static gboolean save_dialog (gint32 image_ID);
|
|
|
|
static void comment_entry_callback (GtkTextBuffer *buffer);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2013-05-28 04:32:08 +08:00
|
|
|
static GimpRunMode run_mode;
|
|
|
|
static GimpParasite *comment_parasite = NULL;
|
|
|
|
static gboolean comment_was_edited = FALSE;
|
2013-05-28 04:55:34 +08:00
|
|
|
static gchar *globalcomment = NULL;
|
|
|
|
static gint Interlace; /* For compression code */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static GIFSaveVals gsvals =
|
|
|
|
{
|
2000-06-08 06:51:23 +08:00
|
|
|
FALSE, /* interlace */
|
|
|
|
TRUE, /* save comment */
|
|
|
|
TRUE, /* loop infinitely */
|
1997-11-25 06:05:25 +08:00
|
|
|
100, /* default_delay between frames (100ms) */
|
2005-08-30 02:45:02 +08:00
|
|
|
0, /* default_dispose = "don't care" */
|
|
|
|
FALSE, /* don't always use default_delay */
|
2010-02-16 05:47:19 +08:00
|
|
|
FALSE, /* don't always use default_dispose */
|
|
|
|
FALSE /* as_animation */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
MAIN ()
|
|
|
|
|
|
|
|
static void
|
2000-01-26 01:46:56 +08:00
|
|
|
query (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef save_args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Image to save" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
|
|
|
|
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
|
2005-08-14 06:52:41 +08:00
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "The name entered" },
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_INT32, "interlace", "Try to save as interlaced" },
|
|
|
|
{ GIMP_PDB_INT32, "loop", "(animated gif) loop infinitely" },
|
2005-08-14 06:52:41 +08:00
|
|
|
{ GIMP_PDB_INT32, "default-delay", "(animated gif) Default delay between framese in milliseconds" },
|
|
|
|
{ GIMP_PDB_INT32, "default-dispose", "(animated gif) Default disposal type (0=`don't care`, 1=combine, 2=replace)" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2005-08-14 06:52:41 +08:00
|
|
|
gimp_install_procedure (SAVE_PROC,
|
2000-01-31 10:32:30 +08:00
|
|
|
"saves files in Compuserve GIF file format",
|
2000-10-20 11:38:15 +08:00
|
|
|
"Save a file in Compuserve GIF format, with "
|
2006-04-13 19:16:13 +08:00
|
|
|
"possible animation, transparency, and comment. "
|
|
|
|
"To save an animation, operate on a multi-layer "
|
|
|
|
"file. The plug-in will intrepret <50% alpha as "
|
|
|
|
"transparent. When run non-interactively, the "
|
|
|
|
"value for the comment is taken from the "
|
|
|
|
"'gimp-comment' parasite. ",
|
1997-11-25 06:05:25 +08:00
|
|
|
"Spencer Kimball, Peter Mattis, Adam Moss, David Koblas",
|
|
|
|
"Spencer Kimball, Peter Mattis, Adam Moss, David Koblas",
|
|
|
|
"1995-1997",
|
2004-05-14 08:01:11 +08:00
|
|
|
N_("GIF image"),
|
2006-04-13 19:16:13 +08:00
|
|
|
"INDEXED*, GRAY*",
|
2000-08-22 09:26:57 +08:00
|
|
|
GIMP_PLUGIN,
|
2001-12-06 10:28:58 +08:00
|
|
|
G_N_ELEMENTS (save_args), 0,
|
1997-11-25 06:05:25 +08:00
|
|
|
save_args, NULL);
|
|
|
|
|
2005-08-14 06:52:41 +08:00
|
|
|
gimp_register_file_handler_mime (SAVE_PROC, "image/gif");
|
|
|
|
gimp_register_save_handler (SAVE_PROC, "gif", "");
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-07-02 19:07:41 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-11-06 23:27:05 +08:00
|
|
|
static GimpParam values[2];
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
2008-08-18 05:30:34 +08:00
|
|
|
GError *error = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
INIT_I18N ();
|
2012-09-23 05:19:32 +08:00
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
2012-11-28 03:58:05 +08:00
|
|
|
run_mode = param[0].data.d_int32;
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
*nreturn_vals = 1;
|
|
|
|
*return_vals = values;
|
2008-03-26 21:12:02 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
values[0].type = GIMP_PDB_STATUS;
|
|
|
|
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-08-14 06:52:41 +08:00
|
|
|
if (strcmp (name, SAVE_PROC) == 0)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2008-03-26 21:12:02 +08:00
|
|
|
const gchar *filename;
|
2012-12-26 16:41:20 +08:00
|
|
|
gint32 image_ID, sanitized_image_ID = 0;
|
2008-03-26 21:12:02 +08:00
|
|
|
gint32 drawable_ID;
|
|
|
|
gint32 orig_image_ID;
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
image_ID = orig_image_ID = param[1].data.d_int32;
|
|
|
|
drawable_ID = param[2].data.d_int32;
|
2008-03-26 21:12:02 +08:00
|
|
|
filename = param[3].data.d_string;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2010-07-27 01:41:50 +08:00
|
|
|
if (run_mode == GIMP_RUN_INTERACTIVE ||
|
|
|
|
run_mode == GIMP_RUN_WITH_LAST_VALS)
|
2010-07-08 01:25:14 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY, FALSE);
|
|
|
|
|
2012-12-26 16:41:20 +08:00
|
|
|
status = sanity_check (filename, &image_ID, &error);
|
2008-08-18 05:30:34 +08:00
|
|
|
|
2010-02-16 05:47:19 +08:00
|
|
|
/* Get the export options */
|
2008-08-18 05:30:34 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
2012-12-26 16:41:20 +08:00
|
|
|
/* If the sanity check succeeded, the image_ID will point to
|
|
|
|
* a duplicate image to delete later. */
|
|
|
|
sanitized_image_ID = image_ID;
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
switch (run_mode)
|
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
/* Possibly retrieve data */
|
|
|
|
gimp_get_data (SAVE_PROC, &gsvals);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
/* First acquire information with a dialog */
|
|
|
|
if (! save_dialog (image_ID))
|
|
|
|
status = GIMP_PDB_CANCEL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RUN_NONINTERACTIVE:
|
|
|
|
/* Make sure all the arguments are there! */
|
|
|
|
if (nparams != 9)
|
|
|
|
{
|
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsvals.interlace = (param[5].data.d_int32) ? TRUE : FALSE;
|
|
|
|
gsvals.save_comment = TRUE; /* no way to to specify that through the PDB */
|
|
|
|
gsvals.loop = (param[6].data.d_int32) ? TRUE : FALSE;
|
|
|
|
gsvals.default_delay = param[7].data.d_int32;
|
|
|
|
gsvals.default_dispose = param[8].data.d_int32;
|
|
|
|
}
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
/* Possibly retrieve data */
|
|
|
|
gimp_get_data (SAVE_PROC, &gsvals);
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-02-13 20:26:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2010-02-16 05:47:19 +08:00
|
|
|
/* Create an exportable image based on the export options */
|
|
|
|
switch (run_mode)
|
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
{
|
|
|
|
GimpExportCapabilities capabilities =
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_ALPHA;
|
|
|
|
|
|
|
|
if (gsvals.as_animation)
|
|
|
|
capabilities |= GIMP_EXPORT_CAN_HANDLE_LAYERS;
|
|
|
|
|
2013-11-10 07:18:48 +08:00
|
|
|
export = gimp_export_image (&image_ID, &drawable_ID, "GIF",
|
2010-02-16 05:47:19 +08:00
|
|
|
capabilities);
|
|
|
|
|
|
|
|
if (export == GIMP_EXPORT_CANCEL)
|
|
|
|
{
|
|
|
|
values[0].data.d_status = GIMP_PDB_CANCEL;
|
2012-12-26 16:41:20 +08:00
|
|
|
if (sanitized_image_ID)
|
|
|
|
gimp_image_delete (sanitized_image_ID);
|
2010-02-16 05:47:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the image to file */
|
2010-02-13 20:26:46 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
|
|
|
{
|
|
|
|
if (save_image (param[3].data.d_string,
|
|
|
|
image_ID, drawable_ID, orig_image_ID,
|
|
|
|
&error))
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
2010-02-13 20:26:46 +08:00
|
|
|
/* Store psvals data */
|
|
|
|
gimp_set_data (SAVE_PROC, &gsvals, sizeof (GIFSaveVals));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
2012-12-26 16:41:20 +08:00
|
|
|
|
|
|
|
gimp_image_delete (sanitized_image_ID);
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
|
|
|
gimp_image_delete (image_ID);
|
2008-08-18 14:31:03 +08:00
|
|
|
}
|
2008-08-18 05:30:34 +08:00
|
|
|
|
2008-08-18 14:31:03 +08:00
|
|
|
if (status != GIMP_PDB_SUCCESS && error)
|
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_STRING;
|
|
|
|
values[1].data.d_string = error->message;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
values[0].data.d_status = status;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* ppmtogif.c - read a portable pixmap and produce a GIF file
|
|
|
|
**
|
1999-11-20 10:20:04 +08:00
|
|
|
** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
|
|
|
|
** Lempel-Ziv compression based on "compress".
|
1997-11-25 06:05:25 +08:00
|
|
|
**
|
|
|
|
** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Copyright (C) 1989 by Jef Poskanzer.
|
|
|
|
**
|
|
|
|
** Permission to use, copy, modify, and distribute this software and its
|
|
|
|
** documentation for any purpose and without fee is hereby granted, provided
|
|
|
|
** that the above copyright notice appear in all copies and that both that
|
|
|
|
** copyright notice and this permission notice appear in supporting
|
|
|
|
** documentation. This software is provided "as is" without express or
|
|
|
|
** implied warranty.
|
|
|
|
**
|
|
|
|
** The Graphics Interchange Format(c) is the Copyright property of
|
|
|
|
** CompuServe Incorporated. GIF(sm) is a Service Mark property of
|
|
|
|
** CompuServe Incorporated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAXCOLORS 256
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointer to function returning an int
|
|
|
|
*/
|
|
|
|
typedef int (*ifunptr) (int, int);
|
|
|
|
|
|
|
|
|
2013-06-07 05:26:16 +08:00
|
|
|
static gint find_unused_ia_color (const guchar *pixels,
|
2006-08-31 01:01:47 +08:00
|
|
|
gint numpixels,
|
|
|
|
gint num_indices,
|
|
|
|
gint *colors);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
static void special_flatten_indexed_alpha (guchar *pixels,
|
2008-03-20 17:29:04 +08:00
|
|
|
gint transparent,
|
2003-07-04 01:45:12 +08:00
|
|
|
gint numpixels);
|
2013-05-28 04:55:34 +08:00
|
|
|
static gint colors_to_bpp (int);
|
|
|
|
static gint bpp_to_colors (int);
|
|
|
|
static gint get_pixel (int, int);
|
|
|
|
static gint gif_next_pixel (ifunptr);
|
|
|
|
static void bump_pixel (void);
|
2006-04-13 19:16:13 +08:00
|
|
|
|
|
|
|
static void gif_encode_header (FILE *, gboolean, int, int, int, int,
|
|
|
|
int *, int *, int *, ifunptr);
|
|
|
|
static void gif_encode_graphic_control_ext (FILE *, int, int, int, int,
|
|
|
|
int, int, int, ifunptr);
|
|
|
|
static void gif_encode_image_data (FILE *, int, int, int, int,
|
|
|
|
ifunptr, gint, gint);
|
|
|
|
static void gif_encode_close (FILE *);
|
|
|
|
static void gif_encode_loop_ext (FILE *, guint);
|
|
|
|
static void gif_encode_comment_ext (FILE *, const gchar *comment);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-04 22:03:00 +08:00
|
|
|
static gint rowstride;
|
2006-04-13 19:16:13 +08:00
|
|
|
static guchar *pixels;
|
2006-04-04 22:03:00 +08:00
|
|
|
static gint cur_progress;
|
|
|
|
static gint max_progress;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static void compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue);
|
|
|
|
static void no_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue);
|
|
|
|
static void rle_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue);
|
|
|
|
static void normal_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue);
|
|
|
|
|
|
|
|
static void put_word (int, FILE *);
|
|
|
|
static void output (gint);
|
|
|
|
static void cl_block (void);
|
|
|
|
static void cl_hash (glong);
|
|
|
|
static void char_init (void);
|
|
|
|
static void char_out (int);
|
|
|
|
static void flush_char (void);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
static gint
|
2013-06-07 05:26:16 +08:00
|
|
|
find_unused_ia_color (const guchar *pixels,
|
2006-08-31 01:01:47 +08:00
|
|
|
gint numpixels,
|
|
|
|
gint num_indices,
|
|
|
|
gint *colors)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
gboolean ix_used[256];
|
2013-05-28 04:55:34 +08:00
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-10-10 02:01:35 +08:00
|
|
|
#ifdef GIFDEBUG
|
2003-06-17 01:51:03 +08:00
|
|
|
g_printerr ("GIF: fuiac: Image claims to use %d/%d indices - finding free "
|
2006-08-31 01:01:47 +08:00
|
|
|
"index...\n", *colors, num_indices);
|
1998-10-10 02:01:35 +08:00
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
for (i = 0; i < 256; i++)
|
2006-08-31 01:01:47 +08:00
|
|
|
ix_used[i] = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
for (i = 0; i < numpixels; i++)
|
1998-09-16 04:34:30 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
if (pixels[i * 2 + 1])
|
2006-08-31 01:01:47 +08:00
|
|
|
ix_used[pixels[i * 2]] = TRUE;
|
1998-09-16 04:34:30 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
for (i = num_indices - 1; i >= 0; i--)
|
1998-09-16 04:34:30 +08:00
|
|
|
{
|
2006-08-31 01:01:47 +08:00
|
|
|
if (! ix_used[i])
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
1998-10-10 02:01:35 +08:00
|
|
|
#ifdef GIFDEBUG
|
2013-06-07 05:26:16 +08:00
|
|
|
g_printerr ("GIF: Found unused color index %d.\n", (int) i);
|
1998-10-10 02:01:35 +08:00
|
|
|
#endif
|
2006-04-13 19:16:13 +08:00
|
|
|
return i;
|
|
|
|
}
|
1998-09-16 04:34:30 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-06-07 05:26:16 +08:00
|
|
|
/* Couldn't find an unused color index within the number of
|
1998-09-16 04:34:30 +08:00
|
|
|
bits per pixel we wanted. Will have to increment the number
|
2013-06-07 05:26:16 +08:00
|
|
|
of colors in the image and assign a transparent pixel there. */
|
2006-08-31 01:01:47 +08:00
|
|
|
if (*colors < 256)
|
1998-09-16 04:34:30 +08:00
|
|
|
{
|
|
|
|
(*colors)++;
|
2006-08-31 01:01:47 +08:00
|
|
|
|
2003-06-17 01:51:03 +08:00
|
|
|
g_printerr ("GIF: 2nd pass "
|
2013-06-07 05:26:16 +08:00
|
|
|
"- Increasing bounds and using color index %d.\n",
|
2006-08-31 01:01:47 +08:00
|
|
|
*colors - 1);
|
2006-04-13 19:16:13 +08:00
|
|
|
return ((*colors) - 1);
|
1998-09-16 04:34:30 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2003-11-15 21:53:33 +08:00
|
|
|
g_message (_("Couldn't simply reduce colors further. Saving as opaque."));
|
2006-08-31 01:01:47 +08:00
|
|
|
|
|
|
|
return -1;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
static void
|
1997-11-25 06:05:25 +08:00
|
|
|
special_flatten_indexed_alpha (guchar *pixels,
|
2006-08-31 01:01:47 +08:00
|
|
|
gint transparent,
|
|
|
|
gint numpixels)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
guint32 i;
|
|
|
|
|
|
|
|
/* Each transparent pixel in the image is mapped to a uniform value for
|
2013-06-07 05:26:16 +08:00
|
|
|
encoding, if image already has <=255 colors */
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2006-08-31 01:01:47 +08:00
|
|
|
if (transparent == -1) /* tough, no indices left for the trans. index */
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
for (i = 0; i < numpixels; i++)
|
|
|
|
pixels[i] = pixels[i * 2];
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else /* make transparent */
|
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
for (i = 0; i < numpixels; i++)
|
|
|
|
{
|
|
|
|
if (! (pixels[i * 2 + 1] & 128))
|
|
|
|
{
|
2006-08-31 01:01:47 +08:00
|
|
|
pixels[i] = (guchar) transparent;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixels[i] = pixels[i * 2];
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-31 01:01:47 +08:00
|
|
|
static gint
|
|
|
|
parse_ms_tag (const gchar *str)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2013-05-28 04:55:34 +08:00
|
|
|
gint sum = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
gint offset = 0;
|
|
|
|
gint length;
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
length = strlen (str);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-04-29 18:48:02 +08:00
|
|
|
find_another_bra:
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((offset < length) && (str[offset] != '('))
|
1997-11-25 06:05:25 +08:00
|
|
|
offset++;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (offset >= length)
|
1997-11-25 06:05:25 +08:00
|
|
|
return(-1);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (! g_ascii_isdigit (str[++offset]))
|
1998-04-29 18:48:02 +08:00
|
|
|
goto find_another_bra;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sum *= 10;
|
|
|
|
sum += str[offset] - '0';
|
|
|
|
offset++;
|
|
|
|
}
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((offset < length) && (g_ascii_isdigit (str[offset])));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (length - offset <= 2)
|
1997-11-25 06:05:25 +08:00
|
|
|
return(-3);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if ((g_ascii_toupper (str[offset]) != 'M')
|
|
|
|
|| (g_ascii_toupper (str[offset+1]) != 'S'))
|
2003-11-27 03:20:24 +08:00
|
|
|
return -4;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-27 03:20:24 +08:00
|
|
|
return sum;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-31 01:01:47 +08:00
|
|
|
static gint
|
|
|
|
parse_disposal_tag (const gchar *str)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
gint offset = 0;
|
|
|
|
gint length;
|
|
|
|
|
|
|
|
length = strlen(str);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((offset + 9) <= length)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
if (strncmp(&str[offset], "(combine)", 9) == 0)
|
|
|
|
return(0x01);
|
|
|
|
if (strncmp(&str[offset], "(replace)", 9) == 0)
|
|
|
|
return(0x02);
|
1997-11-25 06:05:25 +08:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (gsvals.default_dispose);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-18 05:30:34 +08:00
|
|
|
static GimpPDBStatusType
|
|
|
|
sanity_check (const gchar *filename,
|
2012-12-26 16:41:20 +08:00
|
|
|
gint32 *image_ID,
|
2008-08-18 05:30:34 +08:00
|
|
|
GError **error)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2008-03-20 17:29:04 +08:00
|
|
|
gint32 *layers;
|
|
|
|
gint nlayers;
|
|
|
|
gint image_width;
|
|
|
|
gint image_height;
|
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-12-26 16:41:20 +08:00
|
|
|
image_width = gimp_image_width (*image_ID);
|
|
|
|
image_height = gimp_image_height (*image_ID);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-03-20 17:29:04 +08:00
|
|
|
if (image_width > G_MAXUSHORT || image_height > G_MAXUSHORT)
|
|
|
|
{
|
2008-08-18 05:30:34 +08:00
|
|
|
g_set_error (error, 0, 0,
|
|
|
|
_("Unable to save '%s'. "
|
|
|
|
"The GIF file format does not support images that are "
|
|
|
|
"more than %d pixels wide or tall."),
|
|
|
|
gimp_filename_to_utf8 (filename), G_MAXUSHORT);
|
|
|
|
|
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2008-03-20 17:29:04 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*** Iterate through the layers to make sure they're all ***/
|
|
|
|
/*** within the bounds of the image ***/
|
|
|
|
|
2012-12-26 16:41:20 +08:00
|
|
|
*image_ID = gimp_image_duplicate (*image_ID);
|
|
|
|
layers = gimp_image_get_layers (*image_ID, &nlayers);
|
2008-03-20 17:29:04 +08:00
|
|
|
|
2001-07-07 22:53:42 +08:00
|
|
|
for (i = 0; i < nlayers; i++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2008-03-20 17:29:04 +08:00
|
|
|
gint offset_x;
|
|
|
|
gint offset_y;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_drawable_offsets (layers[i], &offset_x, &offset_y);
|
|
|
|
|
2008-03-20 17:29:04 +08:00
|
|
|
if (offset_x < 0 ||
|
|
|
|
offset_y < 0 ||
|
|
|
|
offset_x + gimp_drawable_width (layers[i]) > image_width ||
|
|
|
|
offset_y + gimp_drawable_height (layers[i]) > image_height)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
g_free (layers);
|
|
|
|
|
|
|
|
/* Image has illegal bounds - ask the user what it wants to do */
|
|
|
|
|
|
|
|
/* Do the crop if we can't talk to the user, or if we asked
|
2008-08-18 05:30:34 +08:00
|
|
|
* the user and they said yes.
|
|
|
|
*/
|
2006-04-13 19:16:13 +08:00
|
|
|
if ((run_mode == GIMP_RUN_NONINTERACTIVE) || bad_bounds_dialog ())
|
|
|
|
{
|
2012-12-26 16:41:20 +08:00
|
|
|
gimp_image_crop (*image_ID, image_width, image_height, 0, 0);
|
2008-08-18 05:30:34 +08:00
|
|
|
return GIMP_PDB_SUCCESS;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-26 16:41:20 +08:00
|
|
|
gimp_image_delete (*image_ID);
|
2008-08-18 05:30:34 +08:00
|
|
|
return GIMP_PDB_CANCEL;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (layers);
|
|
|
|
|
2008-08-18 05:30:34 +08:00
|
|
|
return GIMP_PDB_SUCCESS;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-18 05:30:34 +08:00
|
|
|
static gboolean
|
2003-07-02 19:07:41 +08:00
|
|
|
save_image (const gchar *filename,
|
2006-04-13 19:16:13 +08:00
|
|
|
gint32 image_ID,
|
|
|
|
gint32 drawable_ID,
|
2008-08-18 05:30:34 +08:00
|
|
|
gint32 orig_image_ID,
|
|
|
|
GError **error)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-09-23 05:19:32 +08:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
GimpImageType drawable_type;
|
|
|
|
const Babl *format = NULL;
|
|
|
|
FILE *outfile;
|
|
|
|
gint Red[MAXCOLORS];
|
|
|
|
gint Green[MAXCOLORS];
|
|
|
|
gint Blue[MAXCOLORS];
|
|
|
|
guchar *cmap;
|
|
|
|
guint rows, cols;
|
|
|
|
gint BitsPerPixel, liberalBPP = 0, useBPP = 0;
|
|
|
|
gint colors;
|
|
|
|
gint i;
|
|
|
|
gint transparent;
|
|
|
|
gint offset_x, offset_y;
|
|
|
|
|
|
|
|
gint32 *layers;
|
|
|
|
gint nlayers;
|
|
|
|
|
|
|
|
gboolean is_gif89 = FALSE;
|
|
|
|
|
|
|
|
gint Delay89;
|
|
|
|
gint Disposal;
|
|
|
|
gchar *layer_name;
|
|
|
|
|
|
|
|
GimpRGB background;
|
|
|
|
guchar bgred, bggreen, bgblue;
|
|
|
|
guchar bgindex = 0;
|
|
|
|
guint best_error = 0xFFFFFFFF;
|
1998-03-16 18:05:41 +08:00
|
|
|
|
1999-04-26 00:10:43 +08:00
|
|
|
/* Save the comment back to the ImageID, if appropriate */
|
1999-10-04 02:54:54 +08:00
|
|
|
if (globalcomment != NULL && comment_was_edited)
|
1999-04-26 00:10:43 +08:00
|
|
|
{
|
2000-05-27 06:28:40 +08:00
|
|
|
comment_parasite = gimp_parasite_new ("gimp-comment",
|
2006-04-13 19:16:13 +08:00
|
|
|
GIMP_PARASITE_PERSISTENT,
|
|
|
|
strlen (globalcomment) + 1,
|
|
|
|
(void*) globalcomment);
|
2011-03-08 20:19:21 +08:00
|
|
|
gimp_image_attach_parasite (orig_image_ID, comment_parasite);
|
2000-05-27 06:28:40 +08:00
|
|
|
gimp_parasite_free (comment_parasite);
|
1999-04-26 00:10:43 +08:00
|
|
|
comment_parasite = NULL;
|
|
|
|
}
|
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
/* The GIF spec says 7bit ASCII for the comment block. */
|
|
|
|
if (gsvals.save_comment && globalcomment)
|
|
|
|
{
|
|
|
|
const gchar *c = globalcomment;
|
|
|
|
gint len;
|
|
|
|
|
|
|
|
for (len = strlen (c); len; c++, len--)
|
|
|
|
{
|
2003-12-06 07:40:09 +08:00
|
|
|
if ((guchar) *c > 127)
|
2003-07-04 01:45:12 +08:00
|
|
|
{
|
2003-11-15 21:53:33 +08:00
|
|
|
g_message (_("The GIF format only supports comments in "
|
2003-07-04 01:45:12 +08:00
|
|
|
"7bit ASCII encoding. No comment is saved."));
|
|
|
|
|
|
|
|
g_free (globalcomment);
|
|
|
|
globalcomment = NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* get a list of layers for this image_ID */
|
2003-11-06 23:27:05 +08:00
|
|
|
layers = gimp_image_get_layers (image_ID, &nlayers);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
drawable_type = gimp_drawable_type (layers[0]);
|
|
|
|
|
|
|
|
/* If the image has multiple layers (i.e. will be animated), a comment,
|
|
|
|
or transparency, then it must be encoded as a GIF89a file, not a vanilla
|
|
|
|
GIF87a. */
|
1999-10-04 02:54:54 +08:00
|
|
|
if (nlayers > 1)
|
1997-11-25 06:05:25 +08:00
|
|
|
is_gif89 = TRUE;
|
2000-06-08 06:51:23 +08:00
|
|
|
|
|
|
|
if (gsvals.save_comment)
|
1997-11-25 06:05:25 +08:00
|
|
|
is_gif89 = TRUE;
|
|
|
|
|
|
|
|
switch (drawable_type)
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_INDEXEDA_IMAGE:
|
1997-11-25 06:05:25 +08:00
|
|
|
is_gif89 = TRUE;
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_INDEXED_IMAGE:
|
2004-11-02 20:00:25 +08:00
|
|
|
cmap = gimp_image_get_colormap (image_ID, &colors);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2004-09-23 02:43:09 +08:00
|
|
|
gimp_context_get_background (&background);
|
2001-01-15 08:06:43 +08:00
|
|
|
gimp_rgb_get_uchar (&background, &bgred, &bggreen, &bgblue);
|
1998-03-16 18:05:41 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < colors; i++)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
Red[i] = *cmap++;
|
|
|
|
Green[i] = *cmap++;
|
|
|
|
Blue[i] = *cmap++;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
for ( ; i < 256; i++)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
Red[i] = bgred;
|
|
|
|
Green[i] = bggreen;
|
|
|
|
Blue[i] = bgblue;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_GRAYA_IMAGE:
|
1997-11-25 06:05:25 +08:00
|
|
|
is_gif89 = TRUE;
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_GRAY_IMAGE:
|
1998-03-16 18:05:41 +08:00
|
|
|
colors = 256; /* FIXME: Not ideal. */
|
1997-11-25 06:05:25 +08:00
|
|
|
for ( i = 0; i < 256; i++)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
Red[i] = Green[i] = Blue[i] = i;
|
|
|
|
}
|
2012-09-23 05:19:32 +08:00
|
|
|
|
|
|
|
if (drawable_type == GIMP_GRAYA_IMAGE)
|
|
|
|
format = babl_format ("Y'A u8");
|
|
|
|
else
|
|
|
|
format = babl_format ("Y' u8");
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-11-15 21:53:33 +08:00
|
|
|
g_message (_("Cannot save RGB color images. Convert to "
|
|
|
|
"indexed color or grayscale first."));
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2003-06-17 01:37:11 +08:00
|
|
|
|
|
|
|
/* find earliest index in palette which is closest to the background
|
2013-06-07 05:26:16 +08:00
|
|
|
color, and ATTEMPT to use that as the GIF's default background color. */
|
2006-04-04 22:03:00 +08:00
|
|
|
for (i = 255; i >= 0; --i)
|
|
|
|
{
|
|
|
|
guint local_error = 0;
|
|
|
|
|
|
|
|
local_error += (Red[i] - bgred) * (Red[i] - bgred);
|
|
|
|
local_error += (Green[i] - bggreen) * (Green[i] - bggreen);
|
|
|
|
local_error += (Blue[i] - bgblue) * (Blue[i] - bgblue);
|
|
|
|
|
|
|
|
if (local_error <= best_error)
|
|
|
|
{
|
|
|
|
bgindex = i;
|
|
|
|
best_error = local_error;
|
|
|
|
}
|
2003-06-17 01:37:11 +08:00
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* open the destination file for writing */
|
2005-03-04 23:12:29 +08:00
|
|
|
outfile = g_fopen (filename, "wb");
|
1997-11-25 06:05:25 +08:00
|
|
|
if (!outfile)
|
|
|
|
{
|
2008-08-18 05:30:34 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
/* init the progress meter */
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Saving '%s'"),
|
2005-09-30 02:34:08 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* write the GIFheader */
|
|
|
|
|
|
|
|
if (colors < 256)
|
1998-03-16 18:05:41 +08:00
|
|
|
{
|
1998-09-16 04:34:30 +08:00
|
|
|
/* we keep track of how many bits we promised to have in liberalBPP,
|
2006-04-13 19:16:13 +08:00
|
|
|
so that we don't accidentally come under this when doing
|
|
|
|
clever transparency stuff where we can re-use wasted indices. */
|
1998-09-16 04:34:30 +08:00
|
|
|
liberalBPP = BitsPerPixel =
|
2006-04-13 19:16:13 +08:00
|
|
|
colors_to_bpp (colors + ((drawable_type==GIMP_INDEXEDA_IMAGE) ? 1 : 0));
|
1998-03-16 18:05:41 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
1998-03-16 18:05:41 +08:00
|
|
|
{
|
1998-09-29 01:14:29 +08:00
|
|
|
liberalBPP = BitsPerPixel =
|
2006-04-13 19:16:13 +08:00
|
|
|
colors_to_bpp (256);
|
1998-09-29 01:14:29 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (drawable_type == GIMP_INDEXEDA_IMAGE)
|
|
|
|
{
|
2013-06-07 05:26:16 +08:00
|
|
|
g_printerr ("GIF: Too many colors?\n");
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1998-03-16 18:05:41 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-10-04 02:54:54 +08:00
|
|
|
cols = gimp_image_width (image_ID);
|
|
|
|
rows = gimp_image_height (image_ID);
|
1998-10-10 02:01:35 +08:00
|
|
|
Interlace = gsvals.interlace;
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_header (outfile, is_gif89, cols, rows, bgindex,
|
|
|
|
BitsPerPixel, Red, Green, Blue, get_pixel);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* If the image has multiple layers it'll be made into an
|
|
|
|
animated GIF, so write out the infinite-looping extension */
|
|
|
|
if ((nlayers > 1) && (gsvals.loop))
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_loop_ext (outfile, 0);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* Write comment extension - mustn't be written before the looping ext. */
|
2001-09-18 02:11:15 +08:00
|
|
|
if (gsvals.save_comment && globalcomment)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_comment_ext (outfile, globalcomment);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** Now for each layer in the image, save an image in a compound GIF ***/
|
|
|
|
/************************************************************************/
|
|
|
|
|
2006-04-04 22:03:00 +08:00
|
|
|
cur_progress = 0;
|
|
|
|
max_progress = nlayers * rows;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-04 22:03:00 +08:00
|
|
|
for (i = nlayers - 1; i >= 0; i--, cur_progress = (nlayers - i) * rows)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
drawable_type = gimp_drawable_type (layers[i]);
|
2012-09-23 05:19:32 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (layers[i]);
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_drawable_offsets (layers[i], &offset_x, &offset_y);
|
2012-09-23 05:19:32 +08:00
|
|
|
cols = gimp_drawable_width (layers[i]);
|
|
|
|
rows = gimp_drawable_height (layers[i]);
|
|
|
|
rowstride = cols;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2012-09-23 05:19:32 +08:00
|
|
|
pixels = g_new (guchar, (cols * rows *
|
|
|
|
(((drawable_type == GIMP_INDEXEDA_IMAGE) ||
|
|
|
|
(drawable_type == GIMP_GRAYA_IMAGE)) ? 2 : 1)));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-09-23 05:19:32 +08:00
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, cols, rows), 1.0,
|
|
|
|
format, pixels,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* sort out whether we need to do transparency jiggery-pokery */
|
2012-09-23 05:19:32 +08:00
|
|
|
if ((drawable_type == GIMP_INDEXEDA_IMAGE) ||
|
|
|
|
(drawable_type == GIMP_GRAYA_IMAGE))
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
/* Try to find an entry which isn't actually used in the
|
|
|
|
image, for a transparency index. */
|
|
|
|
|
|
|
|
transparent =
|
2013-06-07 05:26:16 +08:00
|
|
|
find_unused_ia_color (pixels,
|
2012-09-23 05:19:32 +08:00
|
|
|
cols * rows,
|
2006-08-31 01:01:47 +08:00
|
|
|
bpp_to_colors (colors_to_bpp (colors)),
|
|
|
|
&colors);
|
2006-04-13 19:16:13 +08:00
|
|
|
|
|
|
|
special_flatten_indexed_alpha (pixels,
|
2006-08-31 01:01:47 +08:00
|
|
|
transparent,
|
2012-09-23 05:19:32 +08:00
|
|
|
cols * rows);
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2006-02-20 01:17:03 +08:00
|
|
|
{
|
|
|
|
transparent = -1;
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
BitsPerPixel = colors_to_bpp (colors);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-09-16 04:34:30 +08:00
|
|
|
if (BitsPerPixel != liberalBPP)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
/* We were able to re-use an index within the existing bitspace,
|
|
|
|
whereas the estimate in the header was pessimistic but still
|
|
|
|
needs to be upheld... */
|
2006-02-20 01:17:03 +08:00
|
|
|
#ifdef GIFDEBUG
|
2006-04-13 19:16:13 +08:00
|
|
|
static gboolean onceonly = FALSE;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (! onceonly)
|
|
|
|
{
|
|
|
|
g_warning ("Promised %d bpp, pondered writing chunk with %d bpp!",
|
2003-06-17 01:51:03 +08:00
|
|
|
liberalBPP, BitsPerPixel);
|
|
|
|
onceonly = TRUE;
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
2006-02-20 01:17:03 +08:00
|
|
|
#endif
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1998-09-16 04:34:30 +08:00
|
|
|
|
2006-02-20 01:17:03 +08:00
|
|
|
useBPP = (BitsPerPixel > liberalBPP) ? BitsPerPixel : liberalBPP;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (is_gif89)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
if (i > 0 && ! gsvals.always_use_default_dispose)
|
|
|
|
{
|
2010-07-09 18:27:36 +08:00
|
|
|
layer_name = gimp_item_get_name (layers[i - 1]);
|
2006-04-13 19:16:13 +08:00
|
|
|
Disposal = parse_disposal_tag (layer_name);
|
|
|
|
g_free (layer_name);
|
|
|
|
}
|
|
|
|
else
|
2006-04-04 22:03:00 +08:00
|
|
|
{
|
|
|
|
Disposal = gsvals.default_dispose;
|
|
|
|
}
|
1997-12-12 18:01:21 +08:00
|
|
|
|
2010-07-09 18:27:36 +08:00
|
|
|
layer_name = gimp_item_get_name (layers[i]);
|
2006-04-13 19:16:13 +08:00
|
|
|
Delay89 = parse_ms_tag (layer_name);
|
|
|
|
g_free (layer_name);
|
|
|
|
|
|
|
|
if (Delay89 < 0 || gsvals.always_use_default_delay)
|
|
|
|
Delay89 = (gsvals.default_delay + 5) / 10;
|
|
|
|
else
|
|
|
|
Delay89 = (Delay89 + 5) / 10;
|
|
|
|
|
|
|
|
/* don't allow a CPU-sucking completely 0-delay looping anim */
|
|
|
|
if ((nlayers > 1) && gsvals.loop && (Delay89 == 0))
|
|
|
|
{
|
|
|
|
static gboolean onceonly = FALSE;
|
|
|
|
|
|
|
|
if (!onceonly)
|
|
|
|
{
|
|
|
|
g_message (_("Delay inserted to prevent evil "
|
2006-02-20 01:17:03 +08:00
|
|
|
"CPU-sucking animation."));
|
2006-04-13 19:16:13 +08:00
|
|
|
onceonly = TRUE;
|
|
|
|
}
|
|
|
|
Delay89 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gif_encode_graphic_control_ext (outfile, Disposal, Delay89, nlayers,
|
|
|
|
cols, rows,
|
|
|
|
transparent,
|
|
|
|
useBPP,
|
|
|
|
get_pixel);
|
|
|
|
}
|
|
|
|
|
2011-04-11 01:05:08 +08:00
|
|
|
gif_encode_image_data (outfile, cols, rows,
|
|
|
|
(rows > 4) ? gsvals.interlace : 0,
|
|
|
|
useBPP,
|
|
|
|
get_pixel,
|
|
|
|
offset_x, offset_y);
|
|
|
|
gimp_progress_update (1.0);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2012-09-23 05:19:32 +08:00
|
|
|
g_object_unref (buffer);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2011-04-11 01:05:08 +08:00
|
|
|
g_free (pixels);
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
g_free(layers);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_close (outfile);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2006-04-13 19:16:13 +08:00
|
|
|
bad_bounds_dialog (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-09-10 02:07:31 +08:00
|
|
|
GtkWidget *dialog;
|
2003-11-06 23:27:05 +08:00
|
|
|
gboolean crop;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-10-12 18:00:23 +08:00
|
|
|
dialog = gtk_message_dialog_new (NULL, 0,
|
|
|
|
GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
|
|
|
|
_("The image you are trying to save as a "
|
|
|
|
"GIF contains layers which extend beyond "
|
|
|
|
"the actual borders of the image."));
|
2000-01-07 06:26:10 +08:00
|
|
|
|
2006-10-12 18:00:23 +08:00
|
|
|
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GIMP_STOCK_TOOL_CROP, GTK_RESPONSE_OK,
|
|
|
|
NULL);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
|
2005-08-14 06:52:41 +08:00
|
|
|
GTK_RESPONSE_OK,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1);
|
2005-02-09 04:40:33 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gimp_window_set_transient (GTK_WINDOW (dialog));
|
|
|
|
|
2006-10-12 18:00:23 +08:00
|
|
|
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
|
|
|
|
_("The GIF file format does not "
|
|
|
|
"allow this. You may choose "
|
|
|
|
"whether to crop all of the "
|
|
|
|
"layers to the image borders, "
|
|
|
|
"or cancel this save."));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-10-12 18:00:23 +08:00
|
|
|
crop = (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_destroy (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return crop;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2010-02-13 19:23:39 +08:00
|
|
|
static GtkWidget *
|
|
|
|
file_gif_toggle_button_init (GtkBuilder *builder,
|
|
|
|
const gchar *name,
|
|
|
|
gboolean initial_value,
|
|
|
|
gboolean *value_pointer)
|
|
|
|
{
|
|
|
|
GtkWidget *toggle = NULL;
|
|
|
|
|
|
|
|
toggle = GTK_WIDGET (gtk_builder_get_object (builder, name));
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), initial_value);
|
|
|
|
g_signal_connect (toggle, "toggled",
|
|
|
|
G_CALLBACK (gimp_toggle_button_update),
|
|
|
|
value_pointer);
|
|
|
|
|
|
|
|
return toggle;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
file_gif_spin_button_int_init (GtkBuilder *builder,
|
|
|
|
const gchar *name,
|
|
|
|
int initial_value,
|
|
|
|
int *value_pointer)
|
|
|
|
{
|
|
|
|
GtkWidget *spin_button = NULL;
|
|
|
|
GtkAdjustment *adjustment = NULL;
|
|
|
|
|
|
|
|
spin_button = GTK_WIDGET (gtk_builder_get_object (builder, name));
|
|
|
|
|
|
|
|
adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spin_button));
|
|
|
|
gtk_adjustment_set_value (adjustment, initial_value);
|
|
|
|
g_signal_connect (adjustment, "value-changed",
|
|
|
|
G_CALLBACK (gimp_int_adjustment_update),
|
|
|
|
&gsvals.default_delay);
|
|
|
|
|
|
|
|
return spin_button;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
file_gif_combo_box_int_update_value (GtkComboBox *combo,
|
|
|
|
gint *value)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
|
|
|
|
{
|
|
|
|
gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)),
|
|
|
|
&iter,
|
|
|
|
DISPOSE_STORE_VALUE_COLUMN, value,
|
|
|
|
-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
file_gif_combo_box_int_init (GtkBuilder *builder,
|
|
|
|
const gchar *name,
|
|
|
|
int initial_value,
|
|
|
|
int *value_pointer,
|
|
|
|
const gchar *first_label,
|
|
|
|
gint first_value,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
GtkWidget *combo = NULL;
|
|
|
|
GtkListStore *store = NULL;
|
|
|
|
const gchar *label = NULL;
|
|
|
|
gint value = 0;
|
|
|
|
GtkTreeIter iter = { 0, };
|
2010-02-18 02:09:58 +08:00
|
|
|
va_list values;
|
2010-02-13 19:23:39 +08:00
|
|
|
|
|
|
|
combo = GTK_WIDGET (gtk_builder_get_object (builder, name));
|
|
|
|
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)));
|
|
|
|
|
|
|
|
/* Populate */
|
|
|
|
va_start (values, first_value);
|
|
|
|
for (label = first_label, value = first_value;
|
|
|
|
label;
|
|
|
|
label = va_arg (values, const gchar *), value = va_arg (values, gint))
|
|
|
|
{
|
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter,
|
|
|
|
DISPOSE_STORE_VALUE_COLUMN, value,
|
|
|
|
DISPOSE_STORE_LABEL_COLUMN, label,
|
|
|
|
-1);
|
|
|
|
}
|
|
|
|
va_end (values);
|
|
|
|
|
|
|
|
/* Set initial value */
|
|
|
|
gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store),
|
|
|
|
&iter,
|
|
|
|
NULL,
|
|
|
|
initial_value);
|
|
|
|
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
|
|
|
|
|
|
|
|
/* Arrange update of value */
|
|
|
|
g_signal_connect (combo, "changed",
|
|
|
|
G_CALLBACK (file_gif_combo_box_int_update_value),
|
|
|
|
value_pointer);
|
|
|
|
|
|
|
|
return combo;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
static gint
|
2000-01-07 06:26:10 +08:00
|
|
|
save_dialog (gint32 image_ID)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2010-02-13 19:23:39 +08:00
|
|
|
GtkBuilder *builder = NULL;
|
|
|
|
gchar *ui_file = NULL;
|
|
|
|
GError *error = NULL;
|
2005-09-10 02:07:31 +08:00
|
|
|
GtkWidget *dialog;
|
2001-09-10 03:54:37 +08:00
|
|
|
GtkWidget *text_view;
|
2003-11-06 23:27:05 +08:00
|
|
|
GtkTextBuffer *text_buffer;
|
2010-02-16 05:47:19 +08:00
|
|
|
GtkWidget *toggle;
|
2001-09-10 03:54:37 +08:00
|
|
|
GtkWidget *frame;
|
|
|
|
GimpParasite *GIF2_CMNT;
|
2003-11-06 23:27:05 +08:00
|
|
|
gint32 nlayers;
|
2010-01-07 01:51:22 +08:00
|
|
|
gboolean animation_supported = FALSE;
|
2003-11-06 23:27:05 +08:00
|
|
|
gboolean run;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gimp_image_get_layers (image_ID, &nlayers);
|
2010-01-07 01:51:22 +08:00
|
|
|
animation_supported = nlayers > 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2009-07-21 20:10:29 +08:00
|
|
|
dialog = gimp_export_dialog_new (_("GIF"), PLUG_IN_BINARY, SAVE_PROC);
|
2005-09-10 02:07:31 +08:00
|
|
|
|
2010-02-13 19:23:39 +08:00
|
|
|
/* GtkBuilder init */
|
|
|
|
builder = gtk_builder_new ();
|
|
|
|
ui_file = g_build_filename (gimp_data_directory (),
|
|
|
|
"ui/plug-ins/plug-in-file-gif.ui",
|
|
|
|
NULL);
|
|
|
|
if (! gtk_builder_add_from_file (builder, ui_file, &error))
|
|
|
|
g_printerr (_("Error loading UI file '%s':\n%s"),
|
|
|
|
ui_file, error ? error->message : "???");
|
|
|
|
g_free (ui_file);
|
|
|
|
|
|
|
|
/* Main vbox */
|
2011-03-04 17:44:58 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (gimp_export_dialog_get_content_area (dialog)),
|
|
|
|
GTK_WIDGET (gtk_builder_get_object (builder, "main-vbox")),
|
|
|
|
TRUE, TRUE, 0);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* regular gif parameter settings */
|
2010-02-13 19:23:39 +08:00
|
|
|
file_gif_toggle_button_init (builder, "interlace",
|
|
|
|
gsvals.interlace, &gsvals.interlace);
|
|
|
|
file_gif_toggle_button_init (builder, "save-comment",
|
|
|
|
gsvals.save_comment, &gsvals.save_comment);
|
2010-02-16 05:47:19 +08:00
|
|
|
file_gif_toggle_button_init (builder, "as-animation",
|
|
|
|
gsvals.as_animation, &gsvals.as_animation);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2010-02-13 19:23:39 +08:00
|
|
|
text_view = GTK_WIDGET (gtk_builder_get_object (builder, "comment"));
|
|
|
|
text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
|
1999-01-24 02:49:52 +08:00
|
|
|
|
2004-11-23 22:28:43 +08:00
|
|
|
if (globalcomment)
|
|
|
|
g_free (globalcomment);
|
|
|
|
|
2011-03-08 20:19:21 +08:00
|
|
|
GIF2_CMNT = gimp_image_get_parasite (image_ID, "gimp-comment");
|
2000-01-26 01:46:56 +08:00
|
|
|
if (GIF2_CMNT)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
|
|
|
globalcomment = g_strndup (gimp_parasite_data (GIF2_CMNT),
|
|
|
|
gimp_parasite_data_size (GIF2_CMNT));
|
|
|
|
gimp_parasite_free (GIF2_CMNT);
|
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
else
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
|
|
|
globalcomment = gimp_get_default_comment ();
|
|
|
|
}
|
1999-08-30 01:43:35 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
if (globalcomment)
|
2001-09-10 03:54:37 +08:00
|
|
|
gtk_text_buffer_set_text (text_buffer, globalcomment, -1);
|
2001-12-29 21:26:29 +08:00
|
|
|
|
2003-01-07 14:16:02 +08:00
|
|
|
g_signal_connect (text_buffer, "changed",
|
2001-09-10 03:54:37 +08:00
|
|
|
G_CALLBACK (comment_entry_callback),
|
|
|
|
NULL);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* additional animated gif parameter settings */
|
2010-02-13 19:23:39 +08:00
|
|
|
file_gif_toggle_button_init (builder, "loop-forever",
|
|
|
|
gsvals.loop, &gsvals.loop);
|
2001-12-29 21:26:29 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* default_delay entry field */
|
2010-02-13 19:23:39 +08:00
|
|
|
file_gif_spin_button_int_init (builder, "delay-spin",
|
|
|
|
gsvals.default_delay, &gsvals.default_delay);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* Disposal selector */
|
2010-02-13 19:23:39 +08:00
|
|
|
file_gif_combo_box_int_init (builder, "dispose-combo",
|
|
|
|
gsvals.default_dispose, &gsvals.default_dispose,
|
|
|
|
_("I don't care"),
|
|
|
|
DISPOSE_UNSPECIFIED,
|
|
|
|
_("Cumulative layers (combine)"),
|
|
|
|
DISPOSE_COMBINE,
|
|
|
|
_("One frame per layer (replace)"),
|
|
|
|
DISPOSE_REPLACE,
|
|
|
|
NULL);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-08-30 02:45:02 +08:00
|
|
|
/* The "Always use default values" toggles */
|
2010-02-13 19:23:39 +08:00
|
|
|
file_gif_toggle_button_init (builder, "use-default-delay",
|
|
|
|
gsvals.always_use_default_delay,
|
|
|
|
&gsvals.always_use_default_delay);
|
|
|
|
file_gif_toggle_button_init (builder, "use-default-dispose",
|
|
|
|
gsvals.always_use_default_dispose,
|
|
|
|
&gsvals.always_use_default_dispose);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2010-02-16 05:47:19 +08:00
|
|
|
frame = GTK_WIDGET (gtk_builder_get_object (builder, "animation-frame"));
|
|
|
|
toggle = GTK_WIDGET (gtk_builder_get_object (builder, "as-animation"));
|
|
|
|
gtk_widget_set_sensitive (toggle, animation_supported);
|
|
|
|
if (! animation_supported)
|
|
|
|
gimp_help_set_help_data (toggle,
|
|
|
|
_("You can only export as animation when the "
|
|
|
|
"image has more than one layer. The image "
|
|
|
|
"you are trying to export only has one "
|
|
|
|
"layer."),
|
|
|
|
NULL);
|
2011-05-11 17:41:26 +08:00
|
|
|
|
|
|
|
g_object_bind_property (toggle, "active",
|
|
|
|
frame, "sensitive",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_destroy (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return run;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-04-13 19:16:13 +08:00
|
|
|
colors_to_bpp (int colors)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2013-05-28 04:55:34 +08:00
|
|
|
gint bpp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (colors <= 2)
|
|
|
|
bpp = 1;
|
|
|
|
else if (colors <= 4)
|
|
|
|
bpp = 2;
|
|
|
|
else if (colors <= 8)
|
|
|
|
bpp = 3;
|
|
|
|
else if (colors <= 16)
|
|
|
|
bpp = 4;
|
|
|
|
else if (colors <= 32)
|
|
|
|
bpp = 5;
|
|
|
|
else if (colors <= 64)
|
|
|
|
bpp = 6;
|
|
|
|
else if (colors <= 128)
|
|
|
|
bpp = 7;
|
|
|
|
else if (colors <= 256)
|
|
|
|
bpp = 8;
|
|
|
|
else
|
|
|
|
{
|
2013-06-07 05:26:16 +08:00
|
|
|
g_warning ("GIF: colors_to_bpp - Eep! too many colors: %d\n", colors);
|
1997-11-25 06:05:25 +08:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bpp;
|
|
|
|
}
|
|
|
|
|
1998-09-16 04:34:30 +08:00
|
|
|
static int
|
2006-04-13 19:16:13 +08:00
|
|
|
bpp_to_colors (int bpp)
|
1998-09-16 04:34:30 +08:00
|
|
|
{
|
2013-05-28 04:55:34 +08:00
|
|
|
gint colors;
|
1998-09-16 04:34:30 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
if (bpp > 8)
|
1998-09-16 04:34:30 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
g_warning ("GIF: bpp_to_colors - Eep! bpp==%d !\n", bpp);
|
1998-09-16 04:34:30 +08:00
|
|
|
return 256;
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1998-09-16 04:34:30 +08:00
|
|
|
colors = 1 << bpp;
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
return colors;
|
1998-09-16 04:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
static int
|
2006-04-13 19:16:13 +08:00
|
|
|
get_pixel (int x,
|
|
|
|
int y)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
return *(pixels + (rowstride * (long) y) + (long) x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* GIFENCODE.C - GIF Image compression interface
|
|
|
|
*
|
|
|
|
* GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
|
2006-04-13 19:16:13 +08:00
|
|
|
* BitsPerPixel, Red, Green, Blue, get_pixel )
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static gint Width, Height;
|
|
|
|
static gint curx, cury;
|
|
|
|
static glong CountDown;
|
|
|
|
static gint Pass = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bump the 'curx' and 'cury' to point to the next pixel
|
|
|
|
*/
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
bump_pixel (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Bump the current X position
|
|
|
|
*/
|
1998-10-10 02:01:35 +08:00
|
|
|
curx++;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are at the end of a scan line, set curx back to the beginning
|
|
|
|
* If we are interlaced, bump the cury to the appropriate spot,
|
|
|
|
* otherwise, just increment it.
|
|
|
|
*/
|
|
|
|
if (curx == Width)
|
|
|
|
{
|
2003-06-13 22:37:00 +08:00
|
|
|
cur_progress++;
|
2006-04-04 22:03:00 +08:00
|
|
|
|
2008-03-20 17:29:04 +08:00
|
|
|
if ((cur_progress % 20) == 0)
|
2006-04-04 22:03:00 +08:00
|
|
|
gimp_progress_update ((gdouble) cur_progress / (gdouble) max_progress);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
curx = 0;
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
if (! Interlace)
|
|
|
|
++cury;
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
switch (Pass)
|
|
|
|
{
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
cury += 8;
|
|
|
|
if (cury >= Height)
|
|
|
|
{
|
|
|
|
Pass++;
|
|
|
|
cury = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
cury += 8;
|
|
|
|
if (cury >= Height)
|
|
|
|
{
|
|
|
|
Pass++;
|
|
|
|
cury = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
cury += 4;
|
|
|
|
if (cury >= Height)
|
|
|
|
{
|
|
|
|
Pass++;
|
|
|
|
cury = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
cury += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the next pixel from the image
|
|
|
|
*/
|
|
|
|
static int
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_next_pixel (ifunptr getpixel)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (CountDown == 0)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
--CountDown;
|
|
|
|
|
|
|
|
r = (*getpixel) (curx, cury);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
bump_pixel ();
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public */
|
|
|
|
|
|
|
|
static void
|
2008-03-20 17:29:04 +08:00
|
|
|
gif_encode_header (FILE *fp,
|
|
|
|
gboolean gif89,
|
|
|
|
int GWidth,
|
|
|
|
int GHeight,
|
|
|
|
int Background,
|
|
|
|
int BitsPerPixel,
|
|
|
|
int Red[],
|
|
|
|
int Green[],
|
|
|
|
int Blue[],
|
|
|
|
ifunptr get_pixel)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
int B;
|
|
|
|
int RWidth, RHeight;
|
|
|
|
int Resolution;
|
|
|
|
int ColorMapSize;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ColorMapSize = 1 << BitsPerPixel;
|
|
|
|
|
|
|
|
RWidth = Width = GWidth;
|
|
|
|
RHeight = Height = GHeight;
|
|
|
|
|
|
|
|
Resolution = BitsPerPixel;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate number of bits we are expecting
|
|
|
|
*/
|
|
|
|
CountDown = (long) Width *(long) Height;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Indicate which pass we are on (if interlace)
|
|
|
|
*/
|
|
|
|
Pass = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the current x and y position
|
|
|
|
*/
|
|
|
|
curx = cury = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the Magic header
|
|
|
|
*/
|
|
|
|
fwrite (gif89 ? "GIF89a" : "GIF87a", 1, 6, fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the screen width and height
|
|
|
|
*/
|
2006-04-13 19:16:13 +08:00
|
|
|
put_word (RWidth, fp);
|
|
|
|
put_word (RHeight, fp);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
2013-06-07 05:26:16 +08:00
|
|
|
* Indicate that there is a global color map
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2006-04-13 19:16:13 +08:00
|
|
|
B = 0x80; /* Yes, there is a color map */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* OR in the resolution
|
|
|
|
*/
|
|
|
|
B |= (Resolution - 1) << 5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OR in the Bits per Pixel
|
|
|
|
*/
|
|
|
|
B |= (BitsPerPixel - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write it out
|
|
|
|
*/
|
|
|
|
fputc (B, fp);
|
|
|
|
|
|
|
|
/*
|
2013-06-07 05:26:16 +08:00
|
|
|
* Write out the Background color
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
fputc (Background, fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Byte of 0's (future expansion)
|
|
|
|
*/
|
|
|
|
fputc (0, fp);
|
|
|
|
|
|
|
|
/*
|
2013-06-07 05:26:16 +08:00
|
|
|
* Write out the Global Color Map
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
1998-10-10 02:01:35 +08:00
|
|
|
for (i = 0; i < ColorMapSize; i++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
fputc (Red[i], fp);
|
|
|
|
fputc (Green[i], fp);
|
|
|
|
fputc (Blue[i], fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_graphic_control_ext (FILE *fp,
|
|
|
|
int Disposal,
|
|
|
|
int Delay89,
|
|
|
|
int NumFramesInImage,
|
|
|
|
int GWidth,
|
|
|
|
int GHeight,
|
|
|
|
int Transparent,
|
|
|
|
int BitsPerPixel,
|
|
|
|
ifunptr get_pixel)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2011-10-03 15:19:06 +08:00
|
|
|
Width = GWidth;
|
|
|
|
Height = GHeight;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate number of bits we are expecting
|
|
|
|
*/
|
|
|
|
CountDown = (long) Width *(long) Height;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Indicate which pass we are on (if interlace)
|
|
|
|
*/
|
|
|
|
Pass = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the current x and y position
|
|
|
|
*/
|
|
|
|
curx = cury = 0;
|
|
|
|
|
|
|
|
/*
|
2013-06-07 05:26:16 +08:00
|
|
|
* Write out extension for transparent color index, if necessary.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
if ( (Transparent >= 0) || (NumFramesInImage > 1) )
|
|
|
|
{
|
|
|
|
/* Extension Introducer - fixed. */
|
|
|
|
fputc ('!', fp);
|
|
|
|
/* Graphic Control Label - fixed. */
|
|
|
|
fputc (0xf9, fp);
|
|
|
|
/* Block Size - fixed. */
|
|
|
|
fputc (4, fp);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Packed Fields - XXXdddut (d=disposal, u=userInput, t=transFlag) */
|
|
|
|
/* s8421 */
|
|
|
|
fputc ( ((Transparent >= 0) ? 0x01 : 0x00) /* TRANSPARENCY */
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
/* DISPOSAL */
|
|
|
|
| ((NumFramesInImage > 1) ? (Disposal << 2) : 0x00 ),
|
|
|
|
/* 0x03 or 0x01 build frames cumulatively */
|
|
|
|
/* 0x02 clears frame before drawing */
|
|
|
|
/* 0x00 'don't care' */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
fp);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
fputc (Delay89 & 255, fp);
|
2006-04-13 19:16:13 +08:00
|
|
|
fputc ((Delay89 >> 8) & 255, fp);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
fputc (Transparent, fp);
|
|
|
|
fputc (0, fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_image_data (FILE *fp,
|
|
|
|
int GWidth,
|
|
|
|
int GHeight,
|
|
|
|
int GInterlace,
|
|
|
|
int BitsPerPixel,
|
|
|
|
ifunptr get_pixel,
|
|
|
|
gint offset_x,
|
|
|
|
gint offset_y)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
int LeftOfs, TopOfs;
|
|
|
|
int InitCodeSize;
|
|
|
|
|
|
|
|
Interlace = GInterlace;
|
|
|
|
|
2011-10-03 21:26:14 +08:00
|
|
|
Width = GWidth;
|
|
|
|
Height = GHeight;
|
1997-11-25 06:05:25 +08:00
|
|
|
LeftOfs = (int) offset_x;
|
|
|
|
TopOfs = (int) offset_y;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate number of bits we are expecting
|
|
|
|
*/
|
2006-04-13 19:16:13 +08:00
|
|
|
CountDown = (long) Width * (long) Height;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Indicate which pass we are on (if interlace)
|
|
|
|
*/
|
|
|
|
Pass = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The initial code size
|
|
|
|
*/
|
|
|
|
if (BitsPerPixel <= 1)
|
|
|
|
InitCodeSize = 2;
|
|
|
|
else
|
|
|
|
InitCodeSize = BitsPerPixel;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the current x and y position
|
|
|
|
*/
|
|
|
|
curx = cury = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write an Image separator
|
|
|
|
*/
|
|
|
|
fputc (',', fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the Image header
|
|
|
|
*/
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
put_word (LeftOfs, fp);
|
|
|
|
put_word (TopOfs, fp);
|
|
|
|
put_word (Width, fp);
|
|
|
|
put_word (Height, fp);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out whether or not the image is interlaced
|
|
|
|
*/
|
|
|
|
if (Interlace)
|
|
|
|
fputc (0x40, fp);
|
|
|
|
else
|
|
|
|
fputc (0x00, fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the initial code size
|
|
|
|
*/
|
|
|
|
fputc (InitCodeSize, fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go and actually compress the data
|
|
|
|
*/
|
2006-04-13 19:16:13 +08:00
|
|
|
compress (InitCodeSize + 1, fp, get_pixel);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out a Zero-length packet (to end the series)
|
|
|
|
*/
|
|
|
|
fputc (0, fp);
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
#if 0
|
1997-11-25 06:05:25 +08:00
|
|
|
/***************************/
|
|
|
|
Interlace = GInterlace;
|
2011-10-03 21:26:14 +08:00
|
|
|
Width = GWidth;
|
|
|
|
Height = GHeight;
|
1997-11-25 06:05:25 +08:00
|
|
|
LeftOfs = TopOfs = 0;
|
|
|
|
|
|
|
|
CountDown = (long) Width *(long) Height;
|
|
|
|
Pass = 0;
|
|
|
|
/*
|
|
|
|
* The initial code size
|
|
|
|
*/
|
|
|
|
if (BitsPerPixel <= 1)
|
|
|
|
InitCodeSize = 2;
|
|
|
|
else
|
|
|
|
InitCodeSize = BitsPerPixel;
|
|
|
|
/*
|
|
|
|
* Set up the current x and y position
|
|
|
|
*/
|
|
|
|
curx = cury = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-03-20 17:29:04 +08:00
|
|
|
gif_encode_close (FILE *fp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Write the GIF file terminator
|
|
|
|
*/
|
|
|
|
fputc (';', fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And close the file
|
|
|
|
*/
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-03-20 17:29:04 +08:00
|
|
|
gif_encode_loop_ext (FILE *fp,
|
|
|
|
guint num_loops)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
fputc(0x21, fp);
|
|
|
|
fputc(0xff, fp);
|
|
|
|
fputc(0x0b, fp);
|
|
|
|
fputs("NETSCAPE2.0", fp);
|
|
|
|
fputc(0x03, fp);
|
|
|
|
fputc(0x01, fp);
|
|
|
|
put_word(num_loops, fp);
|
|
|
|
fputc(0x00, fp);
|
|
|
|
|
|
|
|
/* NOTE: num_loops == 0 means 'loop infinitely' */
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
gif_encode_comment_ext (FILE *fp,
|
|
|
|
const gchar *comment)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-11-20 10:20:04 +08:00
|
|
|
if (!comment || !*comment)
|
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
if (strlen (comment) > 240)
|
1999-01-24 02:49:52 +08:00
|
|
|
{
|
2003-06-17 01:51:03 +08:00
|
|
|
g_printerr ("GIF: warning:"
|
|
|
|
"comment too large - comment block not written.\n");
|
1999-01-24 02:49:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-07-04 01:45:12 +08:00
|
|
|
fputc (0x21, fp);
|
|
|
|
fputc (0xfe, fp);
|
|
|
|
fputc (strlen (comment), fp);
|
|
|
|
fputs (comment, fp);
|
|
|
|
fputc (0x00, fp);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out a word to the GIF file
|
|
|
|
*/
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
put_word (int w,
|
|
|
|
FILE *fp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
fputc (w & 0xff, fp);
|
|
|
|
fputc ((w / 256) & 0xff, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* GIFCOMPR.C - GIF Image compression routines
|
|
|
|
*
|
|
|
|
* Lempel-Ziv compression based on 'compress'. GIF modifications by
|
|
|
|
* David Rowley (mgardi@watdcsu.waterloo.edu)
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* General DEFINEs
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GIF_BITS 12
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
#define HSIZE 5003 /* 80% occupancy */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GIF Image compression - modified 'compress'
|
|
|
|
*
|
|
|
|
* Based on: compress.c - File compression ala IEEE Computer, June 1984.
|
|
|
|
*
|
|
|
|
* By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
|
|
|
|
* Jim McKie (decvax!mcvax!jim)
|
|
|
|
* Steve Davies (decvax!vax135!petsd!peora!srd)
|
|
|
|
* Ken Turkowski (decvax!decwrl!turtlevax!ken)
|
|
|
|
* James A. Woods (decvax!ihnp4!ames!jaw)
|
|
|
|
* Joe Orost (decvax!vax135!petsd!joe)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
static int n_bits; /* number of bits/code */
|
|
|
|
static int maxbits = GIF_BITS; /* user settable max # bits/code */
|
2013-05-28 04:55:34 +08:00
|
|
|
static gint maxcode; /* maximum code, given n_bits */
|
|
|
|
static gint maxmaxcode = (gint) 1 << GIF_BITS; /* should NEVER generate this code */
|
2006-04-13 19:16:13 +08:00
|
|
|
#ifdef COMPATIBLE /* But wrong! */
|
2013-05-28 04:55:34 +08:00
|
|
|
#define MAXCODE(Mn_bits) ((gint) 1 << (Mn_bits) - 1)
|
1997-11-25 06:05:25 +08:00
|
|
|
#else /*COMPATIBLE */
|
2013-05-28 04:55:34 +08:00
|
|
|
#define MAXCODE(Mn_bits) (((gint) 1 << (Mn_bits)) - 1)
|
1997-11-25 06:05:25 +08:00
|
|
|
#endif /*COMPATIBLE */
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static glong htab[HSIZE];
|
1997-11-25 06:05:25 +08:00
|
|
|
static unsigned short codetab[HSIZE];
|
|
|
|
#define HashTabOf(i) htab[i]
|
|
|
|
#define CodeTabOf(i) codetab[i]
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static const gint hsize = HSIZE; /* the original reason for this being
|
|
|
|
variable was "for dynamic table sizing",
|
|
|
|
but since it was never actually changed
|
|
|
|
I made it const --Adam. */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static gint free_ent = 0; /* first unused entry */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* block compression parameters -- after all codes are used up,
|
|
|
|
* and compression rate changes, start over.
|
|
|
|
*/
|
|
|
|
static int clear_flg = 0;
|
|
|
|
|
|
|
|
static int offset;
|
2006-04-13 19:16:13 +08:00
|
|
|
static long int in_count = 1; /* length of input */
|
|
|
|
static long int out_count = 0; /* # of codes output (for debugging) */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* compress stdin to stdout
|
|
|
|
*
|
|
|
|
* Algorithm: use open addressing double hashing (no chaining) on the
|
|
|
|
* prefix code / next character combination. We do a variant of Knuth's
|
|
|
|
* algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
|
|
|
|
* secondary probe. Here, the modular division first probe is gives way
|
|
|
|
* to a faster exclusive-or manipulation. Also do block compression with
|
|
|
|
* an adaptive reset, whereby the code table is cleared when the compression
|
|
|
|
* ratio decreases, but after the table fills. The variable-length output
|
|
|
|
* codes are re-sized at this point, and a special CLEAR code is generated
|
|
|
|
* for the decompressor. Late addition: construct the table according to
|
|
|
|
* file size for noticeable speed improvement on small files. Please direct
|
|
|
|
* questions about this implementation to ames!jaw.
|
|
|
|
*/
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static gint g_init_bits;
|
1997-11-25 06:05:25 +08:00
|
|
|
static FILE *g_outfile;
|
|
|
|
|
|
|
|
static int ClearCode;
|
|
|
|
static int EOFCode;
|
|
|
|
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static gulong cur_accum;
|
|
|
|
static gint cur_bits;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
static gulong masks[] =
|
|
|
|
{
|
|
|
|
0x0000, 0x0001, 0x0003, 0x0007,
|
|
|
|
0x000F, 0x001F, 0x003F, 0x007F,
|
|
|
|
0x00FF, 0x01FF, 0x03FF, 0x07FF,
|
|
|
|
0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
|
|
|
|
0xFFFF
|
|
|
|
};
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
compress (int init_bits,
|
2006-04-13 19:16:13 +08:00
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue)
|
2003-07-29 14:52:41 +08:00
|
|
|
{
|
2013-05-28 04:36:03 +08:00
|
|
|
if (FALSE)
|
|
|
|
no_compress (init_bits, outfile, ReadValue);
|
|
|
|
else if (FALSE)
|
|
|
|
rle_compress (init_bits, outfile, ReadValue);
|
|
|
|
else
|
|
|
|
normal_compress (init_bits, outfile, ReadValue);
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
no_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue)
|
2003-07-29 14:52:41 +08:00
|
|
|
{
|
2013-05-30 13:26:24 +08:00
|
|
|
long fcode;
|
|
|
|
gint i /* = 0 */ ;
|
|
|
|
int c;
|
|
|
|
gint ent;
|
|
|
|
gint hsize_reg;
|
|
|
|
int hshift;
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the globals: g_init_bits - initial number of bits
|
|
|
|
* g_outfile - pointer to output file
|
|
|
|
*/
|
|
|
|
g_init_bits = init_bits;
|
|
|
|
g_outfile = outfile;
|
|
|
|
|
|
|
|
cur_bits = 0;
|
|
|
|
cur_accum = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the necessary values
|
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
out_count = 0;
|
|
|
|
clear_flg = 0;
|
|
|
|
in_count = 1;
|
|
|
|
|
|
|
|
ClearCode = (1 << (init_bits - 1));
|
|
|
|
EOFCode = ClearCode + 1;
|
|
|
|
free_ent = ClearCode + 2;
|
|
|
|
|
|
|
|
|
|
|
|
/* Had some problems here... should be okay now. --Adam */
|
|
|
|
n_bits = g_init_bits;
|
|
|
|
maxcode = MAXCODE (n_bits);
|
|
|
|
|
|
|
|
|
|
|
|
char_init ();
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
ent = gif_next_pixel (ReadValue);
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
hshift = 0;
|
|
|
|
for (fcode = (long) hsize; fcode < 65536L; fcode *= 2L)
|
|
|
|
++hshift;
|
2006-04-13 19:16:13 +08:00
|
|
|
hshift = 8 - hshift; /* set hash code range bound */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
hsize_reg = hsize;
|
2013-05-28 04:55:34 +08:00
|
|
|
cl_hash ((glong) hsize_reg); /* clear hash table */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ClearCode);
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((c = gif_next_pixel (ReadValue)) != EOF)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
2003-07-29 14:52:41 +08:00
|
|
|
++in_count;
|
|
|
|
|
|
|
|
fcode = (long) (((long) c << maxbits) + ent);
|
2013-05-28 04:55:34 +08:00
|
|
|
i = (((gint) c << hshift) ^ ent); /* xor hashing */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
2003-07-29 14:52:41 +08:00
|
|
|
++out_count;
|
|
|
|
ent = c;
|
2013-05-28 04:32:08 +08:00
|
|
|
|
2003-07-29 14:52:41 +08:00
|
|
|
if (free_ent < maxmaxcode)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
CodeTabOf (i) = free_ent++; /* code -> hashtable */
|
|
|
|
HashTabOf (i) = fcode;
|
|
|
|
}
|
2003-07-29 14:52:41 +08:00
|
|
|
else
|
2006-04-13 19:16:13 +08:00
|
|
|
cl_block ();
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put out the final code.
|
|
|
|
*/
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
2003-07-29 14:52:41 +08:00
|
|
|
++out_count;
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) EOFCode);
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
rle_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue)
|
2003-07-29 14:52:41 +08:00
|
|
|
{
|
2013-05-30 13:26:24 +08:00
|
|
|
long fcode;
|
|
|
|
gint i /* = 0 */ ;
|
|
|
|
int c, last;
|
|
|
|
gint ent;
|
|
|
|
gint disp;
|
|
|
|
gint hsize_reg;
|
|
|
|
int hshift;
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the globals: g_init_bits - initial number of bits
|
|
|
|
* g_outfile - pointer to output file
|
|
|
|
*/
|
|
|
|
g_init_bits = init_bits;
|
|
|
|
g_outfile = outfile;
|
|
|
|
|
|
|
|
cur_bits = 0;
|
|
|
|
cur_accum = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the necessary values
|
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
out_count = 0;
|
|
|
|
clear_flg = 0;
|
|
|
|
in_count = 1;
|
|
|
|
|
|
|
|
ClearCode = (1 << (init_bits - 1));
|
|
|
|
EOFCode = ClearCode + 1;
|
|
|
|
free_ent = ClearCode + 2;
|
|
|
|
|
|
|
|
|
|
|
|
/* Had some problems here... should be okay now. --Adam */
|
|
|
|
n_bits = g_init_bits;
|
|
|
|
maxcode = MAXCODE (n_bits);
|
|
|
|
|
|
|
|
|
|
|
|
char_init ();
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
last = ent = gif_next_pixel (ReadValue);
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
hshift = 0;
|
|
|
|
for (fcode = (long) hsize; fcode < 65536L; fcode *= 2L)
|
|
|
|
++hshift;
|
2006-04-13 19:16:13 +08:00
|
|
|
hshift = 8 - hshift; /* set hash code range bound */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
hsize_reg = hsize;
|
2013-05-28 04:55:34 +08:00
|
|
|
cl_hash ((glong) hsize_reg); /* clear hash table */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ClearCode);
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((c = gif_next_pixel (ReadValue)) != EOF)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
2003-07-29 14:52:41 +08:00
|
|
|
++in_count;
|
|
|
|
|
|
|
|
fcode = (long) (((long) c << maxbits) + ent);
|
2013-05-28 04:55:34 +08:00
|
|
|
i = (((gint) c << hshift) ^ ent); /* xor hashing */
|
2003-07-29 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (last == c) {
|
|
|
|
if (HashTabOf (i) == fcode)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
ent = CodeTabOf (i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if ((long) HashTabOf (i) < 0) /* empty slot */
|
|
|
|
goto nomatch;
|
|
|
|
disp = hsize_reg - i; /* secondary hash (after G. Knott) */
|
2003-07-29 14:52:41 +08:00
|
|
|
if (i == 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
disp = 1;
|
2003-07-29 14:52:41 +08:00
|
|
|
probe:
|
|
|
|
if ((i -= disp) < 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
i += hsize_reg;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2003-07-29 14:52:41 +08:00
|
|
|
if (HashTabOf (i) == fcode)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
ent = CodeTabOf (i);
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-29 14:52:41 +08:00
|
|
|
if ((long) HashTabOf (i) > 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
goto probe;
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
nomatch:
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
2003-07-29 14:52:41 +08:00
|
|
|
++out_count;
|
|
|
|
last = ent = c;
|
|
|
|
if (free_ent < maxmaxcode)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
CodeTabOf (i) = free_ent++; /* code -> hashtable */
|
|
|
|
HashTabOf (i) = fcode;
|
|
|
|
}
|
2003-07-29 14:52:41 +08:00
|
|
|
else
|
2006-04-13 19:16:13 +08:00
|
|
|
cl_block ();
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put out the final code.
|
|
|
|
*/
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
2003-07-29 14:52:41 +08:00
|
|
|
++out_count;
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) EOFCode);
|
2003-07-29 14:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
normal_compress (int init_bits,
|
|
|
|
FILE *outfile,
|
|
|
|
ifunptr ReadValue)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2013-05-30 13:26:24 +08:00
|
|
|
long fcode;
|
|
|
|
gint i /* = 0 */ ;
|
|
|
|
int c;
|
|
|
|
gint ent;
|
|
|
|
gint disp;
|
|
|
|
gint hsize_reg;
|
|
|
|
int hshift;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the globals: g_init_bits - initial number of bits
|
|
|
|
* g_outfile - pointer to output file
|
|
|
|
*/
|
|
|
|
g_init_bits = init_bits;
|
|
|
|
g_outfile = outfile;
|
|
|
|
|
|
|
|
cur_bits = 0;
|
|
|
|
cur_accum = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the necessary values
|
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
out_count = 0;
|
|
|
|
clear_flg = 0;
|
|
|
|
in_count = 1;
|
|
|
|
|
|
|
|
ClearCode = (1 << (init_bits - 1));
|
|
|
|
EOFCode = ClearCode + 1;
|
|
|
|
free_ent = ClearCode + 2;
|
|
|
|
|
|
|
|
|
|
|
|
/* Had some problems here... should be okay now. --Adam */
|
|
|
|
n_bits = g_init_bits;
|
|
|
|
maxcode = MAXCODE (n_bits);
|
|
|
|
|
|
|
|
|
|
|
|
char_init ();
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
ent = gif_next_pixel (ReadValue);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
hshift = 0;
|
|
|
|
for (fcode = (long) hsize; fcode < 65536L; fcode *= 2L)
|
|
|
|
++hshift;
|
2006-04-13 19:16:13 +08:00
|
|
|
hshift = 8 - hshift; /* set hash code range bound */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
hsize_reg = hsize;
|
2013-05-28 04:55:34 +08:00
|
|
|
cl_hash ((glong) hsize_reg); /* clear hash table */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ClearCode);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
while ((c = gif_next_pixel (ReadValue)) != EOF)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
1997-11-25 06:05:25 +08:00
|
|
|
++in_count;
|
|
|
|
|
|
|
|
fcode = (long) (((long) c << maxbits) + ent);
|
2013-05-28 04:55:34 +08:00
|
|
|
i = (((gint) c << hshift) ^ ent); /* xor hashing */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (HashTabOf (i) == fcode)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
ent = CodeTabOf (i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if ((long) HashTabOf (i) < 0) /* empty slot */
|
|
|
|
goto nomatch;
|
|
|
|
disp = hsize_reg - i; /* secondary hash (after G. Knott) */
|
1997-11-25 06:05:25 +08:00
|
|
|
if (i == 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
disp = 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
probe:
|
|
|
|
if ((i -= disp) < 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
i += hsize_reg;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (HashTabOf (i) == fcode)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
ent = CodeTabOf (i);
|
|
|
|
continue;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
if ((long) HashTabOf (i) > 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
goto probe;
|
1997-11-25 06:05:25 +08:00
|
|
|
nomatch:
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
1997-11-25 06:05:25 +08:00
|
|
|
++out_count;
|
|
|
|
ent = c;
|
|
|
|
if (free_ent < maxmaxcode)
|
2013-05-28 04:32:08 +08:00
|
|
|
{
|
2006-04-13 19:16:13 +08:00
|
|
|
CodeTabOf (i) = free_ent++; /* code -> hashtable */
|
|
|
|
HashTabOf (i) = fcode;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2006-04-13 19:16:13 +08:00
|
|
|
cl_block ();
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-07-29 14:52:41 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/*
|
|
|
|
* Put out the final code.
|
|
|
|
*/
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ent);
|
1997-11-25 06:05:25 +08:00
|
|
|
++out_count;
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) EOFCode);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-07-29 14:52:41 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* TAG( output )
|
|
|
|
*
|
|
|
|
* Output the given code.
|
|
|
|
* Inputs:
|
|
|
|
* code: A n_bits-bit integer. If == -1, then EOF. This assumes
|
|
|
|
* that n_bits =< (long)wordsize - 1.
|
|
|
|
* Outputs:
|
|
|
|
* Outputs code to the file.
|
|
|
|
* Assumptions:
|
|
|
|
* Chars are 8 bits long.
|
|
|
|
* Algorithm:
|
|
|
|
* Maintain a GIF_BITS character long buffer (so that 8 codes will
|
|
|
|
* fit in it exactly). Use the VAX insv instruction to insert each
|
|
|
|
* code in turn. When the buffer fills up empty it and start over.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2013-05-28 04:55:34 +08:00
|
|
|
output (gint code)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
cur_accum &= masks[cur_bits];
|
|
|
|
|
|
|
|
if (cur_bits > 0)
|
|
|
|
cur_accum |= ((long) code << cur_bits);
|
|
|
|
else
|
|
|
|
cur_accum = code;
|
|
|
|
|
|
|
|
cur_bits += n_bits;
|
|
|
|
|
|
|
|
while (cur_bits >= 8)
|
|
|
|
{
|
|
|
|
char_out ((unsigned int) (cur_accum & 0xff));
|
|
|
|
cur_accum >>= 8;
|
|
|
|
cur_bits -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the next entry is going to be too big for the code size,
|
|
|
|
* then increase it, if possible.
|
|
|
|
*/
|
|
|
|
if (free_ent > maxcode || clear_flg)
|
|
|
|
{
|
|
|
|
if (clear_flg)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
maxcode = MAXCODE (n_bits = g_init_bits);
|
|
|
|
clear_flg = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2006-04-13 19:16:13 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
++n_bits;
|
|
|
|
if (n_bits == maxbits)
|
|
|
|
maxcode = maxmaxcode;
|
|
|
|
else
|
|
|
|
maxcode = MAXCODE (n_bits);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (code == EOFCode)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* At EOF, write the rest of the buffer.
|
|
|
|
*/
|
|
|
|
while (cur_bits > 0)
|
2006-04-13 19:16:13 +08:00
|
|
|
{
|
|
|
|
char_out ((unsigned int) (cur_accum & 0xff));
|
|
|
|
cur_accum >>= 8;
|
|
|
|
cur_bits -= 8;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
flush_char ();
|
|
|
|
|
|
|
|
fflush (g_outfile);
|
|
|
|
|
|
|
|
if (ferror (g_outfile))
|
2013-05-28 04:55:34 +08:00
|
|
|
g_message (_("Error writing output file."));
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear out the hash table
|
|
|
|
*/
|
|
|
|
static void
|
2006-04-13 19:16:13 +08:00
|
|
|
cl_block (void) /* table clear for block compress */
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2013-05-28 04:55:34 +08:00
|
|
|
cl_hash ((glong) hsize);
|
1997-11-25 06:05:25 +08:00
|
|
|
free_ent = ClearCode + 2;
|
|
|
|
clear_flg = 1;
|
|
|
|
|
2013-05-28 04:55:34 +08:00
|
|
|
output ((gint) ClearCode);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-05-28 04:55:34 +08:00
|
|
|
cl_hash (glong hsize) /* reset code table */
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
|
2013-05-30 13:26:24 +08:00
|
|
|
glong *htab_p = htab + hsize;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-05-30 13:26:24 +08:00
|
|
|
long i;
|
|
|
|
long m1 = -1;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
i = hsize - 16;
|
|
|
|
do
|
2006-04-13 19:16:13 +08:00
|
|
|
{ /* might use Sys V memset(3) here */
|
1997-11-25 06:05:25 +08:00
|
|
|
*(htab_p - 16) = m1;
|
|
|
|
*(htab_p - 15) = m1;
|
|
|
|
*(htab_p - 14) = m1;
|
|
|
|
*(htab_p - 13) = m1;
|
|
|
|
*(htab_p - 12) = m1;
|
|
|
|
*(htab_p - 11) = m1;
|
|
|
|
*(htab_p - 10) = m1;
|
|
|
|
*(htab_p - 9) = m1;
|
|
|
|
*(htab_p - 8) = m1;
|
|
|
|
*(htab_p - 7) = m1;
|
|
|
|
*(htab_p - 6) = m1;
|
|
|
|
*(htab_p - 5) = m1;
|
|
|
|
*(htab_p - 4) = m1;
|
|
|
|
*(htab_p - 3) = m1;
|
|
|
|
*(htab_p - 2) = m1;
|
|
|
|
*(htab_p - 1) = m1;
|
|
|
|
htab_p -= 16;
|
|
|
|
}
|
|
|
|
while ((i -= 16) >= 0);
|
|
|
|
|
|
|
|
for (i += 16; i > 0; --i)
|
|
|
|
*--htab_p = m1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* GIF Specific routines
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of characters so far in this 'packet'
|
|
|
|
*/
|
|
|
|
static int a_count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the 'byte output' routine
|
|
|
|
*/
|
|
|
|
static void
|
2004-07-22 01:06:18 +08:00
|
|
|
char_init (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
a_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define the storage for the packet accumulator
|
|
|
|
*/
|
|
|
|
static char accum[256];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a character to the end of the current packet, and if it is 254
|
|
|
|
* characters, flush the packet to disk.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
char_out (int c)
|
|
|
|
{
|
|
|
|
accum[a_count++] = c;
|
|
|
|
if (a_count >= 254)
|
|
|
|
flush_char ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush the packet to disk, and reset the accumulator
|
|
|
|
*/
|
|
|
|
static void
|
2000-01-26 01:46:56 +08:00
|
|
|
flush_char (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (a_count > 0)
|
|
|
|
{
|
|
|
|
fputc (a_count, g_outfile);
|
|
|
|
fwrite (accum, 1, a_count, g_outfile);
|
|
|
|
a_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Save interface functions */
|
|
|
|
|
|
|
|
static void
|
2001-09-10 03:54:37 +08:00
|
|
|
comment_entry_callback (GtkTextBuffer *buffer)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-09-10 03:54:37 +08:00
|
|
|
GtkTextIter start_iter;
|
|
|
|
GtkTextIter end_iter;
|
|
|
|
gchar *text;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-09-10 03:54:37 +08:00
|
|
|
gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
|
|
|
|
text = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-01-19 03:53:42 +08:00
|
|
|
#define MAX_COMMENT 240
|
|
|
|
|
|
|
|
if (strlen (text) > MAX_COMMENT)
|
2001-09-18 02:11:15 +08:00
|
|
|
{
|
2012-01-19 03:53:42 +08:00
|
|
|
/* translators: the %d is *always* 240 here */
|
|
|
|
g_message (_("The default comment is limited to %d characters."),
|
|
|
|
MAX_COMMENT);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2012-01-19 03:53:42 +08:00
|
|
|
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, MAX_COMMENT - 1);
|
2001-09-18 02:11:15 +08:00
|
|
|
gtk_text_buffer_get_end_iter (buffer, &end_iter);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2001-09-18 02:11:15 +08:00
|
|
|
/* this calls us recursivaly, but in the else branch
|
|
|
|
*/
|
|
|
|
gtk_text_buffer_delete (buffer, &start_iter, &end_iter);
|
2003-11-06 23:27:05 +08:00
|
|
|
}
|
2001-09-18 02:11:15 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free (globalcomment);
|
|
|
|
globalcomment = g_strdup (text);
|
|
|
|
comment_was_edited = TRUE;
|
|
|
|
}
|
1999-04-26 00:10:43 +08:00
|
|
|
|
2001-09-10 03:54:37 +08:00
|
|
|
g_free (text);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|