Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_tzp.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "../tif/tifimage/tiff.h"
Toshihiro Shimizu 890ddd
#include "../tif/tifimage/tiffio.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "toonz.h"
Toshihiro Shimizu 890ddd
//#include "tmsg.h"
Toshihiro Shimizu 890ddd
//#include "file.h"
Toshihiro Shimizu 890ddd
//#include "img.h"
Toshihiro Shimizu 890ddd
//#include "tiff.h"
Toshihiro Shimizu 890ddd
//#include "tiffio.h"
Toshihiro Shimizu 890ddd
//#include "tim.h"
Toshihiro Shimizu 890ddd
//#include "history.h"
Toshihiro Shimizu 890ddd
//#include "../infoRegionP.h"
Toshihiro Shimizu 890ddd
//#include "version.h"
Toshihiro Shimizu 890ddd
//#include "machine.h"
Toshihiro Shimizu 890ddd
//#include "tenv.h"
Toshihiro Shimizu 890ddd
//#include "ImageP/img_security.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define ICON_WIDTH 100
Toshihiro Shimizu 890ddd
#define ICON_HEIGHT 90
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TBOOL int
Toshihiro Shimizu 890ddd
#define FALSE 0
Toshihiro Shimizu 890ddd
#define TRUE 1
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define UCHAR unsigned char
Toshihiro Shimizu 890ddd
#define USHORT unsigned short
Toshihiro Shimizu 890ddd
#define ULONG unsigned long
Toshihiro Shimizu 890ddd
#define UINT unsigned int
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define CASE                                                                   \
Shinya Kitaoka 120a6e
  break;                                                                       \
Shinya Kitaoka 120a6e
  case
Toshihiro Shimizu 890ddd
#define __OR case
Shinya Kitaoka 120a6e
#define DEFAULT                                                                \
Shinya Kitaoka 120a6e
  break;                                                                       \
Shinya Kitaoka 120a6e
  default
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef MAXINT
Toshihiro Shimizu 890ddd
#define MAXINT ((int)((~0U) >> 1))
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define NOT_LESS_THAN(A, B)
Shinya Kitaoka 120a6e
typedef struct { UCHAR r, g, b, m; } LPIXEL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TMALLOC(A, C) A = malloc(C * sizeof(A[0]));
Toshihiro Shimizu 890ddd
#define TFREE(A) free(A);
Toshihiro Shimizu 890ddd
#define NIL 0
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
typedef struct {
Shinya Kitaoka 120a6e
  int x1, y1, x2, y2;
Shinya Kitaoka 120a6e
  int x_offset, y_offset;         /*  offset all'interno della regione   */
Shinya Kitaoka 120a6e
  int xsize, ysize;               /*      dimensioni della regione       */
Shinya Kitaoka 120a6e
  int scanNrow, scanNcol;         /* righe e col. dell'immagine da scan. */
Shinya Kitaoka 120a6e
  int startScanRow, startScanCol; /*   offset nell'immagine da scandire  */
Shinya Kitaoka 120a6e
  int step;                       /*          fattore di scale           */
Shinya Kitaoka 120a6e
  int lx_in, ly_in;               /*    dimensioni immag. da scandire    */
Shinya Kitaoka 120a6e
  int verso_x, verso_y;           /* verso di scrittura nel buffer dest. */
Shinya Kitaoka 120a6e
  int buf_inc;                    /* incremento tra due pix. consecutivi */
Shinya Kitaoka 120a6e
  int sxpix, expix, sypix, eypix; /* pixel estremi del buffer di input   */
Toshihiro Shimizu 890ddd
} INFO_REGION;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
typedef struct {
Shinya Kitaoka 120a6e
  /*UCHAR    tone_offs; sempre 0 */
Shinya Kitaoka 120a6e
  UCHAR tone_bits;
Shinya Kitaoka 120a6e
  UCHAR color_offs;
Shinya Kitaoka 120a6e
  UCHAR color_bits;
Shinya Kitaoka 120a6e
  UCHAR pencil_offs;
Shinya Kitaoka 120a6e
  UCHAR pencil_bits;
Shinya Kitaoka 120a6e
  USHORT offset_mask;  // fa allo stesso tempo sia da offset che da maschera
Shinya Kitaoka 120a6e
  USHORT default_val;  // da utilizzare, p.es., per pixel fuori dall'immagine
Shinya Kitaoka 120a6e
  short n_tones;
Shinya Kitaoka 120a6e
  short n_colors;
Shinya Kitaoka 120a6e
  short n_pencils;
Toshihiro Shimizu 890ddd
} TCM_INFO;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static const TCM_INFO Tcm_old_default_info = {
Shinya Kitaoka 120a6e
    /*0,*/ 4, 4, 5, 9, 2, 0x0800, 0x080f, 16, 32, 4};
Shinya Kitaoka 120a6e
static const TCM_INFO Tcm_new_default_info = {
Shinya Kitaoka 120a6e
    /*0,*/ 4, 4, 7, 11, 5, 0x0000, 0x000f, 16, 128, 32};
Shinya Kitaoka 120a6e
static const TCM_INFO Tcm_24_default_info = {
Shinya Kitaoka 120a6e
    /*0,*/ 8, 8, 8, 16, 8, 0x0000, 0x00ff, 256, 256, 256};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_TONE_MASK(TCM) ((1U << (TCM).tone_bits) - 1U)
Shinya Kitaoka 120a6e
#define TCM_COLOR_MASK(TCM)                                                    \
Shinya Kitaoka 120a6e
  (((1U << (TCM).color_bits) - 1U) << (TCM).color_offs)
Shinya Kitaoka 120a6e
#define TCM_PENCIL_MASK(TCM)                                                   \
Shinya Kitaoka 120a6e
  (((1U << (TCM).pencil_bits) - 1U) << (TCM).pencil_offs)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_COLOR_INDEX(TCM, ID)                                               \
Shinya Kitaoka 120a6e
  ((ID) << (TCM).color_offs | ((TCM).n_tones - 1) | (TCM).offset_mask)
Shinya Kitaoka 120a6e
#define TCM_PENCIL_INDEX(TCM, ID)                                              \
Shinya Kitaoka 120a6e
  ((ID) << (TCM).pencil_offs | (TCM).offset_mask)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_INDEX_IS_COLOR_ONLY(TCM, INDEX)                                    \
Shinya Kitaoka 120a6e
  (((INDEX)&TCM_TONE_MASK(TCM)) == TCM_TONE_MASK(TCM))
Shinya Kitaoka 120a6e
#define TCM_INDEX_IS_PENCIL_ONLY(TCM, INDEX) (((INDEX)&TCM_TONE_MASK(TCM)) == 0)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_COLOR_ID(TCM, INDEX)                                               \
Shinya Kitaoka 120a6e
  ((int)(((INDEX) >> (TCM).color_offs) & ((1U << (TCM).color_bits) - 1U)))
Shinya Kitaoka 120a6e
#define TCM_PENCIL_ID(TCM, INDEX)                                              \
Shinya Kitaoka 120a6e
  ((int)(((INDEX) >> (TCM).pencil_offs) & ((1U << (TCM).pencil_bits) - 1U)))
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_MIN_CMAP_BUFFER_SIZE(TCM)                                          \
Shinya Kitaoka 120a6e
  ((((TCM).n_pencils - 1) << (TCM).pencil_offs |                               \
Shinya Kitaoka 120a6e
    ((TCM).n_colors - 1) << (TCM).color_offs | (TCM).n_tones - 1) +            \
Shinya Kitaoka 120a6e
   1)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_MIN_CMAP_COLBUFFER_SIZE(TCM) ((TCM).n_colors * (TCM).n_tones)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_MIN_CMAP_PENBUFFER_SIZE(TCM) ((TCM).n_pencils * (TCM).n_tones)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_CMAP_BUFFER_SIZE(TCM)                                              \
Shinya Kitaoka 120a6e
  (1 << ((TCM).pencil_bits + (TCM).color_bits + (TCM).tone_bits))
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_CMAP_COLBUFFER_SIZE(TCM) (1 << ((TCM).color_bits + (TCM).tone_bits))
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define TCM_CMAP_PENBUFFER_SIZE(TCM)                                           \
Shinya Kitaoka 120a6e
  (1 << ((TCM).pencil_bits + (TCM).tone_bits))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Next_img_read_with_extra = FALSE;
Shinya Kitaoka 120a6e
static int Read_with_extra          = FALSE;
Shinya Kitaoka 120a6e
#define SET_READ_WITH_EXTRA                                                    \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    Read_with_extra          = Next_img_read_with_extra;                       \
Shinya Kitaoka 120a6e
    Next_img_read_with_extra = FALSE;                                          \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
