Skip to content

Latest commit

 

History

History
951 lines (816 loc) · 24 KB

IMG_xcf.c

File metadata and controls

951 lines (816 loc) · 24 KB
 
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
8
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
12
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
*/
/* This is a XCF image file loading framework */
Feb 21, 2003
Feb 21, 2003
24
#include "SDL_endian.h"
25
26
27
28
#include "SDL_image.h"
#ifdef LOAD_XCF
Feb 21, 2003
Feb 21, 2003
29
#if DEBUG
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
static char prop_names [][30] = {
"end",
"colormap",
"active_layer",
"active_channel",
"selection",
"floating_selection",
"opacity",
"mode",
"visible",
"linked",
"preserve_transparency",
"apply_mask",
"edit_mask",
"show_mask",
"show_masked",
"offsets",
"color",
"compression",
"guides",
"resolution",
"tattoo",
"parasites",
"unit",
"paths",
"user_unit"
};
Feb 21, 2003
Feb 21, 2003
57
58
#endif
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
typedef enum
{
PROP_END = 0,
PROP_COLORMAP = 1,
PROP_ACTIVE_LAYER = 2,
PROP_ACTIVE_CHANNEL = 3,
PROP_SELECTION = 4,
PROP_FLOATING_SELECTION = 5,
PROP_OPACITY = 6,
PROP_MODE = 7,
PROP_VISIBLE = 8,
PROP_LINKED = 9,
PROP_PRESERVE_TRANSPARENCY = 10,
PROP_APPLY_MASK = 11,
PROP_EDIT_MASK = 12,
PROP_SHOW_MASK = 13,
PROP_SHOW_MASKED = 14,
PROP_OFFSETS = 15,
PROP_COLOR = 16,
PROP_COMPRESSION = 17,
PROP_GUIDES = 18,
PROP_RESOLUTION = 19,
PROP_TATTOO = 20,
PROP_PARASITES = 21,
PROP_UNIT = 22,
PROP_PATHS = 23,
PROP_USER_UNIT = 24
} xcf_prop_type;
typedef enum {
COMPR_NONE = 0,
COMPR_RLE = 1,
COMPR_ZLIB = 2,
COMPR_FRACTAL = 3
} xcf_compr_type;
typedef enum {
IMAGE_RGB = 0,
IMAGE_GREYSCALE = 1,
IMAGE_INDEXED = 2
} xcf_image_type;
typedef struct {
Uint32 id;
Uint32 length;
union {
struct {
Uint32 num;
char * cmap;
} colormap; // 1
struct {
Uint32 drawable_offset;
} floating_selection; // 5
Sint32 opacity;
Sint32 mode;
int visible;
int linked;
int preserve_transparency;
int apply_mask;
int show_mask;
struct {
Sint32 x;
Sint32 y;
} offset;
unsigned char color [3];
Uint8 compression;
struct {
Sint32 x;
Sint32 y;
} resolution;
struct {
char * name;
Uint32 flags;
Uint32 size;
char * data;
} parasite;
} data;
} xcf_prop;
typedef struct {
char sign [14];
Dec 6, 2018
Dec 6, 2018
141
Uint32 file_version;
142
143
144
Uint32 width;
Uint32 height;
Sint32 image_type;
Dec 6, 2018
Dec 6, 2018
145
Uint32 precision;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
xcf_prop * properties;
Uint32 * layer_file_offsets;
Uint32 * channel_file_offsets;
xcf_compr_type compr;
Uint32 cm_num;
unsigned char * cm_map;
} xcf_header;
typedef struct {
Uint32 width;
Uint32 height;
Sint32 layer_type;
char * name;
xcf_prop * properties;
Uint32 hierarchy_file_offset;
Uint32 layer_mask_offset;
Uint32 offset_x;
Uint32 offset_y;
May 3, 2002
May 3, 2002
168
int visible;
169
170
171
172
173
174
175
176
177
178
179
180
} xcf_layer;
typedef struct {
Uint32 width;
Uint32 height;
char * name;
xcf_prop * properties;
Uint32 hierarchy_file_offset;
Uint32 color;
Uint32 opacity;
May 3, 2002
May 3, 2002
181
182
int selection;
int visible;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
} xcf_channel;
typedef struct {
Uint32 width;
Uint32 height;
Uint32 bpp;
Uint32 * level_file_offsets;
} xcf_hierarchy;
typedef struct {
Uint32 width;
Uint32 height;
Uint32 * tile_file_offsets;
} xcf_level;
typedef unsigned char * xcf_tile;
typedef unsigned char * (* load_tile_type) (SDL_RWops *, Uint32, int, int, int);
/* See if an image is contained in a data source */
Feb 4, 2006
Feb 4, 2006
206
207
int IMG_isXCF(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Sint64 start;
int is_XCF;
char magic[14];
if ( !src )
return 0;
start = SDL_RWtell(src);
is_XCF = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
if (SDL_strncmp(magic, "gimp xcf ", 9) == 0) {
is_XCF = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_XCF);
223
224
225
}
static char * read_string (SDL_RWops * src) {
Oct 16, 2018
Oct 16, 2018
226
Sint64 remaining;
227
228
229
Uint32 tmp;
char * data;
Sep 29, 2018
Sep 29, 2018
230
tmp = SDL_ReadBE32(src);
Oct 16, 2018
Oct 16, 2018
231
232
remaining = SDL_RWsize(src) - SDL_RWtell(src);
if (tmp > 0 && (Sint32)tmp <= remaining) {
Apr 25, 2013
Apr 25, 2013
233
data = (char *) SDL_malloc (sizeof (char) * tmp);
Sep 29, 2018
Sep 29, 2018
234
235
236
237
if (data) {
SDL_RWread(src, data, tmp, 1);
data[tmp - 1] = '\0';
}
Oct 16, 2018
Oct 16, 2018
238
239
}
else {
240
241
242
243
244
data = NULL;
}
return data;
}
Dec 6, 2018
Dec 6, 2018
245
246
247
248
249
250
251
static Uint64 read_offset (SDL_RWops * src, const xcf_header * h) {
Uint64 offset; // starting with version 11, offsets are 64 bits
offset = (h->file_version >= 11) ? (Uint64)SDL_ReadBE32 (src) << 32 : 0;
offset |= SDL_ReadBE32 (src);
return offset;
}
252
253
254
255
256
257
258
259
260
static Uint32 Swap32 (Uint32 v) {
return
((v & 0x000000FF) << 16)
| ((v & 0x0000FF00))
| ((v & 0x00FF0000) >> 16)
| ((v & 0xFF000000));
}
Nov 30, 2018
Nov 30, 2018
261
static int xcf_read_property (SDL_RWops * src, xcf_prop * prop) {
Oct 6, 2017
Oct 6, 2017
262
Uint32 len;
263
264
265
prop->id = SDL_ReadBE32 (src);
prop->length = SDL_ReadBE32 (src);
Feb 21, 2003
Feb 21, 2003
266
#if DEBUG
Nov 30, 2018
Nov 30, 2018
267
printf ("%.8X: %s(%u): %u\n", SDL_RWtell (src), prop->id < 25 ? prop_names [prop->id] : "unknown", prop->id, prop->length);
May 3, 2002
May 3, 2002
268
#endif
269
270
271
272
switch (prop->id) {
case PROP_COLORMAP:
prop->data.colormap.num = SDL_ReadBE32 (src);
Apr 25, 2013
Apr 25, 2013
273
prop->data.colormap.cmap = (char *) SDL_malloc (sizeof (char) * prop->data.colormap.num * 3);
274
275
276
277
278
279
280
281
282
283
284
285
SDL_RWread (src, prop->data.colormap.cmap, prop->data.colormap.num*3, 1);
break;
case PROP_OFFSETS:
prop->data.offset.x = SDL_ReadBE32 (src);
prop->data.offset.y = SDL_ReadBE32 (src);
break;
case PROP_OPACITY:
prop->data.opacity = SDL_ReadBE32 (src);
break;
case PROP_COMPRESSION:
case PROP_COLOR:
Oct 6, 2017
Oct 6, 2017
286
287
288
289
290
291
if (prop->length > sizeof(prop->data)) {
len = sizeof(prop->data);
} else {
len = prop->length;
}
SDL_RWread(src, &prop->data, len, 1);
292
break;
May 3, 2002
May 3, 2002
293
294
295
case PROP_VISIBLE:
prop->data.visible = SDL_ReadBE32 (src);
break;
296
297
default:
// SDL_RWread (src, &prop->data, prop->length, 1);
Nov 30, 2018
Nov 30, 2018
298
299
if (SDL_RWseek (src, prop->length, RW_SEEK_CUR) < 0)
return 0; // ERROR
300
}
Nov 30, 2018
Nov 30, 2018
301
return 1; // OK
302
303
}
Sep 26, 2009
Sep 26, 2009
304
static void free_xcf_header (xcf_header * h) {
305
if (h->cm_num)
Apr 25, 2013
Apr 25, 2013
306
SDL_free (h->cm_map);
Jul 27, 2013
Jul 27, 2013
307
if (h->layer_file_offsets)
Aug 7, 2013
Aug 7, 2013
308
SDL_free (h->layer_file_offsets);
Apr 25, 2013
Apr 25, 2013
309
SDL_free (h);
310
311
}
Sep 26, 2009
Sep 26, 2009
312
static xcf_header * read_xcf_header (SDL_RWops * src) {
313
314
315
xcf_header * h;
xcf_prop prop;
Apr 25, 2013
Apr 25, 2013
316
h = (xcf_header *) SDL_malloc (sizeof (xcf_header));
Sep 12, 2017
Sep 12, 2017
317
318
319
if (!h) {
return NULL;
}
320
321
322
323
SDL_RWread (src, h->sign, 14, 1);
h->width = SDL_ReadBE32 (src);
h->height = SDL_ReadBE32 (src);
h->image_type = SDL_ReadBE32 (src);
Dec 6, 2018
Dec 6, 2018
324
h->file_version = (h->sign[10] - '0') * 100 + (h->sign[11] - '0') * 10 + (h->sign[12] - '0');
Nov 30, 2018
Nov 30, 2018
325
#ifdef DEBUG
Dec 6, 2018
Dec 6, 2018
326
printf ("XCF signature : %.14s (version %u)\n", h->sign, h->file_version);
Nov 30, 2018
Nov 30, 2018
327
328
printf (" (%u,%u) type=%u\n", h->width, h->height, h->image_type);
#endif
Dec 6, 2018
Dec 6, 2018
329
330
331
332
if (h->file_version >= 4)
h->precision = SDL_ReadBE32 (src);
else
h->precision = 150;
333
334
h->properties = NULL;
Jul 27, 2013
Jul 27, 2013
335
h->layer_file_offsets = NULL;
336
337
338
339
340
341
h->compr = COMPR_NONE;
h->cm_num = 0;
h->cm_map = NULL;
// Just read, don't save
do {
Nov 30, 2018
Nov 30, 2018
342
343
344
345
if (!xcf_read_property (src, &prop)) {
free_xcf_header (h);
return NULL;
}
346
if (prop.id == PROP_COMPRESSION)
Feb 3, 2013
Feb 3, 2013
347
h->compr = (xcf_compr_type)prop.data.compression;
348
else if (prop.id == PROP_COLORMAP) {
Feb 21, 2003
Feb 21, 2003
349
// unused var: int i;
Sep 12, 2017
Sep 12, 2017
350
351
352
353
354
355
356
357
358
359
Uint32 cm_num;
unsigned char *cm_map;
cm_num = prop.data.colormap.num;
cm_map = (unsigned char *) SDL_realloc(h->cm_map, sizeof (unsigned char) * 3 * cm_num);
if (cm_map) {
h->cm_num = cm_num;
h->cm_map = cm_map;
SDL_memcpy (h->cm_map, prop.data.colormap.cmap, 3*sizeof (char)*h->cm_num);
}
Apr 25, 2013
Apr 25, 2013
360
SDL_free (prop.data.colormap.cmap);
Sep 12, 2017
Sep 12, 2017
361
362
363
364
365
if (!cm_map) {
free_xcf_header(h);
return NULL;
}
366
367
368
369
370
371
}
} while (prop.id != PROP_END);
return h;
}
Sep 26, 2009
Sep 26, 2009
372
static void free_xcf_layer (xcf_layer * l) {
Apr 25, 2013
Apr 25, 2013
373
374
SDL_free (l->name);
SDL_free (l);
375
376
}
Dec 6, 2018
Dec 6, 2018
377
static xcf_layer * read_xcf_layer (SDL_RWops * src, const xcf_header * h) {
378
379
380
xcf_layer * l;
xcf_prop prop;
Apr 25, 2013
Apr 25, 2013
381
l = (xcf_layer *) SDL_malloc (sizeof (xcf_layer));
382
383
384
385
386
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->layer_type = SDL_ReadBE32 (src);
l->name = read_string (src);
Dec 6, 2018
Dec 6, 2018
387
388
389
#ifdef DEBUG
printf ("layer (%d,%d) type=%d '%s'\n", l->width, l->height, l->layer_type, l->name);
#endif
390
391
do {
Nov 30, 2018
Nov 30, 2018
392
393
394
395
if (!xcf_read_property (src, &prop)) {
free_xcf_layer (l);
return NULL;
}
396
397
398
if (prop.id == PROP_OFFSETS) {
l->offset_x = prop.data.offset.x;
l->offset_y = prop.data.offset.y;
May 3, 2002
May 3, 2002
399
400
} else if (prop.id == PROP_VISIBLE) {
l->visible = prop.data.visible ? 1 : 0;
May 19, 2019
May 19, 2019
401
402
} else if (prop.id == PROP_COLORMAP) {
SDL_free (prop.data.colormap.cmap);
403
404
405
}
} while (prop.id != PROP_END);
Dec 6, 2018
Dec 6, 2018
406
407
l->hierarchy_file_offset = read_offset (src, h);
l->layer_mask_offset = read_offset (src, h);
408
409
410
411
return l;
}
Sep 26, 2009
Sep 26, 2009
412
static void free_xcf_channel (xcf_channel * c) {
Apr 25, 2013
Apr 25, 2013
413
414
SDL_free (c->name);
SDL_free (c);
415
416
}
Dec 6, 2018
Dec 6, 2018
417
static xcf_channel * read_xcf_channel (SDL_RWops * src, const xcf_header * h) {
418
419
420
xcf_channel * l;
xcf_prop prop;
Feb 3, 2013
Feb 3, 2013
421
l = (xcf_channel *) SDL_malloc (sizeof (xcf_channel));
422
423
424
425
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->name = read_string (src);
Dec 6, 2018
Dec 6, 2018
426
427
428
#ifdef DEBUG
printf ("channel (%u,%u) '%s'\n", l->width, l->height, l->name);
#endif
429
430
431
l->selection = 0;
do {
Nov 30, 2018
Nov 30, 2018
432
433
434
435
if (!xcf_read_property (src, &prop)) {
free_xcf_channel (l);
return NULL;
}
436
437
438
439
440
441
switch (prop.id) {
case PROP_OPACITY:
l->opacity = prop.data.opacity << 24;
break;
case PROP_COLOR:
l->color = ((Uint32) prop.data.color[0] << 16)
May 22, 2013
May 22, 2013
442
443
| ((Uint32) prop.data.color[1] << 8)
| ((Uint32) prop.data.color[2]);
444
445
446
447
break;
case PROP_SELECTION:
l->selection = 1;
break;
May 3, 2002
May 3, 2002
448
449
450
case PROP_VISIBLE:
l->visible = prop.data.visible ? 1 : 0;
break;
451
default:
Feb 21, 2003
Feb 21, 2003
452
;
453
454
455
}
} while (prop.id != PROP_END);
Dec 6, 2018
Dec 6, 2018
456
l->hierarchy_file_offset = read_offset (src, h);
457
458
459
460
return l;
}
Sep 26, 2009
Sep 26, 2009
461
static void free_xcf_hierarchy (xcf_hierarchy * h) {
Apr 25, 2013
Apr 25, 2013
462
463
SDL_free (h->level_file_offsets);
SDL_free (h);
464
465
}
Dec 6, 2018
Dec 6, 2018
466
static xcf_hierarchy * read_xcf_hierarchy (SDL_RWops * src, const xcf_header * head) {
467
468
469
xcf_hierarchy * h;
int i;
Apr 25, 2013
Apr 25, 2013
470
h = (xcf_hierarchy *) SDL_malloc (sizeof (xcf_hierarchy));
471
472
473
474
475
476
477
h->width = SDL_ReadBE32 (src);
h->height = SDL_ReadBE32 (src);
h->bpp = SDL_ReadBE32 (src);
h->level_file_offsets = NULL;
i = 0;
do {
Jul 27, 2013
Jul 27, 2013
478
h->level_file_offsets = (Uint32 *) SDL_realloc (h->level_file_offsets, sizeof (Uint32) * (i+1));
Dec 6, 2018
Dec 6, 2018
479
h->level_file_offsets [i] = read_offset (src, head);
480
481
482
483
484
} while (h->level_file_offsets [i++]);
return h;
}
Sep 26, 2009
Sep 26, 2009
485
static void free_xcf_level (xcf_level * l) {
Apr 25, 2013
Apr 25, 2013
486
487
SDL_free (l->tile_file_offsets);
SDL_free (l);
488
489
}
Dec 6, 2018
Dec 6, 2018
490
static xcf_level * read_xcf_level (SDL_RWops * src, const xcf_header * h) {
491
492
493
xcf_level * l;
int i;
Feb 3, 2013
Feb 3, 2013
494
l = (xcf_level *) SDL_malloc (sizeof (xcf_level));
495
496
497
498
499
500
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->tile_file_offsets = NULL;
i = 0;
do {
Feb 3, 2013
Feb 3, 2013
501
l->tile_file_offsets = (Uint32 *) SDL_realloc (l->tile_file_offsets, sizeof (Uint32) * (i+1));
Dec 6, 2018
Dec 6, 2018
502
l->tile_file_offsets [i] = read_offset (src, h);
503
504
505
506
507
} while (l->tile_file_offsets [i++]);
return l;
}
Sep 26, 2009
Sep 26, 2009
508
static void free_xcf_tile (unsigned char * t) {
Apr 25, 2013
Apr 25, 2013
509
SDL_free (t);
510
511
}
Sep 26, 2009
Sep 26, 2009
512
static unsigned char * load_xcf_tile_none (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
513
514
unsigned char * load;
Feb 3, 2013
Feb 3, 2013
515
load = (unsigned char *) SDL_malloc (len); // expect this is okay
Dec 6, 2018
Dec 6, 2018
516
517
if (load != NULL)
SDL_RWread (src, load, len, 1);
518
519
520
521
return load;
}
Sep 26, 2009
Sep 26, 2009
522
static unsigned char * load_xcf_tile_rle (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
523
524
525
526
527
unsigned char * load, * t, * data, * d;
Uint32 reallen;
int i, size, count, j, length;
unsigned char val;
Feb 7, 2018
Feb 7, 2018
528
529
530
531
if (len == 0) { /* probably bogus data. */
return NULL;
}
Feb 3, 2013
Feb 3, 2013
532
t = load = (unsigned char *) SDL_malloc (len);
Dec 6, 2018
Dec 6, 2018
533
534
if (load == NULL)
return NULL;
535
536
reallen = SDL_RWread (src, t, 1, len);
Jan 27, 2018
Jan 27, 2018
537
data = (unsigned char *) SDL_calloc (1, x*y*bpp);
538
539
540
541
for (i = 0; i < bpp; i++) {
d = data + i;
size = x*y;
count = 0;
May 22, 2013
May 22, 2013
542
543
544
545
546
547
while (size > 0) {
val = *t++;
length = val;
if (length >= 128) {
Jan 27, 2018
Jan 27, 2018
548
549
550
551
552
length = 255 - (length - 1);
if (length == 128) {
length = (*t << 8) + t[1];
t += 2;
}
May 22, 2013
May 22, 2013
553
Jan 27, 2018
Jan 27, 2018
554
555
556
557
558
559
if (((size_t) (t - load) + length) >= len) {
break; /* bogus data */
} else if (length > size) {
break; /* bogus data */
}
Jan 27, 2018
Jan 27, 2018
560
561
count += length;
size -= length;
May 22, 2013
May 22, 2013
562
Jan 27, 2018
Jan 27, 2018
563
564
565
566
567
568
569
570
571
572
while (length-- > 0) {
*d = *t++;
d += bpp;
}
} else {
length += 1;
if (length == 128) {
length = (*t << 8) + t[1];
t += 2;
}
573
Jan 27, 2018
Jan 27, 2018
574
575
576
577
578
579
if (((size_t) (t - load)) >= len) {
break; /* bogus data */
} else if (length > size) {
break; /* bogus data */
}
Jan 27, 2018
Jan 27, 2018
580
581
count += length;
size -= length;
582
Jan 27, 2018
Jan 27, 2018
583
val = *t++;
584
Jan 27, 2018
Jan 27, 2018
585
586
587
588
for (j = 0; j < length; j++) {
*d = val;
d += bpp;
}
589
590
}
}
Jan 27, 2018
Jan 27, 2018
591
592
593
594
595
if (size > 0) {
break; /* just drop out, untouched data initialized to zero. */
}
596
597
}
Feb 3, 2013
Feb 3, 2013
598
SDL_free (load);
599
600
601
602
603
return (data);
}
static Uint32 rgb2grey (Uint32 a) {
Uint8 l;
Apr 17, 2013
Apr 17, 2013
604
605
606
l = (Uint8)(0.2990 * ((a & 0x00FF0000) >> 16)
+ 0.5870 * ((a & 0x0000FF00) >> 8)
+ 0.1140 * ((a & 0x000000FF)));
607
608
609
610
return (l << 16) | (l << 8) | l;
}
Sep 26, 2009
Sep 26, 2009
611
static void create_channel_surface (SDL_Surface * surf, xcf_image_type itype, Uint32 color, Uint32 opacity) {
Feb 21, 2003
Feb 21, 2003
612
Uint32 c = 0;
613
614
615
616
617
618
619
620
621
622
623
624
625
switch (itype) {
case IMAGE_RGB:
case IMAGE_INDEXED:
c = opacity | color;
break;
case IMAGE_GREYSCALE:
c = opacity | rgb2grey (color);
break;
}
SDL_FillRect (surf, NULL, c);
}
Sep 12, 2017
Sep 12, 2017
626
627
628
629
630
631
632
633
634
635
636
637
static int
do_layer_surface(SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile)
{
xcf_hierarchy *hierarchy;
xcf_level *level;
unsigned char *tile;
Uint8 *p8;
Uint16 *p16;
Uint32 *p;
int i, j;
Uint32 x, y, tx, ty, ox, oy;
Uint32 *row;
Dec 6, 2018
Dec 6, 2018
638
Uint32 length;
Sep 12, 2017
Sep 12, 2017
639
640
SDL_RWseek(src, layer->hierarchy_file_offset, RW_SEEK_SET);
Dec 6, 2018
Dec 6, 2018
641
hierarchy = read_xcf_hierarchy(src, head);
Sep 12, 2017
Sep 12, 2017
642
Feb 7, 2018
Feb 7, 2018
643
644
645
646
647
648
649
650
651
652
653
654
if (hierarchy->bpp > 4) { /* unsupported. */
SDL_Log("Unknown Gimp image bpp (%u)\n", (unsigned int) hierarchy->bpp);
free_xcf_hierarchy(hierarchy);
return 1;
}
if ((hierarchy->width > 20000) || (hierarchy->height > 20000)) { /* arbitrary limit to avoid integer overflow. */
SDL_Log("Gimp image too large (%ux%u)\n", (unsigned int) hierarchy->width, (unsigned int) hierarchy->height);
free_xcf_hierarchy(hierarchy);
return 1;
}
Sep 12, 2017
Sep 12, 2017
655
656
level = NULL;
for (i = 0; hierarchy->level_file_offsets[i]; i++) {
Dec 6, 2018
Dec 6, 2018
657
658
659
660
if (SDL_RWseek(src, hierarchy->level_file_offsets[i], RW_SEEK_SET) < 0)
break;
if (i > 0) // skip level except the 1st one, just like GIMP does
continue;
Dec 6, 2018
Dec 6, 2018
661
level = read_xcf_level(src, head);
Sep 12, 2017
Sep 12, 2017
662
663
664
665
666
667
ty = tx = 0;
for (j = 0; level->tile_file_offsets[j]; j++) {
SDL_RWseek(src, level->tile_file_offsets[j], RW_SEEK_SET);
ox = tx + 64 > level->width ? level->width % 64 : 64;
oy = ty + 64 > level->height ? level->height % 64 : 64;
Dec 6, 2018
Dec 6, 2018
668
length = ox*oy*6;
Sep 12, 2017
Sep 12, 2017
669
Dec 6, 2018
Dec 6, 2018
670
671
if (level->tile_file_offsets[j + 1] > level->tile_file_offsets[j]) {
length = level->tile_file_offsets[j + 1] - level->tile_file_offsets[j];
Sep 12, 2017
Sep 12, 2017
672
}
Dec 6, 2018
Dec 6, 2018
673
tile = load_tile(src, length, hierarchy->bpp, ox, oy);
Sep 12, 2017
Sep 12, 2017
674
Feb 7, 2018
Feb 7, 2018
675
676
677
678
679
680
681
682
683
684
if (!tile) {
if (hierarchy) {
free_xcf_hierarchy(hierarchy);
}
if (level) {
free_xcf_level(level);
}
return 1;
}
Sep 12, 2017
Sep 12, 2017
685
686
687
688
p8 = tile;
p16 = (Uint16 *) p8;
p = (Uint32 *) p8;
for (y = ty; y < ty + oy; y++) {
Jun 11, 2019
Jun 11, 2019
689
if ((y >= surface->h) || ((tx+ox) > surface->w)) {
Sep 26, 2018
Sep 26, 2018
690
691
break;
}
Sep 12, 2017
Sep 12, 2017
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
row = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch + tx * 4);
switch (hierarchy->bpp) {
case 4:
for (x = tx; x < tx + ox; x++)
*row++ = Swap32(*p++);
break;
case 3:
for (x = tx; x < tx + ox; x++) {
*row = 0xFF000000;
*row |= ((Uint32)*p8++ << 16);
*row |= ((Uint32)*p8++ << 8);
*row |= ((Uint32)*p8++ << 0);
row++;
}
break;
case 2:
/* Indexed / Greyscale + Alpha */
switch (head->image_type) {
case IMAGE_INDEXED:
for (x = tx; x < tx + ox; x++) {
*row = ((Uint32)(head->cm_map[*p8 * 3]) << 16);
*row |= ((Uint32)(head->cm_map[*p8 * 3 + 1]) << 8);
*row |= ((Uint32)(head->cm_map[*p8++ * 3 + 2]) << 0);
*row |= ((Uint32)*p8++ << 24);
row++;
}
break;
case IMAGE_GREYSCALE:
for (x = tx; x < tx + ox; x++) {
*row = ((Uint32)*p8 << 16);
*row |= ((Uint32)*p8 << 8);
*row |= ((Uint32)*p8++ << 0);
*row |= ((Uint32)*p8++ << 24);
row++;
}
break;
default:
Nov 5, 2017
Nov 5, 2017
729
SDL_Log("Unknown Gimp image type (%d)\n", head->image_type);
Sep 12, 2017
Sep 12, 2017
730
731
732
733
734
735
736
737
738
739
if (hierarchy) {
free_xcf_hierarchy(hierarchy);
}
if (level)
free_xcf_level(level);
return 1;
}
break;
case 1:
/* Indexed / Greyscale */
Oct 22, 2017
Oct 22, 2017
740
switch (head->image_type) {
Sep 12, 2017
Sep 12, 2017
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
case IMAGE_INDEXED:
for (x = tx; x < tx + ox; x++) {
*row++ = 0xFF000000
| ((Uint32)(head->cm_map[*p8 * 3]) << 16)
| ((Uint32)(head->cm_map[*p8 * 3 + 1]) << 8)
| ((Uint32)(head->cm_map[*p8 * 3 + 2]) << 0);
p8++;
}
break;
case IMAGE_GREYSCALE:
for (x = tx; x < tx + ox; x++) {
*row++ = 0xFF000000
| (((Uint32)(*p8)) << 16)
| (((Uint32)(*p8)) << 8)
| (((Uint32)(*p8)) << 0);
++p8;
}
break;
default:
Nov 5, 2017
Nov 5, 2017
760
SDL_Log("Unknown Gimp image type (%d)\n", head->image_type);
Sep 12, 2017
Sep 12, 2017
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
if (tile)
free_xcf_tile(tile);
if (level)
free_xcf_level(level);
if (hierarchy)
free_xcf_hierarchy(hierarchy);
return 1;
}
break;
}
}
free_xcf_tile(tile);
tx += 64;
if (tx >= level->width) {
tx = 0;
ty += 64;
}
if (ty >= level->height) {
break;
}
May 22, 2013
May 22, 2013
782
}
Sep 12, 2017
Sep 12, 2017
783
free_xcf_level(level);
May 22, 2013
May 22, 2013
784
}
785
Sep 12, 2017
Sep 12, 2017
786
free_xcf_hierarchy(hierarchy);
787
Sep 12, 2017
Sep 12, 2017
788
return 0;
789
790
}
Feb 4, 2006
Feb 4, 2006
791
792
SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
{
Feb 3, 2013
Feb 3, 2013
793
Sint64 start;
Feb 4, 2006
Feb 4, 2006
794
const char *error = NULL;
795
796
797
798
SDL_Surface *surface, *lays;
xcf_header * head;
xcf_layer * layer;
xcf_channel ** channel;
Feb 4, 2006
Feb 4, 2006
799
int chnls, i, offsets;
Feb 3, 2013
Feb 3, 2013
800
Sint64 offset, fp;
801
802
803
unsigned char * (* load_tile) (SDL_RWops *, Uint32, int, int, int);
Sep 12, 2017
Sep 12, 2017
804
if (!src) {
Jan 4, 2004
Jan 4, 2004
805
806
807
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
808
start = SDL_RWtell(src);
Jan 4, 2004
Jan 4, 2004
809
Feb 21, 2003
Feb 21, 2003
810
811
812
/* Initialize the data we will clean up when we're done */
surface = NULL;
Sep 12, 2017
Sep 12, 2017
813
814
815
816
head = read_xcf_header(src);
if (!head) {
return NULL;
}
817
818
819
820
821
822
823
824
825
switch (head->compr) {
case COMPR_NONE:
load_tile = load_xcf_tile_none;
break;
case COMPR_RLE:
load_tile = load_xcf_tile_rle;
break;
default:
Nov 5, 2017
Nov 5, 2017
826
SDL_Log("Unsupported Compression.\n");
827
828
829
830
831
free_xcf_header (head);
return NULL;
}
/* Create the surface of the appropriate type */
Jan 23, 2012
Jan 23, 2012
832
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
May 22, 2013
May 22, 2013
833
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
834
835
if ( surface == NULL ) {
Feb 4, 2006
Feb 4, 2006
836
error = "Out of memory";
837
838
839
840
841
goto done;
}
offsets = 0;
Dec 6, 2018
Dec 6, 2018
842
while ((offset = read_offset (src, head))) {
Jul 27, 2013
Jul 27, 2013
843
head->layer_file_offsets = (Uint32 *) SDL_realloc (head->layer_file_offsets, sizeof (Uint32) * (offsets+1));
Feb 3, 2013
Feb 3, 2013
844
head->layer_file_offsets [offsets] = (Uint32)offset;
845
846
847
offsets++;
}
fp = SDL_RWtell (src);
May 22, 2013
May 22, 2013
848
Jan 23, 2012
Jan 23, 2012
849
lays = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
May 22, 2013
May 22, 2013
850
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
851
852
if ( lays == NULL ) {
Feb 4, 2006
Feb 4, 2006
853
error = "Out of memory";
854
855
856
857
858
859
goto done;
}
// Blit layers backwards, because Gimp saves them highest first
for (i = offsets; i > 0; i--) {
SDL_Rect rs, rd;
Nov 8, 2009
Nov 8, 2009
860
SDL_RWseek (src, head->layer_file_offsets [i-1], RW_SEEK_SET);
861
Dec 6, 2018
Dec 6, 2018
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
layer = read_xcf_layer (src, head);
if (layer != NULL) {
do_layer_surface (lays, src, head, layer, load_tile);
rs.x = 0;
rs.y = 0;
rs.w = layer->width;
rs.h = layer->height;
rd.x = layer->offset_x;
rd.y = layer->offset_y;
rd.w = layer->width;
rd.h = layer->height;
if (layer->visible)
SDL_BlitSurface (lays, &rs, surface, &rd);
free_xcf_layer (layer);
}
878
879
880
881
}
SDL_FreeSurface (lays);
Nov 8, 2009
Nov 8, 2009
882
SDL_RWseek (src, fp, RW_SEEK_SET);
883
884
885
886
// read channels
channel = NULL;
chnls = 0;
Dec 6, 2018
Dec 6, 2018
887
while ((offset = read_offset (src, head))) {
Feb 3, 2013
Feb 3, 2013
888
channel = (xcf_channel **) SDL_realloc (channel, sizeof (xcf_channel *) * (chnls+1));
889
fp = SDL_RWtell (src);
Nov 8, 2009
Nov 8, 2009
890
SDL_RWseek (src, offset, RW_SEEK_SET);
Dec 24, 2018
Dec 24, 2018
891
channel [chnls] = (read_xcf_channel (src, head));
Dec 6, 2018
Dec 6, 2018
892
893
if (channel [chnls] != NULL)
chnls++;
May 22, 2013
May 22, 2013
894
SDL_RWseek (src, fp, RW_SEEK_SET);
895
896
897
898
899
}
if (chnls) {
SDL_Surface * chs;
Jan 23, 2012
Jan 23, 2012
900
chs = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
May 22, 2013
May 22, 2013
901
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
902
903
if (chs == NULL) {
Feb 4, 2006
Feb 4, 2006
904
error = "Out of memory";
905
906
907
908
goto done;
}
for (i = 0; i < chnls; i++) {
// printf ("CNLBLT %i\n", i);
May 3, 2002
May 3, 2002
909
if (!channel [i]->selection && channel [i]->visible) {
May 22, 2013
May 22, 2013
910
911
create_channel_surface (chs, (xcf_image_type)head->image_type, channel [i]->color, channel [i]->opacity);
SDL_BlitSurface (chs, NULL, surface, NULL);
912
913
914
}
free_xcf_channel (channel [i]);
}
Sep 26, 2018
Sep 26, 2018
915
SDL_free(channel);
916
917
918
919
SDL_FreeSurface (chs);
}
Feb 4, 2006
Feb 4, 2006
920
done:
921
free_xcf_header (head);
Feb 4, 2006
Feb 4, 2006
922
if ( error ) {
Nov 8, 2009
Nov 8, 2009
923
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
924
925
926
927
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
Jun 15, 2014
Jun 15, 2014
928
IMG_SetError("%s", error);
929
930
931
932
933
934
}
return(surface);
}
#else
Oct 9, 2019
Oct 9, 2019
935
936
937
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/* See if an image is contained in a data source */
int IMG_isXCF(SDL_RWops *src)
{
return(0);
}
/* Load a XCF type image from an SDL datasource */
SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_XCF */