ddsread.c 40.8 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * DDS GIMP plugin
 *
 * Copyright (C) 2004-2012 Shawn Kirst <skirst@gmail.com>,
 * with parts (C) 2003 Arne Reuter <homepage@arnereuter.de> where specified.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1301, USA.
 */
22 23

/*
24 25 26 27 28 29
 ** !!! COPYRIGHT NOTICE !!!
 **
 ** The following is based on code (C) 2003 Arne Reuter <homepage@arnereuter.de>
 ** URL: http://www.dr-reuter.de/arne/dds.html
 **
 */
30

Jehan's avatar
Jehan committed
31 32
#include "config.h"

33 34 35 36 37 38 39 40 41 42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

Jehan's avatar
Jehan committed
43 44
#include <libgimp/stdplugins-intl.h>

45 46 47 48 49 50 51 52 53
#include "ddsplugin.h"
#include "dds.h"
#include "dxt.h"
#include "endian.h"
#include "misc.h"
#include "imath.h"

typedef struct
{
54 55 56 57 58 59
  unsigned char  rshift, gshift, bshift, ashift;
  unsigned char  rbits, gbits, bbits, abits;
  unsigned int   rmask, gmask, bmask, amask;
  unsigned int   bpp, gimp_bpp;
  int            tile_height;
  unsigned char *palette;
60 61
} dds_load_info_t;

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
static int           read_header       (dds_header_t      *hdr,
                                        FILE              *fp);
static int           read_header_dx10  (dds_header_dx10_t *hdr,
                                        FILE              *fp);
static int           validate_header   (dds_header_t      *hdr);
static int           setup_dxgi_format (dds_header_t      *hdr,
                                        dds_header_dx10_t *dx10hdr);
static int           load_layer        (FILE              *fp,
                                        dds_header_t      *hdr,
                                        dds_load_info_t   *d,
                                        gint32             image,
                                        unsigned int       level,
                                        char              *prefix,
                                        unsigned int      *l,
                                        guchar            *pixels,
                                        unsigned char     *buf);
static int           load_mipmaps      (FILE              *fp,
                                        dds_header_t      *hdr,
                                        dds_load_info_t   *d,
                                        gint32             image,
                                        char              *prefix,
                                        unsigned int      *l,
                                        guchar            *pixels,
                                        unsigned char     *buf);
static int           load_face         (FILE              *fp,
                                        dds_header_t      *hdr,
                                        dds_load_info_t   *d,
                                        gint32             image,
                                        char              *prefix,
                                        unsigned int      *l,
                                        guchar            *pixels,
                                        unsigned char     *buf);
static unsigned char color_bits        (unsigned int       mask);
static unsigned char color_shift       (unsigned int       mask);
static int           load_dialog       (void);
97 98 99

static int runme = 0;

100 101 102 103
GimpPDBStatusType
read_dds (gchar    *filename,
          gint32   *imageID,
          gboolean  interactive_dds)