typedef struct {
Shinya Kitaoka 120a6e
  USHORT bits_per_sample, samples_per_pixel, photometric;
Shinya Kitaoka 120a6e
  int xsize, ysize, xSBsize, ySBsize, x0, y0;
Shinya Kitaoka 120a6e
  double x_dpi, y_dpi;
Shinya Kitaoka 120a6e
  double h_pos;  // in pixel, mentre TIFFTAG_XPOSITION e' in RESUNITS
Shinya Kitaoka 120a6e
  UCHAR extra_mask;
Shinya Kitaoka 120a6e
  TBOOL edu_file;
Toshihiro Shimizu 890ddd
} TZUP_FIELDS;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Tipi di  IMAGE
Toshihiro Shimizu 890ddd
enum img_type {
Shinya Kitaoka 120a6e
  IMG_NONE,
Shinya Kitaoka 120a6e
  CMAPPED,   /* fa riferimento a una color-map */
Shinya Kitaoka 120a6e
  CMAPPED24, /* 3 x 8 bit (ink, paint, ramp) + 8 bit extra (nel MSB) */
Shinya Kitaoka 120a6e
  RGB,       /* ogni pixel e' una terna red-green-blue   - 8bit per canale */
Shinya Kitaoka 120a6e
  RGB64,     /* ogni pixel e' una terna red-green-blue - 16bit per canale */
Shinya Kitaoka 120a6e
  GR8,       /* a toni di grigio */
Shinya Kitaoka 120a6e
  CMAP       /* color map */
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* pixel-map */
Toshihiro Shimizu 890ddd
struct s_pixmap {
Shinya Kitaoka 120a6e
  USHORT *buffer;
Shinya Kitaoka 120a6e
  UCHAR *extra;     /* patches etc. */
Shinya Kitaoka 120a6e
  UCHAR extra_mask; /* bits extra usati in buffer (CMAPPED24) o in extra */
Shinya Kitaoka 120a6e
  int xsize, ysize;
Shinya Kitaoka 120a6e
  int xSBsize, ySBsize, xD, yD; /* savebox */
Shinya Kitaoka 120a6e
  double x_dpi, y_dpi;
Shinya Kitaoka 120a6e
  double h_pos; /* in pixel */
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* color-map */
Toshihiro Shimizu 890ddd
struct s_cmap {
Shinya Kitaoka 120a6e
  char *name;        /* riferimento a CMAP esterna */
Shinya Kitaoka 120a6e
  LPIXEL *buffer;    /* buffer della colormap (premoltiplicata) */
Shinya Kitaoka 120a6e
  LPIXEL *penbuffer; /* buffer della componente di pencil per > 16 bits */
Shinya Kitaoka 120a6e
  LPIXEL *colbuffer; /* buffer della componente di color  per > 16 bits */
Shinya Kitaoka 120a6e
  LPIXEL *pencil;    /* i colori non premoltiplicati */
Shinya Kitaoka 120a6e
  LPIXEL *color;     /* tanti quanto dicono info.n_pencils e n_colors */
Shinya Kitaoka 120a6e
  TCM_INFO info;     /* vedi tcm.h */
Shinya Kitaoka 120a6e
  void *names;       /* nomi dei colori, gestiti da colorsdb */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* seguono i campi che andrebbero eliminati perche' doppioni */
Shinya Kitaoka 120a6e
  int offset;            /* == info.offset_mask */
Shinya Kitaoka 120a6e
  int pencil_n, color_n; /* == info.n_pencils e n_colors */
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Enum per l'identificazione dell' algoritmo di riduzione dei colori */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
typedef enum { CM_NONE = -1, CM_STANDARD, CM_CUSTOM } IMG_CM_ALGORITHM;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Struttura per le modalita' di scrittura dei formati di file supportati  */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
typedef struct {
Shinya Kitaoka 120a6e
  char rgb_is_compressed;
Shinya Kitaoka 120a6e
  char rgb_write_matte;
Shinya Kitaoka 120a6e
  char rgb_64_bits;
Shinya Kitaoka 120a6e
  char rgb_colorstyle; /* full color or greyscale */
Shinya Kitaoka 120a6e
  char tga_bytes_per_pixel;
Shinya Kitaoka 120a6e
  char tga_is_colormap;
Shinya Kitaoka 120a6e
  char tga_is_compressed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* Microsoft Windows Bitmap (BMP and DIB)
Shinya Kitaoka 120a6e
*
Shinya Kitaoka 120a6e
*                         | compression | colorstyle | numcolors |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*       BLack & White     |      0      |     0      |     2     |
Shinya Kitaoka 120a6e
* ----------------------------------------------------------------
Shinya Kitaoka 120a6e
*    16 Grey Tones        |      0      |    GR8     |     16    |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*    16 Grey Tones Comp.  |      1      |    GR8     |     16    |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*   256 Grey Tones        |      0      |    GR8     |     256   |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*   256 Grey Tones Comp.  |      1      |    GR8     |     256   |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*    16 Color Mapped      |      0      |  CMAPPED   |     16    |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*    16 Color Mapped Comp.|      1      |  CMAPPED   |     16    |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*   256 Color Mapped      |      0      |  CMAPPED   |     256   |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*   256 Color Mapped Comp.|      1      |  CMAPPED   |     256   |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*       Full Color        |      0      |    RGB     |     0     |
Shinya Kitaoka 120a6e
* ---------------------------------------------------------------
Shinya Kitaoka 120a6e
*
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  unsigned short bmp_compression;
Shinya Kitaoka 120a6e
  unsigned short bmp_colorstyle;
Shinya Kitaoka 120a6e
  unsigned short bmp_numcolors;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*---------------------------------------------------------------*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  unsigned int jpg_quality;
Shinya Kitaoka 120a6e
  unsigned int jpg_smoothing;
Shinya Kitaoka 120a6e
  unsigned int jpg_components;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*---------------------------------------------------------------*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  unsigned short tif_compression;
Shinya Kitaoka 120a6e
  unsigned short tif_orientation;
Shinya Kitaoka 120a6e
  unsigned short tif_photometric;
Shinya Kitaoka 120a6e
  unsigned short tif_bits_per_sample;
Shinya Kitaoka 120a6e
  unsigned short tif_samples_per_pixel;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*---------------------------------------------------------------*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  IMG_CM_ALGORITHM cm_algorithm;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*---------------------------------------------------------------*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*TNZMOVIE_QUALITY      pct_quality;
Shinya Kitaoka 120a6e
TNZMOVIE_COMPRESSION  pct_compression;
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  /*---------------------------------------------------------------*/
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
TNZMOVIE_QUALITY     mov_quality;
Shinya Kitaoka 120a6e
TNZMOVIE_COMPRESSION mov_compression;
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  int avi_bpp;
Shinya Kitaoka 120a6e
  char *avi_compression;
Toshihiro Shimizu 890ddd
} IMG_IO_SETTINGS;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Struttura IMAGE */
Toshihiro Shimizu 890ddd
typedef struct s_image {
Shinya Kitaoka 120a6e
  enum img_type type;
Shinya Kitaoka 120a6e
  char *filename;
Shinya Kitaoka 120a6e
  char *history;
Shinya Kitaoka 120a6e
  struct s_pixmap icon;   /* Icon */
Shinya Kitaoka 120a6e
  struct s_cmap cmap;     /* Colormap */
Shinya Kitaoka 120a6e
  struct s_pixmap pixmap; /* Pixel Map */
Shinya Kitaoka 120a6e
  IMG_IO_SETTINGS io_settings;
Toshihiro Shimizu 890ddd
} IMAGE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern int Silent_tiff_print_error;
Toshihiro Shimizu 890ddd
extern int Tiff_ignore_missing_internal_colormap;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int write_rgb_image(IMAGE *image, TIFF *tif),
Shinya Kitaoka 120a6e
    write_cmapped_image(IMAGE *image, TIFF *tif),
Shinya Kitaoka 120a6e
    write_cmapped24_image(IMAGE *image, TIFF *tif),
Shinya Kitaoka 120a6e
    write_extra(IMAGE *image, TIFF *tif),
Shinya Kitaoka 120a6e
    get_tzup_fields(TIFF *tif, TZUP_FIELDS *tzup_f),
Shinya Kitaoka 120a6e
    get_bits_per_sample(TIFF *tif, USHORT *bps),
Shinya Kitaoka 120a6e
    get_samples_per_pixel(TIFF *tif, USHORT *spp),
Shinya Kitaoka 120a6e
    get_image_sizes(TIFF *tif, int *xsize, int *ysize),
Shinya Kitaoka 120a6e
    get_photometric(TIFF *tif, USHORT *pm),
Shinya Kitaoka 120a6e
    get_resolutions(TIFF *tif, double *x_dpi, double *y_dpi),
Shinya Kitaoka 120a6e
    get_history(TIFF *tif, char **history),
Shinya Kitaoka 120a6e
    get_compression(TIFF *tif, int *compression),
Shinya Kitaoka 120a6e
    get_rows_per_strip(TIFF *tif, long *rowperstrip),
Shinya Kitaoka 120a6e
    get_tag_software(TIFF *tif, char *tag_software),
Shinya Kitaoka 120a6e
    get_orientation(TIFF *tif, int *orientation);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static bool scanline_needs_swapping(TIFF *tfp);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void get_planarconfig(TIFF *tif, USHORT *planargonfig);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
static void get_image_offsets_and_dimensions(TIFF *tif, int xSBsize,
Shinya Kitaoka 120a6e
                                             int ySBsize, int *x0, int *y0,
Shinya Kitaoka 120a6e
                                             int *xsize, int *ysize,
Shinya Kitaoka 120a6e
                                             double *h_pos, UCHAR *extra_mask,
Shinya Kitaoka 120a6e
                                             TBOOL *edu_file),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    get_plt_name(char *filename, char *pltname),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    clear_image_buffer_16(IMAGE *img, USHORT bg_val),
Shinya Kitaoka 120a6e
    clear_image_buffer_24(IMAGE *img, ULONG bg_val),
Shinya Kitaoka 120a6e
    clear_extra(IMAGE *img, UCHAR bg_val),
Shinya Kitaoka 120a6e
    clear_image_region_buffer_16(USHORT *buffer, int x0, int y0,
Shinya Kitaoka 120a6e
                                 int clear_xsize, int clear_ysize, int wrap_x,
Shinya Kitaoka 120a6e
                                 USHORT bg_val),
Shinya Kitaoka 120a6e
    clear_image_region_buffer_24(ULONG *buffer, int x0, int y0, int clear_xsize,
Shinya Kitaoka 120a6e
                                 int clear_ysize, int wrap_x, ULONG bg_val),
Shinya Kitaoka 120a6e
    clear_extra_region(UCHAR *extra, int x0, int y0, int clear_xsize,
Shinya Kitaoka 120a6e
                       int clear_ysize, int wrap_x, UCHAR bg_val);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
static int get_image(TIFF *tif, IMAGE *image),
Shinya Kitaoka 120a6e
    get_image_contig_16(TIFF *tif, IMAGE *image),
Shinya Kitaoka 120a6e
    get_image_contig_24(TIFF *tif, IMAGE *image),
Shinya Kitaoka 120a6e
    get_icon(TIFF *tif, IMAGE *image), get_extra(TIFF *tif, IMAGE *image);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static char Verbose = 0; /* Variabile per debug */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static UINT get_output_compression(void) {
Shinya Kitaoka 120a6e
  static UINT output_compression = 0;
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
char *s;
Toshihiro Shimizu 890ddd
if ( !output_compression)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
if (tenv_get_var_s("TOONZ_TZUP_COMPRESSION", &s))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
if (strstr (s, "rle") || strstr (s, "RLE"))
Shinya Kitaoka 120a6e
output_compression = COMPRESSION_TOONZ1;
Shinya Kitaoka 120a6e
else
Shinya Kitaoka 120a6e
output_compression = COMPRESSION_LZW;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
else
Shinya Kitaoka 120a6e
output_compression = COMPRESSION_LZW;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  return output_compression;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef CICCIO
Shinya Kitaoka 120a6e
int img_write_tzup(unsigned short *filename, IMAGE *image) {
Shinya Kitaoka 120a6e
  TIFF *tfp;
Shinya Kitaoka 120a6e
  int bits_per_sample, samples_per_pixel, photometric, planar_config;
Shinya Kitaoka 120a6e
  int orientation, rows_per_strip, bytes_per_line;
Shinya Kitaoka 120a6e
  int width, height, row, scanline, cmap_size, i;
Shinya Kitaoka 120a6e
  char str[200], *history;
Shinya Kitaoka 120a6e
  UCHAR *outbuf;
Shinya Kitaoka 120a6e
  USHORT window[TOONZWINDOW_COUNT];
Shinya Kitaoka 120a6e
  USHORT palette[TOONZPALETTE_COUNT];
Shinya Kitaoka 120a6e
  TCM_INFO *tcm;
Shinya Kitaoka 120a6e
  UINT tif_compression;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* CHECK_IMAGEDLL_LICENSE_AND_GET_IMG_LICENSE_ATTR */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 1;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 1;
Shinya Kitaoka 120a6e
  tfp                                   = TIFFOpen(filename, "w");
Shinya Kitaoka 120a6e
  if (!tfp) {
Shinya Kitaoka 120a6e
    /*throw "unable to open file for output"; , filename);*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE RGB : bits_per_sample       = 8;
Shinya Kitaoka 120a6e
    samples_per_pixel                = 4;
Shinya Kitaoka 120a6e
    photometric                      = PHOTOMETRIC_RGB;
Shinya Kitaoka 120a6e
    planar_config                    = PLANARCONFIG_CONTIG;
Shinya Kitaoka 120a6e
    CASE CMAPPED : bits_per_sample   = 16;
Shinya Kitaoka 120a6e
    samples_per_pixel                = 1;
Shinya Kitaoka 120a6e
    photometric                      = PHOTOMETRIC_PALETTE;
Shinya Kitaoka 120a6e
    planar_config                    = PLANARCONFIG_CONTIG;
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : bits_per_sample = 32;
Shinya Kitaoka 120a6e
    samples_per_pixel                = 1;
Shinya Kitaoka 120a6e
    photometric                      = PHOTOMETRIC_PALETTE;
Shinya Kitaoka 120a6e
    planar_config                    = PLANARCONFIG_CONTIG;
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    /*tmsg_error("bad image type writing file %s", filename);*/
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  orientation = ORIENTATION_BOTLEFT;
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
width  = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
height = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  width  = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  height = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->cmap.info.n_pencils && image->cmap.info.n_colors)
Shinya Kitaoka 120a6e
    tcm = &image->cmap.info;
Shinya Kitaoka 120a6e
  else if (image->type == CMAPPED24)
Shinya Kitaoka 120a6e
    tcm = (TCM_INFO *)&Tcm_24_default_info;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    tcm = (TCM_INFO *)&Tcm_new_default_info;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->cmap.offset)
Shinya Kitaoka 120a6e
    cmap_size = TCM_MIN_CMAP_BUFFER_SIZE(*tcm);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    cmap_size = TCM_CMAP_BUFFER_SIZE(*tcm);
Shinya Kitaoka 120a6e
  palette[0]  = (image->cmap.offset > 0) ? 4 : 3;
Shinya Kitaoka 120a6e
  palette[1]  = image->cmap.offset;
Shinya Kitaoka 120a6e
  palette[2]  = cmap_size <= 0xffff ? cmap_size : 0;
Shinya Kitaoka 120a6e
  palette[3]  = 0 /* tcm->tone_offs */;
Shinya Kitaoka 120a6e
  palette[4]  = tcm->tone_bits;
Shinya Kitaoka 120a6e
  palette[5]  = tcm->color_offs;
Shinya Kitaoka 120a6e
  palette[6]  = tcm->color_bits;
Shinya Kitaoka 120a6e
  palette[7]  = tcm->pencil_offs;
Shinya Kitaoka 120a6e
  palette[8]  = tcm->pencil_bits;
Shinya Kitaoka 120a6e
  palette[9]  = tcm->offset_mask;
Shinya Kitaoka 120a6e
  palette[10] = tcm->n_colors;
Shinya Kitaoka 120a6e
  palette[11] = tcm->n_pencils;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 12; i < TOONZPALETTE_COUNT; i++) palette[i] = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tif_compression = get_output_compression();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_TOONZPALETTE, palette);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_IMAGEWIDTH, width);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_IMAGELENGTH, height);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_ORIENTATION, orientation);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_PLANARCONFIG, planar_config);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_PHOTOMETRIC, photometric);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_COMPRESSION, tif_compression);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* NOTARE CHE VA COMPLETATA LA PARTE RELAITVA AL SETTAGGIO DELLA RISOLUZIONE
