2006-12-09 13:33:38 -08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-02-04 09:34:30 -08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
|
*
|
2009-01-17 14:28:01 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2001-02-04 09:34:30 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 14:28:01 -08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-02-04 09:34:30 -08: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 14:47:19 -07:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2001-02-04 09:34:30 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2012-05-02 18:36:22 -07:00
|
|
|
#include <cairo.h>
|
2012-03-17 13:59:10 -07:00
|
|
|
#include <gegl.h>
|
2012-05-02 18:36:22 -07:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2012-03-17 13:59:10 -07:00
|
|
|
|
2003-10-16 05:24:58 -07:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2012-05-02 18:36:22 -07:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2003-10-16 05:24:58 -07:00
|
|
|
|
2001-05-09 15:34:59 -07:00
|
|
|
#include "core-types.h"
|
2001-02-04 09:34:30 -08:00
|
|
|
|
|
|
|
|
#include "gimppattern.h"
|
2001-04-18 17:23:43 -07:00
|
|
|
#include "gimppattern-header.h"
|
2006-10-03 04:37:37 -07:00
|
|
|
#include "gimppattern-load.h"
|
2012-04-08 15:59:20 -07:00
|
|
|
#include "gimptempbuf.h"
|
2001-05-09 15:34:59 -07:00
|
|
|
|
2003-03-25 08:38:19 -08:00
|
|
|
#include "gimp-intl.h"
|
2001-02-04 09:34:30 -08:00
|
|
|
|
|
|
|
|
|
2004-07-26 12:00:22 -07:00
|
|
|
GList *
|
2014-07-03 18:31:03 -07:00
|
|
|
gimp_pattern_load (GimpContext *context,
|
|
|
|
|
GFile *file,
|
|
|
|
|
GInputStream *input,
|
|
|
|
|
GError **error)
|
2001-02-04 09:34:30 -08:00
|
|
|
{
|
2018-07-06 03:06:08 -07:00
|
|
|
GimpPattern *pattern = NULL;
|
|
|
|
|
const Babl *format = NULL;
|
|
|
|
|
GimpPatternHeader header;
|
|
|
|
|
gsize size;
|
|
|
|
|
gsize bytes_read;
|
2018-07-06 04:07:28 -07:00
|
|
|
gsize bn_size;
|
2018-07-06 03:06:08 -07:00
|
|
|
gchar *name = NULL;
|
2001-02-04 09:34:30 -08:00
|
|
|
|
2014-06-30 17:30:22 -07:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-03 18:31:03 -07:00
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
|
2002-12-02 05:39:09 -08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2001-02-04 09:34:30 -08:00
|
|
|
|
2014-07-02 13:29:57 -07:00
|
|
|
/* read the size */
|
|
|
|
|
if (! g_input_stream_read_all (input, &header, sizeof (header),
|
2014-07-04 09:46:02 -07:00
|
|
|
&bytes_read, NULL, error) ||
|
2014-07-02 13:29:57 -07:00
|
|
|
bytes_read != sizeof (header))
|
2002-12-02 05:39:09 -08:00
|
|
|
{
|
2014-07-04 09:46:02 -07:00
|
|
|
g_prefix_error (error, _("File appears truncated: "));
|
2002-12-02 05:39:09 -08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2001-02-04 09:34:30 -08:00
|
|
|
|
|
|
|
|
/* rearrange the bytes in each unsigned int */
|
|
|
|
|
header.header_size = g_ntohl (header.header_size);
|
|
|
|
|
header.version = g_ntohl (header.version);
|
|
|
|
|
header.width = g_ntohl (header.width);
|
|
|
|
|
header.height = g_ntohl (header.height);
|
|
|
|
|
header.bytes = g_ntohl (header.bytes);
|
|
|
|
|
header.magic_number = g_ntohl (header.magic_number);
|
|
|
|
|
|
|
|
|
|
/* Check for correct file format */
|
2018-07-06 04:07:28 -07:00
|
|
|
if (header.magic_number != GIMP_PATTERN_MAGIC ||
|
|
|
|
|
header.version != 1 ||
|
|
|
|
|
header.header_size <= sizeof (header))
|
2001-02-04 09:34:30 -08:00
|
|
|
{
|
2002-12-02 05:39:09 -08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 09:46:02 -07:00
|
|
|
_("Unknown pattern format version %d."),
|
|
|
|
|
header.version);
|
2001-02-04 09:34:30 -08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2003-07-14 16:30:18 -07:00
|
|
|
|
2001-02-04 09:34:30 -08:00
|
|
|
/* Check for supported bit depths */
|
2003-07-14 16:30:18 -07:00
|
|
|
if (header.bytes < 1 || header.bytes > 4)
|
2001-02-04 09:34:30 -08:00
|
|
|
{
|
2002-12-02 05:39:09 -08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 09:46:02 -07:00
|
|
|
_("Unsupported pattern depth %d.\n"
|
2003-07-14 16:30:18 -07:00
|
|
|
"GIMP Patterns must be GRAY or RGB."),
|
2014-07-04 09:46:02 -07:00
|
|
|
header.bytes);
|
2001-02-04 09:34:30 -08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 14:09:08 -08:00
|
|
|
/* Validate dimensions */
|
2018-07-06 04:07:28 -07:00
|
|
|
if ((header.width == 0) || (header.width > GIMP_PATTERN_MAX_SIZE) ||
|
|
|
|
|
(header.height == 0) || (header.height > GIMP_PATTERN_MAX_SIZE) ||
|
2017-11-20 14:09:08 -08:00
|
|
|
(G_MAXSIZE / header.width / header.height / header.bytes < 1))
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
|
_("Invalid header data in '%s': width=%lu, height=%lu, "
|
|
|
|
|
"bytes=%lu"), gimp_file_get_utf8_name (file),
|
2018-07-06 04:07:28 -07:00
|
|
|
(gulong) header.width,
|
|
|
|
|
(gulong) header.height,
|
|
|
|
|
(gulong) header.bytes);
|
2017-11-20 14:09:08 -08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-04 09:34:30 -08:00
|
|
|
/* Read in the pattern name */
|
|
|
|
|
if ((bn_size = (header.header_size - sizeof (header))))
|
|
|
|
|
{
|
2003-10-16 05:24:58 -07:00
|
|
|
gchar *utf8;
|
|
|
|
|
|
2018-07-06 04:07:28 -07:00
|
|
|
if (bn_size > GIMP_PATTERN_MAX_NAME)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
|
_("Invalid header data in '%s': "
|
|
|
|
|
"Pattern name is too long: %lu"),
|
|
|
|
|
gimp_file_get_utf8_name (file),
|
|
|
|
|
(gulong) bn_size);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = g_new0 (gchar, bn_size + 1);
|
2001-02-04 09:34:30 -08:00
|
|
|
|
2014-07-02 13:29:57 -07:00
|
|
|
if (! g_input_stream_read_all (input, name, bn_size,
|
2014-07-04 09:46:02 -07:00
|
|
|
&bytes_read, NULL, error) ||
|
2014-07-02 13:29:57 -07:00
|
|
|
bytes_read != bn_size)
|
2001-02-04 09:34:30 -08:00
|
|
|
{
|
2014-07-04 09:46:02 -07:00
|
|
|
g_prefix_error (error, _("File appears truncated."));
|
2004-01-29 08:19:57 -08:00
|
|
|
g_free (name);
|
2005-07-23 15:22:45 -07:00
|
|
|
goto error;
|
2001-02-04 09:34:30 -08:00
|
|
|
}
|
2003-07-14 16:30:18 -07:00
|
|
|
|
2017-10-31 04:11:08 -07:00
|
|
|
utf8 = gimp_any_to_utf8 (name, bn_size - 1,
|
2003-10-16 05:24:58 -07:00
|
|
|
_("Invalid UTF-8 string in pattern file '%s'."),
|
2014-07-01 05:25:37 -07:00
|
|
|
gimp_file_get_utf8_name (file));
|
2003-10-16 05:24:58 -07:00
|
|
|
g_free (name);
|
|
|
|
|
name = utf8;
|
2001-02-04 09:34:30 -08:00
|
|
|
}
|
2001-09-02 17:26:06 -07:00
|
|
|
|
2004-01-29 08:19:57 -08:00
|
|
|
if (! name)
|
2001-09-02 17:26:06 -07:00
|
|
|
name = g_strdup (_("Unnamed"));
|
2001-02-04 09:34:30 -08:00
|
|
|
|
2004-01-29 08:19:57 -08:00
|
|
|
pattern = g_object_new (GIMP_TYPE_PATTERN,
|
2005-05-25 16:25:45 -07:00
|
|
|
"name", name,
|
|
|
|
|
"mime-type", "image/x-gimp-pat",
|
2004-01-29 08:19:57 -08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
g_free (name);
|
2001-08-10 07:41:39 -07:00
|
|
|
|
2012-04-21 15:42:00 -07:00
|
|
|
switch (header.bytes)
|
|
|
|
|
{
|
|
|
|
|
case 1: format = babl_format ("Y' u8"); break;
|
|
|
|
|
case 2: format = babl_format ("Y'A u8"); break;
|
|
|
|
|
case 3: format = babl_format ("R'G'B' u8"); break;
|
|
|
|
|
case 4: format = babl_format ("R'G'B'A u8"); break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pattern->mask = gimp_temp_buf_new (header.width, header.height, format);
|
2017-11-20 14:09:08 -08:00
|
|
|
size = (gsize) header.width * header.height * header.bytes;
|
2012-04-08 09:47:49 -07:00
|
|
|
|
2014-07-02 13:29:57 -07:00
|
|
|
if (! g_input_stream_read_all (input,
|
|
|
|
|
gimp_temp_buf_get_data (pattern->mask), size,
|
2014-07-04 09:46:02 -07:00
|
|
|
&bytes_read, NULL, error) ||
|
2014-07-02 13:29:57 -07:00
|
|
|
bytes_read != size)
|
2001-02-04 09:34:30 -08:00
|
|
|
{
|
2014-07-04 09:46:02 -07:00
|
|
|
g_prefix_error (error, _("File appears truncated."));
|
2001-02-04 09:34:30 -08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-26 12:00:22 -07:00
|
|
|
return g_list_prepend (NULL, pattern);
|
2001-02-04 09:34:30 -08:00
|
|
|
|
|
|
|
|
error:
|
2014-07-03 18:31:03 -07:00
|
|
|
|
2001-02-04 09:34:30 -08:00
|
|
|
if (pattern)
|
2003-01-05 14:07:10 -08:00
|
|
|
g_object_unref (pattern);
|
2001-02-04 09:34:30 -08:00
|
|
|
|
2014-07-04 09:46:02 -07:00
|
|
|
g_prefix_error (error, _("Fatal parse error in pattern file: "));
|
|
|
|
|
|
2001-02-04 09:34:30 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-26 12:00:22 -07:00
|
|
|
GList *
|
2014-07-03 18:31:03 -07:00
|
|
|
gimp_pattern_load_pixbuf (GimpContext *context,
|
|
|
|
|
GFile *file,
|
|
|
|
|
GInputStream *input,
|
|
|
|
|
GError **error)
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
{
|
2014-07-03 18:31:03 -07:00
|
|
|
GimpPattern *pattern;
|
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
gchar *name;
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
|
2014-06-30 17:30:22 -07:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-03 18:31:03 -07:00
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2014-07-02 13:29:57 -07:00
|
|
|
pixbuf = gdk_pixbuf_new_from_stream (input, NULL, error);
|
2004-06-03 04:34:51 -07:00
|
|
|
if (! pixbuf)
|
2014-07-01 05:25:37 -07:00
|
|
|
return NULL;
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
|
2004-06-03 05:01:17 -07:00
|
|
|
name = g_strdup (gdk_pixbuf_get_option (pixbuf, "tEXt::Title"));
|
|
|
|
|
|
|
|
|
|
if (! name)
|
|
|
|
|
name = g_strdup (gdk_pixbuf_get_option (pixbuf, "tEXt::Comment"));
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
|
2004-06-03 04:34:51 -07:00
|
|
|
if (! name)
|
2014-07-01 05:25:37 -07:00
|
|
|
name = g_path_get_basename (gimp_file_get_utf8_name (file));
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
|
|
|
|
|
pattern = g_object_new (GIMP_TYPE_PATTERN,
|
2005-05-25 16:25:45 -07:00
|
|
|
"name", name,
|
|
|
|
|
"mime-type", NULL, /* FIXME!! */
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
NULL);
|
|
|
|
|
g_free (name);
|
|
|
|
|
|
2014-02-20 09:20:17 -08:00
|
|
|
pattern->mask = gimp_temp_buf_new_from_pixbuf (pixbuf, NULL);
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
|
2004-06-03 04:34:51 -07:00
|
|
|
g_object_unref (pixbuf);
|
|
|
|
|
|
2004-07-26 12:00:22 -07:00
|
|
|
return g_list_prepend (NULL, pattern);
|
Add support for GdkPixbuf patterns, so now all of png, jpex, pnm, xbm,
2004-06-03 Dave Neary <bolsh@gimp.org>
* app/core/gimp.c:
* app/core/gimpdatafactory.c:
* app/core/gimppattern.[ch]: Add support for GdkPixbuf patterns,
so now all of png, jpex, pnm, xbm, bmp, gif, ico, pcx, ras, tga,
xpm and tiff can be used for patterns.
2004-06-03 04:03:53 -07:00
|
|
|
}
|