104
{
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  gint32 image = 0;
  unsigned char *buf;
  unsigned int l = 0;
  guchar *pixels;
  gchar *tmp;
  FILE *fp;
  dds_header_t hdr;
  dds_header_dx10_t dx10hdr;
  dds_load_info_t d;
  gint *layers, layer_count;
  GimpImageBaseType type;
  int i, j;

  if (interactive_dds)
    {
      if (!load_dialog ())
        return (GIMP_PDB_CANCEL);
    }

  fp = g_fopen (filename, "rb");
  if (fp == 0)
    {
      g_message ("Error opening file.\n");
      return (GIMP_PDB_EXECUTION_ERROR);
    }

  if (strrchr (filename, '/'))
    tmp = g_strdup_printf ("Loading %s:", strrchr (filename, '/') + 1);
  else
    tmp = g_strdup_printf ("Loading %s:", filename);
  gimp_progress_init (tmp);
  g_free (tmp);

  /* read header */
  read_header (&hdr, fp);

  memset (&dx10hdr, 0, sizeof (dds_header_dx10_t));

  /* read DX10 header if necessary */
  if (GETL32(hdr.pixelfmt.fourcc) == FOURCC ('D','X','1','0'))
    {
146 147
      read_header_dx10(&dx10hdr, fp);

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
      if (!setup_dxgi_format (&hdr, &dx10hdr))
        {
          fclose (fp);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
    }

  if (!validate_header (&hdr))
    {
      fclose (fp);
      g_message ("Invalid DDS header!\n");
      return (GIMP_PDB_EXECUTION_ERROR);
    }

  /* a lot of DDS images out there don't have this for some reason -_- */
  if (hdr.pitch_or_linsize == 0)
    {
      if (hdr.pixelfmt.flags & DDPF_FOURCC) /* assume linear size */
        {
          hdr.pitch_or_linsize = ((hdr.width + 3) >> 2) * ((hdr.height + 3) >> 2);
          switch (GETL32(hdr.pixelfmt.fourcc))
            {
            case FOURCC ('D','X','T','1'):
            case FOURCC ('A','T','I','1'):
            case FOURCC ('B','C','4','U'):
            case FOURCC ('B','C','4','S'):
              hdr.pitch_or_linsize *= 8;
              break;
176
            default:
177 178 179 180
              hdr.pitch_or_linsize *= 16;
              break;
            }
        }
181
      else /* assume pitch */
182 183 184 185
        {
          hdr.pitch_or_linsize = hdr.height * hdr.width * (hdr.pixelfmt.bpp >> 3);
        }
    }
186

187 188
  if (hdr.pixelfmt.flags & DDPF_FOURCC)
    {
189
      /* fourcc is dXt* or rXgb */
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
      if (hdr.pixelfmt.fourcc[1] == 'X')
        hdr.pixelfmt.flags |= DDPF_ALPHAPIXELS;
    }

  if (hdr.pixelfmt.flags & DDPF_FOURCC)
    {
      switch (GETL32(hdr.pixelfmt.fourcc))
        {
        case FOURCC ('A','T','I','1'):
        case FOURCC ('B','C','4','U'):
        case FOURCC ('B','C','4','S'):
          d.bpp = d.gimp_bpp = 1;
          type = GIMP_GRAY;
          break;
        case FOURCC ('A','T','I','2'):
        case FOURCC ('B','C','5','U'):
        case FOURCC ('B','C','5','S'):
          d.bpp = d.gimp_bpp = 3;
          type = GIMP_RGB;
          break;
        default:
          d.bpp = d.gimp_bpp = 4;
          type = GIMP_RGB;
          break;
        }
    }
  else
    {
218 219
      d.bpp = hdr.pixelfmt.bpp >> 3;

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
      if (d.bpp == 2)
        {
          if (hdr.pixelfmt.amask == 0xf000) // RGBA4
            {
              d.gimp_bpp = 4;
              type = GIMP_RGB;
            }
          else if (hdr.pixelfmt.amask == 0xff00) //L8A8
            {
              d.gimp_bpp = 2;
              type = GIMP_GRAY;
            }
          else if (hdr.pixelfmt.bmask == 0x1f) //R5G6B5 or RGB5A1
            {
              if (hdr.pixelfmt.amask == 0x8000) // RGB5A1
                d.gimp_bpp = 4;
              else
                d.gimp_bpp = 3;

              type = GIMP_RGB;
            }
          else //L16
            {
              d.gimp_bpp = 1;
              type = GIMP_GRAY;
            }
        }
247
      else
248 249 250 251 252 253 254
        {
          if (hdr.pixelfmt.flags & DDPF_PALETTEINDEXED8)
            {
              type = GIMP_INDEXED;
              d.gimp_bpp = 1;
            }
          else if (hdr.pixelfmt.rmask == 0xe0) // R3G3B2
255
            {
256 257
              type = GIMP_RGB;
              d.gimp_bpp = 3;
258
            }
259
          else
260
            {
261 262 263 264 265 266 267 268 269 270 271
              /* test alpha only image */
              if (d.bpp == 1 && (hdr.pixelfmt.flags & DDPF_ALPHA))
                {
                  d.gimp_bpp = 2;
                  type = GIMP_GRAY;
                }
              else
                {
                  d.gimp_bpp = d.bpp;
                  type = (d.bpp == 1) ? GIMP_GRAY : GIMP_RGB;
                }
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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        }
    }

  image = gimp_image_new (hdr.width, hdr.height, type);

  if (image == -1)
    {
      g_message ("Can't allocate new image.\n");
      fclose (fp);
      return (GIMP_PDB_EXECUTION_ERROR);
    }

  gimp_image_set_filename (image, filename);

  if (hdr.pixelfmt.flags & DDPF_PALETTEINDEXED8)
    {
      d.palette = g_malloc (256 * 4);
      if (fread (d.palette, 1, 1024, fp) != 1024)
        {
          g_message ("Error reading palette.\n");
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      for (i = j = 0; i < 768; i += 3, j += 4)
        {
          d.palette[i + 0] = d.palette[j + 0];
          d.palette[i + 1] = d.palette[j + 1];
          d.palette[i + 2] = d.palette[j + 2];
        }
      gimp_image_set_colormap (image, d.palette, 256);
    }

  d.tile_height = gimp_tile_height ();

  pixels = g_new (guchar, d.tile_height * hdr.width * d.gimp_bpp);
  buf = g_malloc (hdr.pitch_or_linsize);

  d.rshift = color_shift (hdr.pixelfmt.rmask);
  d.gshift = color_shift (hdr.pixelfmt.gmask);
  d.bshift = color_shift (hdr.pixelfmt.bmask);
  d.ashift = color_shift (hdr.pixelfmt.amask);
  d.rbits = color_bits (hdr.pixelfmt.rmask);
  d.gbits = color_bits (hdr.pixelfmt.gmask);
  d.bbits = color_bits (hdr.pixelfmt.bmask);
  d.abits = color_bits (hdr.pixelfmt.amask);
  d.rmask = (hdr.pixelfmt.rmask >> d.rshift) << (8 - d.rbits);
  d.gmask = (hdr.pixelfmt.gmask >> d.gshift) << (8 - d.gbits);
  d.bmask = (hdr.pixelfmt.bmask >> d.bshift) << (8 - d.bbits);
  d.amask = (hdr.pixelfmt.amask >> d.ashift) << (8 - d.abits);

  if (!(hdr.caps.caps2 & DDSCAPS2_CUBEMAP) &&
325 326
      !(hdr.caps.caps2 & DDSCAPS2_VOLUME) &&
      dx10hdr.arraySize == 0)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    {
      if (!load_layer (fp, &hdr, &d, image, 0, "", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if (!load_mipmaps (fp, &hdr, &d, image, "", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
    }
  else if (hdr.caps.caps2 & DDSCAPS2_CUBEMAP)
    {
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEX) &&
          !load_face (fp, &hdr, &d, image, "(positive x)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) &&
          !load_face (fp, &hdr, &d, image, "(negative x)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEY) &&
          !load_face (fp, &hdr, &d, image, "(positive y)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) &&
          !load_face (fp, &hdr, &d, image, "(negative y)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) &&
          !load_face (fp, &hdr, &d, image, "(positive z)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
      if ((hdr.caps.caps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) &&
          !load_face (fp, &hdr, &d, image, "(negative z)", &l, pixels, buf))
        {
          fclose (fp);
          gimp_image_delete (image);
          return (GIMP_PDB_EXECUTION_ERROR);
        }
    }
  else if ((hdr.caps.caps2 & DDSCAPS2_VOLUME) &&
           (hdr.flags & DDSD_DEPTH))
    {
389 390
      unsigned int i, level;
      char *plane;
391 392 393 394
      for (i = 0; i < hdr.depth; ++i)
        {
          plane = g_strdup_printf ("(z = %d)", i);
          if (!load_layer (fp, &hdr, &d, image, 0, plane, &l, pixels, buf))
395
            {
396 397 398 399
              g_free (plane);
              fclose (fp);
              gimp_image_delete (image);
              return (GIMP_PDB_EXECUTION_ERROR);
400
            }
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
          g_free (plane);
        }

      if ((hdr.flags & DDSD_MIPMAPCOUNT) &&
          (hdr.caps.caps1 & DDSCAPS_MIPMAP) &&
          (dds_read_vals.mipmaps != 0))
        {
          for (level = 1; level < hdr.num_mipmaps; ++level)
            {
              int n = hdr.depth >> level;
              if (n < 1) n = 1;
              for (i = 0; i < n; ++i)
                {
                  plane = g_strdup_printf ("(z = %d)", i);
                  if (!load_layer (fp, &hdr, &d, image, level, plane, &l, pixels, buf))
                    {
                      g_free (plane);
                      fclose (fp);
                      gimp_image_delete (image);
                      return (GIMP_PDB_EXECUTION_ERROR);
                    }
                  g_free (plane);
                }
            }
        }
    }
  else if (dx10hdr.arraySize > 0)
    {
429 430 431
      unsigned int i;
      char *elem;

432 433 434 435 436 437 438 439 440 441 442 443 444 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
      for (i = 0; i < dx10hdr.arraySize; ++i)
        {
          elem = g_strdup_printf ("(array element %d)", i);
          if (!load_layer (fp, &hdr, &d, image, 0, elem, &l, pixels, buf))
            {
              fclose (fp);
              gimp_image_delete (image);
              return (GIMP_PDB_EXECUTION_ERROR);
            }
          if (!load_mipmaps (fp, &hdr, &d, image, elem, &l, pixels, buf))
            {
              fclose (fp);
              gimp_image_delete (image);
              return (GIMP_PDB_EXECUTION_ERROR);
            }
          g_free (elem);
        }
    }

  gimp_progress_update (1.0);

  if (hdr.pixelfmt.flags & DDPF_PALETTEINDEXED8)
    g_free (d.palette);

  g_free (buf);
  g_free (pixels);
  fclose (fp);

  layers = gimp_image_get_layers (image, &layer_count);

  if (layers == NULL || layer_count == 0)
    {
      g_message ("Oops!  NULL image read!  Please report this!");
      return (GIMP_PDB_EXECUTION_ERROR);
    }

  gimp_image_set_active_layer (image, layers[0]);
  g_free (layers);

  *imageID = image;

  return (GIMP_PDB_SUCCESS);
474 475
}

476 477 478
static int
read_header (dds_header_t *hdr,
             FILE         *fp)
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
  unsigned char buf[DDS_HEADERSIZE];

  memset (hdr, 0, sizeof (dds_header_t));

  if (fread (buf, 1, DDS_HEADERSIZE, fp) != DDS_HEADERSIZE)
    return (0);

  hdr->magic = GETL32(buf);

  hdr->size = GETL32(buf + 4);
  hdr->flags = GETL32(buf + 8);
  hdr->height = GETL32(buf + 12);
  hdr->width = GETL32(buf + 16);
  hdr->pitch_or_linsize = GETL32(buf + 20);
  hdr->depth = GETL32(buf + 24);
  hdr->num_mipmaps = GETL32(buf + 28);

  hdr->pixelfmt.size = GETL32(buf + 76);
  hdr->pixelfmt.flags = GETL32(buf + 80);
  hdr->pixelfmt.fourcc[0] = buf[84];
  hdr->pixelfmt.fourcc[1] = buf[85];
  hdr->pixelfmt.fourcc[2] = buf[86];
  hdr->pixelfmt.fourcc[3] = buf[87];
  hdr->pixelfmt.bpp = GETL32(buf + 88);
  hdr->pixelfmt.rmask = GETL32(buf + 92);
  hdr->pixelfmt.gmask = GETL32(buf + 96);
  hdr->pixelfmt.bmask = GETL32(buf + 100);
  hdr->pixelfmt.amask = GETL32(buf + 104);

  hdr->caps.caps1 = GETL32(buf + 108);
  hdr->caps.caps2 = GETL32(buf + 112);

  /* GIMP-DDS special info */
  if (GETL32(buf + 32) == FOURCC ('G','I','M','P') &&
      GETL32(buf + 36) == FOURCC ('-','D','D','S'))
    {
516 517 518 519
      hdr->reserved.gimp_dds_special.magic1 = GETL32(buf + 32);
      hdr->reserved.gimp_dds_special.magic2 = GETL32(buf + 36);
      hdr->reserved.gimp_dds_special.version = GETL32(buf + 40);
      hdr->reserved.gimp_dds_special.extra_fourcc = GETL32(buf + 44);
520
    }
521

522
  return (1);
523 524
}

525 526 527
static int
read_header_dx10(dds_header_dx10_t *hdr,
                 FILE              *fp)
528
{
529
  char buf[DDS_HEADERSIZE_DX10];
530

531
  memset (hdr, 0, sizeof (dds_header_dx10_t));
532

533 534
  if (fread (buf, 1, DDS_HEADERSIZE_DX10, fp) != DDS_HEADERSIZE_DX10)
    return (0);
535

536 537 538 539 540
  hdr->dxgiFormat = GETL32(buf);
  hdr->resourceDimension = GETL32(buf + 4);
  hdr->miscFlag = GETL32(buf + 8);
  hdr->arraySize = GETL32(buf + 12);
  hdr->reserved = GETL32(buf + 16);
541

542
  return (1);
543 544
}

545 546
static int
validate_header (dds_header_t *hdr)
547
{
548 549 550 551 552 553 554 555 556 557 558 559 560
  unsigned int fourcc;

  if (hdr->magic != FOURCC ('D','D','S',' '))
    {
      g_message ("Invalid DDS file.\n");
      return (0);
    }

  if ((hdr->flags & DDSD_PITCH) == (hdr->flags & DDSD_LINEARSIZE))
    {
      //g_message ("Warning: DDSD_PITCH or DDSD_LINEARSIZE is not set.\n");
      if (hdr->pixelfmt.flags & DDPF_FOURCC)
        hdr->flags |= DDSD_LINEARSIZE;
561
      else
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        hdr->flags |= DDSD_PITCH;
    }
  /*
     if ((hdr->pixelfmt.flags & DDPF_FOURCC) ==
     (hdr->pixelfmt.flags & DDPF_RGB))
     {
     g_message ("Invalid pixel format.\n");
     return (0);
     }
     */
  fourcc = GETL32(hdr->pixelfmt.fourcc);

  if ((hdr->pixelfmt.flags & DDPF_FOURCC) &&
      fourcc != FOURCC ('D','X','T','1') &&
      fourcc != FOURCC ('D','X','T','2') &&
      fourcc != FOURCC ('D','X','T','3') &&
      fourcc != FOURCC ('D','X','T','4') &&
      fourcc != FOURCC ('D','X','T','5') &&
      fourcc != FOURCC ('R','X','G','B') &&
      fourcc != FOURCC ('A','T','I','1') &&
      fourcc != FOURCC ('B','C','4','U') &&
      fourcc != FOURCC ('B','C','4','S') &&
      fourcc != FOURCC ('A','T','I','2') &&
      fourcc != FOURCC ('B','C','5','U') &&
      fourcc != FOURCC ('B','C','5','S') &&
      fourcc != FOURCC ('D','X','1','0'))
    {
      g_message ("Unsupported format (FOURCC: %c%c%c%c, hex: %08x).\n",
                 hdr->pixelfmt.fourcc[0],
                 hdr->pixelfmt.fourcc[1],
                 hdr->pixelfmt.fourcc[2],
                 hdr->pixelfmt.fourcc[3],
                 GETL32(hdr->pixelfmt.fourcc));
      return (0);
    }

  if (hdr->pixelfmt.flags & DDPF_RGB)
    {
      if ((hdr->pixelfmt.bpp !=  8) &&
          (hdr->pixelfmt.bpp != 16) &&
          (hdr->pixelfmt.bpp != 24) &&
          (hdr->pixelfmt.bpp != 32))
        {
          g_message ("Invalid BPP.\n");
          return (0);
        }
    }
  else if (hdr->pixelfmt.flags & DDPF_LUMINANCE)
    {
      if ((hdr->pixelfmt.bpp !=  8) &&
          (hdr->pixelfmt.bpp != 16))
        {
          g_message ("Invalid BPP.\n");
          return (0);
        }
617 618

      hdr->pixelfmt.flags |= DDPF_RGB;
619 620 621
    }
  else if (hdr->pixelfmt.flags & DDPF_PALETTEINDEXED8)
    {
622
      hdr->pixelfmt.flags |= DDPF_RGB;
623
    }
624

625
  if (!(hdr->pixelfmt.flags & DDPF_RGB) &&
626 627 628
      !(hdr->pixelfmt.flags & DDPF_ALPHA) &&
      !(hdr->pixelfmt.flags & DDPF_FOURCC) &&
      !(hdr->pixelfmt.flags & DDPF_LUMINANCE))
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    {
      g_message ("Unknown pixel format!  Taking a guess, expect trouble!");
      switch (fourcc)
        {
        case FOURCC ('D','X','T','1'):
        case FOURCC ('D','X','T','2'):
        case FOURCC ('D','X','T','3'):
        case FOURCC ('D','X','T','4'):
        case FOURCC ('D','X','T','5'):
        case FOURCC ('R','X','G','B'):
        case FOURCC ('A','T','I','1'):
        case FOURCC ('B','C','4','U'):
        case FOURCC ('B','C','4','S'):
        case FOURCC ('A','T','I','2'):
        case FOURCC ('B','C','5','U'):
        case FOURCC ('B','C','5','S'):
          hdr->pixelfmt.flags |= DDPF_FOURCC;
          break;
        default:
          switch (hdr->pixelfmt.bpp)
649
            {
650 651 652 653 654 655 656 657 658 659 660 661 662 663
            case 8:
              if (hdr->pixelfmt.flags & DDPF_ALPHAPIXELS)
                hdr->pixelfmt.flags |= DDPF_ALPHA;
              else
                hdr->pixelfmt.flags |= DDPF_LUMINANCE;
              break;
            case 16:
            case 24:
            case 32:
              hdr->pixelfmt.flags |= DDPF_RGB;
              break;
            default:
              g_message ("Invalid pixel format.");
              return (0);
664
            }
665 666 667
          break;
        }
    }
668

669
  return (1);
670 671 672 673 674 675
}

/*
 * This function will set the necessary flags and attributes in the standard
 * dds header using the information found in the DX10 header.
 */
676 677 678
static int
setup_dxgi_format (dds_header_t      *hdr,
                   dds_header_dx10_t *dx10hdr)
679
{
680
  if ((dx10hdr->resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE2D) &&
681
      (dx10hdr->miscFlag & D3D10_RESOURCE_MISC_TEXTURECUBE))
682
    {
683
      hdr->caps.caps2 |= DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALL_FACES;
684 685 686
    }
  else if (dx10hdr->resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE3D)
    {
687 688
      hdr->flags |= DDSD_DEPTH;
      hdr->caps.caps2 |= DDSCAPS2_VOLUME;
689
    }
690

691
  if ((dx10hdr->resourceDimension != D3D10_RESOURCE_DIMENSION_TEXTURE1D) &&
692 693
      (dx10hdr->resourceDimension != D3D10_RESOURCE_DIMENSION_TEXTURE2D) &&
      (dx10hdr->resourceDimension != D3D10_RESOURCE_DIMENSION_TEXTURE3D))
694
    return (0);
695

696 697
  // check for a compressed DXGI format
  if ((dx10hdr->dxgiFormat >= DXGI_FORMAT_BC1_TYPELESS) &&
698
      (dx10hdr->dxgiFormat <= DXGI_FORMAT_BC5_SNORM))
699
    {
700 701 702
      // set flag and replace FOURCC
      hdr->pixelfmt.flags |= DDPF_FOURCC;

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
      switch (dx10hdr->dxgiFormat)
        {
        case DXGI_FORMAT_BC1_TYPELESS:
        case DXGI_FORMAT_BC1_UNORM:
        case DXGI_FORMAT_BC1_UNORM_SRGB:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('D','X','T','1'));
          break;
        case DXGI_FORMAT_BC2_TYPELESS:
        case DXGI_FORMAT_BC2_UNORM:
        case DXGI_FORMAT_BC2_UNORM_SRGB:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('D','X','T','3'));
          break;
        case DXGI_FORMAT_BC3_TYPELESS:
        case DXGI_FORMAT_BC3_UNORM:
        case DXGI_FORMAT_BC3_UNORM_SRGB:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('D','X','T','5'));
          break;
        case DXGI_FORMAT_BC4_TYPELESS:
        case DXGI_FORMAT_BC4_UNORM:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('A','T','I','1'));
          break;
        case DXGI_FORMAT_BC4_SNORM:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('B','C','4','S'));
          break;
        case DXGI_FORMAT_BC5_TYPELESS:
        case DXGI_FORMAT_BC5_UNORM:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('A','T','I','2'));
          break;
        case DXGI_FORMAT_BC5_SNORM:
          PUTL32(hdr->pixelfmt.fourcc, FOURCC ('B','C','5','S'));
          break;
        default:
          break;
        }
    }
  else
    {
740 741 742
      /* unset the FOURCC flag */
      hdr->pixelfmt.flags &= ~DDPF_FOURCC;

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 834
      switch (dx10hdr->dxgiFormat)
        {
        case DXGI_FORMAT_B8G8R8A8_TYPELESS:
        case DXGI_FORMAT_B8G8R8A8_UNORM:
        case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
          hdr->pixelfmt.bpp = 32;
          hdr->pixelfmt.flags |= DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = 0x00ff0000;
          hdr->pixelfmt.gmask = 0x0000ff00;
          hdr->pixelfmt.bmask = 0x000000ff;
          hdr->pixelfmt.amask = 0xff000000;
          break;
        case DXGI_FORMAT_B8G8R8X8_TYPELESS:
        case DXGI_FORMAT_B8G8R8X8_UNORM:
        case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
          hdr->pixelfmt.bpp = 32;
          hdr->pixelfmt.flags |= DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = 0x00ff0000;
          hdr->pixelfmt.gmask = 0x0000ff00;
          hdr->pixelfmt.bmask = 0x000000ff;
          hdr->pixelfmt.amask = 0x00000000;
          break;
        case DXGI_FORMAT_R8G8B8A8_TYPELESS:
        case DXGI_FORMAT_R8G8B8A8_UNORM:
        case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
        case DXGI_FORMAT_R8G8B8A8_UINT:
        case DXGI_FORMAT_R8G8B8A8_SNORM:
        case DXGI_FORMAT_R8G8B8A8_SINT:
          hdr->pixelfmt.bpp = 32;
          hdr->pixelfmt.flags |= DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = 0x000000ff;
          hdr->pixelfmt.gmask = 0x0000ff00;
          hdr->pixelfmt.bmask = 0x00ff0000;
          hdr->pixelfmt.amask = 0xff000000;
          break;
        case DXGI_FORMAT_B5G6R5_UNORM:
          hdr->pixelfmt.bpp = 16;
          hdr->pixelfmt.rmask = 0x0000f800;
          hdr->pixelfmt.gmask = 0x000007e0;
          hdr->pixelfmt.bmask = 0x0000001f;
          hdr->pixelfmt.amask = 0x00000000;
          break;
        case DXGI_FORMAT_B5G5R5A1_UNORM:
          hdr->pixelfmt.bpp = 16;
          hdr->pixelfmt.rmask = 0x00007c00;
          hdr->pixelfmt.gmask = 0x000003e0;
          hdr->pixelfmt.bmask = 0x0000001f;
          hdr->pixelfmt.amask = 0x00008000;
          break;
        case DXGI_FORMAT_R10G10B10A2_TYPELESS:
        case DXGI_FORMAT_R10G10B10A2_UNORM:
        case DXGI_FORMAT_R10G10B10A2_UINT:
          hdr->pixelfmt.bpp = 32;
          hdr->pixelfmt.flags |= DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = 0x000003ff;
          hdr->pixelfmt.gmask = 0x000ffc00;
          hdr->pixelfmt.bmask = 0x3ff00000;
          hdr->pixelfmt.amask = 0xc0000000;
          break;
        case DXGI_FORMAT_A8_UNORM:
          hdr->pixelfmt.bpp = 8;
          hdr->pixelfmt.flags |= DDPF_ALPHA | DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = hdr->pixelfmt.gmask = hdr->pixelfmt.bmask = 0;
          hdr->pixelfmt.amask = 0x000000ff;
          break;
        case DXGI_FORMAT_R8_TYPELESS:
        case DXGI_FORMAT_R8_UNORM:
        case DXGI_FORMAT_R8_UINT:
        case DXGI_FORMAT_R8_SNORM:
        case DXGI_FORMAT_R8_SINT:
          hdr->pixelfmt.bpp = 8;
          hdr->pixelfmt.rmask = 0x000000ff;
          hdr->pixelfmt.gmask = hdr->pixelfmt.bmask = hdr->pixelfmt.amask = 0;
          break;
        case DXGI_FORMAT_B4G4R4A4_UNORM:
          hdr->pixelfmt.bpp = 16;
          hdr->pixelfmt.flags |= DDPF_ALPHAPIXELS;
          hdr->pixelfmt.rmask = 0x00000f00;
          hdr->pixelfmt.gmask = 0x000000f0;
          hdr->pixelfmt.bmask = 0x0000000f;
          hdr->pixelfmt.amask = 0x0000f000;
          break;
        case DXGI_FORMAT_UNKNOWN:
          g_message ("Unknown DXGI format.  Expect problems...");
          break;
        default:  /* unsupported DXGI format */
          g_message ("Unsupported DXGI format (%d)", dx10hdr->dxgiFormat);
          return (0);
        }
    }

  return (1);
835 836
}

837 838 839 840 841 842 843

static const Babl*
premultiplied_variant (const Babl* format)
{
  if (format == babl_format ("R'G'B'A u8"))
    return babl_format ("R'aG'aB'aA u8");
  else
844
    g_printerr ("Add format %s to premultiplied_variant () %s: %d\n", babl_get_name (format), __FILE__, __LINE__);
845 846 847
  return format;
}

848 849 850 851 852 853 854 855 856 857
static int
load_layer (FILE            *fp,
            dds_header_t    *hdr,
            dds_load_info_t *d,
            gint32           image,
            unsigned int     level,
            char            *prefix,
            unsigned int    *l,
            guchar          *pixels,
            unsigned char   *buf)
858
{
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  GeglBuffer    *buffer;
  const Babl    *bablfmt = NULL;
  GimpImageType  type = GIMP_RGBA_IMAGE;
  gchar         *layer_name;
  gint           x, y, z, n;
  gint32         layer;
  unsigned int   width = hdr->width >> level;
  unsigned int   height = hdr->height >> level;
  unsigned int   size = hdr->pitch_or_linsize >> (2 * level);
  unsigned int   layerw;
  int            format = DDS_COMPRESS_NONE;

  if (width < 1) width = 1;
  if (height < 1) height = 1;

  switch (d->bpp)
    {
    case 1:
      if (hdr->pixelfmt.flags & DDPF_PALETTEINDEXED8)
        {
          type = GIMP_INDEXED_IMAGE;
          bablfmt = babl_format ("R'G'B' u8");
        }
      else if (hdr->pixelfmt.rmask == 0xe0)
        {
          type = GIMP_RGB_IMAGE;
          bablfmt = babl_format ("R'G'B' u8");
        }
      else if (hdr->pixelfmt.flags & DDPF_ALPHA)
        {
          type = GIMP_GRAYA_IMAGE;
          bablfmt = babl_format ("Y'A u8");
        }
      else
        {
          type = GIMP_GRAY_IMAGE;
          bablfmt = babl_format ("Y' u8");
        }
      break;
    case 2:
      if (hdr->pixelfmt.amask == 0xf000) /* RGBA4 */
        {
          type = GIMP_RGBA_IMAGE;
          bablfmt = babl_format ("R'G'B'A u8");
        }
      else if (hdr->pixelfmt.amask == 0xff00) /* L8A8 */
        {
          type = GIMP_GRAYA_IMAGE;
          bablfmt = babl_format ("Y'A u8");
        }
      else if (hdr->pixelfmt.bmask == 0x1f) /* R5G6B5 or RGB5A1 */
        {
          type = (hdr->pixelfmt.amask == 0x8000) ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
          bablfmt = (hdr->pixelfmt.amask == 0x8000) ? babl_format ("R'G'B'A u8") : babl_format ("R'G'B' u8");
        }
      else /* L16 */
        {
          type = GIMP_GRAY_IMAGE;
          bablfmt = babl_format ("Y' u8");
        }
      break;
    case 3: type = GIMP_RGB_IMAGE;  bablfmt = babl_format ("R'G'B' u8");  break;
    case 4: type = GIMP_RGBA_IMAGE; bablfmt = babl_format ("R'G'B'A u8"); break;
    }

  layer_name = (level) ? g_strdup_printf ("mipmap %d %s", level, prefix) :
    g_strdup_printf ("main surface %s", prefix);

  layer = gimp_layer_new (image, layer_name, width, height, type, 100,
Jehan's avatar
Jehan committed
928
                          GIMP_LAYER_MODE_NORMAL);
929 930 931
  g_free (layer_name);

  gimp_image_insert_layer (image, layer, 0, *l);
932

933
  if ((*l)++) gimp_item_set_visible (layer, FALSE);
934

935
  buffer = gimp_drawable_get_buffer (layer);
936

937
  layerw = gegl_buffer_get_width (buffer);
938

939 940
  if (hdr->pixelfmt.flags & DDPF_FOURCC)
    {
941 942 943
      unsigned int w = (width  + 3) >> 2;
      unsigned int h = (height + 3) >> 2;

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
      switch (GETL32(hdr->pixelfmt.fourcc))
        {
        case FOURCC ('D','X','T','1'): format = DDS_COMPRESS_BC1; break;
        case FOURCC ('D','X','T','2'): bablfmt = premultiplied_variant (bablfmt);
        case FOURCC ('D','X','T','3'): format = DDS_COMPRESS_BC2; break;
        case FOURCC ('D','X','T','4'): bablfmt = premultiplied_variant (bablfmt);
        case FOURCC ('D','X','T','5'): format = DDS_COMPRESS_BC3; break;
        case FOURCC ('R','X','G','B'): format = DDS_COMPRESS_BC3; break;
        case FOURCC ('A','T','I','1'):
        case FOURCC ('B','C','4','U'):
        case FOURCC ('B','C','4','S'): format = DDS_COMPRESS_BC4; break;
        case FOURCC ('A','T','I','2'):
        case FOURCC ('B','C','5','U'):
        case FOURCC ('B','C','5','S'): format = DDS_COMPRESS_BC5; break;
        }
959 960

      size = w * h;
961 962
      if ((format == DDS_COMPRESS_BC1) || (format == DDS_COMPRESS_BC4))
        size *= 8;
963
      else
964 965 966 967 968 969 970 971 972 973 974 975
        size *= 16;
    }

  if ((hdr->flags & DDSD_LINEARSIZE) &&
      !fread (buf, size, 1, fp))
    {
      g_message ("Unexpected EOF.\n");
      return (0);
    }

  if ((hdr->pixelfmt.flags & DDPF_RGB) || (hdr->pixelfmt.flags & DDPF_ALPHA))
    {
976
      z = 0;
977 978 979
      for (y = 0, n = 0; y < height; ++y, ++n)
        {
          if (n >= d->tile_height)
980
            {
981 982 983 984
              gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y - n, layerw, n), 0,
                               bablfmt, pixels, GEGL_AUTO_ROWSTRIDE);
              n = 0;
              gimp_progress_update ((double)y / (double)hdr->height);
985
            }
986 987 988

          if ((hdr->flags & DDSD_PITCH) &&
              !fread (buf, width * d->bpp, 1, fp))
989
            {
990 991
              g_message ("Unexpected EOF.\n");
              return (0);
992
            }
993 994 995 996

          if (!(hdr->flags & DDSD_LINEARSIZE)) z = 0;

          for (x = 0; x < layerw; ++x)
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
              unsigned int pixel = buf[z];
              unsigned int pos = (n * layerw + x) * d->gimp_bpp;

              if (d->bpp > 1) pixel += ((unsigned int)buf[z + 1] <<  8);
              if (d->bpp > 2) pixel += ((unsigned int)buf[z + 2] << 16);
              if (d->bpp > 3) pixel += ((unsigned int)buf[z + 3] << 24);

              if (d->bpp >= 3)
                {
                  if (hdr->pixelfmt.amask == 0xc0000000) // handle RGB10A2
                    {
                      pixels[pos + 0] = (pixel >> d->bshift) >> 2;
                      pixels[pos + 1] = (pixel >> d->gshift) >> 2;
                      pixels[pos + 2] = (pixel >> d->rshift) >> 2;
                      if (hdr->pixelfmt.flags & DDPF_ALPHAPIXELS)
                        pixels[pos + 3] = (pixel >> d->ashift << (8 - d->abits) & d->amask) * 255 / d->amask;
                    }
                  else
                    {
                      pixels[pos] =
                        (pixel >> d->rshift << (8 - d->rbits) & d->rmask) * 255 / d->rmask;
                      pixels[pos + 1] =
                        (pixel >> d->gshift << (8 - d->gbits) & d->gmask) * 255 / d->gmask;
                      pixels[pos + 2] =
                        (pixel >> d->bshift << (8 - d->bbits) & d->bmask) * 255 / d->bmask;
                      if (hdr->pixelfmt.flags & DDPF_ALPHAPIXELS)
                        {
                          pixels[pos + 3] =
                            (pixel >> d->ashift << (8 - d->abits) & d->amask) * 255 / d->amask;
                        }
                    }
                }
              else if (d->bpp == 2)
                {
                  if (hdr->pixelfmt.amask == 0xf000) //RGBA4
                    {
                      pixels[pos] =
                        (pixel >> d->rshift << (8 - d->rbits) & d->rmask) * 255 / d->rmask;
                      pixels[pos + 1] =
                        (pixel >> d->gshift << (8 - d->gbits) & d->gmask) * 255 / d->gmask;
                      pixels[pos + 2] =
                        (pixel >> d->bshift << (8 - d->bbits) & d->bmask) * 255 / d->bmask;
                      pixels[pos + 3] =
                        (pixel >> d->ashift << (8 - d->abits) & d->amask) * 255 / d->amask;
                    }
                  else if (hdr->pixelfmt.amask == 0xff00) //L8A8
                    {
                      pixels[pos] =
                        (pixel >> d->rshift << (8 - d->rbits) & d->rmask) * 255 / d->rmask;
                      pixels[pos + 1] =
                        (pixel >> d->ashift << (8 - d->abits) & d->amask) * 255 / d->amask;
                    }
                  else if (hdr->pixelfmt.bmask == 0x1f) //R5G6B5 or RGB5A1
                    {
                      pixels[pos] =
                        (pixel >> d->rshift << (8 - d->rbits) & d->rmask) * 255 / d->rmask;
                      pixels[pos + 1] =
                        (pixel >> d->gshift << (8 - d->gbits) & d->gmask) * 255 / d->gmask;
                      pixels[pos + 2] =
                        (pixel >> d->bshift << (8 - d->bbits) & d->bmask) * 255 / d->bmask;
                      if (hdr->pixelfmt.amask == 0x8000)
                        {
                          pixels[pos + 3] =
                            (pixel >> d->ashift << (8 - d->abits) & d->amask) * 255 / d->amask;
                        }
                    }
                  else //L16
                    pixels[pos] = (unsigned char)(255 * ((float)(pixel & 0xffff) / 65535.0f));
                }
              else
                {
                  if (hdr->pixelfmt.flags & DDPF_PALETTEINDEXED8)
                    {
                      pixels[pos] = pixel & 0xff;
                    }
                  else if (hdr->pixelfmt.rmask == 0xe0) // R3G3B2
                    {
                      pixels[pos] =
                        (pixel >> d->rshift << (8 - d->rbits) & d->rmask) * 255 / d->rmask;
                      pixels[pos + 1] =
                        (pixel >> d->gshift << (8 - d->gbits) & d->gmask) * 255 / d->gmask;
                      pixels[pos + 2] =
                        (pixel >> d->bshift << (8 - d->bbits) & d->bmask) * 255 / d->bmask;
                    }
                  else if (hdr->pixelfmt.flags & DDPF_ALPHA)
                    {
                      pixels[pos + 0] = 255;
                      pixels[pos + 1] = pixel & 0xff;
                    }
                  else // LUMINANCE
                    {
                      pixels[pos] = pixel & 0xff;
                    }
                }

              z += d->bpp;
1094
            }
1095
        }
1096

1097 1098 1099 1100 1101
      gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y - n, layerw, n), 0,
                       bablfmt, pixels, GEGL_AUTO_ROWSTRIDE);
    }
  else if (hdr->pixelfmt.flags & DDPF_FOURCC)
    {
1102 1103
      unsigned char *dst;

1104 1105 1106 1107 1108
      if (!(hdr->flags & DDSD_LINEARSIZE))
        {
          g_message ("Image marked as compressed, but DDSD_LINEARSIZE is not set.\n");
          return (0);
        }
1109

1110 1111
      dst = g_malloc (width * height * d->gimp_bpp);
      memset (dst, 0, width * height * d->gimp_bpp);
1112

1113 1114 1115 1116 1117 1118
      if (d->gimp_bpp == 4)
        {
          for (y = 0; y < height; ++y)
            for (x = 0; x < width; ++x)
              dst[y * (width * 4) + (x * 4) + 3] = 255;
        }
1119

1120 1121
      dxt_decompress (dst, buf, format, size, width, height, d->gimp_bpp,
                      hdr->pixelfmt.flags & DDPF_NORMAL);
1122 1123

      z = 0;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 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 1162 1163 1164 1165 1166 1167 1168 1169 1170
      for (y = 0, n = 0; y < height; ++y, ++n)
        {
          if (n >= d->tile_height)
            {
              gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y - n, layerw, n), 0,
                               bablfmt, pixels, GEGL_AUTO_ROWSTRIDE);
              n = 0;
              gimp_progress_update ((double)y / (double)hdr->height);
            }

          memcpy (pixels + n * layerw * d->gimp_bpp,
                  dst + y * layerw * d->gimp_bpp,
                  width * d->gimp_bpp);
        }

      gegl_buffer_set (buffer, GEGL_RECTANGLE (0, y - n, layerw, n), 0,
                       bablfmt, pixels, GEGL_AUTO_ROWSTRIDE);

      g_free (dst);
    }

  gegl_buffer_flush (buffer);

  g_object_unref (buffer);

  /* gimp dds specific.  decode encoded images */
  if (dds_read_vals.decode_images &&
      hdr->reserved.gimp_dds_special.magic1 == FOURCC ('G','I','M','P') &&
      hdr->reserved.gimp_dds_special.magic2 == FOURCC ('-','D','D','S'))
    {
      switch (hdr->reserved.gimp_dds_special.extra_fourcc)
        {
        case FOURCC ('A','E','X','P'):
          decode_alpha_exp_image (layer, FALSE);
          break;
        case FOURCC ('Y','C','G','1'):
          decode_ycocg_image (layer, FALSE);
          break;
        case FOURCC ('Y','C','G','2'):
          decode_ycocg_scaled_image (layer, FALSE);
          break;
        default:
          break;
        }
    }

  return (1);
