Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Campbell Barton d869b5
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
Toshihiro Shimizu 890ddd
#define _CRT_SECURE_NO_DEPRECATE 1
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
#include "tiio_sgi.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tpixelgr.h"
Toshihiro Shimizu 890ddd
#include "../compatibility/tfile_io.h"
Toshihiro Shimizu 890ddd
#ifdef LINUX
Toshihiro Shimizu 890ddd
//#define _XOPEN_SOURCE_EXTENDED
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <errno.h></errno.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys stat.h=""></sys>
Toshihiro Shimizu 890ddd
#include <fcntl.h></fcntl.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(MACOSX)
Toshihiro Shimizu 890ddd
#include <sys malloc.h=""></sys>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <malloc.h></malloc.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <io.h></io.h>
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifndef LINUX
Toshihiro Shimizu 890ddd
#include <strings.h></strings.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <stdarg.h></stdarg.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// IMAGERGB:
Toshihiro Shimizu 890ddd
// header file per le immagini sgi
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
struct IMAGERGB {
Shinya Kitaoka 120a6e
  unsigned short imagic; /* stuff saved on disk . . */
Shinya Kitaoka 120a6e
  unsigned short type;
Shinya Kitaoka 120a6e
  unsigned short dim;
Shinya Kitaoka 120a6e
  unsigned short xsize;
Shinya Kitaoka 120a6e
  unsigned short ysize;
Shinya Kitaoka 120a6e
  unsigned short zsize;
Shinya Kitaoka 120a6e
  TUINT32 min;
Shinya Kitaoka 120a6e
  TUINT32 max;
Shinya Kitaoka 120a6e
  TUINT32 wastebytes;
Shinya Kitaoka 120a6e
  char name[80];
Shinya Kitaoka 120a6e
  TUINT32 colormap;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TINT32 file; /* stuff used in core only */
Shinya Kitaoka 120a6e
  unsigned short flags;
Shinya Kitaoka 120a6e
  short dorev;
Shinya Kitaoka 120a6e
  short x;
Shinya Kitaoka 120a6e
  short y;
Shinya Kitaoka 120a6e
  short z;
Shinya Kitaoka 120a6e
  short cnt;
Shinya Kitaoka 120a6e
  unsigned short *ptr;
Shinya Kitaoka 120a6e
  unsigned short *base;
Shinya Kitaoka 120a6e
  unsigned short *tmpbuf;
Shinya Kitaoka 120a6e
  TUINT32 offset;
Shinya Kitaoka 120a6e
  TUINT32 rleend;    /* for rle images */
Shinya Kitaoka 120a6e
  TUINT32 *rowstart; /* for rle images */
Shinya Kitaoka 120a6e
  TINT32 *rowsize;   /* for rle images */
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int IMAGERGB_HEADER_SIZE = sizeof(IMAGERGB);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// macro definite nel vecchio ImageP/filergb.h
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
#define IMAGIC 0732
Toshihiro Shimizu 890ddd
#define TYPEMASK 0xff00
Toshihiro Shimizu 890ddd
#define ITYPE_RLE 0x0100
Toshihiro Shimizu 890ddd
#define ISRLE(type) (((type)&0xff00) == ITYPE_RLE)
Toshihiro Shimizu 890ddd
#define ITYPE_VERBATIM 0x0000
Toshihiro Shimizu 890ddd
#define ISVERBATIM(type) (((type)&0xff00) == ITYPE_VERBATIM)
Toshihiro Shimizu 890ddd
#define BPPMASK 0x00ff
Toshihiro Shimizu 890ddd
#define BPP(type) ((type)&BPPMASK)
Toshihiro Shimizu 890ddd
#define RLE(bpp) (ITYPE_RLE | (bpp))
Toshihiro Shimizu 890ddd
#define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp))
Toshihiro Shimizu 890ddd
#define IBUFSIZE(pixels) ((pixels + (pixels >> 6)) << 2)
Toshihiro Shimizu 890ddd
#define RLE_NOP 0x00
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef _IORW
Toshihiro Shimizu 890ddd
#define _IOREAD 0x1
Toshihiro Shimizu 890ddd
#define _IOWRT 0x2
Toshihiro Shimizu 890ddd
#define _IORW 0x80
Toshihiro Shimizu 890ddd
#define _IOERR 0x20
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 4c5bd5
static USHORT *ibufalloc(IMAGERGB *image, int bpp);
Shinya Kitaoka 4c5bd5
static void cvtshorts(USHORT buffer[], TINT32 n);
Shinya Kitaoka 4c5bd5
static void cvtTINT32s(TUINT32 *buffer, TINT32 n);
Toshihiro Shimizu 890ddd
static void cvtimage(IMAGERGB *image);
Toshihiro Shimizu 890ddd
static void img_rle_expand(USHORT *rlebuf, int ibpp, USHORT *expbuf, int obpp);
Shinya Kitaoka 4c5bd5
static int img_getrowsize(IMAGERGB *image);
Toshihiro Shimizu 890ddd
static TUINT32 img_optseek(IMAGERGB *image, TUINT32 offset);
Toshihiro Shimizu 890ddd
static TINT32 rgb_img_read(IMAGERGB *image, char *buffer, TINT32 count);
Toshihiro Shimizu 890ddd
static int img_badrow(IMAGERGB *image, int y, int z);
Toshihiro Shimizu 890ddd
static TUINT32 img_seek(IMAGERGB *image, UINT y, UINT z, UINT offs);
Toshihiro Shimizu 890ddd
static TINT32 RGB_img_write(IMAGERGB *image, char *buffer, TINT32 count);
Shinya Kitaoka 4c5bd5
static void img_setrowsize(IMAGERGB *image, UINT cnt, UINT y, UINT z);
Toshihiro Shimizu 890ddd
static TINT32 img_rle_compact(USHORT *expbuf, int ibpp, USHORT *rlebuf,
Shinya Kitaoka 120a6e
                              int obpp, int cnt);
Shinya Kitaoka 4c5bd5
static int iflush(IMAGERGB *image);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int do_rgb_write_header(IMAGERGB *img, int fd) {
Shinya Kitaoka 120a6e
  // DOBBIAMO SWAPPPPARE ?????????????????
Shinya Kitaoka 120a6e
  int count = 0;
Shinya Kitaoka 120a6e
  count += write(fd, &img->imagic, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->type, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->dim, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->xsize, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->ysize, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->zsize, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->min, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += write(fd, &img->max, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += write(fd, &img->wastebytes, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += write(fd, img->name, (int)sizeof(img->name));
Shinya Kitaoka 120a6e
  count += write(fd, &img->colormap, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  count += write(fd, &img->file, (int)sizeof(TINT32));
Shinya Kitaoka 120a6e
  count += write(fd, &img->flags, (int)sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->dorev, (int)sizeof(short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->x, (int)sizeof(short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->y, (int)sizeof(short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->z, (int)sizeof(short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->cnt, (int)sizeof(short));
Shinya Kitaoka 120a6e
  count += write(fd, &img->ptr, (int)sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += write(fd, &img->base, (int)sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += write(fd, &img->tmpbuf, (int)sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += write(fd, &img->offset, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += write(fd, &img->rleend, (int)sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += write(fd, &img->rowstart, (int)sizeof(TUINT32 *));
Shinya Kitaoka 120a6e
  count += write(fd, &img->rowsize, (int)sizeof(TINT32 *));
Shinya Kitaoka 120a6e
  if (sizeof(void *) == 8)  // siamo a 64bit: l'header side ha dei padding
Shinya Kitaoka 120a6e
                            // bytes.
Shinya Kitaoka 120a6e
    count = (count + 0x7) & (~0x7);
Shinya Kitaoka 120a6e
  return count;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int do_rgb_read_header(IMAGERGB *img, int fd) {
Shinya Kitaoka 120a6e
  // DOBBIAMO SWAPPPPARE ?????????????????
Shinya Kitaoka 120a6e
  int count = 0;
Shinya Kitaoka 120a6e
  count += read(fd, &img->imagic, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->type, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->dim, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->xsize, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->ysize, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->zsize, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->min, sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += read(fd, &img->max, sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += read(fd, &img->wastebytes, sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += read(fd, img->name, sizeof(img->name));
Shinya Kitaoka 120a6e
  count += read(fd, &img->colormap, sizeof(TUINT32));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  count += read(fd, &img->file, sizeof(TINT32));
Shinya Kitaoka 120a6e
  count += read(fd, &img->flags, sizeof(unsigned short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->dorev, sizeof(short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->x, sizeof(short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->y, sizeof(short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->z, sizeof(short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->cnt, sizeof(short));
Shinya Kitaoka 120a6e
  count += read(fd, &img->ptr, sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += read(fd, &img->base, sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += read(fd, &img->tmpbuf, sizeof(unsigned short *));
Shinya Kitaoka 120a6e
  count += read(fd, &img->offset, sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += read(fd, &img->rleend, sizeof(TUINT32));
Shinya Kitaoka 120a6e
  count += read(fd, &img->rowstart, sizeof(TUINT32 *));
Shinya Kitaoka 120a6e
  count += read(fd, &img->rowsize, sizeof(TINT32 *));
Shinya Kitaoka 120a6e
  if (sizeof(void *) == 8)  // siamo a 64bit: l'header side ha dei padding
Shinya Kitaoka 120a6e
                            // bytes.
Shinya Kitaoka 120a6e
    count = (count + 0x7) & (~0x7);
Shinya Kitaoka 120a6e
  return count;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Shinya Kitaoka 120a6e
enum OpenMode { OpenRead, OpenWrite };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static IMAGERGB *iopen(int fd, OpenMode openMode, unsigned int type,
Shinya Kitaoka 120a6e
                       unsigned int dim, unsigned int xsize, unsigned int ysize,
Shinya Kitaoka 120a6e
                       unsigned int zsize, short dorev) {
Shinya Kitaoka 120a6e
  IMAGERGB *image;
Shinya Kitaoka 120a6e
  extern int errno;
Shinya Kitaoka 120a6e
  int tablesize, f = fd;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image = (IMAGERGB *)malloc((int)sizeof(IMAGERGB));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  memset(image, 0, sizeof(IMAGERGB));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (openMode == OpenWrite) {
Shinya Kitaoka 120a6e
    // WRITE
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    image->imagic = IMAGIC;
Shinya Kitaoka 120a6e
    image->type   = type;
Shinya Kitaoka 120a6e
    image->xsize  = xsize;
Shinya Kitaoka 120a6e
    image->ysize  = 1;
Shinya Kitaoka 120a6e
    image->zsize  = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (dim > 1) image->ysize = ysize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (dim > 2) image->zsize = zsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (image->zsize == 1) {
Shinya Kitaoka 120a6e
      image->dim                        = 2;
Shinya Kitaoka 120a6e
      if (image->ysize == 1) image->dim = 1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      image->dim = 3;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    image->min = 10000000;
Shinya Kitaoka 120a6e
    image->max = 0;
Shinya Kitaoka 120a6e
    strncpy(image->name, "no name", 80);
Shinya Kitaoka 120a6e
    image->wastebytes = 0;
Shinya Kitaoka 120a6e
    image->dorev      = dorev;
Shinya Kitaoka 120a6e
    if (do_rgb_write_header(image, f) != IMAGERGB_HEADER_SIZE) {
Shinya Kitaoka 120a6e
      cout << "iopen: error on write of image header\n" << endl;
Shinya Kitaoka 120a6e
      return NULL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    image->flags = _IOWRT;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    // READ
Shinya Kitaoka 120a6e
    if (do_rgb_read_header(image, f) != IMAGERGB_HEADER_SIZE) {
Shinya Kitaoka 120a6e
      cout << "iopen: error on read of image header" << endl;
Shinya Kitaoka 120a6e
      return NULL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (((image->imagic >> 8) | ((image->imagic & 0xff) << 8)) == IMAGIC) {
Shinya Kitaoka 120a6e
      image->dorev = 1;
Shinya Kitaoka 120a6e
      cvtimage(image);
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      image->dorev = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (image->imagic != IMAGIC) {
Shinya Kitaoka 120a6e
      cout << "iopen: bad magic in image file " << image->imagic << endl;
Shinya Kitaoka 120a6e
      return NULL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    image->flags = _IOREAD;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
    tablesize       = image->ysize * image->zsize * (int)sizeof(TINT32);
Shinya Kitaoka 120a6e
    image->rowstart = (TUINT32 *)malloc(tablesize);
Shinya Kitaoka 120a6e
    image->rowsize  = (TINT32 *)malloc(tablesize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (image->rowstart == 0 || image->rowsize == 0) {
Shinya Kitaoka 120a6e
      cout << "iopen: error on table alloc" << endl;
Shinya Kitaoka 120a6e
      return NULL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    image->rleend = 512L + 2 * tablesize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (openMode == OpenWrite) {
Shinya Kitaoka 120a6e
      // WRITE
Shinya Kitaoka 120a6e
      int max = image->ysize * image->zsize;
Shinya Kitaoka 120a6e
      for (int i = 0; i < max; i++) {
Shinya Kitaoka 120a6e
        image->rowstart[i] = 0;
Shinya Kitaoka 120a6e
        image->rowsize[i]  = -1;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      // READ
Shinya Kitaoka 120a6e
      tablesize = image->ysize * image->zsize * (int)sizeof(TINT32);
Shinya Kitaoka 120a6e
      lseek(f, 512L, 0);
Shinya Kitaoka 120a6e
      if (read(f, image->rowstart, tablesize) != tablesize) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
        DWORD error;
Shinya Kitaoka 120a6e
        error = GetLastError();
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
        TSystem::outputDebug("iopen: error on read of rowstart\n");
Shinya Kitaoka 120a6e
        return NULL;
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (image->dorev) cvtTINT32s(image->rowstart, tablesize);
Shinya Kitaoka 120a6e
      if (read(f, image->rowsize, tablesize) != tablesize) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
        DWORD error;
Shinya Kitaoka 120a6e
        error = GetLastError();
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
        TSystem::outputDebug("iopen: error on read of rowsize\n");
Shinya Kitaoka 120a6e
        return NULL;
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (image->dorev) cvtTINT32s((TUINT32 *)image->rowsize, tablesize);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }  // END ISRLE
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  image->cnt  = 0;
Shinya Kitaoka 120a6e
  image->ptr  = 0;
Shinya Kitaoka 120a6e
  image->base = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if ((image->tmpbuf = ibufalloc(image, BPP(image->type))) == 0) {
Shinya Kitaoka 120a6e
    char xs[1024];
Shinya Kitaoka 120a6e
    sprintf(xs, "%d", image->xsize);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    TSystem::outputDebug(string("iopen: error on tmpbuf alloc %d\n") + xs);
Shinya Kitaoka 120a6e
    return NULL;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  image->x = image->y = image->z = 0;
Shinya Kitaoka 120a6e
  image->file                    = fd;
Shinya Kitaoka 120a6e
  image->offset                  = 512L;  // set up for img_optseek
Shinya Kitaoka 120a6e
  lseek((int)image->file, 512L, 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return (image);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static USHORT *ibufalloc(IMAGERGB *image, int bpp) {
Shinya Kitaoka 120a6e
  return (USHORT *)malloc(IBUFSIZE(image->xsize) * bpp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
   Inverte gli short del buffer
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void cvtshorts(unsigned short buffer[], TINT32 n) {
Shinya Kitaoka 120a6e
  TINT32 nshorts = n >> 1;
Shinya Kitaoka 120a6e
  for (int i = 0; i < nshorts; i++) {
Shinya Kitaoka 120a6e
    unsigned short swrd = *buffer;
Shinya Kitaoka 120a6e
    *buffer++           = (swrd >> 8) | (swrd << 8);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
 *  INVERTE I LONG DEL BUFFER
Toshihiro Shimizu 890ddd
   */
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void cvtTINT32s(TUINT32 buffer[], TINT32 n) {
Shinya Kitaoka 120a6e
  TINT32 nTINT32s = n >> 2;
Shinya Kitaoka 120a6e
  for (int i = 0; i < nTINT32s; i++) {
Shinya Kitaoka 120a6e
    TUINT32 lwrd = buffer[i];
Shinya Kitaoka 120a6e
    buffer[i] = ((lwrd >> 24) | (lwrd >> 8 & 0xff00) | (lwrd << 8 & 0xff0000) |
Shinya Kitaoka 120a6e
                 (lwrd << 24));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
 *  INVERTE I LONG E GLI SHORT DEL BUFFER
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void cvtimage(IMAGERGB *image) {
Shinya Kitaoka 120a6e
  TUINT32 *buffer = (TUINT32 *)image;
Shinya Kitaoka 120a6e
  cvtshorts((unsigned short *)buffer, 12);
Shinya Kitaoka 120a6e
  cvtTINT32s(buffer + 3, 12);
Shinya Kitaoka 120a6e
  cvtTINT32s(buffer + 26, 4);
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define EXPAND_CODE(TYPE)                                                      \
Shinya Kitaoka 120a6e
  while (1) {                                                                  \
Shinya Kitaoka 120a6e
    pixel = *iptr++;                                                           \
Shinya Kitaoka 120a6e
    if (!(count = (pixel & 0x7f))) return;                                     \
Shinya Kitaoka 120a6e
    if (pixel & 0x80) {                                                        \
Shinya Kitaoka 120a6e
      while (count--) *optr++ = (TYPE)*iptr++;                                 \
Shinya Kitaoka 120a6e
    } else {                                                                   \
Shinya Kitaoka 120a6e
      pixel                   = *iptr++;                                       \
Shinya Kitaoka 120a6e
      while (count--) *optr++ = (TYPE)pixel;                                   \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
 *  ESPANDE UNA IMMAGINE FORMATO RGB-RLE
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void img_rle_expand(unsigned short *rlebuf, int ibpp,
Shinya Kitaoka 120a6e
                           unsigned short *expbuf, int obpp) {
Shinya Kitaoka 120a6e
  if (ibpp == 1 && obpp == 1) {
Shinya Kitaoka 120a6e
    unsigned char *iptr = (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
    unsigned char *optr = (unsigned char *)expbuf;
Shinya Kitaoka 120a6e
    unsigned short pixel, count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    EXPAND_CODE(unsigned char);
Shinya Kitaoka 120a6e
  } else if (ibpp == 1 && obpp == 2) {
Shinya Kitaoka 120a6e
    unsigned char *iptr  = (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
    unsigned short *optr = expbuf;
Shinya Kitaoka 120a6e
    unsigned short pixel, count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    EXPAND_CODE(unsigned short);
Shinya Kitaoka 120a6e
  } else if (ibpp == 2 && obpp == 1) {
Shinya Kitaoka 120a6e
    unsigned short *iptr = rlebuf;
Shinya Kitaoka 120a6e
    unsigned char *optr  = (unsigned char *)expbuf;
Shinya Kitaoka 120a6e
    unsigned short pixel, count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    EXPAND_CODE(unsigned char);
Shinya Kitaoka 120a6e
  } else if (ibpp == 2 && obpp == 2) {
Shinya Kitaoka 120a6e
    unsigned short *iptr = rlebuf;
Shinya Kitaoka 120a6e
    unsigned short *optr = expbuf;
Shinya Kitaoka 120a6e
    unsigned short pixel, count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    EXPAND_CODE(unsigned short);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    cout << "rle_expand: bad bpp: " << ibpp << obpp << endl;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 *  RITORNA L'AMPIEZZA DELLA RIGA DI UN IMMAGINE RGB
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int img_getrowsize(IMAGERGB *image) {
Shinya Kitaoka 120a6e
  switch (image->dim) {
Shinya Kitaoka 120a6e
  case 1:
Shinya Kitaoka 120a6e
    return (int)image->rowsize[0];
Shinya Kitaoka 120a6e
  case 2:
Shinya Kitaoka 120a6e
    return (int)image->rowsize[image->y];
Shinya Kitaoka 120a6e
  case 3:
Shinya Kitaoka 120a6e
    return (int)image->rowsize[image->y + image->z * image->ysize];
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * SPOSTA IL PUNTATORE AL FILE RGB
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TUINT32 img_optseek(IMAGERGB *image, TUINT32 offset) {
Shinya Kitaoka 120a6e
  if (image->offset != offset) {
Shinya Kitaoka 120a6e
    image->offset = offset;
Shinya Kitaoka 120a6e
    return (TUINT32)lseek(image->file, (TINT32)offset, 0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return offset;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
 *  LEGGE DAL FILE RGB E RIEMPE IL BUFFER
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TINT32 rgb_img_read(IMAGERGB *image, char *buffer, TINT32 count) {
Shinya Kitaoka 120a6e
  TINT32 retval;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  retval = read(image->file, buffer, count);
Shinya Kitaoka 120a6e
  if (retval == count)
Shinya Kitaoka 120a6e
    image->offset += count;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    // BRUTTO: ma qui non ci si deve mai passare, serve per fare un crash
Shinya Kitaoka 120a6e
    image->offset = (TUINT32)-1;
Shinya Kitaoka 120a6e
  return retval;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * CONTROLLA SE LA RIGA CORRENTE DELL'IMMAGINE RGB E' VALIDA
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int img_badrow(IMAGERGB *image, int y, int z) {
Shinya Kitaoka 120a6e
  if (y >= image->ysize || z >= image->zsize)
Shinya Kitaoka 120a6e
    return 1;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 *  POSIZIONA IL PUNTATORE AL FILE RGB ALL'INIZIO DELL'AREA DATI IMMAGINE
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TUINT32 img_seek(IMAGERGB *image, unsigned int y, unsigned int z,
Shinya Kitaoka 120a6e
                        unsigned int offs) {
Shinya Kitaoka 120a6e
  if (img_badrow(image, y, z)) {
Shinya Kitaoka 120a6e
    cout << "imglib: row number out of range" << endl;
Shinya Kitaoka 120a6e
    return (TUINT32)EOF;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image->x = 0;
Shinya Kitaoka 120a6e
  image->y = y;
Shinya Kitaoka 120a6e
  image->z = z;
Shinya Kitaoka 120a6e
  if (ISVERBATIM(image->type)) {
Shinya Kitaoka 120a6e
    switch (image->dim) {
Shinya Kitaoka 120a6e
    case 1:
Shinya Kitaoka 120a6e
      return img_optseek(image, 512L + offs);
Shinya Kitaoka 120a6e
    case 2:
Shinya Kitaoka 120a6e
      return img_optseek(image,
Shinya Kitaoka 120a6e
                         512L + offs + (y * image->xsize) * BPP(image->type));
Shinya Kitaoka 120a6e
    case 3:
Shinya Kitaoka 120a6e
      return img_optseek(
Shinya Kitaoka 120a6e
          image, 512L + offs +
Shinya Kitaoka 120a6e
                     (y * image->xsize + z * image->xsize * image->ysize) *
Shinya Kitaoka 120a6e
                         BPP(image->type));
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      cout << "img_seek: wierd dim" << endl;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
    switch (image->dim) {
Shinya Kitaoka 120a6e
    case 1:
Shinya Kitaoka 120a6e
      return img_optseek(image, offs + image->rowstart[0]);
Shinya Kitaoka 120a6e
    case 2:
Shinya Kitaoka 120a6e
      return img_optseek(image, offs + image->rowstart[y]);
Shinya Kitaoka 120a6e
    case 3:
Shinya Kitaoka 120a6e
      return img_optseek(image, offs + image->rowstart[y + z * image->ysize]);
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      cout << "img_seek: wierd dim" << endl;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    cout << "img_seek: wierd image type" << endl;
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
   Legge una riga (compressa/non compressa) di un file RGB.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int new_getrow(IMAGERGB *image, void *buffer, UINT y, UINT z) {
Shinya Kitaoka 120a6e
  short cnt;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!(image->flags & (_IORW | _IOREAD))) return -1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->dim < 3) z = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->dim < 2) y = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  img_seek(image, y, z, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ISVERBATIM(image->type)) {
Shinya Kitaoka 120a6e
    switch (BPP(image->type)) {
Shinya Kitaoka 120a6e
    case 1:
Shinya Kitaoka 120a6e
      if (rgb_img_read(image, (char *)buffer, image->xsize) != image->xsize)
Shinya Kitaoka 120a6e
        return -1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      return image->xsize;
Shinya Kitaoka 120a6e
    case 2:
Shinya Kitaoka 120a6e
      cnt = image->xsize << 1;
Shinya Kitaoka 120a6e
      if (rgb_img_read(image, (char *)buffer, cnt) != cnt)
Shinya Kitaoka 120a6e
        return -1;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        if (image->dorev) cvtshorts((unsigned short *)buffer, cnt);
Shinya Kitaoka 120a6e
        return image->xsize;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      cout << "getrow: wierd bpp" << endl;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
    switch (BPP(image->type)) {
Shinya Kitaoka 120a6e
    case 1:
Shinya Kitaoka 120a6e
      if ((cnt = img_getrowsize(image)) == -1) return -1;
Shinya Kitaoka 120a6e
      if (rgb_img_read(image, (char *)image->tmpbuf, cnt) != cnt)
Shinya Kitaoka 120a6e
        return -1;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        img_rle_expand(image->tmpbuf, 1, (unsigned short *)buffer, 1);
Shinya Kitaoka 120a6e
        return image->xsize;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    case 2:
Shinya Kitaoka 120a6e
      if ((cnt = img_getrowsize(image)) == -1) return -1;
Shinya Kitaoka 120a6e
      if (cnt != rgb_img_read(image, (char *)image->tmpbuf, cnt))
Shinya Kitaoka 120a6e
        return -1;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        if (image->dorev) cvtshorts(image->tmpbuf, cnt);
Shinya Kitaoka 120a6e
        img_rle_expand(image->tmpbuf, 2, (unsigned short *)buffer, 2);
Shinya Kitaoka 120a6e
        return image->xsize;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      cout << "getrow: weird bpp" << endl;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    cout << "getrow: weird image type" << endl;
Shinya Kitaoka 120a6e
  return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 *  ROBA PRESA DA ASMWRITERGB.C
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TINT32 RGB_img_write(IMAGERGB *image, char *buffer, TINT32 count) {
Shinya Kitaoka 120a6e
  TINT32 retval;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  retval = write(image->file, buffer, count);
Shinya Kitaoka 120a6e
  if (retval == count)
Shinya Kitaoka 120a6e
    image->offset += count;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    image->offset = (TUINT32)-1;
Shinya Kitaoka 120a6e
  return retval;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void img_setrowsize(IMAGERGB *image, UINT cnt, UINT y, UINT z) {
Shinya Kitaoka 120a6e
  TINT32 *sizeptr = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (img_badrow(image, y, z)) return;
Shinya Kitaoka 120a6e
  switch (image->dim) {
Shinya Kitaoka 120a6e
  case 1:
Shinya Kitaoka 120a6e
    sizeptr            = &image->rowsize[0];
Shinya Kitaoka 120a6e
    image->rowstart[0] = image->rleend;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 2:
Shinya Kitaoka 120a6e
    sizeptr            = &image->rowsize[y];
Shinya Kitaoka 120a6e
    image->rowstart[y] = image->rleend;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 3:
Shinya Kitaoka 120a6e
    sizeptr = &image->rowsize[y + z * image->ysize];
Shinya Kitaoka 120a6e
    image->rowstart[y + z * image->ysize] = image->rleend;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (*sizeptr != -1) image->wastebytes += *sizeptr;
Shinya Kitaoka 120a6e
  *sizeptr = (TINT32)cnt;
Shinya Kitaoka 120a6e
  image->rleend += cnt;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define COMPACT_CODE(TYPE)                                                     \
Shinya Kitaoka 120a6e
  while (iptr < ibufend) {                                                     \
Shinya Kitaoka 120a6e
    sptr = iptr;                                                               \
Shinya Kitaoka 120a6e
    iptr += 2;                                                                 \
Shinya Kitaoka 120a6e
    while ((iptr < ibufend) &&                                                 \
Shinya Kitaoka 120a6e
           ((iptr[-2] != iptr[-1]) || (iptr[-1] != iptr[0])))                  \
Shinya Kitaoka 120a6e
      iptr++;                                                                  \
Shinya Kitaoka 120a6e
    iptr -= 2;                                                                 \
Shinya Kitaoka 120a6e
    count = iptr - sptr;                                                       \
Shinya Kitaoka 120a6e
    while (count) {                                                            \
Shinya Kitaoka 120a6e
      todo = (TYPE)(count > 126 ? 126 : count);                                \
Shinya Kitaoka 120a6e
      count -= todo;                                                           \
Shinya Kitaoka 120a6e
      *optr++                = (TYPE)(0x80 | todo);                            \
Shinya Kitaoka 120a6e
      while (todo--) *optr++ = (TYPE)*sptr++;                                  \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
    sptr = iptr;                                                               \
Shinya Kitaoka 120a6e
    cc   = *iptr++;                                                            \
Shinya Kitaoka 120a6e
    while ((iptr < ibufend) && (*iptr == cc)) iptr++;                          \
Shinya Kitaoka 120a6e
    count = iptr - sptr;                                                       \
Shinya Kitaoka 120a6e
    while (count) {                                                            \
Shinya Kitaoka 120a6e
      todo = (TYPE)(count > 126 ? 126 : count);                                \
Shinya Kitaoka 120a6e
      count -= todo;                                                           \
Shinya Kitaoka 120a6e
      *optr++ = (TYPE)todo;                                                    \
Shinya Kitaoka 120a6e
      *optr++ = (TYPE)cc;                                                      \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }                                                                            \
Shinya Kitaoka 120a6e
  *optr++ = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TINT32 img_rle_compact(unsigned short *expbuf, int ibpp,
Shinya Kitaoka 120a6e
                              unsigned short *rlebuf, int obpp, int cnt) {
Shinya Kitaoka 120a6e
  if (ibpp == 1 && obpp == 1) {
Shinya Kitaoka 120a6e
    unsigned char *iptr    = (unsigned char *)expbuf;
Shinya Kitaoka 120a6e
    unsigned char *ibufend = iptr + cnt;
Shinya Kitaoka 120a6e
    unsigned char *sptr;
Shinya Kitaoka 120a6e
    unsigned char *optr = (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
    TUINT32 todo, cc;
Shinya Kitaoka 120a6e
    TINT32 count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    COMPACT_CODE(unsigned char);
Shinya Kitaoka 120a6e
    return optr - (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
  } else if (ibpp == 1 && obpp == 2) {
Shinya Kitaoka 120a6e
    unsigned char *iptr    = (unsigned char *)expbuf;
Shinya Kitaoka 120a6e
    unsigned char *ibufend = iptr + cnt;
Shinya Kitaoka 120a6e
    unsigned char *sptr;
Shinya Kitaoka 120a6e
    unsigned short *optr = rlebuf;
Shinya Kitaoka 120a6e
    TUINT32 todo, cc;
Shinya Kitaoka 120a6e
    TINT32 count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    COMPACT_CODE(unsigned short);
Shinya Kitaoka 120a6e
    return optr - rlebuf;
Shinya Kitaoka 120a6e
  } else if (ibpp == 2 && obpp == 1) {
Shinya Kitaoka 120a6e
    unsigned short *iptr    = expbuf;
Shinya Kitaoka 120a6e
    unsigned short *ibufend = iptr + cnt;
Shinya Kitaoka 120a6e
    unsigned short *sptr;
Shinya Kitaoka 120a6e
    unsigned char *optr = (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
    TUINT32 todo, cc;
Shinya Kitaoka 120a6e
    TINT32 count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    COMPACT_CODE(unsigned char);
Shinya Kitaoka 120a6e
    return optr - (unsigned char *)rlebuf;
Shinya Kitaoka 120a6e
  } else if (ibpp == 2 && obpp == 2) {
Shinya Kitaoka 120a6e
    unsigned short *iptr    = expbuf;
Shinya Kitaoka 120a6e
    unsigned short *ibufend = iptr + cnt;
Shinya Kitaoka 120a6e
    unsigned short *sptr;
Shinya Kitaoka 120a6e
    unsigned short *optr = rlebuf;
Shinya Kitaoka 120a6e
    unsigned short todo, cc;
Shinya Kitaoka 120a6e
    TINT32 count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    COMPACT_CODE(unsigned short);
Shinya Kitaoka 120a6e
    return optr - rlebuf;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    cout << "rle_compact: bad bpp: %d %d" << ibpp << obpp;
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void iclose(IMAGERGB *image) {
Shinya Kitaoka 120a6e
  TINT32 tablesize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  iflush(image);
Shinya Kitaoka 120a6e
  img_optseek(image, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* CONTROLLARE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->flags & _IOWRT) {
Shinya Kitaoka 120a6e
    if (image->dorev) cvtimage(image);
Shinya Kitaoka 120a6e
    if (do_rgb_write_header(image, image->file) != IMAGERGB_HEADER_SIZE) {
Shinya Kitaoka 120a6e
      fprintf(stderr, "iflush: error on write of image header\n");
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (image->dorev) cvtimage(image);
Shinya Kitaoka 120a6e
    if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
      img_optseek(image, 512L);
Shinya Kitaoka 120a6e
      tablesize = image->ysize * image->zsize * (int)sizeof(TINT32);
Shinya Kitaoka 120a6e
      if (image->dorev) cvtTINT32s(image->rowstart, tablesize);
Shinya Kitaoka 120a6e
      if (RGB_img_write(image, (char *)(image->rowstart), tablesize) !=
Shinya Kitaoka 120a6e
          tablesize) {
Shinya Kitaoka 120a6e
        fprintf(stderr, "iflush: error on write of rowstart\n");
Shinya Kitaoka 120a6e
        return;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      if (image->dorev) cvtTINT32s((TUINT32 *)image->rowsize, tablesize);
Shinya Kitaoka 120a6e
      if (RGB_img_write(image, (char *)(image->rowsize), tablesize) !=
Shinya Kitaoka 120a6e
          tablesize) {
Shinya Kitaoka 120a6e
        fprintf(stderr, "iflush: error on write of rowsize\n");
Shinya Kitaoka 120a6e
        return;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->base) {
Shinya Kitaoka 120a6e
    free(image->base);
Shinya Kitaoka 120a6e
    image->base = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->tmpbuf) {
Shinya Kitaoka 120a6e
    free(image->tmpbuf);
Shinya Kitaoka 120a6e
    image->tmpbuf = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
    free(image->rowstart);
Shinya Kitaoka 120a6e
    image->rowstart = 0;
Shinya Kitaoka 120a6e
    free(image->rowsize);
Shinya Kitaoka 120a6e
    image->rowsize = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  free(image);
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int new_putrow(IMAGERGB *image, void *buffer, UINT y, UINT z) {
Shinya Kitaoka 120a6e
  TINT32 cnt;
Shinya Kitaoka 120a6e
  int dorev, bpp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!(image->flags & (_IORW | _IOWRT))) return -1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->dim < 3) z = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->dim < 2) y = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image->min = 0;
Shinya Kitaoka 120a6e
  bpp        = BPP(image->type);
Shinya Kitaoka 120a6e
  dorev      = image->dorev && bpp == 2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (bpp == 1)
Shinya Kitaoka 120a6e
    image->max = 255; /*commento tutti i calcoli su max e min per velocizzare */
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    image->max = 65535;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ISVERBATIM(image->type)) {
Shinya Kitaoka 120a6e
    img_seek(image, y, z, 0);
Shinya Kitaoka 120a6e
    cnt = image->xsize << (bpp - 1);
Shinya Kitaoka 120a6e
    if (dorev) cvtshorts((unsigned short *)buffer, cnt);
Shinya Kitaoka 120a6e
    if (RGB_img_write(image, (char *)buffer, cnt) != cnt) {
Shinya Kitaoka 120a6e
      if (dorev) cvtshorts((unsigned short *)buffer, cnt);
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (dorev) cvtshorts((unsigned short *)buffer, cnt);
Shinya Kitaoka 120a6e
      return image->xsize;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (ISRLE(image->type)) {
Shinya Kitaoka 120a6e
    cnt = img_rle_compact((unsigned short *)buffer, bpp, image->tmpbuf, bpp,
Shinya Kitaoka 120a6e
                          image->xsize);
Shinya Kitaoka 120a6e
    cnt <<= (bpp - 1);
Shinya Kitaoka 120a6e
    img_setrowsize(image, cnt, y, z);
Shinya Kitaoka 120a6e
    img_seek(image, y, z, 0);
Shinya Kitaoka 120a6e
    if (dorev) cvtshorts(image->tmpbuf, cnt);
Shinya Kitaoka 120a6e
    if (RGB_img_write(image, (char *)(image->tmpbuf), cnt) != cnt) {
Shinya Kitaoka 120a6e
      if (dorev) cvtshorts(image->tmpbuf, cnt);
Shinya Kitaoka 120a6e
      return -1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (dorev) cvtshorts(image->tmpbuf, cnt);
Shinya Kitaoka 120a6e
      return image->xsize;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    fprintf(stderr, "putrow: wierd image type\n");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int iflush(IMAGERGB *image) {
Shinya Kitaoka 120a6e
  unsigned short *base;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if ((image->flags & _IOWRT) && (base = image->base) != NULL &&
Shinya Kitaoka 120a6e
      (image->ptr - base) > 0) {
Shinya Kitaoka 120a6e
    if (new_putrow(image, base, image->y, image->z) != image->xsize) {
Shinya Kitaoka 120a6e
      image->flags |= _IOERR;
Shinya Kitaoka 120a6e
      return (EOF);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class SgiReader final : public Tiio::Reader {
Shinya Kitaoka 120a6e
  IMAGERGB *m_header;
Shinya Kitaoka 120a6e
  int m_currentY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  SgiReader() : m_header(0), m_currentY(0) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ~SgiReader();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void open(FILE *file) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  TPropertyGroup *getProperties() const override { return 0; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void readLine(char *buffer, int x0, int x1, int shrink) override;
Shinya Kitaoka 473e70
  void readLine(short *buffer, int x0, int x1, int shrink) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  int skipLines(int lineCount) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiReader::open(FILE *file) {
Shinya Kitaoka 120a6e
  m_header = iopen(fileno(file), OpenRead, 0, 0, 0, 0, 0, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!m_header) {
Shinya Kitaoka 120a6e
    string str("Can't open file");
Shinya Kitaoka 120a6e
    throw(str);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info.m_lx                     = m_header->xsize;
Shinya Kitaoka 120a6e
  m_info.m_ly                     = m_header->ysize;
Shinya Kitaoka 120a6e
  m_info.m_bitsPerSample          = BPP(m_header->type) * 8;
Shinya Kitaoka 120a6e
  m_info.m_samplePerPixel         = m_header->zsize;
Shinya Kitaoka 120a6e
  Tiio::SgiWriterProperties *prop = new Tiio::SgiWriterProperties();
Shinya Kitaoka 120a6e
  m_info.m_properties             = prop;
Shinya Kitaoka 120a6e
  prop->m_endianess.setValue(m_header->dorev == 1 ? L"Big Endian"
Shinya Kitaoka 120a6e
                                                  : L"Little Endian");
Shinya Kitaoka 120a6e
  prop->m_compressed.setValue(ISRLE(m_header->type) ? true : false);
Shinya Kitaoka 120a6e
  wstring pixelSize;
Shinya Kitaoka 120a6e
  int ps = m_info.m_bitsPerSample * m_info.m_samplePerPixel;
Shinya Kitaoka 120a6e
  if (ps == 8)
Shinya Kitaoka 120a6e
    pixelSize = L"8 bits (Greyscale)";
Shinya Kitaoka 120a6e
  else if (ps == 24)
Shinya Kitaoka 120a6e
    pixelSize = L"24 bits";
Shinya Kitaoka 120a6e
  else if (ps == 32)
Shinya Kitaoka 120a6e
    pixelSize = L"32 bits";
Shinya Kitaoka 120a6e
  else if (ps == 48)
Shinya Kitaoka 120a6e
    pixelSize = L"48 bits";
Shinya Kitaoka 120a6e
  else if (ps == 64)
Shinya Kitaoka 120a6e
    pixelSize = L"64 bits";
Shinya Kitaoka 120a6e
  prop->m_pixelSize.setValue(pixelSize);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
SgiReader::~SgiReader() {
Shinya Kitaoka 120a6e
  if (!m_header) return;
Shinya Kitaoka 120a6e
  if (m_header->base) free(m_header->base);
Shinya Kitaoka 120a6e
  if (m_header->tmpbuf) free(m_header->tmpbuf);
Shinya Kitaoka 120a6e
  if ((ISRLE(m_header->type))) {
Shinya Kitaoka 120a6e
    free(m_header->rowstart);
Shinya Kitaoka 120a6e
    free(m_header->rowsize);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  free(m_header);
Shinya Kitaoka 120a6e
  m_header = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiReader::readLine(short *buffer, int x0, int x1, int shrink) {
Shinya Kitaoka 120a6e
  // assert(shrink == 1);
Shinya Kitaoka 120a6e
  // assert(x0 == 0);
Shinya Kitaoka 120a6e
  // assert(x1 == m_info.m_lx-1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(BPP(m_header->type) == 2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  {  // 64 -> 32
Shinya Kitaoka 120a6e
    TPixel64 *pix = (TPixel64 *)buffer;
Shinya Kitaoka 120a6e
    std::vector<ushort> rbuf(m_info.m_lx), gbuf(m_info.m_lx), bbuf(m_info.m_lx),</ushort>
Shinya Kitaoka 120a6e
        mbuf(m_info.m_lx);
Shinya Kitaoka 120a6e
    if (m_header->zsize == 4) {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &mbuf[0], m_currentY, 3);
Shinya Kitaoka 120a6e
      for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
        pix->r = rbuf[i];
Shinya Kitaoka 120a6e
        pix->g = gbuf[i];
Shinya Kitaoka 120a6e
        pix->b = bbuf[i];
Shinya Kitaoka 120a6e
        pix->m = mbuf[i];
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
        pix->r = rbuf[i];
Shinya Kitaoka 120a6e
        pix->g = gbuf[i];
Shinya Kitaoka 120a6e
        pix->b = bbuf[i];
Shinya Kitaoka 120a6e
        pix->m = 0xffff;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_currentY++;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiReader::readLine(char *buffer, int x0, int x1, int shrink) {
Shinya Kitaoka 120a6e
  // assert(shrink == 1);
Shinya Kitaoka 120a6e
  // assert(x0 == 0);
Shinya Kitaoka 120a6e
  // assert(x1 == m_info.m_lx-1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Non ancora implementata la lettura parziale
Shinya Kitaoka 120a6e
  x0     = 0;
Shinya Kitaoka 120a6e
  x1     = m_info.m_lx - 1;
Shinya Kitaoka 120a6e
  shrink = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (BPP(m_header->type) == 1)  // 32 -> 32
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    TPixel32 *pix = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
    std::vector<uchar> rbuf(m_info.m_lx), gbuf(m_info.m_lx), bbuf(m_info.m_lx),</uchar>
Shinya Kitaoka 120a6e
        mbuf(m_info.m_lx);
Shinya Kitaoka 120a6e
    if (m_header->zsize == 4) {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &mbuf[0], m_currentY, 3);
Shinya Kitaoka 120a6e
      for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
        pix->r = rbuf[i];
Shinya Kitaoka 120a6e
        pix->g = gbuf[i];
Shinya Kitaoka 120a6e
        pix->b = bbuf[i];
Shinya Kitaoka 120a6e
        pix->m = mbuf[i];
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      if (m_header->zsize == 1)
Shinya Kitaoka 120a6e
        for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
          pix->r = rbuf[i];
Shinya Kitaoka 120a6e
          pix->g = rbuf[i];
Shinya Kitaoka 120a6e
          pix->b = rbuf[i];
Shinya Kitaoka 120a6e
          pix->m = 0xff;
Shinya Kitaoka 120a6e
          ++pix;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
        new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
        for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
          pix->r = rbuf[i];
Shinya Kitaoka 120a6e
          pix->g = gbuf[i];
Shinya Kitaoka 120a6e
          pix->b = bbuf[i];
Shinya Kitaoka 120a6e
          pix->m = 0xff;
Shinya Kitaoka 120a6e
          ++pix;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {  // 64 -> 32
Shinya Kitaoka 120a6e
    TPixel32 *pix = (TPixel32 *)buffer;
Shinya Kitaoka 120a6e
    std::vector<ushort> rbuf(m_info.m_lx), gbuf(m_info.m_lx), bbuf(m_info.m_lx),</ushort>
Shinya Kitaoka 120a6e
        mbuf(m_info.m_lx);
Shinya Kitaoka 120a6e
    if (m_header->zsize == 4) {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &mbuf[0], m_currentY, 3);
Shinya Kitaoka 120a6e
      for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
        pix->r = rbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->g = gbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->b = bbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->m = mbuf[i] >> 8;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      new_getrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_getrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      for (int i = 0; i < (x1 - x0 + 1); ++i) {
Shinya Kitaoka 120a6e
        pix->r = rbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->g = gbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->b = bbuf[i] >> 8;
Shinya Kitaoka 120a6e
        pix->m = 0xff;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_currentY++;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int SgiReader::skipLines(int lineCount) {
Shinya Kitaoka 120a6e
  m_currentY += lineCount;
Shinya Kitaoka 120a6e
  return lineCount;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
                                                WRITER
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class SgiWriter final : public Tiio::Writer {
Shinya Kitaoka 120a6e
  int m_currentY;
Shinya Kitaoka 120a6e
  IMAGERGB *m_header;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  TImageInfo m_info;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  SgiWriter() : m_currentY(0), m_header(0){};
Shinya Kitaoka 120a6e
  ~SgiWriter() {
Shinya Kitaoka 120a6e
    if (m_header) iclose(m_header);
Shinya Kitaoka 120a6e
    delete m_properties;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void open(FILE *file, const TImageInfo &info) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPropertyGroup *getProperties() { return m_properties; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void writeLine(char *buffer) override;
Shinya Kitaoka 473e70
  void writeLine(short *buffer) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void flush() override {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //  RowOrder getRowOrder() const { return BOTTOM2TOP; }
Shinya Kitaoka 473e70
  bool write64bitSupported() const override { return true; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setProperties(TPropertyGroup *properties);
Toshihiro Shimizu 890ddd
shun-iwasawa 93fdd4
  // m_header->zsize is updated with "Bits Per Pixel" property value in the
shun-iwasawa 93fdd4
  // function open()
shun-iwasawa 93fdd4
  bool writeAlphaSupported() const override {
shun-iwasawa 93fdd4
    return m_header && (m_header->zsize == 4);
shun-iwasawa 93fdd4
  }
shun-iwasawa 93fdd4
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  SgiWriter(const SgiWriter &);
Shinya Kitaoka 120a6e
  SgiWriter &operator=(const SgiWriter &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Reader *Tiio::makeSgiReader() { return new SgiReader(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tiio::Writer *Tiio::makeSgiWriter() { return new SgiWriter(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiWriter::open(FILE *file, const TImageInfo &info) {
Shinya Kitaoka 120a6e
  if (!m_properties) m_properties = new Tiio::SgiWriterProperties();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TEnumProperty *p =
Shinya Kitaoka 120a6e
      (TEnumProperty *)(m_properties->getProperty("Bits Per Pixel"));
Shinya Kitaoka 120a6e
  assert(p);
Shinya Kitaoka 120a6e
  string str          = ::to_string(p->getValue());
Shinya Kitaoka 120a6e
  int bitPerPixel     = atoi(str.c_str());
Shinya Kitaoka 120a6e
  int channelBytesNum = 1;
Shinya Kitaoka 120a6e
  int dim             = 3;
Shinya Kitaoka 120a6e
  int zsize           = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info = info;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (bitPerPixel) {
Shinya Kitaoka 120a6e
  case 8:
Shinya Kitaoka 120a6e
    dim   = 2;
Shinya Kitaoka 120a6e
    zsize = 1;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 24:
Shinya Kitaoka 120a6e
    dim   = 3;
Shinya Kitaoka 120a6e
    zsize = 3;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 32:
Shinya Kitaoka 120a6e
    dim   = 3;
Shinya Kitaoka 120a6e
    zsize = 4;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 48:
Shinya Kitaoka 120a6e
    dim             = 3;
Shinya Kitaoka 120a6e
    zsize           = 3;
Shinya Kitaoka 120a6e
    channelBytesNum = 2;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case 64:
Shinya Kitaoka 120a6e
    zsize           = 4;
Shinya Kitaoka 120a6e
    dim             = 3;
Shinya Kitaoka 120a6e
    channelBytesNum = 2;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TBoolProperty *bp =
Shinya Kitaoka 120a6e
      (TBoolProperty *)(m_properties->getProperty("RLE-Compressed"));
Shinya Kitaoka 120a6e
  assert(bp);
Shinya Kitaoka 120a6e
  bool compressed = bp->getValue();
Shinya Kitaoka 120a6e
  p               = (TEnumProperty *)(m_properties->getProperty("Endianess"));
Shinya Kitaoka 120a6e
  assert(p);
Shinya Kitaoka 120a6e
  str            = ::to_string(p->getValue());
Shinya Kitaoka 120a6e
  bool bigEndian = (str == "Big Endian");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_header = iopen(
Shinya Kitaoka 120a6e
      fileno(file), OpenWrite,
Shinya Kitaoka 120a6e
      compressed ? RLE(BPP(channelBytesNum)) : VERBATIM(BPP(channelBytesNum)),
Shinya Kitaoka 120a6e
      dim, m_info.m_lx, m_info.m_ly, zsize, bigEndian ? 1 : 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiWriter::writeLine(char *buffer) {
Shinya Kitaoka 120a6e
  if (BPP(m_header->type) == 1) {
Shinya Kitaoka 120a6e
    if (m_header->dim < 3)  // 32->8
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      new_putrow(m_header, buffer, m_currentY, 0);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      std::vector<uchar> rbuf(m_info.m_lx), gbuf(m_info.m_lx),</uchar>
Shinya Kitaoka 120a6e
          bbuf(m_info.m_lx), mbuf(m_info.m_lx);
Shinya Kitaoka 120a6e
      TPixelRGBM32 *pix = (TPixelRGBM32 *)buffer;
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; ++i) {
Shinya Kitaoka 120a6e
        rbuf[i] = pix->r;
Shinya Kitaoka 120a6e
        gbuf[i] = pix->g;
Shinya Kitaoka 120a6e
        bbuf[i] = pix->b;
Shinya Kitaoka 120a6e
        mbuf[i] = pix->m;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      new_putrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_putrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_putrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      if (m_header->zsize == 4) new_putrow(m_header, &mbuf[0], m_currentY, 3);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ++m_currentY;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SgiWriter::writeLine(short *buffer) {
Shinya Kitaoka 120a6e
  assert(BPP(m_header->type) == 2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    if (m_header->dim < 3) {
Shinya Kitaoka 120a6e
      std::vector<ushort> tmp(m_info.m_lx);</ushort>
Shinya Kitaoka 120a6e
      TPixelRGBM64 *pix = (TPixelRGBM64 *)buffer;
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; ++i) {
Shinya Kitaoka 120a6e
        tmp[i] = TPixelGR16::from(*pix).value;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      new_putrow(m_header, &tmp[0], m_currentY, 0);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      std::vector<ushort> rbuf(m_info.m_lx), gbuf(m_info.m_lx),</ushort>
Shinya Kitaoka 120a6e
          bbuf(m_info.m_lx), mbuf(m_info.m_lx);
Shinya Kitaoka 120a6e
      TPixelRGBM64 *pix = (TPixelRGBM64 *)buffer;
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_info.m_lx; ++i) {
Shinya Kitaoka 120a6e
        rbuf[i] = pix->r;
Shinya Kitaoka 120a6e
        gbuf[i] = pix->g;
Shinya Kitaoka 120a6e
        bbuf[i] = pix->b;
Shinya Kitaoka 120a6e
        mbuf[i] = pix->m;
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      new_putrow(m_header, &rbuf[0], m_currentY, 0);
Shinya Kitaoka 120a6e
      new_putrow(m_header, &gbuf[0], m_currentY, 1);
Shinya Kitaoka 120a6e
      new_putrow(m_header, &bbuf[0], m_currentY, 2);
Shinya Kitaoka 120a6e
      if (m_header->zsize == 4) new_putrow(m_header, &mbuf[0], m_currentY, 3);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ++m_currentY;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tiio::SgiWriterProperties::SgiWriterProperties()
Shinya Kitaoka 120a6e
    : m_pixelSize("Bits Per Pixel")
Shinya Kitaoka 120a6e
    , m_endianess("Endianess")
Shinya Kitaoka 120a6e
    , m_compressed("RLE-Compressed", false) {
Shinya Kitaoka 120a6e
  m_pixelSize.addValue(L"24 bits");
Shinya Kitaoka 120a6e
  m_pixelSize.addValue(L"32 bits");
Shinya Kitaoka 120a6e
  m_pixelSize.addValue(L"48 bits");
Shinya Kitaoka 120a6e
  m_pixelSize.addValue(L"64 bits");
Shinya Kitaoka 120a6e
  m_pixelSize.addValue(L"8 bits (Greyscale)");
Shinya Kitaoka 120a6e
  m_pixelSize.setValue(L"32 bits");
Shinya Kitaoka 120a6e
  bind(m_pixelSize);
Shinya Kitaoka 120a6e
  bind(m_compressed);
Shinya Kitaoka 120a6e
  m_endianess.addValue(L"Big Endian");
Shinya Kitaoka 120a6e
  m_endianess.addValue(L"Little Endian");
Shinya Kitaoka 120a6e
  bind(m_endianess);
Toshihiro Shimizu 890ddd
}
shun-iwasawa e87e08
shun-iwasawa e87e08
void Tiio::SgiWriterProperties::updateTranslation() {
shun-iwasawa e87e08
  m_pixelSize.setQStringName(tr("Bits Per Pixel"));
shun-iwasawa e87e08
  m_pixelSize.setItemUIName(L"24 bits", tr("24 bits"));
shun-iwasawa e87e08
  m_pixelSize.setItemUIName(L"32 bits", tr("32 bits"));
shun-iwasawa e87e08
  m_pixelSize.setItemUIName(L"48 bits", tr("48 bits"));
shun-iwasawa e87e08
  m_pixelSize.setItemUIName(L"64 bits", tr("64 bits"));
shun-iwasawa e87e08
  m_pixelSize.setItemUIName(L"8 bits (Greyscale)", tr("8 bits (Greyscale)"));
shun-iwasawa e87e08
  m_endianess.setQStringName(tr("Endianess"));
shun-iwasawa e87e08
  m_endianess.setItemUIName(L"Big Endian", tr("Big Endian"));
shun-iwasawa e87e08
  m_endianess.setItemUIName(L"Little Endian", tr("Little Endian"));
shun-iwasawa e87e08
  m_compressed.setQStringName(tr("RLE-Compressed"));
shun-iwasawa e87e08
}