Commit 410ffc43 authored by Michael Natterer's avatar Michael Natterer

app: move file-pat-load from the file-pat plug-in to the core

parent a6aa14bc
......@@ -17,4 +17,6 @@ libappfile_data_a_SOURCES = \
file-data.c \
file-data.h \
file-data-gbr.c \
file-data-gbr.h
file-data-gbr.h \
file-data-pat.c \
file-data-pat.h
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "core/core-types.h"
#include "core/gimp.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimplayer-new.h"
#include "core/gimpparamspecs.h"
#include "core/gimppattern.h"
#include "core/gimppattern-load.h"
#include "core/gimptempbuf.h"
#include "pdb/gimpprocedure.h"
#include "file-data-pat.h"
#include "gimp-intl.h"
/* local function prototypes */
static GimpImage * file_pat_pattern_to_image (Gimp *gimp,
GimpPattern *pattern);
static GimpPattern * file_pat_image_to_pattern (GimpImage *image);
/* public functions */
GimpValueArray *
file_pat_load_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
GimpValueArray *return_vals;
GimpImage *image = NULL;
const gchar *uri;
GFile *file;
GInputStream *input;
GError *my_error = NULL;
gimp_set_busy (gimp);
uri = g_value_get_string (gimp_value_array_index (args, 1));
file = g_file_new_for_uri (uri);
input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
if (input)
{
GList *list;
list = gimp_pattern_load (context, file, input, error);
if (list)
{
GimpPattern *pattern = list->data;
g_list_free (list);
image = file_pat_pattern_to_image (gimp, pattern);
g_object_unref (pattern);
}
g_object_unref (input);
}
else
{
g_propagate_prefixed_error (error, my_error,
_("Could not open '%s' for reading: "),
gimp_file_get_utf8_name (file));
}
g_object_unref (file);
return_vals = gimp_procedure_get_return_values (procedure, image != NULL,
error ? *error : NULL);
if (image)
gimp_value_set_image (gimp_value_array_index (return_vals, 1), image);
gimp_unset_busy (gimp);
return return_vals;
}
GimpValueArray *
file_pat_save_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
GimpValueArray *return_vals;
GimpImage *image;
GimpPattern *pattern;
const gchar *uri;
GFile *file;
gboolean success;
gimp_set_busy (gimp);
image = gimp_value_get_image (gimp_value_array_index (args, 1), gimp);
uri = g_value_get_string (gimp_value_array_index (args, 3));
file = g_file_new_for_uri (uri);
pattern = file_pat_image_to_pattern (image);
gimp_data_set_file (GIMP_DATA (pattern), file, TRUE, TRUE);
success = gimp_data_save (GIMP_DATA (pattern), error);
g_object_unref (pattern);
g_object_unref (file);
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
gimp_unset_busy (gimp);
return return_vals;
}
/* private functions */
static GimpImage *
file_pat_pattern_to_image (Gimp *gimp,
GimpPattern *pattern)
{
GimpImage *image;
GimpLayer *layer;
const Babl *format;
const gchar *name;
GimpImageBaseType base_type;
gboolean alpha;
gint width;
gint height;
GimpTempBuf *mask = gimp_pattern_get_mask (pattern);
GeglBuffer *buffer;
GimpParasite *parasite;
format = gimp_temp_buf_get_format (mask);
switch (babl_format_get_bytes_per_pixel (format))
{
case 1:
base_type = GIMP_GRAY;
alpha = FALSE;
break;
case 2:
base_type = GIMP_GRAY;
alpha = TRUE;
break;
case 3:
base_type = GIMP_RGB;
alpha = FALSE;
break;
case 4:
base_type = GIMP_RGB;
alpha = TRUE;
break;
default:
g_return_val_if_reached (NULL);
}
name = gimp_object_get_name (pattern);
width = gimp_temp_buf_get_width (mask);
height = gimp_temp_buf_get_height (mask);
image = gimp_image_new (gimp, width, height, base_type,
GIMP_PRECISION_U8_PERCEPTUAL);
parasite = gimp_parasite_new ("gimp-pattern-name",
GIMP_PARASITE_PERSISTENT,
strlen (name) + 1, name);
gimp_image_parasite_attach (image, parasite);
gimp_parasite_free (parasite);
format = gimp_image_get_layer_format (image, alpha);
layer = gimp_layer_new (image, width, height, format, name,
1.0, GIMP_LAYER_MODE_NORMAL);
gimp_image_add_layer (image, layer, NULL, 0, FALSE);
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, width, height), 0,
NULL,
gimp_temp_buf_get_data (mask), GEGL_AUTO_ROWSTRIDE);
return image;
}
static GimpPattern *
file_pat_image_to_pattern (GimpImage *image)
{
return NULL;
}
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __FILE_DATA_PAT_H__
#define __FILE_DATA_PAT_H__
GimpValueArray * file_pat_load_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error);
GimpValueArray * file_pat_save_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error);
#endif /* __FILE_DATA_PAT_H__ */
......@@ -32,6 +32,7 @@
#include "file-data.h"
#include "file-data-gbr.h"
#include "file-data-pat.h"
#include "gimp-intl.h"
......@@ -109,6 +110,68 @@ file_data_init (Gimp *gimp)
gimp_plug_in_manager_add_procedure (gimp->plug_in_manager, proc);
g_object_unref (procedure);
/* file-pat-load */
file = g_file_new_for_path ("file-pat-load");
procedure = gimp_plug_in_procedure_new (GIMP_PLUGIN, file);
g_object_unref (file);
procedure->proc_type = GIMP_INTERNAL;
procedure->marshal_func = file_pat_load_invoker;
proc = GIMP_PLUG_IN_PROCEDURE (procedure);
proc->menu_label = g_strdup (N_("GIMP pattern"));
gimp_plug_in_procedure_set_icon (proc, GIMP_ICON_TYPE_ICON_NAME,
(const guint8 *) "gimp-pattern",
strlen ("gimp-pattern") + 1);
gimp_plug_in_procedure_set_image_types (proc, NULL);
gimp_plug_in_procedure_set_file_proc (proc, "pat", "",
"20,string,GPAT");
gimp_plug_in_procedure_set_mime_types (proc, "image/gimp-x-pat");
gimp_plug_in_procedure_set_handles_uri (proc);
gimp_object_set_static_name (GIMP_OBJECT (procedure), "file-pat-load");
gimp_procedure_set_static_strings (procedure,
"file-pat-load",
"Loads GIMP patterns",
"Loads GIMP patterns",
"Tim Newsome, Michael Natterer",
"Tim Newsome, Michael Natterer",
"1997-2019",
NULL);
gimp_procedure_add_argument (procedure,
gimp_param_spec_int32 ("dummy-param",
"Dummy Param",
"Dummy parameter",
G_MININT32, G_MAXINT32, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_string ("uri",
"URI",
"The URI of the file "
"to load",
TRUE, FALSE, TRUE,
NULL,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_string ("raw-uri",
"Raw URI",
"The URI of the file "
"to load",
TRUE, FALSE, TRUE,
NULL,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_image_id ("image",
"Image",
"Output image",
gimp, FALSE,
GIMP_PARAM_READWRITE));
gimp_plug_in_manager_add_procedure (gimp->plug_in_manager, proc);
g_object_unref (procedure);
}
void
......
......@@ -29,7 +29,6 @@
#include "libgimp/stdplugins-intl.h"
#define LOAD_PROC "file-pat-load"
#define SAVE_PROC "file-pat-save"
#define PLUG_IN_BINARY "file-pat"
#define PLUG_IN_ROLE "gimp-file-pat"
......@@ -43,8 +42,6 @@ static void run (const gchar *name,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static gint32 load_image (GFile *file,
GError **error);
static gboolean save_image (GFile *file,
gint32 image_ID,
gint32 drawable_ID,
......@@ -72,17 +69,6 @@ MAIN ()
static void
query (void)
{
static const GimpParamDef load_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_STRING, "uri", "The URI of the file to load" },
{ GIMP_PDB_STRING, "raw-uri", "The URI of the file to load" }
};
static const GimpParamDef load_return_vals[] =
{
{ GIMP_PDB_IMAGE, "image", "Output image" }
};
static const GimpParamDef save_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
......@@ -93,29 +79,6 @@ query (void)
{ GIMP_PDB_STRING, "description", "Short description of the pattern" }
};
gimp_install_procedure (LOAD_PROC,
"Loads Gimp's .PAT pattern files",
"The images in the pattern dialog can be loaded "
"directly with this plug-in",
"Tim Newsome",
"Tim Newsome",
"1997",
N_("GIMP pattern"),
NULL,
GIMP_PLUGIN,
G_N_ELEMENTS (load_args),
G_N_ELEMENTS (load_return_vals),
load_args, load_return_vals);
gimp_plugin_icon_register (LOAD_PROC, GIMP_ICON_TYPE_ICON_NAME,
(const guint8 *) GIMP_ICON_PATTERN);
gimp_register_file_handler_mime (LOAD_PROC, "image/x-gimp-pat");
gimp_register_file_handler_uri (LOAD_PROC);
gimp_register_magic_load_handler (LOAD_PROC,
"pat",
"",
"20,string,GPAT");
gimp_install_procedure (SAVE_PROC,
"Exports Gimp pattern file (.PAT)",
"New Gimp patterns can be created by exporting them "
......@@ -162,23 +125,7 @@ run (const gchar *name,
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
if (strcmp (name, LOAD_PROC) == 0)
{
image_ID = load_image (g_file_new_for_uri (param[1].data.d_string),
&error);
if (image_ID != -1)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_IMAGE;
values[1].data.d_image = image_ID;
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
else if (strcmp (name, SAVE_PROC) == 0)
if (strcmp (name, SAVE_PROC) == 0)
{
GFile *file;
GimpParasite *parasite;
......@@ -314,198 +261,6 @@ run (const gchar *name,
values[0].data.d_status = status;
}
static gint32
load_image (GFile *file,
GError **error)
{
GInputStream *input;
GimpPatternHeader ph;
gchar *name = NULL;
gchar *temp;
guchar *buf;
gint32 image_ID;
gint32 layer_ID;
GimpParasite *parasite;
GeglBuffer *buffer;
const Babl *file_format;
gint line;
GimpImageBaseType base_type;
GimpImageType image_type;
gsize bytes_read;
gsize size;
gimp_progress_init_printf (_("Opening '%s'"),
g_file_get_parse_name (file));
input = G_INPUT_STREAM (g_file_read (file, NULL, error));
if (! input)
return -1;
if (! g_input_stream_read_all (input, &ph, sizeof (GimpPatternHeader),
&bytes_read, NULL, error) ||
bytes_read != sizeof (GimpPatternHeader))
{
g_object_unref (input);
return -1;
}
/* rearrange the bytes in each unsigned int */
ph.header_size = g_ntohl (ph.header_size);
ph.version = g_ntohl (ph.version);
ph.width = g_ntohl (ph.width);
ph.height = g_ntohl (ph.height);
ph.bytes = g_ntohl (ph.bytes);
ph.magic_number = g_ntohl (ph.magic_number);
if (ph.magic_number != GIMP_PATTERN_MAGIC ||
ph.version != 1 ||
ph.header_size <= sizeof (GimpPatternHeader))
{
g_object_unref (input);
return -1;
}
if ((size = (ph.header_size - sizeof (GimpPatternHeader))) > 0)
{
if (size > GIMP_PATTERN_MAX_NAME)
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Invalid header data in '%s': "
"Pattern name is too long: %lu"),
gimp_file_get_utf8_name (file),
(gulong) size);
return -1;
}
temp = g_new0 (gchar, size + 1);
if (! g_input_stream_read_all (input, temp, size,
&bytes_read, NULL, error) ||
bytes_read != size)
{
g_free (temp);
g_object_unref (input);
return -1;
}
name = gimp_any_to_utf8 (temp, size - 1,
_("Invalid UTF-8 string in pattern file '%s'."),
g_file_get_parse_name (file));
g_free (temp);
}
if (! name)
name = g_strdup (_("Unnamed"));
/* Now there's just raw data left. */
/*
* Create a new image of the proper size and associate the filename with it.
*/
switch (ph.bytes)
{
case 1:
base_type = GIMP_GRAY;
image_type = GIMP_GRAY_IMAGE;
file_format = babl_format ("Y' u8");
break;
case 2:
base_type = GIMP_GRAY;
image_type = GIMP_GRAYA_IMAGE;
file_format = babl_format ("Y'A u8");
break;
case 3:
base_type = GIMP_RGB;
image_type = GIMP_RGB_IMAGE;
file_format = babl_format ("R'G'B' u8");
break;
case 4:
base_type = GIMP_RGB;
image_type = GIMP_RGBA_IMAGE;
file_format = babl_format ("R'G'B'A u8");
break;
default:
g_message ("Unsupported pattern depth: %d\n"
"GIMP Patterns must be GRAY or RGB", ph.bytes);
g_object_unref (input);
return -1;
}
/* Sanitize input dimensions and guard against overflows. */
if ((ph.width == 0) || (ph.width > GIMP_PATTERN_MAX_SIZE) ||
(ph.height == 0) || (ph.height > GIMP_PATTERN_MAX_SIZE) ||
(G_MAXSIZE / ph.width / ph.bytes < 1))
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Invalid header data in '%s': width=%lu, height=%lu, "
"bytes=%lu"), g_file_get_parse_name (file),
(gulong) ph.width,
(gulong) ph.height,
(gulong) ph.bytes);
g_object_unref (input);
return -1;
}
image_ID = gimp_image_new (ph.width, ph.height, base_type);
gimp_image_set_filename (image_ID, g_file_get_uri (file));
parasite = gimp_parasite_new ("gimp-pattern-name",
GIMP_PARASITE_PERSISTENT,
strlen (name) + 1, name);
gimp_image_attach_parasite (image_ID, parasite);
gimp_parasite_free (parasite);
layer_ID = gimp_layer_new (image_ID, name, ph.width, ph.height,
image_type,
100,
gimp_image_get_default_new_layer_mode (image_ID));
gimp_image_insert_layer (image_ID, layer_ID, -1, 0);
g_free (name);
buffer = gimp_drawable_get_buffer (layer_ID);
/* this can't overflow because ph.width is <= GIMP_PATTERN_MAX_SIZE */
buf = g_malloc (ph.width * ph.bytes);
for (line = 0; line < ph.height; line++)
{
if (! g_input_stream_read_all (input, buf, ph.width * ph.bytes,
&bytes_read, NULL, error) ||
bytes_read != ph.width * ph.bytes)
{
if (line == 0)
{
g_free (buf);
g_object_unref (buffer);
g_object_unref (input);
return -1;
}
else
{
g_message ("GIMP Pattern file is truncated "
"(%d of %d lines recovered).",
line - 1, ph.height);
break;
}
}
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, line, ph.width, 1), 0,
file_format, buf, GEGL_AUTO_ROWSTRIDE);
gimp_progress_update ((gdouble) line / (gdouble) ph.height);
}
g_free (buf);
g_object_unref (buffer);
g_object_unref (input);
gimp_progress_update (1.0);
return image_ID;
}
static gboolean
save_image (GFile *file,
gint32 image_ID,
......
......@@ -279,6 +279,7 @@ app/file/file-save.c
app/file/file-utils.c
app/file-data/file-data-gbr.c
app/file-data/file-data-pat.c
app/file-data/file-data.c
app/gegl/gimp-babl.c
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment