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
  {
Shinya Kitaoka 120a6e
  TAG_NONE = 0,
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TAG_DPI      = 1, /* size 16: double[2] (x_dpi, y_dpi) */
Shinya Kitaoka 120a6e
      TAG_HPOS = 2, /* size  8: double */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      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
Shinya Kitaoka 120a6e
#define READ_BYTES_OR_ERROR(buf, n, file)                                      \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    if (fread((buf), 1, (n), (file)) < (n)) goto error;                        \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define BYTES_TO_FLOAT(b, f)                                                   \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    UCHAR *fr = (b);                                                           \
Shinya Kitaoka 120a6e
    UCHAR *to = (UCHAR *)&(f);                                                 \
Shinya Kitaoka 120a6e
    if (TNZ_LITTLE_ENDIAN) {                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[0];                                                           \
Shinya Kitaoka 120a6e
    } else {                                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[0];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[3];                                                           \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define FLOAT_TO_BYTES(f, b)                                                   \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    UCHAR *fr = (UCHAR *)&(f);                                                 \
Shinya Kitaoka 120a6e
    UCHAR *to = (b);                                                           \
Shinya Kitaoka 120a6e
    if (TNZ_LITTLE_ENDIAN) {                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[0];                                                           \
Shinya Kitaoka 120a6e
    } else {                                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[0];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[3];                                                           \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define BYTES_TO_DOUBLE(b, d)                                                  \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    UCHAR *fr = (b);                                                           \
Shinya Kitaoka 120a6e
    UCHAR *to = (UCHAR *)&(d);                                                 \
Shinya Kitaoka 120a6e
    if (TNZ_LITTLE_ENDIAN) {                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[7];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[6];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[5];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[4];                                                           \
Shinya Kitaoka 120a6e
      to[4] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[5] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[6] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[7] = fr[0];                                                           \
Shinya Kitaoka 120a6e
    } else {                                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[0];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[4] = fr[4];                                                           \
Shinya Kitaoka 120a6e
      to[5] = fr[5];                                                           \
Shinya Kitaoka 120a6e
      to[6] = fr[6];                                                           \
Shinya Kitaoka 120a6e
      to[7] = fr[7];                                                           \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define DOUBLE_TO_BYTES(d, b)                                                  \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    UCHAR *fr = (UCHAR *)&(d);                                                 \
Shinya Kitaoka 120a6e
    UCHAR *to = (b);                                                           \
Shinya Kitaoka 120a6e
    if (TNZ_LITTLE_ENDIAN) {                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[7];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[6];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[5];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[4];                                                           \
Shinya Kitaoka 120a6e
      to[4] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[5] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[6] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[7] = fr[0];                                                           \
Shinya Kitaoka 120a6e
    } else {                                                                   \
Shinya Kitaoka 120a6e
      to[0] = fr[0];                                                           \
Shinya Kitaoka 120a6e
      to[1] = fr[1];                                                           \
Shinya Kitaoka 120a6e
      to[2] = fr[2];                                                           \
Shinya Kitaoka 120a6e
      to[3] = fr[3];                                                           \
Shinya Kitaoka 120a6e
      to[4] = fr[4];                                                           \
Shinya Kitaoka 120a6e
      to[5] = fr[5];                                                           \
Shinya Kitaoka 120a6e
      to[6] = fr[6];                                                           \
Shinya Kitaoka 120a6e
      to[7] = fr[7];                                                           \
Shinya Kitaoka 120a6e
    }                                                                          \
Shinya Kitaoka 120a6e
  }
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;
Shinya Kitaoka 120a6e
#define SET_READ_CMAPPED                                                       \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    Read_cmapped              = Next_img_read_tzr_cmapped;                     \
Shinya Kitaoka 120a6e
    Next_img_read_tzr_cmapped = FALSE;                                         \
Shinya Kitaoka 120a6e
  }
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
{
Shinya Kitaoka 120a6e
  /* un colore con cambio colore, un colore con cambio colore */
Shinya Kitaoka 120a6e
  /* = 2 + 2 + 2 + 2 nibble = 4 bytes per 2 pixel */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  /* un colore con cambio colore, un colore con cambio colore */
Shinya Kitaoka 120a6e
  /* = 2 + 4 + 2 + 4 nibble = 6 bytes per 2 pixel */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return 4 * n_pix + 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  /*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define GET_INVAL                                                              \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    inval = *in++;                                                             \
Shinya Kitaoka 120a6e
    remain--;                                                                  \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
#define PUT_OUTVAL                                                             \
Shinya Kitaoka 120a6e
  { *out++ = (UCHAR)outval; }
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
{
Shinya Kitaoka 120a6e
  int count, prevremain, remain;
Shinya Kitaoka 120a6e
  UINT inval, outval;
Shinya Kitaoka 120a6e
  UINT lastval, lastcol__;
Shinya Kitaoka 120a6e
  UINT colmask, maxtone;
Shinya Kitaoka 120a6e
  UINT incol__, tone;
Shinya Kitaoka 120a6e
  USHORT *in, save;
Shinya Kitaoka 120a6e
  UCHAR *out;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  maxtone = 0xF;
Shinya Kitaoka 120a6e
  colmask = 0x07F0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  remain    = buf_in_len;
Shinya Kitaoka 120a6e
  lastval   = maxtone;
Shinya Kitaoka 120a6e
  lastcol__ = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  in  = buf_in;
Shinya Kitaoka 120a6e
  out = buf_out;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (buf_in_len <= 1) {
Shinya Kitaoka 120a6e
    if (buf_in_len < 0) return 0;
Shinya Kitaoka 120a6e
    if (buf_in_len == 0) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = 0xE0;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
      if (inval & colmask) {
Shinya Kitaoka 120a6e
        outval = 0xF8;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = 0xF0 | inval & 0xF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return out - buf_out;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  save                   = buf_in[buf_in_len - 1];
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ colmask;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  GET_INVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out0:
Shinya Kitaoka 120a6e
  incol__              = inval & colmask;
Shinya Kitaoka 120a6e
  if (incol__) incol__ = 0x10;
Shinya Kitaoka 120a6e
  if (incol__ == lastcol__) goto check_tone_on_out0;
Shinya Kitaoka 120a6e
  lastcol__ = incol__;
Shinya Kitaoka 120a6e
  if (!remain) goto end_toonz1_encoding_on_out0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outval = (0xF << 4) | 0x8;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out0:
Shinya Kitaoka 120a6e
  tone = inval & maxtone;
Shinya Kitaoka 120a6e
  if (tone == maxtone) {
Shinya Kitaoka 120a6e
    lastval    = inval;
Shinya Kitaoka 120a6e
    prevremain = remain;
Shinya Kitaoka 120a6e
    do
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
    while (inval == lastval);
Shinya Kitaoka 120a6e
    count = prevremain - remain - 1;
Shinya Kitaoka 120a6e
    if (count <= 0xF)
Shinya Kitaoka 120a6e
      if (count == 0) {
Shinya Kitaoka 120a6e
        outval = (0xF << 4) | 0x0;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        outval = (0xF << 4) | 0x1;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        outval = count << 4;
Shinya Kitaoka 120a6e
        goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (count <= 0xFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x2;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x3;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x5;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x6;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 16;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0xF << 4) | 0x7;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 20;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    outval = tone << 4;
Shinya Kitaoka 120a6e
    GET_INVAL
Shinya Kitaoka 120a6e
    /*goto check_colpen_on_out1;*/
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out1:
Shinya Kitaoka 120a6e
  incol__              = inval & colmask;
Shinya Kitaoka 120a6e
  if (incol__) incol__ = 0x10;
Shinya Kitaoka 120a6e
  if (incol__ == lastcol__) goto check_tone_on_out1;
Shinya Kitaoka 120a6e
  lastcol__ = incol__;
Shinya Kitaoka 120a6e
  if (!remain) goto end_toonz1_encoding_on_out1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outval |= 0xF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0x8 << 4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out1:
Shinya Kitaoka 120a6e
  tone = inval & maxtone;
Shinya Kitaoka 120a6e
  if (tone == maxtone) {
Shinya Kitaoka 120a6e
    lastval    = inval;
Shinya Kitaoka 120a6e
    prevremain = remain;
Shinya Kitaoka 120a6e
    do
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
    while (inval == lastval);
Shinya Kitaoka 120a6e
    count = prevremain - remain - 1;
Shinya Kitaoka 120a6e
    outval |= 0xF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    if (count <= 0xF)
Shinya Kitaoka 120a6e
      if (count == 0) {
Shinya Kitaoka 120a6e
        outval = 0x0 << 4;
Shinya Kitaoka 120a6e
        goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        outval = (0x1 << 4) | count;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (count <= 0xFF) {
Shinya Kitaoka 120a6e
      outval = (0x2 << 4) | (count >> 4);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFF) {
Shinya Kitaoka 120a6e
      outval = (0x3 << 4) | (count >> 8);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFF) {
Shinya Kitaoka 120a6e
      outval = (0x4 << 4) | (count >> 12);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0x5 << 4) | (count >> 16);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0x6 << 4) | (count >> 20);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFFF) {
Shinya Kitaoka 120a6e
      outval = (0x7 << 4) | (count >> 24);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 16;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    outval |= tone;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    GET_INVAL
Shinya Kitaoka 120a6e
    goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out0:
Shinya Kitaoka 120a6e
  if ((save & colmask) != (in[-2] & colmask)) {
Shinya Kitaoka 120a6e
    outval = 0xF8;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0xF0 | save & 0xF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
  return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out1:
Shinya Kitaoka 120a6e
  if ((save & colmask) != (in[-2] & colmask)) {
Shinya Kitaoka 120a6e
    outval |= 0xF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    outval = 0x80;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  outval |= 0xF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = (save & 0xF) << 4;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int count, prevremain, remain;
Shinya Kitaoka 120a6e
  UINT inval, outval;
Shinya Kitaoka 120a6e
  UINT lastval, lastcol__;
Shinya Kitaoka 120a6e
  UINT colmask, maxtone;
Shinya Kitaoka 120a6e
  UINT incol__, tone;
Shinya Kitaoka 120a6e
  ULONG *in, save;
Shinya Kitaoka 120a6e
  UCHAR *out;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  maxtone = 0xFF;
Shinya Kitaoka 120a6e
  colmask = 0x0FF00;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  remain    = buf_in_len;
Shinya Kitaoka 120a6e
  lastval   = maxtone;
Shinya Kitaoka 120a6e
  lastcol__ = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  in  = buf_in;
Shinya Kitaoka 120a6e
  out = buf_out;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (buf_in_len <= 1) {
Shinya Kitaoka 120a6e
    if (buf_in_len < 0) return 0;
Shinya Kitaoka 120a6e
    if (buf_in_len == 0) {
Shinya Kitaoka 120a6e
      outval = 0xFFFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = 0xFE;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
      if (inval & colmask) {
Shinya Kitaoka 120a6e
        outval = 0xF8;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      if ((inval & 0xF) == 0xF) inval--;
Shinya Kitaoka 120a6e
      outval = 0xF0 | inval & 0xF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (inval & 0xF0) >> 4;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return out - buf_out;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  save                   = buf_in[buf_in_len - 1];
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ colmask;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  GET_INVAL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out0:
Shinya Kitaoka 120a6e
  incol__              = inval & colmask;
Shinya Kitaoka 120a6e
  if (incol__) incol__ = 0x100;
Shinya Kitaoka 120a6e
  if (incol__ == lastcol__) goto check_tone_on_out0;
Shinya Kitaoka 120a6e
  lastcol__ = incol__;
Shinya Kitaoka 120a6e
  if (!remain) goto end_toonz1_encoding_and_save_on_out0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0x8 << 4;
Shinya Kitaoka 120a6e
  goto check_tone_on_out1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out0:
Shinya Kitaoka 120a6e
  tone = inval & maxtone;
Shinya Kitaoka 120a6e
  if (tone == maxtone) {
Shinya Kitaoka 120a6e
    lastval    = inval;
Shinya Kitaoka 120a6e
    prevremain = remain;
Shinya Kitaoka 120a6e
    do
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
    while (inval == lastval);
Shinya Kitaoka 120a6e
    count = prevremain - remain - 1;
Shinya Kitaoka 120a6e
    if (count <= 0xF)
Shinya Kitaoka 120a6e
      if (count == 0) {
Shinya Kitaoka 120a6e
        outval = 0xFF;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        outval = 0x0 << 4;
Shinya Kitaoka 120a6e
        goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        outval = 0xFF;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        outval = (0x1 << 4) | count;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (count <= 0xFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x2 << 4) | (count >> 4);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xF) << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x3 << 4) | (count >> 8);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count & 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x4 << 4) | (count >> 12);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xF) << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x5 << 4) | (count >> 16);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF000) >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x6 << 4) | (count >> 20);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF000) >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xF00000) >> 16;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFFF) {
Shinya Kitaoka 120a6e
      outval = 0xFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (0x7 << 4) | (count >> 24);
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF000) >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0xFF00000) >> 20;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    /*if ((tone & 0xF)==0xF) tone--;*/
Shinya Kitaoka 120a6e
    outval = (((tone & 0xF) << 4) | ((tone & 0xF0)) >> 4);
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    GET_INVAL
Shinya Kitaoka 120a6e
    goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_colpen_on_out1:
Shinya Kitaoka 120a6e
  incol__              = inval & colmask;
Shinya Kitaoka 120a6e
  if (incol__) incol__ = 0x100;
Shinya Kitaoka 120a6e
  if (incol__ == lastcol__) goto check_tone_on_out1;
Shinya Kitaoka 120a6e
  lastcol__ = incol__;
Shinya Kitaoka 120a6e
  if (!remain) goto end_toonz1_encoding_and_save_on_out1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outval |= 0xF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = (0xF << 4) | 0x8;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  goto check_tone_on_out0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
check_tone_on_out1:
Shinya Kitaoka 120a6e
  tone = inval & maxtone;
Shinya Kitaoka 120a6e
  if (tone == maxtone) {
Shinya Kitaoka 120a6e
    lastval    = inval;
Shinya Kitaoka 120a6e
    prevremain = remain;
Shinya Kitaoka 120a6e
    do
Shinya Kitaoka 120a6e
      GET_INVAL
Shinya Kitaoka 120a6e
    while (inval == lastval);
Shinya Kitaoka 120a6e
    count = prevremain - remain - 1;
Shinya Kitaoka 120a6e
    outval |= 0xF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    outval = 0xF << 4;
Shinya Kitaoka 120a6e
    if (count <= 0xF)
Shinya Kitaoka 120a6e
      if (count == 0) {
Shinya Kitaoka 120a6e
        outval |= 0x0;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        outval |= 0x1;
Shinya Kitaoka 120a6e
        PUT_OUTVAL
Shinya Kitaoka 120a6e
        outval = count << 4;
Shinya Kitaoka 120a6e
        goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else if (count <= 0xFF) {
Shinya Kitaoka 120a6e
      outval |= 0x2;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFF) {
Shinya Kitaoka 120a6e
      outval |= 0x3;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XF) << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFF) {
Shinya Kitaoka 120a6e
      outval |= 0x4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count & 0XFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFF) {
Shinya Kitaoka 120a6e
      outval |= 0x5;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XF) << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFF) {
Shinya Kitaoka 120a6e
      outval |= 0x6;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 16;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XFF00) >> 8;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count & 0XFF;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      goto check_colpen_on_out0;