1171 1172
}

1173 1174 1175 1176 1177 1178 1179 1180 1181
static int
load_mipmaps (FILE *fp,
              dds_header_t    *hdr,
              dds_load_info_t *d,
              gint32           image,
              char            *prefix,
              unsigned int    *l,
              guchar          *pixels,
              unsigned char   *buf)
1182
{
1183
  unsigned int level;
1184

1185
  if ((hdr->flags & DDSD_MIPMAPCOUNT) &&
1186 1187
      (hdr->caps.caps1 & DDSCAPS_MIPMAP) &&
      (dds_read_vals.mipmaps != 0))
1188 1189 1190 1191 1192 1193 1194 1195
    {
      for (level = 1; level < hdr->num_mipmaps; ++level)
        {
          if (!load_layer (fp, hdr, d, image, level, prefix, l, pixels, buf))
            return (0);
        }
    }
  return (1);
1196 1197
}

1198 1199 1200 1201 1202 1203 1204 1205 1206
static int
load_face (FILE            *fp,
           dds_header_t    *hdr,
           dds_load_info_t *d,
           gint32           image,
           char            *prefix,
           unsigned int    *l,
           guchar          *pixels,
           unsigned char   *buf)
1207
{
1208 1209 1210
  if (!load_layer (fp, hdr, d, image, 0, prefix, l, pixels, buf))
    return (0);
  return (load_mipmaps (fp, hdr, d, image, prefix, l, pixels, buf));
1211 1212
}

