raw.c 35.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* Raw image loader (and saver) plugin 3.4
 *
 * by tim copperfield [timecop@japan.co.jp]
 * http://www.ne.jp/asahi/linux/timecop
 *
 * Updated for Gimp 2.1 by pg@futureware.at and mitch@gimp.org
 *
 * This plugin is not based on any other plugin.
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"

#include <errno.h>
#include <string.h>
29 30

#include <fcntl.h>
31 32
#include <sys/stat.h>
#include <sys/types.h>
33 34

#ifdef HAVE_UNISTD_H
35
#include <unistd.h>
36 37 38
#endif

#include <glib/gstdio.h>
39

40 41 42 43
#ifdef G_OS_WIN32
#include <io.h>
#endif

44 45 46 47 48 49
#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"

#include "libgimp/stdplugins-intl.h"


50 51 52 53
#define LOAD_PROC      "file-raw-load"
#define SAVE_PROC      "file-raw-save"
#define PLUG_IN_BINARY "raw"
#define PREVIEW_SIZE   350
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 115 116 117 118 119 120


typedef enum
{
  RAW_RGB,                      /* RGB Image */
  RAW_RGBA,                     /* RGB Image with an Alpha channel */
  RAW_PLANAR,                   /* Planar RGB */
  RAW_INDEXED                   /* Indexed image */
} RawType;

typedef enum
{
  RAW_PALETTE_RGB,              /* standard RGB */
  RAW_PALETTE_BGR               /* Windows BGRX */
} RawPaletteType;

typedef struct
{
  gint32         file_offset;    /* offset to beginning of image in raw data */
  gint32         image_width;    /* width of the raw image                   */
  gint32         image_height;   /* height of the raw image                  */
  RawType        image_type;     /* type of image (RGB, INDEXED, etc)        */
  gint32         palette_offset; /* offset inside the palette file, if any   */
  RawPaletteType palette_type;   /* type of palette (RGB/BGR)                */
} RawConfig;

typedef struct
{
  FILE         *fp;        /* pointer to the already open file */
  GimpDrawable *drawable;  /* gimp drawable                    */
  GimpPixelRgn  region;    /* gimp pixel region                */
  gint32        image_id;  /* gimp image id                    */
  guchar        cmap[768]; /* color map for indexed images     */
} RawGimpData;


static void              query             (void);
static void              run               (const gchar      *name,
                                            gint              nparams,
                                            const GimpParam  *param,
                                            gint             *nreturn_vals,
                                            GimpParam       **return_vals);

/* prototypes for the new load functions */
static gboolean          raw_load_standard (RawGimpData      *data,
                                            gint              bpp);
static gboolean          raw_load_planar   (RawGimpData      *data);
static gboolean          raw_load_palette  (RawGimpData      *data,
                                            gchar            *palette_filename);

/* support functions */
static gint32            get_file_info     (gchar            *filename);
static void              raw_read_row      (FILE             *fp,
                                            guchar           *buf,
                                            gint32            offset,
                                            gint32            size);
static int               mmap_read         (gint              fd,
                                            gpointer          buf,
                                            gint32            len,
                                            gint32            pos,
                                            gint              rowstride);
static gint32            load_image        (gchar            *filename);
static GimpPDBStatusType save_image        (gchar            *filename,
                                            gint32            image_id,
                                            gint32            drawable_id);

/* gui functions */
Michael Natterer's avatar
Michael Natterer committed
121 122
static void              preview_update    (GimpPreviewArea   *preview);
static void              palette_update    (GimpPreviewArea   *preview);
123 124 125 126 127
static gboolean          load_dialog       (gchar             *name);
static gboolean          save_dialog       (gchar             *filename,
                                            gint32             image_id,
                                            gint32             drawable_id);
static void              palette_callback  (GimpFileEntry     *file_entry,
Michael Natterer's avatar
Michael Natterer committed
128
                                            GimpPreviewArea   *preview);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152


static RawConfig *runtime             = NULL;
static gchar     *palfile             = NULL;
static gint       preview_fd          = -1;
static guchar     preview_cmap[1024];
static gboolean   preview_cmap_update = TRUE;


GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,   /* init_proc  */
  NULL,   /* quit_proc  */
  query,  /* query_proc */
  run,    /* run_proc   */
};

MAIN()