Shinya Kitaoka 120a6e
*
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_XRESOLUTION, image->pixmap.x_dpi);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_YRESOLUTION, image->pixmap.y_dpi);
Shinya Kitaoka 120a6e
  switch (orientation) {
Shinya Kitaoka 120a6e
    CASE ORIENTATION_BOTLEFT
Shinya Kitaoka 120a6e
        : __OR ORIENTATION_BOTRIGHT
Shinya Kitaoka 120a6e
          : __OR ORIENTATION_TOPLEFT
Shinya Kitaoka 120a6e
            : __OR ORIENTATION_TOPRIGHT
Shinya Kitaoka 120a6e
              : if (image->pixmap.x_dpi) TIFFSetField(
Shinya Kitaoka 120a6e
                    tfp, TIFFTAG_XPOSITION,
Shinya Kitaoka 120a6e
                    image->pixmap.h_pos / image->pixmap.x_dpi + 8.0);
Shinya Kitaoka 120a6e
    CASE ORIENTATION_LEFTBOT
Shinya Kitaoka 120a6e
        : __OR ORIENTATION_RIGHTBOT
Shinya Kitaoka 120a6e
          : __OR ORIENTATION_LEFTTOP
Shinya Kitaoka 120a6e
            : __OR ORIENTATION_RIGHTTOP
Shinya Kitaoka 120a6e
              : if (image->pixmap.y_dpi) TIFFSetField(
Shinya Kitaoka 120a6e
                    tfp, TIFFTAG_XPOSITION,
Shinya Kitaoka 120a6e
                    image->pixmap.h_pos / image->pixmap.y_dpi + 8.0);
Toshihiro Shimizu 890ddd
  }
Shinya Kitaoka 120a6e
  /*sprintf(str, "TOONZ %s", versione_del_software);*/
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_SOFTWARE, str);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* Aggiungo le informazioni relative alla savebox a all'history */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  window[0] = image->pixmap.xD;
Shinya Kitaoka 120a6e
  window[1] = image->pixmap.yD;
Shinya Kitaoka 120a6e
  window[2] = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  window[3] = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  window[4] = image->pixmap.extra_mask;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 5; i < TOONZWINDOW_COUNT - 1; i++) window[i] = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  window[TOONZWINDOW_COUNT - 1] = 0;
Shinya Kitaoka 120a6e
  /*  (Img_license_attr & TA_TOONZ_EDU) != 0;*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_TOONZWINDOW, window);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  history = build_history();
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
if (image->history)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
switch(check_history(image->history, history))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
CASE APPEND:
Shinya Kitaoka 120a6e
image->history = append_history(image->history, history);
Shinya Kitaoka 120a6e
CASE REPLACE:
Shinya Kitaoka 120a6e
image->history = replace_last_history(image->history, history);
Shinya Kitaoka 120a6e
DEFAULT:
Shinya Kitaoka 120a6e
tmsg_error("Internal error: bad history type");
Shinya Kitaoka 120a6e
abort();
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
free (history);
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
else
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  image->history = history;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_TOONZHISTORY, image->history);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bytes_per_line = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* massima lunghezza di bytes in una strip e' 8k
Shinya Kitaoka 120a6e
* vedi Graphics File Formats pag.48
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  if (planar_config == PLANARCONFIG_CONTIG)
Shinya Kitaoka 120a6e
    rows_per_strip = (8 * 1024) / bytes_per_line;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    rows_per_strip = 1L;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_ROWSPERSTRIP,
Shinya Kitaoka 120a6e
               rows_per_strip == 0 ? 1L : rows_per_strip);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE RGB : if (!write_rgb_image(image, tfp)) {
Shinya Kitaoka 120a6e
      // tmsg_error("unable to write buffer to file %s", filename);
Shinya Kitaoka 120a6e
      goto bad;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    CASE CMAPPED : if (!write_cmapped_image(image, tfp)) {
Shinya Kitaoka 120a6e
      // tmsg_error("unable to write buffer to file %s", filename);
Shinya Kitaoka 120a6e
      goto bad;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : if (!write_cmapped24_image(image, tfp)) {
Shinya Kitaoka 120a6e
      // tmsg_error("unable to write buffer to file %s", filename);
Shinya Kitaoka 120a6e
      goto bad;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    // tmsg_error("bad image type writing file %s", filename);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int write_rgb_image(IMAGE *image, TIFF *tfp) {
Shinya Kitaoka 120a6e
  int scanlinesize;
Shinya Kitaoka 120a6e
  UCHAR *outbuf, *buf;
Shinya Kitaoka 120a6e
  int row, lx, ly, x, wrapx, x0, y0;
Shinya Kitaoka 120a6e
  LPIXEL *gl_buf, *tmp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx    = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly    = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x0    = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0    = image->pixmap.yD;
Shinya Kitaoka 120a6e
  wrapx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tmp          = (LPIXEL *)image->pixmap.buffer + y0 * wrapx + x0;
Shinya Kitaoka 120a6e
  scanlinesize = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
  outbuf       = new UCHAR[scanlinesize];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!outbuf) return FALSE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
    buf    = outbuf;
Shinya Kitaoka 120a6e
    gl_buf = tmp;
Shinya Kitaoka 120a6e
    for (x = 0; x < lx; x++) {
Shinya Kitaoka 120a6e
      *buf++ = gl_buf->r;
Shinya Kitaoka 120a6e
      *buf++ = gl_buf->g;
Shinya Kitaoka 120a6e
      *buf++ = gl_buf->b;
Shinya Kitaoka 120a6e
      *buf++ = gl_buf->m;
Shinya Kitaoka 120a6e
      gl_buf++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (TIFFWriteScanline(tfp, outbuf, row, 0) < 0) goto bad;
Shinya Kitaoka 120a6e
    tmp += wrapx;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  delete[] outbuf;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->pixmap.extra_mask) write_extra(image, tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  // tmsg_error("write_scan_line failed at row %d", row);
Shinya Kitaoka 120a6e
  delete[] outbuf;
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int write_cmapped_image(IMAGE *image, TIFF *tfp) {
Shinya Kitaoka 120a6e
  int scanline, lx, ly, x0, y0, wrapx;
Shinya Kitaoka 120a6e
  int row;
Shinya Kitaoka 120a6e
  UCHAR *outbuf;
Shinya Kitaoka 120a6e
  USHORT *tmp;
Shinya Kitaoka 120a6e
  int tmp_icon;
Shinya Kitaoka 120a6e
  struct s_pixmap ori_icon;
Shinya Kitaoka 120a6e
  UINT tif_compression;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tif_compression = get_output_compression();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tmp_icon = FALSE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx    = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly    = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x0    = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0    = image->pixmap.yD;
Shinya Kitaoka 120a6e
  wrapx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tmp    = image->pixmap.buffer + y0 * wrapx + x0;
Shinya Kitaoka 120a6e
  outbuf = 0;
Shinya Kitaoka 120a6e
  for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
    outbuf = (UCHAR *)tmp;
Shinya Kitaoka 120a6e
    if (TIFFWriteScanline(tfp, outbuf, row, 0) < 0) goto bad;
Shinya Kitaoka 120a6e
    tmp += wrapx;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->pixmap.extra_mask && !image->icon.buffer) {
Shinya Kitaoka 120a6e
    ori_icon = image->icon;
Shinya Kitaoka 120a6e
    // make_icon (image, ICON_WIDTH, ICON_HEIGHT);
Shinya Kitaoka 120a6e
    tmp_icon = TRUE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->icon.buffer) {
Shinya Kitaoka 120a6e
    if (!TIFFFlush(tfp)) {
Shinya Kitaoka 120a6e
      // tmsg_error("Unable to flush data to .tz(up) file");
Shinya Kitaoka 120a6e
      goto bad;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    lx = image->icon.xsize;
Shinya Kitaoka 120a6e
    ly = image->icon.ysize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_SUBFILETYPE, 1);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_BITSPERSAMPLE, 16);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_SAMPLESPERPIXEL, 1);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_IMAGEWIDTH, lx);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_IMAGELENGTH, ly);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_COMPRESSION, tif_compression);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    scanline = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
    outbuf   = (UCHAR *)image->icon.buffer;
Shinya Kitaoka 120a6e
    for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
      if (TIFFWriteScanline(tfp, outbuf, row, 0) < 0) {
Shinya Kitaoka 120a6e
        // tmsg_error("error writing icon to .tz(up) file");
Shinya Kitaoka 120a6e
        goto bad;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      outbuf += scanline;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->pixmap.extra_mask) write_extra(image, tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (tmp_icon && image->icon.buffer) {
Shinya Kitaoka 120a6e
    TFREE(image->icon.buffer)
Shinya Kitaoka 120a6e
    image->icon = ori_icon;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  if (tmp_icon && image->icon.buffer) {
Shinya Kitaoka 120a6e
    TFREE(image->icon.buffer)
Shinya Kitaoka 120a6e
    image->icon = ori_icon;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int write_cmapped24_image(IMAGE *image, TIFF *tfp) {
Shinya Kitaoka 120a6e
  int scanline, lx, ly, x0, y0, wrapx;
Shinya Kitaoka 120a6e
  int row;
Shinya Kitaoka 120a6e
  UCHAR *outbuf;
Shinya Kitaoka 120a6e
  ULONG *tmp;
Shinya Kitaoka 120a6e
  UINT tif_compression;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tif_compression = get_output_compression();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx    = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly    = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x0    = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0    = image->pixmap.yD;
Shinya Kitaoka 120a6e
  wrapx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tmp    = (ULONG *)image->pixmap.buffer + y0 * wrapx + x0;
Shinya Kitaoka 120a6e
  outbuf = 0;
Shinya Kitaoka 120a6e
  for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
    outbuf = (UCHAR *)tmp;
Shinya Kitaoka 120a6e
    if (TIFFWriteScanline(tfp, outbuf, row, 0) < 0) goto bad;
Shinya Kitaoka 120a6e
    tmp += wrapx;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->icon.buffer) {
Shinya Kitaoka 120a6e
    if (!TIFFFlush(tfp)) {
Shinya Kitaoka 120a6e
      // tmsg_error("Unable to flush data to .tz(up) file");
Shinya Kitaoka 120a6e
      goto bad;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    lx = image->icon.xsize;
Shinya Kitaoka 120a6e
    ly = image->icon.ysize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_SUBFILETYPE, 1);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_BITSPERSAMPLE, 32);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_SAMPLESPERPIXEL, 1);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_IMAGEWIDTH, lx);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_IMAGELENGTH, ly);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
Shinya Kitaoka 120a6e
    TIFFSetField(tfp, TIFFTAG_COMPRESSION, tif_compression);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    scanline = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
    outbuf   = (UCHAR *)image->icon.buffer;
Shinya Kitaoka 120a6e
    for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
      if (TIFFWriteScanline(tfp, outbuf, row, 0) < 0) {
Shinya Kitaoka 120a6e
        // tmsg_error("error writing icon to .tz(up) file");
Shinya Kitaoka 120a6e
        goto bad;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      outbuf += scanline;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int write_extra(IMAGE *image, TIFF *tfp) {
Shinya Kitaoka 120a6e
  int lx, ly, x0, y0, wrapx, row;
Shinya Kitaoka 120a6e
  UCHAR *extra;
Shinya Kitaoka 120a6e
  UINT tif_compression;
Shinya Kitaoka 120a6e
  int rowsperstrip;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!image->pixmap.extra_mask) return TRUE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tif_compression = get_output_compression();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFFlush(tfp)) {
Shinya Kitaoka 120a6e
    // tmsg_error("Unable to flush data to .tz(up) file");
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  lx    = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly    = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x0    = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0    = image->pixmap.yD;
Shinya Kitaoka 120a6e
  wrapx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_SUBFILETYPE, 1);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_BITSPERSAMPLE, 8);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_SAMPLESPERPIXEL, 1);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_IMAGEWIDTH, lx);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_IMAGELENGTH, ly);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_COMPRESSION, tif_compression);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rowsperstrip = (8 * 1024) / lx; /* contig, 1 byte per pixel */
