1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpbrush-load.c
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21
22 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <gegl.h>
24
25 #include "libgimpbase/gimpbase.h"
26
27 #include "core-types.h"
28
29 #include "gimpbrush.h"
30 #include "gimpbrush-header.h"
31 #include "gimpbrush-load.h"
32 #include "gimpbrush-private.h"
33 #include "gimppattern-header.h"
34 #include "gimptempbuf.h"
35
36 #include "gimp-intl.h"
37
38 /* stuff from abr2gbr Copyright (C) 2001 Marco Lamberto <lm@sunnyspot.org> */
39 /* the above is GPL see http://the.sunnyspot.org/gimp/ */
40
41 typedef struct _AbrHeader AbrHeader;
42 typedef struct _AbrBrushHeader AbrBrushHeader;
43 typedef struct _AbrSampledBrushHeader AbrSampledBrushHeader;
44
45 struct _AbrHeader
46 {
47 gint16 version;
48 gint16 count;
49 };
50
51 struct _AbrBrushHeader
52 {
53 gint16 type;
54 gint32 size;
55 };
56
57 struct _AbrSampledBrushHeader
58 {
59 gint32 misc;
60 gint16 spacing;
61 gchar antialiasing;
62 gint16 bounds[4];
63 gint32 bounds_long[4];
64 gint16 depth;
65 gboolean wide;
66 };
67
68
69 /* local function prototypes */
70
71 static GList * gimp_brush_load_abr_v12 (GDataInputStream *input,
72 AbrHeader *abr_hdr,
73 GFile *file,
74 GError **error);
75 static GList * gimp_brush_load_abr_v6 (GDataInputStream *input,
76 AbrHeader *abr_hdr,
77 GFile *file,
78 GError **error);
79 static GimpBrush * gimp_brush_load_abr_brush_v12 (GDataInputStream *input,
80 AbrHeader *abr_hdr,
81 gint index,
82 GFile *file,
83 GError **error);
84 static GimpBrush * gimp_brush_load_abr_brush_v6 (GDataInputStream *input,
85 AbrHeader *abr_hdr,
86 gint32 max_offset,
87 gint index,
88 GFile *file,
89 GError **error);
90
91 static gchar abr_read_char (GDataInputStream *input,
92 GError **error);
93 static gint16 abr_read_short (GDataInputStream *input,
94 GError **error);
95 static gint32 abr_read_long (GDataInputStream *input,
96 GError **error);
97 static gchar * abr_read_ucs2_text (GDataInputStream *input,
98 GError **error);
99 static gboolean abr_supported (AbrHeader *abr_hdr,
100 GError **error);
101 static gboolean abr_reach_8bim_section (GDataInputStream *input,
102 const gchar *name,
103 GError **error);
104 static gboolean abr_rle_decode (GDataInputStream *input,
105 gchar *buffer,
106 gsize buffer_size,
107 gint32 height,
108 GError **error);
109
110
111 /* public functions */
112
113 GList *
gimp_brush_load(GimpContext * context,GFile * file,GInputStream * input,GError ** error)114 gimp_brush_load (GimpContext *context,
115 GFile *file,
116 GInputStream *input,
117 GError **error)
118 {
119 GimpBrush *brush;
120
121 g_return_val_if_fail (G_IS_FILE (file), NULL);
122 g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
123 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
124
125 brush = gimp_brush_load_brush (context, file, input, error);
126 if (! brush)
127 return NULL;
128
129 return g_list_prepend (NULL, brush);
130 }
131
132 GimpBrush *
gimp_brush_load_brush(GimpContext * context,GFile * file,GInputStream * input,GError ** error)133 gimp_brush_load_brush (GimpContext *context,
134 GFile *file,
135 GInputStream *input,
136 GError **error)
137 {
138 GimpBrush *brush;
139 gsize bn_size;
140 GimpBrushHeader header;
141 gchar *name = NULL;
142 guchar *mask;
143 gsize bytes_read;
144 gssize i, size;
145 gboolean success = TRUE;
146
147 g_return_val_if_fail (G_IS_FILE (file), NULL);
148 g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
149 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
150
151 /* read the header */
152 if (! g_input_stream_read_all (input, &header, sizeof (header),
153 &bytes_read, NULL, error) ||
154 bytes_read != sizeof (header))
155 {
156 return NULL;
157 }
158
159 /* rearrange the bytes in each unsigned int */
160 header.header_size = g_ntohl (header.header_size);
161 header.version = g_ntohl (header.version);
162 header.width = g_ntohl (header.width);
163 header.height = g_ntohl (header.height);
164 header.bytes = g_ntohl (header.bytes);
165 header.magic_number = g_ntohl (header.magic_number);
166 header.spacing = g_ntohl (header.spacing);
167
168 /* Check for correct file format */
169
170 if (header.width == 0)
171 {
172 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
173 _("Fatal parse error in brush file: Width = 0."));
174 return NULL;
175 }
176
177 if (header.height == 0)
178 {
179 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
180 _("Fatal parse error in brush file: Height = 0."));
181 return NULL;
182 }
183
184 if (header.bytes == 0)
185 {
186 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
187 _("Fatal parse error in brush file: Bytes = 0."));
188 return NULL;
189 }
190
191 if (header.width > GIMP_BRUSH_MAX_SIZE ||
192 header.height > GIMP_BRUSH_MAX_SIZE ||
193 G_MAXSIZE / header.width / header.height / MAX (4, header.bytes) < 1)
194 {
195 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
196 _("Fatal parse error in brush file: %dx%d over max size."),
197 header.width, header.height);
198 return NULL;
199 }
200
201 switch (header.version)
202 {
203 case 1:
204 /* If this is a version 1 brush, set the fp back 8 bytes */
205 if (! g_seekable_seek (G_SEEKABLE (input), -8, G_SEEK_CUR,
206 NULL, error))
207 return NULL;
208
209 header.header_size += 8;
210 /* spacing is not defined in version 1 */
211 header.spacing = 25;
212 break;
213
214 case 3: /* cinepaint brush */
215 if (header.bytes == 18 /* FLOAT16_GRAY_GIMAGE */)
216 {
217 header.bytes = 2;
218 }
219 else
220 {
221 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
222 _("Fatal parse error in brush file: Unknown depth %d."),
223 header.bytes);
224 return NULL;
225 }
226 /* fallthrough */
227
228 case 2:
229 if (header.magic_number == GIMP_BRUSH_MAGIC)
230 break;
231
232 default:
233 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
234 _("Fatal parse error in brush file: Unknown version %d."),
235 header.version);
236 return NULL;
237 }
238
239 if (header.header_size < sizeof (GimpBrushHeader))
240 {
241 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
242 _("Unsupported brush format"));
243 return NULL;
244 }
245
246 /* Read in the brush name */
247 if ((bn_size = (header.header_size - sizeof (header))))
248 {
249 gchar *utf8;
250
251 if (bn_size > GIMP_BRUSH_MAX_NAME)
252 {
253 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
254 _("Invalid header data in '%s': "
255 "Brush name is too long: %lu"),
256 gimp_file_get_utf8_name (file),
257 (gulong) bn_size);
258 return NULL;
259 }
260
261 name = g_new0 (gchar, bn_size + 1);
262
263 if (! g_input_stream_read_all (input, name, bn_size,
264 &bytes_read, NULL, error) ||
265 bytes_read != bn_size)
266 {
267 g_free (name);
268 return NULL;
269 }
270
271 utf8 = gimp_any_to_utf8 (name, bn_size - 1,
272 _("Invalid UTF-8 string in brush file '%s'."),
273 gimp_file_get_utf8_name (file));
274 g_free (name);
275 name = utf8;
276 }
277
278 if (! name)
279 name = g_strdup (_("Unnamed"));
280
281 brush = g_object_new (GIMP_TYPE_BRUSH,
282 "name", name,
283 "mime-type", "image/x-gimp-gbr",
284 NULL);
285 g_free (name);
286
287 brush->priv->mask = gimp_temp_buf_new (header.width, header.height,
288 babl_format ("Y u8"));
289
290 mask = gimp_temp_buf_get_data (brush->priv->mask);
291 size = header.width * header.height * header.bytes;
292
293 switch (header.bytes)
294 {
295 case 1:
296 success = (g_input_stream_read_all (input, mask, size,
297 &bytes_read, NULL, error) &&
298 bytes_read == size);
299
300 /* For backwards-compatibility, check if a pattern follows.
301 * The obsolete .gpb format did it this way.
302 */
303 if (success)
304 {
305 GimpPatternHeader ph;
306 goffset rewind;
307
308 rewind = g_seekable_tell (G_SEEKABLE (input));
309
310 if (g_input_stream_read_all (input, &ph, sizeof (GimpPatternHeader),
311 &bytes_read, NULL, NULL) &&
312 bytes_read == sizeof (GimpPatternHeader))
313 {
314 /* rearrange the bytes in each unsigned int */
315 ph.header_size = g_ntohl (ph.header_size);
316 ph.version = g_ntohl (ph.version);
317 ph.width = g_ntohl (ph.width);
318 ph.height = g_ntohl (ph.height);
319 ph.bytes = g_ntohl (ph.bytes);
320 ph.magic_number = g_ntohl (ph.magic_number);
321
322 if (ph.magic_number == GIMP_PATTERN_MAGIC &&
323 ph.version == 1 &&
324 ph.header_size > sizeof (GimpPatternHeader) &&
325 ph.bytes == 3 &&
326 ph.width == header.width &&
327 ph.height == header.height &&
328 g_input_stream_skip (input,
329 ph.header_size -
330 sizeof (GimpPatternHeader),
331 NULL, NULL) ==
332 ph.header_size - sizeof (GimpPatternHeader))
333 {
334 guchar *pixmap;
335 gssize pixmap_size;
336
337 brush->priv->pixmap =
338 gimp_temp_buf_new (header.width, header.height,
339 babl_format ("R'G'B' u8"));
340
341 pixmap = gimp_temp_buf_get_data (brush->priv->pixmap);
342
343 pixmap_size = gimp_temp_buf_get_data_size (brush->priv->pixmap);
344
345 success = (g_input_stream_read_all (input, pixmap,
346 pixmap_size,
347 &bytes_read, NULL,
348 error) &&
349 bytes_read == pixmap_size);
350 }
351 else
352 {
353 /* seek back if pattern wasn't found */
354 success = g_seekable_seek (G_SEEKABLE (input),
355 rewind, G_SEEK_SET,
356 NULL, error);
357 }
358 }
359 }
360 break;
361
362 case 2: /* cinepaint brush, 16 bit floats */
363 {
364 guchar buf[8 * 1024];
365
366 for (i = 0; success && i < size;)
367 {
368 gssize bytes = MIN (size - i, sizeof (buf));
369
370 success = (g_input_stream_read_all (input, buf, bytes,
371 &bytes_read, NULL, error) &&
372 bytes_read == bytes);
373
374 if (success)
375 {
376 guint16 *b = (guint16 *) buf;
377
378 i += bytes;
379
380 for (; bytes > 0; bytes -= 2, mask++, b++)
381 {
382 union
383 {
384 guint16 u[2];
385 gfloat f;
386 } short_float;
387
388 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
389 short_float.u[0] = 0;
390 short_float.u[1] = GUINT16_FROM_BE (*b);
391 #else
392 short_float.u[0] = GUINT16_FROM_BE (*b);
393 short_float.u[1] = 0;
394 #endif
395
396 *mask = (guchar) (short_float.f * 255.0 + 0.5);
397 }
398 }
399 }
400 }
401 break;
402
403 case 4:
404 {
405 guchar *pixmap;
406 guchar buf[8 * 1024];
407
408 brush->priv->pixmap = gimp_temp_buf_new (header.width, header.height,
409 babl_format ("R'G'B' u8"));
410 pixmap = gimp_temp_buf_get_data (brush->priv->pixmap);
411
412 for (i = 0; success && i < size;)
413 {
414 gssize bytes = MIN (size - i, sizeof (buf));
415
416 success = (g_input_stream_read_all (input, buf, bytes,
417 &bytes_read, NULL, error) &&
418 bytes_read == bytes);
419
420 if (success)
421 {
422 guchar *b = buf;
423
424 i += bytes;
425
426 for (; bytes > 0; bytes -= 4, pixmap += 3, mask++, b += 4)
427 {
428 pixmap[0] = b[0];
429 pixmap[1] = b[1];
430 pixmap[2] = b[2];
431
432 mask[0] = b[3];
433 }
434 }
435 }
436 }
437 break;
438
439 default:
440 g_object_unref (brush);
441 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
442 _("Fatal parse error in brush file:\n"
443 "Unsupported brush depth %d\n"
444 "GIMP brushes must be GRAY or RGBA."),
445 header.bytes);
446 return NULL;
447 }
448
449 if (! success)
450 {
451 g_object_unref (brush);
452 return NULL;
453 }
454
455 brush->priv->spacing = header.spacing;
456 brush->priv->x_axis.x = header.width / 2.0;
457 brush->priv->x_axis.y = 0.0;
458 brush->priv->y_axis.x = 0.0;
459 brush->priv->y_axis.y = header.height / 2.0;
460
461 return brush;
462 }
463
464 GList *
gimp_brush_load_abr(GimpContext * context,GFile * file,GInputStream * input,GError ** error)465 gimp_brush_load_abr (GimpContext *context,
466 GFile *file,
467 GInputStream *input,
468 GError **error)
469 {
470 GDataInputStream *data_input;
471 AbrHeader abr_hdr;
472 GList *brush_list = NULL;
473 GError *my_error = NULL;
474
475 g_return_val_if_fail (G_IS_FILE (file), NULL);
476 g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
477 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
478
479 data_input = g_data_input_stream_new (input);
480
481 g_data_input_stream_set_byte_order (data_input,
482 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
483
484 abr_hdr.version = abr_read_short (data_input, &my_error);
485 if (my_error)
486 goto done;
487
488 /* sub-version for ABR v6 */
489 abr_hdr.count = abr_read_short (data_input, &my_error);
490 if (my_error)
491 goto done;
492
493 if (abr_supported (&abr_hdr, &my_error))
494 {
495 switch (abr_hdr.version)
496 {
497 case 1:
498 case 2:
499 brush_list = gimp_brush_load_abr_v12 (data_input, &abr_hdr,
500 file, &my_error);
501 break;
502
503 case 10:
504 case 6:
505 brush_list = gimp_brush_load_abr_v6 (data_input, &abr_hdr,
506 file, &my_error);
507 break;
508 }
509 }
510
511 done:
512
513 g_object_unref (data_input);
514
515 if (! brush_list)
516 {
517 if (! my_error)
518 g_set_error (&my_error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
519 _("Unable to decode abr format version %d."),
520 abr_hdr.version);
521 }
522
523 if (my_error)
524 g_propagate_error (error, my_error);
525
526 return g_list_reverse (brush_list);
527 }
528
529
530 /* private functions */
531
532 static GList *
gimp_brush_load_abr_v12(GDataInputStream * input,AbrHeader * abr_hdr,GFile * file,GError ** error)533 gimp_brush_load_abr_v12 (GDataInputStream *input,
534 AbrHeader *abr_hdr,
535 GFile *file,
536 GError **error)
537 {
538 GList *brush_list = NULL;
539 gint i;
540
541 for (i = 0; i < abr_hdr->count; i++)
542 {
543 GimpBrush *brush;
544 GError *my_error = NULL;
545
546 brush = gimp_brush_load_abr_brush_v12 (input, abr_hdr, i,
547 file, &my_error);
548
549 /* a NULL brush without an error means an unsupported brush
550 * type was encountered, silently skip it and try the next one
551 */
552
553 if (brush)
554 {
555 brush_list = g_list_prepend (brush_list, brush);
556 }
557 else if (my_error)
558 {
559 g_propagate_error (error, my_error);
560 break;
561 }
562 }
563
564 return brush_list;
565 }
566
567 static GList *
gimp_brush_load_abr_v6(GDataInputStream * input,AbrHeader * abr_hdr,GFile * file,GError ** error)568 gimp_brush_load_abr_v6 (GDataInputStream *input,
569 AbrHeader *abr_hdr,
570 GFile *file,
571 GError **error)
572 {
573 GList *brush_list = NULL;
574 gint32 sample_section_size;
575 goffset sample_section_end;
576 gint i = 1;
577
578 if (! abr_reach_8bim_section (input, "samp", error))
579 return brush_list;
580
581 sample_section_size = abr_read_long (input, error);
582 if (error && *error)
583 return brush_list;
584
585 sample_section_end = (sample_section_size +
586 g_seekable_tell (G_SEEKABLE (input)));
587
588 while (g_seekable_tell (G_SEEKABLE (input)) < sample_section_end)
589 {
590 GimpBrush *brush;
591 GError *my_error = NULL;
592
593 brush = gimp_brush_load_abr_brush_v6 (input, abr_hdr, sample_section_end,
594 i, file, &my_error);
595
596 /* a NULL brush without an error means an unsupported brush
597 * type was encountered, silently skip it and try the next one
598 */
599
600 if (brush)
601 {
602 brush_list = g_list_prepend (brush_list, brush);
603 }
604 else if (my_error)
605 {
606 g_propagate_error (error, my_error);
607 break;
608 }
609
610 i++;
611 }
612
613 return brush_list;
614 }
615
616 static GimpBrush *
gimp_brush_load_abr_brush_v12(GDataInputStream * input,AbrHeader * abr_hdr,gint index,GFile * file,GError ** error)617 gimp_brush_load_abr_brush_v12 (GDataInputStream *input,
618 AbrHeader *abr_hdr,
619 gint index,
620 GFile *file,
621 GError **error)
622 {
623 GimpBrush *brush = NULL;
624 AbrBrushHeader abr_brush_hdr;
625
626 abr_brush_hdr.type = abr_read_short (input, error);
627 if (error && *error)
628 return NULL;
629
630 abr_brush_hdr.size = abr_read_long (input, error);
631 if (error && *error)
632 return NULL;
633
634 if (abr_brush_hdr.size < 0)
635 {
636 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
637 _("Fatal parse error in brush file: "
638 "Brush size value corrupt."));
639 return NULL;
640 }
641
642 /* g_print(" + BRUSH\n | << type: %i block size: %i bytes\n",
643 * abr_brush_hdr.type, abr_brush_hdr.size);
644 */
645
646 switch (abr_brush_hdr.type)
647 {
648 case 1: /* computed brush */
649 /* FIXME: support it!
650 *
651 * We can probabaly feed the info into the generated brush code
652 * and get a usable brush back. It seems to support the same
653 * types -akl
654 */
655 g_printerr ("WARNING: computed brush unsupported, skipping.\n");
656 g_seekable_seek (G_SEEKABLE (input), abr_brush_hdr.size,
657 G_SEEK_CUR, NULL, NULL);
658 break;
659
660 case 2: /* sampled brush */
661 {
662 AbrSampledBrushHeader abr_sampled_brush_hdr;
663 gint width, height;
664 gint bytes;
665 gint size;
666 guchar *mask;
667 gint i;
668 gchar *name;
669 gchar *sample_name = NULL;
670 gchar *tmp;
671 gshort compress;
672
673 abr_sampled_brush_hdr.misc = abr_read_long (input, error);
674 if (error && *error)
675 break;
676
677 abr_sampled_brush_hdr.spacing = abr_read_short (input, error);
678 if (error && *error)
679 break;
680
681 if (abr_hdr->version == 2)
682 {
683 sample_name = abr_read_ucs2_text (input, error);
684 if (error && *error)
685 break;
686 }
687
688 abr_sampled_brush_hdr.antialiasing = abr_read_char (input, error);
689 if (error && *error)
690 break;
691
692 for (i = 0; i < 4; i++)
693 {
694 abr_sampled_brush_hdr.bounds[i] = abr_read_short (input, error);
695 if (error && *error)
696 break;
697 }
698
699 for (i = 0; i < 4; i++)
700 {
701 abr_sampled_brush_hdr.bounds_long[i] = abr_read_long (input, error);
702 if (error && *error)
703 break;
704 }
705
706 abr_sampled_brush_hdr.depth = abr_read_short (input, error);
707 if (error && *error)
708 break;
709
710 height = (abr_sampled_brush_hdr.bounds_long[2] -
711 abr_sampled_brush_hdr.bounds_long[0]); /* bottom - top */
712 width = (abr_sampled_brush_hdr.bounds_long[3] -
713 abr_sampled_brush_hdr.bounds_long[1]); /* right - left */
714 bytes = abr_sampled_brush_hdr.depth >> 3;
715
716 /* g_print ("width %i height %i bytes %i\n", width, height, bytes); */
717
718 if (width < 1 || width > 10000 ||
719 height < 1 || height > 10000 ||
720 bytes < 1 || bytes > 1 ||
721 G_MAXSIZE / width / height / bytes < 1)
722 {
723 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
724 _("Fatal parse error in brush file: "
725 "Brush dimensions out of range."));
726 break;
727 }
728
729 abr_sampled_brush_hdr.wide = height > 16384;
730
731 if (abr_sampled_brush_hdr.wide)
732 {
733 /* FIXME: support wide brushes */
734
735 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
736 _("Fatal parse error in brush file: "
737 "Wide brushes are not supported."));
738 break;
739 }
740
741 tmp = g_path_get_basename (gimp_file_get_utf8_name (file));
742 if (! sample_name)
743 {
744 /* build name from filename and index */
745 name = g_strdup_printf ("%s-%03d", tmp, index);
746 }
747 else
748 {
749 /* build name from filename and sample name */
750 name = g_strdup_printf ("%s-%s", tmp, sample_name);
751 g_free (sample_name);
752 }
753 g_free (tmp);
754
755 brush = g_object_new (GIMP_TYPE_BRUSH,
756 "name", name,
757 /* FIXME: MIME type!! */
758 "mime-type", "application/x-photoshop-abr",
759 NULL);
760
761 g_free (name);
762
763 brush->priv->spacing = abr_sampled_brush_hdr.spacing;
764 brush->priv->x_axis.x = width / 2.0;
765 brush->priv->x_axis.y = 0.0;
766 brush->priv->y_axis.x = 0.0;
767 brush->priv->y_axis.y = height / 2.0;
768 brush->priv->mask = gimp_temp_buf_new (width, height,
769 babl_format ("Y u8"));
770
771 mask = gimp_temp_buf_get_data (brush->priv->mask);
772 size = width * height * bytes;
773
774 compress = abr_read_char (input, error);
775 if (error && *error)
776 {
777 g_object_unref (brush);
778 brush = NULL;
779 break;
780 }
781
782 /* g_print(" | << size: %dx%d %d bit (%d bytes) %s\n",
783 * width, height, abr_sampled_brush_hdr.depth, size,
784 * comppres ? "compressed" : "raw");
785 */
786
787 if (! compress)
788 {
789 gsize bytes_read;
790
791 if (! g_input_stream_read_all (G_INPUT_STREAM (input),
792 mask, size,
793 &bytes_read, NULL, error) ||
794 bytes_read != size)
795 {
796 g_object_unref (brush);
797 brush = NULL;
798 break;
799 }
800 }
801 else
802 {
803 if (! abr_rle_decode (input, (gchar *) mask, size, height, error))
804 {
805 g_object_unref (brush);
806 brush = NULL;
807 break;
808 }
809 }
810 }
811 break;
812
813 default:
814 g_printerr ("WARNING: unknown brush type, skipping.\n");
815 g_seekable_seek (G_SEEKABLE (input), abr_brush_hdr.size,
816 G_SEEK_CUR, NULL, NULL);
817 break;
818 }
819
820 return brush;
821 }
822
823 static GimpBrush *
gimp_brush_load_abr_brush_v6(GDataInputStream * input,AbrHeader * abr_hdr,gint32 max_offset,gint index,GFile * file,GError ** error)824 gimp_brush_load_abr_brush_v6 (GDataInputStream *input,
825 AbrHeader *abr_hdr,
826 gint32 max_offset,
827 gint index,
828 GFile *file,
829 GError **error)
830 {
831 GimpBrush *brush = NULL;
832 guchar *mask;
833
834 gint32 brush_size;
835 gint32 brush_end;
836 goffset next_brush;
837
838 gint32 top, left, bottom, right;
839 gint16 depth;
840 gchar compress;
841
842 gint32 width, height;
843 gint32 size;
844
845 gchar *tmp;
846 gchar *name;
847 gboolean r;
848
849 brush_size = abr_read_long (input, error);
850 if (error && *error)
851 return NULL;
852
853 if (brush_size < 0)
854 {
855 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
856 _("Fatal parse error in brush file: "
857 "Brush size value corrupt."));
858 return NULL;
859 }
860
861 brush_end = brush_size;
862
863 /* complement to 4 */
864 while (brush_end % 4 != 0)
865 brush_end++;
866
867 next_brush = (brush_end + g_seekable_tell (G_SEEKABLE (input)));
868
869 if (abr_hdr->count == 1)
870 {
871 /* discard key and short coordinates and unknown short */
872 r = g_seekable_seek (G_SEEKABLE (input), 47, G_SEEK_CUR,
873 NULL, error);
874 }
875 else
876 {
877 /* discard key and unknown bytes */
878 r = g_seekable_seek (G_SEEKABLE (input), 301, G_SEEK_CUR,
879 NULL, error);
880 }
881
882 if (! r)
883 {
884 g_prefix_error (error,
885 _("Fatal parse error in brush file: "
886 "File appears truncated: "));
887 return NULL;
888 }
889
890 top = abr_read_long (input, error); if (error && *error) return NULL;
891 left = abr_read_long (input, error); if (error && *error) return NULL;
892 bottom = abr_read_long (input, error); if (error && *error) return NULL;
893 right = abr_read_long (input, error); if (error && *error) return NULL;
894 depth = abr_read_short (input, error); if (error && *error) return NULL;
895 compress = abr_read_char (input, error); if (error && *error) return NULL;
896
897 depth = depth >> 3;
898
899 width = right - left;
900 height = bottom - top;
901 size = width * depth * height;
902
903 #if 0
904 g_printerr ("width %i height %i depth %i compress %i\n",
905 width, height, depth, compress);
906 #endif
907
908 if (width < 1 || width > 10000 ||
909 height < 1 || height > 10000 ||
910 depth < 1 || depth > 1 ||
911 G_MAXSIZE / width / height / depth < 1)
912 {
913 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
914 _("Fatal parse error in brush file: "
915 "Brush dimensions out of range."));
916 return NULL;
917 }
918
919 if (compress < 0 || compress > 1)
920 {
921 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
922 _("Fatal parse error in brush file: "
923 "Unknown compression method."));
924 return NULL;
925 }
926
927 tmp = g_path_get_basename (gimp_file_get_utf8_name (file));
928 name = g_strdup_printf ("%s-%03d", tmp, index);
929 g_free (tmp);
930
931 brush = g_object_new (GIMP_TYPE_BRUSH,
932 "name", name,
933 /* FIXME: MIME type!! */
934 "mime-type", "application/x-photoshop-abr",
935 NULL);
936
937 g_free (name);
938
939 brush->priv->spacing = 25; /* real value needs 8BIMdesc section parser */
940 brush->priv->x_axis.x = width / 2.0;
941 brush->priv->x_axis.y = 0.0;
942 brush->priv->y_axis.x = 0.0;
943 brush->priv->y_axis.y = height / 2.0;
944 brush->priv->mask = gimp_temp_buf_new (width, height,
945 babl_format ("Y u8"));
946
947 mask = gimp_temp_buf_get_data (brush->priv->mask);
948
949 /* data decoding */
950 if (! compress)
951 {
952 /* not compressed - read raw bytes as brush data */
953 gsize bytes_read;
954
955 if (! g_input_stream_read_all (G_INPUT_STREAM (input),
956 mask, size,
957 &bytes_read, NULL, error) ||
958 bytes_read != size)
959 {
960 g_object_unref (brush);
961 return NULL;
962 }
963 }
964 else
965 {
966 if (! abr_rle_decode (input, (gchar *) mask, size, height, error))
967 {
968 g_object_unref (brush);
969 return NULL;
970 }
971 }
972
973 if (g_seekable_tell (G_SEEKABLE (input)) <= next_brush)
974 g_seekable_seek (G_SEEKABLE (input), next_brush, G_SEEK_SET,
975 NULL, NULL);
976
977 return brush;
978 }
979
980 static gchar
abr_read_char(GDataInputStream * input,GError ** error)981 abr_read_char (GDataInputStream *input,
982 GError **error)
983 {
984 return g_data_input_stream_read_byte (input, NULL, error);
985 }
986
987 static gint16
abr_read_short(GDataInputStream * input,GError ** error)988 abr_read_short (GDataInputStream *input,
989 GError **error)
990 {
991 return g_data_input_stream_read_int16 (input, NULL, error);
992 }
993
994 static gint32
abr_read_long(GDataInputStream * input,GError ** error)995 abr_read_long (GDataInputStream *input,
996 GError **error)
997 {
998 return g_data_input_stream_read_int32 (input, NULL, error);
999 }
1000
1001 static gchar *
abr_read_ucs2_text(GDataInputStream * input,GError ** error)1002 abr_read_ucs2_text (GDataInputStream *input,
1003 GError **error)
1004 {
1005 gchar *name_ucs2;
1006 gchar *name_utf8;
1007 gint len;
1008 gint i;
1009
1010 /* two-bytes characters encoded (UCS-2)
1011 * format:
1012 * long : number of characters in string
1013 * data : zero terminated UCS-2 string
1014 */
1015
1016 len = 2 * abr_read_long (input, error);
1017 if (len <= 0)
1018 return NULL;
1019
1020 name_ucs2 = g_new (gchar, len);
1021
1022 for (i = 0; i < len; i++)
1023 {
1024 name_ucs2[i] = abr_read_char (input, error);
1025 if (error && *error)
1026 {
1027 g_free (name_ucs2);
1028 return NULL;
1029 }
1030 }
1031
1032 name_utf8 = g_convert (name_ucs2, len,
1033 "UTF-8", "UCS-2BE",
1034 NULL, NULL, NULL);
1035
1036 g_free (name_ucs2);
1037
1038 return name_utf8;
1039 }
1040
1041 static gboolean
abr_supported(AbrHeader * abr_hdr,GError ** error)1042 abr_supported (AbrHeader *abr_hdr,
1043 GError **error)
1044 {
1045 switch (abr_hdr->version)
1046 {
1047 case 1:
1048 case 2:
1049 return TRUE;
1050 break;
1051
1052 case 10:
1053 case 6:
1054 /* in this case, count contains format sub-version */
1055 if (abr_hdr->count == 1 || abr_hdr->count == 2)
1056 return TRUE;
1057
1058 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
1059 _("Fatal parse error in brush file: "
1060 "Unable to decode abr format version %d."),
1061
1062 /* horrid subversion display, but better than
1063 * having yet another translatable string for
1064 * this
1065 */
1066 abr_hdr->version * 10 + abr_hdr->count);
1067 break;
1068 }
1069
1070 return FALSE;
1071 }
1072
1073 static gboolean
abr_reach_8bim_section(GDataInputStream * input,const gchar * name,GError ** error)1074 abr_reach_8bim_section (GDataInputStream *input,
1075 const gchar *name,
1076 GError **error)
1077 {
1078 while (TRUE)
1079 {
1080 gchar tag[4];
1081 gchar tagname[5];
1082 guint32 section_size;
1083 gsize bytes_read;
1084
1085 if (! g_input_stream_read_all (G_INPUT_STREAM (input),
1086 tag, 4,
1087 &bytes_read, NULL, error) ||
1088 bytes_read != 4)
1089 return FALSE;
1090
1091 if (strncmp (tag, "8BIM", 4))
1092 return FALSE;
1093
1094 if (! g_input_stream_read_all (G_INPUT_STREAM (input),
1095 tagname, 4,
1096 &bytes_read, NULL, error) ||
1097 bytes_read != 4)
1098 return FALSE;
1099
1100 tagname[4] = '\0';
1101
1102 if (! strncmp (tagname, name, 4))
1103 return TRUE;
1104
1105 section_size = abr_read_long (input, error);
1106 if (error && *error)
1107 return FALSE;
1108
1109 if (! g_seekable_seek (G_SEEKABLE (input), section_size, G_SEEK_CUR,
1110 NULL, error))
1111 return FALSE;
1112 }
1113
1114 return FALSE;
1115 }
1116
1117 static gboolean
abr_rle_decode(GDataInputStream * input,gchar * buffer,gsize buffer_size,gint32 height,GError ** error)1118 abr_rle_decode (GDataInputStream *input,
1119 gchar *buffer,
1120 gsize buffer_size,
1121 gint32 height,
1122 GError **error)
1123 {
1124 gint i, j;
1125 gshort *cscanline_len = NULL;
1126 gchar *cdata = NULL;
1127 gchar *data = buffer;
1128
1129 /* read compressed size foreach scanline */
1130 cscanline_len = gegl_scratch_new (gshort, height);
1131 for (i = 0; i < height; i++)
1132 {
1133 cscanline_len[i] = abr_read_short (input, error);
1134 if ((error && *error) || cscanline_len[i] <= 0)
1135 goto err;
1136 }
1137
1138 /* unpack each scanline data */
1139 for (i = 0; i < height; i++)
1140 {
1141 gint len;
1142 gsize bytes_read;
1143
1144 len = cscanline_len[i];
1145
1146 cdata = gegl_scratch_alloc (len);
1147
1148 if (! g_input_stream_read_all (G_INPUT_STREAM (input),
1149 cdata, len,
1150 &bytes_read, NULL, error) ||
1151 bytes_read != len)
1152 {
1153 goto err;
1154 }
1155
1156 for (j = 0; j < len;)
1157 {
1158 gint32 n = cdata[j++];
1159
1160 if (n >= 128) /* force sign */
1161 n -= 256;
1162
1163 if (n < 0)
1164 {
1165 /* copy the following char -n + 1 times */
1166
1167 if (n == -128) /* it's a nop */
1168 continue;
1169
1170 n = -n + 1;
1171
1172 if (j + 1 > len || (data - buffer) + n > buffer_size)
1173 goto err;
1174
1175 memset (data, cdata[j], n);
1176
1177 j += 1;
1178 data += n;
1179 }
1180 else
1181 {
1182 /* read the following n + 1 chars (no compr) */
1183
1184 n = n + 1;
1185
1186 if (j + n > len || (data - buffer) + n > buffer_size)
1187 goto err;
1188
1189 memcpy (data, &cdata[j], n);
1190
1191 j += n;
1192 data += n;
1193 }
1194 }
1195
1196 g_clear_pointer (&cdata, gegl_scratch_free);
1197 }
1198
1199 g_clear_pointer (&cscanline_len, gegl_scratch_free);
1200
1201 return TRUE;
1202
1203 err:
1204 g_clear_pointer (&cdata, gegl_scratch_free);
1205 g_clear_pointer (&cscanline_len, gegl_scratch_free);
1206 if (error && ! *error)
1207 {
1208 g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
1209 _("Fatal parse error in brush file: "
1210 "RLE compressed brush data corrupt."));
1211 }
1212 return FALSE;
1213 }
1214