static void
query (void)
{
  static GimpParamDef load_args[] =
  {
153
    { GIMP_PDB_INT32,  "run-mode",     "Interactive"                  },
154
    { GIMP_PDB_STRING, "filename",     "The name of the file to load" },
155
    { GIMP_PDB_STRING, "raw-filename", "The name entered"             }
156 157 158 159 160 161 162 163 164
  };

  static GimpParamDef load_return_vals[] =
  {
    { GIMP_PDB_IMAGE, "image", "Output image" }
  };

  static GimpParamDef save_args[] =
  {
165
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },
166 167 168
    { GIMP_PDB_IMAGE,    "image",        "Input image"                  },
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save"             },
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
169
    { GIMP_PDB_STRING,   "raw-filename", "The name entered"             }
170 171
  };

172
  gimp_install_procedure (LOAD_PROC,
173 174 175 176 177 178 179 180 181 182 183 184
                          "Load raw images, specifying image information",
                          "Load raw images, specifying image information",
                          "timecop, pg@futureware.at",
                          "timecop, pg@futureware.at",
                          "Aug 2004",
                          N_("Raw Image Data"),
                          NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (load_args),
                          G_N_ELEMENTS (load_return_vals),
                          load_args, load_return_vals);

185
  gimp_register_load_handler (LOAD_PROC, "", "");
186

187
  gimp_install_procedure (SAVE_PROC,
188 189 190 191 192 193 194 195 196 197 198
                          "Dump images to disk in raw format",
                          "Dump images to disk in raw format",
                          "timecop, pg@futureware.at",
                          "timecop, pg@futureware.at",
                          "Aug 2004",
                          N_("Raw Image Data"),
                          "INDEXED, GRAY, RGB, RGBA",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (save_args), 0,
                          save_args, NULL);

199
  gimp_register_save_handler (SAVE_PROC, "", "");
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
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam  values[2];
  GimpRunMode       run_mode;
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  gint32            image_id;
  gint32            drawable_id;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;

  /* allocate config structure and fill with defaults */
  runtime = g_new0 (RawConfig, 1);

  runtime->file_offset    = 0;
  runtime->image_width    = PREVIEW_SIZE;
  runtime->image_height   = PREVIEW_SIZE;
  runtime->palette_offset = 0;
  runtime->palette_type   = RAW_PALETTE_RGB;

232
  if (strcmp (name, LOAD_PROC) == 0)
233 234 235
    {
      if (run_mode == GIMP_RUN_INTERACTIVE)
        {
236
          gimp_get_data (LOAD_PROC, runtime);
237

238
          preview_fd = g_open (param[1].data.d_string, O_RDONLY, 0);
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

          if (preview_fd < 0)
            {
              g_message (_("Could not open '%s' for reading: %s"),
                         gimp_filename_to_utf8 (param[1].data.d_string),
                         g_strerror (errno));
              status = GIMP_PDB_EXECUTION_ERROR;
            }
          else
            {
              if (! load_dialog (param[1].data.d_string))
                status = GIMP_PDB_CANCEL;

              close (preview_fd);
            }
        }
      else
        {
          /* we only run interactively due to the nature of this plugin.
           * things like generate preview etc like to call us non-
           * interactively.  here we stop that.
           */
          status = GIMP_PDB_CALLING_ERROR;
        }

      /* we are okay, and the user clicked OK in the load dialog */
      if (status == GIMP_PDB_SUCCESS)
        {
          image_id = load_image (param[1].data.d_string);

          if (image_id != -1)
            {
271
              gimp_set_data (LOAD_PROC, runtime, sizeof (RawConfig));
272 273 274 275 276 277 278 279 280 281 282

              *nreturn_vals = 2;
              values[1].type         = GIMP_PDB_IMAGE;
              values[1].data.d_image = image_id;
            }
          else
            {
              status = GIMP_PDB_CALLING_ERROR;
            }
        }
    }
283
  else if (strcmp (name, SAVE_PROC) == 0)
