xcf.c 52.9 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>

#include <glib.h>
#include <gtk/gtk.h>

#include "floating_sel.h"
#include "gimage.h"
#include "gimage_mask.h"
#include "interface.h"
#include "plug_in.h"
#include "procedural_db.h"
#include "tile_swap.h"
#include "xcf.h"
#include "frac.h"

25 26 27 28
#include "drawable_pvt.h"
#include "layer_pvt.h"
#include "channel_pvt.h"
#include "tile_manager_pvt.h"
Elliot Lee's avatar
Elliot Lee committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/* #define SWAP_FROM_FILE */


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
} PropType;

typedef enum
{
  COMPRESS_NONE = 0,
  COMPRESS_RLE = 1,
  COMPRESS_ZLIB = 2,
  COMPRESS_FRACTAL = 3
} CompressionType;

typedef GImage* XcfLoader(XcfInfo *info);

static Argument* xcf_load_invoker (Argument  *args);
static Argument* xcf_save_invoker (Argument  *args);

static gint xcf_save_image         (XcfInfo     *info,
				    GImage      *gimage);
static void xcf_save_choose_format (XcfInfo     *info,
				    GImage      *gimage);
static void xcf_save_image_props   (XcfInfo     *info,
				    GImage      *gimage);
static void xcf_save_layer_props   (XcfInfo     *info,
				    GImage      *gimage,
				    Layer       *layer);
static void xcf_save_channel_props (XcfInfo     *info,
				    GImage      *gimage,
				    Channel     *channel);
static void xcf_save_prop          (XcfInfo     *info,
				    PropType     prop_type,
				    ...);
static void xcf_save_layer         (XcfInfo     *info,
				    GImage      *gimage,
				    Layer       *layer);
static void xcf_save_channel       (XcfInfo     *info,
				    GImage      *gimage,
				    Channel     *channel);
static void xcf_save_hierarchy     (XcfInfo     *info,
				    TileManager *tiles);
static void xcf_save_level         (XcfInfo     *info,
				    TileLevel   *level);
static void xcf_save_tile          (XcfInfo     *info,
				    Tile        *tile);
static void xcf_save_tile_rle      (XcfInfo     *info,
				    Tile        *tile);

static GImage*  xcf_load_image         (XcfInfo     *info);
static gint     xcf_load_image_props   (XcfInfo     *info,
					GImage      *gimage);
static gint     xcf_load_layer_props   (XcfInfo     *info,
					GImage      *gimage,
					Layer       *layer);
static gint     xcf_load_channel_props (XcfInfo     *info,
					GImage      *gimage,
					Channel     *channel);
static gint     xcf_load_prop          (XcfInfo     *info,
					PropType    *prop_type,
					guint32     *prop_size);
static Layer*   xcf_load_layer         (XcfInfo     *info,
					GImage      *gimage);
static Channel* xcf_load_channel       (XcfInfo     *info,
					GImage      *gimage);
115 116
static LayerMask* xcf_load_layer_mask  (XcfInfo     *info,
					GImage      *gimage);
Elliot Lee's avatar
Elliot Lee committed
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static gint     xcf_load_hierarchy     (XcfInfo     *info,
					TileManager *tiles);
static gint     xcf_load_level         (XcfInfo     *info,
					TileManager *tiles,
					TileLevel   *level,
					int          level_num);
static gint     xcf_load_tile          (XcfInfo     *info,
					Tile        *tile);
static gint     xcf_load_tile_rle      (XcfInfo     *info,
					Tile        *tile);

#ifdef SWAP_FROM_FILE
static int      xcf_swap_func          (int          fd,
					Tile        *tile,
					int          cmd,
					gpointer     user_data);
#endif


static void xcf_seek_pos (XcfInfo *info,
			  guint    pos);
static void xcf_seek_end (XcfInfo *info);

static guint xcf_read_int32   (FILE     *fp,
			       guint32  *data,
			       gint      count);
static guint xcf_read_int8    (FILE     *fp,
			       guint8   *data,
			       gint      count);
static guint xcf_read_string  (FILE     *fp,
			       gchar   **data,
			       gint      count);
static guint xcf_write_int32  (FILE     *fp,
			       guint32  *data,
			       gint      count);
static guint xcf_write_int8   (FILE     *fp,
			       guint8   *data,
			       gint      count);
static guint xcf_write_string (FILE     *fp,
			       gchar   **data,
			       gint      count);



static ProcArg xcf_load_args[] =
{
  { PDB_INT32,
    "dummy_param",
    "dummy parameter" },
  { PDB_STRING,
    "filename",
    "The name of the file to load" },
  { PDB_STRING,
    "raw_filename",
    "The name of the file to load" },
};

static ProcArg xcf_load_return_vals[] =
{
  { PDB_IMAGE,
    "image",
    "Output image" },
};

static PlugInProcDef xcf_plug_in_load_proc =
{
  "gimp_xcf_load",
  "<Load>/XCF",
  NULL,
  "xcf",
  "",
  "0,string,gimp\\040xcf\\040",
  NULL, /* ignored for load */
  0,    /* ignored for load */
  {
    "gimp_xcf_load",
    "loads file saved in the .xcf file format",
    "The xcf file format has been designed specifically for loading and saving tiled and layered images in the GIMP. This procedure will load the specified file.",
    "Spencer Kimball & Peter Mattis",
    "Spencer Kimball & Peter Mattis",
    "1995-1996",
    PDB_INTERNAL,
    3,
    xcf_load_args,
    1,
    xcf_load_return_vals,
    { { xcf_load_invoker } },
  },
  NULL, /* fill me in at runtime */
  NULL /* fill me in at runtime */
};

static ProcArg xcf_save_args[] =
{
  { PDB_INT32,
    "dummy_param",
    "dummy parameter" },
  { PDB_IMAGE,
    "image",
    "Input image" },
  { PDB_DRAWABLE,
    "drawable",
    "Active drawable of input image" },
  { PDB_STRING,
    "filename",
    "The name of the file to save the image in" },
  { PDB_STRING,
    "raw_filename",
    "The name of the file to load" },
};

static PlugInProcDef xcf_plug_in_save_proc =
{
  "gimp_xcf_save",
  "<Save>/XCF",
  NULL,
  "xcf",
  "",
  NULL,
  "RGB*, GRAY*, INDEXED*",
  0, /* fill me in at runtime */
  {
    "gimp_xcf_save",
    "saves file in the .xcf file format",
    "The xcf file format has been designed specifically for loading and saving tiled and layered images in the GIMP. This procedure will save the specified image in the xcf file format.",
    "Spencer Kimball & Peter Mattis",
    "Spencer Kimball & Peter Mattis",
    "1995-1996",
    PDB_INTERNAL,
    5,
    xcf_save_args,
    0,
    NULL,
    { { xcf_save_invoker } },
  },
  NULL, /* fill me in at runtime */
  NULL /* fill me in at runtime */
};

static XcfLoader* xcf_loaders[] =
{
  xcf_load_image,			/* version 0 */
  xcf_load_image			/* version 1 */
};

static int N_xcf_loaders=(sizeof(xcf_loaders)/sizeof(xcf_loaders[0]));

void
xcf_init ()
{
  /* So this is sort of a hack, but its better than it was before.  To do this
   * right there would be a file load-save handler type and the whole interface
   * would change but there isn't, and currently the plug-in structure contains
   * all the load-save info, so it makes sense to use that for the XCF load/save
   * handlers, even though they are internal.  The only thing it requires is
   * using a PlugInProcDef struct.  -josh */
  procedural_db_register (&xcf_plug_in_save_proc.db_info);
  procedural_db_register (&xcf_plug_in_load_proc.db_info);
  xcf_plug_in_save_proc.image_types_val = plug_in_image_types_parse (xcf_plug_in_save_proc.image_types);
  xcf_plug_in_load_proc.image_types_val = plug_in_image_types_parse (xcf_plug_in_load_proc.image_types);
  plug_in_add_internal (&xcf_plug_in_save_proc);
  plug_in_add_internal (&xcf_plug_in_load_proc);
}

static Argument*
xcf_load_invoker (Argument *args)
{
  XcfInfo info;
  Argument *return_args;
  GImage *gimage;
  char *filename;
  int success;
  char id[14];

  gimage = NULL;

  success = FALSE;

  filename = args[1].value.pdb_pointer;

297
  info.fp = fopen (filename, "rb");
Elliot Lee's avatar
Elliot Lee committed
298 299 300 301 302 303
  if (info.fp)
    {
      info.cp = 0;
      info.filename = filename;
      info.active_layer = 0;
      info.active_channel = 0;
304 305
      info.floating_sel_drawable = NULL;
      info.floating_sel = NULL;
Elliot Lee's avatar
Elliot Lee committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      info.floating_sel_offset = 0;
      info.swap_num = 0;
      info.ref_count = NULL;
      info.compression = COMPRESS_NONE;

      success = TRUE;
      info.cp += xcf_read_int8 (info.fp, (guint8*) id, 14);
      if (strncmp (id, "gimp xcf ", 9) != 0)
	success = FALSE;
      else if (strcmp (id+9, "file") == 0) 
	{
	  info.file_version = 0;
	} 
      else if (id[9] == 'v') 
	{
	  info.file_version = atoi(id + 10);
	}
      else 
	success = FALSE;
      
      if (success)
	{
	  if (info.file_version < N_xcf_loaders)
	    {
	      gimage = (*(xcf_loaders[info.file_version])) (&info);
331 332
	      if (!gimage)
		success = FALSE;
Elliot Lee's avatar
Elliot Lee committed
333 334 335
	    }
	  else 
	    {
336
	      g_message ("XCF error: unsupported XCF file version %d encountered", info.file_version);
Elliot Lee's avatar
Elliot Lee committed
337 338 339
	      success = FALSE;
	    }
	}
340
      fclose (info.fp);
Elliot Lee's avatar
Elliot Lee committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    }
  
  return_args = procedural_db_return_args (&xcf_plug_in_load_proc.db_info, success);

  if (success)
    return_args[1].value.pdb_int = gimage->ID;

  return return_args;
}

static Argument*
xcf_save_invoker (Argument *args)
{
  XcfInfo info;
  Argument *return_args;
  GImage *gimage;
  char *filename;
  int success;

  success = FALSE;

  gimage = gimage_get_ID (args[1].value.pdb_int);
  filename = args[3].value.pdb_pointer;

365
  info.fp = fopen (filename, "wb");
Elliot Lee's avatar
Elliot Lee committed
366 367 368 369 370 371
  if (info.fp)
    {
      info.cp = 0;
      info.filename = filename;
      info.active_layer = 0;
      info.active_channel = 0;
372 373
      info.floating_sel_drawable = NULL;
      info.floating_sel = NULL;
Elliot Lee's avatar
Elliot Lee committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
      info.floating_sel_offset = 0;
      info.swap_num = 0;
      info.ref_count = NULL;
      info.compression = COMPRESS_RLE;

      xcf_save_choose_format (&info, gimage);

      success = xcf_save_image (&info, gimage);
      fclose (info.fp);
    }
  else
    {
      g_warning ("open failed on %s: %s\n", filename, g_strerror(errno));
    }

  return_args = procedural_db_return_args (&xcf_plug_in_save_proc.db_info, success);

  return return_args;
}

static void
xcf_save_choose_format (XcfInfo *info,
			GImage  *gimage)
{
  int save_version = 0;			/* default to oldest */

  if (gimage->cmap) 
    save_version = 1;			/* need version 1 for colormaps */

  info->file_version = save_version;
}

static gint
xcf_save_image (XcfInfo *info,
		GImage  *gimage)
{
  Layer *layer;
  Layer *floating_layer;
  Channel *channel;
  guint32 saved_pos;
  guint32 offset;
  guint nlayers;
  guint nchannels;
417
  GSList *list;
Elliot Lee's avatar
Elliot Lee committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  int have_selection;
  int t1, t2, t3, t4;
  char version_tag[14];

  floating_layer = gimage_floating_sel (gimage);
  if (floating_layer)
    floating_sel_relax (floating_layer, FALSE);

  /* write out the tag information for the image */
  if (info->file_version > 0) 
    {
      sprintf (version_tag, "gimp xcf v%03d", info->file_version);
    } 
  else 
    {
      strcpy (version_tag, "gimp xcf file");
    }
  info->cp += xcf_write_int8 (info->fp, (guint8*) version_tag, 14);

  /* write out the width, height and image type information for the image */
  info->cp += xcf_write_int32 (info->fp, (guint32*) &gimage->width, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &gimage->height, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &gimage->base_type, 1);

  /* determine the number of layers and channels in the image */
443 444
  nlayers = (guint) g_slist_length (gimage->layers);
  nchannels = (guint) g_slist_length (gimage->channels);
Elliot Lee's avatar
Elliot Lee committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

  /* check and see if we have to save out the selection */
  have_selection = gimage_mask_bounds (gimage, &t1, &t2, &t3, &t4);
  if (have_selection)
    nchannels += 1;

  /* write the property information for the image.
   */
  xcf_save_image_props (info, gimage);

  /* save the current file position as it is the start of where
   *  we place the layer offset information.
   */
  saved_pos = info->cp;

  /* Initialize the fractal compression saving routines
   */
  if (info->compression == COMPRESS_FRACTAL)
    xcf_save_compress_frac_init (2, 2);

  /* seek to after the offset lists */
  xcf_seek_pos (info, info->cp + (nlayers + nchannels + 2) * 4);

  list = gimage->layers;
  while (list)
    {
      layer = list->data;
      list = list->next;

      /* save the start offset of where we are writing
       *  out the next layer.
       */
      offset = info->cp;

      /* write out the layer. */
      xcf_save_layer (info, gimage, layer);

      /* seek back to where we are to write out the next
       *  layer offset and write it out.
       */
      xcf_seek_pos (info, saved_pos);
      info->cp += xcf_write_int32 (info->fp, &offset, 1);

      /* increment the location we are to write out the
       *  next offset.
       */
      saved_pos = info->cp;

      /* seek to the end of the file which is where
       *  we will write out the next layer.
       */
      xcf_seek_end (info);
    }

  /* write out a '0' offset position to indicate the end
   *  of the layer offsets.
   */
  offset = 0;
  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
  saved_pos = info->cp;
  xcf_seek_end (info);


  list = gimage->channels;
  while (list || have_selection)
    {
      if (list)
	{
	  channel = list->data;
	  list = list->next;
	}
      else
	{
	  channel = gimage->selection_mask;
	  have_selection = FALSE;
	}

      /* save the start offset of where we are writing
       *  out the next channel.
       */
      offset = info->cp;

      /* write out the layer. */
      xcf_save_channel (info, gimage, channel);

      /* seek back to where we are to write out the next
       *  channel offset and write it out.
       */
      xcf_seek_pos (info, saved_pos);
      info->cp += xcf_write_int32 (info->fp, &offset, 1);

      /* increment the location we are to write out the
       *  next offset.
       */
      saved_pos = info->cp;

      /* seek to the end of the file which is where
       *  we will write out the next channel.
       */
      xcf_seek_end (info);
    }

  /* write out a '0' offset position to indicate the end
   *  of the channel offsets.
   */
  offset = 0;
  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
  saved_pos = info->cp;

  if (floating_layer)
    floating_sel_rigor (floating_layer, FALSE);

  return !ferror(info->fp);
}

static void
xcf_save_image_props (XcfInfo *info,
		      GImage  *gimage)
{
  /* check and see if we should save the colormap property */
  if (gimage->cmap)
    xcf_save_prop (info, PROP_COLORMAP, gimage->num_cols, gimage->cmap);

  if (info->compression != COMPRESS_NONE)
    xcf_save_prop (info, PROP_COMPRESSION, info->compression);

  if (gimage->guides)
    xcf_save_prop (info, PROP_GUIDES, gimage->guides);

  xcf_save_prop (info, PROP_END);
}

static void
xcf_save_layer_props (XcfInfo *info,
		      GImage  *gimage,
		      Layer   *layer)
{
584
  if (layer == gimage->active_layer)
Elliot Lee's avatar
Elliot Lee committed
585 586 587 588 589 590 591 592 593
    xcf_save_prop (info, PROP_ACTIVE_LAYER);

  if (layer == gimage_floating_sel (gimage))
    {
      info->floating_sel_drawable = layer->fs.drawable;
      xcf_save_prop (info, PROP_FLOATING_SELECTION);
    }

  xcf_save_prop (info, PROP_OPACITY, layer->opacity);
594
  xcf_save_prop (info, PROP_VISIBLE, GIMP_DRAWABLE(layer)->visible);
Elliot Lee's avatar
Elliot Lee committed
595 596 597 598 599
  xcf_save_prop (info, PROP_LINKED, layer->linked);
  xcf_save_prop (info, PROP_PRESERVE_TRANSPARENCY, layer->preserve_trans);
  xcf_save_prop (info, PROP_APPLY_MASK, layer->apply_mask);
  xcf_save_prop (info, PROP_EDIT_MASK, layer->edit_mask);
  xcf_save_prop (info, PROP_SHOW_MASK, layer->show_mask);
600
  xcf_save_prop (info, PROP_OFFSETS, GIMP_DRAWABLE(layer)->offset_x, GIMP_DRAWABLE(layer)->offset_y);
Elliot Lee's avatar
Elliot Lee committed
601 602 603 604 605 606 607 608 609 610
  xcf_save_prop (info, PROP_MODE, layer->mode);

  xcf_save_prop (info, PROP_END);
}

static void
xcf_save_channel_props (XcfInfo *info,
			GImage  *gimage,
			Channel *channel)
{
611
  if (channel == gimage->active_channel)
Elliot Lee's avatar
Elliot Lee committed
612 613 614 615 616 617
    xcf_save_prop (info, PROP_ACTIVE_CHANNEL);

  if (channel == gimage->selection_mask)
    xcf_save_prop (info, PROP_SELECTION);

  xcf_save_prop (info, PROP_OPACITY, channel->opacity);
618
  xcf_save_prop (info, PROP_VISIBLE, GIMP_DRAWABLE(channel)->visible);
Elliot Lee's avatar
Elliot Lee committed
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 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
  xcf_save_prop (info, PROP_SHOW_MASKED, channel->show_masked);
  xcf_save_prop (info, PROP_COLOR, channel->col);

  xcf_save_prop (info, PROP_END);
}

static void
xcf_save_prop (XcfInfo  *info,
	       PropType  prop_type,
	       ...)
{
  guint32 size;
  va_list args;

  va_start (args, prop_type);

  switch (prop_type)
    {
    case PROP_END:
      size = 0;

      info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
      info->cp += xcf_write_int32 (info->fp, &size, 1);
      break;
    case PROP_COLORMAP:
      {
	guint32 ncolors;
	guchar *colors;

	ncolors = va_arg (args, guint32);
	colors = va_arg (args, guchar*);
	size = 4 + ncolors;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &ncolors, 1);
	info->cp += xcf_write_int8 (info->fp, colors, ncolors * 3);
      }
      break;
    case PROP_ACTIVE_LAYER:
    case PROP_ACTIVE_CHANNEL:
    case PROP_SELECTION:
      size = 0;

      info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
      info->cp += xcf_write_int32 (info->fp, &size, 1);
      break;
    case PROP_FLOATING_SELECTION:
      {
	guint32 dummy;

	dummy = 0;
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->floating_sel_offset = info->cp;
	info->cp += xcf_write_int32 (info->fp, &dummy, 1);
      }
      break;
    case PROP_OPACITY:
      {
	gint32 opacity;

	opacity = va_arg (args, gint32);

	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, (guint32*) &opacity, 1);
      }
      break;
    case PROP_MODE:
      {
	gint32 mode;

	mode = va_arg (args, gint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, (guint32*) &mode, 1);
      }
      break;
    case PROP_VISIBLE:
      {
	guint32 visible;

	visible = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &visible, 1);
      }
      break;
    case PROP_LINKED:
      {
	guint32 linked;

	linked = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &linked, 1);
      }
      break;
    case PROP_PRESERVE_TRANSPARENCY:
      {
	guint32 preserve_trans;

	preserve_trans = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &preserve_trans, 1);
      }
      break;
    case PROP_APPLY_MASK:
      {
	guint32 apply_mask;

	apply_mask = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &apply_mask, 1);
      }
      break;
    case PROP_EDIT_MASK:
      {
	guint32 edit_mask;

	edit_mask = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &edit_mask, 1);
      }
      break;
    case PROP_SHOW_MASK:
      {
	guint32 show_mask;

	show_mask = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &show_mask, 1);
      }
      break;
    case PROP_SHOW_MASKED:
      {
	guint32 show_masked;

	show_masked = va_arg (args, guint32);
	size = 4;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, &show_masked, 1);
      }
      break;
    case PROP_OFFSETS:
      {
	gint32 offsets[2];

	offsets[0] = va_arg (args, gint32);
	offsets[1] = va_arg (args, gint32);
	size = 8;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int32 (info->fp, (guint32*) offsets, 2);
      }
      break;
    case PROP_COLOR:
      {
	guchar *color;

	color = va_arg (args, guchar*);
	size = 3;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int8 (info->fp, color, 3);
      }
      break;
    case PROP_COMPRESSION:
      {
	guint8 compression;

	compression = (guint8) va_arg (args, guint32);
	size = 1;

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);
	info->cp += xcf_write_int8 (info->fp, &compression, 1);
      }
      break;
    case PROP_GUIDES:
      {
	GList *guides;
	Guide *guide;
	gint32 position;
	gint8 orientation;
	int nguides;

	guides = va_arg (args, GList*);
834
	nguides = g_list_length (guides);
Elliot Lee's avatar
Elliot Lee committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

	size = nguides * (4 + 1);

	info->cp += xcf_write_int32 (info->fp, (guint32*) &prop_type, 1);
	info->cp += xcf_write_int32 (info->fp, &size, 1);

	while (guides)
	  {
	    guide = guides->data;
	    guides = guides->next;

	    position = guide->position;
	    orientation = guide->orientation;

	    info->cp += xcf_write_int32 (info->fp, (guint32*) &position, 1);
	    info->cp += xcf_write_int8 (info->fp, (guint8*) &orientation, 1);
	  }
      }
      break;
    }

  va_end (args);
}

static void
xcf_save_layer (XcfInfo *info,
		GImage  *gimage,
		Layer   *layer)
{
  guint32 saved_pos;
  guint32 offset;

  /* check and see if this is the drawable that the floating
   *  selection is attached to.
   */
870
  if (GIMP_DRAWABLE(layer) == info->floating_sel_drawable)
Elliot Lee's avatar
Elliot Lee committed
871 872 873 874 875 876 877 878
    {
      saved_pos = info->cp;
      xcf_seek_pos (info, info->floating_sel_offset);
      info->cp += xcf_write_int32 (info->fp, &saved_pos, 1);
      xcf_seek_pos (info, saved_pos);
    }

  /* write out the width, height and image type information for the layer */
879 880 881
  info->cp += xcf_write_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->width, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->height, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->type, 1);
Elliot Lee's avatar
Elliot Lee committed
882 883

  if (info->compression == COMPRESS_FRACTAL)
884
    xcf_compress_frac_info (GIMP_DRAWABLE(layer)->type);
Elliot Lee's avatar
Elliot Lee committed
885 886

  /* write out the layers name */
887
  info->cp += xcf_write_string (info->fp, &GIMP_DRAWABLE(layer)->name, 1);
Elliot Lee's avatar
Elliot Lee committed
888 889 890 891 892 893 894 895 896 897 898 899 900

  /* write out the layer properties */
  xcf_save_layer_props (info, gimage, layer);

  /* save the current position which is where the hierarchy offset
   *  will be stored.
   */
  saved_pos = info->cp;

  /* write out the layer tile hierarchy */
  xcf_seek_pos (info, info->cp + 8);
  offset = info->cp;

901
  xcf_save_hierarchy (info, GIMP_DRAWABLE(layer)->tiles);
Elliot Lee's avatar
Elliot Lee committed
902 903 904 905 906 907 908 909 910 911 912

  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
  saved_pos = info->cp;

  /* write out the layer mask */
  if (layer->mask)
    {
      xcf_seek_end (info);
      offset = info->cp;

913
      xcf_save_channel (info, gimage, GIMP_CHANNEL(layer->mask));
Elliot Lee's avatar
Elliot Lee committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
    }
  else
    offset = 0;

  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
}

static void
xcf_save_channel (XcfInfo *info,
		  GImage  *gimage,
		  Channel *channel)
{
  guint32 saved_pos;
  guint32 offset;

  /* check and see if this is the drawable that the floating
   *  selection is attached to.
   */
933
  if (GIMP_DRAWABLE(channel) == info->floating_sel_drawable)
Elliot Lee's avatar
Elliot Lee committed
934 935 936 937 938 939 940 941
    {
      saved_pos = info->cp;
      xcf_seek_pos (info, info->floating_sel_offset);
      info->cp += xcf_write_int32 (info->fp, (guint32*) &info->cp, 1);
      xcf_seek_pos (info, saved_pos);
    }

  /* write out the width and height information for the channel */
942 943
  info->cp += xcf_write_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(channel)->width, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(channel)->height, 1);
Elliot Lee's avatar
Elliot Lee committed
944 945

  /* write out the channels name */
946
  info->cp += xcf_write_string (info->fp, &GIMP_DRAWABLE(channel)->name, 1);
Elliot Lee's avatar
Elliot Lee committed
947 948 949 950 951 952 953 954 955 956 957 958 959

  /* write out the channel properties */
  xcf_save_channel_props (info, gimage, channel);

  /* save the current position which is where the hierarchy offset
   *  will be stored.
   */
  saved_pos = info->cp;

  /* write out the channel tile hierarchy */
  xcf_seek_pos (info, info->cp + 4);
  offset = info->cp;

960
  xcf_save_hierarchy (info, GIMP_DRAWABLE(channel)->tiles);
Elliot Lee's avatar
Elliot Lee committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
  saved_pos = info->cp;
}

static void
xcf_save_hierarchy (XcfInfo     *info,
		    TileManager *tiles)
{
  guint32 saved_pos;
  guint32 offset;
  int i;

  info->cp += xcf_write_int32 (info->fp, (guint32*) &tiles->levels[0].width, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &tiles->levels[0].height, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &tiles->levels[0].bpp, 1);

  saved_pos = info->cp;

  xcf_seek_pos (info, info->cp + (tiles->nlevels + 1) * 4);

  for (i = 0; i < tiles->nlevels; i++)
    {
      /* save the start offset of where we are writing
       *  out the next level.
       */
      offset = info->cp;

      /* write out the level. */
      xcf_save_level (info, &tiles->levels[i]);

      /* seek back to where we are to write out the next
       *  level offset and write it out.
       */
      xcf_seek_pos (info, saved_pos);
      info->cp += xcf_write_int32 (info->fp, &offset, 1);

      /* increment the location we are to write out the
       *  next offset.
       */
      saved_pos = info->cp;

      /* seek to the end of the file which is where
       *  we will write out the next level.
       */
      xcf_seek_end (info);
    }

  /* write out a '0' offset position to indicate the end
   *  of the level offsets.
   */
  offset = 0;
  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
}

static void
xcf_save_level (XcfInfo   *info,
		TileLevel *level)
{
  guint32 saved_pos;
  guint32 offset;
  guint ntiles;
  int i;

  info->cp += xcf_write_int32 (info->fp, (guint32*) &level->width, 1);
  info->cp += xcf_write_int32 (info->fp, (guint32*) &level->height, 1);

  saved_pos = info->cp;

  if (level->tiles)
    {
      ntiles = level->ntile_rows * level->ntile_cols;
      xcf_seek_pos (info, info->cp + (ntiles + 1) * 4);

      for (i = 0; i < ntiles; i++)
	{
	  /* save the start offset of where we are writing
	   *  out the next tile.
	   */
	  offset = info->cp;

	  /* write out the tile. */
	  switch (info->compression)
	    {
	    case COMPRESS_NONE:
	      xcf_save_tile (info, &level->tiles[i]);
	      break;
	    case COMPRESS_RLE:
	      xcf_save_tile_rle (info, &level->tiles[i]);
	      break;
	    case COMPRESS_ZLIB:
	      g_error ("xcf: zlib compression unimplemented");
	      break;
	    case COMPRESS_FRACTAL:
	      xcf_save_frac_compressed_tile (info, &level->tiles[i]);
	      break;
	    }

	  /* seek back to where we are to write out the next
	   *  tile offset and write it out.
	   */
	  xcf_seek_pos (info, saved_pos);
	  info->cp += xcf_write_int32 (info->fp, &offset, 1);

	  /* increment the location we are to write out the
	   *  next offset.
	   */
	  saved_pos = info->cp;

	  /* seek to the end of the file which is where
	   *  we will write out the next tile.
	   */
	  xcf_seek_end (info);
	}
    }

  /* write out a '0' offset position to indicate the end
   *  of the level offsets.
   */
  offset = 0;
  xcf_seek_pos (info, saved_pos);
  info->cp += xcf_write_int32 (info->fp, &offset, 1);
}

static void
xcf_save_tile (XcfInfo *info,
	       Tile    *tile)
{
  tile_ref (tile);
  info->cp += xcf_write_int8 (info->fp, tile->data, tile_size (tile));
  tile_unref (tile, FALSE);
}

static void
xcf_save_tile_rle (XcfInfo *info,
		   Tile    *tile)
{
  guchar *data, *t;
  guchar buffer[1024];
  unsigned int last;
  int state;
  int length;
  int count;
  int size;
  int bpp;
  int i, j, k;

  tile_ref (tile);

  bpp = tile->bpp;

  for (i = 0; i < bpp; i++)
    {
      data = tile->data + i;

      state = 0;
      length = 0;
      count = 0;
      size = tile->ewidth * tile->eheight;
      last = -1;

      while (size > 0)
	{
	  switch (state)
	    {
	    case 0:
	      /* in state 0 we try to find a long sequence of
	       *  matching values.
	       */
	      if ((length == 32768) ||
1133 1134
		  ((size - length) <= 0) ||
		  ((length > 1) && (last != *data)))
Elliot Lee's avatar
Elliot Lee committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
		{
		  count += length;
		  if (length >= 128)
		    {
		      buffer[0] = 127;
                      buffer[1] = (length >> 8);
                      buffer[2] = length & 0x00FF;
		      buffer[3] = last;
		      info->cp += xcf_write_int8 (info->fp, buffer, 4);
		    }
		  else
		    {
		      buffer[0] = length - 1;
		      buffer[1] = last;
		      info->cp += xcf_write_int8 (info->fp, buffer, 2);
		    }
		  size -= length;
		  length = 0;
		}
	      else if ((length == 1) && (last != *data))
		state = 1;
	      break;
	    case 1:
	      /* in state 1 we try and find a long sequence of
	       *  non-matching values.
	       */
	      if ((length == 32768) ||
1162 1163
		  ((size - length) == 0) ||
		  ((length > 0) && (last == *data)))
Elliot Lee's avatar
Elliot Lee committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
		{
		  count += length;
		  state = 0;

		  if (length >= 128)
		    {
		      buffer[0] = 255 - 127;
                      buffer[1] = (length >> 8);
                      buffer[2] = length & 0x00FF;
		      k = 3;
		    }
		  else
		    {
		      buffer[0] = 255 - (length - 1);
		      k = 1;
		    }

		  t = data - length * bpp;
		  for (j = 0; j < length; j++)
		    {
		      buffer[k++] = *t;
		      t += bpp;

		      if (k >= 1024)
			{
			  info->cp += xcf_write_int8 (info->fp, buffer, 1024);
			  k = 0;
			}
		    }

		  if (k > 0)
		    info->cp += xcf_write_int8 (info->fp, buffer, k);
		  size -= length;
		  length = 0;
		}
	      break;
	    }

1202 1203 1204 1205 1206
	  if (size > 0) {
	    length += 1;
	    last = *data;
	    data += bpp;
	  }
Elliot Lee's avatar
Elliot Lee committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	}

      if (count != (tile->ewidth * tile->eheight))
	g_print ("xcf: uh oh! xcf rle tile saving error: %d\n", count);
    }

  tile_unref (tile, FALSE);
}


static GImage*
xcf_load_image (XcfInfo *info)
{
  GImage *gimage;
  Layer *layer;
  Channel *channel;
  guint32 saved_pos;
  guint32 offset;
  int width;
  int height;
  int image_type;

  /* read in the image width, height and type */
  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &image_type, 1);

  /* create a new gimage */
  gimage = gimage_new (width, height, image_type);
  if (!gimage)
    return NULL;

  /* read the image properties */
  if (!xcf_load_image_props (info, gimage))
    goto error;

  if (info->compression == COMPRESS_FRACTAL)
    xcf_load_compress_frac_init (1, 2);

  while (1)
    {
      /* read in the offset of the next layer */
      info->cp += xcf_read_int32 (info->fp, &offset, 1);

      /* if the offset is 0 then we are at the end
       *  of the layer list.
       */
      if (offset == 0)
	break;

      /* save the current position as it is where the
       *  next layer offset is stored.
       */
      saved_pos = info->cp;

      /* seek to the layer offset */
      xcf_seek_pos (info, offset);

      /* read in the layer */
      layer = xcf_load_layer (info, gimage);
      if (!layer)
	goto error;

      if (info->compression == COMPRESS_FRACTAL)
1271
	xcf_compress_frac_info (GIMP_DRAWABLE(layer)->type);
Elliot Lee's avatar
Elliot Lee committed
1272 1273

      /* add the layer to the image if its not the floating selection */
1274
      if (layer != info->floating_sel)
1275
	gimage_add_layer (gimage, layer, g_slist_length (gimage->layers));
Elliot Lee's avatar
Elliot Lee committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
      xcf_seek_pos (info, saved_pos);
    }

  while (1)
    {
      /* read in the offset of the next channel */
      info->cp += xcf_read_int32 (info->fp, &offset, 1);

      /* if the offset is 0 then we are at the end
       *  of the channel list.
       */
      if (offset == 0)
	break;

      /* save the current position as it is where the
       *  next channel offset is stored.
       */
      saved_pos = info->cp;

      /* seek to the channel offset */
      xcf_seek_pos (info, offset);

      /* read in the layer */
      channel = xcf_load_channel (info, gimage);
      if (!channel)
	goto error;

      /* add the channel to the image if its not the selection */
      if (channel != gimage->selection_mask)
	gimage_add_channel (gimage, channel, -1);

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
      xcf_seek_pos (info, saved_pos);
    }

  if (info->active_layer)
    gimage_set_active_layer (gimage, info->active_layer);

  if (info->active_channel)
    gimage_set_active_channel (gimage, info->active_channel);

  gimage_set_filename (gimage, info->filename);

  return gimage;

error:
  gimage_delete (gimage);
  return NULL;
}

static gint
xcf_load_image_props (XcfInfo *info,
		      GImage  *gimage)
{
  PropType prop_type;
  guint32 prop_size;

  while (1)
    {
      if (!xcf_load_prop (info, &prop_type, &prop_size))
	return FALSE;

      switch (prop_type)
	{
	case PROP_END:
	  return TRUE;
	case PROP_COLORMAP:
	  if (info->file_version == 0) 
	    {
	      int i;
1352 1353 1354
	      g_message ("XCF warning: version 0 of XCF file format\n"
			 "did not save indexed colormaps correctly.\n"
			 "Substituting grayscale map.");
Elliot Lee's avatar
Elliot Lee committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	      info->cp += xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
	      gimage->cmap = g_new (guchar, gimage->num_cols*3);
	      xcf_seek_pos (info, info->cp + gimage->num_cols);
	      for (i = 0; i<gimage->num_cols; i++) 
		{
		  gimage->cmap[i*3+0] = i;
		  gimage->cmap[i*3+1] = i;
		  gimage->cmap[i*3+2] = i;
		}
	    }
	  else 
	    {
	      info->cp += xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
	      gimage->cmap = g_new (guchar, gimage->num_cols*3);
	      info->cp += xcf_read_int8 (info->fp, (guint8*) gimage->cmap, gimage->num_cols*3);
	    }
	  break;
	case PROP_COMPRESSION:
	  {
	    guint8 compression;

	    info->cp += xcf_read_int8 (info->fp, (guint8*) &compression, 1);

	    if ((compression != COMPRESS_NONE) &&
		(compression != COMPRESS_RLE) &&
		(compression != COMPRESS_ZLIB) &&
		(compression != COMPRESS_FRACTAL))
	      {
		g_warning ("unknown compression type: %d", (int) compression);
		return FALSE;
	      }

	    info->compression = compression;
	  }
	  break;
	case PROP_GUIDES:
	  {
	    Guide *guide;
	    gint32 position;
	    gint8 orientation;
	    int i, nguides;

	    nguides = prop_size / (4 + 1);
	    for (i = 0; i < nguides; i++)
	      {
		info->cp += xcf_read_int32 (info->fp, (guint32*) &position, 1);
		info->cp += xcf_read_int8 (info->fp, (guint8*) &orientation, 1);

		guide = g_new (Guide, 1);
		guide->position = position;
		guide->orientation = orientation;

		gimage->guides = g_list_prepend (gimage->guides, guide);
	      }

	    gimage->guides = g_list_reverse (gimage->guides);
	  }
	  break;
	default:
	  g_warning ("unexpected/unknown image property: %d (skipping)", prop_type);

	  {
	    guint8 buf[16];
	    guint amount;

	    while (prop_size > 0)
	      {
		amount = MIN (16, prop_size);
		info->cp += xcf_read_int8 (info->fp, buf, amount);
		prop_size -= MIN (16, amount);
	      }
	  }
	  break;
	}
    }

  return FALSE;
}

static gint
xcf_load_layer_props (XcfInfo *info,
		      GImage  *gimage,
		      Layer   *layer)
{
  PropType prop_type;
  guint32 prop_size;

  while (1)
    {
      if (!xcf_load_prop (info, &prop_type, &prop_size))
	return FALSE;

      switch (prop_type)
	{
	case PROP_END:
	  return TRUE;
	case PROP_ACTIVE_LAYER:
1452
	  info->active_layer = layer;
Elliot Lee's avatar
Elliot Lee committed
1453 1454
	  break;
	case PROP_FLOATING_SELECTION:
1455
	  info->floating_sel = layer;
Elliot Lee's avatar
Elliot Lee committed
1456 1457 1458 1459 1460 1461
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &info->floating_sel_offset, 1);
	  break;
	case PROP_OPACITY:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->opacity, 1);
	  break;
	case PROP_VISIBLE:
1462
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->visible, 1);
Elliot Lee's avatar
Elliot Lee committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
	  break;
	case PROP_LINKED:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->linked, 1);
	  break;
	case PROP_PRESERVE_TRANSPARENCY:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->preserve_trans, 1);
	  break;
	case PROP_APPLY_MASK:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->apply_mask, 1);
	  break;
	case PROP_EDIT_MASK:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->edit_mask, 1);
	  break;
	case PROP_SHOW_MASK:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->show_mask, 1);
	  break;
	case PROP_OFFSETS:
1480 1481
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->offset_x, 1);
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(layer)->offset_y, 1);
Elliot Lee's avatar
Elliot Lee committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
	  break;
	case PROP_MODE:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &layer->mode, 1);
	  break;
	default:
	  g_warning ("unexpected/unknown layer property: %d (skipping)", prop_type);

	  {
	    guint8 buf[16];
	    guint amount;

	    while (prop_size > 0)
	      {
		amount = MIN (16, prop_size);
		info->cp += xcf_read_int8 (info->fp, buf, amount);
		prop_size -= MIN (16, amount);
	      }
	  }
	  break;
	}
    }

  return FALSE;
}

static gint
xcf_load_channel_props (XcfInfo *info,
			GImage  *gimage,
			Channel *channel)
{
  PropType prop_type;
  guint32 prop_size;

  while (1)
    {
      if (!xcf_load_prop (info, &prop_type, &prop_size))
	return FALSE;

      switch (prop_type)
	{
	case PROP_END:
	  return TRUE;
	case PROP_ACTIVE_CHANNEL:
1525
	  info->active_channel = channel;
Elliot Lee's avatar
Elliot Lee committed
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	  break;
	case PROP_SELECTION:
	  channel_delete (gimage->selection_mask);
	  gimage->selection_mask = channel;
	  channel->boundary_known = FALSE;
	  channel->bounds_known = FALSE;
	  break;
	case PROP_OPACITY:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &channel->opacity, 1);
	  break;
	case PROP_VISIBLE:
1537
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &GIMP_DRAWABLE(channel)->visible, 1);
Elliot Lee's avatar
Elliot Lee committed
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	  break;
	case PROP_SHOW_MASKED:
	  info->cp += xcf_read_int32 (info->fp, (guint32*) &channel->show_masked, 1);
	  break;
	case PROP_COLOR:
	  info->cp += xcf_read_int8 (info->fp, (guint8*) channel->col, 3);
	  break;
	default:
	  g_warning ("unexpected/unknown channel property: %d (skipping)", prop_type);

	  {
	    guint8 buf[16];
	    guint amount;

	    while (prop_size > 0)
	      {
		amount = MIN (16, prop_size);
		info->cp += xcf_read_int8 (info->fp, buf, amount);
		prop_size -= MIN (16, amount);
	      }
	  }
	  break;
	}
    }

  return FALSE;
}

static gint
xcf_load_prop (XcfInfo  *info,
	       PropType *prop_type,
	       guint32  *prop_size)
{
  info->cp += xcf_read_int32 (info->fp, (guint32*) prop_type, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) prop_size, 1);
  return TRUE;
}

static Layer*
xcf_load_layer (XcfInfo *info,
		GImage  *gimage)
{
  Layer *layer;
1581
  LayerMask *layer_mask;
Elliot Lee's avatar
Elliot Lee committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
  guint32 hierarchy_offset;
  guint32 layer_mask_offset;
  int apply_mask;
  int edit_mask;
  int show_mask;
  int width;
  int height;
  int type;
  int add_floating_sel;
  char *name;

  /* check and see if this is the drawable the floating selection
   *  is attached to. if it is then we'll do the attachment at
   *  the end of this function.
   */
  add_floating_sel = (info->cp == info->floating_sel_offset);

  /* read in the layer width, height, type and name */
  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &type, 1);
  info->cp += xcf_read_string (info->fp, &name, 1);

  /* create a new layer */
  layer = layer_new (gimage->ID, width, height, type, name, 255, NORMAL_MODE);
  if (!layer)
    {
      g_free (name);
      return NULL;
    }

  /* read in the layer properties */
  if (!xcf_load_layer_props (info, gimage, layer))
    goto error;

  if (info->compression == COMPRESS_FRACTAL)
1618
    xcf_compress_frac_info (GIMP_DRAWABLE(layer)->type);
Elliot Lee's avatar
Elliot Lee committed
1619 1620 1621 1622 1623 1624 1625

  /* read the hierarchy and layer mask offsets */
  info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);
  info->cp += xcf_read_int32 (info->fp, &layer_mask_offset, 1);

  /* read in the hierarchy */
  xcf_seek_pos (info, hierarchy_offset);
1626
  if (!xcf_load_hierarchy (info, GIMP_DRAWABLE(layer)->tiles))
Elliot Lee's avatar
Elliot Lee committed
1627 1628 1629 1630 1631 1632 1633
    goto error;

  /* read in the layer mask */
  if (layer_mask_offset != 0)
    {
      xcf_seek_pos (info, layer_mask_offset);

1634
      layer_mask = xcf_load_layer_mask (info, gimage);
Elliot Lee's avatar
Elliot Lee committed
1635 1636 1637
      if (!layer_mask)
	goto error;

1638 1639 1640 1641
      /* set the offsets of the layer_mask */
      GIMP_DRAWABLE(layer_mask)->offset_x = GIMP_DRAWABLE(layer)->offset_x;
      GIMP_DRAWABLE(layer_mask)->offset_y = GIMP_DRAWABLE(layer)->offset_y;

Elliot Lee's avatar
Elliot Lee committed
1642 1643 1644 1645
      apply_mask = layer->apply_mask;
      edit_mask = layer->edit_mask;
      show_mask = layer->show_mask;

1646
      layer_add_mask (layer, layer_mask);
Elliot Lee's avatar
Elliot Lee committed
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657

      layer->apply_mask = apply_mask;
      layer->edit_mask = edit_mask;
      layer->show_mask = show_mask;
    }

  /* attach the floating selection... */
  if (add_floating_sel)
    {
      Layer *floating_sel;

1658 1659
      floating_sel = info->floating_sel;
      floating_sel_attach (floating_sel, GIMP_DRAWABLE(layer));
Elliot Lee's avatar
Elliot Lee committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    }

  return layer;

error:
  layer_delete (layer);
  return NULL;
}

static Channel*
xcf_load_channel (XcfInfo *info,
		  GImage  *gimage)
{
  Channel *channel;
  guint32 hierarchy_offset;
  int width;
  int height;
  int add_floating_sel;
  char *name;
  guchar color[3] = { 0, 0, 0 };

  /* check and see if this is the drawable the floating selection
   *  is attached to. if it is then we'll do the attachment at
   *  the end of this function.
   */
  add_floating_sel = (info->cp == info->floating_sel_offset);

  /* read in the layer width, height and name */
  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);
  info->cp += xcf_read_string (info->fp, &name, 1);

  /* create a new channel */
  channel = channel_new (gimage->ID, width, height, name, 255, color);
  if (!channel)
    {
      g_free (name);
      return NULL;
    }

  /* read in the channel properties */
  if (!xcf_load_channel_props (info, gimage, channel))
    goto error;

  /* read the hierarchy and layer mask offsets */
  info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);

  /* read in the hierarchy */
  xcf_seek_pos (info, hierarchy_offset);
1709
  if (!xcf_load_hierarchy (info, GIMP_DRAWABLE(channel)->tiles))
Elliot Lee's avatar
Elliot Lee committed
1710 1711 1712 1713 1714 1715 1716
    goto error;

  /* attach the floating selection... */
  if (add_floating_sel)
    {
      Layer *floating_sel;

1717 1718
      floating_sel = info->floating_sel;
      floating_sel_attach (floating_sel, GIMP_DRAWABLE(channel));
Elliot Lee's avatar
Elliot Lee committed
1719 1720 1721 1722 1723 1724 1725 1726 1727
    }

  return channel;

error:
  channel_delete (channel);
  return NULL;
}

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
static LayerMask*
xcf_load_layer_mask (XcfInfo *info,
		     GImage  *gimage)
{
  LayerMask *layer_mask;
  guint32 hierarchy_offset;
  int width;
  int height;
  int add_floating_sel;
  char *name;
  guchar color[3] = { 0, 0, 0 };

  /* check and see if this is the drawable the floating selection
   *  is attached to. if it is then we'll do the attachment at
   *  the end of this function.
   */
  add_floating_sel = (info->cp == info->floating_sel_offset);

  /* read in the layer width, height and name */
  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);
  info->cp += xcf_read_string (info->fp, &name, 1);

  /* create a new layer mask */
  layer_mask = layer_mask_new (gimage->ID, width, height, name, 255, color);
  if (!layer_mask)
    {
      g_free (name);
      return NULL;
    }

  /* read in the layer_mask properties */
  if (!xcf_load_channel_props (info, gimage, GIMP_CHANNEL(layer_mask)))
    goto error;

  /* read the hierarchy and layer mask offsets */
  info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);

  /* read in the hierarchy */
  xcf_seek_pos (info, hierarchy_offset);
  if (!xcf_load_hierarchy (info, GIMP_DRAWABLE(layer_mask)->tiles))
    goto error;

  /* attach the floating selection... */
  if (add_floating_sel)
    {
      Layer *floating_sel;

      floating_sel = info->floating_sel;
      floating_sel_attach (floating_sel, GIMP_DRAWABLE(layer_mask));
    }

  return layer_mask;

error:
  layer_mask_delete (layer_mask);
  return NULL;
}

Elliot Lee's avatar
Elliot Lee committed
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
static gint
xcf_load_hierarchy (XcfInfo     *info,
		    TileManager *tiles)
{
  guint32 saved_pos;
  guint32 offset;
  int width;
  int height;
  int bpp;
  int i;

  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &bpp, 1);

  /* make sure the values in the file correspond to the values
   *  calculated when the TileManager was created.
   */
  if ((width != tiles->levels[0].width) ||
      (height != tiles->levels[0].height) ||
      (bpp != tiles->levels[0].bpp))
    return FALSE;

  /* load in the levels...we make sure that the number of levels
   *  calculated when the TileManager was created is the same
   *  as the number of levels found in the file.
   */
  for (i = 0; i < tiles->nlevels; i++)
    {
      /* read in the offset of the next level */
      info->cp += xcf_read_int32 (info->fp, &offset, 1);

      if (offset == 0)
	{
	  g_warning ("not enough levels found in hierarchy");
	  return FALSE;
	}

      /* save the current position as it is where the
       *  next level offset is stored.
       */
      saved_pos = info->cp;

      /* seek to the level offset */
      xcf_seek_pos (info, offset);

      /* read in the level */
      if (!xcf_load_level (info, tiles, &tiles->levels[i], i))
	return FALSE;

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
      xcf_seek_pos (info, saved_pos);
    }

  info->cp += xcf_read_int32 (info->fp, &offset, 1);
  if (offset != 0)
    {
      g_warning ("encountered garbage after reading hierarchy: %d", offset);
      return FALSE;
    }

  return TRUE;
}

static gint
xcf_load_level (XcfInfo     *info,
		TileManager *tiles,
		TileLevel   *level,
		int          level_num)
{
  guint32 saved_pos;
  guint32 offset;
  guint ntiles;
  int width;
  int height;
  int i;

  info->cp += xcf_read_int32 (info->fp, (guint32*) &width, 1);
  info->cp += xcf_read_int32 (info->fp, (guint32*) &height, 1);

  if ((width != level->width) ||
      (height != level->height))
    return FALSE;

  /* read in the first tile offset.
   *  if it is '0', then this tile level is empty
   *  and we can simply return.
   */
  info->cp += xcf_read_int32 (info->fp, &offset, 1);
  if (offset == 0)
    return TRUE;

  /* get the first tile from this level. this will
   *  cause 'level->tiles' to be created so that
   *  we can load in the tiles.
   */
  tile_manager_get (tiles, 0, level_num);

  ntiles = level->ntile_rows * level->ntile_cols;
  for (i = 0; i < ntiles; i++)
    {
      if (offset == 0)
	{
	  g_warning ("not enough tiles found in level");
	  return FALSE;
	}

      /* save the current position as it is where the
       *  next tile offset is stored.
       */
      saved_pos = info->cp;

      /* seek to the tile offset */
      xcf_seek_pos (info, offset);

      /* read in the tile */
      switch (info->compression)
	{
	case COMPRESS_NONE:
	  if (!xcf_load_tile (info, &level->tiles[i]))
	    return FALSE;
	  break;
	case COMPRESS_RLE:
	  if (!xcf_load_tile_rle (info, &level->tiles[i]))
	    return FALSE;
	  break;
	case COMPRESS_ZLIB:
	  g_error ("xcf: zlib compression unimplemented");
	  break;
	case COMPRESS_FRACTAL:
	  g_error ("xcf: fractal compression unimplemented");
	  break;
	}

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
      xcf_seek_pos (info, saved_pos);

      /* read in the offset of the next tile */
      info->cp += xcf_read_int32 (info->fp, &offset, 1);
    }

  if (offset != 0)
    {
      g_warning ("encountered garbage after reading level: %d", offset);
      return FALSE;
    }

  return TRUE;
}

static gint
xcf_load_tile (XcfInfo *info,
	       Tile    *tile)
{
#ifdef SWAP_FROM_FILE

  if (!info->swap_num)
    {
      info->ref_count = g_new (int, 1);
      info->swap_num = tile_swap_add (info->filename, xcf_swap_func, info->ref_count);
    }

  tile->swap_num = info->swap_num;
  tile->swap_offset = info->cp;
  *info->ref_count += 1;

#else

  tile_ref (tile);
  info->cp += xcf_read_int8 (info->fp, tile->data, tile_size (tile));
  tile_unref (tile, TRUE);

#endif

  return TRUE;
}

static gint
xcf_load_tile_rle (XcfInfo *info,
		   Tile    *tile)
{
  guchar *data;
  guchar buffer[1024];
  guchar val;
  int size;
  int count;
  int length;
  int tmp;
  int bpp;
  int i, j;

  tile_ref (tile);

  data = tile->data;
  bpp = tile->bpp;

  for (i = 0; i < bpp; i++)
    {
      data = tile->data + i;
      size = tile->ewidth * tile->eheight;
      count = 0;

      while (size > 0)
	{
	  info->cp += xcf_read_int8 (info->fp, &val, 1);

	  length = val;
	  if (length >= 128)
	    {
	      length = 255 - (length - 1);
	      if (length == 128)
		{
		  info->cp += xcf_read_int8 (info->fp, buffer, 2);
		  length = (buffer[0] << 8) + buffer[1];
		}

	      count += length;
	      size -= length;

	      while (length > 0)
		{
		  tmp = MIN (length, 1024);
		  info->cp += xcf_read_int8 (info->fp, buffer, tmp);
		  length -= tmp;

		  for (j = 0; j < tmp; j++)
		    {
		      *data = buffer[j];
		      data += bpp;
		    }
		}
	    }
	  else
	    {
	      length += 1;
	      if (length == 128)
		{
		  info->cp += xcf_read_int8 (info->fp, buffer, 2);
		  length = (buffer[0] << 8) + buffer[1];
		}

	      count += length;
	      size -= length;

              if (size < 0)
                g_warning ("xcf: uh oh! xcf rle tile loading error: %d", count);

	      info->cp += xcf_read_int8 (info->fp, &val, 1);

	      for (j = 0; j < length; j++)
		{
		  *data = val;
		  data += bpp;
		}
	    }
	}
    }

  tile_unref (tile, TRUE);

  return TRUE;
}


#ifdef SWAP_FROM_FILE

static int
xcf_swap_func (int       fd,
	       Tile     *tile,
	       int       cmd,
	       gpointer  user_data)
{
  int bytes;
  int err;
  int nleft;
  int *ref_count;

  switch (cmd)
    {
    case SWAP_IN:
      lseek (fd, tile->swap_offset, SEEK_SET);

      bytes = tile_size (tile);
      tile_alloc (tile);

      nleft = bytes;
      while (nleft > 0)
	{
	  do {
	    err = read (fd, tile->data + bytes - nleft, nleft);
	  } while ((err == -1) && ((errno == EAGAIN) || (errno == EINTR)));

	  if (err <= 0)
	    {
	      g_warning ("unable to read tile data from xcf file: %d ( %d ) bytes read", err, nleft);
	      return FALSE;
	    }

	  nleft -= err;
	}
      break;
    case SWAP_OUT:
    case SWAP_DELETE:
    case SWAP_COMPRESS:
      ref_count = user_data;
      *ref_count -= 1;
      if (*ref_count == 0)
	{
	  tile_swap_remove (tile->swap_num);
	  g_free (ref_count);
	}

      tile->swap_num = 1;
      tile->swap_offset = -1;

      return TRUE;
    }

  return FALSE;
}

#endif


static void
xcf_seek_pos (XcfInfo *info,
	      guint    pos)
{
  if (info->cp != pos)
    {
      info->cp = pos;
      fseek (info->fp, info->cp, SEEK_SET);
    }
}

static void
xcf_seek_end (XcfInfo *info)
{
  fseek (info->fp, 0, SEEK_END);
  info->cp = ftell (info->fp);
}


static guint
xcf_read_int32 (FILE     *fp,
		guint32  *data,
		gint      count)
{
  guint total;

  total = count;
  if (count > 0)
    {
      xcf_read_int8 (fp, (guint8*) data, count * 4);

      while (count--)
        {
          *data = ntohl (*data);
          data++;
        }
    }

  return total * 4;
}

static guint
xcf_read_int8 (FILE     *fp,
	       guint8   *data,
	       gint      count)
{
  guint total;
  int bytes;

  total = count;
  while (count > 0)
    {
      bytes = fread ((char*) data, sizeof (char), count, fp);
Manish Singh's avatar
Manish Singh committed
2168 2169
      if (bytes <= 0) /* something bad happened */
        break;
Elliot Lee's avatar
Elliot Lee committed
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
      count -= bytes;
      data += bytes;
    }

  return total;
}

static guint
xcf_read_string (FILE     *fp,
		 gchar   **data,
		 gint      count)
{
  guint32 tmp;
  guint total;
  int i;

  total = 0;
  for (i = 0; i < count; i++)
    {
      total += xcf_read_int32 (fp, &tmp, 1);
      if (tmp > 0)
        {
          data[i] = g_new (gchar, tmp);
          total += xcf_read_int8 (fp, (guint8*) data[i], tmp);
        }
      else
        {
          data[i] = NULL;
        }
    }

  return total;
}

static guint
xcf_write_int32 (FILE     *fp,
		 guint32  *data,
		 gint      count)
{
  guint32 tmp;
  int i;

  if (count > 0)
    {
      for (i = 0; i < count; i++)
        {
          tmp = htonl (data[i]);
          xcf_write_int8 (fp, (guint8*) &tmp, 4);
        }
    }

  return count * 4;
}

static guint
xcf_write_int8 (FILE     *fp,
		guint8   *data,
		gint      count)
{
  guint total;
  int bytes;

  total = count;
  while (count > 0)
    {
      bytes = fwrite ((char*) data, sizeof (char), count, fp);
      count -= bytes;
      data += bytes;
    }

  return total;
}

static guint
xcf_write_string (FILE     *fp,
		  gchar   **data,
		  gint      count)
{
  guint32 tmp;
  guint total;
  int i;

  total = 0;
  for (i = 0; i < count; i++)
    {
      if (data[i])
        tmp = strlen (data[i]) + 1;
      else
        tmp = 0;

      xcf_write_int32 (fp, &tmp, 1);
      if (tmp > 0)
        xcf_write_int8 (fp, (guint8*) data[i], tmp);

      total += 4 + tmp;
    }

  return total;
}