Berni - Wed Mar 7 09:18:02 PST 2001
authorSam Lantinga <slouken@lokigames.com>
Wed, 07 Mar 2001 17:16:38 +0000
changeset 26d51719e1b5d9
parent 25 c9117619e2af
child 27 34870338be23
Berni - Wed Mar 7 09:18:02 PST 2001
* Added initial GIMP XCF support (disabled by default)
CHANGES
IMG.c
IMG_xcf.c
Makefile.am
SDL_image.h
configure.in
     1.1 --- a/CHANGES	Wed Mar 07 17:00:59 2001 +0000
     1.2 +++ b/CHANGES	Wed Mar 07 17:16:38 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.1.1:
     1.6 +Berni - Wed Mar  7 09:18:02 PST 2001
     1.7 + * Added initial GIMP XCF support (disabled by default)
     1.8  Mattias Engdegård - Wed Mar  7 09:01:49 PST 2001
     1.9   * Added general PNM (PPM/PGM/PBM) support
    1.10  Mattias Engdegård - Sun Mar  4 14:23:42 PST 2001
     2.1 --- a/IMG.c	Wed Mar 07 17:00:59 2001 +0000
     2.2 +++ b/IMG.c	Wed Mar 07 17:16:38 2001 +0000
     2.3 @@ -43,6 +43,7 @@
     2.4  	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
     2.5  	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
     2.6  	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
     2.7 +	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
     2.8  	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
     2.9  	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
    2.10  	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/IMG_xcf.c	Wed Mar 07 17:16:38 2001 +0000
     3.3 @@ -0,0 +1,796 @@
     3.4 +/*
     3.5 +    IMGLIB:  An example image loading library for use with SDL
     3.6 +    Copyright (C) 1999  Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Library General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Library General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Library General Public
    3.19 +    License along with this library; if not, write to the Free
    3.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    5635-34 Springhouse Dr.
    3.24 +    Pleasanton, CA 94588 (USA)
    3.25 +    slouken@devolution.com
    3.26 +
    3.27 +    XCF support by Bernhard J. Pietsch <bjtp@gmx.net>
    3.28 +*/
    3.29 +
    3.30 +/* This is a XCF image file loading framework */
    3.31 +
    3.32 +#include <stdio.h>
    3.33 +#include <ctype.h>
    3.34 +#include <string.h>
    3.35 +
    3.36 +#include "SDL_image.h"
    3.37 +
    3.38 +#ifdef LOAD_XCF
    3.39 +
    3.40 +static char prop_names [][30] = {
    3.41 +  "end",
    3.42 +  "colormap",
    3.43 +  "active_layer",
    3.44 +  "active_channel",
    3.45 +  "selection",
    3.46 +  "floating_selection",
    3.47 +  "opacity",
    3.48 +  "mode",
    3.49 +  "visible",
    3.50 +  "linked",
    3.51 +  "preserve_transparency",
    3.52 +  "apply_mask",
    3.53 +  "edit_mask",
    3.54 +  "show_mask",
    3.55 +  "show_masked",
    3.56 +  "offsets",
    3.57 +  "color",
    3.58 +  "compression",
    3.59 +  "guides",
    3.60 +  "resolution",
    3.61 +  "tattoo",
    3.62 +  "parasites",
    3.63 +  "unit",
    3.64 +  "paths",
    3.65 +  "user_unit"
    3.66 +};
    3.67 +
    3.68 +typedef enum
    3.69 +{
    3.70 +  PROP_END = 0,
    3.71 +  PROP_COLORMAP = 1,
    3.72 +  PROP_ACTIVE_LAYER = 2,
    3.73 +  PROP_ACTIVE_CHANNEL = 3,
    3.74 +  PROP_SELECTION = 4,
    3.75 +  PROP_FLOATING_SELECTION = 5,
    3.76 +  PROP_OPACITY = 6,
    3.77 +  PROP_MODE = 7,
    3.78 +  PROP_VISIBLE = 8,
    3.79 +  PROP_LINKED = 9,
    3.80 +  PROP_PRESERVE_TRANSPARENCY = 10,
    3.81 +  PROP_APPLY_MASK = 11,
    3.82 +  PROP_EDIT_MASK = 12,
    3.83 +  PROP_SHOW_MASK = 13,
    3.84 +  PROP_SHOW_MASKED = 14,
    3.85 +  PROP_OFFSETS = 15,
    3.86 +  PROP_COLOR = 16,
    3.87 +  PROP_COMPRESSION = 17,
    3.88 +  PROP_GUIDES = 18,
    3.89 +  PROP_RESOLUTION = 19,
    3.90 +  PROP_TATTOO = 20,
    3.91 +  PROP_PARASITES = 21,
    3.92 +  PROP_UNIT = 22,
    3.93 +  PROP_PATHS = 23,
    3.94 +  PROP_USER_UNIT = 24
    3.95 +} xcf_prop_type;
    3.96 +
    3.97 +typedef enum {
    3.98 +  COMPR_NONE    = 0,
    3.99 +  COMPR_RLE     = 1,
   3.100 +  COMPR_ZLIB    = 2,
   3.101 +  COMPR_FRACTAL = 3
   3.102 +} xcf_compr_type;
   3.103 +
   3.104 +typedef enum {
   3.105 +  IMAGE_RGB       = 0,
   3.106 +  IMAGE_GREYSCALE = 1,
   3.107 +  IMAGE_INDEXED   = 2
   3.108 +} xcf_image_type;
   3.109 +
   3.110 +typedef struct {
   3.111 +  Uint32 id;
   3.112 +  Uint32 length;
   3.113 +  union {
   3.114 +    struct {
   3.115 +      Uint32 num;
   3.116 +      char * cmap;
   3.117 +    } colormap; // 1
   3.118 +    struct {
   3.119 +      Uint32 drawable_offset;
   3.120 +    } floating_selection; // 5
   3.121 +    Sint32 opacity;
   3.122 +    Sint32 mode;
   3.123 +    int    visible;
   3.124 +    int    linked;
   3.125 +    int    preserve_transparency;
   3.126 +    int    apply_mask;
   3.127 +    int    show_mask;
   3.128 +    struct {
   3.129 +      Sint32 x;
   3.130 +      Sint32 y;
   3.131 +    } offset;
   3.132 +    unsigned char color [3];
   3.133 +    Uint8 compression;
   3.134 +    struct {
   3.135 +      Sint32 x;
   3.136 +      Sint32 y;
   3.137 +    } resolution;
   3.138 +    struct {
   3.139 +      char * name;
   3.140 +      Uint32 flags;
   3.141 +      Uint32 size;
   3.142 +      char * data;
   3.143 +    } parasite;
   3.144 +  } data;
   3.145 +} xcf_prop;
   3.146 +
   3.147 +typedef struct {
   3.148 +  char   sign [14];
   3.149 +  Uint32 width;
   3.150 +  Uint32 height;
   3.151 +  Sint32 image_type;
   3.152 +  xcf_prop * properties;
   3.153 +
   3.154 +  Uint32 * layer_file_offsets;
   3.155 +  Uint32 * channel_file_offsets;
   3.156 +
   3.157 +  xcf_compr_type compr;
   3.158 +  Uint32         cm_num;
   3.159 +  unsigned char * cm_map;
   3.160 +} xcf_header;
   3.161 +
   3.162 +typedef struct {
   3.163 +  Uint32 width;
   3.164 +  Uint32 height;
   3.165 +  Sint32 layer_type;
   3.166 +  char * name;
   3.167 +  xcf_prop * properties;
   3.168 +
   3.169 +  Uint32 hierarchy_file_offset;
   3.170 +  Uint32 layer_mask_offset;
   3.171 +
   3.172 +  Uint32 offset_x;
   3.173 +  Uint32 offset_y;
   3.174 +} xcf_layer;
   3.175 +
   3.176 +typedef struct {
   3.177 +  Uint32 width;
   3.178 +  Uint32 height;
   3.179 +  char * name;
   3.180 +  xcf_prop * properties;
   3.181 +
   3.182 +  Uint32 hierarchy_file_offset;
   3.183 +
   3.184 +  Uint32 color;
   3.185 +  Uint32 opacity;
   3.186 +  int selection : 1;
   3.187 +} xcf_channel;
   3.188 +
   3.189 +typedef struct {
   3.190 +  Uint32 width;
   3.191 +  Uint32 height;
   3.192 +  Uint32 bpp;
   3.193 +
   3.194 +  Uint32 * level_file_offsets;
   3.195 +} xcf_hierarchy;
   3.196 +
   3.197 +typedef struct {
   3.198 +  Uint32 width;
   3.199 +  Uint32 height;
   3.200 +
   3.201 +  Uint32 * tile_file_offsets;
   3.202 +} xcf_level;
   3.203 +
   3.204 +typedef unsigned char * xcf_tile;
   3.205 +
   3.206 +typedef unsigned char * (* load_tile_type) (SDL_RWops *, Uint32, int, int, int);
   3.207 +
   3.208 +
   3.209 +/* See if an image is contained in a data source */
   3.210 +int IMG_isXCF(SDL_RWops *src) {
   3.211 +  int is_XCF;
   3.212 +  char magic[14];
   3.213 +
   3.214 +  is_XCF = 0;
   3.215 +  if ( SDL_RWread(src, magic, 14, 1) ) {
   3.216 +    if (strncmp(magic, "gimp xcf ", 9) == 0) {
   3.217 +      is_XCF = 1;
   3.218 +    }
   3.219 +  }
   3.220 +
   3.221 +  return(is_XCF);
   3.222 +}
   3.223 +
   3.224 +static char * read_string (SDL_RWops * src) {
   3.225 +  Uint32 tmp;
   3.226 +  char * data;
   3.227 +
   3.228 +  tmp = SDL_ReadBE32 (src);
   3.229 +  if (tmp > 0) {
   3.230 +    data = (char *) malloc (sizeof (char) * tmp);
   3.231 +    SDL_RWread (src, data, tmp, 1);
   3.232 +  }
   3.233 +  else {
   3.234 +    data = NULL;
   3.235 +  }
   3.236 +
   3.237 +  return data;
   3.238 +}
   3.239 +
   3.240 +
   3.241 +static Uint32 Swap32 (Uint32 v) {
   3.242 +  return
   3.243 +    ((v & 0x000000FF) << 16)
   3.244 +    |  ((v & 0x0000FF00))
   3.245 +    |  ((v & 0x00FF0000) >> 16)
   3.246 +    |  ((v & 0xFF000000));
   3.247 +}
   3.248 +
   3.249 +void xcf_read_property (SDL_RWops * src, xcf_prop * prop) {
   3.250 +  prop->id = SDL_ReadBE32 (src);
   3.251 +  prop->length = SDL_ReadBE32 (src);
   3.252 +
   3.253 +  printf ("%.8X: %s: %d\n", SDL_RWtell (src), prop->id < 25 ? prop_names [prop->id] : "unknown", prop->length);
   3.254 +
   3.255 +  switch (prop->id) {
   3.256 +  case PROP_COLORMAP:
   3.257 +    prop->data.colormap.num = SDL_ReadBE32 (src);
   3.258 +    prop->data.colormap.cmap = (char *) malloc (sizeof (char) * prop->data.colormap.num * 3);
   3.259 +    SDL_RWread (src, prop->data.colormap.cmap, prop->data.colormap.num*3, 1);
   3.260 +    break;
   3.261 +
   3.262 +  case PROP_OFFSETS:
   3.263 +    prop->data.offset.x = SDL_ReadBE32 (src);
   3.264 +    prop->data.offset.y = SDL_ReadBE32 (src);
   3.265 +    break;
   3.266 +  case PROP_OPACITY:
   3.267 +    prop->data.opacity = SDL_ReadBE32 (src);
   3.268 +    break;
   3.269 +  case PROP_COMPRESSION:
   3.270 +  case PROP_COLOR:
   3.271 +    SDL_RWread (src, &prop->data, prop->length, 1);
   3.272 +    break;
   3.273 +  default:
   3.274 +    //    SDL_RWread (src, &prop->data, prop->length, 1);
   3.275 +    SDL_RWseek (src, prop->length, SEEK_CUR);
   3.276 +  }
   3.277 +}
   3.278 +
   3.279 +void free_xcf_header (xcf_header * h) {
   3.280 +  if (h->cm_num)
   3.281 +    free (h->cm_map);
   3.282 +
   3.283 +  free (h);
   3.284 +}
   3.285 +
   3.286 +xcf_header * read_xcf_header (SDL_RWops * src) {
   3.287 +  xcf_header * h;
   3.288 +  xcf_prop prop;
   3.289 +
   3.290 +  h = (xcf_header *) malloc (sizeof (xcf_header));
   3.291 +  SDL_RWread (src, h->sign, 14, 1);
   3.292 +  h->width       = SDL_ReadBE32 (src);
   3.293 +  h->height      = SDL_ReadBE32 (src);
   3.294 +  h->image_type  = SDL_ReadBE32 (src);
   3.295 +
   3.296 +  h->properties = NULL;
   3.297 +  h->compr      = COMPR_NONE;
   3.298 +  h->cm_num = 0;
   3.299 +  h->cm_map = NULL;
   3.300 +
   3.301 +  // Just read, don't save
   3.302 +  do {
   3.303 +    xcf_read_property (src, &prop);
   3.304 +    if (prop.id == PROP_COMPRESSION)
   3.305 +      h->compr = prop.data.compression;
   3.306 +    else if (prop.id == PROP_COLORMAP) {
   3.307 +      int i;
   3.308 +
   3.309 +      h->cm_num = prop.data.colormap.num;
   3.310 +      h->cm_map = (char *) malloc (sizeof (char) * 3 * h->cm_num);
   3.311 +      memcpy (h->cm_map, prop.data.colormap.cmap, 3*sizeof (char)*h->cm_num);
   3.312 +      free (prop.data.colormap.cmap);
   3.313 +    }
   3.314 +  } while (prop.id != PROP_END);
   3.315 +
   3.316 +  return h;
   3.317 +}
   3.318 +
   3.319 +void free_xcf_layer (xcf_layer * l) {
   3.320 +  free (l->name);
   3.321 +  free (l);
   3.322 +}
   3.323 +
   3.324 +xcf_layer * read_xcf_layer (SDL_RWops * src) {
   3.325 +  xcf_layer * l;
   3.326 +  xcf_prop    prop;
   3.327 +
   3.328 +  l = (xcf_layer *) malloc (sizeof (xcf_layer));
   3.329 +  l->width  = SDL_ReadBE32 (src);
   3.330 +  l->height = SDL_ReadBE32 (src);
   3.331 +  l->layer_type = SDL_ReadBE32 (src);
   3.332 +
   3.333 +  l->name = read_string (src);
   3.334 +
   3.335 +  do {
   3.336 +    xcf_read_property (src, &prop);
   3.337 +    if (prop.id == PROP_OFFSETS) {
   3.338 +      l->offset_x = prop.data.offset.x;
   3.339 +      l->offset_y = prop.data.offset.y;
   3.340 +    }
   3.341 +  } while (prop.id != PROP_END);
   3.342 +
   3.343 +  l->hierarchy_file_offset = SDL_ReadBE32 (src);
   3.344 +  l->layer_mask_offset     = SDL_ReadBE32 (src);
   3.345 +
   3.346 +  return l;
   3.347 +}
   3.348 +
   3.349 +void free_xcf_channel (xcf_channel * c) {
   3.350 +  free (c->name);
   3.351 +  free (c);
   3.352 +}
   3.353 +
   3.354 +xcf_channel * read_xcf_channel (SDL_RWops * src) {
   3.355 +  xcf_channel * l;
   3.356 +  xcf_prop    prop;
   3.357 +
   3.358 +  l = (xcf_channel *) malloc (sizeof (xcf_channel));
   3.359 +  l->width  = SDL_ReadBE32 (src);
   3.360 +  l->height = SDL_ReadBE32 (src);
   3.361 +
   3.362 +  l->name = read_string (src);
   3.363 +
   3.364 +  l->selection = 0;
   3.365 +  do {
   3.366 +    xcf_read_property (src, &prop);
   3.367 +    switch (prop.id) {
   3.368 +    case PROP_OPACITY:
   3.369 +      l->opacity = prop.data.opacity << 24;
   3.370 +      break;
   3.371 +    case PROP_COLOR:
   3.372 +      l->color = ((Uint32) prop.data.color[0] << 16)
   3.373 +	| ((Uint32) prop.data.color[1] << 8)
   3.374 +	| ((Uint32) prop.data.color[2]);
   3.375 +      break;
   3.376 +    case PROP_SELECTION:
   3.377 +      l->selection = 1;
   3.378 +      break;
   3.379 +    default:
   3.380 +    }
   3.381 +  } while (prop.id != PROP_END);
   3.382 +
   3.383 +  l->hierarchy_file_offset = SDL_ReadBE32 (src);
   3.384 +
   3.385 +  return l;
   3.386 +}
   3.387 +
   3.388 +void free_xcf_hierarchy (xcf_hierarchy * h) {
   3.389 +  free (h->level_file_offsets);
   3.390 +  free (h);
   3.391 +}
   3.392 +
   3.393 +xcf_hierarchy * read_xcf_hierarchy (SDL_RWops * src) {
   3.394 +  xcf_hierarchy * h;
   3.395 +  int i;
   3.396 +
   3.397 +  h = (xcf_hierarchy *) malloc (sizeof (xcf_hierarchy));
   3.398 +  h->width  = SDL_ReadBE32 (src);
   3.399 +  h->height = SDL_ReadBE32 (src);
   3.400 +  h->bpp    = SDL_ReadBE32 (src);
   3.401 +
   3.402 +  h->level_file_offsets = NULL;
   3.403 +  i = 0;
   3.404 +  do {
   3.405 +    h->level_file_offsets = (Uint32 *) realloc (h->level_file_offsets, sizeof (Uint32) * (i+1));
   3.406 +    h->level_file_offsets [i] = SDL_ReadBE32 (src);
   3.407 +  } while (h->level_file_offsets [i++]);
   3.408 +
   3.409 +  return h;
   3.410 +}
   3.411 +
   3.412 +void free_xcf_level (xcf_level * l) {
   3.413 +  free (l->tile_file_offsets);
   3.414 +  free (l);
   3.415 +}
   3.416 +
   3.417 +xcf_level * read_xcf_level (SDL_RWops * src) {
   3.418 +  xcf_level * l;
   3.419 +  int i;
   3.420 +
   3.421 +  l = (xcf_level *) malloc (sizeof (xcf_level));
   3.422 +  l->width  = SDL_ReadBE32 (src);
   3.423 +  l->height = SDL_ReadBE32 (src);
   3.424 +
   3.425 +  l->tile_file_offsets = NULL;
   3.426 +  i = 0;
   3.427 +  do {
   3.428 +    l->tile_file_offsets = (Uint32 *) realloc (l->tile_file_offsets, sizeof (Uint32) * (i+1));
   3.429 +    l->tile_file_offsets [i] = SDL_ReadBE32 (src);
   3.430 +  } while (l->tile_file_offsets [i++]);
   3.431 +
   3.432 +  return l;
   3.433 +}
   3.434 +
   3.435 +void free_xcf_tile (unsigned char * t) {
   3.436 +  free (t);
   3.437 +}
   3.438 +
   3.439 +unsigned char * load_xcf_tile_none (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
   3.440 +  unsigned char * load;
   3.441 +
   3.442 +  load = (char *) malloc (len); // expect this is okay
   3.443 +  SDL_RWread (src, load, len, 1);
   3.444 +
   3.445 +  return load;
   3.446 +}
   3.447 +
   3.448 +unsigned char * load_xcf_tile_rle (SDL_RWops * src, Uint32 len, int bpp, int x, int y) {
   3.449 +  unsigned char * load, * t, * data, * d;
   3.450 +  Uint32 reallen;
   3.451 +  int i, size, count, j, length;
   3.452 +  unsigned char val;
   3.453 +
   3.454 +  t = load = (char *) malloc (len);
   3.455 +  reallen = SDL_RWread (src, t, 1, len);
   3.456 +
   3.457 +  data = (char *) malloc (x*y*bpp);
   3.458 +  for (i = 0; i < bpp; i++) {
   3.459 +    d    = data + i;
   3.460 +    size = x*y;
   3.461 +    count = 0;
   3.462 + 
   3.463 +    while (size > 0) {
   3.464 +      val = *t++;
   3.465 +
   3.466 +      length = val;
   3.467 +      if (length >= 128) {
   3.468 +	length = 255 - (length - 1);
   3.469 +	if (length == 128) {
   3.470 +	  length = (*t << 8) + t[1];
   3.471 +	  t += 2;
   3.472 +	}
   3.473 +
   3.474 +	count += length;
   3.475 +	size -= length;
   3.476 +
   3.477 +	while (length-- > 0) {
   3.478 +	  *d = *t++;
   3.479 +	  d += bpp;
   3.480 +	}
   3.481 +      }
   3.482 +      else {
   3.483 +	length += 1;
   3.484 +	if (length == 128) {
   3.485 +	  length = (*t << 8) + t[1];
   3.486 +	  t += 2;
   3.487 +	}
   3.488 +
   3.489 +	count += length;
   3.490 +	size -= length;
   3.491 +
   3.492 +	val = *t++;
   3.493 +
   3.494 +	for (j = 0; j < length; j++) {
   3.495 +	  *d = val;
   3.496 +	  d += bpp;
   3.497 +	}
   3.498 +      }
   3.499 +    }
   3.500 +  }
   3.501 +
   3.502 +  free (load);
   3.503 +  return (data);
   3.504 +}
   3.505 +
   3.506 +static Uint32 rgb2grey (Uint32 a) {
   3.507 +  Uint8 l;
   3.508 +  l = 0.2990 * ((a && 0x00FF0000) >> 16)
   3.509 +    + 0.5870 * ((a && 0x0000FF00) >>  8)
   3.510 +    + 0.1140 * ((a && 0x000000FF));
   3.511 +
   3.512 +  return (l << 16) | (l << 8) | l;
   3.513 +}
   3.514 +
   3.515 +void create_channel_surface (SDL_Surface * surf, xcf_image_type itype, Uint32 color, Uint32 opacity) {
   3.516 +  Uint32 c;
   3.517 +
   3.518 +  switch (itype) {
   3.519 +  case IMAGE_RGB:
   3.520 +  case IMAGE_INDEXED:
   3.521 +    c = opacity | color;
   3.522 +    break;
   3.523 +  case IMAGE_GREYSCALE:
   3.524 +    c = opacity | rgb2grey (color);
   3.525 +    break;
   3.526 +  }
   3.527 +  SDL_FillRect (surf, NULL, c);
   3.528 +}
   3.529 +
   3.530 +int do_layer_surface (SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile) {
   3.531 +  xcf_hierarchy * hierarchy;
   3.532 +  xcf_level     * level;
   3.533 +  unsigned char * tile;
   3.534 +  Uint8  * p8;
   3.535 +  Uint16 * p16;
   3.536 +  Uint32 * p;
   3.537 +  int x, y, tx, ty, ox, oy, width, height, i, j;
   3.538 +  Uint32 *row;
   3.539 +
   3.540 +  SDL_RWseek (src, layer->hierarchy_file_offset, SEEK_SET);
   3.541 +  hierarchy = read_xcf_hierarchy (src);
   3.542 +
   3.543 +  level = NULL;
   3.544 +  for (i = 0; hierarchy->level_file_offsets [i]; i++) {
   3.545 +    SDL_RWseek (src, hierarchy->level_file_offsets [i], SEEK_SET);
   3.546 +    level = read_xcf_level (src);
   3.547 +
   3.548 +    ty = tx = 0;
   3.549 +    for (j = 0; level->tile_file_offsets [j]; j++) {
   3.550 +      SDL_RWseek (src, level->tile_file_offsets [j], SEEK_SET);
   3.551 +      ox = tx+64 > level->width ? level->width % 64 : 64;
   3.552 +      oy = ty+64 > level->height ? level->height % 64 : 64;
   3.553 +
   3.554 +      if (level->tile_file_offsets [j+1]) {
   3.555 +	tile = load_tile
   3.556 +	  (src,
   3.557 +	   level->tile_file_offsets [j+1] - level->tile_file_offsets [j],
   3.558 +	   hierarchy->bpp,
   3.559 +	   ox, oy);
   3.560 +      }
   3.561 +      else {
   3.562 +	tile = load_tile
   3.563 +	  (src,
   3.564 +	   ox*oy*6,
   3.565 +	   hierarchy->bpp,
   3.566 +	   ox, oy);
   3.567 +      }
   3.568 +
   3.569 +      p8  = tile;
   3.570 +      p16 = (Uint16 *) p8;
   3.571 +      p   = (Uint32 *) p8;
   3.572 +      for (y=ty; y < ty+oy; y++) {
   3.573 +	row = (Uint32 *)((Uint8 *)surface->pixels + y*surface->pitch + tx*4);
   3.574 +	switch (hierarchy->bpp) {
   3.575 +	case 4:
   3.576 +	  for (x=tx; x < tx+ox; x++)
   3.577 +	    *row++ = Swap32 (*p++);
   3.578 +	  break;
   3.579 +	case 3:
   3.580 +	  for (x=tx; x < tx+ox; x++) {
   3.581 +	    *row = 0xFF000000;
   3.582 +	    *row |= ((Uint32) *(p8++) << 16);
   3.583 +	    *row |= ((Uint32) *(p8++) << 8);
   3.584 +	    *row |= ((Uint32) *(p8++) << 0);
   3.585 +	    row++;
   3.586 +	  }
   3.587 +	  break;
   3.588 +	case 2: // Indexed/Greyscale + Alpha
   3.589 +	  switch (head->image_type) {
   3.590 +	  case IMAGE_INDEXED:
   3.591 +	    for (x=tx; x < tx+ox; x++) {
   3.592 +	      *row =  ((Uint32) (head->cm_map [*p8*3])     << 16);
   3.593 +	      *row |= ((Uint32) (head->cm_map [*p8*3+1])   << 8);
   3.594 +	      *row |= ((Uint32) (head->cm_map [*p8++*3+2]) << 0);
   3.595 +	      *row |= ((Uint32) *p8++ << 24);;
   3.596 +	      row++;
   3.597 +	    }
   3.598 +	    break;
   3.599 +	  case IMAGE_GREYSCALE:
   3.600 +	    for (x=tx; x < tx+ox; x++) {
   3.601 +	      *row = ((Uint32) *p8 << 16);
   3.602 +	      *row |= ((Uint32) *p8 << 8);
   3.603 +	      *row |= ((Uint32) *p8++ << 0);
   3.604 +	      *row |= ((Uint32) *p8++ << 24);;
   3.605 +	      row++;
   3.606 +	    }
   3.607 +	    break;	    
   3.608 +	  default:
   3.609 +	    fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
   3.610 +	    return 1;
   3.611 +	  }
   3.612 +	  break;
   3.613 +	case 1: // Indexed/Greyscale
   3.614 +	  switch (head->image_type) {
   3.615 +	  case IMAGE_INDEXED:
   3.616 +	    for (x = tx; x < tx+ox; x++) {
   3.617 +	      *row++ = 0xFF000000
   3.618 +		| ((Uint32) (head->cm_map [*p8*3]) << 16)
   3.619 +		| ((Uint32) (head->cm_map [*p8*3+1]) << 8)
   3.620 +		| ((Uint32) (head->cm_map [*p8*3+2]) << 0);
   3.621 +	      p8++;
   3.622 +	    }
   3.623 +	    break;
   3.624 +	  case IMAGE_GREYSCALE:
   3.625 +	    for (x=tx; x < tx+ox; x++) {
   3.626 +	      *row++ = 0xFF000000
   3.627 +		| (((Uint32) (*p8)) << 16)
   3.628 +		| (((Uint32) (*p8)) << 8)
   3.629 +		| (((Uint32) (*p8++)) << 0);
   3.630 +	    }
   3.631 +	    break;	    
   3.632 +	  default:
   3.633 +	    fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type);
   3.634 +	    return 1;
   3.635 +	  }
   3.636 +	  break;
   3.637 +	}
   3.638 +      }
   3.639 +      tx += 64;
   3.640 +      if (tx >= level->width) {
   3.641 +	tx = 0;
   3.642 +	ty += 64;
   3.643 +      }
   3.644 +      if (ty >= level->height) {
   3.645 +	break;
   3.646 +      }
   3.647 +
   3.648 +      free_xcf_tile (tile);
   3.649 +    }
   3.650 +    free_xcf_level (level);
   3.651 +  }
   3.652 +
   3.653 +  free_xcf_hierarchy (hierarchy);
   3.654 +}
   3.655 +
   3.656 +SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src) {
   3.657 +  SDL_Surface *surface, *lays;
   3.658 +  xcf_header * head;
   3.659 +  xcf_layer  * layer;
   3.660 +  xcf_channel ** channel;
   3.661 +  int read_error, chnls, i, offsets;
   3.662 +  Uint32 offset, fp;
   3.663 +
   3.664 +  unsigned char * (* load_tile) (SDL_RWops *, Uint32, int, int, int);
   3.665 +
   3.666 +  /* Initialize the data we will clean up when we're done */
   3.667 +  surface = NULL;
   3.668 +  read_error = 0;
   3.669 +
   3.670 +  /* Check to make sure we have something to do */
   3.671 +  if ( ! src ) {
   3.672 +    goto done;
   3.673 +  }
   3.674 +
   3.675 +  head = read_xcf_header (src);
   3.676 +
   3.677 +  switch (head->compr) {
   3.678 +  case COMPR_NONE:
   3.679 +    load_tile = load_xcf_tile_none;
   3.680 +    break;
   3.681 +  case COMPR_RLE:
   3.682 +    load_tile = load_xcf_tile_rle;
   3.683 +    break;
   3.684 +  default:
   3.685 +    fprintf (stderr, "Unsupported Compression.\n");
   3.686 +    free_xcf_header (head);
   3.687 +    return NULL;
   3.688 +  }
   3.689 +
   3.690 +  /* Create the surface of the appropriate type */
   3.691 +  surface = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   3.692 +			     0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   3.693 +
   3.694 +  if ( surface == NULL ) {
   3.695 +    IMG_SetError("Out of memory");
   3.696 +    goto done;
   3.697 +  }
   3.698 +
   3.699 +  head->layer_file_offsets = NULL;
   3.700 +  offsets = 0;
   3.701 +
   3.702 +  while (offset = SDL_ReadBE32 (src)) {
   3.703 +    head->layer_file_offsets = (Uint32 *) realloc (head->layer_file_offsets, sizeof (Uint32) * (offsets+1));
   3.704 +    head->layer_file_offsets [offsets] = offset;
   3.705 +    offsets++;
   3.706 +  }
   3.707 +  fp = SDL_RWtell (src);
   3.708 + 
   3.709 +  lays = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   3.710 +			  0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   3.711 +
   3.712 +  if ( lays == NULL ) {
   3.713 +    IMG_SetError("Out of memory");
   3.714 +    goto done;
   3.715 +  }
   3.716 +
   3.717 +  // Blit layers backwards, because Gimp saves them highest first
   3.718 +  for (i = offsets; i > 0; i--) {
   3.719 +    SDL_Rect rs, rd;
   3.720 +    SDL_RWseek (src, head->layer_file_offsets [i-1], SEEK_SET);
   3.721 +
   3.722 +    layer = read_xcf_layer (src);
   3.723 +    do_layer_surface (lays, src, head, layer, load_tile);
   3.724 +    rs.x = 0;
   3.725 +    rs.y = 0;
   3.726 +    rs.w = layer->width;
   3.727 +    rs.h = layer->height;
   3.728 +    rd.x = layer->offset_x;
   3.729 +    rd.y = layer->offset_y;
   3.730 +    rd.w = layer->width;
   3.731 +    rd.h = layer->height;
   3.732 +    free_xcf_layer (layer);
   3.733 +
   3.734 +    SDL_BlitSurface (lays, &rs, surface, &rd);
   3.735 +  }
   3.736 +
   3.737 +  SDL_FreeSurface (lays);
   3.738 +
   3.739 +  SDL_RWseek (src, fp, SEEK_SET);
   3.740 +
   3.741 +  // read channels
   3.742 +  channel = NULL;
   3.743 +  chnls   = 0;
   3.744 +  while (offset = SDL_ReadBE32 (src)) {
   3.745 +    channel = (xcf_channel **) realloc (channel, sizeof (xcf_channel *) * (chnls+1));
   3.746 +    fp = SDL_RWtell (src);
   3.747 +    SDL_RWseek (src, offset, SEEK_SET);
   3.748 +    channel [chnls++] = (read_xcf_channel (src));
   3.749 +    SDL_RWseek (src, fp, SEEK_SET);    
   3.750 +  }
   3.751 +
   3.752 +  if (chnls) {
   3.753 +    SDL_Surface * chs;
   3.754 +
   3.755 +    chs = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32,
   3.756 +			   0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
   3.757 +
   3.758 +    if (chs == NULL) {
   3.759 +      IMG_SetError("Out of memory");
   3.760 +      goto done;
   3.761 +    }
   3.762 +    for (i = 0; i < chnls; i++) {
   3.763 +      //      printf ("CNLBLT %i\n", i);
   3.764 +      if (!channel [i]->selection) {
   3.765 +	create_channel_surface (chs, head->image_type, channel [i]->color, channel [i]->opacity);
   3.766 +	SDL_BlitSurface (chs, NULL, surface, NULL);
   3.767 +      }
   3.768 +      free_xcf_channel (channel [i]);
   3.769 +    }
   3.770 +
   3.771 +    SDL_FreeSurface (chs);
   3.772 +  }
   3.773 +
   3.774 + done:
   3.775 +  free_xcf_header (head);
   3.776 +  if ( read_error ) {
   3.777 +    SDL_FreeSurface(surface);
   3.778 +    IMG_SetError("Error reading XCF data");
   3.779 +    surface = NULL;
   3.780 +  }
   3.781 +
   3.782 +  return(surface);
   3.783 +}
   3.784 +
   3.785 +#else
   3.786 +
   3.787 +/* See if an image is contained in a data source */
   3.788 +int IMG_isXCF(SDL_RWops *src)
   3.789 +{
   3.790 +  return(0);
   3.791 +}
   3.792 +
   3.793 +/* Load a XCF type image from an SDL datasource */
   3.794 +SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
   3.795 +{
   3.796 +  return(NULL);
   3.797 +}
   3.798 +
   3.799 +#endif /* LOAD_XCF */
     4.1 --- a/Makefile.am	Wed Mar 07 17:00:59 2001 +0000
     4.2 +++ b/Makefile.am	Wed Mar 07 17:16:38 2001 +0000
     4.3 @@ -16,6 +16,7 @@
     4.4  	IMG_pnm.c		\
     4.5  	IMG_tga.c		\
     4.6  	IMG_tif.c		\
     4.7 +	IMG_xcf.c		\
     4.8  	IMG_xpm.c
     4.9  
    4.10  EXTRA_DIST =			\
     5.1 --- a/SDL_image.h	Wed Mar 07 17:00:59 2001 +0000
     5.2 +++ b/SDL_image.h	Wed Mar 07 17:16:38 2001 +0000
     5.3 @@ -58,6 +58,7 @@
     5.4  extern DECLSPEC int IMG_isBMP(SDL_RWops *src);
     5.5  extern DECLSPEC int IMG_isPNM(SDL_RWops *src);
     5.6  extern DECLSPEC int IMG_isXPM(SDL_RWops *src);
     5.7 +extern DECLSPEC int IMG_isXCF(SDL_RWops *src);
     5.8  extern DECLSPEC int IMG_isPCX(SDL_RWops *src);
     5.9  extern DECLSPEC int IMG_isGIF(SDL_RWops *src);
    5.10  extern DECLSPEC int IMG_isJPG(SDL_RWops *src);
    5.11 @@ -68,6 +69,7 @@
    5.12  extern DECLSPEC SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src);
    5.13  extern DECLSPEC SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src);
    5.14  extern DECLSPEC SDL_Surface *IMG_LoadXPM_RW(SDL_RWops *src);
    5.15 +extern DECLSPEC SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src);
    5.16  extern DECLSPEC SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src);
    5.17  extern DECLSPEC SDL_Surface *IMG_LoadGIF_RW(SDL_RWops *src);
    5.18  extern DECLSPEC SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src);
     6.1 --- a/configure.in	Wed Mar 07 17:00:59 2001 +0000
     6.2 +++ b/configure.in	Wed Mar 07 17:16:38 2001 +0000
     6.3 @@ -158,6 +158,12 @@
     6.4          AC_MSG_WARN([TIF image loading disabled])
     6.5      fi
     6.6  fi
     6.7 +AC_ARG_ENABLE(xcf,
     6.8 +[  --enable-xcf            support loading XCF images [default=no]],
     6.9 +              , enable_xcf=no)
    6.10 +if test x$enable_xcf = xyes; then
    6.11 +    CFLAGS="$CFLAGS -DLOAD_XCF"
    6.12 +fi
    6.13  AC_ARG_ENABLE(xpm,
    6.14  [  --enable-xpm            support loading XPM images [default=yes]],
    6.15                , enable_xpm=yes)