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"
|
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
#include <errno.h>
|
2001-02-05 01:34:30 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-02-07 09:16:18 +08:00
|
|
|
#include <stdio.h>
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-02-07 09:24:22 +08:00
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
#include <gegl.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
#include <glib/gstdio.h>
|
2001-02-07 09:16:18 +08:00
|
|
|
|
2001-02-05 01:34:30 +08:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _O_BINARY
|
|
|
|
#define _O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2012-03-18 04:59:10 +08:00
|
|
|
#include <gegl.h>
|
|
|
|
|
2003-10-16 20:24:58 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2001-02-05 01:34:30 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/temp-buf.h"
|
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
#include "gegl/gimp-gegl-utils.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"
|
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 *
|
2010-04-11 19:12:41 +08:00
|
|
|
gimp_pattern_load (GimpContext *context,
|
|
|
|
const gchar *filename,
|
2004-06-03 19:34:51 +08:00
|
|
|
GError **error)
|
2001-02-05 01:34:30 +08:00
|
|
|
{
|
|
|
|
GimpPattern *pattern = NULL;
|
|
|
|
gint fd;
|
|
|
|
PatternHeader header;
|
|
|
|
gint bn_size;
|
|
|
|
gchar *name = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2004-01-30 00:19:57 +08:00
|
|
|
g_return_val_if_fail (g_path_is_absolute (filename), 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
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
|
2001-02-05 01:34:30 +08:00
|
|
|
if (fd == -1)
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
|
|
|
|
_("Could not open '%s' for reading: %s"),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
2002-12-02 21:39:09 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
/* Read in the header size */
|
|
|
|
if (read (fd, &header, sizeof (header)) != 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."),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
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."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), 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."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), 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);
|
|
|
|
|
|
|
|
if ((read (fd, name, bn_size)) < bn_size)
|
|
|
|
{
|
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."),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
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'."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
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-09 05:56:52 +08:00
|
|
|
pattern->mask = gimp_temp_buf_new (header.width, header.height,
|
|
|
|
gimp_bpp_to_babl_format (header.bytes));
|
2012-04-09 00:47:49 +08:00
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
if (read (fd, gimp_temp_buf_get_data (pattern->mask),
|
2001-02-05 01:34:30 +08:00
|
|
|
header.width * header.height * header.bytes) <
|
|
|
|
header.width * header.height * header.bytes)
|
|
|
|
{
|
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."),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
2001-02-05 01:34:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
return g_list_prepend (NULL, pattern);
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (pattern)
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_unref (pattern);
|
2001-02-05 01:34:30 +08:00
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
GList *
|
2010-04-11 19:12:41 +08:00
|
|
|
gimp_pattern_load_pixbuf (GimpContext *context,
|
|
|
|
const gchar *filename,
|
2004-06-03 19:34:51 +08:00
|
|
|
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
|
|
|
{
|
2004-06-03 19:34:51 +08:00
|
|
|
GimpPattern *pattern;
|
|
|
|
GdkPixbuf *pixbuf;
|
2012-04-09 00:47:49 +08:00
|
|
|
GeglBuffer *src_buffer;
|
|
|
|
GeglBuffer *dest_buffer;
|
2004-06-03 19:34:51 +08:00
|
|
|
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
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2004-06-03 19:34:51 +08:00
|
|
|
pixbuf = gdk_pixbuf_new_from_file (filename, 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
|
|
|
|
2004-06-03 19:34:51 +08:00
|
|
|
if (! pixbuf)
|
|
|
|
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)
|
2005-02-21 05:49:53 +08:00
|
|
|
name = g_filename_display_basename (filename);
|
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);
|
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
pattern->mask =
|
2012-04-09 05:56:52 +08:00
|
|
|
gimp_temp_buf_new (gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf),
|
|
|
|
gimp_bpp_to_babl_format (gdk_pixbuf_get_n_channels (pixbuf)));
|
2004-06-03 19:34:51 +08:00
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
src_buffer = gimp_pixbuf_create_buffer (pixbuf);
|
|
|
|
dest_buffer = gimp_temp_buf_create_buffer (pattern->mask, FALSE);
|
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
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
gegl_buffer_copy (src_buffer, NULL, dest_buffer, 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
|
|
|
|
2012-04-09 00:47:49 +08:00
|
|
|
g_object_unref (src_buffer);
|
|
|
|
g_object_unref (dest_buffer);
|
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
|
|
|
}
|