1213 1214
static unsigned char
color_bits (unsigned int mask)
1215
{
1216
  unsigned char i = 0;
1217

1218 1219 1220
  while (mask)
    {
      if (mask & 1) ++i;
1221
      mask >>= 1;
1222 1223
    }
  return (i);
1224 1225
}

1226 1227
static unsigned char
color_shift (unsigned int mask)
1228
{
1229
  unsigned char i = 0;
1230

1231 1232 1233
  if (!mask) return (0);
  while (!((mask >> i) & 1)) ++i;
  return (i);
1234 1235
}

1236 1237 1238 1239
static void
load_dialog_response (GtkWidget *widget,
                      gint       response_id,
                      gpointer   data)
1240
{
1241 1242 1243 1244 1245 1246 1247 1248
  switch (response_id)
    {
    case GTK_RESPONSE_OK:
      runme = 1;
    default:
      gtk_widget_destroy (widget);
      break;
    }
1249 1250
}

1251 1252 1253
static void
toggle_clicked (GtkWidget *widget,
                gpointer   data)
1254
{
1255 1256
  int *flag = (int*) data;
  (*flag) = !(*flag);
1257 1258
}

1259 1260
static int
load_dialog (void)
1261
{
1262 1263 1264
  GtkWidget *dlg;
  GtkWidget *vbox;
  GtkWidget *check;
1265

1266
  dlg = gimp_dialog_new (_("Load DDS"), "dds", NULL, GTK_WIN_POS_MOUSE,
1267
                         gimp_standard_help_func, LOAD_PROC,
Jehan's avatar
Jehan committed
1268 1269
                         _("Cancel"), GTK_RESPONSE_CANCEL,
                         _("OK"),     GTK_RESPONSE_OK,
1270 1271
                         NULL);

1272
  g_signal_connect (dlg, "response",
Jehan's avatar
Jehan committed
1273 1274
                    G_CALLBACK (load_dialog_response),
                    0);
1275
  g_signal_connect (dlg, "destroy",
Jehan's avatar
Jehan committed
1276 1277
                    G_CALLBACK (gtk_main_quit),
                    0);
1278

1279 1280 1281
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
Jehan's avatar
Jehan committed
1282
                      vbox, 1, 1, 0);
1283
  gtk_widget_show (vbox);
1284

1285 1286 1287
  check = gtk_check_button_new_with_label (_("Load mipmaps"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), dds_read_vals.mipmaps);
  g_signal_connect (check, "clicked",
Jehan's avatar
Jehan committed
1288
                    G_CALLBACK (toggle_clicked), &dds_read_vals.mipmaps);
1289 1290
  gtk_box_pack_start (GTK_BOX (vbox), check, 1, 1, 0);
  gtk_widget_show (check);
1291

1292 1293 1294
  check = gtk_check_button_new_with_label (_("Automatically decode YCoCg/AExp images when detected"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), dds_read_vals.decode_images);
  g_signal_connect (check, "clicked",
Jehan's avatar
Jehan committed
1295
                    G_CALLBACK (toggle_clicked), &dds_read_vals.decode_images);
1296 1297
  gtk_box_pack_start (GTK_BOX (vbox), check, 1, 1, 0);
  gtk_widget_show (check);
1298

1299
  gtk_widget_show (dlg);
1300

1301
  runme = 0;
1302

1303
  gtk_main ();
1304

1305
  return (runme);
1306
}