Skip to content

Latest commit

 

History

History
825 lines (697 loc) · 18.1 KB

IMG_xcf.c

File metadata and controls

825 lines (697 loc) · 18.1 KB
 
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 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
24
25
26
*/
/* This is a XCF image file loading framework */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
Feb 21, 2003
Feb 21, 2003
27
#include <stdlib.h>
28
Feb 21, 2003
Feb 21, 2003
29
#include "SDL_endian.h"
30
31
32
33
#include "SDL_image.h"
#ifdef LOAD_XCF
Feb 21, 2003
Feb 21, 2003
34
#if DEBUG
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
62
63
#endif
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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];
Uint32 width;
Uint32 height;
Sint32 image_type;
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
171
int visible;
172
173
174
175
176
177
178
179
180
181
182
183
} 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
184
185
int selection;
int visible;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
} 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
209
210
int IMG_isXCF(SDL_RWops *src)
{
Feb 3, 2013
Feb 3, 2013
211
Sint64 start;
Feb 4, 2006
Feb 4, 2006
212
213
214
int is_XCF;
char magic[14];
Feb 13, 2007
Feb 13, 2007
215
216
if ( !src )
return 0;
Feb 4, 2006
Feb 4, 2006
217
218
219
start = SDL_RWtell(src);
is_XCF = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
Feb 3, 2013
Feb 3, 2013
220
if (SDL_strncmp(magic, "gimp xcf ", 9) == 0) {
Feb 4, 2006
Feb 4, 2006
221
222
223
is_XCF = 1;
}
}
Nov 8, 2009
Nov 8, 2009
224
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
225
return(is_XCF);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
}
static char * read_string (SDL_RWops * src) {
Uint32 tmp;
char * data;
tmp = SDL_ReadBE32 (src);
if (tmp > 0) {
data = (char *) malloc (sizeof (char) * tmp);
SDL_RWread (src, data, tmp, 1);
}
else {
data = NULL;
}
return data;
}
static Uint32 Swap32 (Uint32 v) {
return
((v & 0x000000FF) << 16)
| ((v & 0x0000FF00))
| ((v & 0x00FF0000) >> 16)
| ((v & 0xFF000000));
}
Sep 26, 2009
Sep 26, 2009
253
static void xcf_read_property (SDL_RWops * src, xcf_prop * prop) {
254
255
256
prop->id = SDL_ReadBE32 (src);
prop->length = SDL_ReadBE32 (src);
Feb 21, 2003
Feb 21, 2003
257
#if DEBUG
258
printf ("%.8X: %s: %d\n", SDL_RWtell (src), prop->id < 25 ? prop_names [prop->id] : "unknown", prop->length);
May 3, 2002
May 3, 2002
259
#endif
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
switch (prop->id) {
case PROP_COLORMAP:
prop->data.colormap.num = SDL_ReadBE32 (src);
prop->data.colormap.cmap = (char *) malloc (sizeof (char) * prop->data.colormap.num * 3);
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:
SDL_RWread (src, &prop->data, prop->length, 1);
break;
May 3, 2002
May 3, 2002
279
280
281
case PROP_VISIBLE:
prop->data.visible = SDL_ReadBE32 (src);
break;
282
283
default:
// SDL_RWread (src, &prop->data, prop->length, 1);
Nov 8, 2009
Nov 8, 2009
284
SDL_RWseek (src, prop->length, RW_SEEK_CUR);
285
286
287
}
}
Sep 26, 2009
Sep 26, 2009
288
static void free_xcf_header (xcf_header * h) {
289
290
291
292
293
294
if (h->cm_num)
free (h->cm_map);
free (h);
}
Sep 26, 2009
Sep 26, 2009
295
static xcf_header * read_xcf_header (SDL_RWops * src) {
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
xcf_header * h;
xcf_prop prop;
h = (xcf_header *) malloc (sizeof (xcf_header));
SDL_RWread (src, h->sign, 14, 1);
h->width = SDL_ReadBE32 (src);
h->height = SDL_ReadBE32 (src);
h->image_type = SDL_ReadBE32 (src);
h->properties = NULL;
h->compr = COMPR_NONE;
h->cm_num = 0;
h->cm_map = NULL;
// Just read, don't save
do {
xcf_read_property (src, &prop);
if (prop.id == PROP_COMPRESSION)
Feb 3, 2013
Feb 3, 2013
314
h->compr = (xcf_compr_type)prop.data.compression;
315
else if (prop.id == PROP_COLORMAP) {
Feb 21, 2003
Feb 21, 2003
316
// unused var: int i;
317
318
h->cm_num = prop.data.colormap.num;
Feb 3, 2013
Feb 3, 2013
319
h->cm_map = (unsigned char *) SDL_malloc (sizeof (unsigned char) * 3 * h->cm_num);
320
321
322
323
324
325
326
327
memcpy (h->cm_map, prop.data.colormap.cmap, 3*sizeof (char)*h->cm_num);
free (prop.data.colormap.cmap);
}
} while (prop.id != PROP_END);
return h;
}
Sep 26, 2009
Sep 26, 2009
328
static void free_xcf_layer (xcf_layer * l) {
329
330
331
332
free (l->name);
free (l);
}
Sep 26, 2009
Sep 26, 2009
333
static xcf_layer * read_xcf_layer (SDL_RWops * src) {
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
xcf_layer * l;
xcf_prop prop;
l = (xcf_layer *) malloc (sizeof (xcf_layer));
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->layer_type = SDL_ReadBE32 (src);
l->name = read_string (src);
do {
xcf_read_property (src, &prop);
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
349
350
} else if (prop.id == PROP_VISIBLE) {
l->visible = prop.data.visible ? 1 : 0;
351
352
353
354
355
356
357
358
359
}
} while (prop.id != PROP_END);
l->hierarchy_file_offset = SDL_ReadBE32 (src);
l->layer_mask_offset = SDL_ReadBE32 (src);
return l;
}
Sep 26, 2009
Sep 26, 2009
360
static void free_xcf_channel (xcf_channel * c) {
361
362
363
364
free (c->name);
free (c);
}
Sep 26, 2009
Sep 26, 2009
365
static xcf_channel * read_xcf_channel (SDL_RWops * src) {
366
367
368
xcf_channel * l;
xcf_prop prop;
Feb 3, 2013
Feb 3, 2013
369
l = (xcf_channel *) SDL_malloc (sizeof (xcf_channel));
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->name = read_string (src);
l->selection = 0;
do {
xcf_read_property (src, &prop);
switch (prop.id) {
case PROP_OPACITY:
l->opacity = prop.data.opacity << 24;
break;
case PROP_COLOR:
l->color = ((Uint32) prop.data.color[0] << 16)
| ((Uint32) prop.data.color[1] << 8)
| ((Uint32) prop.data.color[2]);
break;
case PROP_SELECTION:
l->selection = 1;
break;
May 3, 2002
May 3, 2002
390
391
392
case PROP_VISIBLE:
l->visible = prop.data.visible ? 1 : 0;
break;
393
default:
Feb 21, 2003
Feb 21, 2003
394
;
395
396
397
398
399
400
401
402
}
} while (prop.id != PROP_END);
l->hierarchy_file_offset = SDL_ReadBE32 (src);
return l;
}
Sep 26, 2009
Sep 26, 2009
403
static void free_xcf_hierarchy (xcf_hierarchy * h) {
404
405
406
407
free (h->level_file_offsets);
free (h);
}
Sep 26, 2009
Sep 26, 2009
408
static xcf_hierarchy * read_xcf_hierarchy (SDL_RWops * src) {
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
xcf_hierarchy * h;
int i;
h = (xcf_hierarchy *) malloc (sizeof (xcf_hierarchy));
h->width = SDL_ReadBE32 (src);
h->height = SDL_ReadBE32 (src);
h->bpp = SDL_ReadBE32 (src);
h->level_file_offsets = NULL;
i = 0;
do {
h->level_file_offsets = (Uint32 *) realloc (h->level_file_offsets, sizeof (Uint32) * (i+1));
h->level_file_offsets [i] = SDL_ReadBE32 (src);
} while (h->level_file_offsets [i++]);
return h;
}
Sep 26, 2009
Sep 26, 2009
427
static void free_xcf_level (xcf_level * l) {
428
429
430
431
free (l->tile_file_offsets);
free (l);
}
Sep 26, 2009
Sep 26, 2009
432
static xcf_level * read_xcf_level (SDL_RWops * src) {
433
434
435
xcf_level * l;
int i;
Feb 3, 2013
Feb 3, 2013
436
l = (xcf_level *) SDL_malloc (sizeof (xcf_level));
437
438
439
440
441
442
l->width = SDL_ReadBE32 (src);
l->height = SDL_ReadBE32 (src);
l->tile_file_offsets = NULL;
i = 0;
do {
Feb 3, 2013
Feb 3, 2013
443
l->tile_file_offsets = (Uint32 *) SDL_realloc (l->tile_file_offsets, sizeof (Uint32) * (i+1));
444
445
446
447
448
449
l->tile_file_offsets [i] = SDL_ReadBE32 (src);
} while (l->tile_file_offsets [i++]);
return l;
}
Sep 26, 2009
Sep 26, 2009
450
static void free_xcf_tile (unsigned char * t) {
451
452
453
free (t);
}
Sep 26, 2009
Sep 26, 2009
454
static unsigned char * load_xcf_tile_none (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
455
456
unsigned char * load;
Feb 3, 2013
Feb 3, 2013
457
load = (unsigned char *) SDL_malloc (len); // expect this is okay
458
459
460
461
462
SDL_RWread (src, load, len, 1);
return load;
}
Sep 26, 2009
Sep 26, 2009
463
static unsigned char * load_xcf_tile_rle (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
464
465
466
467
468
unsigned char * load, * t, * data, * d;
Uint32 reallen;
int i, size, count, j, length;
unsigned char val;
Feb 3, 2013
Feb 3, 2013
469
t = load = (unsigned char *) SDL_malloc (len);
470
471
reallen = SDL_RWread (src, t, 1, len);
Apr 30, 2006
Apr 30, 2006
472
data = (unsigned char *) malloc (x*y*bpp);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
for (i = 0; i < bpp; i++) {
d = data + i;
size = x*y;
count = 0;
while (size > 0) {
val = *t++;
length = val;
if (length >= 128) {
length = 255 - (length - 1);
if (length == 128) {
length = (*t << 8) + t[1];
t += 2;
}
count += length;
size -= length;
while (length-- > 0) {
*d = *t++;
d += bpp;
}
}
else {
length += 1;
if (length == 128) {
length = (*t << 8) + t[1];
t += 2;
}
count += length;
size -= length;
val = *t++;
for (j = 0; j < length; j++) {
*d = val;
d += bpp;
}
}
}
}
Feb 3, 2013
Feb 3, 2013
517
SDL_free (load);
518
519
520
521
522
return (data);
}
static Uint32 rgb2grey (Uint32 a) {
Uint8 l;
Apr 17, 2013
Apr 17, 2013
523
524
525
l = (Uint8)(0.2990 * ((a & 0x00FF0000) >> 16)
+ 0.5870 * ((a & 0x0000FF00) >> 8)
+ 0.1140 * ((a & 0x000000FF)));
526
527
528
529
return (l << 16) | (l << 8) | l;
}
Sep 26, 2009
Sep 26, 2009
530
static void create_channel_surface (SDL_Surface * surf, xcf_image_type itype, Uint32 color, Uint32 opacity) {
Feb 21, 2003
Feb 21, 2003
531
Uint32 c = 0;
532
533
534
535
536
537
538
539
540
541
542
543
544
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 26, 2009
Sep 26, 2009
545
static int do_layer_surface (SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile) {
546
547
548
549
550
551
xcf_hierarchy * hierarchy;
xcf_level * level;
unsigned char * tile;
Uint8 * p8;
Uint16 * p16;
Uint32 * p;
Feb 3, 2013
Feb 3, 2013
552
553
int i, j;
Uint32 x, y, tx, ty, ox, oy;
554
555
Uint32 *row;
Nov 8, 2009
Nov 8, 2009
556
SDL_RWseek (src, layer->hierarchy_file_offset, RW_SEEK_SET);
557
558
559
560
hierarchy = read_xcf_hierarchy (src);
level = NULL;
for (i = 0; hierarchy->level_file_offsets [i]; i++) {
Nov 8, 2009
Nov 8, 2009
561
SDL_RWseek (src, hierarchy->level_file_offsets [i], RW_SEEK_SET);
562
563
564
565
level = read_xcf_level (src);
ty = tx = 0;
for (j = 0; level->tile_file_offsets [j]; j++) {
Nov 8, 2009
Nov 8, 2009
566
SDL_RWseek (src, level->tile_file_offsets [j], RW_SEEK_SET);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
ox = tx+64 > level->width ? level->width % 64 : 64;
oy = ty+64 > level->height ? level->height % 64 : 64;
if (level->tile_file_offsets [j+1]) {
tile = load_tile
(src,
level->tile_file_offsets [j+1] - level->tile_file_offsets [j],
hierarchy->bpp,
ox, oy);
}
else {
tile = load_tile
(src,
ox*oy*6,
hierarchy->bpp,
ox, oy);
}
p8 = tile;
p16 = (Uint16 *) p8;
p = (Uint32 *) p8;
for (y=ty; y < ty+oy; y++) {
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:
fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
return 1;
}
break;
case 1: // Indexed/Greyscale
switch (head->image_type) {
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)
Jul 21, 2007
Jul 21, 2007
645
646
| (((Uint32) (*p8)) << 0);
++p8;
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
}
break;
default:
fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
return 1;
}
break;
}
}
tx += 64;
if (tx >= level->width) {
tx = 0;
ty += 64;
}
if (ty >= level->height) {
break;
}
free_xcf_tile (tile);
}
free_xcf_level (level);
}
free_xcf_hierarchy (hierarchy);
Feb 21, 2003
Feb 21, 2003
671
672
return 0;
673
674
}
Feb 4, 2006
Feb 4, 2006
675
676
SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
{
Feb 3, 2013
Feb 3, 2013
677
Sint64 start;
Feb 4, 2006
Feb 4, 2006
678
const char *error = NULL;
679
680
681
682
SDL_Surface *surface, *lays;
xcf_header * head;
xcf_layer * layer;
xcf_channel ** channel;
Feb 4, 2006
Feb 4, 2006
683
int chnls, i, offsets;
Feb 3, 2013
Feb 3, 2013
684
Sint64 offset, fp;
685
686
687
unsigned char * (* load_tile) (SDL_RWops *, Uint32, int, int, int);
Jan 4, 2004
Jan 4, 2004
688
689
690
691
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
692
start = SDL_RWtell(src);
Jan 4, 2004
Jan 4, 2004
693
Feb 21, 2003
Feb 21, 2003
694
695
696
/* Initialize the data we will clean up when we're done */
surface = NULL;
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
head = read_xcf_header (src);
switch (head->compr) {
case COMPR_NONE:
load_tile = load_xcf_tile_none;
break;
case COMPR_RLE:
load_tile = load_xcf_tile_rle;
break;
default:
fprintf (stderr, "Unsupported Compression.\n");
free_xcf_header (head);
return NULL;
}
/* Create the surface of the appropriate type */
Jan 23, 2012
Jan 23, 2012
713
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
714
715
716
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
if ( surface == NULL ) {
Feb 4, 2006
Feb 4, 2006
717
error = "Out of memory";
718
719
720
721
722
723
goto done;
}
head->layer_file_offsets = NULL;
offsets = 0;
Feb 21, 2003
Feb 21, 2003
724
while ((offset = SDL_ReadBE32 (src))) {
725
head->layer_file_offsets = (Uint32 *) realloc (head->layer_file_offsets, sizeof (Uint32) * (offsets+1));
Feb 3, 2013
Feb 3, 2013
726
head->layer_file_offsets [offsets] = (Uint32)offset;
727
728
729
730
offsets++;
}
fp = SDL_RWtell (src);
Jan 23, 2012
Jan 23, 2012
731
lays = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
732
733
734
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
if ( lays == NULL ) {
Feb 4, 2006
Feb 4, 2006
735
error = "Out of memory";
736
737
738
739
740
741
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
742
SDL_RWseek (src, head->layer_file_offsets [i-1], RW_SEEK_SET);
743
744
745
746
747
748
749
750
751
752
753
754
layer = read_xcf_layer (src);
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;
May 3, 2002
May 3, 2002
755
756
757
if (layer->visible)
SDL_BlitSurface (lays, &rs, surface, &rd);
free_xcf_layer (layer);
758
759
760
761
}
SDL_FreeSurface (lays);
Nov 8, 2009
Nov 8, 2009
762
SDL_RWseek (src, fp, RW_SEEK_SET);
763
764
765
766
// read channels
channel = NULL;
chnls = 0;
Feb 21, 2003
Feb 21, 2003
767
while ((offset = SDL_ReadBE32 (src))) {
Feb 3, 2013
Feb 3, 2013
768
channel = (xcf_channel **) SDL_realloc (channel, sizeof (xcf_channel *) * (chnls+1));
769
fp = SDL_RWtell (src);
Nov 8, 2009
Nov 8, 2009
770
SDL_RWseek (src, offset, RW_SEEK_SET);
771
channel [chnls++] = (read_xcf_channel (src));
Nov 8, 2009
Nov 8, 2009
772
SDL_RWseek (src, fp, RW_SEEK_SET);
773
774
775
776
777
}
if (chnls) {
SDL_Surface * chs;
Jan 23, 2012
Jan 23, 2012
778
chs = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
779
780
781
0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
if (chs == NULL) {
Feb 4, 2006
Feb 4, 2006
782
error = "Out of memory";
783
784
785
786
goto done;
}
for (i = 0; i < chnls; i++) {
// printf ("CNLBLT %i\n", i);
May 3, 2002
May 3, 2002
787
if (!channel [i]->selection && channel [i]->visible) {
Feb 3, 2013
Feb 3, 2013
788
create_channel_surface (chs, (xcf_image_type)head->image_type, channel [i]->color, channel [i]->opacity);
789
790
791
792
793
794
795
796
SDL_BlitSurface (chs, NULL, surface, NULL);
}
free_xcf_channel (channel [i]);
}
SDL_FreeSurface (chs);
}
Feb 4, 2006
Feb 4, 2006
797
done:
798
free_xcf_header (head);
Feb 4, 2006
Feb 4, 2006
799
if ( error ) {
Nov 8, 2009
Nov 8, 2009
800
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
801
802
803
804
805
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
IMG_SetError(error);
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
}
return(surface);
}
#else
/* 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 */