284 285 286 287 288 289
    {
      image_id    = param[1].data.d_int32;
      drawable_id = param[2].data.d_int32;

      if (run_mode == GIMP_RUN_INTERACTIVE)
        {
290
          gimp_get_data (SAVE_PROC, runtime);
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 325

          if (nparams != 5)
            {
              status = GIMP_PDB_CALLING_ERROR;
            }
          else if (! save_dialog (param[3].data.d_string, image_id, drawable_id))
            {
              status = GIMP_PDB_CANCEL;
            }
        }
      else
        {
          /* we want to make sure we always do save-as for raw images
           * to avoid confusion and data loss
           */
          status = GIMP_PDB_CALLING_ERROR;
        }

      if (status == GIMP_PDB_SUCCESS)
        {
          status = save_image (param[3].data.d_string, image_id, drawable_id);
        }
    }

  values[0].data.d_status = status;
  g_free (runtime);
}


/* get file size from a filename */
static gint32
get_file_info (gchar *filename)
{
  struct stat status;

326
  g_stat (filename, &status);
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 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

  return status.st_size;
}

/* new image handle functions */
static void
raw_read_row (FILE   *fp,
              guchar *buf,
              gint32  offset,
              gint32  size)
{
  fseek (fp, offset, SEEK_SET);

  if (! fread (buf, size, 1, fp))
    {
      g_printerr ("fread failed\n");
      memset (buf, 0xFF, size);
    }
}

/* similar to the above function but memset is done differently. has nothing
 * to do with mmap, by the way
 */
static gint
mmap_read (int     fd,
           void   *buf,
           gint32  len,
           gint32  pos,
           gint    rowstride)
{
  lseek (fd, pos, SEEK_SET);
  if (! read (fd, buf, len))
    memset (buf, 0xFF, rowstride);
  return 0;
}

/* this handles 1, 3, 4 bpp "standard" images */
static gboolean
raw_load_standard (RawGimpData *data,
                   gint         bpp)
{
  guchar *row = NULL;

  row = g_try_malloc (runtime->image_width * runtime->image_height * bpp);
  if (! row)
    return FALSE;

  raw_read_row (data->fp, row, runtime->file_offset,
                (runtime->image_width * runtime->image_height * bpp));
  gimp_pixel_rgn_set_rect (&data->region, row,
                           0, 0, runtime->image_width, runtime->image_height);
  g_free (row);
  return TRUE;
}

/* this handles 3 bpp "planar" images */
static gboolean
raw_load_planar (RawGimpData *data)
{
  gint32  r_offset, g_offset, b_offset, i, j, k;
  guchar *r_row, *b_row, *g_row, *row;
  gint    bpp = 3; /* adding support for alpha channel should be easy */

  /* red, green, blue rows temporary data */
  r_row = g_malloc (runtime->image_width);
  g_row = g_malloc (runtime->image_width);
  b_row = g_malloc (runtime->image_width);

  /* row for the pixel region, after combining RGB together */
  row = g_malloc (runtime->image_width * bpp);

  r_offset = runtime->file_offset;
  g_offset = r_offset + runtime->image_width * runtime->image_height;
  b_offset = g_offset + runtime->image_width * runtime->image_height;

  for (i = 0; i < runtime->image_height; i++)
    {
      /* Read R, G, B rows */
      raw_read_row (data->fp, r_row, r_offset + (runtime->image_width * i),
                    runtime->image_width);
      raw_read_row (data->fp, g_row, g_offset + (runtime->image_width * i),
                    runtime->image_width);
      raw_read_row (data->fp, b_row, b_offset + (runtime->image_width * i),
                    runtime->image_width);

      /* Combine separate R, G and B rows into RGB triples */
      for (j = 0, k = 0; j < runtime->image_width; j++)
        {
          row[k++] = r_row[j];
          row[k++] = g_row[j];
          row[k++] = b_row[j];
        }

      gimp_pixel_rgn_set_row (&data->region, row, 0, i, runtime->image_width);
      gimp_progress_update ((gfloat) i / (gfloat) runtime->image_height);
    }

  g_free (row);
  g_free (r_row);
  g_free (g_row);
  g_free (b_row);

  return TRUE;
}

static gboolean
raw_load_palette (RawGimpData *data,
                  gchar       *palette_file)
{
  guchar temp[1024];
  gint   fd, i, j;

  if (palette_file)
    {
441
      fd = g_open (palette_file, O_RDONLY, 0);
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 474 475 476 477

      if (! fd)
        return FALSE;

      lseek (fd, runtime->palette_offset, SEEK_SET);

      switch (runtime->palette_type)
        {
        case RAW_PALETTE_RGB:
          read (fd, data->cmap, 768);
          break;

        case RAW_PALETTE_BGR:
          read (fd, temp, 1024);
          for (i = 0, j = 0; i < 256; i++)
            {
              data->cmap[j++] = temp[i * 4 + 2];
              data->cmap[j++] = temp[i * 4 + 1];
              data->cmap[j++] = temp[i * 4];
            }
          break;
        }

      close (fd);
    }
  else
    {
      /* make a fake grayscale color map */
      for (i = 0, j = 0; i < 256; i++)
        {
          data->cmap[j++] = i;
          data->cmap[j++] = i;
          data->cmap[j++] = i;
        }
    }

478
  gimp_image_set_colormap (data->image_id, data->cmap, 256);
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

  return TRUE;
}

