2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-24 22:05:25 +00:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
2013-10-03 22:06:24 +02:00
|
|
|
*
|
1997-11-24 22:05:25 +00:00
|
|
|
* PNM reading and writing code Copyright (C) 1996 Erik Nygren
|
|
|
|
*
|
2009-01-17 22:28:01 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-24 22:05:25 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 22:28:01 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-24 22:05:25 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-07-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1997-11-24 22:05:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The pnm reading and writing code was written from scratch by Erik Nygren
|
|
|
|
* (nygren@mit.edu) based on the specifications in the man pages and
|
|
|
|
* does not contain any code from the netpbm or pbmplus distributions.
|
2006-05-28 21:29:59 +00:00
|
|
|
*
|
|
|
|
* 2006: pbm saving written by Martin K Collins (martin@mkcollins.org)
|
2015-01-16 15:37:24 +01:00
|
|
|
* 2015: pfm reading written by Tobias Ellinghaus (me@houz.org)
|
1997-11-24 22:05:25 +00:00
|
|
|
*/
|
|
|
|
|
1999-05-29 01:28:24 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
#include <setjmp.h>
|
2013-10-03 21:44:24 +02:00
|
|
|
#include <stdlib.h>
|
2015-01-16 15:37:24 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <errno.h>
|
2000-01-08 15:23:28 +00:00
|
|
|
|
1999-05-29 01:28:24 +00:00
|
|
|
#include <libgimp/gimp.h>
|
1999-10-14 02:11:52 +00:00
|
|
|
#include <libgimp/gimpui.h>
|
2000-01-08 15:23:28 +00:00
|
|
|
|
2000-01-03 13:01:51 +00:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1999-05-29 01:28:24 +00:00
|
|
|
|
2000-05-01 17:01:18 +00:00
|
|
|
|
2005-08-18 08:57:43 +00:00
|
|
|
#define LOAD_PROC "file-pnm-load"
|
|
|
|
#define PNM_SAVE_PROC "file-pnm-save"
|
2006-05-28 21:29:59 +00:00
|
|
|
#define PBM_SAVE_PROC "file-pbm-save"
|
2005-08-18 08:57:43 +00:00
|
|
|
#define PGM_SAVE_PROC "file-pgm-save"
|
|
|
|
#define PPM_SAVE_PROC "file-ppm-save"
|
2023-01-17 02:33:16 +00:00
|
|
|
#define PAM_SAVE_PROC "file-pam-save"
|
2015-01-20 20:14:07 +05:30
|
|
|
#define PFM_SAVE_PROC "file-pfm-save"
|
2008-08-11 10:06:13 +00:00
|
|
|
#define PLUG_IN_BINARY "file-pnm"
|
2011-04-08 20:31:34 +02:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-pnm"
|
2005-08-18 08:57:43 +00:00
|
|
|
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* Declare local data types
|
|
|
|
*/
|
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
FILE_TYPE_PNM,
|
|
|
|
FILE_TYPE_PBM,
|
|
|
|
FILE_TYPE_PGM,
|
|
|
|
FILE_TYPE_PPM,
|
2023-01-17 02:33:16 +00:00
|
|
|
FILE_TYPE_PAM,
|
2019-11-08 10:15:02 +01:00
|
|
|
FILE_TYPE_PFM
|
|
|
|
} FileType;
|
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
typedef struct _PNMScanner PNMScanner;
|
2013-10-03 22:06:24 +02:00
|
|
|
typedef struct _PNMInfo PNMInfo;
|
|
|
|
typedef struct _PNMRowInfo PNMRowInfo;
|
|
|
|
|
|
|
|
typedef void (* PNMLoaderFunc) (PNMScanner *scanner,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer);
|
|
|
|
typedef gboolean (* PNMSaverowFunc) (PNMRowInfo *info,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 22:06:24 +02:00
|
|
|
GError **error);
|
2013-10-03 19:59:54 +02:00
|
|
|
|
|
|
|
struct _PNMScanner
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
GInputStream *input; /* The input stream of the file being read */
|
|
|
|
gchar cur; /* The current character in the input stream */
|
|
|
|
gint eof; /* Have we reached end of file? */
|
|
|
|
gchar *inbuf; /* Input buffer - initially 0 */
|
|
|
|
gint inbufsize; /* Size of input buffer */
|
|
|
|
gint inbufvalidsize; /* Size of input buffer with valid data */
|
|
|
|
gint inbufpos; /* Position in input buffer */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _PNMInfo
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2013-10-03 22:06:24 +02:00
|
|
|
gint xres, yres; /* The size of the image */
|
2015-01-16 15:37:24 +01:00
|
|
|
gboolean float_format; /* Whether it is a floating point format */
|
2017-02-20 21:46:21 +00:00
|
|
|
gint maxval; /* For integer format image files, the max value
|
2006-02-04 01:02:25 +00:00
|
|
|
* which we need to normalize to */
|
2015-01-16 15:37:24 +01:00
|
|
|
gfloat scale_factor; /* PFM files have a scale factor */
|
2013-10-03 22:06:24 +02:00
|
|
|
gint np; /* Number of image planes (0 for pbm) */
|
|
|
|
gboolean asciibody; /* 1 if ascii body, 0 if raw body */
|
|
|
|
jmp_buf jmpbuf; /* Where to jump to on an error loading */
|
|
|
|
|
|
|
|
PNMLoaderFunc loader; /* Routine to use to load the pnm body */
|
2013-10-03 19:59:54 +02:00
|
|
|
};
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Contains the information needed to write out PNM rows */
|
2013-10-03 22:06:24 +02:00
|
|
|
struct _PNMRowInfo
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2013-10-03 21:44:24 +02:00
|
|
|
GOutputStream *output; /* Output stream */
|
|
|
|
gchar *rowbuf; /* Buffer for writing out rows */
|
|
|
|
gint xres; /* X resolution */
|
|
|
|
gint np; /* Number of planes */
|
2017-02-20 23:12:05 +00:00
|
|
|
gint bpc; /* Bytes per color */
|
2013-10-03 21:44:24 +02:00
|
|
|
guchar *red; /* Colormap red */
|
|
|
|
guchar *grn; /* Colormap green */
|
|
|
|
guchar *blu; /* Colormap blue */
|
2023-01-17 02:33:16 +00:00
|
|
|
guchar *alpha; /* Colormap alpha (PAM only) */
|
2013-10-03 21:44:24 +02:00
|
|
|
gboolean zero_is_black; /* index zero is black (PBM only) */
|
2013-10-03 22:06:24 +02:00
|
|
|
};
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
#define BUFLEN 512 /* The input buffer size for data returned
|
|
|
|
* from the scanner. Note that lines
|
|
|
|
* aren't allowed to be over 256 characters
|
|
|
|
* by the spec anyways so this shouldn't
|
|
|
|
* be an issue. */
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
typedef struct _Pnm Pnm;
|
|
|
|
typedef struct _PnmClass PnmClass;
|
|
|
|
|
|
|
|
struct _Pnm
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _PnmClass
|
|
|
|
{
|
|
|
|
GimpPlugInClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define PNM_TYPE (pnm_get_type ())
|
2023-10-18 18:29:37 +02:00
|
|
|
#define PNM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PNM_TYPE, Pnm))
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
GType pnm_get_type (void) G_GNUC_CONST;
|
|
|
|
|
2023-08-06 02:56:44 +02:00
|
|
|
static GList * pnm_query_procedures (GimpPlugIn *plug_in);
|
|
|
|
static GimpProcedure * pnm_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name);
|
|
|
|
|
|
|
|
static GimpValueArray * pnm_load (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GFile *file,
|
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpMetadataLoadFlags *flags,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data);
|
|
|
|
static GimpValueArray * pnm_save (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
|
|
|
GFile *file,
|
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data);
|
|
|
|
|
|
|
|
static GimpImage * load_image (GFile *file,
|
|
|
|
GError **error);
|
|
|
|
static gint save_image (GFile *file,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
FileType file_type,
|
|
|
|
GObject *config,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
static gboolean save_dialog (GimpProcedure *procedure,
|
|
|
|
GObject *config,
|
|
|
|
GimpImage *image);
|
|
|
|
|
|
|
|
static void process_pam_header (PNMScanner *scan,
|
|
|
|
PNMInfo *info);
|
|
|
|
|
|
|
|
static void pnm_load_ascii (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer);
|
|
|
|
static void pnm_load_raw (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer);
|
|
|
|
static void pnm_load_rawpbm (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer);
|
|
|
|
static void pnm_load_rawpfm (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer);
|
|
|
|
|
|
|
|
static void create_pam_header (const gchar **header_string,
|
|
|
|
PNMRowInfo *rowinfo,
|
|
|
|
PNMSaverowFunc *saverow,
|
|
|
|
GimpImageType drawable_type,
|
|
|
|
GeglBuffer *buffer,
|
|
|
|
const Babl **format,
|
|
|
|
gint *rowbufsize,
|
|
|
|
gint *np,
|
|
|
|
gchar *comment);
|
|
|
|
|
|
|
|
static gboolean pnmsaverow_ascii (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
static gboolean pnmsaverow_raw (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
static gboolean pnmsaverow_raw_pbm (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
static gboolean pnmsaverow_ascii_pbm (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
static gboolean pnmsaverow_ascii_indexed (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
static gboolean pnmsaverow_raw_indexed (PNMRowInfo *ri,
|
|
|
|
guchar *data,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
static PNMScanner * pnmscanner_create (GInputStream *input);
|
|
|
|
static void pnmscanner_destroy (PNMScanner *s);
|
|
|
|
static void pnmscanner_createbuffer (PNMScanner *s,
|
|
|
|
gint bufsize);
|
|
|
|
static void pnmscanner_getchar (PNMScanner *s);
|
|
|
|
static void pnmscanner_eatwhitespace (PNMScanner *s);
|
|
|
|
static void pnmscanner_eatinnerspace (PNMScanner *s);
|
|
|
|
static void pnmscanner_gettoken (PNMScanner *s,
|
|
|
|
gchar *buf,
|
|
|
|
gint bufsize);
|
|
|
|
static void pnmscanner_getsmalltoken (PNMScanner *s,
|
|
|
|
gchar *buf);
|
|
|
|
static void pnmscanner_getheaderline (PNMScanner *s,
|
|
|
|
gchar *buf,
|
|
|
|
gint bufsize);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
#define pnmscanner_eof(s) ((s)->eof)
|
|
|
|
#define pnmscanner_input(s) ((s)->input)
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Checks for a fatal error */
|
2013-10-03 19:59:54 +02:00
|
|
|
#define CHECK_FOR_ERROR(predicate, jmpbuf, ...) \
|
1997-11-24 22:05:25 +00:00
|
|
|
if ((predicate)) \
|
2013-10-03 19:59:54 +02:00
|
|
|
{ g_message (__VA_ARGS__); longjmp ((jmpbuf), 1); }
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
G_DEFINE_TYPE (Pnm, pnm, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (PNM_TYPE)
|
2022-05-26 00:59:36 +02:00
|
|
|
DEFINE_STD_SET_I18N
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
|
2013-10-03 22:06:24 +02:00
|
|
|
static const struct
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2013-10-03 22:06:24 +02:00
|
|
|
gchar name;
|
|
|
|
gint np;
|
|
|
|
gint asciibody;
|
|
|
|
gint maxval;
|
|
|
|
PNMLoaderFunc loader;
|
2000-01-25 17:46:56 +00:00
|
|
|
} pnm_types[] =
|
|
|
|
{
|
2023-01-17 02:33:16 +00:00
|
|
|
{ '1', 0, 1, 1, pnm_load_ascii }, /* ASCII PBM */
|
|
|
|
{ '2', 1, 1, 255, pnm_load_ascii }, /* ASCII PGM */
|
|
|
|
{ '3', 3, 1, 255, pnm_load_ascii }, /* ASCII PPM */
|
|
|
|
{ '4', 0, 0, 1, pnm_load_rawpbm }, /* RAW PBM */
|
|
|
|
{ '5', 1, 0, 255, pnm_load_raw }, /* RAW PGM */
|
|
|
|
{ '6', 3, 0, 255, pnm_load_raw }, /* RAW PPM */
|
2023-01-15 17:14:57 +00:00
|
|
|
{ '7', 4, 0, 65535, pnm_load_raw }, /* RAW PAM */
|
2023-01-17 02:33:16 +00:00
|
|
|
{ 'F', 3, 0, 0, pnm_load_rawpfm }, /* RAW PFM (color) */
|
|
|
|
{ 'f', 1, 0, 0, pnm_load_rawpfm }, /* RAW PFM (grayscale) */
|
|
|
|
{ 0 , 0, 0, 0, NULL }
|
1997-11-24 22:05:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2019-08-24 13:18:49 +02:00
|
|
|
pnm_class_init (PnmClass *klass)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 13:18:49 +02:00
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
|
|
|
|
|
|
|
plug_in_class->query_procedures = pnm_query_procedures;
|
|
|
|
plug_in_class->create_procedure = pnm_create_procedure;
|
2022-05-26 00:59:36 +02:00
|
|
|
plug_in_class->set_i18n = STD_SET_I18N;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-24 13:18:49 +02:00
|
|
|
pnm_init (Pnm *pnm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GList *
|
|
|
|
pnm_query_procedures (GimpPlugIn *plug_in)
|
|
|
|
{
|
|
|
|
GList *list = NULL;
|
|
|
|
|
|
|
|
list = g_list_append (list, g_strdup (LOAD_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (PNM_SAVE_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (PBM_SAVE_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (PGM_SAVE_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (PPM_SAVE_PROC));
|
2023-01-17 02:33:16 +00:00
|
|
|
list = g_list_append (list, g_strdup (PAM_SAVE_PROC));
|
2019-08-24 13:18:49 +02:00
|
|
|
list = g_list_append (list, g_strdup (PFM_SAVE_PROC));
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpProcedure *
|
|
|
|
pnm_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
|
|
|
|
|
|
|
if (! strcmp (name, LOAD_PROC))
|
|
|
|
{
|
2023-08-06 03:21:27 +02:00
|
|
|
procedure = gimp_load_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
pnm_load, NULL, NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PNM Image"));
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Loads files in the PNM file format"),
|
|
|
|
_("This plug-in loads files in the "
|
|
|
|
"various Netpbm portable file formats."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Erik Nygren",
|
|
|
|
"Erik Nygren",
|
|
|
|
"1996");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-anymap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
2023-01-15 17:14:57 +00:00
|
|
|
"pnm,ppm,pgm,pbm,pfm,pam");
|
2019-08-24 13:18:49 +02:00
|
|
|
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"0,string,P1,0,string,P2,0,string,P3,"
|
|
|
|
"0,string,P4,0,string,P5,0,string,P6,"
|
2023-01-15 17:14:57 +00:00
|
|
|
"0,string,P7,0,string,PF,0,string,Pf");
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
|
|
|
else if (! strcmp (name, PNM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, pnm_save,
|
|
|
|
GINT_TO_POINTER (FILE_TYPE_PNM),
|
|
|
|
NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB, GRAY, INDEXED");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PNM image"));
|
2023-04-02 02:55:17 +00:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("PNM"));
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PNM file format"),
|
|
|
|
_("PNM export handles all image types "
|
|
|
|
"without transparency."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Erik Nygren",
|
|
|
|
"Erik Nygren",
|
|
|
|
"1996");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-anymap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"pnm");
|
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
GIMP_PROC_ARG_INT (procedure, "raw",
|
|
|
|
_("Data formatting"),
|
|
|
|
_("TRUE for raw output, FALSE for ascii output"),
|
|
|
|
0, 1, 1,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
|
|
|
else if (! strcmp (name, PBM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, pnm_save,
|
|
|
|
GINT_TO_POINTER (FILE_TYPE_PBM),
|
|
|
|
NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB, GRAY, INDEXED");
|
2023-04-02 02:55:17 +00:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("PBM"));
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PBM image"));
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PBM file format"),
|
|
|
|
_("PBM exporting produces mono images "
|
|
|
|
"without transparency."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Erik Nygren",
|
|
|
|
"Erik Nygren",
|
|
|
|
"1996");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-bitmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"pbm");
|
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
GIMP_PROC_ARG_INT (procedure, "raw",
|
|
|
|
_("Data formatting"),
|
|
|
|
_("TRUE for raw output, FALSE for ascii output"),
|
|
|
|
0, 1, 1,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
|
|
|
else if (! strcmp (name, PGM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, pnm_save,
|
|
|
|
GINT_TO_POINTER (FILE_TYPE_PGM),
|
|
|
|
NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB, GRAY, INDEXED");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PGM image"));
|
2023-04-02 02:55:17 +00:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("PGM"));
|
2019-08-24 13:18:49 +02:00
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PGM file format"),
|
|
|
|
_("PGM exporting produces grayscale "
|
|
|
|
"images without transparency."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Erik Nygren",
|
|
|
|
"Erik Nygren",
|
|
|
|
"1996");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-graymap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"pgm");
|
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
GIMP_PROC_ARG_INT (procedure, "raw",
|
|
|
|
_("Data formatting"),
|
|
|
|
_("TRUE for raw output, FALSE for ascii output"),
|
|
|
|
0, 1, 1,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
|
|
|
else if (! strcmp (name, PPM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, pnm_save,
|
|
|
|
GINT_TO_POINTER (FILE_TYPE_PPM),
|
|
|
|
NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB, GRAY, INDEXED");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PPM image"));
|
2023-04-02 02:55:17 +00:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("PPM"));
|
2019-08-24 13:18:49 +02:00
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PPM file format"),
|
|
|
|
_("PPM export handles RGB images "
|
|
|
|
"without transparency."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Erik Nygren",
|
|
|
|
"Erik Nygren",
|
|
|
|
"1996");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-pixmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"ppm");
|
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
GIMP_PROC_ARG_INT (procedure, "raw",
|
|
|
|
"Data formatting",
|
|
|
|
_("TRUE for raw output, FALSE for ascii output"),
|
|
|
|
0, 1, 1,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
2023-01-17 02:33:16 +00:00
|
|
|
else if (! strcmp (name, PAM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
2023-01-17 02:33:16 +00:00
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2023-07-22 23:49:47 +02:00
|
|
|
FALSE, pnm_save,
|
2023-01-17 02:33:16 +00:00
|
|
|
GINT_TO_POINTER (FILE_TYPE_PAM),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB*, GRAY*, INDEXED*");
|
|
|
|
|
|
|
|
gimp_procedure_set_menu_label (procedure, _("PAM image"));
|
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PAM file format"),
|
|
|
|
_("PAM export handles RGB images "
|
|
|
|
"with or without transparency."),
|
2023-01-17 02:33:16 +00:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Jörg Walter",
|
|
|
|
"Jörg Walter",
|
|
|
|
"2009");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-arbitrarymap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"pam");
|
|
|
|
}
|
2019-08-24 13:18:49 +02:00
|
|
|
else if (! strcmp (name, PFM_SAVE_PROC))
|
|
|
|
{
|
2023-07-22 23:49:47 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
2019-08-30 12:52:28 +02:00
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2023-07-22 23:49:47 +02:00
|
|
|
FALSE, pnm_save,
|
2019-11-08 10:15:02 +01:00
|
|
|
GINT_TO_POINTER (FILE_TYPE_PFM),
|
|
|
|
NULL);
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB, GRAY, INDEXED");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("PFM image"));
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
_("Exports files in the PFM file format"),
|
|
|
|
_("PFM export handles all images "
|
|
|
|
"without transparency."),
|
2019-08-24 13:18:49 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Mukund Sivaraman",
|
|
|
|
"Mukund Sivaraman",
|
|
|
|
"2015");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-portable-floatmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"pfm");
|
|
|
|
}
|
|
|
|
|
|
|
|
return procedure;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpValueArray *
|
2023-08-06 02:56:44 +02:00
|
|
|
pnm_load (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GFile *file,
|
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpMetadataLoadFlags *flags,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data)
|
2019-08-24 13:18:49 +02:00
|
|
|
{
|
|
|
|
GimpValueArray *return_vals;
|
|
|
|
GimpImage *image;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
|
|
|
image = load_image (file, &error);
|
|
|
|
|
|
|
|
if (! image)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_EXECUTION_ERROR,
|
|
|
|
error);
|
|
|
|
|
|
|
|
return_vals = gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_SUCCESS,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
|
|
|
|
|
|
|
|
return return_vals;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpValueArray *
|
|
|
|
pnm_save (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2020-04-14 11:46:17 +02:00
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
2019-08-24 13:18:49 +02:00
|
|
|
GFile *file,
|
2023-07-20 23:16:27 +02:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
2019-08-24 13:18:49 +02:00
|
|
|
gpointer run_data)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2023-07-20 23:16:27 +02:00
|
|
|
FileType file_type = GPOINTER_TO_INT (run_data);
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
|
|
|
const gchar *format_name = NULL;
|
|
|
|
GError *error = NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
switch (run_mode)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 13:18:49 +02:00
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
2019-09-20 19:39:00 +02:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
switch (file_type)
|
2019-08-24 13:18:49 +02:00
|
|
|
{
|
2019-11-08 10:15:02 +01:00
|
|
|
case FILE_TYPE_PNM:
|
2020-04-14 11:46:17 +02:00
|
|
|
format_name = "PNM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PNM",
|
2019-08-24 13:18:49 +02:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED);
|
2019-11-08 10:15:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_TYPE_PBM:
|
2020-04-14 11:46:17 +02:00
|
|
|
format_name = "PBM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PBM",
|
2019-08-24 13:18:49 +02:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_BITMAP);
|
2019-11-08 10:15:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_TYPE_PGM:
|
2020-04-14 11:46:17 +02:00
|
|
|
format_name = "PGM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PGM",
|
2019-08-24 13:18:49 +02:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY);
|
2019-11-08 10:15:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_TYPE_PPM:
|
2020-04-14 11:46:17 +02:00
|
|
|
format_name = "PPM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PPM",
|
2019-08-24 13:18:49 +02:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED);
|
2019-11-08 10:15:02 +01:00
|
|
|
break;
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
case FILE_TYPE_PAM:
|
|
|
|
format_name = "PAM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PAM",
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_ALPHA |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED);
|
|
|
|
break;
|
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
case FILE_TYPE_PFM:
|
2020-04-14 11:46:17 +02:00
|
|
|
format_name = "PFM";
|
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "PFM",
|
2019-08-24 13:18:49 +02:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY);
|
2019-11-08 10:15:02 +01:00
|
|
|
break;
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
2019-08-24 13:18:49 +02:00
|
|
|
|
|
|
|
if (export == GIMP_EXPORT_CANCEL)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CANCEL,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2020-04-14 11:46:17 +02:00
|
|
|
if (n_drawables != 1)
|
|
|
|
{
|
|
|
|
g_set_error (&error, G_FILE_ERROR, 0,
|
|
|
|
_("%s format does not support multiple layers."),
|
|
|
|
format_name);
|
|
|
|
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CALLING_ERROR,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type != FILE_TYPE_PFM &&
|
2023-01-17 02:33:16 +00:00
|
|
|
file_type != FILE_TYPE_PAM &&
|
2019-11-08 10:15:02 +01:00
|
|
|
run_mode == GIMP_RUN_INTERACTIVE)
|
2019-08-24 13:18:49 +02:00
|
|
|
{
|
2023-04-02 02:55:17 +00:00
|
|
|
if (! save_dialog (procedure, G_OBJECT (config), image))
|
2019-11-08 10:15:02 +01:00
|
|
|
status = GIMP_PDB_CANCEL;
|
2019-08-24 13:18:49 +02:00
|
|
|
}
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
|
|
|
{
|
2020-04-14 11:46:17 +02:00
|
|
|
if (! save_image (file, image, drawables[0], file_type, G_OBJECT (config),
|
2019-11-08 10:15:02 +01:00
|
|
|
&error))
|
2015-01-20 20:14:07 +05:30
|
|
|
{
|
2019-08-24 13:18:49 +02:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
2008-08-18 06:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2020-04-14 11:46:17 +02:00
|
|
|
{
|
|
|
|
gimp_image_delete (image);
|
|
|
|
g_free (drawables);
|
|
|
|
}
|
2016-04-19 01:20:28 +01:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
return gimp_procedure_new_return_values (procedure, status, error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
static GimpImage *
|
2013-10-03 19:59:54 +02:00
|
|
|
load_image (GFile *file,
|
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
GInputStream *input;
|
|
|
|
GeglBuffer *buffer;
|
2019-08-24 13:18:49 +02:00
|
|
|
GimpImage * volatile image = NULL;
|
|
|
|
GimpLayer *layer;
|
2013-10-03 19:59:54 +02:00
|
|
|
char buf[BUFLEN + 4]; /* buffer for random things like scanning */
|
|
|
|
PNMInfo *pnminfo;
|
|
|
|
PNMScanner *volatile scan;
|
2023-01-15 17:14:57 +00:00
|
|
|
gint ctr;
|
|
|
|
GimpPrecision precision;
|
|
|
|
gboolean is_pam = FALSE;
|
|
|
|
GimpImageType layer_type;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2014-07-23 16:39:00 +02:00
|
|
|
gimp_progress_init_printf (_("Opening '%s'"),
|
|
|
|
g_file_get_parse_name (file));
|
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
input = G_INPUT_STREAM (g_file_read (file, NULL, error));
|
|
|
|
if (! input)
|
2019-08-24 13:18:49 +02:00
|
|
|
return NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* allocate the necessary structures */
|
2001-12-06 02:28:58 +00:00
|
|
|
pnminfo = g_new (PNMInfo, 1);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
scan = NULL;
|
|
|
|
/* set error handling */
|
|
|
|
if (setjmp (pnminfo->jmpbuf))
|
|
|
|
{
|
|
|
|
/* If we get here, we had a problem reading the file */
|
|
|
|
if (scan)
|
2006-02-04 01:02:25 +00:00
|
|
|
pnmscanner_destroy (scan);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
g_object_unref (input);
|
1997-11-24 22:05:25 +00:00
|
|
|
g_free (pnminfo);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
if (image)
|
|
|
|
gimp_image_delete (image);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
return NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
if (! (scan = pnmscanner_create (input)))
|
2005-08-18 08:57:43 +00:00
|
|
|
longjmp (pnminfo->jmpbuf, 1);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Get magic number */
|
2000-01-25 17:46:56 +00:00
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
2006-02-04 01:02:25 +00:00
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
2006-05-28 21:29:59 +00:00
|
|
|
_("Premature end of file."));
|
2006-02-04 01:02:25 +00:00
|
|
|
CHECK_FOR_ERROR ((buf[0] != 'P' || buf[2]), pnminfo->jmpbuf,
|
2006-05-28 21:29:59 +00:00
|
|
|
_("Invalid file."));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Look up magic number to see what type of PNM this is */
|
2006-02-04 01:02:25 +00:00
|
|
|
for (ctr = 0; pnm_types[ctr].name; ctr++)
|
1997-11-24 22:05:25 +00:00
|
|
|
if (buf[1] == pnm_types[ctr].name)
|
|
|
|
{
|
2015-01-16 15:37:24 +01:00
|
|
|
pnminfo->np = pnm_types[ctr].np;
|
|
|
|
pnminfo->asciibody = pnm_types[ctr].asciibody;
|
|
|
|
pnminfo->float_format = g_ascii_tolower (pnm_types[ctr].name) == 'f';
|
|
|
|
pnminfo->maxval = pnm_types[ctr].maxval;
|
|
|
|
pnminfo->loader = pnm_types[ctr].loader;
|
2023-01-15 17:14:57 +00:00
|
|
|
|
|
|
|
if (pnm_types[ctr].name == '7')
|
|
|
|
is_pam = TRUE;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2005-08-18 08:57:43 +00:00
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
if (! pnminfo->loader)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2003-06-13 14:37:00 +00:00
|
|
|
g_message (_("File not in a supported format."));
|
2007-10-27 16:50:55 +00:00
|
|
|
longjmp (pnminfo->jmpbuf, 1);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 17:14:57 +00:00
|
|
|
if (is_pam)
|
|
|
|
{
|
|
|
|
process_pam_header (scan, pnminfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
|
|
|
pnminfo->xres = g_ascii_isdigit(*buf) ? atoi (buf) : 0;
|
|
|
|
CHECK_FOR_ERROR (pnminfo->xres <= 0, pnminfo->jmpbuf,
|
|
|
|
_("Invalid X resolution."));
|
|
|
|
CHECK_FOR_ERROR (pnminfo->xres > GIMP_MAX_IMAGE_SIZE, pnminfo->jmpbuf,
|
|
|
|
_("Image width is larger than GIMP can handle."));
|
2006-02-04 01:02:25 +00:00
|
|
|
|
2023-01-15 17:14:57 +00:00
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
|
|
|
pnminfo->yres = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
|
|
|
CHECK_FOR_ERROR (pnminfo->yres <= 0, pnminfo->jmpbuf,
|
|
|
|
_("Invalid Y resolution."));
|
|
|
|
CHECK_FOR_ERROR (pnminfo->yres > GIMP_MAX_IMAGE_SIZE, pnminfo->jmpbuf,
|
|
|
|
_("Image height is larger than GIMP can handle."));
|
|
|
|
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
|
2015-01-16 15:37:24 +01:00
|
|
|
if (pnminfo->float_format)
|
|
|
|
{
|
|
|
|
gchar *endptr = NULL;
|
|
|
|
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
|
|
|
|
|
|
|
pnminfo->scale_factor = g_ascii_strtod (buf, &endptr);
|
|
|
|
CHECK_FOR_ERROR (endptr == NULL || *endptr != 0 || errno == ERANGE,
|
|
|
|
pnminfo->jmpbuf, _("Bogus scale factor."));
|
2019-11-08 10:15:02 +01:00
|
|
|
CHECK_FOR_ERROR (! isnormal (pnminfo->scale_factor),
|
2015-01-16 15:37:24 +01:00
|
|
|
pnminfo->jmpbuf, _("Unsupported scale factor."));
|
2017-02-20 21:46:21 +00:00
|
|
|
precision = GIMP_PRECISION_FLOAT_LINEAR;
|
2015-01-16 15:37:24 +01:00
|
|
|
}
|
|
|
|
else if (pnminfo->np != 0) /* pbm's don't have a maxval field */
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2023-01-15 17:14:57 +00:00
|
|
|
if (! is_pam)
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
2006-02-04 01:02:25 +00:00
|
|
|
|
2023-01-15 17:14:57 +00:00
|
|
|
pnminfo->maxval = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
|
|
|
}
|
|
|
|
CHECK_FOR_ERROR (((pnminfo->maxval <= 0) || (pnminfo->maxval > 65535)),
|
2011-03-26 15:09:59 +05:30
|
|
|
pnminfo->jmpbuf, _("Unsupported maximum value."));
|
2023-01-15 17:14:57 +00:00
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
if (pnminfo->maxval < 256)
|
|
|
|
{
|
Initial space invasion commit in GIMP
All babl formats now have a space equivalent to a color profile,
determining the format's primaries and TRCs. This commit makes GIMP
aware of this.
libgimp:
- enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA
as deprecated aliases, add PERCEPTUAL values so we now have LINEAR,
NON_LINEAR and PERCPTUAL for each encoding, matching the babl
encoding variants RGB, R'G'B' and R~G~B~.
- gimp_color_transform_can_gegl_copy() now returns TRUE if both
profiles can return a babl space, increasing the amount of fast babl
color conversions significantly.
- TODO: no solution yet for getting libgimp drawable proxy buffers in
the right format with space.
plug-ins:
- follow the GimpPrecision change.
- TODO: everything else unchanged and partly broken or sub-optimal,
like setting a new image's color profile too late.
app:
- add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as
replacement for all "linear" booleans.
- change gimp-babl functions to take babl spaces and GimpTRCType
parameters and support all sorts of new perceptual ~ formats.
- a lot of places changed in the early days of goat invasion didn't
take advantage of gimp-babl utility functions and constructed
formats manually. They all needed revisiting and many now use much
simpler code calling gimp-babl API.
- change gimp_babl_format_get_color_profile() to really extract a
newly allocated color profile from the format, and add
gimp_babl_get_builtin_color_profile() which does the same as
gimp_babl_format_get_color_profile() did before. Visited all callers
to decide whether they are looking for the format's actual profile,
or for one of the builtin profiles, simplifying code that only needs
builtin profiles.
- drawables have a new get_space_api(), get_linear() is now get_trc().
- images now have a "layer space" and an API to get it,
gimp_image_get_layer_format() returns formats in that space.
- an image's layer space is created from the image's color profile,
change gimpimage-color-profile to deal with that correctly
- change many babl_format() calls to babl_format_with_space() and take
the space from passed formats or drawables
- add function gimp_layer_fix_format_space() which replaces the
layer's buffer with one that has the image's layer format, but
doesn't change pixel values
- use gimp_layer_fix_format_space() to make sure layers loaded from
XCF and created by plug-ins have the right space when added to the
image, because it's impossible to always assign the right space upon
layer creation
- "assign color profile" and "discard color profile" now require use
of gimp_layer_fix_format_space() too because the profile is now
embedded in all formats via the space. Add
gimp_image_assign_color_profile() which does all that and call it
instead of a simple gimp_image_set_color_profile(), also from the
PDB set-color-profile functions, which are essentially "assign" and
"discard" calls.
- generally, make sure a new image's color profile is set before
adding layers to it, gimp_image_set_color_profile() is more than
before considered know-what-you-are-doing API.
- take special precaution in all places that call
gimp_drawable_convert_type(), we now must pass a new_profile from
all callers that convert layers within the same image (such as
image_convert_type, image_convert_precision), because the layer's
new space can't be determined from the image's layer format during
the call.
- change all "linear" properties to "trc", in all config objects like
for levels and curves, in the histogram, in the widgets. This results
in some GUI that now has three choices instead of two.
TODO: we might want to reduce that back to two later.
- keep "linear" boolean properties around as compat if needed for file
pasring, but always convert the parsed parsed boolean to
GimpTRCType.
- TODO: the image's "enable color management" switch is currently
broken, will fix that in another commit.
2018-07-21 14:23:01 +02:00
|
|
|
precision = GIMP_PRECISION_U8_NON_LINEAR;
|
2017-02-20 21:46:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Initial space invasion commit in GIMP
All babl formats now have a space equivalent to a color profile,
determining the format's primaries and TRCs. This commit makes GIMP
aware of this.
libgimp:
- enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA
as deprecated aliases, add PERCEPTUAL values so we now have LINEAR,
NON_LINEAR and PERCPTUAL for each encoding, matching the babl
encoding variants RGB, R'G'B' and R~G~B~.
- gimp_color_transform_can_gegl_copy() now returns TRUE if both
profiles can return a babl space, increasing the amount of fast babl
color conversions significantly.
- TODO: no solution yet for getting libgimp drawable proxy buffers in
the right format with space.
plug-ins:
- follow the GimpPrecision change.
- TODO: everything else unchanged and partly broken or sub-optimal,
like setting a new image's color profile too late.
app:
- add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as
replacement for all "linear" booleans.
- change gimp-babl functions to take babl spaces and GimpTRCType
parameters and support all sorts of new perceptual ~ formats.
- a lot of places changed in the early days of goat invasion didn't
take advantage of gimp-babl utility functions and constructed
formats manually. They all needed revisiting and many now use much
simpler code calling gimp-babl API.
- change gimp_babl_format_get_color_profile() to really extract a
newly allocated color profile from the format, and add
gimp_babl_get_builtin_color_profile() which does the same as
gimp_babl_format_get_color_profile() did before. Visited all callers
to decide whether they are looking for the format's actual profile,
or for one of the builtin profiles, simplifying code that only needs
builtin profiles.
- drawables have a new get_space_api(), get_linear() is now get_trc().
- images now have a "layer space" and an API to get it,
gimp_image_get_layer_format() returns formats in that space.
- an image's layer space is created from the image's color profile,
change gimpimage-color-profile to deal with that correctly
- change many babl_format() calls to babl_format_with_space() and take
the space from passed formats or drawables
- add function gimp_layer_fix_format_space() which replaces the
layer's buffer with one that has the image's layer format, but
doesn't change pixel values
- use gimp_layer_fix_format_space() to make sure layers loaded from
XCF and created by plug-ins have the right space when added to the
image, because it's impossible to always assign the right space upon
layer creation
- "assign color profile" and "discard color profile" now require use
of gimp_layer_fix_format_space() too because the profile is now
embedded in all formats via the space. Add
gimp_image_assign_color_profile() which does all that and call it
instead of a simple gimp_image_set_color_profile(), also from the
PDB set-color-profile functions, which are essentially "assign" and
"discard" calls.
- generally, make sure a new image's color profile is set before
adding layers to it, gimp_image_set_color_profile() is more than
before considered know-what-you-are-doing API.
- take special precaution in all places that call
gimp_drawable_convert_type(), we now must pass a new_profile from
all callers that convert layers within the same image (such as
image_convert_type, image_convert_precision), because the layer's
new space can't be determined from the image's layer format during
the call.
- change all "linear" properties to "trc", in all config objects like
for levels and curves, in the histogram, in the widgets. This results
in some GUI that now has three choices instead of two.
TODO: we might want to reduce that back to two later.
- keep "linear" boolean properties around as compat if needed for file
pasring, but always convert the parsed parsed boolean to
GimpTRCType.
- TODO: the image's "enable color management" switch is currently
broken, will fix that in another commit.
2018-07-21 14:23:01 +02:00
|
|
|
precision = GIMP_PRECISION_U16_NON_LINEAR;
|
2017-02-20 21:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Initial space invasion commit in GIMP
All babl formats now have a space equivalent to a color profile,
determining the format's primaries and TRCs. This commit makes GIMP
aware of this.
libgimp:
- enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA
as deprecated aliases, add PERCEPTUAL values so we now have LINEAR,
NON_LINEAR and PERCPTUAL for each encoding, matching the babl
encoding variants RGB, R'G'B' and R~G~B~.
- gimp_color_transform_can_gegl_copy() now returns TRUE if both
profiles can return a babl space, increasing the amount of fast babl
color conversions significantly.
- TODO: no solution yet for getting libgimp drawable proxy buffers in
the right format with space.
plug-ins:
- follow the GimpPrecision change.
- TODO: everything else unchanged and partly broken or sub-optimal,
like setting a new image's color profile too late.
app:
- add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as
replacement for all "linear" booleans.
- change gimp-babl functions to take babl spaces and GimpTRCType
parameters and support all sorts of new perceptual ~ formats.
- a lot of places changed in the early days of goat invasion didn't
take advantage of gimp-babl utility functions and constructed
formats manually. They all needed revisiting and many now use much
simpler code calling gimp-babl API.
- change gimp_babl_format_get_color_profile() to really extract a
newly allocated color profile from the format, and add
gimp_babl_get_builtin_color_profile() which does the same as
gimp_babl_format_get_color_profile() did before. Visited all callers
to decide whether they are looking for the format's actual profile,
or for one of the builtin profiles, simplifying code that only needs
builtin profiles.
- drawables have a new get_space_api(), get_linear() is now get_trc().
- images now have a "layer space" and an API to get it,
gimp_image_get_layer_format() returns formats in that space.
- an image's layer space is created from the image's color profile,
change gimpimage-color-profile to deal with that correctly
- change many babl_format() calls to babl_format_with_space() and take
the space from passed formats or drawables
- add function gimp_layer_fix_format_space() which replaces the
layer's buffer with one that has the image's layer format, but
doesn't change pixel values
- use gimp_layer_fix_format_space() to make sure layers loaded from
XCF and created by plug-ins have the right space when added to the
image, because it's impossible to always assign the right space upon
layer creation
- "assign color profile" and "discard color profile" now require use
of gimp_layer_fix_format_space() too because the profile is now
embedded in all formats via the space. Add
gimp_image_assign_color_profile() which does all that and call it
instead of a simple gimp_image_set_color_profile(), also from the
PDB set-color-profile functions, which are essentially "assign" and
"discard" calls.
- generally, make sure a new image's color profile is set before
adding layers to it, gimp_image_set_color_profile() is more than
before considered know-what-you-are-doing API.
- take special precaution in all places that call
gimp_drawable_convert_type(), we now must pass a new_profile from
all callers that convert layers within the same image (such as
image_convert_type, image_convert_precision), because the layer's
new space can't be determined from the image's layer format during
the call.
- change all "linear" properties to "trc", in all config objects like
for levels and curves, in the histogram, in the widgets. This results
in some GUI that now has three choices instead of two.
TODO: we might want to reduce that back to two later.
- keep "linear" boolean properties around as compat if needed for file
pasring, but always convert the parsed parsed boolean to
GimpTRCType.
- TODO: the image's "enable color management" switch is currently
broken, will fix that in another commit.
2018-07-21 14:23:01 +02:00
|
|
|
precision = GIMP_PRECISION_U8_NON_LINEAR;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 06:55:56 +05:30
|
|
|
/* Create a new image of the proper size and associate the filename
|
2019-11-08 10:15:02 +01:00
|
|
|
* with it.
|
|
|
|
*/
|
2019-08-24 13:18:49 +02:00
|
|
|
image = gimp_image_new_with_precision (pnminfo->xres, pnminfo->yres,
|
|
|
|
(pnminfo->np >= 3) ? GIMP_RGB : GIMP_GRAY,
|
|
|
|
precision);
|
2015-01-18 06:55:56 +05:30
|
|
|
|
2023-01-15 17:14:57 +00:00
|
|
|
switch (pnminfo->np)
|
|
|
|
{
|
2023-01-17 02:33:16 +00:00
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
layer_type = GIMP_GRAY_IMAGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
layer_type = GIMP_GRAYA_IMAGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
layer_type = GIMP_RGB_IMAGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
layer_type = GIMP_RGBA_IMAGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
layer_type = GIMP_GRAY_IMAGE;
|
2023-01-15 17:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
layer = gimp_layer_new (image, _("Background"), pnminfo->xres, pnminfo->yres,
|
2023-01-15 17:14:57 +00:00
|
|
|
layer_type, 100,
|
2019-08-24 13:18:49 +02:00
|
|
|
gimp_image_get_default_new_layer_mode (image));
|
|
|
|
gimp_image_insert_layer (image, layer, NULL, 0);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
pnminfo->loader (scan, pnminfo, buffer);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Destroy the scanner */
|
2000-01-25 17:46:56 +00:00
|
|
|
pnmscanner_destroy (scan);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
g_object_unref (buffer);
|
1997-11-24 22:05:25 +00:00
|
|
|
g_free (pnminfo);
|
2013-10-03 19:59:54 +02:00
|
|
|
g_object_unref (input);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
return image;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 17:14:57 +00:00
|
|
|
/* Code referenced from Jörg Walter */
|
|
|
|
static void
|
|
|
|
process_pam_header (PNMScanner *scan,
|
|
|
|
PNMInfo *pnminfo)
|
|
|
|
{
|
|
|
|
char buf[BUFLEN + 4];
|
|
|
|
gboolean is_unsupported_tupltype = FALSE;
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
while (! scan->eof)
|
2023-01-15 17:14:57 +00:00
|
|
|
{
|
|
|
|
pnmscanner_getheaderline (scan, buf, BUFLEN);
|
|
|
|
if (! strcmp (buf, "WIDTH"))
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
pnminfo->xres = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
|
|
|
}
|
|
|
|
else if (! strcmp (buf, "HEIGHT"))
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
2023-01-17 02:33:16 +00:00
|
|
|
pnminfo->yres = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
2023-01-15 17:14:57 +00:00
|
|
|
}
|
2023-01-17 02:33:16 +00:00
|
|
|
else if (! strcmp (buf, "DEPTH"))
|
2023-01-15 17:14:57 +00:00
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
pnminfo->np = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
|
|
|
}
|
2023-01-17 02:33:16 +00:00
|
|
|
else if (! strcmp (buf, "MAXVAL"))
|
2023-01-15 17:14:57 +00:00
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
pnminfo->maxval = g_ascii_isdigit (*buf) ? atoi (buf) : 0;
|
|
|
|
}
|
|
|
|
else if (! strcmp (buf, "TUPLTYPE"))
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
|
|
|
|
/* PAM files may have custom tupltypes;
|
|
|
|
* however, these are the only 'officially' defined
|
|
|
|
* ones */
|
|
|
|
if (strcmp (buf, "BLACKANDWHITE") &&
|
|
|
|
strcmp (buf, "BLACKANDWHITE_ALPHA") &&
|
|
|
|
strcmp (buf, "GRAYSCALE") &&
|
|
|
|
strcmp (buf, "GRAYSCALE_ALPHA") &&
|
|
|
|
strcmp (buf, "RGB") &&
|
|
|
|
strcmp (buf, "RGB_ALPHA"))
|
|
|
|
{
|
|
|
|
is_unsupported_tupltype = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (! strcmp (buf, "ENDHDR"))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* skip unknown headers but recognize xv's thumbnail format */
|
|
|
|
CHECK_FOR_ERROR (g_ascii_isdigit (*buf), pnminfo->jmpbuf,
|
|
|
|
_("PAM: Unsupported inofficial PNM variant."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK_FOR_ERROR (pnmscanner_eof (scan), pnminfo->jmpbuf,
|
|
|
|
_("PAM: Premature end of file."));
|
|
|
|
CHECK_FOR_ERROR (pnminfo->xres > GIMP_MAX_IMAGE_SIZE, pnminfo->jmpbuf,
|
|
|
|
_("Image width is larger than GIMP can handle."));
|
|
|
|
CHECK_FOR_ERROR (pnminfo->xres <= 0, pnminfo->jmpbuf,
|
|
|
|
_("PAM: Invalid X resolution."));
|
|
|
|
CHECK_FOR_ERROR (pnminfo->yres <= 0, pnminfo->jmpbuf,
|
|
|
|
_("PAM: Invalid Y resolution."));
|
|
|
|
CHECK_FOR_ERROR ((pnminfo->maxval <= 0), pnminfo->jmpbuf,
|
|
|
|
_("PAM: Invalid maximum value."));
|
|
|
|
CHECK_FOR_ERROR ((pnminfo->np <= 0), pnminfo->jmpbuf,
|
|
|
|
_("PAM: Invalid depth."));
|
|
|
|
CHECK_FOR_ERROR (is_unsupported_tupltype, pnminfo->jmpbuf,
|
|
|
|
_("PAM: Unsupported tupltype."));
|
|
|
|
}
|
|
|
|
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
2012-11-26 01:31:39 +01:00
|
|
|
pnm_load_ascii (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2017-02-20 21:46:21 +00:00
|
|
|
gint bpc;
|
2007-10-27 17:05:55 +00:00
|
|
|
guchar *data, *d;
|
2017-02-20 21:46:21 +00:00
|
|
|
gushort *s;
|
2007-10-27 17:05:55 +00:00
|
|
|
gint x, y, i, b;
|
|
|
|
gint start, end, scanlines;
|
|
|
|
gint np;
|
|
|
|
gchar buf[BUFLEN];
|
|
|
|
gboolean aborted = FALSE;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
np = (info->np) ? (info->np) : 1;
|
2017-02-20 21:46:21 +00:00
|
|
|
|
|
|
|
if (info->maxval > 255)
|
|
|
|
bpc = 2;
|
|
|
|
else
|
|
|
|
bpc = 1;
|
|
|
|
|
|
|
|
/* No overflow as long as gimp_tile_height() < 1365 = 2^(31 - 18) / 6 */
|
|
|
|
data = g_new (guchar, gimp_tile_height () * info->xres * np * bpc);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* Buffer reads to increase performance */
|
2006-02-04 01:02:25 +00:00
|
|
|
pnmscanner_createbuffer (scan, 4096);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2007-10-27 17:05:55 +00:00
|
|
|
for (y = 0; y < info->yres; y += scanlines)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
start = y;
|
2007-10-27 17:05:55 +00:00
|
|
|
end = y + gimp_tile_height ();
|
|
|
|
end = MIN (end, info->yres);
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
scanlines = end - start;
|
2007-10-27 17:05:55 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
d = data;
|
2017-02-20 21:46:21 +00:00
|
|
|
s = (gushort *)d;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < scanlines; i++)
|
2006-02-04 01:02:25 +00:00
|
|
|
for (x = 0; x < info->xres; x++)
|
|
|
|
{
|
|
|
|
for (b = 0; b < np; b++)
|
|
|
|
{
|
2007-10-27 17:05:55 +00:00
|
|
|
if (aborted)
|
|
|
|
{
|
|
|
|
d[b] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncated files will just have all 0's
|
|
|
|
at the end of the images */
|
2006-05-28 21:29:59 +00:00
|
|
|
if (pnmscanner_eof (scan))
|
2007-10-27 17:05:55 +00:00
|
|
|
{
|
|
|
|
g_message (_("Premature end of file."));
|
|
|
|
aborted = TRUE;
|
|
|
|
|
|
|
|
d[b] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-12 07:15:38 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
if (info->np)
|
|
|
|
pnmscanner_gettoken (scan, buf, BUFLEN);
|
|
|
|
else
|
|
|
|
pnmscanner_getsmalltoken (scan, buf);
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
if (info->maxval == 1)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2007-09-12 07:15:38 +00:00
|
|
|
if (info->np)
|
|
|
|
d[b] = (*buf == '0') ? 0x00 : 0xff;
|
|
|
|
else
|
|
|
|
d[b] = (*buf == '0') ? 0xff : 0x00; /* invert for PBM */
|
2017-02-20 21:46:21 +00:00
|
|
|
}
|
|
|
|
else if (bpc > 1)
|
|
|
|
{
|
|
|
|
s[b] = (65535.0 * (((gdouble) (g_ascii_isdigit (*buf) ?
|
|
|
|
atoi (buf) : 0))
|
|
|
|
/ (gdouble) (info->maxval)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-12 07:15:38 +00:00
|
|
|
d[b] = (255.0 * (((gdouble) (g_ascii_isdigit (*buf) ?
|
|
|
|
atoi (buf) : 0))
|
|
|
|
/ (gdouble) (info->maxval)));
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
if (bpc > 1)
|
|
|
|
{
|
|
|
|
s += np;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d += np;
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y, info->xres, scanlines), 0,
|
|
|
|
NULL, data, GEGL_AUTO_ROWSTRIDE);
|
|
|
|
|
2000-01-14 19:57:42 +00:00
|
|
|
gimp_progress_update ((double) y / (double) info->yres);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (data);
|
2012-11-26 01:31:39 +01:00
|
|
|
|
|
|
|
gimp_progress_update (1.0);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
2012-11-26 01:31:39 +01:00
|
|
|
pnm_load_raw (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
GInputStream *input;
|
|
|
|
gint bpc;
|
2017-02-20 21:46:21 +00:00
|
|
|
guchar *data, *d;
|
|
|
|
gushort *s;
|
2013-10-03 19:59:54 +02:00
|
|
|
gint x, y, i;
|
|
|
|
gint start, end, scanlines;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2011-03-26 15:09:59 +05:30
|
|
|
if (info->maxval > 255)
|
|
|
|
bpc = 2;
|
|
|
|
else
|
|
|
|
bpc = 1;
|
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
/* No overflow as long as gimp_tile_height() < 1365 = 2^(31 - 18) / 6 */
|
2011-03-26 15:09:59 +05:30
|
|
|
data = g_new (guchar, gimp_tile_height () * info->xres * info->np * bpc);
|
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
input = pnmscanner_input (scan);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
for (y = 0; y < info->yres; y += scanlines)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
start = y;
|
|
|
|
end = y + gimp_tile_height ();
|
|
|
|
end = MIN (end, info->yres);
|
|
|
|
scanlines = end - start;
|
|
|
|
d = data;
|
2017-02-20 21:46:21 +00:00
|
|
|
s = (gushort *)data;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < scanlines; i++)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
gsize bytes_read;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (g_input_stream_read_all (input, d, info->xres * info->np * bpc,
|
|
|
|
&bytes_read, NULL, &error))
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR (info->xres * info->np * bpc != bytes_read,
|
|
|
|
info->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR (FALSE, info->jmpbuf, "%s", error->message);
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
|
2011-03-26 15:09:59 +05:30
|
|
|
if (bpc > 1)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
|
|
|
for (x = 0; x < info->xres * info->np; x++)
|
|
|
|
{
|
2011-03-26 15:09:59 +05:30
|
|
|
int v;
|
|
|
|
|
|
|
|
v = *d++ << 8;
|
|
|
|
v += *d++;
|
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
s[x] = MIN (v, info->maxval); /* guard against overflow */
|
|
|
|
s[x] = 65535.0 * (gdouble) v / (gdouble) info->maxval;
|
2003-06-11 12:47:16 +00:00
|
|
|
}
|
2017-02-20 21:46:21 +00:00
|
|
|
s += info->xres * info->np;
|
2003-06-11 12:47:16 +00:00
|
|
|
}
|
2011-03-26 15:09:59 +05:30
|
|
|
else
|
|
|
|
{
|
|
|
|
if (info->maxval != 255) /* Normalize if needed */
|
|
|
|
{
|
|
|
|
for (x = 0; x < info->xres * info->np; x++)
|
|
|
|
{
|
|
|
|
d[x] = MIN (d[x], info->maxval); /* guard against overflow */
|
|
|
|
d[x] = 255.0 * (gdouble) d[x] / (gdouble) info->maxval;
|
|
|
|
}
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2011-03-26 15:09:59 +05:30
|
|
|
d += info->xres * info->np;
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2017-02-20 21:46:21 +00:00
|
|
|
gegl_buffer_set (buffer,
|
|
|
|
GEGL_RECTANGLE (0, y, info->xres, scanlines), 0,
|
|
|
|
NULL, data, GEGL_AUTO_ROWSTRIDE);
|
2011-03-26 15:09:59 +05:30
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
gimp_progress_update ((double) y / (double) info->yres);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (data);
|
2012-11-26 01:31:39 +01:00
|
|
|
|
|
|
|
gimp_progress_update (1.0);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
1999-10-14 02:11:52 +00:00
|
|
|
static void
|
2012-11-26 01:31:39 +01:00
|
|
|
pnm_load_rawpbm (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
GInputStream *input;
|
|
|
|
guchar *buf;
|
|
|
|
guchar curbyte;
|
|
|
|
guchar *data, *d;
|
|
|
|
gint x, y, i;
|
|
|
|
gint start, end, scanlines;
|
|
|
|
gint rowlen, bufpos;
|
|
|
|
|
|
|
|
input = pnmscanner_input (scan);
|
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
rowlen = (int)ceil ((double)(info->xres)/8.0);
|
|
|
|
data = g_new (guchar, gimp_tile_height () * info->xres);
|
|
|
|
buf = g_new (guchar, rowlen);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
for (y = 0; y < info->yres; y += scanlines)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
start = y;
|
|
|
|
end = y + gimp_tile_height ();
|
|
|
|
end = MIN (end, info->yres);
|
|
|
|
scanlines = end - start;
|
|
|
|
d = data;
|
|
|
|
|
|
|
|
for (i = 0; i < scanlines; i++)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
gsize bytes_read;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (g_input_stream_read_all (input, buf, rowlen,
|
|
|
|
&bytes_read, NULL, &error))
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR (rowlen != bytes_read,
|
|
|
|
info->jmpbuf,
|
|
|
|
_("Premature end of file."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR (FALSE, info->jmpbuf, "%s", error->message);
|
|
|
|
}
|
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
bufpos = 0;
|
|
|
|
curbyte = buf[0];
|
|
|
|
|
|
|
|
for (x = 0; x < info->xres; x++)
|
|
|
|
{
|
|
|
|
if ((x % 8) == 0)
|
|
|
|
curbyte = buf[bufpos++];
|
|
|
|
d[x] = (curbyte & 0x80) ? 0x00 : 0xff;
|
|
|
|
curbyte <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
d += info->xres;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y, info->xres, scanlines), 0,
|
|
|
|
NULL, data, GEGL_AUTO_ROWSTRIDE);
|
|
|
|
|
2000-01-14 19:57:42 +00:00
|
|
|
gimp_progress_update ((double) y / (double) info->yres);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
g_free (buf);
|
1997-11-24 22:05:25 +00:00
|
|
|
g_free (data);
|
2012-11-26 01:31:39 +01:00
|
|
|
|
|
|
|
gimp_progress_update (1.0);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 15:37:24 +01:00
|
|
|
static void
|
|
|
|
pnm_load_rawpfm (PNMScanner *scan,
|
|
|
|
PNMInfo *info,
|
|
|
|
GeglBuffer *buffer)
|
|
|
|
{
|
|
|
|
GInputStream *input;
|
|
|
|
gfloat *data;
|
|
|
|
gint x, y;
|
|
|
|
gboolean swap_byte_order;
|
|
|
|
|
|
|
|
swap_byte_order =
|
|
|
|
(info->scale_factor >= 0.0) ^ (G_BYTE_ORDER == G_BIG_ENDIAN);
|
|
|
|
|
|
|
|
data = g_new (gfloat, info->xres * info->np);
|
|
|
|
|
|
|
|
input = pnmscanner_input (scan);
|
|
|
|
|
|
|
|
for (y = info->yres - 1; y >= 0; y--)
|
|
|
|
{
|
|
|
|
gsize bytes_read;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (g_input_stream_read_all (input, data,
|
|
|
|
info->xres * info->np * sizeof (float),
|
|
|
|
&bytes_read, NULL, &error))
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR
|
|
|
|
(info->xres * info->np * sizeof (float) != bytes_read,
|
|
|
|
info->jmpbuf, _("Premature end of file."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK_FOR_ERROR (FALSE, info->jmpbuf, "%s", error->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < info->xres * info->np; x++)
|
|
|
|
{
|
|
|
|
if (swap_byte_order)
|
|
|
|
{
|
|
|
|
union { gfloat f; guint32 i; } v;
|
|
|
|
|
|
|
|
v.f = data[x];
|
|
|
|
v.i = GUINT32_SWAP_LE_BE (v.i);
|
|
|
|
data[x] = v.f;
|
|
|
|
}
|
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
/* let's see if this is what people want, the PFM specs are
|
|
|
|
* a little vague about what the scale factor should be used
|
|
|
|
* for
|
|
|
|
*/
|
2015-01-16 15:37:24 +01:00
|
|
|
data[x] *= fabsf (info->scale_factor);
|
2019-11-08 10:15:02 +01:00
|
|
|
/* Keep values smaller than zero. That is in line with what
|
|
|
|
* the TIFF loader does. If the user doesn't want the
|
2021-11-14 11:22:24 +00:00
|
|
|
* negative numbers they have to get rid of them afterwards
|
2019-11-08 10:15:02 +01:00
|
|
|
*/
|
2018-10-30 13:37:07 +01:00
|
|
|
/* data[x] = fmaxf (0.0f, fminf (FLT_MAX, data[x])); */
|
2015-01-16 15:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gegl_buffer_set (buffer,
|
|
|
|
GEGL_RECTANGLE (0, y, info->xres, 1), 0,
|
|
|
|
NULL, data, GEGL_AUTO_ROWSTRIDE);
|
|
|
|
|
|
|
|
if (y % 32 == 0)
|
2015-01-20 21:07:59 +05:30
|
|
|
gimp_progress_update ((double) (info->yres - y) / (double) info->yres);
|
2015-01-16 15:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (data);
|
|
|
|
|
|
|
|
gimp_progress_update (1.0);
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
output_write (GOutputStream *output,
|
|
|
|
gconstpointer buffer,
|
|
|
|
gsize count,
|
|
|
|
GError **error)
|
|
|
|
{
|
2014-09-07 20:30:14 +02:00
|
|
|
return g_output_stream_write_all (output, buffer, count, NULL, NULL, error);
|
2013-10-03 21:44:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
static void
|
|
|
|
create_pam_header (const gchar **header_string,
|
|
|
|
PNMRowInfo *rowinfo,
|
|
|
|
PNMSaverowFunc *saverow,
|
|
|
|
GimpImageType drawable_type,
|
|
|
|
GeglBuffer *buffer,
|
|
|
|
const Babl **format,
|
|
|
|
gint *rowbufsize,
|
|
|
|
gint *np,
|
|
|
|
gchar *comment)
|
|
|
|
{
|
|
|
|
gint maxval = 255;
|
|
|
|
gint xres = gegl_buffer_get_width (buffer);
|
|
|
|
gint yres = gegl_buffer_get_height (buffer);
|
|
|
|
gchar *tupltype = NULL;
|
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
comment = g_strdup_printf ("# Created by GIMP version %s PNM plug-in",
|
|
|
|
GIMP_VERSION);
|
2023-01-17 02:33:16 +00:00
|
|
|
*saverow = pnmsaverow_raw;
|
|
|
|
|
|
|
|
if (rowinfo->bpc == 2)
|
|
|
|
maxval = 65535;
|
|
|
|
|
|
|
|
switch (drawable_type)
|
|
|
|
{
|
|
|
|
case GIMP_GRAY_IMAGE:
|
|
|
|
tupltype = "GRAYSCALE";
|
|
|
|
*np = 1;
|
|
|
|
if (rowinfo->bpc == 1)
|
|
|
|
{
|
|
|
|
*format = babl_format ("Y' u8");
|
|
|
|
*rowbufsize = xres;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*format = babl_format ("Y' u16");
|
|
|
|
*rowbufsize = xres * 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_GRAYA_IMAGE:
|
|
|
|
tupltype = "GRAYSCALE_ALPHA";
|
|
|
|
*np = 2;
|
|
|
|
if (rowinfo->bpc == 1)
|
|
|
|
{
|
|
|
|
*format = babl_format ("Y'A u8");
|
|
|
|
*rowbufsize = xres * 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*format = babl_format ("Y'A u16");
|
|
|
|
*rowbufsize = xres * 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGB_IMAGE:
|
|
|
|
tupltype = "RGB";
|
|
|
|
*np = 3;
|
|
|
|
if (rowinfo->bpc == 1)
|
|
|
|
{
|
|
|
|
*format = babl_format ("R'G'B' u8");
|
|
|
|
*rowbufsize = xres * 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*format = babl_format ("R'G'B' u16");
|
|
|
|
*rowbufsize = xres * 6;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGBA_IMAGE:
|
|
|
|
tupltype = "RGB_ALPHA";
|
|
|
|
*np = 4;
|
|
|
|
if (rowinfo->bpc == 1)
|
|
|
|
{
|
|
|
|
*format = babl_format ("R'G'B'A u8");
|
|
|
|
*rowbufsize = xres * 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*format = babl_format ("R'G'B'A u16");
|
|
|
|
*rowbufsize = xres * 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_INDEXED_IMAGE:
|
|
|
|
tupltype = "RGB";
|
|
|
|
*np = 3;
|
|
|
|
*format = babl_format ("R'G'B' u8");
|
|
|
|
*rowbufsize = xres * 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_INDEXEDA_IMAGE:
|
|
|
|
tupltype = "RGB_ALPHA";
|
|
|
|
*np = 4;
|
|
|
|
*format = babl_format ("R'G'B'A u8");
|
|
|
|
*rowbufsize = xres * 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*header_string = g_strdup_printf ("P7\n%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n"
|
|
|
|
"MAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
|
|
|
|
comment, xres, yres, *np, maxval, tupltype);
|
2023-04-02 02:55:17 +00:00
|
|
|
|
|
|
|
if (comment)
|
|
|
|
g_free (comment);
|
2023-01-17 02:33:16 +00:00
|
|
|
}
|
|
|
|
|
2006-05-28 21:29:59 +00:00
|
|
|
/* Writes out mono raw rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_raw_pbm (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
2009-03-05 14:08:56 +00:00
|
|
|
gint b, p = 0;
|
|
|
|
gchar *rbcur = ri->rowbuf;
|
|
|
|
gint32 len = (gint) ceil ((gdouble) (ri->xres) / 8.0);
|
2006-05-28 21:29:59 +00:00
|
|
|
|
|
|
|
for (b = 0; b < len; b++) /* each output byte */
|
|
|
|
{
|
2009-03-05 14:08:56 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
rbcur[b] = 0;
|
|
|
|
|
2006-05-28 21:29:59 +00:00
|
|
|
for (i = 0; i < 8; i++) /* each bit in this byte */
|
|
|
|
{
|
|
|
|
if (p >= ri->xres)
|
2009-03-05 14:08:56 +00:00
|
|
|
break;
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2009-03-12 22:18:44 +00:00
|
|
|
if (data[p] != ri->zero_is_black)
|
2009-03-05 14:08:56 +00:00
|
|
|
rbcur[b] |= (char) (1 << (7 - i));
|
2006-05-28 21:29:59 +00:00
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:44:24 +02:00
|
|
|
return output_write (ri->output, ri->rowbuf, len, error);
|
2006-05-28 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Writes out mono ascii rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_ascii_pbm (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
|
|
|
static gint line_len = 0; /* ascii pbm lines must be <= 70 chars long */
|
|
|
|
gint32 len = 0;
|
|
|
|
gint i;
|
|
|
|
gchar *rbcur = ri->rowbuf;
|
|
|
|
|
|
|
|
for (i = 0; i < ri->xres; i++)
|
|
|
|
{
|
|
|
|
if (line_len > 69)
|
|
|
|
{
|
2009-03-05 14:08:56 +00:00
|
|
|
rbcur[i] = '\n';
|
2006-05-28 21:29:59 +00:00
|
|
|
line_len = 0;
|
|
|
|
len++;
|
|
|
|
rbcur++;
|
|
|
|
}
|
|
|
|
|
2009-03-12 22:18:44 +00:00
|
|
|
if (data[i] == ri->zero_is_black)
|
2009-03-05 14:08:56 +00:00
|
|
|
rbcur[i] = '0';
|
2006-05-28 21:29:59 +00:00
|
|
|
else
|
2009-03-05 14:08:56 +00:00
|
|
|
rbcur[i] = '1';
|
2006-05-28 21:29:59 +00:00
|
|
|
|
|
|
|
line_len++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(rbcur+i) = '\n';
|
|
|
|
|
2013-10-03 21:44:24 +02:00
|
|
|
return output_write (ri->output, ri->rowbuf, len, error);
|
2006-05-28 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 23:26:16 +02:00
|
|
|
/* Writes out RGB and grayscale raw rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_raw (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2017-02-20 23:12:05 +00:00
|
|
|
gint i;
|
|
|
|
if (ri->bpc == 2)
|
|
|
|
{
|
|
|
|
gushort *d = (gushort *)data;
|
|
|
|
for (i = 0; i < ri->xres * ri->np; i++)
|
|
|
|
{
|
|
|
|
*d = g_htons(*d);
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output_write (ri->output, data, ri->xres * ri->np * ri->bpc, error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
/* Writes out RGB and grayscale float rows */
|
|
|
|
static gboolean
|
|
|
|
pnmsaverow_float (PNMRowInfo *ri,
|
|
|
|
const float *data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return output_write (ri->output, data,
|
|
|
|
ri->xres * ri->np * sizeof (float),
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* Writes out indexed raw rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_raw_indexed (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2006-02-04 01:02:25 +00:00
|
|
|
gint i;
|
2023-01-17 02:33:16 +00:00
|
|
|
gint offset = 3;
|
|
|
|
gchar *rbcur = ri->rowbuf;
|
|
|
|
|
|
|
|
if (ri->alpha)
|
|
|
|
offset = 4;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ri->xres; i++)
|
|
|
|
{
|
|
|
|
*(rbcur++) = ri->red[*data];
|
|
|
|
*(rbcur++) = ri->grn[*data];
|
2023-01-17 02:33:16 +00:00
|
|
|
if (! ri->alpha)
|
|
|
|
{
|
|
|
|
*(rbcur++) = ri->blu[*(data++)];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(rbcur++) = ri->blu[*(data)];
|
|
|
|
*(rbcur++) = ri->alpha[*(data++)];
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
return output_write (ri->output, ri->rowbuf, ri->xres * offset, error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 23:26:16 +02:00
|
|
|
/* Writes out RGB and grayscale ascii rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_ascii (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2006-02-04 01:02:25 +00:00
|
|
|
gint i;
|
|
|
|
gchar *rbcur = ri->rowbuf;
|
2017-02-20 23:12:05 +00:00
|
|
|
gushort *sdata = (gushort *)data;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2013-10-03 21:44:24 +02:00
|
|
|
for (i = 0; i < ri->xres * ri->np; i++)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2017-02-20 23:12:05 +00:00
|
|
|
if (ri->bpc == 2)
|
|
|
|
{
|
|
|
|
sprintf ((gchar *) rbcur,"%d\n", 0xffff & *(sdata++));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf ((gchar *) rbcur,"%d\n", 0xff & *(data++));
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
rbcur += strlen (rbcur);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2017-02-20 23:12:05 +00:00
|
|
|
return output_write (ri->output, ri->rowbuf, rbcur - ri->rowbuf,
|
2013-10-03 21:44:24 +02:00
|
|
|
error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 23:26:16 +02:00
|
|
|
/* Writes out RGB and grayscale ascii rows */
|
2013-10-03 21:44:24 +02:00
|
|
|
static gboolean
|
|
|
|
pnmsaverow_ascii_indexed (PNMRowInfo *ri,
|
2017-02-20 23:12:05 +00:00
|
|
|
guchar *data,
|
2013-10-03 21:44:24 +02:00
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2006-02-04 01:02:25 +00:00
|
|
|
gint i;
|
|
|
|
gchar *rbcur = ri->rowbuf;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ri->xres; i++)
|
|
|
|
{
|
2009-03-05 14:08:56 +00:00
|
|
|
sprintf (rbcur, "%d\n", 0xff & ri->red[*(data)]);
|
2006-02-04 01:02:25 +00:00
|
|
|
rbcur += strlen (rbcur);
|
2009-03-05 14:08:56 +00:00
|
|
|
sprintf (rbcur, "%d\n", 0xff & ri->grn[*(data)]);
|
2006-02-04 01:02:25 +00:00
|
|
|
rbcur += strlen (rbcur);
|
2009-03-05 14:08:56 +00:00
|
|
|
sprintf (rbcur, "%d\n", 0xff & ri->blu[*(data++)]);
|
2006-02-04 01:02:25 +00:00
|
|
|
rbcur += strlen (rbcur);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2013-10-03 21:44:24 +02:00
|
|
|
return output_write (ri->output, ri->rowbuf, strlen ((char *) ri->rowbuf),
|
|
|
|
error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
static gboolean
|
2019-08-24 13:18:49 +02:00
|
|
|
save_image (GFile *file,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
2019-11-08 10:15:02 +01:00
|
|
|
FileType file_type,
|
|
|
|
GObject *config,
|
2019-08-24 13:18:49 +02:00
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2015-01-20 20:14:07 +05:30
|
|
|
gboolean status = FALSE;
|
|
|
|
GOutputStream *output = NULL;
|
|
|
|
GeglBuffer *buffer = NULL;
|
2023-01-17 02:33:16 +00:00
|
|
|
const Babl *format = NULL;
|
2013-10-03 21:44:24 +02:00
|
|
|
const gchar *header_string = NULL;
|
2005-08-18 08:57:43 +00:00
|
|
|
GimpImageType drawable_type;
|
|
|
|
PNMRowInfo rowinfo;
|
2013-10-03 22:06:24 +02:00
|
|
|
PNMSaverowFunc saverow = NULL;
|
2006-02-04 01:02:25 +00:00
|
|
|
guchar red[256];
|
|
|
|
guchar grn[256];
|
|
|
|
guchar blu[256];
|
2023-01-17 02:33:16 +00:00
|
|
|
guchar alpha[256]; /* PAM only */
|
2006-02-04 01:02:25 +00:00
|
|
|
gchar buf[BUFLEN];
|
|
|
|
gint np = 0;
|
|
|
|
gint xres, yres;
|
|
|
|
gint ypos, yend;
|
|
|
|
gint rowbufsize = 0;
|
2019-11-08 10:15:02 +01:00
|
|
|
gchar *comment = NULL;
|
|
|
|
gboolean config_raw = TRUE;
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
if (file_type != FILE_TYPE_PFM && file_type != FILE_TYPE_PAM)
|
2019-11-08 10:15:02 +01:00
|
|
|
g_object_get (config,
|
|
|
|
"raw", &config_raw,
|
|
|
|
NULL);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
/* Make sure we're not saving an image with an alpha channel
|
|
|
|
* unless we're exporting a PAM file */
|
|
|
|
if (file_type != FILE_TYPE_PAM && gimp_drawable_has_alpha (drawable))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2016-02-16 02:35:43 +01:00
|
|
|
g_message (_("Cannot export images with alpha channel."));
|
2015-01-20 20:14:07 +05:30
|
|
|
goto out;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 02:35:43 +01:00
|
|
|
gimp_progress_init_printf (_("Exporting '%s'"),
|
2014-07-23 16:39:00 +02:00
|
|
|
g_file_get_parse_name (file));
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* open the file */
|
2014-10-04 02:44:54 +02:00
|
|
|
output = G_OUTPUT_STREAM (g_file_replace (file,
|
|
|
|
NULL, FALSE, G_FILE_CREATE_NONE,
|
|
|
|
NULL, error));
|
2013-10-03 21:44:24 +02:00
|
|
|
if (! output)
|
2015-01-20 20:14:07 +05:30
|
|
|
goto out;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
2012-11-26 01:31:39 +01:00
|
|
|
|
|
|
|
xres = gegl_buffer_get_width (buffer);
|
|
|
|
yres = gegl_buffer_get_height (buffer);
|
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
drawable_type = gimp_drawable_type (drawable);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:18:49 +02:00
|
|
|
switch (gimp_image_get_precision (image))
|
2017-02-20 23:12:05 +00:00
|
|
|
{
|
|
|
|
case GIMP_PRECISION_U8_LINEAR:
|
Initial space invasion commit in GIMP
All babl formats now have a space equivalent to a color profile,
determining the format's primaries and TRCs. This commit makes GIMP
aware of this.
libgimp:
- enum GimpPrecision: rename GAMMA values to NON_LINEAR and keep GAMMA
as deprecated aliases, add PERCEPTUAL values so we now have LINEAR,
NON_LINEAR and PERCPTUAL for each encoding, matching the babl
encoding variants RGB, R'G'B' and R~G~B~.
- gimp_color_transform_can_gegl_copy() now returns TRUE if both
profiles can return a babl space, increasing the amount of fast babl
color conversions significantly.
- TODO: no solution yet for getting libgimp drawable proxy buffers in
the right format with space.
plug-ins:
- follow the GimpPrecision change.
- TODO: everything else unchanged and partly broken or sub-optimal,
like setting a new image's color profile too late.
app:
- add enum GimpTRCType { LINEAR, NON_LINEAR, PERCEPTUAL } as
replacement for all "linear" booleans.
- change gimp-babl functions to take babl spaces and GimpTRCType
parameters and support all sorts of new perceptual ~ formats.
- a lot of places changed in the early days of goat invasion didn't
take advantage of gimp-babl utility functions and constructed
formats manually. They all needed revisiting and many now use much
simpler code calling gimp-babl API.
- change gimp_babl_format_get_color_profile() to really extract a
newly allocated color profile from the format, and add
gimp_babl_get_builtin_color_profile() which does the same as
gimp_babl_format_get_color_profile() did before. Visited all callers
to decide whether they are looking for the format's actual profile,
or for one of the builtin profiles, simplifying code that only needs
builtin profiles.
- drawables have a new get_space_api(), get_linear() is now get_trc().
- images now have a "layer space" and an API to get it,
gimp_image_get_layer_format() returns formats in that space.
- an image's layer space is created from the image's color profile,
change gimpimage-color-profile to deal with that correctly
- change many babl_format() calls to babl_format_with_space() and take
the space from passed formats or drawables
- add function gimp_layer_fix_format_space() which replaces the
layer's buffer with one that has the image's layer format, but
doesn't change pixel values
- use gimp_layer_fix_format_space() to make sure layers loaded from
XCF and created by plug-ins have the right space when added to the
image, because it's impossible to always assign the right space upon
layer creation
- "assign color profile" and "discard color profile" now require use
of gimp_layer_fix_format_space() too because the profile is now
embedded in all formats via the space. Add
gimp_image_assign_color_profile() which does all that and call it
instead of a simple gimp_image_set_color_profile(), also from the
PDB set-color-profile functions, which are essentially "assign" and
"discard" calls.
- generally, make sure a new image's color profile is set before
adding layers to it, gimp_image_set_color_profile() is more than
before considered know-what-you-are-doing API.
- take special precaution in all places that call
gimp_drawable_convert_type(), we now must pass a new_profile from
all callers that convert layers within the same image (such as
image_convert_type, image_convert_precision), because the layer's
new space can't be determined from the image's layer format during
the call.
- change all "linear" properties to "trc", in all config objects like
for levels and curves, in the histogram, in the widgets. This results
in some GUI that now has three choices instead of two.
TODO: we might want to reduce that back to two later.
- keep "linear" boolean properties around as compat if needed for file
pasring, but always convert the parsed parsed boolean to
GimpTRCType.
- TODO: the image's "enable color management" switch is currently
broken, will fix that in another commit.
2018-07-21 14:23:01 +02:00
|
|
|
case GIMP_PRECISION_U8_NON_LINEAR:
|
|
|
|
case GIMP_PRECISION_U8_PERCEPTUAL:
|
2017-02-20 23:12:05 +00:00
|
|
|
rowinfo.bpc = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rowinfo.bpc = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* write out magic number */
|
2023-01-17 02:33:16 +00:00
|
|
|
if (file_type == FILE_TYPE_PAM)
|
|
|
|
{
|
|
|
|
create_pam_header (&header_string, &rowinfo, &saverow, drawable_type,
|
|
|
|
buffer, &format, &rowbufsize, &np, comment);
|
|
|
|
}
|
|
|
|
else if (file_type != FILE_TYPE_PFM && ! config_raw)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type == FILE_TYPE_PBM)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P1\n";
|
2016-09-04 17:19:08 +02:00
|
|
|
format = gegl_buffer_get_format (buffer);
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 0;
|
|
|
|
rowbufsize = xres + (int) (xres / 70) + 1;
|
|
|
|
saverow = pnmsaverow_ascii_pbm;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (drawable_type)
|
|
|
|
{
|
|
|
|
case GIMP_GRAY_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P2\n";
|
2017-02-20 23:12:05 +00:00
|
|
|
if (rowinfo.bpc == 1)
|
|
|
|
{
|
|
|
|
format = babl_format ("Y' u8");
|
|
|
|
rowbufsize = xres * 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = babl_format ("Y' u16");
|
|
|
|
rowbufsize = xres * 6;
|
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 1;
|
|
|
|
saverow = pnmsaverow_ascii;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGB_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P3\n";
|
2017-02-20 23:12:05 +00:00
|
|
|
if (rowinfo.bpc == 1)
|
|
|
|
{
|
|
|
|
format = babl_format ("R'G'B' u8");
|
|
|
|
rowbufsize = xres * 12;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = babl_format ("R'G'B' u16");
|
|
|
|
rowbufsize = xres * 18;
|
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 3;
|
|
|
|
saverow = pnmsaverow_ascii;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_INDEXED_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P3\n";
|
2012-11-26 01:31:39 +01:00
|
|
|
format = gegl_buffer_get_format (buffer);
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 1;
|
|
|
|
rowbufsize = xres * 12;
|
|
|
|
saverow = pnmsaverow_ascii_indexed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-12-06 20:42:30 +05:30
|
|
|
g_warning ("PNM: Unknown drawable_type\n");
|
2015-01-20 20:14:07 +05:30
|
|
|
goto out;
|
2006-05-28 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 10:15:02 +01:00
|
|
|
else if (file_type != FILE_TYPE_PFM)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type == FILE_TYPE_PBM)
|
2006-05-28 21:29:59 +00:00
|
|
|
{
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P4\n";
|
2016-09-04 17:19:08 +02:00
|
|
|
format = gegl_buffer_get_format (buffer);
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 0;
|
2013-10-03 22:06:24 +02:00
|
|
|
rowbufsize = (gint) ceil ((gdouble) xres / 8.0);
|
2006-05-28 21:29:59 +00:00
|
|
|
saverow = pnmsaverow_raw_pbm;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (drawable_type)
|
|
|
|
{
|
|
|
|
case GIMP_GRAY_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P5\n";
|
2017-02-20 23:12:05 +00:00
|
|
|
if (rowinfo.bpc == 1)
|
|
|
|
{
|
|
|
|
format = babl_format ("Y' u8");
|
|
|
|
rowbufsize = xres;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = babl_format ("Y' u16");
|
|
|
|
rowbufsize = xres * 2;
|
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 1;
|
|
|
|
saverow = pnmsaverow_raw;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGB_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P6\n";
|
2017-02-20 23:12:05 +00:00
|
|
|
if (rowinfo.bpc == 1)
|
|
|
|
{
|
|
|
|
format = babl_format ("R'G'B' u8");
|
|
|
|
rowbufsize = xres * 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format = babl_format ("R'G'B' u16");
|
|
|
|
rowbufsize = xres * 6;
|
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 3;
|
|
|
|
saverow = pnmsaverow_raw;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_INDEXED_IMAGE:
|
2013-10-03 21:44:24 +02:00
|
|
|
header_string = "P6\n";
|
2012-11-26 01:31:39 +01:00
|
|
|
format = gegl_buffer_get_format (buffer);
|
2006-05-28 21:29:59 +00:00
|
|
|
np = 1;
|
|
|
|
rowbufsize = xres * 3;
|
|
|
|
saverow = pnmsaverow_raw_indexed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-12-06 20:42:30 +05:30
|
|
|
g_warning ("PNM: Unknown drawable_type\n");
|
2015-01-20 20:14:07 +05:30
|
|
|
goto out;
|
2006-05-28 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2015-01-20 20:14:07 +05:30
|
|
|
else
|
2013-10-03 21:44:24 +02:00
|
|
|
{
|
2015-01-20 20:14:07 +05:30
|
|
|
switch (drawable_type)
|
|
|
|
{
|
|
|
|
case GIMP_GRAY_IMAGE:
|
|
|
|
header_string = "Pf\n";
|
|
|
|
format = babl_format ("Y float");
|
|
|
|
np = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGB_IMAGE:
|
|
|
|
header_string = "PF\n";
|
|
|
|
format = babl_format ("RGB float");
|
|
|
|
np = 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_warning ("PFM: Unknown drawable_type\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2013-10-03 21:44:24 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
if (! output_write (output, header_string, strlen (header_string), error))
|
|
|
|
goto out;
|
|
|
|
|
2009-03-12 22:18:44 +00:00
|
|
|
rowinfo.zero_is_black = FALSE;
|
2009-03-05 14:08:56 +00:00
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
if (drawable_type == GIMP_INDEXED_IMAGE ||
|
|
|
|
drawable_type == GIMP_INDEXEDA_IMAGE)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2000-01-25 17:46:56 +00:00
|
|
|
guchar *cmap;
|
2009-03-12 22:18:44 +00:00
|
|
|
gint num_colors;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-05-23 23:37:46 +02:00
|
|
|
cmap = gimp_image_get_colormap (image, NULL, &num_colors);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type == FILE_TYPE_PBM)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2009-03-12 22:18:44 +00:00
|
|
|
/* Test which of the two colors is white and which is black */
|
|
|
|
switch (num_colors)
|
2009-03-05 14:08:56 +00:00
|
|
|
{
|
2009-03-12 22:18:44 +00:00
|
|
|
case 1:
|
|
|
|
rowinfo.zero_is_black = (GIMP_RGB_LUMINANCE (cmap[0],
|
|
|
|
cmap[1],
|
|
|
|
cmap[2]) < 128);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
rowinfo.zero_is_black = (GIMP_RGB_LUMINANCE (cmap[0],
|
|
|
|
cmap[1],
|
|
|
|
cmap[2]) <
|
|
|
|
GIMP_RGB_LUMINANCE (cmap[3],
|
|
|
|
cmap[4],
|
|
|
|
cmap[5]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-02-16 02:35:43 +01:00
|
|
|
g_warning ("Images exported as PBM should be black/white");
|
2009-03-12 22:18:44 +00:00
|
|
|
break;
|
2009-03-05 14:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-09 12:12:12 +01:00
|
|
|
const guchar *c = cmap;
|
|
|
|
gint i;
|
2009-03-05 14:08:56 +00:00
|
|
|
|
2009-03-12 22:18:44 +00:00
|
|
|
for (i = 0; i < num_colors; i++)
|
2009-03-05 14:08:56 +00:00
|
|
|
{
|
2010-01-09 12:12:12 +01:00
|
|
|
red[i] = *c++;
|
|
|
|
grn[i] = *c++;
|
|
|
|
blu[i] = *c++;
|
2023-01-17 02:33:16 +00:00
|
|
|
if (drawable_type == GIMP_INDEXEDA_IMAGE)
|
|
|
|
alpha[i] = 255;
|
2009-03-05 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rowinfo.red = red;
|
|
|
|
rowinfo.grn = grn;
|
|
|
|
rowinfo.blu = blu;
|
2023-01-17 02:33:16 +00:00
|
|
|
if (drawable_type == GIMP_INDEXEDA_IMAGE)
|
|
|
|
rowinfo.alpha = alpha;
|
|
|
|
else
|
|
|
|
rowinfo.alpha = NULL;
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2009-03-05 14:08:56 +00:00
|
|
|
g_free (cmap);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
if (file_type != FILE_TYPE_PFM && file_type != FILE_TYPE_PAM)
|
2015-01-21 15:24:22 +05:30
|
|
|
{
|
|
|
|
/* write out comment string */
|
|
|
|
comment = g_strdup_printf("# Created by GIMP version %s PNM plug-in\n",
|
|
|
|
GIMP_VERSION);
|
2015-01-20 20:14:07 +05:30
|
|
|
|
2015-01-21 15:24:22 +05:30
|
|
|
if (! output_write (output, comment, strlen (comment), error))
|
|
|
|
goto out;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* write out resolution and maxval */
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type == FILE_TYPE_PBM)
|
|
|
|
{
|
|
|
|
g_snprintf (buf, sizeof (buf), "%d %d\n", xres, yres);
|
|
|
|
}
|
|
|
|
else if (file_type != FILE_TYPE_PFM)
|
|
|
|
{
|
|
|
|
g_snprintf (buf, sizeof (buf), "%d %d\n%d\n", xres, yres,
|
|
|
|
rowinfo.bpc == 1 ? 255 : 65535);
|
|
|
|
}
|
2015-01-20 20:14:07 +05:30
|
|
|
else
|
2019-11-08 10:15:02 +01:00
|
|
|
{
|
|
|
|
g_snprintf (buf, sizeof (buf), "%d %d\n%s\n", xres, yres,
|
|
|
|
G_BYTE_ORDER == G_BIG_ENDIAN ? "1.0" : "-1.0");
|
|
|
|
}
|
2006-05-28 21:29:59 +00:00
|
|
|
|
2023-01-17 02:33:16 +00:00
|
|
|
if (file_type != FILE_TYPE_PAM)
|
|
|
|
if (! output_write (output, buf, strlen (buf), error))
|
|
|
|
goto out;
|
2015-01-20 20:14:07 +05:30
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
if (file_type != FILE_TYPE_PFM)
|
2013-10-03 21:44:24 +02:00
|
|
|
{
|
2015-01-20 20:14:07 +05:30
|
|
|
guchar *data;
|
|
|
|
guchar *d;
|
|
|
|
gchar *rowbuf = NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
/* allocate a buffer for retrieving information from the pixel region */
|
|
|
|
data = g_new (guchar,
|
|
|
|
gimp_tile_height () * xres *
|
|
|
|
babl_format_get_bytes_per_pixel (format));
|
2013-10-03 22:06:24 +02:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
rowbuf = g_new (gchar, rowbufsize + 1);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
rowinfo.output = output;
|
|
|
|
rowinfo.rowbuf = rowbuf;
|
|
|
|
rowinfo.xres = xres;
|
|
|
|
rowinfo.np = np;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
d = NULL; /* only to please the compiler */
|
1999-12-27 12:02:07 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
/* Write the body out */
|
|
|
|
for (ypos = 0; ypos < yres; ypos++)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2015-01-20 20:14:07 +05:30
|
|
|
if ((ypos % gimp_tile_height ()) == 0)
|
|
|
|
{
|
|
|
|
yend = ypos + gimp_tile_height ();
|
|
|
|
yend = MIN (yend, yres);
|
|
|
|
|
|
|
|
gegl_buffer_get (buffer,
|
|
|
|
GEGL_RECTANGLE (0, ypos, xres, yend - ypos), 1.0,
|
|
|
|
format, data,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
|
|
|
|
|
|
|
d = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! saverow (&rowinfo, d, error))
|
|
|
|
{
|
|
|
|
g_free (rowbuf);
|
|
|
|
g_free (data);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-02-20 23:12:05 +00:00
|
|
|
d += xres * (np ? np : 1) * rowinfo.bpc;
|
2015-01-20 20:14:07 +05:30
|
|
|
|
|
|
|
if (ypos % 32 == 0)
|
|
|
|
gimp_progress_update ((double) ypos / (double) yres);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (rowbuf);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* allocate a buffer for retrieving information from the pixel
|
2019-11-08 10:15:02 +01:00
|
|
|
* region
|
|
|
|
*/
|
2015-01-20 20:14:07 +05:30
|
|
|
gfloat *data = g_new (gfloat, xres * np);
|
|
|
|
|
|
|
|
rowinfo.output = output;
|
|
|
|
rowinfo.rowbuf = NULL;
|
|
|
|
rowinfo.xres = xres;
|
|
|
|
rowinfo.np = np;
|
2012-11-26 01:31:39 +01:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
/* Write the body out in reverse row order */
|
|
|
|
for (ypos = yres - 1; ypos >= 0; ypos--)
|
|
|
|
{
|
2012-11-26 01:31:39 +01:00
|
|
|
gegl_buffer_get (buffer,
|
2015-01-20 20:14:07 +05:30
|
|
|
GEGL_RECTANGLE (0, ypos, xres, 1), 1.0,
|
2015-01-20 21:00:04 +05:30
|
|
|
format, data,
|
2012-11-26 01:31:39 +01:00
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
if (! pnmsaverow_float (&rowinfo, data, error))
|
|
|
|
{
|
|
|
|
g_free (data);
|
|
|
|
goto out;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
if (ypos % 32 == 0)
|
|
|
|
gimp_progress_update ((double) (yres - ypos) / (double) yres);
|
2013-10-03 21:44:24 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
g_free (data);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-26 01:31:39 +01:00
|
|
|
gimp_progress_update (1.0);
|
2015-01-20 20:14:07 +05:30
|
|
|
status = TRUE;
|
|
|
|
|
|
|
|
out:
|
2018-11-27 12:27:20 +01:00
|
|
|
if (! status)
|
|
|
|
{
|
|
|
|
GCancellable *cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
g_cancellable_cancel (cancellable);
|
|
|
|
g_output_stream_close (output, cancellable, NULL);
|
|
|
|
g_object_unref (cancellable);
|
|
|
|
}
|
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
if (comment)
|
|
|
|
g_free (comment);
|
|
|
|
if (buffer)
|
|
|
|
g_object_unref (buffer);
|
|
|
|
if (output)
|
|
|
|
g_object_unref (output);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2015-01-20 20:14:07 +05:30
|
|
|
return status;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 22:06:24 +02:00
|
|
|
static gboolean
|
2019-11-08 10:15:02 +01:00
|
|
|
save_dialog (GimpProcedure *procedure,
|
2023-04-02 02:55:17 +00:00
|
|
|
GObject *config,
|
|
|
|
GimpImage *image)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2023-04-02 02:55:17 +00:00
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *frame;
|
|
|
|
GtkListStore *store;
|
|
|
|
gboolean run;
|
|
|
|
|
|
|
|
dialog = gimp_save_procedure_dialog_new (GIMP_SAVE_PROCEDURE (procedure),
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
image);
|
1997-11-24 22:05:25 +00:00
|
|
|
/* file save type */
|
2023-04-02 02:55:17 +00:00
|
|
|
store = gimp_int_store_new (_("_ASCII"), 0,
|
|
|
|
_("_Raw"), 1,
|
|
|
|
NULL);
|
|
|
|
frame = gimp_procedure_dialog_get_int_radio (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"raw", GIMP_INT_STORE (store));
|
2019-11-08 10:15:02 +01:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (frame), 12);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-04-02 02:55:17 +00:00
|
|
|
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog), NULL);
|
2005-09-09 18:38:00 +00:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2005-09-09 18:38:00 +00:00
|
|
|
gtk_widget_destroy (dialog);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2003-11-06 15:27:05 +00:00
|
|
|
return run;
|
2000-01-25 17:46:56 +00:00
|
|
|
}
|
|
|
|
|
2003-11-06 15:27:05 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/**************** FILE SCANNER UTILITIES **************/
|
|
|
|
|
|
|
|
/* pnmscanner_create ---
|
|
|
|
* Creates a new scanner based on a file descriptor. The
|
|
|
|
* look ahead buffer is one character initially.
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static PNMScanner *
|
2013-10-03 19:59:54 +02:00
|
|
|
pnmscanner_create (GInputStream *input)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
PNMScanner *s;
|
2013-10-03 19:59:54 +02:00
|
|
|
gsize bytes_read;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2001-12-06 02:28:58 +00:00
|
|
|
s = g_new (PNMScanner, 1);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2013-10-03 19:59:54 +02:00
|
|
|
s->input = input;
|
2004-11-14 02:50:33 +00:00
|
|
|
s->inbuf = NULL;
|
2013-10-03 19:59:54 +02:00
|
|
|
s->eof = FALSE;
|
|
|
|
|
|
|
|
if (! g_input_stream_read_all (input, &s->cur, 1,
|
|
|
|
&bytes_read, NULL, NULL) ||
|
|
|
|
bytes_read != 1)
|
|
|
|
{
|
|
|
|
s->eof = TRUE;
|
|
|
|
}
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
return s;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pnmscanner_destroy ---
|
|
|
|
* Destroys a scanner and its resources. Doesn't close the fd.
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
1997-11-24 22:05:25 +00:00
|
|
|
pnmscanner_destroy (PNMScanner *s)
|
|
|
|
{
|
2006-02-04 01:02:25 +00:00
|
|
|
if (s->inbuf)
|
|
|
|
g_free (s->inbuf);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
g_free (s);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pnmscanner_createbuffer ---
|
|
|
|
* Creates a buffer so we can do buffered reads.
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
1997-11-24 22:05:25 +00:00
|
|
|
pnmscanner_createbuffer (PNMScanner *s,
|
2006-02-04 01:02:25 +00:00
|
|
|
gint bufsize)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
gsize bytes_read;
|
|
|
|
|
2007-10-27 16:50:55 +00:00
|
|
|
s->inbuf = g_new (gchar, bufsize);
|
|
|
|
s->inbufsize = bufsize;
|
|
|
|
s->inbufpos = 0;
|
2013-10-03 19:59:54 +02:00
|
|
|
s->inbufvalidsize = 0;
|
|
|
|
|
|
|
|
g_input_stream_read_all (s->input, s->inbuf, bufsize,
|
|
|
|
&bytes_read, NULL, NULL);
|
|
|
|
|
|
|
|
s->inbufvalidsize = bytes_read;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pnmscanner_gettoken ---
|
|
|
|
* Gets the next token, eating any leading whitespace.
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
1997-11-24 22:05:25 +00:00
|
|
|
pnmscanner_gettoken (PNMScanner *s,
|
2006-02-04 01:02:25 +00:00
|
|
|
gchar *buf,
|
|
|
|
gint bufsize)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2007-10-27 16:50:55 +00:00
|
|
|
gint ctr = 0;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2003-11-26 19:20:24 +00:00
|
|
|
pnmscanner_eatwhitespace (s);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2003-11-26 19:20:24 +00:00
|
|
|
while (! s->eof &&
|
|
|
|
! g_ascii_isspace (s->cur) &&
|
|
|
|
(s->cur != '#') &&
|
|
|
|
(ctr < bufsize))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
buf[ctr++] = s->cur;
|
2006-02-04 01:02:25 +00:00
|
|
|
pnmscanner_getchar (s);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2007-10-27 16:50:55 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
buf[ctr] = '\0';
|
|
|
|
}
|
|
|
|
|
2000-02-19 19:36:10 +00:00
|
|
|
/* pnmscanner_getsmalltoken ---
|
|
|
|
* Gets the next char, eating any leading whitespace.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pnmscanner_getsmalltoken (PNMScanner *s,
|
2006-02-04 01:02:25 +00:00
|
|
|
gchar *buf)
|
2000-02-19 19:36:10 +00:00
|
|
|
{
|
2006-02-04 01:02:25 +00:00
|
|
|
pnmscanner_eatwhitespace (s);
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2013-10-03 22:06:24 +02:00
|
|
|
if (! s->eof && ! g_ascii_isspace (s->cur) && (s->cur != '#'))
|
2000-02-19 19:36:10 +00:00
|
|
|
{
|
|
|
|
*buf = s->cur;
|
2006-02-04 01:02:25 +00:00
|
|
|
pnmscanner_getchar (s);
|
2000-02-19 19:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* pnmscanner_getchar ---
|
|
|
|
* Reads a character from the input stream
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
1997-11-24 22:05:25 +00:00
|
|
|
pnmscanner_getchar (PNMScanner *s)
|
|
|
|
{
|
|
|
|
if (s->inbuf)
|
|
|
|
{
|
|
|
|
s->cur = s->inbuf[s->inbufpos++];
|
2007-10-27 16:50:55 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
if (s->inbufpos >= s->inbufvalidsize)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
2006-05-28 21:29:59 +00:00
|
|
|
if (s->inbufpos > s->inbufvalidsize)
|
2013-10-03 19:59:54 +02:00
|
|
|
{
|
|
|
|
s->eof = 1;
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
else
|
2013-10-03 19:59:54 +02:00
|
|
|
{
|
|
|
|
gsize bytes_read;
|
|
|
|
|
|
|
|
g_input_stream_read_all (s->input, s->inbuf, s->inbufsize,
|
|
|
|
&bytes_read, NULL, NULL);
|
|
|
|
|
|
|
|
s->inbufvalidsize = bytes_read;
|
|
|
|
}
|
2007-10-27 16:50:55 +00:00
|
|
|
|
2006-02-04 01:02:25 +00:00
|
|
|
s->inbufpos = 0;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
else
|
2007-10-27 16:50:55 +00:00
|
|
|
{
|
2013-10-03 19:59:54 +02:00
|
|
|
gsize bytes_read;
|
|
|
|
|
|
|
|
s->eof = FALSE;
|
|
|
|
|
|
|
|
if (! g_input_stream_read_all (s->input, &s->cur, 1,
|
|
|
|
&bytes_read, NULL, NULL) ||
|
|
|
|
bytes_read != 1)
|
|
|
|
{
|
|
|
|
s->eof = TRUE;
|
|
|
|
}
|
2007-10-27 16:50:55 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pnmscanner_eatwhitespace ---
|
|
|
|
* Eats up whitespace from the input and returns when done or eof.
|
|
|
|
* Also deals with comments.
|
|
|
|
*/
|
1998-03-19 02:11:53 +00:00
|
|
|
static void
|
1997-11-24 22:05:25 +00:00
|
|
|
pnmscanner_eatwhitespace (PNMScanner *s)
|
|
|
|
{
|
2007-10-27 16:50:55 +00:00
|
|
|
gint state = 0;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-11-08 10:15:02 +01:00
|
|
|
while (! (s->eof) && (state != -1))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
switch (state)
|
2006-02-04 01:02:25 +00:00
|
|
|
{
|
|
|
|
case 0: /* in whitespace */
|
|
|
|
if (s->cur == '#')
|
|
|
|
{
|
|
|
|
state = 1; /* goto comment */
|
2007-10-27 16:50:55 +00:00
|
|
|
pnmscanner_getchar (s);
|
2006-02-04 01:02:25 +00:00
|
|
|
}
|
2013-10-03 22:06:24 +02:00
|
|
|
else if (! g_ascii_isspace (s->cur))
|
2007-10-27 16:50:55 +00:00
|
|
|
{
|
|
|
|
state = -1;
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
else
|
2007-10-27 16:50:55 +00:00
|
|
|
{
|
|
|
|
pnmscanner_getchar (s);
|
|
|
|
}
|
2006-02-04 01:02:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* in comment */
|
|
|
|
if (s->cur == '\n')
|
|
|
|
state = 0; /* goto whitespace */
|
|
|
|
pnmscanner_getchar (s);
|
|
|
|
break;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-15 17:14:57 +00:00
|
|
|
|
|
|
|
/* Code referenced from Jörg Walter */
|
|
|
|
/* pnmscanner_eatinnerspace ---
|
|
|
|
* Eats up spaces and tabs from PAM header input and returns when done or eof.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pnmscanner_eatinnerspace (PNMScanner *s)
|
|
|
|
{
|
|
|
|
while (! (s->eof) && (s->cur == ' ' || s->cur == '\t'))
|
|
|
|
pnmscanner_getchar (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Code referenced from Jörg Walter */
|
|
|
|
/* pnmscanner_getheaderline ---
|
|
|
|
* Gets the next header line (next token occurring at the start of a line)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pnmscanner_getheaderline (PNMScanner *s,
|
|
|
|
gchar *buf,
|
|
|
|
gint bufsize)
|
|
|
|
{
|
|
|
|
pnmscanner_eatinnerspace (s);
|
|
|
|
while (! (s->eof) && s->cur != '\n')
|
|
|
|
{
|
|
|
|
pnmscanner_gettoken (s, buf, bufsize);
|
|
|
|
pnmscanner_eatinnerspace (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
pnmscanner_gettoken (s, buf, bufsize);
|
2023-02-13 22:33:35 +01:00
|
|
|
}
|