2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-02-05 01:34:30 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2001-02-05 01:34:30 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-02-05 01: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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-02-05 01:34:30 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-05-03 09:36:22 +08:00
|
|
|
#include <cairo.h>
|
2012-03-18 04:59:10 +08:00
|
|
|
#include <gegl.h>
|
2012-05-03 09:36:22 +08:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2012-03-18 04:59:10 +08:00
|
|
|
|
2003-10-16 20:24:58 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2012-05-03 09:36:22 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2003-10-16 20:24:58 +08:00
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
#include "gimppattern.h"
|
2001-04-19 08:23:43 +08:00
|
|
|
#include "gimppattern-header.h"
|
2006-10-03 19:37:37 +08:00
|
|
|
#include "gimppattern-load.h"
|
2012-04-09 06:59:20 +08:00
|
|
|
#include "gimptempbuf.h"
|
2001-05-10 06:34:59 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
GList *
|
2014-07-04 09:31:03 +08:00
|
|
|
gimp_pattern_load (GimpContext *context,
|
|
|
|
GFile *file,
|
|
|
|
GInputStream *input,
|
|
|
|
GError **error)
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
|
|
|
GimpPattern *pattern = NULL;
|
2012-04-22 06:42:00 +08:00
|
|
|
const Babl *format = NULL;
|
2001-02-05 01:34:30 +08:00
|
|
|
PatternHeader header;
|
2014-07-03 04:29:57 +08:00
|
|
|
gsize size;
|
|
|
|
gsize bytes_read;
|
2001-02-05 01:34:30 +08:00
|
|
|
gint bn_size;
|
2014-07-04 09:31:03 +08:00
|
|
|
gchar *name = NULL;
|
2001-02-05 01:34:30 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-04 09:31:03 +08:00
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
|
2002-12-02 21:39:09 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2001-02-05 01:34:30 +08:00
|
|
|
|
2014-07-03 04:29:57 +08:00
|
|
|
/* read the size */
|
|
|
|
if (! g_input_stream_read_all (input, &header, sizeof (header),
|
|
|
|
&bytes_read, NULL, NULL) ||
|
|
|
|
bytes_read != sizeof (header))
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in pattern file '%s': "
|
2005-11-02 23:10:41 +08:00
|
|
|
"File appears truncated."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2002-12-02 21:39:09 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2001-02-05 01: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 */
|
2003-07-15 07:30:18 +08:00
|
|
|
if (header.magic_number != GPATTERN_MAGIC || header.version != 1 ||
|
|
|
|
header.header_size <= sizeof (header))
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in pattern file '%s': "
|
|
|
|
"Unknown pattern format version %d."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), header.version);
|
2001-02-05 01:34:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2003-07-15 07:30:18 +08:00
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
/* Check for supported bit depths */
|
2003-07-15 07:30:18 +08:00
|
|
|
if (header.bytes < 1 || header.bytes > 4)
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in pattern file '%s: "
|
|
|
|
"Unsupported pattern depth %d.\n"
|
2003-07-15 07:30:18 +08:00
|
|
|
"GIMP Patterns must be GRAY or RGB."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), header.bytes);
|
2001-02-05 01:34:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read in the pattern name */
|
|
|
|
if ((bn_size = (header.header_size - sizeof (header))))
|
|
|
|
{
|
2003-10-16 20:24:58 +08:00
|
|
|
gchar *utf8;
|
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
name = g_new (gchar, bn_size);
|
|
|
|
|
2014-07-03 04:29:57 +08:00
|
|
|
if (! g_input_stream_read_all (input, name, bn_size,
|
|
|
|
&bytes_read, NULL, NULL) ||
|
|
|
|
bytes_read != bn_size)
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in pattern file '%s': "
|
2005-11-02 23:10:41 +08:00
|
|
|
"File appears truncated."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2004-01-30 00:19:57 +08:00
|
|
|
g_free (name);
|
2005-07-24 06:22:45 +08:00
|
|
|
goto error;
|
2001-02-05 01:34:30 +08:00
|
|
|
}
|
2003-07-15 07:30:18 +08:00
|
|
|
|
2003-10-16 20:24:58 +08:00
|
|
|
utf8 = gimp_any_to_utf8 (name, -1,
|
|
|
|
_("Invalid UTF-8 string in pattern file '%s'."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2003-10-16 20:24:58 +08:00
|
|
|
g_free (name);
|
|
|
|
name = utf8;
|
2001-02-05 01:34:30 +08:00
|
|
|
}
|
2001-09-03 08:26:06 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
if (! name)
|
2001-09-03 08:26:06 +08:00
|
|
|
name = g_strdup (_("Unnamed"));
|
2001-02-05 01:34:30 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
pattern = g_object_new (GIMP_TYPE_PATTERN,
|
2005-05-26 07:25:45 +08:00
|
|
|
"name", name,
|
|
|
|
"mime-type", "image/x-gimp-pat",
|
2004-01-30 00:19:57 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_free (name);
|
2001-08-10 22:41:39 +08:00
|
|
|
|
2012-04-22 06:42:00 +08: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);
|
2014-07-03 04:29:57 +08:00
|
|
|
size = header.width * header.height * header.bytes;
|
2012-04-09 00:47:49 +08:00
|
|
|
|
2014-07-03 04:29:57 +08:00
|
|
|
if (! g_input_stream_read_all (input,
|
|
|
|
gimp_temp_buf_get_data (pattern->mask), size,
|
|
|
|
&bytes_read, NULL, NULL) ||
|
|
|
|
bytes_read != size)
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in pattern file '%s': "
|
2005-11-02 23:10:41 +08:00
|
|
|
"File appears truncated."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2001-02-05 01:34:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
return g_list_prepend (NULL, pattern);
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
error:
|
2014-07-04 09:31:03 +08:00
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
if (pattern)
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_unref (pattern);
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
GList *
|
2014-07-04 09:31:03 +08: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 19:03:53 +08:00
|
|
|
{
|
2014-07-04 09:31:03 +08: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 19:03:53 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-04 09:31:03 +08: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 19:03:53 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2014-07-03 04:29:57 +08:00
|
|
|
pixbuf = gdk_pixbuf_new_from_stream (input, NULL, error);
|
2004-06-03 19:34:51 +08:00
|
|
|
if (! pixbuf)
|
2014-07-01 20:25:37 +08: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 19:03:53 +08:00
|
|
|
|
2004-06-03 20:01:17 +08: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 19:03:53 +08:00
|
|
|
|
2004-06-03 19:34:51 +08:00
|
|
|
if (! name)
|
2014-07-01 20:25:37 +08: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 19:03:53 +08:00
|
|
|
|
|
|
|
pattern = g_object_new (GIMP_TYPE_PATTERN,
|
2005-05-26 07:25:45 +08: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 19:03:53 +08:00
|
|
|
NULL);
|
|
|
|
g_free (name);
|
|
|
|
|
2014-02-21 01: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 19:03:53 +08:00
|
|
|
|
2004-06-03 19:34:51 +08:00
|
|
|
g_object_unref (pixbuf);
|
|
|
|
|
2004-07-27 03:00:22 +08: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 19:03:53 +08:00
|
|
|
}
|