IMG_xcf.c
changeset 26 d51719e1b5d9
child 53 96b084473b47
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/IMG_xcf.c	Wed Mar 07 17:16:38 2001 +0000
     1.3 @@ -0,0 +1,796 @@
     1.4 +/*
     1.5 +    IMGLIB:  An example image loading library for use with SDL
     1.6 +    Copyright (C) 1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +
    1.27 +    XCF support by Bernhard J. Pietsch <bjtp@gmx.net>
    1.28 +*/
    1.29 +
    1.30 +/* This is a XCF image file loading framework */
    1.31 +
    1.32 +#include <stdio.h>
    1.33 +#include <ctype.h>
    1.34 +#include <string.h>
    1.35 +
    1.36 +#include "SDL_image.h"
    1.37 +
    1.38 +#ifdef LOAD_XCF
    1.39 +
    1.40 +static char prop_names [][30] = {
    1.41 +  "end",
    1.42 +  "colormap",
    1.43 +  "active_layer",
    1.44 +  "active_channel",
    1.45 +  "selection",
    1.46 +  "floating_selection",
    1.47 +  "opacity",
    1.48 +  "mode",
    1.49 +  "visible",
    1.50 +  "linked",
    1.51 +  "preserve_transparency",
    1.52 +  "apply_mask",
    1.53 +  "edit_mask",
    1.54 +  "show_mask",
    1.55 +  "show_masked",
    1.56 +  "offsets",
    1.57 +  "color",
    1.58 +  "compression",
    1.59 +  "guides",
    1.60 +  "resolution",
    1.61 +  "tattoo",
    1.62 +  "parasites",
    1.63 +  "unit",
    1.64 +  "paths",
    1.65 +  "user_unit"
    1.66 +};
    1.67 +
    1.68 +typedef enum
    1.69 +{
    1.70 +  PROP_END = 0,
    1.71 +  PROP_COLORMAP = 1,
    1.72 +  PROP_ACTIVE_LAYER = 2,
    1.73 +  PROP_ACTIVE_CHANNEL = 3,
    1.74 +  PROP_SELECTION = 4,
    1.75 +  PROP_FLOATING_SELECTION = 5,
    1.76 +  PROP_OPACITY = 6,
    1.77 +  PROP_MODE = 7,
    1.78 +  PROP_VISIBLE = 8,
    1.79 +  PROP_LINKED = 9,
    1.80 +  PROP_PRESERVE_TRANSPARENCY = 10,
    1.81 +  PROP_APPLY_MASK = 11,
    1.82 +  PROP_EDIT_MASK = 12,
    1.83 +  PROP_SHOW_MASK = 13,
    1.84 +  PROP_SHOW_MASKED = 14,
    1.85 +  PROP_OFFSETS = 15,
    1.86 +  PROP_COLOR = 16,
    1.87 +  PROP_COMPRESSION = 17,
    1.88 +  PROP_GUIDES = 18,
    1.89 +  PROP_RESOLUTION = 19,
    1.90 +  PROP_TATTOO = 20,
    1.91 +  PROP_PARASITES = 21,
    1.92 +  PROP_UNIT = 22,
    1.93 +  PROP_PATHS = 23,
    1.94 +  PROP_USER_UNIT = 24
    1.95 +} xcf_prop_type;
    1.96 +
    1.97 +typedef enum {
    1.98 +  COMPR_NONE    = 0,
    1.99 +  COMPR_RLE     = 1,
   1.100 +  COMPR_ZLIB    = 2,
   1.101 +  COMPR_FRACTAL = 3
   1.102 +} xcf_compr_type;
   1.103 +
   1.104 +typedef enum {
   1.105 +  IMAGE_RGB       = 0,
   1.106 +  IMAGE_GREYSCALE = 1,
   1.107 +  IMAGE_INDEXED   = 2
   1.108 +} xcf_image_type;
   1.109 +
   1.110 +typedef struct {
   1.111 +  Uint32 id;
   1.112 +  Uint32 length;
   1.113 +  union {
   1.114 +    struct {
   1.115 +      Uint32 num;
   1.116 +      char * cmap;
   1.117 +    } colormap; // 1
   1.118 +    struct {
   1.119 +      Uint32 drawable_offset;
   1.120 +    } floating_selection; // 5
   1.121 +    Sint32 opacity;
   1.122 +    Sint32 mode;
   1.123 +    int    visible;
   1.124 +    int    linked;
   1.125 +    int    preserve_transparency;
   1.126 +    int    apply_mask;
   1.127 +    int    show_mask;
   1.128 +    struct {
   1.129 +      Sint32 x;
   1.130 +      Sint32 y;
   1.131 +    } offset;
   1.132 +    unsigned char color [3];
   1.133 +    Uint8 compression;
   1.134 +    struct {
   1.135 +      Sint32 x;
   1.136 +      Sint32 y;
   1.137 +    } resolution;
   1.138 +    struct {
   1.139 +      char * name;
   1.140 +      Uint32 flags;
   1.141 +      Uint32 size;
   1.142 +      char * data;
   1.143 +    } parasite;
   1.144 +  } data;
   1.145 +} xcf_prop;
   1.146 +
   1.147 +typedef struct {
   1.148 +  char   sign [14];
   1.149 +  Uint32 width;
   1.150 +  Uint32 height;
   1.151 +  Sint32 image_type;
   1.152 +  xcf_prop * properties;
   1.153 +
   1.154 +  Uint32 * layer_file_offsets;
   1.155 +  Uint32 * channel_file_offsets;
   1.156 +
   1.157 +  xcf_compr_type compr;
   1.158 +  Uint32         cm_num;
   1.159 +  unsigned char * cm_map;
   1.160 +} xcf_header;
   1.161 +
   1.162 +typedef struct {
   1.163 +  Uint32 width;
   1.164 +  Uint32 height;
   1.165 +  Sint32 layer_type;
   1.166 +  char * name;
   1.167 +  xcf_prop * properties;
   1.168 +
   1.169 +  Uint32 hierarchy_file_offset;
   1.170 +  Uint32 layer_mask_offset;
   1.171 +
   1.172 +  Uint32 offset_x;
   1.173 +  Uint32 offset_y;
   1.174 +} xcf_layer;
   1.175 +
   1.176 +typedef struct {
   1.177 +  Uint32 width;
   1.178 +  Uint32 height;
   1.179 +  char * name;
   1.180 +  xcf_prop * properties;
   1.181 +
   1.182 +  Uint32 hierarchy_file_offset;
   1.183 +
   1.184 +  Uint32 color;
   1.185 +  Uint32 opacity;
   1.186 +  int selection : 1;
   1.187 +} xcf_channel;
   1.188 +
   1.189 +typedef struct {
   1.190 +  Uint32 width;
   1.191 +  Uint32 height;
   1.192 +  Uint32 bpp;
   1.193 +
   1.194 +  Uint32 * level_file_offsets;
   1.195 +} xcf_hierarchy;
   1.196 +
   1.197 +typedef struct {
   1.198 +  Uint32 width;
   1.199 +  Uint32 height;
   1.200 +
   1.201 +  Uint32 * tile_file_offsets;
   1.202 +} xcf_level;
   1.203 +
   1.204 +typedef unsigned char * xcf_tile;
   1.205 +
   1.206 +typedef unsigned char * (* load_tile_type) (SDL_RWops *, Uint32, int, int, int);
   1.207 +
   1.208 +
   1.209 +/* See if an image is contained in a data source */
   1.210 +int IMG_isXCF(SDL_RWops *src) {
   1.211 +  int is_XCF;
   1.212 +  char magic[14];
   1.213 +
   1.214 +  is_XCF = 0;
   1.215 +  if ( SDL_RWread(src, magic, 14, 1) ) {
   1.216 +    if (strncmp(magic, "gimp xcf ", 9) == 0) {
   1.217 +      is_XCF = 1;
   1.218 +    }
   1.219 +  }
   1.220 +
   1.221 +  return(is_XCF);
   1.222 +}
   1.223 +
   1.224 +static char * read_string (SDL_RWops * src) {
   1.225 +  Uint32 tmp;
   1.226 +  char * data;
   1.227 +
   1.228 +  tmp = SDL_ReadBE32 (src);
   1.229 +  if (tmp > 0) {
   1.230 +    data = (char *) malloc (sizeof (char) * tmp);
   1.231 +    SDL_RWread (src, data, tmp, 1);
   1.232 +  }
   1.233 +  else {
   1.234 +    data = NULL;
   1.235 +  }
   1.236 +
   1.237 +  return data;
   1.238 +}
   1.239 +
   1.240 +
   1.241 +static Uint32 Swap32 (Uint32 v) {
   1.242 +  return
   1.243 +    ((v & 0x000000FF) << 16)
   1.244 +    |  ((v & 0x0000FF00))
   1.245 +    |  ((v & 0x00FF0000) >> 16)
   1.246 +    |  ((v & 0xFF000000));
   1.247 +}
   1.248 +
   1.249 +void xcf_read_property (SDL_RWops * src, xcf_prop * prop) {
   1.250 +  prop->id = SDL_ReadBE32 (src);
   1.251 +  prop->length = SDL_ReadBE32 (src);
   1.252 +
   1.253 +  printf ("%.8X: %s: %d\n", SDL_RWtell (src), prop->id < 25 ? prop_names [prop->id] : "unknown", prop->length);
   1.254 +
   1.255 +  switch (prop->id) {
   1.256 +  case PROP_COLORMAP:
   1.257 +    prop->data.colormap.num = SDL_ReadBE32 (src);
   1.258 +    prop->data.colormap.cmap = (char *) malloc (sizeof (char) * prop->data.colormap.num * 3);
   1.259 +    SDL_RWread (src, prop->data.colormap.cmap, prop->data.colormap.num*3, 1);
   1.260 +    break;
   1.261 +
   1.262 +  case PROP_OFFSETS:
   1.263 +    prop->data.offset.x = SDL_ReadBE32 (src);
   1.264 +    prop->data.offset.y = SDL_ReadBE32 (src);
   1.265 +    break;
   1.266 +  case PROP_OPACITY:
   1.267 +    prop->data.opacity = SDL_ReadBE32 (src);
   1.268 +    break;
   1.269 +  case PROP_COMPRESSION:
   1.270 +  case PROP_COLOR:
   1.271 +    SDL_RWread (src, &prop->data, prop->length, 1);
   1.272 +    break;
   1.273 +  default:
   1.274 +    //    SDL_RWread (src, &prop->data, prop->length, 1);
   1.275 +    SDL_RWseek (src, prop->length, SEEK_CUR);
   1.276 +  }
   1.277 +}
   1.278 +
   1.279 +void free_xcf_header (xcf_header * h) {
   1.280 +  if (h->cm_num)
   1.281 +    free (h->cm_map);
   1.282 +
   1.283 +  free (h);
   1.284 +}
   1.285 +
   1.286 +xcf_header * read_xcf_header (SDL_RWops * src) {
   1.287 +  xcf_header * h;
   1.288 +  xcf_prop prop;
   1.289 +
   1.290 +  h = (xcf_header *) malloc (sizeof (xcf_header));
   1.291 +  SDL_RWread (src, h->sign, 14, 1);
   1.292 +  h->width       = SDL_ReadBE32 (src);
   1.293 +  h->height      = SDL_ReadBE32 (src);
   1.294 +  h->image_type  = SDL_ReadBE32 (src);
   1.295 +
   1.296 +  h->properties = NULL;
   1.297 +  h->compr      = COMPR_NONE;
   1.298 +  h->cm_num = 0;
   1.299 +  h->cm_map = NULL;
   1.300 +
   1.301 +  // Just read, don't save
   1.302 +  do {
   1.303 +    xcf_read_property (src, &prop);
   1.304 +    if (prop.id == PROP_COMPRESSION)
   1.305 +      h->compr = prop.data.compression;
   1.306 +    else if (prop.id == PROP_COLORMAP) {
   1.307 +      int i;
   1.308 +
   1.309 +      h->cm_num = prop.data.colormap.num;
   1.310 +      h->cm_map = (char *) malloc (sizeof (char) * 3 * h->cm_num);
   1.311 +      memcpy (h->cm_map, prop.data.colormap.cmap, 3*sizeof (char)*h->cm_num);
   1.312 +      free (prop.data.colormap.cmap);
   1.313 +    }
   1.314 +  } while (prop.id != PROP_END);
   1.315 +
   1.316 +  return h;
   1.317 +}
   1.318 +
   1.319 +void free_xcf_layer (xcf_layer * l) {
   1.320 +  free (l->name);
   1.321 +  free (l);
   1.322 +}
   1.323 +
   1.324 +xcf_layer * read_xcf_layer (SDL_RWops * src) {
   1.325 +  xcf_layer * l;
   1.326 +  xcf_prop    prop;
   1.327 +
   1.328 +  l = (xcf_layer *) malloc (sizeof (xcf_layer));
   1.329 +  l->width  = SDL_ReadBE32 (src);
   1.330 +  l->height = SDL_ReadBE32 (src);
   1.331 +  l->layer_type = SDL_ReadBE32 (src);
   1.332 +
   1.333 +  l->name = read_string (src);
   1.334 +
   1.335 +  do {
   1.336 +    xcf_read_property (src, &prop);
   1.337 +    if (prop.id == PROP_OFFSETS) {
   1.338 +      l->offset_x = prop.data.offset.x;
   1.339 +      l->offset_y = prop.data.offset.y;
   1.340 +    }
   1.341 +  } while (prop.id != PROP_END);
   1.342 +
   1.343 +  l->hierarchy_file_offset = SDL_ReadBE32 (src);
   1.344 +  l->layer_mask_offset     = SDL_ReadBE32 (src);
   1.345 +
   1.346 +  return l;
   1.347 +}
   1.348 +
   1.349 +void free_xcf_channel (xcf_channel * c) {
   1.350 +  free (c->name);
   1.351 +  free (c);
   1.352 +}
   1.353 +
   1.354 +xcf_channel * read_xcf_channel (SDL_RWops * src) {
   1.355 +  xcf_channel * l;
   1.356 +  xcf_prop    prop;
   1.357 +
   1.358 +  l = (xcf_channel *) malloc (sizeof (xcf_channel));
   1.359 +  l->width  = SDL_ReadBE32 (src);
   1.360 +  l->height = SDL_ReadBE32 (src);
   1.361 +
   1.362 +  l->name = read_string (src);
   1.363 +
   1.364 +  l->selection = 0;
   1.365 +  do {
   1.366 +    xcf_read_property (src, &prop);
   1.367 +    switch (prop.id) {
   1.368 +    case PROP_OPACITY:
   1.369 +      l->opacity = prop.data.opacity << 24;
   1.370 +      break;
   1.371 +    case PROP_COLOR:
   1.372 +      l->color = ((Uint32) prop.data.color[0] << 16)
   1.373 +	| ((Uint32) prop.data.color[1] << 8)
   1.374 +	| ((Uint32) prop.data.color[2]);
   1.375 +      break;
   1.376 +    case PROP_SELECTION:
   1.377 +      l->selection = 1;
   1.378 +      break;
   1.379 +    default:
   1.380 +    }
   1.381 +  } while (prop.id != PROP_END);
   1.382 +
   1.383 +  l->hierarchy_file_offset = SDL_ReadBE32 (src);
   1.384 +
   1.385 +  return l;
   1.386 +}
   1.387 +
   1.388 +void free_xcf_hierarchy (xcf_hierarchy * h) {
   1.389 +  free (h->level_file_offsets);
   1.390 +  free (h);
   1.391 +}
   1.392 +
   1.393 +xcf_hierarchy * read_xcf_hierarchy (SDL_RWops * src) {
   1.394 +  xcf_hierarchy * h;
   1.395 +  int i;
   1.396 +
   1.397 +  h = (xcf_hierarchy *) malloc (sizeof (xcf_hierarchy));
   1.398 +  h->width  = SDL_ReadBE32 (src);
   1.399 +  h->height = SDL_ReadBE32 (src);
   1.400 +  h->bpp    = SDL_ReadBE32 (src);
   1.401 +
   1.402 +  h->level_file_offsets = NULL;
   1.403 +  i = 0;
   1.404 +  do {
   1.405 +    h->level_file_offsets = (Uint32 *) realloc (h->level_file_offsets, sizeof (Uint32) * (i+1));
   1.406 +    h->level_file_offsets [i] = SDL_ReadBE32 (src);
   1.407 +  } while (h->level_file_offsets [i++]);
   1.408 +
   1.409 +  return h;
   1.410 +}
   1.411 +
   1.412 +void free_xcf_level (xcf_level * l) {
   1.413 +  free (l->tile_file_offsets);
   1.414 +  free (l);
   1.415 +}
   1.416 +
   1.417 +xcf_level * read_xcf_level (SDL_RWops * src) {
   1.418 +  xcf_level * l;
   1.419 +  int i;
   1.420 +
   1.421 +  l = (xcf_level *) malloc (sizeof (xcf_level));
   1.422 +  l->width  = SDL_ReadBE32 (src);
   1.423 +  l->height = SDL_ReadBE32 (src);
   1.424 +
   1.425 +  l->tile_file_offsets = NULL;
   1.426 +  i = 0;
   1.427 +  do {
   1.428 +    l->tile_file_offsets = (Uint32 *) realloc (l->tile_file_offsets, sizeof (Uint32) * (i+1));
   1.429 +    l->tile_file_offsets [i] = SDL_ReadBE32 (src);
   1.430 +  } while (l->tile_file_offsets [i++]);
   1.431 +
   1.432 +  return l;
   1.433 +}
   1.434 +
   1.435 +void free_xcf_tile (unsigned char * t) {
   1.436 +  free (t);
   1.437 +}
   1.438 +
   1.439 +unsigned char * load_xcf_tile_none (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
   1.440 +  unsigned char * load;
   1.441 +
   1.442 +  load = (char *) malloc (len); // expect this is okay
   1.443 +  SDL_RWread (src, load, len, 1);
   1.444 +
   1.445 +  return load;
   1.446 +}
   1.447 +
   1.448 +unsigned char * load_xcf_tile_rle (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
   1.449 +  unsigned char * load, * t, * data, * d;
   1.450 +  Uint32 reallen;
   1.451 +  int i, size, count, j, length;
   1.452 +  unsigned char val;
   1.453 +
   1.454 +  t = load = (char *) malloc (len);
   1.455 +  reallen = SDL_RWread (src, t, 1, len);
   1.456 +
   1.457 +  data = (char *) malloc (x*y*bpp);
   1.458 +  for (i = 0; i < bpp; i++) {
   1.459 +    d    = data + i;
   1.460 +    size = x*y;
   1.461 +    count = 0;
   1.462 + 
   1.463 +    while (size > 0) {
   1.464 +      val = *t++;
   1.465 +
   1.466 +      length = val;
   1.467 +      if (length >= 128) {
   1.468 +	length = 255 - (length - 1);
   1.469 +	if (length == 128) {
   1.470 +	  length = (*t << 8) + t[1];
   1.471 +	  t += 2;
   1.472 +	}
   1.473 +
   1.474 +	count += length;
   1.475 +	size -= length;
   1.476 +
   1.477 +	while (length-- > 0) {
   1.478 +	  *d = *t++;
   1.479 +	  d += bpp;
   1.480 +	}
   1.481 +      }
   1.482 +      else {
   1.483 +	length += 1;
   1.484 +	if (length == 128) {
   1.485 +	  length = (*t << 8) + t[1];
   1.486 +	  t += 2;
   1.487 +	}
   1.488 +
   1.489 +	count += length;
   1.490 +	size -= length;
   1.491 +
   1.492 +	val = *t++;
   1.493 +
   1.494 +	for (j = 0; j < length; j++) {
   1.495 +	  *d = val;
   1.496 +	  d += bpp;
   1.497 +	}
   1.498 +      }
   1.499 +    }
   1.500 +  }
   1.501 +
   1.502 +  free (load);
   1.503 +  return (data);
   1.504 +}
   1.505 +
   1.506 +static Uint32 rgb2grey (Uint32 a) {
   1.507 +  Uint8 l;
   1.508 +  l = 0.2990 * ((a && 0x00FF0000) >> 16)
   1.509 +    + 0.5870 * ((a && 0x0000FF00) >>  8)
   1.510 +    + 0.1140 * ((a && 0x000000FF));
   1.511 +
   1.512 +  return (l << 16) | (l << 8) | l;
   1.513 +}
   1.514 +
   1.515 +void create_channel_surface (SDL_Surface * surf, xcf_image_type itype, Uint32 color, Uint32 opacity) {
   1.516 +  Uint32 c;
   1.517 +
   1.518 +  switch (itype) {
   1.519 +  case IMAGE_RGB:
   1.520 +  case IMAGE_INDEXED:
   1.521 +    c = opacity | color;
   1.522 +    break;
   1.523 +  case IMAGE_GREYSCALE:
   1.524 +    c = opacity | rgb2grey (color);
   1.525 +    break;
   1.526 +  }
   1.527 +  SDL_FillRect (surf, NULL, c);
   1.528 +}
   1.529 +
   1.530 +int do_layer_surface (SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile) {
   1.531 +  xcf_hierarchy * hierarchy;
   1.532 +  xcf_level     * level;
   1.533 +  unsigned char * tile;
   1.534 +  Uint8  * p8;
   1.535 +  Uint16 * p16;
   1.536 +  Uint32 * p;
   1.537 +  int x, y, tx, ty, ox, oy, width, height, i, j;
   1.538 +  Uint32 *row;
   1.539 +
   1.540 +  SDL_RWseek (src, layer->hierarchy_file_offset, SEEK_SET);
   1.541 +  hierarchy = read_xcf_hierarchy (src);
   1.542 +
   1.543 +  level = NULL;
   1.544 +  for (i = 0; hierarchy->level_file_offsets [i]; i++) {
   1.545 +    SDL_RWseek (src, hierarchy->level_file_offsets [i], SEEK_SET);
   1.546 +    level = read_xcf_level (src);
   1.547 +
   1.548 +    ty = tx = 0;
   1.549 +    for (j = 0; level->tile_file_offsets [j]; j++) {
   1.550 +      SDL_RWseek (src, level->tile_file_offsets [j], SEEK_SET);
   1.551 +      ox = tx+64 > level->width ? level->width % 64 : 64;
   1.552 +      oy = ty+64 > level->height ? level->height % 64 : 64;
   1.553 +
   1.554 +      if (level->tile_file_offsets [j+1]) {
   1.555 +	tile = load_tile
   1.556 +	  (src,
   1.557 +	   level->tile_file_offsets [j+1] - level->tile_file_offsets [j],
   1.558 +	   hierarchy->bpp,
   1.559 +	   ox, oy);
   1.560 +      }
   1.561 +      else {
   1.562 +	tile = load_tile
   1.563 +	  (src,
   1.564 +	   ox*oy*6,
   1.565 +	   hierarchy->bpp,
   1.566 +	   ox, oy);
   1.567 +      }
   1.568 +
   1.569 +      p8  = tile;
   1.570 +      p16 = (Uint16 *) p8;
   1.571 +      p   = (Uint32 *) p8;
   1.572 +      for (y=ty; y < ty+oy; y++) {
   1.573 +	row = (Uint32 *)((Uint8 *)surface->pixels + y*surface->pitch + tx*4);
   1.574 +	switch (hierarchy->bpp) {
   1.575 +	case 4:
   1.576 +	  for (x=tx; x < tx+ox; x++)
   1.577 +	    *row++ = Swap32 (*p++);
   1.578 +	  break;
   1.579 +	case 3:
   1.580 +	  for (x=tx; x < tx+ox; x++) {
   1.581 +	    *row = 0xFF000000;
   1.582 +	    *row |= ((Uint32) *(p8++) << 16);
   1.583 +	    *row |= ((Uint32) *(p8++) << 8);
   1.584 +	    *row |= ((Uint32) *(p8++) << 0);
   1.585 +	    row++;
   1.586 +	  }
   1.587 +	  break;
   1.588 +	case 2: // Indexed/Greyscale + Alpha
   1.589 +	  switch (head->image_type) {
   1.590 +	  case IMAGE_INDEXED:
   1.591 +	    for (x=tx; x < tx+ox; x++) {
   1.592 +	      *row =  ((Uint32) (head->cm_map [*p8*3])     << 16);
   1.593 +	      *row |= ((Uint32) (head->cm_map [*p8*3+1])   << 8);
   1.594 +	      *row |= ((Uint32) (head->cm_map [*p8++*3+2]) << 0);
   1.595 +	      *row |= ((Uint32) *p8++ << 24);;
   1.596 +	      row++;
   1.597 +	    }
   1.598 +	    break;
   1.599 +	  case IMAGE_GREYSCALE:
   1.600 +	    for (x=tx; x < tx+ox; x++) {
   1.601 +	      *row = ((Uint32) *p8 << 16);
   1.602 +	      *row |= ((Uint32) *p8 << 8);
   1.603 +	      *row |= ((Uint32) *p8++ << 0);
   1.604 +	      *row |= ((Uint32) *p8++ << 24);;
   1.605 +	      row++;
   1.606 +	    }
   1.607 +	    break;	    
   1.608 +	  default:
   1.609 +	    fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
   1.610 +	    return 1;
   1.611 +	  }
   1.612 +	  break;
   1.613 +	case 1: // Indexed/Greyscale
   1.614 +	  switch (head->image_type) {
   1.615 +	  case IMAGE_INDEXED:
   1.616 +	    for (x = tx; x < tx+ox; x++) {
   1.617 +	      *row++ = 0xFF000000
   1.618 +		| ((Uint32) (head->cm_map [*p8*3]) << 16)
   1.619 +		| ((Uint32) (head->cm_map [*p8*3+1]) << 8)
   1.620 +		| ((Uint32) (head->cm_map [*p8*3+2]) << 0);
   1.621 +	      p8++;
   1.622 +	    }
   1.623 +	    break;
   1.624 +	  case IMAGE_GREYSCALE:
   1.625 +	    for (x=tx; x < tx+ox; x++) {
   1.626 +	      *row++ = 0xFF000000
   1.627 +		| (((Uint32) (*p8)) << 16)
   1.628 +		| (((Uint32) (*p8)) << 8)
   1.629 +		| (((Uint32) (*p8++)) << 0);
   1.630 +	    }
   1.631 +	    break;	    
   1.632 +	  default:
   1.633 +	    fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
   1.634 +	    return 1;
   1.635 +	  }
   1.636 +	  break;
   1.637 +	}
   1.638 +      }
   1.639 +      tx += 64;
   1.640 +      if (tx >= level->width) {
   1.641 +	tx = 0;
   1.642 +	ty += 64;
   1.643 +      }
   1.644 +      if (ty >= level->height) {
   1.645 +	break;
   1.646 +      }
   1.647 +
   1.648 +      free_xcf_tile (tile);
   1.649 +    }
   1.650 +    free_xcf_level (level);
   1.651 +  }
   1.652 +
   1.653 +  free_xcf_hierarchy (hierarchy);
   1.654 +}
   1.655 +
   1.656 +SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src) {
   1.657 +  SDL_Surface *surface, *lays;
   1.658 +  xcf_header * head;
   1.659 +  xcf_layer  * layer;
   1.660 +  xcf_channel ** channel;
   1.661 +  int read_error, chnls, i, offsets;
   1.662 +  Uint32 offset, fp;
   1.663 +
   1.664 +  unsigned char * (* load_tile) (SDL_RWops *, Uint32, int, int, int);
   1.665 +
   1.666 +  /* Initialize the data we will clean up when we're done */
   1.667 +  surface = NULL;
   1.668 +  read_error = 0;
   1.669 +
   1.670 +  /* Check to make sure we have something to do */
   1.671 +  if ( ! src ) {
   1.672 +    goto done;
   1.673 +  }
   1.674 +
   1.675 +  head = read_xcf_header (src);
   1.676 +
   1.677 +  switch (head->compr) {
   1.678 +  case COMPR_NONE:
   1.679 +    load_tile = load_xcf_tile_none;
   1.680 +    break;
   1.681 +  case COMPR_RLE:
   1.682 +    load_tile = load_xcf_tile_rle;
   1.683 +    break;
   1.684 +  default:
   1.685 +    fprintf (stderr, "Unsupported Compression.\n");
   1.686 +    free_xcf_header (head);
   1.687 +    return NULL;
   1.688 +  }
   1.689 +
   1.690 +  /* Create the surface of the appropriate type */
   1.691 +  surface = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   1.692 +			     0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   1.693 +
   1.694 +  if ( surface == NULL ) {
   1.695 +    IMG_SetError("Out of memory");
   1.696 +    goto done;
   1.697 +  }
   1.698 +
   1.699 +  head->layer_file_offsets = NULL;
   1.700 +  offsets = 0;
   1.701 +
   1.702 +  while (offset = SDL_ReadBE32 (src)) {
   1.703 +    head->layer_file_offsets = (Uint32 *) realloc (head->layer_file_offsets, sizeof (Uint32) * (offsets+1));
   1.704 +    head->layer_file_offsets [offsets] = offset;
   1.705 +    offsets++;
   1.706 +  }
   1.707 +  fp = SDL_RWtell (src);
   1.708 + 
   1.709 +  lays = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   1.710 +			  0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   1.711 +
   1.712 +  if ( lays == NULL ) {
   1.713 +    IMG_SetError("Out of memory");
   1.714 +    goto done;
   1.715 +  }
   1.716 +
   1.717 +  // Blit layers backwards, because Gimp saves them highest first
   1.718 +  for (i = offsets; i > 0; i--) {
   1.719 +    SDL_Rect rs, rd;
   1.720 +    SDL_RWseek (src, head->layer_file_offsets [i-1], SEEK_SET);
   1.721 +
   1.722 +    layer = read_xcf_layer (src);
   1.723 +    do_layer_surface (lays, src, head, layer, load_tile);
   1.724 +    rs.x = 0;
   1.725 +    rs.y = 0;
   1.726 +    rs.w = layer->width;
   1.727 +    rs.h = layer->height;
   1.728 +    rd.x = layer->offset_x;
   1.729 +    rd.y = layer->offset_y;
   1.730 +    rd.w = layer->width;
   1.731 +    rd.h = layer->height;
   1.732 +    free_xcf_layer (layer);
   1.733 +
   1.734 +    SDL_BlitSurface (lays, &rs, surface, &rd);
   1.735 +  }
   1.736 +
   1.737 +  SDL_FreeSurface (lays);
   1.738 +
   1.739 +  SDL_RWseek (src, fp, SEEK_SET);
   1.740 +
   1.741 +  // read channels
   1.742 +  channel = NULL;
   1.743 +  chnls   = 0;
   1.744 +  while (offset = SDL_ReadBE32 (src)) {
   1.745 +    channel = (xcf_channel **) realloc (channel, sizeof (xcf_channel *) * (chnls+1));
   1.746 +    fp = SDL_RWtell (src);
   1.747 +    SDL_RWseek (src, offset, SEEK_SET);
   1.748 +    channel [chnls++] = (read_xcf_channel (src));
   1.749 +    SDL_RWseek (src, fp, SEEK_SET);    
   1.750 +  }
   1.751 +
   1.752 +  if (chnls) {
   1.753 +    SDL_Surface * chs;
   1.754 +
   1.755 +    chs = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   1.756 +			   0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   1.757 +
   1.758 +    if (chs == NULL) {
   1.759 +      IMG_SetError("Out of memory");
   1.760 +      goto done;
   1.761 +    }
   1.762 +    for (i = 0; i < chnls; i++) {
   1.763 +      //      printf ("CNLBLT %i\n", i);
   1.764 +      if (!channel [i]->selection) {
   1.765 +	create_channel_surface (chs, head->image_type, channel [i]->color, channel [i]->opacity);
   1.766 +	SDL_BlitSurface (chs, NULL, surface, NULL);
   1.767 +      }
   1.768 +      free_xcf_channel (channel [i]);
   1.769 +    }
   1.770 +
   1.771 +    SDL_FreeSurface (chs);
   1.772 +  }
   1.773 +
   1.774 + done:
   1.775 +  free_xcf_header (head);
   1.776 +  if ( read_error ) {
   1.777 +    SDL_FreeSurface(surface);
   1.778 +    IMG_SetError("Error reading XCF data");
   1.779 +    surface = NULL;
   1.780 +  }
   1.781 +
   1.782 +  return(surface);
   1.783 +}
   1.784 +
   1.785 +#else
   1.786 +
   1.787 +/* See if an image is contained in a data source */
   1.788 +int IMG_isXCF(SDL_RWops *src)
   1.789 +{
   1.790 +  return(0);
   1.791 +}
   1.792 +
   1.793 +/* Load a XCF type image from an SDL datasource */
   1.794 +SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
   1.795 +{
   1.796 +  return(NULL);
   1.797 +}
   1.798 +
   1.799 +#endif /* LOAD_XCF */