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