IMG_xcf.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 07 Aug 2013 09:18:35 -0700
changeset 419 e108e1228d62
parent 415 1700d607fce3
child 451 48116d511e5d
permissions -rw-r--r--
Fixed bug 2010 - Memory leaks in do_layer_surface function in IMG_xcf.c file

Nitz

In static int do_layer_surface (SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile)

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