/* end new image handle functions */

static GimpPDBStatusType
save_image (gchar  *filename,
            gint32  image_id,
            gint32  drawable_id)
{
  GimpDrawable     *drawable;
  GimpPixelRgn      pixel_rgn;
  guchar           *cmap = NULL;  /* colormap for indexed images */
  guchar           *buf;
  guchar           *red, *green, *blue, *alpha = NULL;
  gint32            width, height, bpp = 0;
  gboolean          have_alpha = 0;
  FILE             *fp;
  gint              i, j = 0;
  gint              palsize = 0;
  GimpPDBStatusType ret = GIMP_PDB_EXECUTION_ERROR;

  /* get info about the current image */
  drawable = gimp_drawable_get (drawable_id);

  bpp        = gimp_drawable_bpp (drawable_id);
  have_alpha = gimp_drawable_has_alpha (drawable_id);

  if (gimp_drawable_is_indexed (drawable_id))
    {
      bpp  = 1;
511
      cmap = gimp_image_get_colormap (image_id, &palsize);
512 513 514 515 516 517 518 519 520 521 522 523 524
    }

  width  = drawable->width;
  height = drawable->height;

  gimp_pixel_rgn_init (&pixel_rgn, drawable,
                       0, 0, drawable->width, drawable->height,
                       FALSE, FALSE);

  buf = g_new (guchar, width * height * bpp);

  gimp_pixel_rgn_get_rect (&pixel_rgn, buf, 0, 0, width, height);

525
  fp = g_fopen (filename, "wb");
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

  if (! fp)
    {
      g_message (_("Could not open '%s' for writing: %s"),
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
      return GIMP_PDB_EXECUTION_ERROR;
    }

  ret = GIMP_PDB_SUCCESS;

  switch (runtime->image_type)
    {
    case RAW_RGB:
      if (! fwrite (buf, width * height * bpp, 1, fp))
        {
          return GIMP_PDB_EXECUTION_ERROR;
        }

      fclose (fp);

      if (cmap)
        {
          /* we have a colormap, too.write it into filename+pal */
          gchar *newfile = g_strconcat (filename, ".pal", NULL);
          gchar *temp;

552
          fp = g_fopen (newfile, "wb");
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 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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

          if (! fp)
            {
              g_message (_("Could not open '%s' for writing: %s"),
                         gimp_filename_to_utf8 (newfile), g_strerror (errno));
              return GIMP_PDB_EXECUTION_ERROR;
            }

          switch (runtime->palette_type)
            {
            case RAW_PALETTE_RGB:
              if (!fwrite (cmap, palsize * 3, 1, fp))
                ret = GIMP_PDB_EXECUTION_ERROR;
              fclose (fp);
              break;

            case RAW_PALETTE_BGR:
              temp = g_malloc0 (palsize * 4);
              for (i = 0, j = 0; i < palsize * 3; i += 3)
                {
                  temp[j++] = cmap[i + 2];
                  temp[j++] = cmap[i + 1];
                  temp[j++] = cmap[i];
                  temp[j++] = 0;
                }
              if (!fwrite (temp, palsize * 4, 1, fp))
                ret = GIMP_PDB_EXECUTION_ERROR;
              fclose (fp);
              g_free (temp);
              break;
            }
        }
      break;

    case RAW_PLANAR:
      red   = g_new (guchar, width * height);
      green = g_new (guchar, width * height);
      blue  = g_new (guchar, width * height);
      if (have_alpha)
        alpha = g_new (guchar, width * height);

      for (i = 0; i < width * height * bpp; i += bpp)
        {
          red[j]   = buf[i];
          green[j] = buf[i + 1];
          blue[j]  = buf[i + 2];
          if (have_alpha)
            alpha[j] = buf[i + 3];
          j++;
        }

      ret = GIMP_PDB_SUCCESS;
      if (!fwrite (red, width * height, 1, fp))
        ret = GIMP_PDB_EXECUTION_ERROR;
      if (!fwrite (green, width * height, 1, fp))
        ret = GIMP_PDB_EXECUTION_ERROR;
      if (!fwrite (blue, width * height, 1, fp))
        ret = GIMP_PDB_EXECUTION_ERROR;
      if (have_alpha)
        {
          if (!fwrite (alpha, width * height, 1, fp))
            ret = GIMP_PDB_EXECUTION_ERROR;
        }
      g_free (red);
      g_free (green);
      g_free (blue);
      if (have_alpha)
        g_free (alpha);
      fclose (fp);
      break;

    default:
      break;
    }

  return ret;
}

static gint32
load_image (gchar *filename)
{
  RawGimpData       *data;
  gint32             layer_id = -1;
  GimpImageType      ltype    = GIMP_RGB;
  GimpImageBaseType  itype    = GIMP_RGB_IMAGE;
  gint32             size;
  gint               bpp = 0;

  data = g_new0 (RawGimpData, 1);

643
  data->fp = g_fopen (filename, "rb");
644 645 646 647 648 649 650
  if (! data->fp)
    {
      g_message (_("Could not open '%s' for reading: %s"),
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
      return -1;
    }

Sven Neumann's avatar
Sven Neumann committed
651 652 653
  gimp_progress_init (NULL);
  gimp_progress_set_text (_("Opening '%s'..."),
                          gimp_filename_to_utf8 (filename));
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

  size = get_file_info (filename);

  switch (runtime->image_type)
    {
    case RAW_RGB:             /* standard RGB */
    case RAW_PLANAR:          /* planar RGB */
      bpp   = 3;
      ltype = GIMP_RGB_IMAGE;
      itype = GIMP_RGB;
      break;

    case RAW_RGBA:            /* RGB + alpha */
      bpp   = 4;
      ltype = GIMP_RGBA_IMAGE;
      itype = GIMP_RGB;
      break;

    case RAW_INDEXED:         /* Indexed */
      bpp   = 1;
      ltype = GIMP_INDEXED_IMAGE;
      itype = GIMP_INDEXED;
      break;
    }

  /* make sure we don't load image bigger than file size */
  if (runtime->image_height > (size / runtime->image_width / bpp))
    runtime->image_height = size / runtime->image_width / bpp;

  data->image_id = gimp_image_new (runtime->image_width,
                                   runtime->image_height,
                                   itype);
  gimp_image_set_filename(data->image_id, filename);
  layer_id = gimp_layer_new (data->image_id, _("Background"),
                             runtime->image_width, runtime->image_height, ltype,
                             100, GIMP_NORMAL_MODE);
  gimp_image_add_layer (data->image_id, layer_id, 0);

  data->drawable = gimp_drawable_get (layer_id);

  gimp_pixel_rgn_init (&data->region, data->drawable,
                       0, 0, runtime->image_width, runtime->image_height,
                       TRUE, FALSE);

  switch (runtime->image_type)
    {
    case RAW_RGB:
    case RAW_RGBA:
      raw_load_standard (data, bpp);
      break;

    case RAW_PLANAR:
      raw_load_planar (data);
      break;

    case RAW_INDEXED:
      raw_load_palette (data, palfile);
      raw_load_standard (data, bpp);
      break;
    }

  fclose (data->fp);

  gimp_drawable_flush (data->drawable);
  gimp_drawable_detach (data->drawable);

  return data->image_id;
}


/* misc GUI stuff */

static void
Michael Natterer's avatar
Michael Natterer committed
727
preview_update (GimpPreviewArea *preview)
728
{
Michael Natterer's avatar
Michael Natterer committed
729 730 731 732 733 734 735 736 737 738
  gint     width;
  gint     height;
  gint32   pos, len;
  gint     x, y;

  width  = MIN (runtime->image_width,  preview->width);
  height = MIN (runtime->image_height, preview->height);

  gimp_preview_area_fill (preview, 0, 0, preview->width, preview->height,
                          255, 255, 255);
739 740 741 742 743

  switch (runtime->image_type)
    {
    case RAW_RGB:
      /* standard RGB image */
Michael Natterer's avatar
Michael Natterer committed
744 745
      {
        guchar *row = g_malloc0 (width * 3);
746

Michael Natterer's avatar
Michael Natterer committed
747
        len = width * 3;
748

Michael Natterer's avatar
Michael Natterer committed
749 750 751 752
        for (y = 0; y < height; y++)
          {
            pos = runtime->file_offset + runtime->image_width * y * 3;
            mmap_read (preview_fd, row, len, pos, width * 3);
753

Michael Natterer's avatar
Michael Natterer committed
754 755 756
            gimp_preview_area_draw (preview, 0, y, width, 1,
                                    GIMP_RGB_IMAGE, row, width * 3);
          }
757

Michael Natterer's avatar
Michael Natterer committed
758 759 760
        g_free (row);
      }
      break;
761

Michael Natterer's avatar
Michael Natterer committed
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
    case RAW_PLANAR:
      {
        guchar *r_row = g_malloc0 (width);
        guchar *g_row = g_malloc0 (width);
        guchar *b_row = g_malloc0 (width);
        guchar *row   = g_malloc0 (width * 3);

        for (y = 0; y < height; y++)
          {
            gint j, k;

            pos = (runtime->file_offset +
                   (y * runtime->image_width));
            mmap_read (preview_fd, r_row, width, pos, width);

            pos = (runtime->file_offset +
                   (runtime->image_width * (runtime->image_height + y)));
            mmap_read (preview_fd, g_row, width, pos, width);

            pos = (runtime->file_offset +
                   (runtime->image_width * (runtime->image_height * 2 + y)));
            mmap_read (preview_fd, b_row, width, pos, width);

            for (j = 0, k = 0; j < width; j++)
              {
                row[k++] = r_row[j];
                row[k++] = g_row[j];
                row[k++] = b_row[j];
              }

            gimp_preview_area_draw (preview, 0, y, width, 1,
                                    GIMP_RGB_IMAGE, row, width * 3);
          }

        g_free (b_row);
        g_free (g_row);
        g_free (r_row);
        g_free (row);
      }
801 802 803 804
      break;

    case RAW_RGBA:
      /* RGB + alpha image */
Michael Natterer's avatar
Michael Natterer committed
805 806
      {
        guchar *row = g_malloc0 (width * 4);
807

Michael Natterer's avatar
Michael Natterer committed
808
        len = width * 4;
809

Michael Natterer's avatar
Michael Natterer committed
810 811 812 813
        for (y = 0; y < height; y++)
          {
            pos = runtime->file_offset + runtime->image_width * y * 4;
            mmap_read (preview_fd, row, len, pos, width * 4);
814

Michael Natterer's avatar
Michael Natterer committed
815 816 817
            gimp_preview_area_draw (preview, 0, y, width, 1,
                                    GIMP_RGBA_IMAGE, row, width * 4);
          }
818

Michael Natterer's avatar
Michael Natterer committed
819 820
        g_free (row);
      }
821 822 823 824
      break;

    case RAW_INDEXED:
      /* indexed image */
Michael Natterer's avatar
Michael Natterer committed
825 826 827 828 829 830 831 832 833 834 835 836
      {
        guchar *index = g_malloc0 (width);
        guchar *row   = g_malloc0 (width * 3);

        len = width;

        if (preview_cmap_update)
          {
            if (palfile)
              {
                gint fd;

837
                fd = g_open (palfile, O_RDONLY, 0);
Michael Natterer's avatar
Michael Natterer committed
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 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
                lseek (fd, runtime->palette_offset, SEEK_SET);
                read (fd, preview_cmap,
                      (runtime->palette_type == RAW_PALETTE_RGB) ? 768 : 1024);
                close (fd);
              }
            else
              {
                /* make fake palette, maybe overwrite it later */
                for (y = 0, x = 0; y < 256; y++)
                  {
                    preview_cmap[x++] = y;
                    preview_cmap[x++] = y;
                    if (runtime->palette_type == RAW_PALETTE_RGB)
                      {
                        preview_cmap[x++] = y;
                      }
                    else
                      {
                        preview_cmap[x++] = y;
                        preview_cmap[x++] = 0;
                      }
                  }
              }

            preview_cmap_update = FALSE;
          }

        for (y = 0; y < height; y++)
          {
            guchar *p = row;

            pos = runtime->file_offset + runtime->image_width * y;
            mmap_read (preview_fd, index, len, pos, width);

            for (x = 0; x < width; x++)
              {
                switch (runtime->palette_type)
                  {
                  case RAW_PALETTE_RGB:
                    *p++ = preview_cmap[index[x] * 3];
                    *p++ = preview_cmap[index[x] * 3 + 1];
                    *p++ = preview_cmap[index[x] * 3 + 2];
                    break;
                  case RAW_PALETTE_BGR:
                    *p++ = preview_cmap[index[x] * 4 + 2];
                    *p++ = preview_cmap[index[x] * 4 + 1];
                    *p++ = preview_cmap[index[x] * 4];
                    break;
                  }
              }

            gimp_preview_area_draw (preview, 0, y, width, 1,
                                    GIMP_RGB_IMAGE, row, width * 3);
          }

        g_free (index);
      }
895 896 897 898 899
      break;
    }
}

static void
Michael Natterer's avatar
Michael Natterer committed
900
palette_update (GimpPreviewArea *preview)
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
{
  preview_cmap_update = TRUE;

  preview_update (preview);
}

static gboolean
load_dialog (gchar *filename)
{
  GtkWidget *dialog;
  GtkWidget *main_vbox;
  GtkWidget *preview;
  GtkWidget *table;
  GtkWidget *frame;
  GtkWidget *combo;
  GtkObject *adj;
  GtkWidget *entry;
  gint32     size;
  gboolean   run;

  size = get_file_info (filename);

923
  gimp_ui_init (PLUG_IN_BINARY, TRUE);
924

925
  dialog = gimp_dialog_new (_("Load Image from Raw Data"), PLUG_IN_BINARY,
926
                            NULL, 0,
927
                            gimp_standard_help_func, LOAD_PROC,
928 929

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
930
                            GTK_STOCK_OPEN,   GTK_RESPONSE_OK,
931 932 933

                            NULL);

934
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
935 936 937
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
938

939 940
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
Michael Natterer's avatar
Michael Natterer committed
941
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
942 943 944 945
  gtk_widget_show (main_vbox);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Michael Natterer's avatar
Michael Natterer committed
946
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, TRUE, TRUE, 0);
947 948
  gtk_widget_show (frame);

Michael Natterer's avatar
Michael Natterer committed
949 950
  preview = gimp_preview_area_new ();
  gtk_widget_set_size_request (preview, PREVIEW_SIZE, PREVIEW_SIZE);
951 952 953
  gtk_container_add (GTK_CONTAINER (frame), preview);
  gtk_widget_show (preview);

Michael Natterer's avatar
Michael Natterer committed
954 955 956
  g_signal_connect_after (preview, "size_allocate",
                          G_CALLBACK (preview_update),
                          NULL);
957 958 959 960 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

  frame = gimp_frame_new (_("Image"));
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  table = gtk_table_new (4, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);

  combo = gimp_int_combo_box_new (_("RGB"),        RAW_RGB,
                                  _("RGBA"),       RAW_RGBA,
                                  _("Planar RGB"), RAW_PLANAR,
                                  _("Indexed"),    RAW_INDEXED,
                                  NULL);
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (combo),
                                 runtime->image_type);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("Image _Type:"), 0.0, 0.5,
                             combo, 2, FALSE);

  g_signal_connect (combo, "changed",
                    G_CALLBACK (gimp_int_combo_box_get_active),
                    &runtime->image_type);
  g_signal_connect_swapped (combo, "changed",
                            G_CALLBACK (preview_update),
                            preview);

  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
                              _("O_ffset:"), -1, 9,
                              runtime->file_offset, 0, size, 1, 1000, 0,
                              TRUE, 0.0, 0.0,
                              NULL, NULL);