Shinya Kitaoka 120a6e
  NOT_LESS_THAN(1, rowsperstrip)
Shinya Kitaoka 120a6e
  TIFFSetField(tfp, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  extra = image->pixmap.extra + y0 * wrapx + x0;
Shinya Kitaoka 120a6e
  for (row = 0; row < ly; row++) {
Shinya Kitaoka 120a6e
    if (TIFFWriteScanline(tfp, extra, row, 0) < 0) goto bad;
Shinya Kitaoka 120a6e
    extra += wrapx;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  // tmsg_error ("error writing extra information to .tz(up) file");
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void next_img_read_with_extra(void) { Next_img_read_with_extra = TRUE; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageP TImageReaderTZP::load() {
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
FILE *fp;
Shinya Kitaoka 120a6e
if ((fp = _wfopen(getFilePath().getWideString().c_str(), L"rb")) == NULL)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
throw TImageException(getFilePath(),"can't open file");
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
//{
Shinya Kitaoka 120a6e
//  fclose(fp);
Shinya Kitaoka 120a6e
//  throw TImageException(getFilePath(),"invalid file format");
Shinya Kitaoka 120a6e
//}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TRaster32P raster(lx,ly);
Shinya Kitaoka 120a6e
TPixel32* row;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TRasterImageP rasImage(raster);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TImageP image(rasImage);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  wstring fn = getFilePath().getWideString();
Shinya Kitaoka 120a6e
  TIFF *tfp;
Shinya Kitaoka 120a6e
  IMAGE *image = NIL;
Shinya Kitaoka 120a6e
  TZUP_FIELDS tzup_f;
Shinya Kitaoka 120a6e
  // char pltname[1024];
Shinya Kitaoka 120a6e
  USHORT *window = NIL;
Shinya Kitaoka 120a6e
  USHORT *palette; /*  [TOONZPALETTE_COUNT] */
Shinya Kitaoka 120a6e
  // int max_n_colors, max_n_pencils;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
CHECK_IMAGEDLL_LICENSE_AND_GET_IMG_LICENSE_ATTR
Shinya Kitaoka 120a6e
SET_READ_WITH_EXTRA
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 1;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 1;
Shinya Kitaoka 120a6e
  tfp                                   = TIFFOpen(fn.c_str(), "r");
Shinya Kitaoka 120a6e
  if (!tfp) return TImageP();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!get_tzup_fields(tfp, &tzup_f)) return TImageP();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
if (tzup_f.edu_file && !(Img_license_attr & TA_TOONZ_EDU))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
char str[1024];
Shinya Kitaoka 120a6e
BUILD_EDU_ERROR_STRING(str)
Shinya Kitaoka 120a6e
tmsg_error (str);
Shinya Kitaoka 120a6e
goto bad;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // image = new IMAGE; // new_img();
Shinya Kitaoka 120a6e
  // if (!image)
Shinya Kitaoka 120a6e
  //  goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_TOONZPALETTE, &palette)) {
Shinya Kitaoka 120a6e
    // image->cmap.info = Tcm_old_default_info;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    //// image->cmap.info.tone_offs   = palette[3]; sempre 0
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
image->cmap.info.tone_bits   = (UCHAR)palette[4];
Shinya Kitaoka 120a6e
image->cmap.info.color_offs  = (UCHAR)palette[5];
Shinya Kitaoka 120a6e
image->cmap.info.color_bits  = (UCHAR)palette[6];
Shinya Kitaoka 120a6e
image->cmap.info.pencil_offs = (UCHAR)palette[7];
Shinya Kitaoka 120a6e
image->cmap.info.pencil_bits = (UCHAR)palette[8];
Shinya Kitaoka 120a6e
image->cmap.info.offset_mask = palette[9];
Shinya Kitaoka 120a6e
image->cmap.info.n_tones     = 1 << palette[4];
Shinya Kitaoka 120a6e
image->cmap.info.n_colors    = palette[10];
Shinya Kitaoka 120a6e
image->cmap.info.n_pencils   = palette[11];
Shinya Kitaoka 120a6e
image->cmap.info.default_val = (image->cmap.info.n_tones-1) |
Shinya Kitaoka 120a6e
                      image->cmap.info.offset_mask;
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* estendo la palette */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
max_n_colors  = 1 << image->cmap.info.color_bits;
Toshihiro Shimizu 890ddd
max_n_pencils = 1 << image->cmap.info.pencil_bits;
Toshihiro Shimizu 890ddd
if (max_n_colors  > image->cmap.info.n_colors ||
Shinya Kitaoka 120a6e
max_n_pencils > image->cmap.info.n_pencils)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
image->cmap.info.n_colors  = max_n_colors;
Shinya Kitaoka 120a6e
image->cmap.info.n_pencils = max_n_pencils;
Shinya Kitaoka 120a6e
image->cmap.color_n  = image->cmap.info.n_colors;
Shinya Kitaoka 120a6e
image->cmap.pencil_n = image->cmap.info.n_pencils;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterCM16P raster(tzup_f.xsize, tzup_f.ysize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ///===========
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    bool swapNeeded = scanline_needs_swapping(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int x0    = tzup_f.x0;
Shinya Kitaoka 120a6e
    int y0    = tzup_f.y0;
Shinya Kitaoka 120a6e
    int lx    = tzup_f.xSBsize;
Shinya Kitaoka 120a6e
    int ly    = tzup_f.ySBsize;
Shinya Kitaoka 120a6e
    int x1    = x0 + lx - 1;
Shinya Kitaoka 120a6e
    int y1    = y0 + ly - 1;
Shinya Kitaoka 120a6e
    int xsize = tzup_f.xsize;
Shinya Kitaoka 120a6e
    int ysize = tzup_f.ysize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int wrap = tzup_f.xsize;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    assert(raster->getBounds().contains(TRect(x0, y0, x1, y1)));
Shinya Kitaoka 120a6e
    raster->fillOutside(TRect(x0, y0, x1, y1), TPixelCM16(0, 0, 15));
Shinya Kitaoka 120a6e
    raster->lock();
Shinya Kitaoka 120a6e
    for (int y = y0; y <= y1; y++) {
Shinya Kitaoka 120a6e
      TPixelCM16 *row = raster->pixels(y) + x0;
Shinya Kitaoka 120a6e
      if (TIFFReadScanline(tfp, (UCHAR *)row, y - y0, 0) < 0)
Shinya Kitaoka 120a6e
        if (swapNeeded) TIFFSwabArrayOfShort((USHORT *)row, lx);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    raster->unlock();
Shinya Kitaoka 120a6e
    /*
Toshihiro Shimizu 890ddd
buf   = image->pixmap.buffer;
Toshihiro Shimizu 890ddd
default_val = image->cmap.info.default_val;
Toshihiro Shimizu 890ddd
for (y = 0; y < y0; y++)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
pix = buf + y * wrap;
Shinya Kitaoka 120a6e
for (x = 0; x < xsize; x++)
Shinya Kitaoka 120a6e
*pix++ = default_val;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
for ( ; y <= y1; y++)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
pix = buf + y * wrap;
Shinya Kitaoka 120a6e
for (x = 0; x < x0; x++)
Shinya Kitaoka 120a6e
*pix++ = default_val;
Shinya Kitaoka 120a6e
if (TIFFReadScanline (tf, (UCHAR *)pix, y - y0, 0) < 0)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
static int gia_dato = FALSE;
Shinya Kitaoka 120a6e
if ( !gia_dato)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
//tmsg_error("bad data read on line %d", y);
Shinya Kitaoka 120a6e
gia_dato = TRUE;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
memset (pix, 0, lx * sizeof(*pix));
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
if (swap_needed)
Shinya Kitaoka 120a6e
TIFFSwabArrayOfShort((USHORT *)pix, lx);
Shinya Kitaoka 120a6e
pix += lx;
Shinya Kitaoka 120a6e
for (x = x1 + 1; x < xsize; x++)
Shinya Kitaoka 120a6e
*pix++ = default_val;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
for ( ; y < ysize; y++)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
pix = buf + y * wrap;
Shinya Kitaoka 120a6e
for (x = 0; x < xsize; x++)
Shinya Kitaoka 120a6e
*pix++ = default_val;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ///===========
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  switch (tzup_f.photometric) {
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_MINISBLACK : __OR PHOTOMETRIC_MINISWHITE
Shinya Kitaoka 120a6e
                                  : __OR PHOTOMETRIC_RGB
Shinya Kitaoka 120a6e
                                    :;  // image->type = RGB;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_PALETTE :
Shinya Kitaoka 120a6e
        /*
Shinya Kitaoka 120a6e
if (tzup_f.bits_per_sample == 32)
Shinya Kitaoka 120a6e
image->type = CMAPPED24;
Shinya Kitaoka 120a6e
else
Shinya Kitaoka 120a6e
image->type = CMAPPED;
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        DEFAULT:
Shinya Kitaoka 120a6e
        // tmsg_error("bad photometric interpretation in file %s", filename);
Shinya Kitaoka 120a6e
        // goto bad;
Shinya Kitaoka 120a6e
        ;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // if (!get_history(tfp, &image->history))
Shinya Kitaoka 120a6e
  //  image->history = NIL;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // image->pixmap.extra_mask = tzup_f.extra_mask;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // if (!allocate_pixmap(image, tzup_f.xsize, tzup_f.ysize))
Shinya Kitaoka 120a6e
  // image->pixmap.buffer = new USHORT[tzup_f.xsize*tzup_f.ysize];
Shinya Kitaoka 120a6e
  // if(!image->pixmap.buffer)
Shinya Kitaoka 120a6e
  //  goto bad;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // image->pixmap.xD      = tzup_f.x0;
Shinya Kitaoka 120a6e
  // image->pixmap.yD      = tzup_f.y0;
Shinya Kitaoka 120a6e
  // image->pixmap.xSBsize = tzup_f.xSBsize;
Shinya Kitaoka 120a6e
  // image->pixmap.ySBsize = tzup_f.ySBsize;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
if (!get_image(tfp, image))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
//tmsg_error("no image while reading file %s", filename);
Shinya Kitaoka 120a6e
goto bad;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
if (!get_icon(tfp, image))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
//make_icon(image, ICON_WIDTH, ICON_HEIGHT);
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
if (image->pixmap.extra)
Shinya Kitaoka 120a6e
if ( !get_extra (tfp, image))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
//tmsg_error("missing extra information while reading file %s", filename);
Shinya Kitaoka 120a6e
goto bad;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
image->pixmap.x_dpi = tzup_f.x_dpi;
Toshihiro Shimizu 890ddd
image->pixmap.y_dpi = tzup_f.y_dpi;
Toshihiro Shimizu 890ddd
image->pixmap.h_pos = tzup_f.h_pos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Silent_tiff_print_error = 0;
Toshihiro Shimizu 890ddd
Tiff_ignore_missing_internal_colormap = 0;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // image->filename = 0; // strsave(filename);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // get_plt_name(filename, pltname);
Shinya Kitaoka 120a6e
  // image->cmap.name = 0; // strsave(pltname);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(0);
Shinya Kitaoka 120a6e
  return 0;
Shinya Kitaoka 120a6e
  // TToonzImageP toonzImage(raster);
Shinya Kitaoka 120a6e
  // return TImageP(toonzImage);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
if (image)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
//free_img(image);
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Silent_tiff_print_error = 0;
Shinya Kitaoka 120a6e
Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
if (tfp)
Shinya Kitaoka 120a6e
TIFFClose(tfp);
Shinya Kitaoka 120a6e
return NIL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static bool scanline_needs_swapping(TIFF *tfp) {
Shinya Kitaoka 120a6e
  USHORT compression;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIFFGetField(tfp, TIFFTAG_COMPRESSION, &compression);
Shinya Kitaoka 120a6e
  return compression == COMPRESSION_LZW && TIFFNeedSwab(tfp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int read_region_tzup_16(IMAGE *image, TIFF *tfp, char *filename,
Shinya Kitaoka 120a6e
                               INFO_REGION *region, int scale, UCHAR *buf,
Shinya Kitaoka 120a6e
                               int scanline_size, int rowperstrip, int wrap_out,
Shinya Kitaoka 120a6e
                               int xD_offset, int yD_offset) {
Shinya Kitaoka 120a6e
  USHORT *inp = NIL, *outp = NIL, *appo_outp = NIL;
Shinya Kitaoka 120a6e
  int row, nrow, rrow;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  appo_outp = image->pixmap.buffer + yD_offset * wrap_out + xD_offset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (Verbose) printf("Posizione in uscita: %d, %d\n", xD_offset, yD_offset);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* Puntatore per avanzamento nel buffer della regione */
Shinya Kitaoka 120a6e
  outp = appo_outp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  row = region->startScanRow;
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* Questa serie di scanline viene fatta perche' non viene
Shinya Kitaoka 120a6e
* accettato un accesso random alle righe del file.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  if (row > 0) {
Shinya Kitaoka 120a6e
    int c;
Shinya Kitaoka 120a6e
    c = (row / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
    for (; c < row; c++) {
Shinya Kitaoka 120a6e
      if (TIFFReadScanline(tfp, buf, c, 0) < 0) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad image data read on line %d of file %s", c, filename);
Shinya Kitaoka 120a6e
        return FALSE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (nrow = 0; nrow < region->scanNrow; nrow++) {
Shinya Kitaoka 120a6e
    appo_outp = outp;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, buf, row, 0) < 0) {
Shinya Kitaoka 120a6e
      // tmsg_error("bad image data read at line %d of file %s", row, filename);
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed)
Shinya Kitaoka 120a6e
      TIFFSwabArrayOfShort((USHORT *)buf, scanline_size / sizeof(USHORT));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    inp = (USHORT *)buf + region->startScanCol;
Shinya Kitaoka 120a6e
    for (rrow = 0; rrow < region->scanNcol; rrow++) {
Shinya Kitaoka 120a6e
      *outp++ = *inp;
Shinya Kitaoka 120a6e
      inp += scale;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (scale > 1) {
Shinya Kitaoka 120a6e
      register currRow = 0, stepRow = 1, nextRow = 0;
Shinya Kitaoka 120a6e
      if (row + scale > region->ly_in)
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        nextRow = row + scale;
Shinya Kitaoka 120a6e
      stepRow   = (nextRow / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
      for (currRow = stepRow; currRow < nextRow; currRow++) {
Shinya Kitaoka 120a6e
        if (TIFFReadScanline(tfp, buf, currRow, 0) < 0) {
Shinya Kitaoka 120a6e
          // tmsg_error("bad image data in file %s at line %d", filename,
Shinya Kitaoka 120a6e
          // currRow);
Shinya Kitaoka 120a6e
          return FALSE;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    outp = appo_outp + wrap_out;
Shinya Kitaoka 120a6e
    row += scale;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int read_region_tzup_24(IMAGE *image, TIFF *tfp, char *filename,
Shinya Kitaoka 120a6e
                               INFO_REGION *region, int scale, UCHAR *buf,
Shinya Kitaoka 120a6e
                               int scanline_size, int rowperstrip, int wrap_out,
Shinya Kitaoka 120a6e
                               int xD_offset, int yD_offset) {
Shinya Kitaoka 120a6e
  ULONG *inp = NIL, *outp = NIL, *appo_outp = NIL;
Shinya Kitaoka 120a6e
  int row, nrow, rrow;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  appo_outp = (ULONG *)image->pixmap.buffer + yD_offset * wrap_out + xD_offset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (Verbose) printf("Posizione in uscita: %d, %d\n", xD_offset, yD_offset);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* Puntatore per avanzamento nel buffer della regione */
Shinya Kitaoka 120a6e
  outp = appo_outp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  row = region->startScanRow;
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* Questa serie di scanline viene fatta perche' non viene
Shinya Kitaoka 120a6e
* accettato un accesso random alle righe del file.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  if (row > 0) {
Shinya Kitaoka 120a6e
    int c;
Shinya Kitaoka 120a6e
    c = (row / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
    for (; c < row; c++) {
Shinya Kitaoka 120a6e
      if (TIFFReadScanline(tfp, buf, c, 0) < 0) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad image data read on line %d of file %s", c, filename);
Shinya Kitaoka 120a6e
        return FALSE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (nrow = 0; nrow < region->scanNrow; nrow++) {
Shinya Kitaoka 120a6e
    appo_outp = outp;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, buf, row, 0) < 0) {
Shinya Kitaoka 120a6e
      // tmsg_error("bad image data read at line %d of file %s", row, filename);
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed)
Shinya Kitaoka 120a6e
      TIFFSwabArrayOfLong((TUINT32 *)buf, scanline_size / sizeof(ULONG));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    inp = (ULONG *)buf + region->startScanCol;
Shinya Kitaoka 120a6e
    for (rrow = 0; rrow < region->scanNcol; rrow++) {
Shinya Kitaoka 120a6e
      *outp++ = *inp;
Shinya Kitaoka 120a6e
      inp += scale;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (scale > 1) {
Shinya Kitaoka 120a6e
      register currRow = 0, stepRow = 1, nextRow = 0;
Shinya Kitaoka 120a6e
      if (row + scale > region->ly_in)
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        nextRow = row + scale;
Shinya Kitaoka 120a6e
      stepRow   = (nextRow / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
      for (currRow = stepRow; currRow < nextRow; currRow++) {
Shinya Kitaoka 120a6e
        if (TIFFReadScanline(tfp, buf, currRow, 0) < 0) {
Shinya Kitaoka 120a6e
          // tmsg_error("bad image data in file %s at line %d", filename,
Shinya Kitaoka 120a6e
          // currRow);
Shinya Kitaoka 120a6e
          return FALSE;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    outp = appo_outp + wrap_out;
Shinya Kitaoka 120a6e
    row += scale;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int read_region_extra(IMAGE *image, TIFF *tfp, char *filename,
Shinya Kitaoka 120a6e
                             INFO_REGION *region, int scale, UCHAR *buf,
Shinya Kitaoka 120a6e
                             int wrap_out, int xD_offset, int yD_offset) {
Shinya Kitaoka 120a6e
  UCHAR *inp = NIL, *outp = NIL, *appo_outp = NIL;
Shinya Kitaoka 120a6e
  int row, nrow, rrow;
Shinya Kitaoka 120a6e
  int scanline_size, rowperstrip;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFReadDirectory(tfp)) return FALSE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  scanline_size = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_ROWSPERSTRIP, &rowperstrip))
Shinya Kitaoka 120a6e
    rowperstrip = MAXINT / 2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  appo_outp = image->pixmap.extra + yD_offset * wrap_out + xD_offset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (Verbose)
Shinya Kitaoka 120a6e
    printf("Posizione in uscita (extra): %d, %d\n", xD_offset, yD_offset);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* Puntatore per avanzamento nel buffer della regione */
Shinya Kitaoka 120a6e
  outp = appo_outp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  row = region->startScanRow;
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
* Questa serie di scanline viene fatta perche' non viene
Shinya Kitaoka 120a6e
* accettato un accesso random alle righe del file.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  if (row > 0) {
Shinya Kitaoka 120a6e
    int c;
Shinya Kitaoka 120a6e
    c = (row / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
    for (; c < row; c++) {
Shinya Kitaoka 120a6e
      if (TIFFReadScanline(tfp, buf, c, 0) < 0) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad extra data read on line %d of file %s", c, filename);
Shinya Kitaoka 120a6e
        return FALSE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (nrow = 0; nrow < region->scanNrow; nrow++) {
Shinya Kitaoka 120a6e
    appo_outp = outp;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, buf, row, 0) < 0) {
Shinya Kitaoka 120a6e
      // tmsg_error("bad extra data read at line %d of file %s", row, filename);
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    inp = buf + region->startScanCol;
Shinya Kitaoka 120a6e
    for (rrow = 0; rrow < region->scanNcol; rrow++) {
Shinya Kitaoka 120a6e
      *outp++ = *inp;
Shinya Kitaoka 120a6e
      inp += scale;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (scale > 1) {
Shinya Kitaoka 120a6e
      register currRow = 0, stepRow = 1, nextRow = 0;
Shinya Kitaoka 120a6e
      if (row + scale > region->ly_in)
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        nextRow = row + scale;
Shinya Kitaoka 120a6e
      stepRow   = (nextRow / rowperstrip) * rowperstrip;
Shinya Kitaoka 120a6e
      for (currRow = stepRow; currRow < nextRow; currRow++) {
Shinya Kitaoka 120a6e
        if (TIFFReadScanline(tfp, buf, currRow, 0) < 0) {
Shinya Kitaoka 120a6e
          // tmsg_error("bad extra data in file %s at line %d", filename,
Shinya Kitaoka 120a6e
          // currRow);
Shinya Kitaoka 120a6e
          return FALSE;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    outp = appo_outp + wrap_out;
Shinya Kitaoka 120a6e
    row += scale;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
#ifdef CICCIO
Shinya Kitaoka 120a6e
IMAGE *img_read_region_tzup(unsigned short *filename, int x1, int y1, int x2,
Shinya Kitaoka 120a6e
                            int y2, int scale) {
Shinya Kitaoka 120a6e
  TIFF *tfp    = NIL;
Shinya Kitaoka 120a6e
  IMAGE *image = NIL;
Shinya Kitaoka 120a6e
  INFO_REGION region;
Shinya Kitaoka 120a6e
  TZUP_FIELDS tzup_f;
Shinya Kitaoka 120a6e
  char pltname[1024];
Shinya Kitaoka 120a6e
  UCHAR *buf = NIL;
Shinya Kitaoka 120a6e
  USHORT planarconfig;
Shinya Kitaoka 120a6e
  int rowperstrip;
Shinya Kitaoka 120a6e
  int xsize_out, ysize_out, clear_xsize, clear_ysize;
Shinya Kitaoka 120a6e
  int xD_offset, yD_offset;
Shinya Kitaoka 120a6e
  int x1_reg, y1_reg, x2_reg, y2_reg;
Shinya Kitaoka 120a6e
  int box_x1, box_y1, box_x2, box_y2;
Shinya Kitaoka 120a6e
  USHORT *palette; /*  [TOONZPALETTE_COUNT] */
Shinya Kitaoka 120a6e
  int scanline_size;
Shinya Kitaoka 120a6e
  int ret;
Shinya Kitaoka 120a6e
  int max_n_colors, max_n_pencils;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
CHECK_IMAGEDLL_LICENSE_AND_GET_IMG_LICENSE_ATTR
Toshihiro Shimizu 890ddd
SET_READ_WITH_EXTRA
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 1;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 1;
Shinya Kitaoka 120a6e
  tfp                                   = TIFFOpen(filename, "r");
Shinya Kitaoka 120a6e
  if (!tfp) return NIL;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!get_tzup_fields(tfp, &tzup_f)) goto bad;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
if (tzup_f.edu_file && !(Img_license_attr & TA_TOONZ_EDU))
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
char str[1024];
Shinya Kitaoka 120a6e
BUILD_EDU_ERROR_STRING(str)
Shinya Kitaoka 120a6e
tmsg_error (str);
Shinya Kitaoka 120a6e
goto bad;
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIFFGetField(tfp, TIFFTAG_ROWSPERSTRIP, &rowperstrip);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFGetField(tfp, TIFFTAG_PLANARCONFIG, &planarconfig);
Shinya Kitaoka 120a6e
  if (planarconfig == PLANARCONFIG_SEPARATE) {
Shinya Kitaoka 120a6e
    // tmsg_error("separate buffer image in file %s not supported
Shinya Kitaoka 120a6e
    // yet",filename);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image = new IMAGE;  // new_img();
Shinya Kitaoka 120a6e
  if (!image) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_TOONZPALETTE, &palette)) {
Shinya Kitaoka 120a6e
    image->cmap.info = Tcm_old_default_info;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    /*image->cmap.info.tone_offs   = palette[3]; */
Shinya Kitaoka 120a6e
    image->cmap.info.tone_bits   = (UCHAR)palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.color_offs  = (UCHAR)palette[5];
Shinya Kitaoka 120a6e
    image->cmap.info.color_bits  = (UCHAR)palette[6];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_offs = (UCHAR)palette[7];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_bits = (UCHAR)palette[8];
Shinya Kitaoka 120a6e
    image->cmap.info.offset_mask = palette[9];
Shinya Kitaoka 120a6e
    image->cmap.info.n_tones     = 1 << palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors    = palette[10];
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils   = palette[11];
Shinya Kitaoka 120a6e
    image->cmap.info.default_val =
Shinya Kitaoka 120a6e
        (image->cmap.info.n_tones - 1) | image->cmap.info.offset_mask;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* estendo la palette */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  max_n_colors  = 1 << image->cmap.info.color_bits;
Shinya Kitaoka 120a6e
  max_n_pencils = 1 << image->cmap.info.pencil_bits;
Shinya Kitaoka 120a6e
  if (max_n_colors > image->cmap.info.n_colors ||
Shinya Kitaoka 120a6e
      max_n_pencils > image->cmap.info.n_pencils) {
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors  = max_n_colors;
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils = max_n_pencils;
Shinya Kitaoka 120a6e
    image->cmap.color_n        = image->cmap.info.n_colors;
Shinya Kitaoka 120a6e
    image->cmap.pencil_n       = image->cmap.info.n_pencils;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (tzup_f.photometric) {
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_MINISBLACK : __OR PHOTOMETRIC_MINISWHITE
Shinya Kitaoka 120a6e
                                  : __OR PHOTOMETRIC_RGB : image->type = RGB;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_PALETTE : if (tzup_f.bits_per_sample == 32) image->type =
Shinya Kitaoka 120a6e
        CMAPPED24;
Shinya Kitaoka 120a6e
    else image->type = CMAPPED;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    // tmsg_error("bad photometric interpretation in file %s", filename);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x1_reg = x1 - tzup_f.x0;
Shinya Kitaoka 120a6e
  y1_reg = y1 - tzup_f.y0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x2_reg = x2 - tzup_f.x0;
Shinya Kitaoka 120a6e
  y2_reg = y2 - tzup_f.y0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xsize_out = (x2 - x1) / scale + 1;
Shinya Kitaoka 120a6e
  ysize_out = (y2 - y1) / scale + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  getInfoRegion(®ion, x1_reg, y1_reg, x2_reg, y2_reg, scale, tzup_f.xSBsize,
Shinya Kitaoka 120a6e
                tzup_f.ySBsize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xD_offset = region.x_offset;
Shinya Kitaoka 120a6e
  yD_offset = region.y_offset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // if (Verbose)
Shinya Kitaoka 120a6e
  //  printInfoRegion(®ion);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image->pixmap.extra_mask = tzup_f.extra_mask;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // if (!allocate_pixmap(image, xsize_out, ysize_out))
Shinya Kitaoka 120a6e
  image->pixmap.buffer = new USHORT[xsize_out * ysize_out];
Shinya Kitaoka 120a6e
  if (image->pixmap.buffer) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TRUE /* x1 != 0 || x2 != tzup_f.xsize - 1 ||
Toshihiro Shimizu 890ddd
             y1 != 0 || y2 != tzup_f.ysize - 1 */) {
Shinya Kitaoka 120a6e
    clear_xsize = xsize_out;
Shinya Kitaoka 120a6e
    clear_ysize = ysize_out;
Shinya Kitaoka 120a6e
    if (image->type == CMAPPED)
Shinya Kitaoka 120a6e
      clear_image_region_buffer_16(image->pixmap.buffer, 0, 0, clear_xsize,
Shinya Kitaoka 120a6e
                                   clear_ysize, xsize_out,
Shinya Kitaoka 120a6e
                                   image->cmap.info.offset_mask + 15);
Shinya Kitaoka 120a6e
    else if (image->type == CMAPPED24)
Shinya Kitaoka 120a6e
      clear_image_region_buffer_24((ULONG *)image->pixmap.buffer, 0, 0,
Shinya Kitaoka 120a6e
                                   clear_xsize, clear_ysize, xsize_out, 255);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      abort();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    clear_xsize = 0;
Shinya Kitaoka 120a6e
    clear_ysize = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (Verbose) {
Shinya Kitaoka 120a6e
    printf("Clear xsize/xsize_out: %d/%d in %d:\n", clear_xsize, xsize_out,
Shinya Kitaoka 120a6e
           tzup_f.xsize);
Shinya Kitaoka 120a6e
    printf("Clear ysize/ysize_out: %d/%d in %d:\n", clear_ysize, ysize_out,
Shinya Kitaoka 120a6e
           tzup_f.ysize);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->pixmap.extra)
Shinya Kitaoka 120a6e
    clear_extra_region(image->pixmap.extra, 0, 0, clear_xsize, clear_ysize,
Shinya Kitaoka 120a6e
                       xsize_out, 0);
Shinya Kitaoka 120a6e
  box_x1 = tzup_f.x0;
Shinya Kitaoka 120a6e
  box_y1 = tzup_f.y0;
Shinya Kitaoka 120a6e
  box_x2 = tzup_f.x0 + tzup_f.xSBsize - 1;
Shinya Kitaoka 120a6e
  box_y2 = tzup_f.y0 + tzup_f.ySBsize - 1;
Shinya Kitaoka 120a6e
  if (x1 > box_x2 || x2 < box_x1 || y1 > box_y2 || y2 < box_y1) {
Shinya Kitaoka 120a6e
    image->pixmap.xsize   = xsize_out;
Shinya Kitaoka 120a6e
    image->pixmap.ysize   = ysize_out;
Shinya Kitaoka 120a6e
    image->pixmap.xSBsize = xsize_out;
Shinya Kitaoka 120a6e
    image->pixmap.ySBsize = ysize_out;
Shinya Kitaoka 120a6e
    image->pixmap.xD      = 0;
Shinya Kitaoka 120a6e
    image->pixmap.yD      = 0;
Shinya Kitaoka 120a6e
    image->pixmap.x_dpi   = tzup_f.x_dpi;
Shinya Kitaoka 120a6e
    image->pixmap.y_dpi   = tzup_f.y_dpi;
Shinya Kitaoka 120a6e
    image->pixmap.h_pos   = tzup_f.h_pos;
Shinya Kitaoka 120a6e
    image->filename       = strsave(filename);
Shinya Kitaoka 120a6e
    get_plt_name(filename, pltname);
Shinya Kitaoka 120a6e
    image->cmap.name = strsave(pltname);
Shinya Kitaoka 120a6e
    goto ok;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image->pixmap.xD      = xD_offset;
Shinya Kitaoka 120a6e
  image->pixmap.yD      = yD_offset;
Shinya Kitaoka 120a6e
  image->pixmap.xSBsize = region.scanNcol;
Shinya Kitaoka 120a6e
  image->pixmap.ySBsize = region.scanNrow;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* Buffer per la scanline */
Shinya Kitaoka 120a6e
  scanline_size = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
  TMALLOC(buf, scanline_size)
Shinya Kitaoka 120a6e
  if (!buf) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE CMAPPED : ret = read_region_tzup_16(
Shinya Kitaoka 120a6e
                       image, tfp, filename, ®ion, scale, buf, scanline_size,
Shinya Kitaoka 120a6e
                       rowperstrip, xsize_out, xD_offset, yD_offset);
Shinya Kitaoka 120a6e
    CASE CMAPPED24
Shinya Kitaoka 120a6e
        : ret = read_region_tzup_24(image, tfp, filename, ®ion, scale, buf,
Shinya Kitaoka 120a6e
                                    scanline_size, rowperstrip, xsize_out,
Shinya Kitaoka 120a6e
                                    xD_offset, yD_offset);
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    ret = FALSE;
Shinya Kitaoka 120a6e
    abort();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!ret) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image->pixmap.x_dpi = tzup_f.x_dpi;
Shinya Kitaoka 120a6e
  image->pixmap.y_dpi = tzup_f.y_dpi;
Shinya Kitaoka 120a6e
  image->pixmap.h_pos = tzup_f.h_pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image->filename = strsave(filename);
Shinya Kitaoka 120a6e
  get_plt_name(filename, pltname);
Shinya Kitaoka 120a6e
  image->cmap.name = strsave(pltname);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!get_icon(tfp, image)) make_icon(image, ICON_WIDTH, ICON_HEIGHT);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (image->pixmap.extra) {
Shinya Kitaoka 120a6e
    ret = read_region_extra(image, tfp, filename, ®ion, scale, buf,
Shinya Kitaoka 120a6e
                            xsize_out, xD_offset, yD_offset);
Shinya Kitaoka 120a6e
    if (!ret) goto bad;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ok:
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  TFREE(buf);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  if (image) free_img(image);
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  TFREE(buf);
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
IMAGE *img_read_tzup_info(unsigned short *filename) {
Shinya Kitaoka 120a6e
  TIFF *tfp;
Shinya Kitaoka 120a6e
  IMAGE *image = NIL;
Shinya Kitaoka 120a6e
  TZUP_FIELDS tzup_f;
Shinya Kitaoka 120a6e
  USHORT *palette; /*  [TOONZPALETTE_COUNT] */
Shinya Kitaoka 120a6e
  int max_n_colors, max_n_pencils;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* CHECK_IMAGEDLL_LICENSE_AND_GET_IMG_LICENSE_ATTR */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 1;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tfp = TIFFOpen((wchar_t *)filename, "r");
Shinya Kitaoka 120a6e
  if (!tfp) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!get_tzup_fields(tfp, &tzup_f)) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*if (tzup_f.edu_file && !(Img_license_attr & TA_TOONZ_EDU))
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
char str[1024];
Shinya Kitaoka 120a6e
BUILD_EDU_ERROR_STRING(str)
Shinya Kitaoka 120a6e
tmsg_warning (str);
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image = new IMAGE;  // new_img();
Shinya Kitaoka 120a6e
  if (!image) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_TOONZPALETTE, &palette)) {
Shinya Kitaoka 120a6e
    image->cmap.info = Tcm_old_default_info;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    /*image->cmap.info.tone_offs   = palette[3]; */
Shinya Kitaoka 120a6e
    image->cmap.info.tone_bits   = (UCHAR)palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.color_offs  = (UCHAR)palette[5];
Shinya Kitaoka 120a6e
    image->cmap.info.color_bits  = (UCHAR)palette[6];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_offs = (UCHAR)palette[7];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_bits = (UCHAR)palette[8];
Shinya Kitaoka 120a6e
    image->cmap.info.offset_mask = palette[9];
Shinya Kitaoka 120a6e
    image->cmap.info.n_tones     = 1 << palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors    = palette[10];
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils   = palette[11];
Shinya Kitaoka 120a6e
    image->cmap.info.default_val =
Shinya Kitaoka 120a6e
        (image->cmap.info.n_tones - 1) | image->cmap.info.offset_mask;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* estendo la palette */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  max_n_colors  = 1 << image->cmap.info.color_bits;
Shinya Kitaoka 120a6e
  max_n_pencils = 1 << image->cmap.info.pencil_bits;
Shinya Kitaoka 120a6e
  if (max_n_colors > image->cmap.info.n_colors ||
Shinya Kitaoka 120a6e
      max_n_pencils > image->cmap.info.n_pencils) {
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors  = max_n_colors;
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils = max_n_pencils;
Shinya Kitaoka 120a6e
    image->cmap.color_n        = image->cmap.info.n_colors;
Shinya Kitaoka 120a6e
    image->cmap.pencil_n       = image->cmap.info.n_pencils;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  switch (tzup_f.photometric) {
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_MINISBLACK : __OR PHOTOMETRIC_MINISWHITE
Shinya Kitaoka 120a6e
                                  : __OR PHOTOMETRIC_RGB : image->type = RGB;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    CASE PHOTOMETRIC_PALETTE : if (tzup_f.bits_per_sample == 32) image->type =
Shinya Kitaoka 120a6e
        CMAPPED24;
Shinya Kitaoka 120a6e
    else image->type = CMAPPED;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    // tmsg_error("bad photometric interpretation in file %s", filename);
Shinya Kitaoka 120a6e
    goto bad;
Toshihiro Shimizu 890ddd
  }
Shinya Kitaoka 120a6e
  image->pixmap.extra_mask = tzup_f.extra_mask;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!get_history(tfp, &image->history)) {
Shinya Kitaoka 120a6e
    image->history = NIL;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image->pixmap.xsize   = tzup_f.xsize;
Shinya Kitaoka 120a6e
  image->pixmap.ysize   = tzup_f.ysize;
Shinya Kitaoka 120a6e
  image->pixmap.xD      = tzup_f.x0;
Shinya Kitaoka 120a6e
  image->pixmap.yD      = tzup_f.y0;
Shinya Kitaoka 120a6e
  image->pixmap.xSBsize = tzup_f.xSBsize;
Shinya Kitaoka 120a6e
  image->pixmap.ySBsize = tzup_f.ySBsize;
Shinya Kitaoka 120a6e
  image->pixmap.x_dpi   = tzup_f.x_dpi;
Shinya Kitaoka 120a6e
  image->pixmap.y_dpi   = tzup_f.y_dpi;
Shinya Kitaoka 120a6e
  image->pixmap.h_pos   = tzup_f.h_pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  if (image) {
Shinya Kitaoka 120a6e
    // free_img(image);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  if (tfp) TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef CICCIO
Shinya Kitaoka 120a6e
IMAGE *img_read_tzup_icon(char *filename) {
Shinya Kitaoka 120a6e
  TIFF *tfp;
Shinya Kitaoka 120a6e
  IMAGE *image = NIL;
Shinya Kitaoka 120a6e
  TZUP_FIELDS tzup_f;
Shinya Kitaoka 120a6e
  char pltname[1024];
Shinya Kitaoka 120a6e
  USHORT *palette; /*  [TOONZPALETTE_COUNT] */
Shinya Kitaoka 120a6e
  int max_n_colors, max_n_pencils;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 1;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 1;
Shinya Kitaoka 120a6e
  tfp                                   = TIFFOpen(filename, "r");
Shinya Kitaoka 120a6e
  if (!tfp) return NIL;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!get_tzup_fields(tfp, &tzup_f)) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image = new_img();
Shinya Kitaoka 120a6e
  if (!image) goto bad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  image->type           = (tzup_f.bits_per_sample == 32) ? CMAPPED24 : CMAPPED;
Shinya Kitaoka 120a6e
  image->pixmap.xsize   = tzup_f.xsize;
Shinya Kitaoka 120a6e
  image->pixmap.ysize   = tzup_f.ysize;
Shinya Kitaoka 120a6e
  image->pixmap.xSBsize = tzup_f.xSBsize;
Shinya Kitaoka 120a6e
  image->pixmap.ySBsize = tzup_f.ySBsize;
Shinya Kitaoka 120a6e
  image->pixmap.xD      = tzup_f.x0;
Shinya Kitaoka 120a6e
  image->pixmap.yD      = tzup_f.y0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  get_plt_name(filename, pltname);
Shinya Kitaoka 120a6e
  image->cmap.name = strsave(pltname);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_TOONZPALETTE, &palette)) {
Shinya Kitaoka 120a6e
    image->cmap.info = Tcm_old_default_info;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    /*image->cmap.info.tone_offs   = palette[3]; */
Shinya Kitaoka 120a6e
    image->cmap.info.tone_bits   = palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.color_offs  = palette[5];
Shinya Kitaoka 120a6e
    image->cmap.info.color_bits  = palette[6];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_offs = palette[7];
Shinya Kitaoka 120a6e
    image->cmap.info.pencil_bits = palette[8];
Shinya Kitaoka 120a6e
    image->cmap.info.offset_mask = palette[9];
Shinya Kitaoka 120a6e
    image->cmap.info.n_tones     = 1 << palette[4];
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors    = palette[10];
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils   = palette[11];
Shinya Kitaoka 120a6e
    image->cmap.info.default_val =
Shinya Kitaoka 120a6e
        (image->cmap.info.n_tones - 1) | image->cmap.info.offset_mask;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* estendo la palette */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  max_n_colors  = 1 << image->cmap.info.color_bits;
Shinya Kitaoka 120a6e
  max_n_pencils = 1 << image->cmap.info.pencil_bits;
Shinya Kitaoka 120a6e
  if (max_n_colors > image->cmap.info.n_colors ||
Shinya Kitaoka 120a6e
      max_n_pencils > image->cmap.info.n_pencils) {
Shinya Kitaoka 120a6e
    image->cmap.info.n_colors  = max_n_colors;
Shinya Kitaoka 120a6e
    image->cmap.info.n_pencils = max_n_pencils;
Shinya Kitaoka 120a6e
    image->cmap.color_n        = image->cmap.info.n_colors;
Shinya Kitaoka 120a6e
    image->cmap.pencil_n       = image->cmap.info.n_pencils;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!get_icon(tfp, image)) {
Shinya Kitaoka 120a6e
    // tmsg_error("unable to read icon image of file %s", filename);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  if (image) free_img(image);
Shinya Kitaoka 120a6e
  Silent_tiff_print_error               = 0;
Shinya Kitaoka 120a6e
  Tiff_ignore_missing_internal_colormap = 0;
Shinya Kitaoka 120a6e
  TIFFClose(tfp);
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_tzup_fields(TIFF *tfp, TZUP_FIELDS *tzup_f) {
Shinya Kitaoka 120a6e
  if (!get_bits_per_sample(tfp, &tzup_f->bits_per_sample)) {
Shinya Kitaoka 120a6e
    // tmsg_error("can't handle %d-bit images", tzup_f->bits_per_sample);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!get_samples_per_pixel(tfp, &tzup_f->samples_per_pixel)) {
Shinya Kitaoka 120a6e
    // tmsg_error("can't handle %d-channel images", tzup_f->samples_per_pixel);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!get_image_sizes(tfp, &tzup_f->xSBsize, &tzup_f->ySBsize)) {
Shinya Kitaoka 120a6e
    // tmsg_error("bad image size %dx%d reading .tz(up) file",
Shinya Kitaoka 120a6e
    //           tzup_f->xSBsize, tzup_f->ySBsize);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!get_photometric(tfp, &tzup_f->photometric)) {
Shinya Kitaoka 120a6e
    // tmsg_error("bad photometric interpretation reading .tz(up) file");
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!get_resolutions(tfp, &tzup_f->x_dpi, &tzup_f->y_dpi)) {
Shinya Kitaoka 120a6e
    // tmsg_error("bad resolutions xres=%lf, yres=%lf reading .tz(up) file",
Shinya Kitaoka 120a6e
    //           tzup_f->x_dpi, tzup_f->y_dpi);
Shinya Kitaoka 120a6e
    goto bad;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  get_image_offsets_and_dimensions(tfp, tzup_f->xSBsize, tzup_f->ySBsize,
Shinya Kitaoka 120a6e
                                   &tzup_f->x0, &tzup_f->y0, &tzup_f->xsize,
Shinya Kitaoka 120a6e
                                   &tzup_f->ysize, &tzup_f->h_pos,
Shinya Kitaoka 120a6e
                                   &tzup_f->extra_mask, &tzup_f->edu_file);
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bad:
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_bits_per_sample(TIFF *tif, USHORT *bps) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
Shinya Kitaoka 120a6e
    *bps = 0;
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  switch (*bps) {
Shinya Kitaoka 120a6e
    CASE 1 : __OR 2 : __OR 4 : __OR 8 : __OR 16 : __OR 32 : return TRUE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_samples_per_pixel(TIFF *tif, USHORT *spp) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp)) {
Shinya Kitaoka 120a6e
    *spp = 0;
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  switch (*spp) { CASE 1 : __OR 3 : __OR 4 : return TRUE; }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_image_sizes(TIFF *tif, int *xsize, int *ysize) {
Shinya Kitaoka 120a6e
  *ysize = 0;
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, xsize)) return FALSE;
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ysize)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void get_image_offsets_and_dimensions(TIFF *tfp, int xSBsize,
Shinya Kitaoka 120a6e
                                             int ySBsize, int *x0, int *y0,
Shinya Kitaoka 120a6e
                                             int *xsize, int *ysize,
Shinya Kitaoka 120a6e
                                             double *h_pos, UCHAR *extra_mask,
Shinya Kitaoka 120a6e
                                             TBOOL *edu_file) {
Shinya Kitaoka 120a6e
  USHORT *window, orientation;
Shinya Kitaoka 120a6e
  float xposition, dpi;
Shinya Kitaoka 120a6e
  // int i;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_TOONZWINDOW, &window)) {
Shinya Kitaoka 120a6e
    *x0         = 0;
Shinya Kitaoka 120a6e
    *y0         = 0;
Shinya Kitaoka 120a6e
    *xsize      = xSBsize;
Shinya Kitaoka 120a6e
    *ysize      = ySBsize;
Shinya Kitaoka 120a6e
    *extra_mask = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    *edu_file = FALSE;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *x0                     = window[0];
Shinya Kitaoka 120a6e
    *y0                     = window[1];
Shinya Kitaoka 120a6e
    *xsize                  = window[2];
Shinya Kitaoka 120a6e
    *ysize                  = window[3];
Shinya Kitaoka 120a6e
    if (*xsize == 0) *xsize = xSBsize + *x0;
Shinya Kitaoka 120a6e
    if (*ysize == 0) *ysize = ySBsize + *y0;
Shinya Kitaoka 120a6e
    *extra_mask             = Read_with_extra ? window[4] : 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    *edu_file = window[TOONZWINDOW_COUNT - 1] & 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_XPOSITION, &xposition)) xposition = 8.0;
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tfp, TIFFTAG_ORIENTATION, &orientation))
Shinya Kitaoka 120a6e
    orientation = ORIENTATION_TOPLEFT;
Shinya Kitaoka 120a6e
  switch (orientation) {
Shinya Kitaoka 120a6e
    CASE ORIENTATION_BOTLEFT
Shinya Kitaoka 120a6e
        : __OR ORIENTATION_BOTRIGHT
Shinya Kitaoka 120a6e
          : __OR ORIENTATION_TOPLEFT
Shinya Kitaoka 120a6e
            : __OR ORIENTATION_TOPRIGHT
Shinya Kitaoka 120a6e
              : if (!TIFFGetField(tfp, TIFFTAG_XRESOLUTION, &dpi)) dpi = 0.0;
Shinya Kitaoka 120a6e
    CASE ORIENTATION_LEFTBOT
Shinya Kitaoka 120a6e
        : __OR ORIENTATION_RIGHTBOT
Shinya Kitaoka 120a6e
          : __OR ORIENTATION_LEFTTOP
Shinya Kitaoka 120a6e
            : __OR ORIENTATION_RIGHTTOP
Shinya Kitaoka 120a6e
              : if (!TIFFGetField(tfp, TIFFTAG_YRESOLUTION, &dpi)) dpi = 0.0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  *h_pos = (xposition - 8.0) * dpi;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_photometric(TIFF *tif, USHORT *pm) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, pm)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_resolutions(TIFF *tif, double *x_dpi, double *y_dpi) {
Shinya Kitaoka 120a6e
  float xdpi, ydpi;
Shinya Kitaoka 120a6e
  // USHORT resunit;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xdpi) ||
Shinya Kitaoka 120a6e
      !TIFFGetField(tif, TIFFTAG_YRESOLUTION, &ydpi)) {
Shinya Kitaoka 120a6e
    *x_dpi = *y_dpi = 0.0;
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  *x_dpi = (double)xdpi;
Shinya Kitaoka 120a6e
  *y_dpi = (double)ydpi;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_orientation(TIFF *tif, int *orientation) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_ORIENTATION, &orientation)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_rows_per_strip(TIFF *tif, long *rowperstrip) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowperstrip)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_compression(TIFF *tif, int *compression) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_tag_software(TIFF *tif, char *tag_software) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_SOFTWARE, tag_software)) return FALSE;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void get_planarconfig(TIFF *tif, USHORT *planarconfig) {
Shinya Kitaoka 120a6e
  TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_history(TIFF *tif, char **history) {
Shinya Kitaoka 120a6e
  if (!TIFFGetField(tif, TIFFTAG_TOONZHISTORY, history)) return FALSE;
Shinya Kitaoka 120a6e
  //*history = strsave(*history);
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_image(TIFF *tf, IMAGE *image) {
Shinya Kitaoka 120a6e
  USHORT planarconfig;
Shinya Kitaoka 120a6e
  // int i;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIFFGetField(tf, TIFFTAG_PLANARCONFIG, &planarconfig);
Shinya Kitaoka 120a6e
  if (planarconfig == PLANARCONFIG_SEPARATE) {
Shinya Kitaoka 120a6e
    // tmsg_error("separate buffer image not supported yet in .tz(up) files");
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE CMAPPED : return get_image_contig_16(tf, image);
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : return get_image_contig_24(tf, image);
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    abort();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_image_contig_16(TIFF *tf, IMAGE *image) {
Shinya Kitaoka 120a6e
  int x, y, x0, y0, x1, y1, lx, ly, xsize, ysize, wrap;
Shinya Kitaoka 120a6e
  USHORT *buf, *pix, default_val;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tf);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x0          = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0          = image->pixmap.yD;
Shinya Kitaoka 120a6e
  lx          = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly          = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x1          = x0 + lx - 1;
Shinya Kitaoka 120a6e
  y1          = y0 + ly - 1;
Shinya Kitaoka 120a6e
  xsize       = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ysize       = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap        = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  buf         = image->pixmap.buffer;
Shinya Kitaoka 120a6e
  default_val = image->cmap.info.default_val;
Shinya Kitaoka 120a6e
  for (y = 0; y < y0; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y <= y1; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < x0; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tf, (UCHAR *)pix, y - y0, 0) < 0) {
Shinya Kitaoka 120a6e
      static int gia_dato = FALSE;
Shinya Kitaoka 120a6e
      if (!gia_dato) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad data read on line %d", y);
Shinya Kitaoka 120a6e
        gia_dato = TRUE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      memset(pix, 0, lx * sizeof(*pix));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed) TIFFSwabArrayOfShort((USHORT *)pix, lx);
Shinya Kitaoka 120a6e
    pix += lx;
Shinya Kitaoka 120a6e
    for (x = x1 + 1; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y < ysize; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_image_contig_24(TIFF *tf, IMAGE *image) {
Shinya Kitaoka 120a6e
  int x, y, x0, y0, x1, y1, lx, ly, xsize, ysize, wrap;
Shinya Kitaoka 120a6e
  ULONG *buf, *pix, default_val;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tf);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x0          = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0          = image->pixmap.yD;
Shinya Kitaoka 120a6e
  lx          = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly          = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x1          = x0 + lx - 1;
Shinya Kitaoka 120a6e
  y1          = y0 + ly - 1;
Shinya Kitaoka 120a6e
  xsize       = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ysize       = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap        = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  buf         = (ULONG *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
  default_val = image->cmap.info.default_val;
Shinya Kitaoka 120a6e
  for (y = 0; y < y0; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y <= y1; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < x0; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tf, (UCHAR *)pix, y - y0, 0) < 0) {
Shinya Kitaoka 120a6e
      static int gia_dato = FALSE;
Shinya Kitaoka 120a6e
      if (!gia_dato) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad data read on line %d", y);
Shinya Kitaoka 120a6e
        gia_dato = TRUE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      memset(pix, 0, lx * sizeof(*pix));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed) TIFFSwabArrayOfLong((TUINT32 *)pix, lx);
Shinya Kitaoka 120a6e
    pix += lx;
Shinya Kitaoka 120a6e
    for (x = x1 + 1; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y < ysize; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = default_val;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_icon_16(TIFF *tfp, IMAGE *image) {
Shinya Kitaoka 120a6e
  int lx, ly;
Shinya Kitaoka 120a6e
  UCHAR *buffer;
Shinya Kitaoka 120a6e
  USHORT *icon_buffer;
Shinya Kitaoka 120a6e
  int scanline, row;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFReadDirectory(tfp)) return FALSE;
Shinya Kitaoka 120a6e
  if (!get_image_sizes(tfp, &lx, &ly)) return FALSE;
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  scanline = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  icon_buffer = new USHORT[lx * ly];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!icon_buffer) return FALSE;
Shinya Kitaoka 120a6e
  image->icon.buffer  = icon_buffer;
Shinya Kitaoka 120a6e
  image->icon.xsize   = lx;
Shinya Kitaoka 120a6e
  image->icon.ysize   = ly;
Shinya Kitaoka 120a6e
  image->icon.xSBsize = lx;
Shinya Kitaoka 120a6e
  image->icon.ySBsize = ly;
Shinya Kitaoka 120a6e
  image->icon.xD      = 0;
Shinya Kitaoka 120a6e
  image->icon.yD      = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  buffer = (UCHAR *)image->icon.buffer;
Shinya Kitaoka 120a6e
  for (row = 0; row < image->icon.ysize; row++) {
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, buffer, row, 0) < 0) {
Shinya Kitaoka 120a6e
      // tmsg_error("bad data read on line %d", row);
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed) TIFFSwabArrayOfShort((USHORT *)buffer, lx);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    buffer += scanline;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_icon_24(TIFF *tfp, IMAGE *image) {
Shinya Kitaoka 120a6e
  int lx, ly;
Shinya Kitaoka 120a6e
  UCHAR *buffer;
Shinya Kitaoka 120a6e
  ULONG *icon_buffer;
Shinya Kitaoka 120a6e
  int scanline, row;
Shinya Kitaoka 120a6e
  TBOOL swap_needed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFReadDirectory(tfp)) return FALSE;
Shinya Kitaoka 120a6e
  if (!get_image_sizes(tfp, &lx, &ly)) return FALSE;
Shinya Kitaoka 120a6e
  swap_needed = scanline_needs_swapping(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  scanline = TIFFScanlineSize(tfp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  icon_buffer = new ULONG[lx * ly];
Shinya Kitaoka 120a6e
  if (!icon_buffer) return FALSE;
Shinya Kitaoka 120a6e
  image->icon.buffer  = (USHORT *)icon_buffer;
Shinya Kitaoka 120a6e
  image->icon.xsize   = lx;
Shinya Kitaoka 120a6e
  image->icon.ysize   = ly;
Shinya Kitaoka 120a6e
  image->icon.xSBsize = lx;
Shinya Kitaoka 120a6e
  image->icon.ySBsize = ly;
Shinya Kitaoka 120a6e
  image->icon.xD      = 0;
Shinya Kitaoka 120a6e
  image->icon.yD      = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  buffer = (UCHAR *)image->icon.buffer;
Shinya Kitaoka 120a6e
  for (row = 0; row < image->icon.ysize; row++) {
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, buffer, row, 0) < 0) {
Shinya Kitaoka 120a6e
      // tmsg_error("bad data read on line %d", row);
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (swap_needed) TIFFSwabArrayOfLong((TUINT32 *)buffer, lx);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    buffer += scanline;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_icon(TIFF *tfp, IMAGE *image) {
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE CMAPPED : return get_icon_16(tfp, image);
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : return get_icon_24(tfp, image);
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    abort();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int get_extra(TIFF *tfp, IMAGE *image) {
Shinya Kitaoka 120a6e
  int x, y, x0, y0, x1, y1, lx, ly, xsize, ysize, wrap;
Shinya Kitaoka 120a6e
  UCHAR *buf, *pix;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TIFFReadDirectory(tfp)) return FALSE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x0    = image->pixmap.xD;
Shinya Kitaoka 120a6e
  y0    = image->pixmap.yD;
Shinya Kitaoka 120a6e
  lx    = image->pixmap.xSBsize;
Shinya Kitaoka 120a6e
  ly    = image->pixmap.ySBsize;
Shinya Kitaoka 120a6e
  x1    = x0 + lx - 1;
Shinya Kitaoka 120a6e
  y1    = y0 + ly - 1;
Shinya Kitaoka 120a6e
  xsize = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ysize = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap  = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  buf   = image->pixmap.extra;
Shinya Kitaoka 120a6e
  for (y = 0; y < y0; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y <= y1; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < x0; x++) *pix++ = 0;
Shinya Kitaoka 120a6e
    if (TIFFReadScanline(tfp, (UCHAR *)pix, y - y0, 0) < 0) {
Shinya Kitaoka 120a6e
      static int gia_dato = FALSE;
Shinya Kitaoka 120a6e
      if (!gia_dato) {
Shinya Kitaoka 120a6e
        // tmsg_error("bad data read on extra line %d", y);
Shinya Kitaoka 120a6e
        gia_dato = TRUE;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      memset(pix, 0, lx * sizeof(*pix));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    pix += lx;
Shinya Kitaoka 120a6e
    for (x = x1 + 1; x < xsize; x++) *pix++ = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (; y < ysize; y++) {
Shinya Kitaoka 120a6e
    pix = buf + y * wrap;
Shinya Kitaoka 120a6e
    for (x = 0; x < xsize; x++) *pix++ = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void get_plt_name(char *filename, char *pltname) {
Shinya Kitaoka 120a6e
  // sprintf (pltname, "%s.plt", tim_get_name_nodot((const char*)filename));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void clear_image_buffer_16(IMAGE *img, USHORT bg_val) {
Shinya Kitaoka 120a6e
  clear_image_region_buffer_16(img->pixmap.buffer, 0, 0, img->pixmap.xsize,
Shinya Kitaoka 120a6e
                               img->pixmap.ysize, img->pixmap.xsize, bg_val);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void clear_image_buffer_24(IMAGE *img, ULONG bg_val) {
Shinya Kitaoka 120a6e
  clear_image_region_buffer_24((ULONG *)img->pixmap.buffer, 0, 0,
Shinya Kitaoka 120a6e
                               img->pixmap.xsize, img->pixmap.ysize,
Shinya Kitaoka 120a6e
                               img->pixmap.xsize, bg_val);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void clear_extra(IMAGE *img, UCHAR bg_val) {
Shinya Kitaoka 120a6e
  clear_extra_region(img->pixmap.extra, 0, 0, img->pixmap.xsize,
Shinya Kitaoka 120a6e
                     img->pixmap.ysize, img->pixmap.xsize, bg_val);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void clear_image_region_buffer_16(USHORT *buffer, int x0, int y0,
Shinya Kitaoka 120a6e
                                         int xsize, int ysize, int wrap_x,
Shinya Kitaoka 120a6e
                                         USHORT bg_val) {
Shinya Kitaoka 120a6e
  USHORT *tmp = new USHORT[xsize];
Shinya Kitaoka 120a6e
  int x, y, bytes;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!tmp) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (x = 0; x < xsize; x++) tmp[x] = bg_val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bytes = xsize * sizeof(USHORT);
Shinya Kitaoka 120a6e
  buffer += x0 + y0 * wrap_x;
Shinya Kitaoka 120a6e
  for (y = 0; y < ysize; y++) {
Shinya Kitaoka 120a6e
    memcpy(buffer, tmp, bytes);
Shinya Kitaoka 120a6e
    buffer += wrap_x;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  delete[] tmp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void clear_image_region_buffer_24(ULONG *buffer, int x0, int y0,
Shinya Kitaoka 120a6e
                                         int xsize, int ysize, int wrap_x,
Shinya Kitaoka 120a6e
                                         ULONG bg_val) {
Shinya Kitaoka 120a6e
  ULONG *tmp = new ULONG[xsize];
Shinya Kitaoka 120a6e
  int x, y, bytes;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!tmp) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (x = 0; x < xsize; x++) tmp[x] = bg_val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bytes = xsize * sizeof(ULONG);
Shinya Kitaoka 120a6e
  buffer += x0 + y0 * wrap_x;
Shinya Kitaoka 120a6e
  for (y = 0; y < ysize; y++) {
Shinya Kitaoka 120a6e
    memcpy(buffer, tmp, bytes);
Shinya Kitaoka 120a6e
    buffer += wrap_x;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  delete tmp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void clear_extra_region(UCHAR *extra, int x0, int y0, int xsize,
Shinya Kitaoka 120a6e
                               int ysize, int wrap_x, UCHAR bg_val) {
Shinya Kitaoka 120a6e
  int y;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  extra += x0 + y0 * wrap_x;
Shinya Kitaoka 120a6e
  for (y = 0; y < ysize; y++) {
Shinya Kitaoka 120a6e
    memset(extra, bg_val, xsize);
Shinya Kitaoka 120a6e
    extra += wrap_x;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}