Commit 4ffe1f03 authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

Added loading of Photoshop brushes. Fixes bug #163047:

2005-04-15  Michael Natterer  <mitch@gimp.org>

	Added loading of Photoshop brushes. Fixes bug #163047:

	* app/core/Makefile.am
	* app/core/gimpbrush-load.[ch]: new files holding all brush
	loading code. Added .abr loader based on a patch by Adrian
	Likins. Didn't add a new brush subclass as in the original patch,
	but only a loading function which loads the brushes into standard
	GimpBrush objects. Did misc fixes to the code, needs further
	cleanup.

	* app/core/gimpbrush.[ch]: removed brush loading code here.

	* app/core/gimpbrushpipe.c: changed #includes accordingly.

	* app/core/gimp.c (gimp_real_initialize): register the new load
	functions and their extensions with the brush factory.
parent d92c1cf3
2005-04-15 Michael Natterer <mitch@gimp.org>
Added loading of Photoshop brushes. Fixes bug #163047:
* app/core/Makefile.am
* app/core/gimpbrush-load.[ch]: new files holding all brush
loading code. Added .abr loader based on a patch by Adrian
Likins. Didn't add a new brush subclass as in the original patch,
but only a loading function which loads the brushes into standard
GimpBrush objects. Did misc fixes to the code, needs further
cleanup.
* app/core/gimpbrush.[ch]: removed brush loading code here.
* app/core/gimpbrushpipe.c: changed #includes accordingly.
* app/core/gimp.c (gimp_real_initialize): register the new load
functions and their extensions with the brush factory.
2005-04-15 Simon Budig <simon@gimp.org>
* app/tools/gimpnewrectselecttool.c: NULLified the Keyboard
......
......@@ -48,6 +48,8 @@ libappcore_a_sources = \
gimpbrush.c \
gimpbrush.h \
gimpbrush-header.h \
gimpbrush-load.c \
gimpbrush-load.h \
gimpbrushgenerated.c \
gimpbrushgenerated.h \
gimpbrushpipe.c \
......
......@@ -48,6 +48,7 @@
#include "gimp-units.h"
#include "gimp-utils.h"
#include "gimpbrush.h"
#include "gimpbrush-load.h"
#include "gimpbrushgenerated.h"
#include "gimpbrushpipe.h"
#include "gimpbuffer.h"
......@@ -547,6 +548,8 @@ gimp_real_initialize (Gimp *gimp,
{
{ gimp_brush_load, GIMP_BRUSH_FILE_EXTENSION, FALSE },
{ gimp_brush_load, GIMP_BRUSH_PIXMAP_FILE_EXTENSION, FALSE },
{ gimp_brush_load_abr, GIMP_BRUSH_PS_FILE_EXTENSION, FALSE },
{ gimp_brush_load_abr, GIMP_BRUSH_PSP_FILE_EXTENSION, FALSE },
{ gimp_brush_generated_load, GIMP_BRUSH_GENERATED_FILE_EXTENSION, TRUE },
{ gimp_brush_pipe_load, GIMP_BRUSH_PIPE_FILE_EXTENSION, FALSE }
};
......
......@@ -18,28 +18,7 @@
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32 /* gets defined by glib.h */
#include <io.h>
#endif
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
#include "libgimpbase/gimpbase.h"
......@@ -51,7 +30,7 @@
#include "config/gimpbaseconfig.h"
#include "gimpbrush.h"
#include "gimpbrush-header.h"
#include "gimpbrush-load.h"
#include "gimpbrushgenerated.h"
#include "gimpmarshal.h"
......@@ -378,46 +357,6 @@ gimp_brush_get_standard (void)
return standard_brush;
}
GList *
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpBrush *brush;
gint fd;
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);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (fd == -1)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return NULL;
}
brush = gimp_brush_load_brush (fd, filename, error);
close (fd);
if (! brush)
return NULL;
/* Swap the brush to disk (if we're being stingy with memory) */
if (stingy_memory_use)
{
temp_buf_swap (brush->mask);
if (brush->pixmap)
temp_buf_swap (brush->pixmap);
}
return g_list_prepend (NULL, brush);
}
GimpBrush *
gimp_brush_select_brush (GimpBrush *brush,
GimpCoords *last_coords,
......@@ -509,251 +448,3 @@ gimp_brush_spacing_changed (GimpBrush *brush)
g_signal_emit (brush, brush_signals[SPACING_CHANGED], 0);
}
GimpBrush *
gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error)
{
GimpBrush *brush;
gint bn_size;
BrushHeader header;
gchar *name = NULL;
guchar *pixmap;
guchar *mask;
gssize i, size;
gboolean success = TRUE;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (fd != -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
/* Read in the header size */
if (read (fd, &header, sizeof (header)) != sizeof (header))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Could not read %d bytes from '%s': %s"),
(gint) sizeof (header),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return NULL;
}
/* 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);
header.spacing = g_ntohl (header.spacing);
/* Check for correct file format */
if (header.width == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Width = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
if (header.height == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Height = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
if (header.bytes == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Bytes = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
switch (header.version)
{
case 1:
/* If this is a version 1 brush, set the fp back 8 bytes */
lseek (fd, -8, SEEK_CUR);
header.header_size += 8;
/* spacing is not defined in version 1 */
header.spacing = 25;
break;
case 3: /* cinepaint brush */
if (header.bytes == 18 /* FLOAT16_GRAY_GIMAGE */)
{
header.bytes = 2;
}
else
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown depth %d."),
gimp_filename_to_utf8 (filename), header.bytes);
return NULL;
}
/* fallthrough */
case 2:
if (header.magic_number == GBRUSH_MAGIC)
break;
default:
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown version %d."),
gimp_filename_to_utf8 (filename), header.version);
return NULL;
}
/* Read in the brush name */
if ((bn_size = (header.header_size - sizeof (header))))
{
gchar *utf8;
name = g_new (gchar, bn_size);
if ((read (fd, name, bn_size)) < bn_size)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"File appears truncated."),
gimp_filename_to_utf8 (filename));
g_free (name);
return NULL;
}
utf8 = gimp_any_to_utf8 (name, -1,
_("Invalid UTF-8 string in brush file '%s'."),
gimp_filename_to_utf8 (filename));
g_free (name);
name = utf8;
}
if (!name)
name = g_strdup (_("Unnamed"));
brush = g_object_new (GIMP_TYPE_BRUSH,
"name", name,
NULL);
g_free (name);
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
mask = temp_buf_data (brush->mask);
size = header.width * header.height * header.bytes;
switch (header.bytes)
{
case 1:
success = (read (fd, mask, size) == size);
break;
case 2: /* cinepaint brush, 16 bit floats */
{
guchar buf[8 * 1024];
for (i = 0; success && i < size;)
{
gssize bytes = MIN (size - i, sizeof (buf));
success = (read (fd, buf, bytes) == bytes);
if (success)
{
guint16 *b = (guint16 *) buf;
i += bytes;
for (; bytes > 0; bytes -= 2, mask++, b++)
{
union
{
guint16 u[2];
gfloat f;
} short_float;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
short_float.u[0] = 0;
short_float.u[1] = GUINT16_FROM_BE (*b);
#else
short_float.u[0] = GUINT16_FROM_BE (*b);
short_float.u[1] = 0;
#endif
*mask = (guchar) (short_float.f * 255.0 + 0.5);
}
}
}
}
break;
case 4:
{
guchar buf[8 * 1024];
brush->pixmap = temp_buf_new (header.width, header.height,
3, 0, 0, NULL);
pixmap = temp_buf_data (brush->pixmap);
for (i = 0; success && i < size;)
{
gssize bytes = MIN (size - i, sizeof (buf));
success = (read (fd, buf, bytes) == bytes);
if (success)
{
guchar *b = buf;
i += bytes;
for (; bytes > 0; bytes -= 4, pixmap += 3, mask++, b += 4)
{
pixmap[0] = b[0];
pixmap[1] = b[1];
pixmap[2] = b[2];
mask[0] = b[3];
}
}
}
}
break;
default:
g_object_unref (brush);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unsupported brush depth %d\n"
"GIMP brushes must be GRAY or RGBA."),
gimp_filename_to_utf8 (filename), header.bytes);
return NULL;
}
if (! success)
{
g_object_unref (brush);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"File appears truncated."),
gimp_filename_to_utf8 (filename));
return NULL;
}
brush->spacing = header.spacing;
brush->x_axis.x = header.width / 2.0;
brush->x_axis.y = 0.0;
brush->y_axis.x = 0.0;
brush->y_axis.y = header.height / 2.0;
return brush;
}
/* The GIMP -- an 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __GIMP_BRUSH_LOAD_H__
#define __GIMP_BRUSH_LOAD_H__
#define GIMP_BRUSH_FILE_EXTENSION ".gbr"
#define GIMP_BRUSH_PIXMAP_FILE_EXTENSION ".gpb"
#define GIMP_BRUSH_PS_FILE_EXTENSION ".abr"
#define GIMP_BRUSH_PSP_FILE_EXTENSION ".jbr"
GList * gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
GimpBrush * gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error);
GList * gimp_brush_load_abr (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
#endif /* __GIMP_BRUSH_LOAD_H__ */
......@@ -18,28 +18,7 @@
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32 /* gets defined by glib.h */
#include <io.h>
#endif
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
#include "libgimpbase/gimpbase.h"
......@@ -51,7 +30,7 @@
#include "config/gimpbaseconfig.h"
#include "gimpbrush.h"
#include "gimpbrush-header.h"
#include "gimpbrush-load.h"
#include "gimpbrushgenerated.h"
#include "gimpmarshal.h"
......@@ -378,46 +357,6 @@ gimp_brush_get_standard (void)
return standard_brush;
}
GList *
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpBrush *brush;
gint fd;
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);
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
if (fd == -1)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return NULL;
}
brush = gimp_brush_load_brush (fd, filename, error);
close (fd);
if (! brush)
return NULL;
/* Swap the brush to disk (if we're being stingy with memory) */
if (stingy_memory_use)
{
temp_buf_swap (brush->mask);
if (brush->pixmap)
temp_buf_swap (brush->pixmap);
}
return g_list_prepend (NULL, brush);
}
GimpBrush *
gimp_brush_select_brush (GimpBrush *brush,
GimpCoords *last_coords,
......@@ -509,251 +448,3 @@ gimp_brush_spacing_changed (GimpBrush *brush)
g_signal_emit (brush, brush_signals[SPACING_CHANGED], 0);
}
GimpBrush *
gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error)
{
GimpBrush *brush;
gint bn_size;
BrushHeader header;
gchar *name = NULL;
guchar *pixmap;
guchar *mask;
gssize i, size;
gboolean success = TRUE;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (fd != -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
/* Read in the header size */
if (read (fd, &header, sizeof (header)) != sizeof (header))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Could not read %d bytes from '%s': %s"),
(gint) sizeof (header),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return NULL;
}
/* 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);
header.spacing = g_ntohl (header.spacing);
/* Check for correct file format */
if (header.width == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Width = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
if (header.height == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Height = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
if (header.bytes == 0)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Bytes = 0."),
gimp_filename_to_utf8 (filename));
return NULL;
}
switch (header.version)
{
case 1:
/* If this is a version 1 brush, set the fp back 8 bytes */
lseek (fd, -8, SEEK_CUR);
header.header_size += 8;
/* spacing is not defined in version 1 */
header.spacing = 25;
break;
case 3: /* cinepaint brush */
if (header.bytes == 18 /* FLOAT16_GRAY_GIMAGE */)
{
header.bytes = 2;
}
else
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown depth %d."),
gimp_filename_to_utf8 (filename), header.bytes);
return NULL;
}
/* fallthrough */
case 2:
if (header.magic_number == GBRUSH_MAGIC)
break;
default:
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parse error in brush file '%s': "
"Unknown version %d."),
gimp_filename_to_utf8 (filename), header.version);
return NULL;
}