992
  g_signal_connect (adj, "value-changed",
993 994
                    G_CALLBACK (gimp_int_adjustment_update),
                    &runtime->file_offset);
995
  g_signal_connect_swapped (adj, "value-changed",
996 997 998 999 1000 1001 1002 1003 1004
                            G_CALLBACK (preview_update),
                            preview);

  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
                              _("_Width:"), -1, 9,
                              runtime->image_width, 1, 4096, 1, 10, 0,
                              TRUE, 0.0, 0.0,
                              NULL, NULL);

1005
  g_signal_connect (adj, "value-changed",
1006 1007
                    G_CALLBACK (gimp_int_adjustment_update),
                    &runtime->image_width);
1008
  g_signal_connect_swapped (adj, "value-changed",
1009 1010 1011 1012 1013 1014 1015 1016 1017
                            G_CALLBACK (preview_update),
                            preview);

  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 3,
                              _("_Height:"), -1, 9,
                              runtime->image_height, 1, 4096, 1, 10, 0,
                              TRUE, 0.0, 0.0,
                              NULL, NULL);

1018
  g_signal_connect (adj, "value-changed",
1019 1020
                    G_CALLBACK (gimp_int_adjustment_update),
                    &runtime->image_height);
1021
  g_signal_connect_swapped (adj, "value-changed",
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
                            G_CALLBACK (preview_update),
                            preview);


  frame = gimp_frame_new (_("Palette"));
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  table = gtk_table_new (3, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  gtk_container_add (GTK_CONTAINER (frame), table);
  gtk_widget_show (table);

  combo = gimp_int_combo_box_new (_("R, G, B (normal)"),       RAW_PALETTE_RGB,
                                  _("B, G, R, X (bmp style)"), RAW_PALETTE_BGR,
                                  NULL);
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (combo),
                                 runtime->palette_type);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("_Palette Type:"), 0.0, 0.5,
                             combo, 2, FALSE);

  g_signal_connect (combo, "changed",
                    G_CALLBACK (gimp_int_combo_box_get_active),
                    &runtime->palette_type);
  g_signal_connect_swapped (combo, "changed",
                            G_CALLBACK (palette_update),
                            preview);

  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
                              _("Off_set:"), -1, 0,
                              runtime->palette_offset, 0, 1 << 24, 1, 768, 0,
                              TRUE, 0.0, 0.0,
                              NULL, NULL);

