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
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#endif
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 "tmachine.h"
Toshihiro Shimizu 890ddd
#include "../infoRegionP.h"
Toshihiro Shimizu 890ddd
#include "ImageP/filetzr.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if !defined(TNZ_LITTLE_ENDIAN)
Toshihiro Shimizu 890ddd
  TNZ_LITTLE_ENDIAN undefined!!
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if TNZ_LITTLE_ENDIAN
Toshihiro Shimizu 890ddd
#define TZR_MAGIC ('T' | 'Z' << 8 | 'R' << 16 | 'A' << 24)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define TZR_MAGIC ('T' << 24 | 'Z' << 16 | 'R' << 8 | 'A')
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef NOTES
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
numbers are always big endian (most significant byte first)
Toshihiro Shimizu 890ddd
floats and doubles are IEEE, sign and exponent first (1+8+23 and 1+11+52)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
4 bytes: 'T' 'Z' 'R' 'A' (0x545a5241 big endian or 0x41525a54 little endian)
Toshihiro Shimizu 890ddd
1 byte: type of file (0 is not valid)
Toshihiro Shimizu 890ddd
1 byte: ST1 (subtype, must be 0 if not used)
Toshihiro Shimizu 890ddd
1 byte: ST2 (")
Toshihiro Shimizu 890ddd
1 byte: ST3 (")
Toshihiro Shimizu 890ddd
(types form a tree, numbers should increase chronologically)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
type 1: fast
Toshihiro Shimizu 890ddd
  ST1 1: lineart CM (1 ink, 2 colors (transparent and opaque), 4 bits ramp)
Toshihiro Shimizu 890ddd
    ST2 1: RLE compression (see image section)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    3 bytes: image lx
Toshihiro Shimizu 890ddd
    3 bytes: image ly
Toshihiro Shimizu 890ddd
    3 bytes: image offset from start of file
Toshihiro Shimizu 890ddd
    tagged fields (fill up the space before the image):
Toshihiro Shimizu 890ddd
      1 byte field tag
Toshihiro Shimizu 890ddd
      if (tag >= 128)
Toshihiro Shimizu 890ddd
        2 bytes field length(=n)
Toshihiro Shimizu 890ddd
	n bytes field value
Toshihiro Shimizu 890ddd
      else
Toshihiro Shimizu 890ddd
	1 byte field length(=n)
Toshihiro Shimizu 890ddd
	n bytes field value
Toshihiro Shimizu 890ddd
    image:
Toshihiro Shimizu 890ddd
      rle encoded 1 line at a time (to allow for subsampling)
Toshihiro Shimizu 890ddd
      rle encoding of 1 line:
Toshihiro Shimizu 890ddd
        2 or 3 bytes: length in bytes of encoded line
Toshihiro Shimizu 890ddd
	(2 if image lx <= 8*1024, 3 otherwise)
Toshihiro Shimizu 890ddd
        1 nibble at a time, most significative nibble first:
Toshihiro Shimizu 890ddd
        if (0x0 <= nibble[0] <= 0xE)
Toshihiro Shimizu 890ddd
	  nibble[0] is the tone value (0 is black)
Toshihiro Shimizu 890ddd
        if (nibble[0] == 0xF)
Toshihiro Shimizu 890ddd
          if (0x0 <= nibble[1] <= 0x7)
Toshihiro Shimizu 890ddd
            nibble[1] is the count of nibbles which follow (may be zero)
Toshihiro Shimizu 890ddd
            nibble[2...] number of white pixels in a run - 1, big endian
Toshihiro Shimizu 890ddd
          if (nibble[1] == 0x8)
Toshihiro Shimizu 890ddd
            toggle between transparent (0) and opaque (1) (starts with 0)
Toshihiro Shimizu 890ddd
          if (nibble[1] == 0xF)
Toshihiro Shimizu 890ddd
            if (nibble[2] == 0xE)
Toshihiro Shimizu 890ddd
              end of encoding
Toshihiro Shimizu 890ddd
            if (nibble[2] == 0xF)
Toshihiro Shimizu 890ddd
              nibble[3] is last pixel tone value, end of encoding
Toshihiro Shimizu 890ddd
        if line ends at odd nibble, a 0x0 nibble is appended
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
32:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
        1 nibble at a time, most significative nibble first:
Toshihiro Shimizu 890ddd
	
Toshihiro Shimizu 890ddd
        if (0x0 <= nibble[1]|nibble[0] <= 0xFE)
Toshihiro Shimizu 890ddd
	  nibble[1]|nibble[0] is the tone value (0 is black)
Toshihiro Shimizu 890ddd
        if (nibble[1]|nibble[0] == 0xFF)
Toshihiro Shimizu 890ddd
          if (0x0 <= nibble[2] <= 0x7)
Toshihiro Shimizu 890ddd
            nibble[2] is the count of nibbles which follow (may be zero)
Toshihiro Shimizu 890ddd
            nibble[3...] number of white pixels in a run - 1, big endian
Toshihiro Shimizu 890ddd
          if (nibble[2] == 0x8)
Toshihiro Shimizu 890ddd
            toggle between transparent (0) and opaque (1) (starts with 0)
Toshihiro Shimizu 890ddd
          if (nibble[2] == 0xF)
Toshihiro Shimizu 890ddd
            if (nibble[3] == 0xE)
Toshihiro Shimizu 890ddd
              end of encoding
Toshihiro Shimizu 890ddd
            if (nibble[3] == 0xF)
Toshihiro Shimizu 890ddd
              nibble[4]|nibble[5] is last pixel tone value, end of encoding
Toshihiro Shimizu 890ddd
        if line ends at odd nibble, a 0x0 nibble is appended
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef enum
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
	TAG_NONE = 0,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TAG_DPI = 1,	  /* size 16: double[2] (x_dpi, y_dpi) */
Toshihiro Shimizu 890ddd
		TAG_HPOS = 2, /* size  8: double */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TAG_HOW_MANY = 256
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  TAG_TYPE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TAGS_BUFLEN 65536
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define READ_BYTES_OR_ERROR(buf, n, file)       \
Toshihiro Shimizu 890ddd
	{                                           \
Toshihiro Shimizu 890ddd
		if (fread((buf), 1, (n), (file)) < (n)) \
Toshihiro Shimizu 890ddd
			goto error;                         \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BYTES_TO_FLOAT(b, f)       \
Toshihiro Shimizu 890ddd
	{                              \
Toshihiro Shimizu 890ddd
		UCHAR *fr = (b);           \
Toshihiro Shimizu 890ddd
		UCHAR *to = (UCHAR *)&(f); \
Toshihiro Shimizu 890ddd
		if (TNZ_LITTLE_ENDIAN) {   \
Toshihiro Shimizu 890ddd
			to[0] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[0];         \
Toshihiro Shimizu 890ddd
		} else {                   \
Toshihiro Shimizu 890ddd
			to[0] = fr[0];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[3];         \
Toshihiro Shimizu 890ddd
		}                          \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FLOAT_TO_BYTES(f, b)       \
Toshihiro Shimizu 890ddd
	{                              \
Toshihiro Shimizu 890ddd
		UCHAR *fr = (UCHAR *)&(f); \
Toshihiro Shimizu 890ddd
		UCHAR *to = (b);           \
Toshihiro Shimizu 890ddd
		if (TNZ_LITTLE_ENDIAN) {   \
Toshihiro Shimizu 890ddd
			to[0] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[0];         \
Toshihiro Shimizu 890ddd
		} else {                   \
Toshihiro Shimizu 890ddd
			to[0] = fr[0];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[3];         \
Toshihiro Shimizu 890ddd
		}                          \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BYTES_TO_DOUBLE(b, d)      \
Toshihiro Shimizu 890ddd
	{                              \
Toshihiro Shimizu 890ddd
		UCHAR *fr = (b);           \
Toshihiro Shimizu 890ddd
		UCHAR *to = (UCHAR *)&(d); \
Toshihiro Shimizu 890ddd
		if (TNZ_LITTLE_ENDIAN) {   \
Toshihiro Shimizu 890ddd
			to[0] = fr[7];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[6];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[5];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[4];         \
Toshihiro Shimizu 890ddd
			to[4] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[5] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[6] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[7] = fr[0];         \
Toshihiro Shimizu 890ddd
		} else {                   \
Toshihiro Shimizu 890ddd
			to[0] = fr[0];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[4] = fr[4];         \
Toshihiro Shimizu 890ddd
			to[5] = fr[5];         \
Toshihiro Shimizu 890ddd
			to[6] = fr[6];         \
Toshihiro Shimizu 890ddd
			to[7] = fr[7];         \
Toshihiro Shimizu 890ddd
		}                          \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define DOUBLE_TO_BYTES(d, b)      \
Toshihiro Shimizu 890ddd
	{                              \
Toshihiro Shimizu 890ddd
		UCHAR *fr = (UCHAR *)&(d); \
Toshihiro Shimizu 890ddd
		UCHAR *to = (b);           \
Toshihiro Shimizu 890ddd
		if (TNZ_LITTLE_ENDIAN) {   \
Toshihiro Shimizu 890ddd
			to[0] = fr[7];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[6];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[5];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[4];         \
Toshihiro Shimizu 890ddd
			to[4] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[5] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[6] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[7] = fr[0];         \
Toshihiro Shimizu 890ddd
		} else {                   \
Toshihiro Shimizu 890ddd
			to[0] = fr[0];         \
Toshihiro Shimizu 890ddd
			to[1] = fr[1];         \
Toshihiro Shimizu 890ddd
			to[2] = fr[2];         \
Toshihiro Shimizu 890ddd
			to[3] = fr[3];         \
Toshihiro Shimizu 890ddd
			to[4] = fr[4];         \
Toshihiro Shimizu 890ddd
			to[5] = fr[5];         \
Toshihiro Shimizu 890ddd
			to[6] = fr[6];         \
Toshihiro Shimizu 890ddd
			to[7] = fr[7];         \
Toshihiro Shimizu 890ddd
		}                          \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void *Tzr_buffer;
Toshihiro Shimizu 890ddd
static int   Tzr_buffer_bytes;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int Next_img_read_tzr_cmapped = FALSE;
Toshihiro Shimizu 890ddd
static int Read_cmapped = FALSE;
Toshihiro Shimizu 890ddd
#define SET_READ_CMAPPED                          \
Toshihiro Shimizu 890ddd
	{                                             \
Toshihiro Shimizu 890ddd
		Read_cmapped = Next_img_read_tzr_cmapped; \
Toshihiro Shimizu 890ddd
		Next_img_read_tzr_cmapped = FALSE;        \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_safe_bytes_for_1_1_1_pixels (int n_pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* un colore con cambio colore, un colore con cambio colore */
Toshihiro Shimizu 890ddd
	/* = 2 + 2 + 2 + 2 nibble = 4 bytes per 2 pixel */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 2 * n_pix + 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_safe_bytes_for_2_1_1_pixels (int n_pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* un colore con cambio colore, un colore con cambio colore */
Toshihiro Shimizu 890ddd
	/* = 2 + 4 + 2 + 4 nibble = 6 bytes per 2 pixel */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 4 * n_pix + 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  /*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define GET_INVAL      \
Toshihiro Shimizu 890ddd
	{                  \
Toshihiro Shimizu 890ddd
		inval = *in++; \
Toshihiro Shimizu 890ddd
		remain--;      \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#define PUT_OUTVAL              \
Toshihiro Shimizu 890ddd
	{                           \
Toshihiro Shimizu 890ddd
		*out++ = (UCHAR)outval; \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_encode_cm16_1_1_1 (USHORT *buf_in, int buf_in_len,
Toshihiro Shimizu 890ddd
                           UCHAR  *buf_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int count, prevremain, remain;
Toshihiro Shimizu 890ddd
	UINT inval, outval;
Toshihiro Shimizu 890ddd
	UINT lastval, lastcol__;
Toshihiro Shimizu 890ddd
	UINT colmask, maxtone;
Toshihiro Shimizu 890ddd
	UINT incol__, tone;
Toshihiro Shimizu 890ddd
	USHORT *in, save;
Toshihiro Shimizu 890ddd
	UCHAR *out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	maxtone = 0xF;
Toshihiro Shimizu 890ddd
	colmask = 0x07F0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	remain = buf_in_len;
Toshihiro Shimizu 890ddd
	lastval = maxtone;
Toshihiro Shimizu 890ddd
	lastcol__ = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	in = buf_in;
Toshihiro Shimizu 890ddd
	out = buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (buf_in_len <= 1) {
Toshihiro Shimizu 890ddd
		if (buf_in_len < 0)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		if (buf_in_len == 0) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = 0xE0;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
			if (inval & colmask) {
Toshihiro Shimizu 890ddd
				outval = 0xF8;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = 0xF0 | inval & 0xF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return out - buf_out;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	save = buf_in[buf_in_len - 1];
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ colmask;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	GET_INVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out0:
Toshihiro Shimizu 890ddd
	incol__ = inval & colmask;
Toshihiro Shimizu 890ddd
	if (incol__)
Toshihiro Shimizu 890ddd
		incol__ = 0x10;
Toshihiro Shimizu 890ddd
	if (incol__ == lastcol__)
Toshihiro Shimizu 890ddd
		goto check_tone_on_out0;
Toshihiro Shimizu 890ddd
	lastcol__ = incol__;
Toshihiro Shimizu 890ddd
	if (!remain)
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_on_out0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval = (0xF << 4) | 0x8;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out0:
Toshihiro Shimizu 890ddd
	tone = inval & maxtone;
Toshihiro Shimizu 890ddd
	if (tone == maxtone) {
Toshihiro Shimizu 890ddd
		lastval = inval;
Toshihiro Shimizu 890ddd
		prevremain = remain;
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
		while (inval == lastval);
Toshihiro Shimizu 890ddd
		count = prevremain - remain - 1;
Toshihiro Shimizu 890ddd
		if (count <= 0xF)
Toshihiro Shimizu 890ddd
			if (count == 0) {
Toshihiro Shimizu 890ddd
				outval = (0xF << 4) | 0x0;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outval = (0xF << 4) | 0x1;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				outval = count << 4;
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else if (count <= 0xFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x2;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x3;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x5;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x6;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 16;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0xF << 4) | 0x7;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 20;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		outval = tone << 4;
Toshihiro Shimizu 890ddd
		GET_INVAL
Toshihiro Shimizu 890ddd
		/*goto check_colpen_on_out1;*/
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out1:
Toshihiro Shimizu 890ddd
	incol__ = inval & colmask;
Toshihiro Shimizu 890ddd
	if (incol__)
Toshihiro Shimizu 890ddd
		incol__ = 0x10;
Toshihiro Shimizu 890ddd
	if (incol__ == lastcol__)
Toshihiro Shimizu 890ddd
		goto check_tone_on_out1;
Toshihiro Shimizu 890ddd
	lastcol__ = incol__;
Toshihiro Shimizu 890ddd
	if (!remain)
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_on_out1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval |= 0xF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0x8 << 4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out1:
Toshihiro Shimizu 890ddd
	tone = inval & maxtone;
Toshihiro Shimizu 890ddd
	if (tone == maxtone) {
Toshihiro Shimizu 890ddd
		lastval = inval;
Toshihiro Shimizu 890ddd
		prevremain = remain;
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
		while (inval == lastval);
Toshihiro Shimizu 890ddd
		count = prevremain - remain - 1;
Toshihiro Shimizu 890ddd
		outval |= 0xF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		if (count <= 0xF)
Toshihiro Shimizu 890ddd
			if (count == 0) {
Toshihiro Shimizu 890ddd
				outval = 0x0 << 4;
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outval = (0x1 << 4) | count;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else if (count <= 0xFF) {
Toshihiro Shimizu 890ddd
			outval = (0x2 << 4) | (count >> 4);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFF) {
Toshihiro Shimizu 890ddd
			outval = (0x3 << 4) | (count >> 8);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0x4 << 4) | (count >> 12);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0x5 << 4) | (count >> 16);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0x6 << 4) | (count >> 20);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = (0x7 << 4) | (count >> 24);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 16;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		outval |= tone;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		GET_INVAL
Toshihiro Shimizu 890ddd
		goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out0:
Toshihiro Shimizu 890ddd
	if ((save & colmask) != (in[-2] & colmask)) {
Toshihiro Shimizu 890ddd
		outval = 0xF8;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0xF0 | save & 0xF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out1:
Toshihiro Shimizu 890ddd
	if ((save & colmask) != (in[-2] & colmask)) {
Toshihiro Shimizu 890ddd
		outval |= 0xF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		outval = 0x80;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	outval |= 0xF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = (save & 0xF) << 4;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_encode_cm24_2_1_1 (ULONG *buf_in, int buf_in_len,
Toshihiro Shimizu 890ddd
                           UCHAR  *buf_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int count, prevremain, remain;
Toshihiro Shimizu 890ddd
	UINT inval, outval;
Toshihiro Shimizu 890ddd
	UINT lastval, lastcol__;
Toshihiro Shimizu 890ddd
	UINT colmask, maxtone;
Toshihiro Shimizu 890ddd
	UINT incol__, tone;
Toshihiro Shimizu 890ddd
	ULONG *in, save;
Toshihiro Shimizu 890ddd
	UCHAR *out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	maxtone = 0xFF;
Toshihiro Shimizu 890ddd
	colmask = 0x0FF00;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	remain = buf_in_len;
Toshihiro Shimizu 890ddd
	lastval = maxtone;
Toshihiro Shimizu 890ddd
	lastcol__ = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	in = buf_in;
Toshihiro Shimizu 890ddd
	out = buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (buf_in_len <= 1) {
Toshihiro Shimizu 890ddd
		if (buf_in_len < 0)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		if (buf_in_len == 0) {
Toshihiro Shimizu 890ddd
			outval = 0xFFFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = 0xFE;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
			if (inval & colmask) {
Toshihiro Shimizu 890ddd
				outval = 0xF8;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			if ((inval & 0xF) == 0xF)
Toshihiro Shimizu 890ddd
				inval--;
Toshihiro Shimizu 890ddd
			outval = 0xF0 | inval & 0xF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (inval & 0xF0) >> 4;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return out - buf_out;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	save = buf_in[buf_in_len - 1];
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ colmask;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	GET_INVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out0:
Toshihiro Shimizu 890ddd
	incol__ = inval & colmask;
Toshihiro Shimizu 890ddd
	if (incol__)
Toshihiro Shimizu 890ddd
		incol__ = 0x100;
Toshihiro Shimizu 890ddd
	if (incol__ == lastcol__)
Toshihiro Shimizu 890ddd
		goto check_tone_on_out0;
Toshihiro Shimizu 890ddd
	lastcol__ = incol__;
Toshihiro Shimizu 890ddd
	if (!remain)
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_and_save_on_out0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0x8 << 4;
Toshihiro Shimizu 890ddd
	goto check_tone_on_out1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out0:
Toshihiro Shimizu 890ddd
	tone = inval & maxtone;
Toshihiro Shimizu 890ddd
	if (tone == maxtone) {
Toshihiro Shimizu 890ddd
		lastval = inval;
Toshihiro Shimizu 890ddd
		prevremain = remain;
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
		while (inval == lastval);
Toshihiro Shimizu 890ddd
		count = prevremain - remain - 1;
Toshihiro Shimizu 890ddd
		if (count <= 0xF)
Toshihiro Shimizu 890ddd
			if (count == 0) {
Toshihiro Shimizu 890ddd
				outval = 0xFF;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				outval = 0x0 << 4;
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outval = 0xFF;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				outval = (0x1 << 4) | count;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else if (count <= 0xFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x2 << 4) | (count >> 4);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xF) << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x3 << 4) | (count >> 8);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count & 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x4 << 4) | (count >> 12);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xF) << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x5 << 4) | (count >> 16);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF000) >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x6 << 4) | (count >> 20);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF000) >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xF00000) >> 16;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFFF) {
Toshihiro Shimizu 890ddd
			outval = 0xFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (0x7 << 4) | (count >> 24);
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF000) >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0xFF00000) >> 20;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		/*if ((tone & 0xF)==0xF) tone--;*/
Toshihiro Shimizu 890ddd
		outval = (((tone & 0xF) << 4) | ((tone & 0xF0)) >> 4);
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		GET_INVAL
Toshihiro Shimizu 890ddd
		goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out1:
Toshihiro Shimizu 890ddd
	incol__ = inval & colmask;
Toshihiro Shimizu 890ddd
	if (incol__)
Toshihiro Shimizu 890ddd
		incol__ = 0x100;
Toshihiro Shimizu 890ddd
	if (incol__ == lastcol__)
Toshihiro Shimizu 890ddd
		goto check_tone_on_out1;
Toshihiro Shimizu 890ddd
	lastcol__ = incol__;
Toshihiro Shimizu 890ddd
	if (!remain)
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_and_save_on_out1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval |= 0xF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = (0xF << 4) | 0x8;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	goto check_tone_on_out0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out1:
Toshihiro Shimizu 890ddd
	tone = inval & maxtone;
Toshihiro Shimizu 890ddd
	if (tone == maxtone) {
Toshihiro Shimizu 890ddd
		lastval = inval;
Toshihiro Shimizu 890ddd
		prevremain = remain;
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			GET_INVAL
Toshihiro Shimizu 890ddd
		while (inval == lastval);
Toshihiro Shimizu 890ddd
		count = prevremain - remain - 1;
Toshihiro Shimizu 890ddd
		outval |= 0xF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		outval = 0xF << 4;
Toshihiro Shimizu 890ddd
		if (count <= 0xF)
Toshihiro Shimizu 890ddd
			if (count == 0) {
Toshihiro Shimizu 890ddd
				outval |= 0x0;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outval |= 0x1;
Toshihiro Shimizu 890ddd
				PUT_OUTVAL
Toshihiro Shimizu 890ddd
				outval = count << 4;
Toshihiro Shimizu 890ddd
				goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else if (count <= 0xFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x2;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x3;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XF) << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count & 0XFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x5;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XF) << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x6;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 16;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XFF00) >> 8;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count & 0XFF;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out0;
Toshihiro Shimizu 890ddd
		} else if (count <= 0xFFFFFFF) {
Toshihiro Shimizu 890ddd
			outval |= 0x7;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = count >> 20;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XFF000) >> 12;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XFF0) >> 4;
Toshihiro Shimizu 890ddd
			PUT_OUTVAL
Toshihiro Shimizu 890ddd
			outval = (count & 0XF) << 4;
Toshihiro Shimizu 890ddd
			goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		/*if ((tone & 0xF)==0xF) tone--;*/
Toshihiro Shimizu 890ddd
		outval |= tone & 0xF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		outval = (tone & 0xF0);
Toshihiro Shimizu 890ddd
		/*PUT_OUTVAL*/
Toshihiro Shimizu 890ddd
		GET_INVAL
Toshihiro Shimizu 890ddd
		goto check_colpen_on_out1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_and_save_on_out0:
Toshihiro Shimizu 890ddd
	if ((save & colmask) != (in[-2] & colmask)) {
Toshihiro Shimizu 890ddd
		outval = 0xFF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		outval = 0x8 << 4;
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_on_out1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out0:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = (save & 0xF) << 4;
Toshihiro Shimizu 890ddd
	outval |= (save & 0xF0) >> 4;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_and_save_on_out1:
Toshihiro Shimizu 890ddd
	if ((save & colmask) != (in[-2] & colmask)) {
Toshihiro Shimizu 890ddd
		outval |= 0xF;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		outval = (0xF << 4) | 0x80;
Toshihiro Shimizu 890ddd
		PUT_OUTVAL
Toshihiro Shimizu 890ddd
		goto end_toonz1_encoding_on_out0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out1:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	outval |= 0xF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = 0xFF;
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = (0xF << 4) | (save & 0xF);
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	outval = (save & 0xF0);
Toshihiro Shimizu 890ddd
	PUT_OUTVAL
Toshihiro Shimizu 890ddd
	buf_in[buf_in_len - 1] = save;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL write_tzr_1_1_1 (char *filename, FILE *file, IMAGE *image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, safe_bytes, enc, enclen_bytes, bytes, y;
Toshihiro Shimizu 890ddd
	USHORT *buffer, *line;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	wrap = lx;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(lx);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	buffer = image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	for (line = buffer, y = 0; y < ly; line += wrap, y++) {
Toshihiro Shimizu 890ddd
		enc = tzr_encode_cm16_1_1_1(line, lx, tzr_buf + enclen_bytes);
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2) {
Toshihiro Shimizu 890ddd
			tzr_buf[0] = (enc >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[1] = enc & 0xFF;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			tzr_buf[0] = (enc >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[1] = (enc >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[2] = enc & 0xFF;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		bytes = enclen_bytes + enc;
Toshihiro Shimizu 890ddd
		if (fwrite(tzr_buf, 1, bytes, file) != bytes)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL write_tzr_2_1_1 (char *filename, FILE *file, IMAGE *image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, safe_bytes, enc, enclen_bytes, bytes, y;
Toshihiro Shimizu 890ddd
	ULONG *buffer, *line;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	wrap = lx;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(lx);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	buffer = (ULONG *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	for (line = buffer, y = 0; y < ly; line += wrap, y++) {
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		enc = tzr_encode_cm24_2_1_1(line, lx, tzr_buf + enclen_bytes);
Toshihiro Shimizu 890ddd
		/*for (i=0; i
Toshihiro Shimizu 890ddd
    printf("*** %x %x %x %x\n", tzr_buf[enclen_bytes+i],
Toshihiro Shimizu 890ddd
                                tzr_buf[enclen_bytes+i+1], 
Toshihiro Shimizu 890ddd
				tzr_buf[enclen_bytes+i+2], 
Toshihiro Shimizu 890ddd
				tzr_buf[enclen_bytes+i+3]); */
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2) {
Toshihiro Shimizu 890ddd
			tzr_buf[0] = (enc >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[1] = enc & 0xFF;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			tzr_buf[0] = (enc >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[1] = (enc >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
			tzr_buf[2] = enc & 0xFF;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		bytes = enclen_bytes + enc;
Toshihiro Shimizu 890ddd
		if (fwrite(tzr_buf, 1, bytes, file) != bytes)
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL write_tzr_header (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                               UINT tzr_type)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR buf[100 + TAGS_BUFLEN];
Toshihiro Shimizu 890ddd
	ULONG *buf32;
Toshihiro Shimizu 890ddd
	int lx, ly, img_offs = 0, img_offs_offs = 0, pos = 0;
Toshihiro Shimizu 890ddd
	double x_dpi, y_dpi, h_pos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (tzr_type) {
Toshihiro Shimizu 890ddd
		CASE 0x01010100 : __OR 0x02010100 : lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
		ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
		buf32 = (ULONG *)buf;
Toshihiro Shimizu 890ddd
		*buf32 = TZR_MAGIC;
Toshihiro Shimizu 890ddd
		buf[4] = (tzr_type >> 24) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[5] = (tzr_type >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[6] = (tzr_type >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[7] = tzr_type & 0xFF;
Toshihiro Shimizu 890ddd
		buf[8] = (lx >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[9] = (lx >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[10] = lx & 0xFF;
Toshihiro Shimizu 890ddd
		buf[11] = (ly >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[12] = (ly >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
		buf[13] = ly & 0xFF;
Toshihiro Shimizu 890ddd
		img_offs_offs = 14;
Toshihiro Shimizu 890ddd
		pos = img_offs_offs + 3;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		assert(FALSE);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (image->pixmap.x_dpi && image->pixmap.y_dpi) {
Toshihiro Shimizu 890ddd
		buf[pos] = TAG_DPI;
Toshihiro Shimizu 890ddd
		buf[pos + 1] = 8 + 8;
Toshihiro Shimizu 890ddd
		x_dpi = image->pixmap.x_dpi;
Toshihiro Shimizu 890ddd
		y_dpi = image->pixmap.y_dpi;
Toshihiro Shimizu 890ddd
		DOUBLE_TO_BYTES(x_dpi, buf + pos + 2)
Toshihiro Shimizu 890ddd
		DOUBLE_TO_BYTES(y_dpi, buf + pos + 10)
Toshihiro Shimizu 890ddd
		pos += 1 + 1 + 8 + 8;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (image->pixmap.h_pos) {
Toshihiro Shimizu 890ddd
		buf[pos] = TAG_HPOS;
Toshihiro Shimizu 890ddd
		buf[pos + 1] = 8;
Toshihiro Shimizu 890ddd
		h_pos = image->pixmap.h_pos;
Toshihiro Shimizu 890ddd
		DOUBLE_TO_BYTES(h_pos, buf + pos + 2)
Toshihiro Shimizu 890ddd
		pos += 1 + 1 + 8;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	img_offs = pos;
Toshihiro Shimizu 890ddd
	buf[img_offs_offs] = (img_offs >> 16) & 0xFF;
Toshihiro Shimizu 890ddd
	buf[img_offs_offs + 1] = (img_offs >> 8) & 0xFF;
Toshihiro Shimizu 890ddd
	buf[img_offs_offs + 2] = img_offs & 0xFF;
Toshihiro Shimizu 890ddd
	if (fwrite(buf, 1, img_offs, file) != img_offs)
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TBOOL img_write_tzr (char *filename, IMAGE *image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FILE *file;
Toshihiro Shimizu 890ddd
	UCHAR buf[256];
Toshihiro Shimizu 890ddd
	UINT tzr_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	file = fopen(filename, "wb");
Toshihiro Shimizu 890ddd
	if (!file)
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	switch (image->type) {
Toshihiro Shimizu 890ddd
		CASE CMAPPED : tzr_type = 0x01010100;
Toshihiro Shimizu 890ddd
		CASE CMAPPED24 : tzr_type = 0x02010100;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		tmsg_error("bad image type for .tzr files");
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!write_tzr_header(filename, file, image, tzr_type))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	switch (tzr_type) {
Toshihiro Shimizu 890ddd
		CASE 0x01010100 : if (!write_tzr_1_1_1(filename, file, image)) goto error;
Toshihiro Shimizu 890ddd
		CASE 0x02010100 : if (!write_tzr_2_1_1(filename, file, image)) goto error;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	fclose(file);
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	if (file)
Toshihiro Shimizu 890ddd
		fclose(file);
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_decode_1_1_1_cm16 (UCHAR  *buf_in, int *buf_in_len,
Toshihiro Shimizu 890ddd
                           USHORT *buf_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *in;
Toshihiro Shimizu 890ddd
	USHORT *out;
Toshihiro Shimizu 890ddd
	int count;
Toshihiro Shimizu 890ddd
	UINT inval, in0, in1;
Toshihiro Shimizu 890ddd
	UINT outval, maxtone, outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define GET_IN0 (inval = *in++, in1 = inval & 0xF, in0 = inval >> 4)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	maxtone = 0xF;
Toshihiro Shimizu 890ddd
	outval = 0;
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	count = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	in = buf_in;
Toshihiro Shimizu 890ddd
	out = buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in0:
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
	while (count--)
Toshihiro Shimizu 890ddd
		*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in0:
Toshihiro Shimizu 890ddd
	if (GET_IN0 == 0xF) {
Toshihiro Shimizu 890ddd
		switch (in1) {
Toshihiro Shimizu 890ddd
			CASE 0x0 :
Toshihiro Shimizu 890ddd
				*out++ = outval | maxtone;
Toshihiro Shimizu 890ddd
			goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x1 : count = GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x2 : count = *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x3 : count = *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x4 : count = *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x5 : count = *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x6 : count = *in++ << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x7 : count = *in++ << 20;
Toshihiro Shimizu 890ddd
			count += *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x8 : outval ^= 0x10;
Toshihiro Shimizu 890ddd
			goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0xF : switch (GET_IN0)
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				CASE 0xE : CASE 0xF :
Toshihiro Shimizu 890ddd
							   *out++ = outval | in1;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				goto rle_decoding_error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			goto end_rle_decoding;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			goto rle_decoding_error;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*out++ = outval | in0;
Toshihiro Shimizu 890ddd
		goto start_from_in1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in1:
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
	while (count--)
Toshihiro Shimizu 890ddd
		*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in1:
Toshihiro Shimizu 890ddd
	if (in1 == 0xF) {
Toshihiro Shimizu 890ddd
		switch (GET_IN0) {
Toshihiro Shimizu 890ddd
			CASE 0x0 :
Toshihiro Shimizu 890ddd
				*out++ = outval | maxtone;
Toshihiro Shimizu 890ddd
			goto start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x1 : count = in1;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x2 : count = in1 << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x3 : count = in1 << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x4 : count = in1 << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x5 : count = in1 << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x6 : count = in1 << 20;
Toshihiro Shimizu 890ddd
			count += *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x7 : count = in1 << 24;
Toshihiro Shimizu 890ddd
			count += *in++ << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x8 : outval ^= 0x10;
Toshihiro Shimizu 890ddd
			goto start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0xF : switch (in1)
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				CASE 0xE : CASE 0xF :
Toshihiro Shimizu 890ddd
							   *out++ = outval | *in++ >> 4;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				goto rle_decoding_error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			goto end_rle_decoding;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*out++ = outval | in1;
Toshihiro Shimizu 890ddd
		goto start_from_in0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_rle_decoding:
Toshihiro Shimizu 890ddd
	if (buf_in_len)
Toshihiro Shimizu 890ddd
		*buf_in_len = in - buf_in;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
rle_decoding_error:
Toshihiro Shimizu 890ddd
	if (buf_in_len)
Toshihiro Shimizu 890ddd
		*buf_in_len = 0;
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tzr_decode_2_1_1_cm24 (UCHAR  *buf_in, int *buf_in_len,
Toshihiro Shimizu 890ddd
                           ULONG *buf_out)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *in;
Toshihiro Shimizu 890ddd
	ULONG *out;
Toshihiro Shimizu 890ddd
	int count;
Toshihiro Shimizu 890ddd
	UINT inval, in0, in1, aux;
Toshihiro Shimizu 890ddd
	UINT outval, maxtone, outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define GET_IN0 (inval = *in++, in1 = inval & 0xF, in0 = inval >> 4)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	maxtone = 0xFF;
Toshihiro Shimizu 890ddd
	outval = 0;
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	count = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	in = buf_in;
Toshihiro Shimizu 890ddd
	out = buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in0:
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
	while (count--)
Toshihiro Shimizu 890ddd
		*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
start_from_in0:
Toshihiro Shimizu 890ddd
	GET_IN0;
Toshihiro Shimizu 890ddd
	if (inval == 0xFF) {
Toshihiro Shimizu 890ddd
		GET_IN0;
Toshihiro Shimizu 890ddd
		switch (in0) {
Toshihiro Shimizu 890ddd
			CASE 0x0 :
Toshihiro Shimizu 890ddd
				*out++ = outval | maxtone;
Toshihiro Shimizu 890ddd
			goto start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x1 : count = in1;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x2 : count = in1 << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x3 : count = in1 << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x4 : count = in1 << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x5 : count = in1 << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x6 : count = in1 << 20;
Toshihiro Shimizu 890ddd
			count += *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x7 : count = in1 << 24;
Toshihiro Shimizu 890ddd
			count += *in++ << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x8 : outval ^= 0x100;
Toshihiro Shimizu 890ddd
			goto start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0xF : switch (in1)
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				CASE 0xE : CASE 0xF : GET_IN0;
Toshihiro Shimizu 890ddd
				*out++ = outval | ((in1 << 4) | in0);
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				goto rle_decoding_error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			goto end_rle_decoding;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			goto rle_decoding_error;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*out++ = outval | ((in1 << 4) | in0);
Toshihiro Shimizu 890ddd
		goto start_from_in0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in1:
Toshihiro Shimizu 890ddd
	outval_maxtone = outval | maxtone;
Toshihiro Shimizu 890ddd
	*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
	while (count--)
Toshihiro Shimizu 890ddd
		*out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in1:
Toshihiro Shimizu 890ddd
	aux = in1;
Toshihiro Shimizu 890ddd
	GET_IN0;
Toshihiro Shimizu 890ddd
	if (in0 == 0xF && aux == 0xF) {
Toshihiro Shimizu 890ddd
		switch (in1) {
Toshihiro Shimizu 890ddd
			CASE 0x0 :
Toshihiro Shimizu 890ddd
				*out++ = outval | maxtone;
Toshihiro Shimizu 890ddd
			goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x1 : GET_IN0;
Toshihiro Shimizu 890ddd
			count = in0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x2 : count = *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x3 : count = *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x4 : count = *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x5 : count = *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x6 : count = *in++ << 16;
Toshihiro Shimizu 890ddd
			count += *in++ << 8;
Toshihiro Shimizu 890ddd
			count += *in++;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x7 : count = *in++ << 20;
Toshihiro Shimizu 890ddd
			count += *in++ << 12;
Toshihiro Shimizu 890ddd
			count += *in++ << 4;
Toshihiro Shimizu 890ddd
			count += GET_IN0;
Toshihiro Shimizu 890ddd
			goto count_out_and_start_from_in1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0x8 : outval ^= 0x100;
Toshihiro Shimizu 890ddd
			goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CASE 0xF : switch (GET_IN0)
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				UINT aux;
Toshihiro Shimizu 890ddd
				CASE 0xE : CASE 0xF : aux = in1;
Toshihiro Shimizu 890ddd
				GET_IN0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				*out++ = outval | (aux | (in0 << 4));
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				goto rle_decoding_error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			goto end_rle_decoding;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		*out++ = outval | ((in0 << 4) | aux);
Toshihiro Shimizu 890ddd
		goto start_from_in1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_rle_decoding:
Toshihiro Shimizu 890ddd
	if (buf_in_len)
Toshihiro Shimizu 890ddd
		*buf_in_len = in - buf_in;
Toshihiro Shimizu 890ddd
	return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
rle_decoding_error:
Toshihiro Shimizu 890ddd
	if (buf_in_len)
Toshihiro Shimizu 890ddd
		*buf_in_len = 0;
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL read_tzr_1_1_1 (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                             int img_offs, TBOOL cmapped)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, y, x;
Toshihiro Shimizu 890ddd
	USHORT *buf_cm, *line_cm;
Toshihiro Shimizu 890ddd
	LPIXEL *buf_rgbm, *line_rgbm;
Toshihiro Shimizu 890ddd
	int safe_bytes, enclen_bytes, enclen, dec, tzr_dec;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
	UINT tone, color;
Toshihiro Shimizu 890ddd
	LPIXEL val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fseek(file, img_offs, SEEK_SET)) {
Toshihiro Shimizu 890ddd
		tmsg_error("inconsistent data in %s (seek failed)", filename);
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	wrap = lx;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(lx);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (cmapped) {
Toshihiro Shimizu 890ddd
		buf_cm = image->pixmap.buffer;
Toshihiro Shimizu 890ddd
		for (line_cm = buf_cm, y = 0; y < ly; line_cm += wrap, y++) {
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
			if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
			dec = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, line_cm);
Toshihiro Shimizu 890ddd
			assert(dec == lx);
Toshihiro Shimizu 890ddd
			assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
		for (line_rgbm = buf_rgbm, y = 0; y < ly; line_rgbm += wrap, y++) {
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
			if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
			line_cm = (USHORT *)line_rgbm;
Toshihiro Shimizu 890ddd
			dec = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, line_cm);
Toshihiro Shimizu 890ddd
			assert(dec == lx);
Toshihiro Shimizu 890ddd
			assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
			for (x = lx - 1; x >= 0; x--) {
Toshihiro Shimizu 890ddd
				tone = line_cm[x] & 0xF;
Toshihiro Shimizu 890ddd
				color = line_cm[x] & 0x10;
Toshihiro Shimizu 890ddd
				if (color) {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = tone * 8;
Toshihiro Shimizu 890ddd
					val.m = 255;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = 0;
Toshihiro Shimizu 890ddd
					val.m = ~(tone * 17);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				line_rgbm[x] = val;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void vaffa(void)
Toshihiro Shimizu 890ddd
{}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL read_tzr_2_1_1 (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                             int img_offs, TBOOL cmapped)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx, ly, wrap, y, x;
Toshihiro Shimizu 890ddd
	ULONG *buf_cm, *line_cm;
Toshihiro Shimizu 890ddd
	LPIXEL *buf_rgbm, *line_rgbm;
Toshihiro Shimizu 890ddd
	int safe_bytes, enclen_bytes, enclen, dec, tzr_dec;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
	UINT tone, color;
Toshihiro Shimizu 890ddd
	LPIXEL val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fseek(file, img_offs, SEEK_SET)) {
Toshihiro Shimizu 890ddd
		tmsg_error("inconsistent data in %s (seek failed)", filename);
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	wrap = lx;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(lx);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (cmapped) {
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		buf_cm = (ULONG *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
		for (line_cm = buf_cm, y = 0; y < ly; line_cm += wrap, y++) {
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
			if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
			dec = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, line_cm);
Toshihiro Shimizu 890ddd
			assert(dec == lx);
Toshihiro Shimizu 890ddd
			assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
		for (line_rgbm = buf_rgbm, y = 0; y < ly; line_rgbm += wrap, y++) {
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
			if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
			line_cm = (ULONG *)line_rgbm;
Toshihiro Shimizu 890ddd
			dec = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, line_cm);
Toshihiro Shimizu 890ddd
			assert(dec == lx);
Toshihiro Shimizu 890ddd
			assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
			for (x = lx - 1; x >= 0; x--) {
Toshihiro Shimizu 890ddd
				tone = line_cm[x] & 0xFF;
Toshihiro Shimizu 890ddd
				color = line_cm[x] & 0x100;
Toshihiro Shimizu 890ddd
				if (color) {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = tone >> 1; /* ???? tone * 8;*/
Toshihiro Shimizu 890ddd
					val.m = 255;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = 0;
Toshihiro Shimizu 890ddd
					val.m = ~(tone);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				line_rgbm[x] = val;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL read_region_tzr_1_1_1 (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                                    int img_offs, TBOOL cmapped,
Toshihiro Shimizu 890ddd
				    INFO_REGION *region)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int reg_lx, reg_ly, full_lx, full_ly, wrap, y, x, skip, step, k, tmp_x;
Toshihiro Shimizu 890ddd
	USHORT *buf_cm, *line_cm, *tmp_buf;
Toshihiro Shimizu 890ddd
	LPIXEL *buf_rgbm, *line_rgbm;
Toshihiro Shimizu 890ddd
	int safe_bytes, decline_bytes, enclen_bytes, enclen, dec, tzr_dec;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
	UINT tone, color;
Toshihiro Shimizu 890ddd
	LPIXEL val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fseek(file, img_offs, SEEK_SET)) {
Toshihiro Shimizu 890ddd
		tmsg_error("inconsistent data in %s (seek failed)", filename);
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	full_lx = region->lx_in;
Toshihiro Shimizu 890ddd
	wrap = region->xsize;
Toshihiro Shimizu 890ddd
	step = region->step;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (full_lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(full_lx);
Toshihiro Shimizu 890ddd
	decline_bytes = full_lx * sizeof(USHORT);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes + decline_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes + decline_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes + decline_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	tmp_buf = (USHORT *)(tzr_buf + safe_bytes);
Toshihiro Shimizu 890ddd
	for (y = 0; y < region->startScanRow; y++) {
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
		if (fseek(file, enclen, SEEK_CUR))
Toshihiro Shimizu 890ddd
			goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	buf_cm = image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	line_cm = buf_cm + region->x_offset + region->y_offset * wrap;
Toshihiro Shimizu 890ddd
	buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	line_rgbm = buf_rgbm + region->x_offset + region->y_offset * wrap;
Toshihiro Shimizu 890ddd
	for (y = 0; y < region->scanNrow; y++, line_cm += wrap, line_rgbm += wrap) {
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
		dec = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, tmp_buf);
Toshihiro Shimizu 890ddd
		assert(dec == full_lx);
Toshihiro Shimizu 890ddd
		assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
		if (cmapped)
Toshihiro Shimizu 890ddd
			for (tmp_x = region->x1, k = 0; k < region->scanNcol; k++, tmp_x += step) {
Toshihiro Shimizu 890ddd
				line_cm[k] = tmp_buf[tmp_x];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			for (tmp_x = region->x1, k = 0; k < region->scanNcol; k++, tmp_x += step) {
Toshihiro Shimizu 890ddd
				tone = tmp_buf[tmp_x] & 0xF;
Toshihiro Shimizu 890ddd
				color = tmp_buf[tmp_x] & 0x10;
Toshihiro Shimizu 890ddd
				if (color) {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = tone * 8;
Toshihiro Shimizu 890ddd
					val.m = 255;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = 0;
Toshihiro Shimizu 890ddd
					val.m = ~(tone * 17);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				line_rgbm[k] = val;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (y != region->scanNrow - 1)
Toshihiro Shimizu 890ddd
			for (skip = 0; skip < step - 1; skip++) {
Toshihiro Shimizu 890ddd
				READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
				if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
					enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
				if (fseek(file, enclen, SEEK_CUR))
Toshihiro Shimizu 890ddd
					goto error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL read_region_tzr_2_1_1 (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                                    int img_offs, TBOOL cmapped,
Toshihiro Shimizu 890ddd
				    INFO_REGION *region)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int reg_lx, reg_ly, full_lx, full_ly, wrap, y, x, skip, step, k, tmp_x;
Toshihiro Shimizu 890ddd
	ULONG *buf_cm, *line_cm, *tmp_buf;
Toshihiro Shimizu 890ddd
	LPIXEL *buf_rgbm, *line_rgbm;
Toshihiro Shimizu 890ddd
	int safe_bytes, decline_bytes, enclen_bytes, enclen, dec, tzr_dec;
Toshihiro Shimizu 890ddd
	UCHAR *tzr_buf;
Toshihiro Shimizu 890ddd
	UINT tone, color;
Toshihiro Shimizu 890ddd
	LPIXEL val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fseek(file, img_offs, SEEK_SET)) {
Toshihiro Shimizu 890ddd
		tmsg_error("inconsistent data in %s (seek failed)", filename);
Toshihiro Shimizu 890ddd
		return FALSE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	full_lx = region->lx_in;
Toshihiro Shimizu 890ddd
	wrap = region->xsize;
Toshihiro Shimizu 890ddd
	step = region->step;
Toshihiro Shimizu 890ddd
	enclen_bytes = 2 + (full_lx > 8 * 1024);
Toshihiro Shimizu 890ddd
	safe_bytes = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(full_lx);
Toshihiro Shimizu 890ddd
	decline_bytes = full_lx * sizeof(ULONG);
Toshihiro Shimizu 890ddd
	tzr_buf = Tzr_buffer;
Toshihiro Shimizu 890ddd
	if (safe_bytes + decline_bytes > Tzr_buffer_bytes) {
Toshihiro Shimizu 890ddd
		TREALLOC(tzr_buf, safe_bytes + decline_bytes)
Toshihiro Shimizu 890ddd
		Tzr_buffer = tzr_buf;
Toshihiro Shimizu 890ddd
		if (Tzr_buffer)
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = safe_bytes + decline_bytes;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			Tzr_buffer_bytes = 0;
Toshihiro Shimizu 890ddd
			return FALSE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	tmp_buf = (ULONG *)(tzr_buf + safe_bytes);
Toshihiro Shimizu 890ddd
	for (y = 0; y < region->startScanRow; y++) {
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
		if (fseek(file, enclen, SEEK_CUR))
Toshihiro Shimizu 890ddd
			goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	buf_cm = (ULONG *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	line_cm = buf_cm + region->x_offset + region->y_offset * wrap;
Toshihiro Shimizu 890ddd
	buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Toshihiro Shimizu 890ddd
	line_rgbm = buf_rgbm + region->x_offset + region->y_offset * wrap;
Toshihiro Shimizu 890ddd
	for (y = 0; y < region->scanNrow; y++, line_cm += wrap, line_rgbm += wrap) {
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
		if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
		READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Toshihiro Shimizu 890ddd
		dec = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, tmp_buf);
Toshihiro Shimizu 890ddd
		assert(dec == full_lx);
Toshihiro Shimizu 890ddd
		assert(tzr_dec == enclen);
Toshihiro Shimizu 890ddd
		if (cmapped)
Toshihiro Shimizu 890ddd
			for (tmp_x = region->x1, k = 0; k < region->scanNcol; k++, tmp_x += step) {
Toshihiro Shimizu 890ddd
				line_cm[k] = tmp_buf[tmp_x];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			for (tmp_x = region->x1, k = 0; k < region->scanNcol; k++, tmp_x += step) {
Toshihiro Shimizu 890ddd
				tone = tmp_buf[tmp_x] & 0xFF;
Toshihiro Shimizu 890ddd
				color = tmp_buf[tmp_x] & 0x100;
Toshihiro Shimizu 890ddd
				if (color) {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = tone >> 1 /* ???? tone * 8  */;
Toshihiro Shimizu 890ddd
					val.m = 255;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					val.r = val.g = val.b = 0;
Toshihiro Shimizu 890ddd
					val.m = ~(tone);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				line_rgbm[k] = val;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (y != region->scanNrow - 1)
Toshihiro Shimizu 890ddd
			for (skip = 0; skip < step - 1; skip++) {
Toshihiro Shimizu 890ddd
				READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Toshihiro Shimizu 890ddd
				if (enclen_bytes == 2)
Toshihiro Shimizu 890ddd
					enclen = tzr_buf[0] << 8 | tzr_buf[1];
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Toshihiro Shimizu 890ddd
				if (fseek(file, enclen, SEEK_CUR))
Toshihiro Shimizu 890ddd
					goto error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TBOOL read_tzr_header (char *filename, FILE *file, IMAGE *image,
Toshihiro Shimizu 890ddd
                              UINT *tzr_type, int *p_img_offs, TBOOL cmapped,
Toshihiro Shimizu 890ddd
			      TBOOL read_tags)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR buf[TAGS_BUFLEN];
Toshihiro Shimizu 890ddd
	int lx, ly, img_offs = 0;
Toshihiro Shimizu 890ddd
	int tags_len, file_pos, pos;
Toshihiro Shimizu 890ddd
	double x_dpi, y_dpi, h_pos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*tzr_type = 0;
Toshihiro Shimizu 890ddd
	READ_BYTES_OR_ERROR(buf, 8, file)
Toshihiro Shimizu 890ddd
	if (*(ULONG *)buf != TZR_MAGIC) {
Toshihiro Shimizu 890ddd
		tmsg_error("bad magic number in %s", filename);
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	switch (buf[4 + 0]) {
Toshihiro Shimizu 890ddd
		CASE 1 : switch (buf[4 + 1])
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			CASE 1 : switch (buf[4 + 2])
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				CASE 1 : *tzr_type = 0x01010100;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				tmsg_error("unsupported file type (%d %d %d) in %s",
Toshihiro Shimizu 890ddd
						   buf[4 + 0], buf[4 + 1], buf[4 + 2], filename);
Toshihiro Shimizu 890ddd
				goto error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		CASE 2 : switch (buf[4 + 1])
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			CASE 1 : switch (buf[4 + 2])
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				CASE 1 : *tzr_type = 0x02010100;
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
				tmsg_error("unsupported file type (%d %d %d) in %s",
Toshihiro Shimizu 890ddd
						   buf[4 + 0], buf[4 + 1], buf[4 + 2], filename);
Toshihiro Shimizu 890ddd
				goto error;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		DEFAULT:
Toshihiro Shimizu 890ddd
			tmsg_error("unsupported file type (%d %d) in %s",
Toshihiro Shimizu 890ddd
					   buf[4 + 0], buf[4 + 1], filename);
Toshihiro Shimizu 890ddd
			goto error;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		tmsg_error("unsupported file type (%d) in %s",
Toshihiro Shimizu 890ddd
				   buf[4 + 0], filename);
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	switch (*tzr_type) {
Toshihiro Shimizu 890ddd
		CASE 0x01010100 : __OR 0x02010100 : READ_BYTES_OR_ERROR(buf, 9, file)
Toshihiro Shimizu 890ddd
												lx = buf[0] << 16 | buf[1] << 8 | buf[2];
Toshihiro Shimizu 890ddd
		ly = buf[3] << 16 | buf[4] << 8 | buf[5];
Toshihiro Shimizu 890ddd
		img_offs = buf[6] << 16 | buf[7] << 8 | buf[8];
Toshihiro Shimizu 890ddd
		if (*tzr_type == 0x01010100) {
Toshihiro Shimizu 890ddd
			if (cmapped) {
Toshihiro Shimizu 890ddd
				image->type = CMAPPED;
Toshihiro Shimizu 890ddd
				image->cmap.info = Tcm_new_default_info;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				image->type = RGB;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			if (cmapped) {
Toshihiro Shimizu 890ddd
				image->type = CMAPPED24;
Toshihiro Shimizu 890ddd
				image->cmap.info = Tcm_24_default_info;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				image->type = RGB;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		image->pixmap.xsize = lx;
Toshihiro Shimizu 890ddd
		image->pixmap.ysize = ly;
Toshihiro Shimizu 890ddd
		image->pixmap.xSBsize = lx;
Toshihiro Shimizu 890ddd
		image->pixmap.ySBsize = ly;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		assert(0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (read_tags) {
Toshihiro Shimizu 890ddd
		file_pos = ftell(file);
Toshihiro Shimizu 890ddd
		tags_len = img_offs - file_pos;
Toshihiro Shimizu 890ddd
		if (tags_len > TAGS_BUFLEN) {
Toshihiro Shimizu 890ddd
			tmsg_error("tags section of %s too long", filename);
Toshihiro Shimizu 890ddd
			goto error;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (tags_len)
Toshihiro Shimizu 890ddd
			READ_BYTES_OR_ERROR(buf, tags_len, file)
Toshihiro Shimizu 890ddd
		pos = 0;
Toshihiro Shimizu 890ddd
		while (pos < tags_len) {
Toshihiro Shimizu 890ddd
			switch (buf[pos]) {
Toshihiro Shimizu 890ddd
				CASE TAG_DPI : if (buf[pos + 1] != 8 + 8) goto unknown_tag;
Toshihiro Shimizu 890ddd
				BYTES_TO_DOUBLE(buf + pos + 2, x_dpi)
Toshihiro Shimizu 890ddd
				BYTES_TO_DOUBLE(buf + pos + 10, y_dpi)
Toshihiro Shimizu 890ddd
				image->pixmap.x_dpi = x_dpi;
Toshihiro Shimizu 890ddd
				image->pixmap.y_dpi = y_dpi;
Toshihiro Shimizu 890ddd
				pos += 1 + 1 + 8 + 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				CASE TAG_HPOS : if (buf[pos + 1] != 8) goto unknown_tag;
Toshihiro Shimizu 890ddd
				BYTES_TO_DOUBLE(buf + pos + 2, h_pos)
Toshihiro Shimizu 890ddd
				image->pixmap.h_pos = h_pos;
Toshihiro Shimizu 890ddd
				pos += 1 + 1 + 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			DEFAULT:
Toshihiro Shimizu 890ddd
			unknown_tag:
Toshihiro Shimizu 890ddd
				if (buf[pos] >= 128)
Toshihiro Shimizu 890ddd
					pos += 1 + 2 + (buf[pos + 1] << 8 | buf[pos + 2]);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					pos += 1 + 1 + buf[pos + 1];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	*p_img_offs = img_offs;
Toshihiro Shimizu 890ddd
	return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*===========================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void next_img_read_tzr_cmapped (void)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Next_img_read_tzr_cmapped = TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IMAGE *img_read_tzr (char *filename)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FILE *file;
Toshihiro Shimizu 890ddd
	IMAGE *image;
Toshihiro Shimizu 890ddd
	UINT tzr_type;
Toshihiro Shimizu 890ddd
	int lx, ly, img_offs;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SET_READ_CMAPPED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	file = fopen(filename, "rb");
Toshihiro Shimizu 890ddd
	if (!file)
Toshihiro Shimizu 890ddd
		return NIL;
Toshihiro Shimizu 890ddd
	image = new_img();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Toshihiro Shimizu 890ddd
						 Read_cmapped, TRUE))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	if (!allocate_pixmap(image, lx, ly))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	switch (tzr_type) {
Toshihiro Shimizu 890ddd
		CASE 0x01010100 : if (!read_tzr_1_1_1(filename, file, image, img_offs, Read_cmapped)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CASE 0x02010100 : if (!read_tzr_2_1_1(filename, file, image, img_offs, Read_cmapped)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	fclose(file);
Toshihiro Shimizu 890ddd
	return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	if (image)
Toshihiro Shimizu 890ddd
		free_img(image);
Toshihiro Shimizu 890ddd
	if (file)
Toshihiro Shimizu 890ddd
		fclose(file);
Toshihiro Shimizu 890ddd
	return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IMAGE *img_read_region_tzr (char *filename, int x0, int y0, 
Toshihiro Shimizu 890ddd
                                            int x1, int y1, int step)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FILE *file;
Toshihiro Shimizu 890ddd
	IMAGE *image;
Toshihiro Shimizu 890ddd
	UINT tzr_type;
Toshihiro Shimizu 890ddd
	int full_lx, full_ly, reg_lx, reg_ly, img_offs;
Toshihiro Shimizu 890ddd
	INFO_REGION region;
Toshihiro Shimizu 890ddd
	int i, pixels;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SET_READ_CMAPPED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	file = fopen(filename, "rb");
Toshihiro Shimizu 890ddd
	if (!file)
Toshihiro Shimizu 890ddd
		return NIL;
Toshihiro Shimizu 890ddd
	image = new_img();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Toshihiro Shimizu 890ddd
						 Read_cmapped, TRUE))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	full_lx = image->pixmap.xsize;
Toshihiro Shimizu 890ddd
	full_ly = image->pixmap.ysize;
Toshihiro Shimizu 890ddd
	if (x0 == -1)
Toshihiro Shimizu 890ddd
		x0 = full_lx - 1;
Toshihiro Shimizu 890ddd
	if (y0 == -1)
Toshihiro Shimizu 890ddd
		y0 = full_ly - 1;
Toshihiro Shimizu 890ddd
	reg_lx = (x1 - x0) / step + 1;
Toshihiro Shimizu 890ddd
	reg_ly = (y1 - y0) / step + 1;
Toshihiro Shimizu 890ddd
	if (!allocate_pixmap(image, reg_lx, reg_ly))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* per adesso si fa un clear brutale */
Toshihiro Shimizu 890ddd
	pixels = reg_lx * reg_ly;
Toshihiro Shimizu 890ddd
	switch (image->type) {
Toshihiro Shimizu 890ddd
		CASE RGB : for (i = 0; i < pixels; i++)((ULONG *)(image->pixmap.buffer))[i] = 0;
Toshihiro Shimizu 890ddd
		CASE CMAPPED : for (i = 0; i < pixels; i++)((USHORT *)(image->pixmap.buffer))[i] = 0xF;
Toshihiro Shimizu 890ddd
		CASE CMAPPED24 : for (i = 0; i < pixels; i++)((ULONG *)(image->pixmap.buffer))[i] = 0xFF;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		assert(0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	getInfoRegion(®ion, x0, y0, x1, y1, step, full_lx, full_ly);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (tzr_type) {
Toshihiro Shimizu 890ddd
		CASE 0x01010100 : if (!read_region_tzr_1_1_1(filename, file, image, img_offs,
Toshihiro Shimizu 890ddd
													 Read_cmapped, ®ion)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CASE 0x02010100 : if (!read_region_tzr_2_1_1(filename, file, image, img_offs,
Toshihiro Shimizu 890ddd
													 Read_cmapped, ®ion)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	fclose(file);
Toshihiro Shimizu 890ddd
	return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	if (image)
Toshihiro Shimizu 890ddd
		free_img(image);
Toshihiro Shimizu 890ddd
	if (file)
Toshihiro Shimizu 890ddd
		fclose(file);
Toshihiro Shimizu 890ddd
	return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IMAGE *img_read_tzr_info (char *filename)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FILE *file;
Toshihiro Shimizu 890ddd
	IMAGE *image;
Toshihiro Shimizu 890ddd
	int img_offs;
Toshihiro Shimizu 890ddd
	UINT tzr_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	file = fopen(filename, "rb");
Toshihiro Shimizu 890ddd
	if (!file)
Toshihiro Shimizu 890ddd
		return NIL;
Toshihiro Shimizu 890ddd
	image = new_img();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Toshihiro Shimizu 890ddd
						 Read_cmapped, TRUE))
Toshihiro Shimizu 890ddd
		goto error;
Toshihiro Shimizu 890ddd
	fclose(file);
Toshihiro Shimizu 890ddd
	return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
	if (image)
Toshihiro Shimizu 890ddd
		free_img(image);
Toshihiro Shimizu 890ddd
	if (file)
Toshihiro Shimizu 890ddd
		fclose(file);
Toshihiro Shimizu 890ddd
	return NIL;
Toshihiro Shimizu 890ddd
}