Shinya Kitaoka 120a6e
    } else if (count <= 0xFFFFFFF) {
Shinya Kitaoka 120a6e
      outval |= 0x7;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = count >> 20;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XFF000) >> 12;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XFF0) >> 4;
Shinya Kitaoka 120a6e
      PUT_OUTVAL
Shinya Kitaoka 120a6e
      outval = (count & 0XF) << 4;
Shinya Kitaoka 120a6e
      goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    /*if ((tone & 0xF)==0xF) tone--;*/
Shinya Kitaoka 120a6e
    outval |= tone & 0xF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    outval = (tone & 0xF0);
Shinya Kitaoka 120a6e
    /*PUT_OUTVAL*/
Shinya Kitaoka 120a6e
    GET_INVAL
Shinya Kitaoka 120a6e
    goto check_colpen_on_out1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_and_save_on_out0:
Shinya Kitaoka 120a6e
  if ((save & colmask) != (in[-2] & colmask)) {
Shinya Kitaoka 120a6e
    outval = 0xFF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    outval = 0x8 << 4;
Shinya Kitaoka 120a6e
    goto end_toonz1_encoding_on_out1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out0:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = (save & 0xF) << 4;
Shinya Kitaoka 120a6e
  outval |= (save & 0xF0) >> 4;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
  return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_toonz1_encoding_and_save_on_out1:
Shinya Kitaoka 120a6e
  if ((save & colmask) != (in[-2] & colmask)) {
Shinya Kitaoka 120a6e
    outval |= 0xF;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    outval = (0xF << 4) | 0x80;
Shinya Kitaoka 120a6e
    PUT_OUTVAL
Shinya Kitaoka 120a6e
    goto end_toonz1_encoding_on_out0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
end_toonz1_encoding_on_out1:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  outval |= 0xF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = 0xFF;
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = (0xF << 4) | (save & 0xF);
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  outval = (save & 0xF0);
Shinya Kitaoka 120a6e
  PUT_OUTVAL
Shinya Kitaoka 120a6e
  buf_in[buf_in_len - 1] = save;
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int lx, ly, wrap, safe_bytes, enc, enclen_bytes, bytes, y;
Shinya Kitaoka 120a6e
  USHORT *buffer, *line;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx           = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ly           = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap         = lx;
Shinya Kitaoka 120a6e
  enclen_bytes = 2 + (lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes   = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(lx);
Shinya Kitaoka 120a6e
  tzr_buf      = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  buffer = image->pixmap.buffer;
Shinya Kitaoka 120a6e
  for (line = buffer, y = 0; y < ly; line += wrap, y++) {
Shinya Kitaoka 120a6e
    enc = tzr_encode_cm16_1_1_1(line, lx, tzr_buf + enclen_bytes);
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2) {
Shinya Kitaoka 120a6e
      tzr_buf[0] = (enc >> 8) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[1] = enc & 0xFF;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      tzr_buf[0] = (enc >> 16) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[1] = (enc >> 8) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[2] = enc & 0xFF;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    bytes = enclen_bytes + enc;
Shinya Kitaoka 120a6e
    if (fwrite(tzr_buf, 1, bytes, file) != bytes) return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int lx, ly, wrap, safe_bytes, enc, enclen_bytes, bytes, y;
Shinya Kitaoka 120a6e
  ULONG *buffer, *line;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx           = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ly           = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap         = lx;
Shinya Kitaoka 120a6e
  enclen_bytes = 2 + (lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes   = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(lx);
Shinya Kitaoka 120a6e
  tzr_buf      = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  buffer = (ULONG *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
  for (line = buffer, y = 0; y < ly; line += wrap, y++) {
Shinya Kitaoka 120a6e
    int i;
Shinya Kitaoka 120a6e
    enc = tzr_encode_cm24_2_1_1(line, lx, tzr_buf + enclen_bytes);
Shinya Kitaoka 120a6e
    /*for (i=0; i
Shinya Kitaoka 120a6e
printf("*** %x %x %x %x\n", tzr_buf[enclen_bytes+i],
Shinya Kitaoka 120a6e
                    tzr_buf[enclen_bytes+i+1],
Shinya Kitaoka 120a6e
                    tzr_buf[enclen_bytes+i+2],
Shinya Kitaoka 120a6e
                    tzr_buf[enclen_bytes+i+3]); */
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2) {
Shinya Kitaoka 120a6e
      tzr_buf[0] = (enc >> 8) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[1] = enc & 0xFF;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      tzr_buf[0] = (enc >> 16) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[1] = (enc >> 8) & 0xFF;
Shinya Kitaoka 120a6e
      tzr_buf[2] = enc & 0xFF;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    bytes = enclen_bytes + enc;
Shinya Kitaoka 120a6e
    if (fwrite(tzr_buf, 1, bytes, file) != bytes) return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  UCHAR buf[100 + TAGS_BUFLEN];
Shinya Kitaoka 120a6e
  ULONG *buf32;
Shinya Kitaoka 120a6e
  int lx, ly, img_offs = 0, img_offs_offs = 0, pos = 0;
Shinya Kitaoka 120a6e
  double x_dpi, y_dpi, h_pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (tzr_type) {
Shinya Kitaoka 120a6e
    CASE 0x01010100 : __OR 0x02010100 : lx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
    ly                                     = image->pixmap.ysize;
Shinya Kitaoka 120a6e
    buf32                                  = (ULONG *)buf;
Shinya Kitaoka 120a6e
    *buf32                                 = TZR_MAGIC;
Shinya Kitaoka 120a6e
    buf[4]                                 = (tzr_type >> 24) & 0xFF;
Shinya Kitaoka 120a6e
    buf[5]                                 = (tzr_type >> 16) & 0xFF;
Shinya Kitaoka 120a6e
    buf[6]                                 = (tzr_type >> 8) & 0xFF;
Shinya Kitaoka 120a6e
    buf[7]                                 = tzr_type & 0xFF;
Shinya Kitaoka 120a6e
    buf[8]                                 = (lx >> 16) & 0xFF;
Shinya Kitaoka 120a6e
    buf[9]                                 = (lx >> 8) & 0xFF;
Shinya Kitaoka 120a6e
    buf[10]                                = lx & 0xFF;
Shinya Kitaoka 120a6e
    buf[11]                                = (ly >> 16) & 0xFF;
Shinya Kitaoka 120a6e
    buf[12]                                = (ly >> 8) & 0xFF;
Shinya Kitaoka 120a6e
    buf[13]                                = ly & 0xFF;
Shinya Kitaoka 120a6e
    img_offs_offs                          = 14;
Shinya Kitaoka 120a6e
    pos                                    = img_offs_offs + 3;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    assert(FALSE);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->pixmap.x_dpi && image->pixmap.y_dpi) {
Shinya Kitaoka 120a6e
    buf[pos]     = TAG_DPI;
Shinya Kitaoka 120a6e
    buf[pos + 1] = 8 + 8;
Shinya Kitaoka 120a6e
    x_dpi        = image->pixmap.x_dpi;
Shinya Kitaoka 120a6e
    y_dpi        = image->pixmap.y_dpi;
Shinya Kitaoka 120a6e
    DOUBLE_TO_BYTES(x_dpi, buf + pos + 2)
Shinya Kitaoka 120a6e
    DOUBLE_TO_BYTES(y_dpi, buf + pos + 10)
Shinya Kitaoka 120a6e
    pos += 1 + 1 + 8 + 8;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (image->pixmap.h_pos) {
Shinya Kitaoka 120a6e
    buf[pos]     = TAG_HPOS;
Shinya Kitaoka 120a6e
    buf[pos + 1] = 8;
Shinya Kitaoka 120a6e
    h_pos        = image->pixmap.h_pos;
Shinya Kitaoka 120a6e
    DOUBLE_TO_BYTES(h_pos, buf + pos + 2)
Shinya Kitaoka 120a6e
    pos += 1 + 1 + 8;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  img_offs               = pos;
Shinya Kitaoka 120a6e
  buf[img_offs_offs]     = (img_offs >> 16) & 0xFF;
Shinya Kitaoka 120a6e
  buf[img_offs_offs + 1] = (img_offs >> 8) & 0xFF;
Shinya Kitaoka 120a6e
  buf[img_offs_offs + 2] = img_offs & 0xFF;
Shinya Kitaoka 120a6e
  if (fwrite(buf, 1, img_offs, file) != img_offs) return FALSE;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  FILE *file;
Shinya Kitaoka 120a6e
  UCHAR buf[256];
Shinya Kitaoka 120a6e
  UINT tzr_type;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  file = fopen(filename, "wb");
Shinya Kitaoka 120a6e
  if (!file) return FALSE;
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE CMAPPED : tzr_type   = 0x01010100;
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : tzr_type = 0x02010100;
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    tmsg_error("bad image type for .tzr files");
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!write_tzr_header(filename, file, image, tzr_type)) goto error;
Shinya Kitaoka 120a6e
  switch (tzr_type) {
Shinya Kitaoka 120a6e
    CASE 0x01010100 : if (!write_tzr_1_1_1(filename, file, image)) goto error;
Shinya Kitaoka 120a6e
    CASE 0x02010100 : if (!write_tzr_2_1_1(filename, file, image)) goto error;
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  fclose(file);
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  if (file) fclose(file);
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  UCHAR *in;
Shinya Kitaoka 120a6e
  USHORT *out;
Shinya Kitaoka 120a6e
  int count;
Shinya Kitaoka 120a6e
  UINT inval, in0, in1;
Shinya Kitaoka 120a6e
  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
Shinya Kitaoka 120a6e
  maxtone        = 0xF;
Shinya Kitaoka 120a6e
  outval         = 0;
Shinya Kitaoka 120a6e
  outval_maxtone = outval | maxtone;
Shinya Kitaoka 120a6e
  count          = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  in  = buf_in;
Shinya Kitaoka 120a6e
  out = buf_out;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in0:
Shinya Kitaoka 120a6e
  outval_maxtone         = outval | maxtone;
Shinya Kitaoka 120a6e
  *out++                 = outval_maxtone;
Shinya Kitaoka 120a6e
  while (count--) *out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in0:
Shinya Kitaoka 120a6e
  if (GET_IN0 == 0xF) {
Shinya Kitaoka 120a6e
    switch (in1) {
Shinya Kitaoka 120a6e
      CASE 0x0 : *out++ = outval | maxtone;
Shinya Kitaoka 120a6e
      goto start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x1 : count = GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x2 : count = *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x3 : count = *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x4 : count = *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x5 : count = *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x6 : count = *in++ << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x7 : count = *in++ << 20;
Shinya Kitaoka 120a6e
      count += *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x8 : outval ^= 0x10;
Shinya Kitaoka 120a6e
      goto start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0xF : switch (GET_IN0) {
Shinya Kitaoka 120a6e
        CASE 0xE : CASE 0xF : *out++ = outval | in1;
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        goto rle_decoding_error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      goto end_rle_decoding;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    DEFAULT:
Shinya Kitaoka 120a6e
      goto rle_decoding_error;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *out++ = outval | in0;
Shinya Kitaoka 120a6e
    goto start_from_in1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in1:
Shinya Kitaoka 120a6e
  outval_maxtone         = outval | maxtone;
Shinya Kitaoka 120a6e
  *out++                 = outval_maxtone;
Shinya Kitaoka 120a6e
  while (count--) *out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in1:
Shinya Kitaoka 120a6e
  if (in1 == 0xF) {
Shinya Kitaoka 120a6e
    switch (GET_IN0) {
Shinya Kitaoka 120a6e
      CASE 0x0 : *out++ = outval | maxtone;
Shinya Kitaoka 120a6e
      goto start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x1 : count = in1;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x2 : count = in1 << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x3 : count = in1 << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x4 : count = in1 << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x5 : count = in1 << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x6 : count = in1 << 20;
Shinya Kitaoka 120a6e
      count += *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x7 : count = in1 << 24;
Shinya Kitaoka 120a6e
      count += *in++ << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x8 : outval ^= 0x10;
Shinya Kitaoka 120a6e
      goto start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0xF : switch (in1) {
Shinya Kitaoka 120a6e
        CASE 0xE : CASE 0xF : *out++ = outval | *in++ >> 4;
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        goto rle_decoding_error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      goto end_rle_decoding;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    DEFAULT:
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *out++ = outval | in1;
Shinya Kitaoka 120a6e
    goto start_from_in0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_rle_decoding:
Shinya Kitaoka 120a6e
  if (buf_in_len) *buf_in_len = in - buf_in;
Shinya Kitaoka 120a6e
  return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
rle_decoding_error:
Shinya Kitaoka 120a6e
  if (buf_in_len) *buf_in_len = 0;
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  UCHAR *in;
Shinya Kitaoka 120a6e
  ULONG *out;
Shinya Kitaoka 120a6e
  int count;
Shinya Kitaoka 120a6e
  UINT inval, in0, in1, aux;
Shinya Kitaoka 120a6e
  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
Shinya Kitaoka 120a6e
  maxtone        = 0xFF;
Shinya Kitaoka 120a6e
  outval         = 0;
Shinya Kitaoka 120a6e
  outval_maxtone = outval | maxtone;
Shinya Kitaoka 120a6e
  count          = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  in  = buf_in;
Shinya Kitaoka 120a6e
  out = buf_out;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  goto start_from_in0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in0:
Shinya Kitaoka 120a6e
  outval_maxtone         = outval | maxtone;
Shinya Kitaoka 120a6e
  *out++                 = outval_maxtone;
Shinya Kitaoka 120a6e
  while (count--) *out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
start_from_in0:
Shinya Kitaoka 120a6e
  GET_IN0;
Shinya Kitaoka 120a6e
  if (inval == 0xFF) {
Shinya Kitaoka 120a6e
    GET_IN0;
Shinya Kitaoka 120a6e
    switch (in0) {
Shinya Kitaoka 120a6e
      CASE 0x0 : *out++ = outval | maxtone;
Shinya Kitaoka 120a6e
      goto start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x1 : count = in1;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x2 : count = in1 << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x3 : count = in1 << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x4 : count = in1 << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x5 : count = in1 << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x6 : count = in1 << 20;
Shinya Kitaoka 120a6e
      count += *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x7 : count = in1 << 24;
Shinya Kitaoka 120a6e
      count += *in++ << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x8 : outval ^= 0x100;
Shinya Kitaoka 120a6e
      goto start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0xF : switch (in1) {
Shinya Kitaoka 120a6e
        CASE 0xE : CASE 0xF : GET_IN0;
Shinya Kitaoka 120a6e
        *out++ = outval | ((in1 << 4) | in0);
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        goto rle_decoding_error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      goto end_rle_decoding;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    DEFAULT:
Shinya Kitaoka 120a6e
      goto rle_decoding_error;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *out++ = outval | ((in1 << 4) | in0);
Shinya Kitaoka 120a6e
    goto start_from_in0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
count_out_and_start_from_in1:
Shinya Kitaoka 120a6e
  outval_maxtone         = outval | maxtone;
Shinya Kitaoka 120a6e
  *out++                 = outval_maxtone;
Shinya Kitaoka 120a6e
  while (count--) *out++ = outval_maxtone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
start_from_in1:
Shinya Kitaoka 120a6e
  aux = in1;
Shinya Kitaoka 120a6e
  GET_IN0;
Shinya Kitaoka 120a6e
  if (in0 == 0xF && aux == 0xF) {
Shinya Kitaoka 120a6e
    switch (in1) {
Shinya Kitaoka 120a6e
      CASE 0x0 : *out++ = outval | maxtone;
Shinya Kitaoka 120a6e
      goto start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x1 : GET_IN0;
Shinya Kitaoka 120a6e
      count = in0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x2 : count = *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x3 : count = *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x4 : count = *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x5 : count = *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x6 : count = *in++ << 16;
Shinya Kitaoka 120a6e
      count += *in++ << 8;
Shinya Kitaoka 120a6e
      count += *in++;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x7 : count = *in++ << 20;
Shinya Kitaoka 120a6e
      count += *in++ << 12;
Shinya Kitaoka 120a6e
      count += *in++ << 4;
Shinya Kitaoka 120a6e
      count += GET_IN0;
Shinya Kitaoka 120a6e
      goto count_out_and_start_from_in1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0x8 : outval ^= 0x100;
Shinya Kitaoka 120a6e
      goto start_from_in0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      CASE 0xF : switch (GET_IN0) {
Shinya Kitaoka 120a6e
        UINT aux;
Shinya Kitaoka 120a6e
        CASE 0xE : CASE 0xF : aux = in1;
Shinya Kitaoka 120a6e
        GET_IN0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        *out++ = outval | (aux | (in0 << 4));
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        goto rle_decoding_error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      goto end_rle_decoding;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    DEFAULT:
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    *out++ = outval | ((in0 << 4) | aux);
Shinya Kitaoka 120a6e
    goto start_from_in1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end_rle_decoding:
Shinya Kitaoka 120a6e
  if (buf_in_len) *buf_in_len = in - buf_in;
Shinya Kitaoka 120a6e
  return out - buf_out;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
rle_decoding_error:
Shinya Kitaoka 120a6e
  if (buf_in_len) *buf_in_len = 0;
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int lx, ly, wrap, y, x;
Shinya Kitaoka 120a6e
  USHORT *buf_cm, *line_cm;
Shinya Kitaoka 120a6e
  LPIXEL *buf_rgbm, *line_rgbm;
Shinya Kitaoka 120a6e
  int safe_bytes, enclen_bytes, enclen, dec, tzr_dec;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
  UINT tone, color;
Shinya Kitaoka 120a6e
  LPIXEL val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fseek(file, img_offs, SEEK_SET)) {
Shinya Kitaoka 120a6e
    tmsg_error("inconsistent data in %s (seek failed)", filename);
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  lx           = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ly           = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap         = lx;
Shinya Kitaoka 120a6e
  enclen_bytes = 2 + (lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes   = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(lx);
Shinya Kitaoka 120a6e
  tzr_buf      = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (cmapped) {
Shinya Kitaoka 120a6e
    buf_cm = image->pixmap.buffer;
Shinya Kitaoka 120a6e
    for (line_cm = buf_cm, y = 0; y < ly; line_cm += wrap, y++) {
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
      if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
      dec = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, line_cm);
Shinya Kitaoka 120a6e
      assert(dec == lx);
Shinya Kitaoka 120a6e
      assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
    for (line_rgbm = buf_rgbm, y = 0; y < ly; line_rgbm += wrap, y++) {
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
      if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
      line_cm = (USHORT *)line_rgbm;
Shinya Kitaoka 120a6e
      dec     = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, line_cm);
Shinya Kitaoka 120a6e
      assert(dec == lx);
Shinya Kitaoka 120a6e
      assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
      for (x = lx - 1; x >= 0; x--) {
Shinya Kitaoka 120a6e
        tone  = line_cm[x] & 0xF;
Shinya Kitaoka 120a6e
        color = line_cm[x] & 0x10;
Shinya Kitaoka 120a6e
        if (color) {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = tone * 8;
Shinya Kitaoka 120a6e
          val.m                 = 255;
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = 0;
Shinya Kitaoka 120a6e
          val.m                 = ~(tone * 17);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        line_rgbm[x] = val;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int lx, ly, wrap, y, x;
Shinya Kitaoka 120a6e
  ULONG *buf_cm, *line_cm;
Shinya Kitaoka 120a6e
  LPIXEL *buf_rgbm, *line_rgbm;
Shinya Kitaoka 120a6e
  int safe_bytes, enclen_bytes, enclen, dec, tzr_dec;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
  UINT tone, color;
Shinya Kitaoka 120a6e
  LPIXEL val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fseek(file, img_offs, SEEK_SET)) {
Shinya Kitaoka 120a6e
    tmsg_error("inconsistent data in %s (seek failed)", filename);
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  lx           = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ly           = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  wrap         = lx;
Shinya Kitaoka 120a6e
  enclen_bytes = 2 + (lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes   = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(lx);
Shinya Kitaoka 120a6e
  tzr_buf      = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (cmapped) {
Shinya Kitaoka 120a6e
    int i;
Shinya Kitaoka 120a6e
    buf_cm = (ULONG *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
    for (line_cm = buf_cm, y = 0; y < ly; line_cm += wrap, y++) {
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
      if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
      dec = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, line_cm);
Shinya Kitaoka 120a6e
      assert(dec == lx);
Shinya Kitaoka 120a6e
      assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    buf_rgbm = (LPIXEL *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
    for (line_rgbm = buf_rgbm, y = 0; y < ly; line_rgbm += wrap, y++) {
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
      if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
      READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
      line_cm = (ULONG *)line_rgbm;
Shinya Kitaoka 120a6e
      dec     = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, line_cm);
Shinya Kitaoka 120a6e
      assert(dec == lx);
Shinya Kitaoka 120a6e
      assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
      for (x = lx - 1; x >= 0; x--) {
Shinya Kitaoka 120a6e
        tone  = line_cm[x] & 0xFF;
Shinya Kitaoka 120a6e
        color = line_cm[x] & 0x100;
Shinya Kitaoka 120a6e
        if (color) {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = tone >> 1; /* ???? tone * 8;*/
Shinya Kitaoka 120a6e
          val.m                 = 255;
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = 0;
Shinya Kitaoka 120a6e
          val.m                 = ~(tone);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        line_rgbm[x] = val;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int reg_lx, reg_ly, full_lx, full_ly, wrap, y, x, skip, step, k, tmp_x;
Shinya Kitaoka 120a6e
  USHORT *buf_cm, *line_cm, *tmp_buf;
Shinya Kitaoka 120a6e
  LPIXEL *buf_rgbm, *line_rgbm;
Shinya Kitaoka 120a6e
  int safe_bytes, decline_bytes, enclen_bytes, enclen, dec, tzr_dec;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
  UINT tone, color;
Shinya Kitaoka 120a6e
  LPIXEL val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fseek(file, img_offs, SEEK_SET)) {
Shinya Kitaoka 120a6e
    tmsg_error("inconsistent data in %s (seek failed)", filename);
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  full_lx       = region->lx_in;
Shinya Kitaoka 120a6e
  wrap          = region->xsize;
Shinya Kitaoka 120a6e
  step          = region->step;
Shinya Kitaoka 120a6e
  enclen_bytes  = 2 + (full_lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes    = enclen_bytes + tzr_safe_bytes_for_1_1_1_pixels(full_lx);
Shinya Kitaoka 120a6e
  decline_bytes = full_lx * sizeof(USHORT);
Shinya Kitaoka 120a6e
  tzr_buf       = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes + decline_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes + decline_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes + decline_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  tmp_buf = (USHORT *)(tzr_buf + safe_bytes);
Shinya Kitaoka 120a6e
  for (y = 0; y < region->startScanRow; y++) {
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
    if (fseek(file, enclen, SEEK_CUR)) goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  buf_cm    = image->pixmap.buffer;
Shinya Kitaoka 120a6e
  line_cm   = buf_cm + region->x_offset + region->y_offset * wrap;
Shinya Kitaoka 120a6e
  buf_rgbm  = (LPIXEL *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
  line_rgbm = buf_rgbm + region->x_offset + region->y_offset * wrap;
Shinya Kitaoka 120a6e
  for (y = 0; y < region->scanNrow; y++, line_cm += wrap, line_rgbm += wrap) {
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
    dec = tzr_decode_1_1_1_cm16(tzr_buf, &tzr_dec, tmp_buf);
Shinya Kitaoka 120a6e
    assert(dec == full_lx);
Shinya Kitaoka 120a6e
    assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
    if (cmapped)
Shinya Kitaoka 120a6e
      for (tmp_x = region->x1, k = 0; k < region->scanNcol;
Shinya Kitaoka 120a6e
           k++, tmp_x += step) {
Shinya Kitaoka 120a6e
        line_cm[k] = tmp_buf[tmp_x];
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      for (tmp_x = region->x1, k = 0; k < region->scanNcol;
Shinya Kitaoka 120a6e
           k++, tmp_x += step) {
Shinya Kitaoka 120a6e
        tone  = tmp_buf[tmp_x] & 0xF;
Shinya Kitaoka 120a6e
        color = tmp_buf[tmp_x] & 0x10;
Shinya Kitaoka 120a6e
        if (color) {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = tone * 8;
Shinya Kitaoka 120a6e
          val.m                 = 255;
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = 0;
Shinya Kitaoka 120a6e
          val.m                 = ~(tone * 17);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        line_rgbm[k] = val;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (y != region->scanNrow - 1)
Shinya Kitaoka 120a6e
      for (skip = 0; skip < step - 1; skip++) {
Shinya Kitaoka 120a6e
        READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
        if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
          enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
        if (fseek(file, enclen, SEEK_CUR)) goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  int reg_lx, reg_ly, full_lx, full_ly, wrap, y, x, skip, step, k, tmp_x;
Shinya Kitaoka 120a6e
  ULONG *buf_cm, *line_cm, *tmp_buf;
Shinya Kitaoka 120a6e
  LPIXEL *buf_rgbm, *line_rgbm;
Shinya Kitaoka 120a6e
  int safe_bytes, decline_bytes, enclen_bytes, enclen, dec, tzr_dec;
Shinya Kitaoka 120a6e
  UCHAR *tzr_buf;
Shinya Kitaoka 120a6e
  UINT tone, color;
Shinya Kitaoka 120a6e
  LPIXEL val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fseek(file, img_offs, SEEK_SET)) {
Shinya Kitaoka 120a6e
    tmsg_error("inconsistent data in %s (seek failed)", filename);
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  full_lx       = region->lx_in;
Shinya Kitaoka 120a6e
  wrap          = region->xsize;
Shinya Kitaoka 120a6e
  step          = region->step;
Shinya Kitaoka 120a6e
  enclen_bytes  = 2 + (full_lx > 8 * 1024);
Shinya Kitaoka 120a6e
  safe_bytes    = enclen_bytes + tzr_safe_bytes_for_2_1_1_pixels(full_lx);
Shinya Kitaoka 120a6e
  decline_bytes = full_lx * sizeof(ULONG);
Shinya Kitaoka 120a6e
  tzr_buf       = Tzr_buffer;
Shinya Kitaoka 120a6e
  if (safe_bytes + decline_bytes > Tzr_buffer_bytes) {
Shinya Kitaoka 120a6e
    TREALLOC(tzr_buf, safe_bytes + decline_bytes)
Shinya Kitaoka 120a6e
    Tzr_buffer = tzr_buf;
Shinya Kitaoka 120a6e
    if (Tzr_buffer)
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = safe_bytes + decline_bytes;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      Tzr_buffer_bytes = 0;
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  tmp_buf = (ULONG *)(tzr_buf + safe_bytes);
Shinya Kitaoka 120a6e
  for (y = 0; y < region->startScanRow; y++) {
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
    if (fseek(file, enclen, SEEK_CUR)) goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  buf_cm    = (ULONG *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
  line_cm   = buf_cm + region->x_offset + region->y_offset * wrap;
Shinya Kitaoka 120a6e
  buf_rgbm  = (LPIXEL *)image->pixmap.buffer;
Shinya Kitaoka 120a6e
  line_rgbm = buf_rgbm + region->x_offset + region->y_offset * wrap;
Shinya Kitaoka 120a6e
  for (y = 0; y < region->scanNrow; y++, line_cm += wrap, line_rgbm += wrap) {
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
    if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
    READ_BYTES_OR_ERROR(tzr_buf, enclen, file)
Shinya Kitaoka 120a6e
    dec = tzr_decode_2_1_1_cm24(tzr_buf, &tzr_dec, tmp_buf);
Shinya Kitaoka 120a6e
    assert(dec == full_lx);
Shinya Kitaoka 120a6e
    assert(tzr_dec == enclen);
Shinya Kitaoka 120a6e
    if (cmapped)
Shinya Kitaoka 120a6e
      for (tmp_x = region->x1, k = 0; k < region->scanNcol;
Shinya Kitaoka 120a6e
           k++, tmp_x += step) {
Shinya Kitaoka 120a6e
        line_cm[k] = tmp_buf[tmp_x];
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      for (tmp_x = region->x1, k = 0; k < region->scanNcol;
Shinya Kitaoka 120a6e
           k++, tmp_x += step) {
Shinya Kitaoka 120a6e
        tone  = tmp_buf[tmp_x] & 0xFF;
Shinya Kitaoka 120a6e
        color = tmp_buf[tmp_x] & 0x100;
Shinya Kitaoka 120a6e
        if (color) {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = tone >> 1 /* ???? tone * 8  */;
Shinya Kitaoka 120a6e
          val.m                 = 255;
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          val.r = val.g = val.b = 0;
Shinya Kitaoka 120a6e
          val.m                 = ~(tone);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        line_rgbm[k] = val;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (y != region->scanNrow - 1)
Shinya Kitaoka 120a6e
      for (skip = 0; skip < step - 1; skip++) {
Shinya Kitaoka 120a6e
        READ_BYTES_OR_ERROR(tzr_buf, enclen_bytes, file)
Shinya Kitaoka 120a6e
        if (enclen_bytes == 2)
Shinya Kitaoka 120a6e
          enclen = tzr_buf[0] << 8 | tzr_buf[1];
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          enclen = tzr_buf[0] << 16 | tzr_buf[1] << 8 | tzr_buf[2];
Shinya Kitaoka 120a6e
        if (fseek(file, enclen, SEEK_CUR)) goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  UCHAR buf[TAGS_BUFLEN];
Shinya Kitaoka 120a6e
  int lx, ly, img_offs = 0;
Shinya Kitaoka 120a6e
  int tags_len, file_pos, pos;
Shinya Kitaoka 120a6e
  double x_dpi, y_dpi, h_pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  *tzr_type = 0;
Shinya Kitaoka 120a6e
  READ_BYTES_OR_ERROR(buf, 8, file)
Shinya Kitaoka 120a6e
  if (*(ULONG *)buf != TZR_MAGIC) {
Shinya Kitaoka 120a6e
    tmsg_error("bad magic number in %s", filename);
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  switch (buf[4 + 0]) {
Shinya Kitaoka 120a6e
    CASE 1 : switch (buf[4 + 1]) {
Shinya Kitaoka 120a6e
      CASE 1 : switch (buf[4 + 2]) {
Shinya Kitaoka 120a6e
        CASE 1 : *tzr_type = 0x01010100;
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        tmsg_error("unsupported file type (%d %d %d) in %s", buf[4 + 0],
Shinya Kitaoka 120a6e
                   buf[4 + 1], buf[4 + 2], filename);
Shinya Kitaoka 120a6e
        goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    CASE 2 : switch (buf[4 + 1]) {
Shinya Kitaoka 120a6e
      CASE 1 : switch (buf[4 + 2]) {
Shinya Kitaoka 120a6e
        CASE 1 : *tzr_type = 0x02010100;
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
        tmsg_error("unsupported file type (%d %d %d) in %s", buf[4 + 0],
Shinya Kitaoka 120a6e
                   buf[4 + 1], buf[4 + 2], filename);
Shinya Kitaoka 120a6e
        goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    DEFAULT:
Shinya Kitaoka 120a6e
      tmsg_error("unsupported file type (%d %d) in %s", buf[4 + 0], buf[4 + 1],
Shinya Kitaoka 120a6e
                 filename);
Shinya Kitaoka 120a6e
      goto error;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    tmsg_error("unsupported file type (%d) in %s", buf[4 + 0], filename);
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  switch (*tzr_type) {
Shinya Kitaoka 120a6e
    CASE 0x01010100 : __OR 0x02010100 : READ_BYTES_OR_ERROR(buf, 9, file) lx =
Shinya Kitaoka 120a6e
                                            buf[0] << 16 | buf[1] << 8 | buf[2];
Shinya Kitaoka 120a6e
    ly       = buf[3] << 16 | buf[4] << 8 | buf[5];
Shinya Kitaoka 120a6e
    img_offs = buf[6] << 16 | buf[7] << 8 | buf[8];
Shinya Kitaoka 120a6e
    if (*tzr_type == 0x01010100) {
Shinya Kitaoka 120a6e
      if (cmapped) {
Shinya Kitaoka 120a6e
        image->type      = CMAPPED;
Shinya Kitaoka 120a6e
        image->cmap.info = Tcm_new_default_info;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        image->type = RGB;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (cmapped) {
Shinya Kitaoka 120a6e
        image->type      = CMAPPED24;
Shinya Kitaoka 120a6e
        image->cmap.info = Tcm_24_default_info;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        image->type = RGB;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    image->pixmap.xsize   = lx;
Shinya Kitaoka 120a6e
    image->pixmap.ysize   = ly;
Shinya Kitaoka 120a6e
    image->pixmap.xSBsize = lx;
Shinya Kitaoka 120a6e
    image->pixmap.ySBsize = ly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (read_tags) {
Shinya Kitaoka 120a6e
    file_pos = ftell(file);
Shinya Kitaoka 120a6e
    tags_len = img_offs - file_pos;
Shinya Kitaoka 120a6e
    if (tags_len > TAGS_BUFLEN) {
Shinya Kitaoka 120a6e
      tmsg_error("tags section of %s too long", filename);
Shinya Kitaoka 120a6e
      goto error;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (tags_len) READ_BYTES_OR_ERROR(buf, tags_len, file)
Shinya Kitaoka 120a6e
    pos = 0;
Shinya Kitaoka 120a6e
    while (pos < tags_len) {
Shinya Kitaoka 120a6e
      switch (buf[pos]) {
Shinya Kitaoka 120a6e
        CASE TAG_DPI : if (buf[pos + 1] != 8 + 8) goto unknown_tag;
Shinya Kitaoka 120a6e
        BYTES_TO_DOUBLE(buf + pos + 2, x_dpi)
Shinya Kitaoka 120a6e
        BYTES_TO_DOUBLE(buf + pos + 10, y_dpi)
Shinya Kitaoka 120a6e
        image->pixmap.x_dpi = x_dpi;
Shinya Kitaoka 120a6e
        image->pixmap.y_dpi = y_dpi;
Shinya Kitaoka 120a6e
        pos += 1 + 1 + 8 + 8;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        CASE TAG_HPOS : if (buf[pos + 1] != 8) goto unknown_tag;
Shinya Kitaoka 120a6e
        BYTES_TO_DOUBLE(buf + pos + 2, h_pos)
Shinya Kitaoka 120a6e
        image->pixmap.h_pos = h_pos;
Shinya Kitaoka 120a6e
        pos += 1 + 1 + 8;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      DEFAULT:
Shinya Kitaoka 120a6e
      unknown_tag:
Shinya Kitaoka 120a6e
        if (buf[pos] >= 128)
Shinya Kitaoka 120a6e
          pos += 1 + 2 + (buf[pos + 1] << 8 | buf[pos + 2]);
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          pos += 1 + 1 + buf[pos + 1];
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  *p_img_offs = img_offs;
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  FILE *file;
Shinya Kitaoka 120a6e
  IMAGE *image;
Shinya Kitaoka 120a6e
  UINT tzr_type;
Shinya Kitaoka 120a6e
  int lx, ly, img_offs;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  SET_READ_CMAPPED
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  file = fopen(filename, "rb");
Shinya Kitaoka 120a6e
  if (!file) return NIL;
Shinya Kitaoka 120a6e
  image = new_img();
Shinya Kitaoka 120a6e
  if (!image) goto error;
Shinya Kitaoka 120a6e
  if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Shinya Kitaoka 120a6e
                       Read_cmapped, TRUE))
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  lx = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  ly = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  if (!allocate_pixmap(image, lx, ly)) goto error;
Shinya Kitaoka 120a6e
  switch (tzr_type) {
Shinya Kitaoka 120a6e
    CASE 0x01010100 : if (!read_tzr_1_1_1(filename, file, image, img_offs,
Shinya Kitaoka 120a6e
                                          Read_cmapped)) goto error;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    CASE 0x02010100 : if (!read_tzr_2_1_1(filename, file, image, img_offs,
Shinya Kitaoka 120a6e
                                          Read_cmapped)) goto error;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  fclose(file);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  if (image) free_img(image);
Shinya Kitaoka 120a6e
  if (file) fclose(file);
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  FILE *file;
Shinya Kitaoka 120a6e
  IMAGE *image;
Shinya Kitaoka 120a6e
  UINT tzr_type;
Shinya Kitaoka 120a6e
  int full_lx, full_ly, reg_lx, reg_ly, img_offs;
Shinya Kitaoka 120a6e
  INFO_REGION region;
Shinya Kitaoka 120a6e
  int i, pixels;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  SET_READ_CMAPPED
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  file = fopen(filename, "rb");
Shinya Kitaoka 120a6e
  if (!file) return NIL;
Shinya Kitaoka 120a6e
  image = new_img();
Shinya Kitaoka 120a6e
  if (!image) goto error;
Shinya Kitaoka 120a6e
  if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Shinya Kitaoka 120a6e
                       Read_cmapped, TRUE))
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  full_lx          = image->pixmap.xsize;
Shinya Kitaoka 120a6e
  full_ly          = image->pixmap.ysize;
Shinya Kitaoka 120a6e
  if (x0 == -1) x0 = full_lx - 1;
Shinya Kitaoka 120a6e
  if (y0 == -1) y0 = full_ly - 1;
Shinya Kitaoka 120a6e
  reg_lx           = (x1 - x0) / step + 1;
Shinya Kitaoka 120a6e
  reg_ly           = (y1 - y0) / step + 1;
Shinya Kitaoka 120a6e
  if (!allocate_pixmap(image, reg_lx, reg_ly)) goto error;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* per adesso si fa un clear brutale */
Shinya Kitaoka 120a6e
  pixels = reg_lx * reg_ly;
Shinya Kitaoka 120a6e
  switch (image->type) {
Shinya Kitaoka 120a6e
    CASE RGB
Shinya Kitaoka 120a6e
        : for (i = 0; i < pixels; i++)((ULONG *)(image->pixmap.buffer))[i] = 0;
Shinya Kitaoka 120a6e
    CASE CMAPPED : for (i = 0; i < pixels;
Shinya Kitaoka 120a6e
                        i++)((USHORT *)(image->pixmap.buffer))[i] = 0xF;
Shinya Kitaoka 120a6e
    CASE CMAPPED24 : for (i = 0; i < pixels;
Shinya Kitaoka 120a6e
                          i++)((ULONG *)(image->pixmap.buffer))[i] = 0xFF;
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  getInfoRegion(®ion, x0, y0, x1, y1, step, full_lx, full_ly);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (tzr_type) {
Shinya Kitaoka 120a6e
    CASE 0x01010100
Shinya Kitaoka 120a6e
        : if (!read_region_tzr_1_1_1(filename, file, image, img_offs,
Shinya Kitaoka 120a6e
                                     Read_cmapped, ®ion)) goto error;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    CASE 0x02010100
Shinya Kitaoka 120a6e
        : if (!read_region_tzr_2_1_1(filename, file, image, img_offs,
Shinya Kitaoka 120a6e
                                     Read_cmapped, ®ion)) goto error;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DEFAULT:
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  fclose(file);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  if (image) free_img(image);
Shinya Kitaoka 120a6e
  if (file) fclose(file);
Shinya Kitaoka 120a6e
  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
{
Shinya Kitaoka 120a6e
  FILE *file;
Shinya Kitaoka 120a6e
  IMAGE *image;
Shinya Kitaoka 120a6e
  int img_offs;
Shinya Kitaoka 120a6e
  UINT tzr_type;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  file = fopen(filename, "rb");
Shinya Kitaoka 120a6e
  if (!file) return NIL;
Shinya Kitaoka 120a6e
  image = new_img();
Shinya Kitaoka 120a6e
  if (!image) goto error;
Shinya Kitaoka 120a6e
  if (!read_tzr_header(filename, file, image, &tzr_type, &img_offs,
Shinya Kitaoka 120a6e
                       Read_cmapped, TRUE))
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  fclose(file);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  if (image) free_img(image);
Shinya Kitaoka 120a6e
  if (file) fclose(file);
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}