1058
  g_signal_connect (adj, "value-changed",
1059 1060
                    G_CALLBACK (gimp_int_adjustment_update),
                    &runtime->palette_offset);
1061
  g_signal_connect_swapped (adj, "value-changed",
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
                            G_CALLBACK (palette_update),
                            preview);

  entry = gimp_file_entry_new (_("Select Palette File to Load"),
                               palfile, FALSE, TRUE);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
                             _("Pal_ette File:"), 0.0, 0.5,
                             entry, 2, FALSE);

  g_signal_connect (entry, "filename_changed",
                    G_CALLBACK (palette_callback),
                    preview);

  gtk_widget_show (dialog);

  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);

  return run;
}

static gboolean
save_dialog (gchar * filename,
             gint32 image_id,
             gint32 drawable_id)
{
  GtkWidget *dialog;
  GtkWidget *main_vbox;
  GtkWidget *frame;
  gboolean   run;

1094
  gimp_ui_init (PLUG_IN_BINARY, TRUE);
1095

1096 1097 1098
  dialog = gimp_dialog_new (_("Raw Image Save"), PLUG_IN_BINARY,
                            NULL, 0,
                            gimp_standard_help_func, SAVE_PROC,
1099 1100 1101 1102 1103 1104

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,

                            NULL);

1105 1106 1107 1108 1109
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

1110 1111
  gimp_window_set_transient (GTK_WINDOW (dialog));

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 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
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), main_vbox,
                      FALSE, FALSE, 0);
  gtk_widget_show (main_vbox);

  frame = gimp_int_radio_group_new (TRUE, _("RGB Save Type"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &runtime->image_type,
                                    runtime->image_type,
                                    _("Standard (R,G,B)"),     RAW_RGB,    NULL,
                                    _("Planar (RRR,GGG,BBB)"), RAW_PLANAR, NULL,
                                    NULL);
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  frame = gimp_int_radio_group_new (TRUE, _("Indexed Palette Type"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &runtime->palette_type,
                                    runtime->palette_type,
                                    _("R, G, B (normal)"),
                                    RAW_PALETTE_RGB, NULL,
                                    _("B, G, R, X (bmp style)"),
                                    RAW_PALETTE_BGR, NULL,
                                    NULL);
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  gtk_widget_show (dialog);

  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);

  return run;
}

static void
Michael Natterer's avatar
Michael Natterer committed
1150 1151
palette_callback (GimpFileEntry   *file_entry,
                  GimpPreviewArea *preview)
1152 1153 1154 1155 1156 1157 1158 1159
{
  if (palfile)
    g_free (palfile);

  palfile = gimp_file_entry_get_filename (file_entry);

  palette_update (preview);
}