1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include <sys/types.h>
36
#include <glib-object.h>
37
#include <glib/gstdio.h>
39
#include "libgimpbase/gimpbase.h"
42
#include "libgimpbase/gimpwin32-io.h"
45
#include "core-types.h"
47
#include "base/temp-buf.h"
49
#include "gimpbrush.h"
50
#include "gimpbrush-header.h"
51
#include "gimpbrush-load.h"
53
#include "gimp-intl.h"
56
/* stuff from abr2gbr Copyright (C) 2001 Marco Lamberto <lm@sunnyspot.org> */
57
/* the above is gpl see http://the.sunnyspot.org/gimp/ */
59
typedef struct _AbrHeader AbrHeader;
60
typedef struct _AbrBrushHeader AbrBrushHeader;
61
typedef struct _AbrSampledBrushHeader AbrSampledBrushHeader;
69
struct _AbrBrushHeader
75
struct _AbrSampledBrushHeader
81
gint32 bounds_long[4];
87
/* local function prototypes */
89
static GimpBrush * gimp_brush_load_abr_brush (FILE *file,
91
const gchar *filename,
93
static gchar abr_read_char (FILE *file);
94
static gint16 abr_read_short (FILE *file);
95
static gint32 abr_read_long (FILE *file);
98
/* public functions */
101
gimp_brush_load (const gchar *filename,
107
g_return_val_if_fail (filename != NULL, NULL);
108
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
109
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
111
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
114
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
115
_("Could not open '%s' for reading: %s"),
116
gimp_filename_to_utf8 (filename), g_strerror (errno));
120
brush = gimp_brush_load_brush (fd, filename, error);
127
return g_list_prepend (NULL, brush);
131
gimp_brush_load_brush (gint fd,
132
const gchar *filename,
142
gboolean success = TRUE;
144
g_return_val_if_fail (filename != NULL, NULL);
145
g_return_val_if_fail (fd != -1, NULL);
146
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
148
/* Read in the header size */
149
if (read (fd, &header, sizeof (header)) != sizeof (header))
151
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
152
_("Could not read %d bytes from '%s': %s"),
153
(gint) sizeof (header),
154
gimp_filename_to_utf8 (filename), g_strerror (errno));
158
/* rearrange the bytes in each unsigned int */
159
header.header_size = g_ntohl (header.header_size);
160
header.version = g_ntohl (header.version);
161
header.width = g_ntohl (header.width);
162
header.height = g_ntohl (header.height);
163
header.bytes = g_ntohl (header.bytes);
164
header.magic_number = g_ntohl (header.magic_number);
165
header.spacing = g_ntohl (header.spacing);
167
/* Check for correct file format */
169
if (header.width == 0)
171
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
172
_("Fatal parse error in brush file '%s': "
174
gimp_filename_to_utf8 (filename));
178
if (header.height == 0)
180
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
181
_("Fatal parse error in brush file '%s': "
183
gimp_filename_to_utf8 (filename));
187
if (header.bytes == 0)
189
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
190
_("Fatal parse error in brush file '%s': "
192
gimp_filename_to_utf8 (filename));
196
switch (header.version)
199
/* If this is a version 1 brush, set the fp back 8 bytes */
200
lseek (fd, -8, SEEK_CUR);
201
header.header_size += 8;
202
/* spacing is not defined in version 1 */
206
case 3: /* cinepaint brush */
207
if (header.bytes == 18 /* FLOAT16_GRAY_GIMAGE */)
213
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
214
_("Fatal parse error in brush file '%s': "
215
"Unknown depth %d."),
216
gimp_filename_to_utf8 (filename), header.bytes);
222
if (header.magic_number == GBRUSH_MAGIC)
226
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
227
_("Fatal parse error in brush file '%s': "
228
"Unknown version %d."),
229
gimp_filename_to_utf8 (filename), header.version);
233
/* Read in the brush name */
234
if ((bn_size = (header.header_size - sizeof (header))))
238
name = g_new (gchar, bn_size);
240
if ((read (fd, name, bn_size)) < bn_size)
242
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
243
_("Fatal parse error in brush file '%s': "
244
"File appears truncated."),
245
gimp_filename_to_utf8 (filename));
250
utf8 = gimp_any_to_utf8 (name, -1,
251
_("Invalid UTF-8 string in brush file '%s'."),
252
gimp_filename_to_utf8 (filename));
258
name = g_strdup (_("Unnamed"));
260
brush = g_object_new (GIMP_TYPE_BRUSH,
262
"mime-type", "image/x-gimp-gbr",
266
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
268
mask = temp_buf_data (brush->mask);
269
size = header.width * header.height * header.bytes;
271
switch (header.bytes)
274
success = (read (fd, mask, size) == size);
277
case 2: /* cinepaint brush, 16 bit floats */
279
guchar buf[8 * 1024];
281
for (i = 0; success && i < size;)
283
gssize bytes = MIN (size - i, sizeof (buf));
285
success = (read (fd, buf, bytes) == bytes);
289
guint16 *b = (guint16 *) buf;
293
for (; bytes > 0; bytes -= 2, mask++, b++)
301
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
302
short_float.u[0] = 0;
303
short_float.u[1] = GUINT16_FROM_BE (*b);
305
short_float.u[0] = GUINT16_FROM_BE (*b);
306
short_float.u[1] = 0;
309
*mask = (guchar) (short_float.f * 255.0 + 0.5);
318
guchar buf[8 * 1024];
320
brush->pixmap = temp_buf_new (header.width, header.height,
322
pixmap = temp_buf_data (brush->pixmap);
324
for (i = 0; success && i < size;)
326
gssize bytes = MIN (size - i, sizeof (buf));
328
success = (read (fd, buf, bytes) == bytes);
336
for (; bytes > 0; bytes -= 4, pixmap += 3, mask++, b += 4)
350
g_object_unref (brush);
351
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
352
_("Fatal parse error in brush file '%s': "
353
"Unsupported brush depth %d\n"
354
"GIMP brushes must be GRAY or RGBA."),
355
gimp_filename_to_utf8 (filename), header.bytes);
361
g_object_unref (brush);
362
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
363
_("Fatal parse error in brush file '%s': "
364
"File appears truncated."),
365
gimp_filename_to_utf8 (filename));
369
brush->spacing = header.spacing;
370
brush->x_axis.x = header.width / 2.0;
371
brush->x_axis.y = 0.0;
372
brush->y_axis.x = 0.0;
373
brush->y_axis.y = header.height / 2.0;
379
gimp_brush_load_abr (const gchar *filename,
384
GList *brush_list = NULL;
386
g_return_val_if_fail (filename != NULL, NULL);
387
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
388
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
390
file = g_fopen (filename, "rb");
394
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
395
_("Could not open '%s' for reading: %s"),
396
gimp_filename_to_utf8 (filename), g_strerror (errno));
400
abr_hdr.version = abr_read_short (file);
401
abr_hdr.count = abr_read_short (file);
403
/* g_print("version: %d count: %d\n", abr_hdr.version, abr_hdr.count); */
405
if (abr_hdr.version > 1)
407
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
408
_("Fatal parse error in brush file '%s': "
409
"unable to decode abr format version %d."),
410
gimp_filename_to_utf8 (filename), abr_hdr.version);
416
for (i = 0; i < abr_hdr.count; i++)
420
brush = gimp_brush_load_abr_brush (file, i, filename, error);
424
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
425
_("Fatal parse error in brush file '%s'"),
426
gimp_filename_to_utf8 (filename));
430
brush_list = g_list_prepend (brush_list, brush);
436
return g_list_reverse (brush_list);
440
/* private functions */
443
gimp_brush_load_abr_brush (FILE *file,
445
const gchar *filename,
448
GimpBrush *brush = NULL;
449
AbrBrushHeader abr_brush_hdr;
451
g_return_val_if_fail (filename != NULL, NULL);
452
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
454
abr_brush_hdr.type = abr_read_short (file);
455
abr_brush_hdr.size = abr_read_long (file);
457
/* g_print(" + BRUSH\n | << type: %i block size: %i bytes\n",
458
* abr_brush_hdr.type, abr_brush_hdr.size);
461
switch (abr_brush_hdr.type)
463
case 1: /* computed brush */
464
/* FIXME: support it!
466
* We can probabaly feed the info into the generated brush code
467
* and get a useable brush back. It seems to support the same
470
g_printerr ("WARNING: computed brush unsupported, skipping.\n");
471
fseek (file, abr_brush_hdr.size, SEEK_CUR);
474
case 2: /* sampled brush */
476
AbrSampledBrushHeader abr_sampled_brush_hdr;
486
abr_sampled_brush_hdr.misc = abr_read_long (file);
487
abr_sampled_brush_hdr.spacing = abr_read_short (file);
488
abr_sampled_brush_hdr.antialiasing = abr_read_char (file);
490
for (i = 0; i < 4; i++)
491
abr_sampled_brush_hdr.bounds[i] = abr_read_short (file);
492
for (i = 0; i < 4; i++)
493
abr_sampled_brush_hdr.bounds_long[i] = abr_read_long (file);
495
abr_sampled_brush_hdr.depth = abr_read_short (file);
497
height = (abr_sampled_brush_hdr.bounds_long[2] -
498
abr_sampled_brush_hdr.bounds_long[0]); /* bottom - top */
499
width = (abr_sampled_brush_hdr.bounds_long[3] -
500
abr_sampled_brush_hdr.bounds_long[1]); /* right - left */
501
bytes = abr_sampled_brush_hdr.depth >> 3;
503
/* g_print("width %i height %i\n", width, height); */
505
abr_sampled_brush_hdr.wide = height > 16384;
507
if (abr_sampled_brush_hdr.wide)
509
/* FIXME: support wide brushes */
511
g_printerr ("WARING: wide brushes not supported\n");
515
tmp = g_filename_display_basename (filename);
516
name = g_strdup_printf ("%s-%03d", tmp, index);
519
brush = g_object_new (GIMP_TYPE_BRUSH,
521
/* FIXME: MIME type!! */
522
"mime-type", "application/x-photoshop-abr",
527
brush->spacing = abr_sampled_brush_hdr.spacing;
528
brush->x_axis.x = width / 2.0;
529
brush->x_axis.y = 0.0;
530
brush->y_axis.x = 0.0;
531
brush->y_axis.y = height / 2.0;
532
brush->mask = temp_buf_new (width, height,
535
mask = temp_buf_data (brush->mask);
536
size = width * height * bytes;
538
compress = abr_read_char (file);
540
/* g_print(" | << size: %dx%d %d bit (%d bytes) %s\n",
541
* width, height, abr_sampled_brush_hdr.depth, size,
542
* comppres ? "compressed" : "raw");
547
fread (mask, size, 1, file);
551
gint16 *cscanline_len;
556
/* read compressed size foreach scanline */
557
cscanline_len = g_new0 (gshort, height);
558
for (i = 0; i < height; i++)
559
cscanline_len[i] = abr_read_short (file);
561
/* unpack each scanline data */
562
for (i = 0; i < height; i++)
564
for (j = 0; j < cscanline_len[i];)
566
n = abr_read_char (file);
568
if (n >= 128) /* force sign */
573
/* copy the following char -n + 1 times */
575
if (n == -128) /* it's a nop */
579
ch = abr_read_char (file);
581
for (c = 0; c < n; c++, mask++)
586
/* read the following n + 1 chars (no compr) */
588
for (c = 0; c < n + 1; c++, j++, mask++)
589
*mask = (guchar) abr_read_char (file);
594
g_free (cscanline_len);
600
g_printerr ("WARNING: unknown brush type, skipping.\n");
601
fseek (file, abr_brush_hdr.size, SEEK_CUR);
609
abr_read_char (FILE *file)
615
abr_read_short (FILE *file)
619
fread (&val, sizeof (val), 1, file);
621
return GINT16_FROM_BE (val);
625
abr_read_long (FILE *file)
629
fread (&val, sizeof (val), 1, file);
631
return GINT32_FROM_BE (val);