shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
#include "tiffiop.h"
shun-iwasawa ac6927
#include <assert.h></assert.h>
shun-iwasawa ac6927
#include "toonztags.h"
shun-iwasawa ac6927
#include "tnztypes.h"
shun-iwasawa ac6927
shun-iwasawa ac6927
#ifndef UCHAR
shun-iwasawa ac6927
#define UCHAR unsigned char
shun-iwasawa ac6927
#endif
shun-iwasawa ac6927
shun-iwasawa ac6927
#ifndef USHORT
shun-iwasawa ac6927
#define USHORT unsigned short
shun-iwasawa ac6927
#endif
shun-iwasawa ac6927
shun-iwasawa ac6927
#ifndef UINT
shun-iwasawa ac6927
#define UINT TUINT32
shun-iwasawa ac6927
#endif
shun-iwasawa ac6927
shun-iwasawa ac6927
extern "C" {
shun-iwasawa ac6927
static int tif_toonz1_decode_cm24(UCHAR* buf_in, int* buf_in_len,
shun-iwasawa ac6927
                                  TUINT32* buf_out);
shun-iwasawa ac6927
shun-iwasawa ac6927
static int tif_toonz1_decode_cm16(UCHAR* buf_in, int* buf_in_len,
shun-iwasawa ac6927
                                  USHORT* buf_out, int tone_bits,
shun-iwasawa ac6927
                                  int color_offs, int color_bits,
shun-iwasawa ac6927
                                  int pencil_offs, int pencil_bits,
shun-iwasawa ac6927
                                  USHORT offset_mask);
shun-iwasawa ac6927
static int Toonz1Decode(TIFF* tif, tidataval_t* buffer, tsize_t bytes,
shun-iwasawa ac6927
                        tsample_t s);
shun-iwasawa ac6927
shun-iwasawa ac6927
static int TIFFInitToonz1(TIFF* tif, int) {
shun-iwasawa ac6927
  tif->tif_decoderow   = Toonz1Decode;
shun-iwasawa ac6927
  tif->tif_decodestrip = 0;
shun-iwasawa ac6927
  tif->tif_decodetile  = 0;
shun-iwasawa ac6927
  tif->tif_encoderow   = 0;  // Toonz1Encode;
shun-iwasawa ac6927
  tif->tif_encodestrip = 0;
shun-iwasawa ac6927
  tif->tif_encodetile  = 0;
shun-iwasawa ac6927
  return 1;
shun-iwasawa ac6927
}
shun-iwasawa ac6927
//-------------------- DECODE
shun-iwasawa ac6927
shun-iwasawa ac6927
static int Toonz1Decode(TIFF* tif, tidataval_t* buffer, tsize_t bytes,
shun-iwasawa ac6927
                        tsample_t s) {
shun-iwasawa ac6927
  int enc, dec;
shun-iwasawa ac6927
  short bitspersample;
shun-iwasawa ac6927
  // USHORT *palette;
shun-iwasawa ac6927
  int tone_bits, color_offs, color_bits, pencil_offs, pencil_bits;
shun-iwasawa ac6927
  USHORT offset_mask;
shun-iwasawa ac6927
shun-iwasawa ac6927
  if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample)) assert(0);
shun-iwasawa ac6927
  enc = dec = 0;
shun-iwasawa ac6927
  switch (bitspersample) {
shun-iwasawa ac6927
  case 8:
shun-iwasawa ac6927
    assert(!"Not Implemented");
shun-iwasawa ac6927
    /*
shun-iwasawa ac6927
dec = tif_toonz1_decode_extra ((UCHAR *)tif->tif_rawcp, &enc,
shun-iwasawa ac6927
                       (UCHAR *)buffer);
shun-iwasawa ac6927
*/
shun-iwasawa ac6927
    break;
shun-iwasawa ac6927
shun-iwasawa ac6927
  case 16: {
shun-iwasawa ac6927
    USHORT* palette;
shun-iwasawa ac6927
    USHORT paletteCount;
shun-iwasawa ac6927
    if (TIFFGetField(tif, TIFFTAG_TOONZPALETTE, &paletteCount, &palette)) {
shun-iwasawa ac6927
      tone_bits   = palette[4];
shun-iwasawa ac6927
      color_offs  = palette[5];
shun-iwasawa ac6927
      color_bits  = palette[6];
shun-iwasawa ac6927
      pencil_offs = palette[7];
shun-iwasawa ac6927
      pencil_bits = palette[8];
shun-iwasawa ac6927
      offset_mask = palette[9];
shun-iwasawa ac6927
    } else {
shun-iwasawa ac6927
      tone_bits   = 4;
shun-iwasawa ac6927
      color_offs  = 4;
shun-iwasawa ac6927
      color_bits  = 7;
shun-iwasawa ac6927
      pencil_offs = 11;
shun-iwasawa ac6927
      pencil_bits = 5;
shun-iwasawa ac6927
      offset_mask = 0;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    dec = tif_toonz1_decode_cm16((UCHAR*)tif->tif_rawcp, &enc, (USHORT*)buffer,
shun-iwasawa ac6927
                                 tone_bits, color_offs, color_bits, pencil_offs,
shun-iwasawa ac6927
                                 pencil_bits, offset_mask);
shun-iwasawa ac6927
  } break;
shun-iwasawa ac6927
shun-iwasawa ac6927
  case 32:
shun-iwasawa ac6927
    dec =
shun-iwasawa ac6927
        tif_toonz1_decode_cm24((UCHAR*)tif->tif_rawcp, &enc, (TUINT32*)buffer);
shun-iwasawa ac6927
    break;
shun-iwasawa ac6927
shun-iwasawa ac6927
  default:
shun-iwasawa ac6927
    assert(0);
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
  assert(enc);
shun-iwasawa ac6927
  assert(dec * bitspersample == bytes * 8);
shun-iwasawa ac6927
  tif->tif_rawcc += enc;
shun-iwasawa ac6927
  tif->tif_rawcp += enc;
shun-iwasawa ac6927
shun-iwasawa ac6927
  return 1;
shun-iwasawa ac6927
}
shun-iwasawa ac6927
shun-iwasawa ac6927
//-------------------- DECODE CM16 ----------------------------
shun-iwasawa ac6927
shun-iwasawa ac6927
static int tif_toonz1_decode_cm16(UCHAR* buf_in, int* buf_in_len,
shun-iwasawa ac6927
                                  USHORT* buf_out, int tone_bits,
shun-iwasawa ac6927
                                  int color_offs, int color_bits,
shun-iwasawa ac6927
                                  int pencil_offs, int pencil_bits,
shun-iwasawa ac6927
                                  USHORT offset_mask)
shun-iwasawa ac6927
shun-iwasawa ac6927
{
shun-iwasawa ac6927
  UCHAR* in;
shun-iwasawa ac6927
  USHORT* out;
shun-iwasawa ac6927
  int count, col_offs, pen_offs;
shun-iwasawa ac6927
  UINT inval, in0, in1, tmp, not_colmask, not_penmask;
shun-iwasawa ac6927
  UINT outval, maxtone, outval_maxtone;
shun-iwasawa ac6927
shun-iwasawa ac6927
#define GET_IN0 (inval = *in++, in1 = inval & 0xF, in0 = inval >> 4)
shun-iwasawa ac6927
shun-iwasawa ac6927
  maxtone     = (1U << tone_bits) - 1U;
shun-iwasawa ac6927
  col_offs    = color_offs;
shun-iwasawa ac6927
  pen_offs    = pencil_offs;
shun-iwasawa ac6927
  not_colmask = ~(((1U << color_bits) - 1U) << color_offs);
shun-iwasawa ac6927
  not_penmask = ~(((1U << pencil_bits) - 1U) << pencil_offs);
shun-iwasawa ac6927
  assert(maxtone <= 0xF);
shun-iwasawa ac6927
  outval         = offset_mask;
shun-iwasawa ac6927
  outval_maxtone = outval | maxtone;
shun-iwasawa ac6927
shun-iwasawa ac6927
  in  = buf_in;
shun-iwasawa ac6927
  out = buf_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
  goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
count_out_and_start_from_in0:
shun-iwasawa ac6927
  outval_maxtone = outval | maxtone;
shun-iwasawa ac6927
  *out++         = (USHORT)outval_maxtone;
shun-iwasawa ac6927
  while (count--) *out++ = (USHORT)outval_maxtone;
shun-iwasawa ac6927
shun-iwasawa ac6927
start_from_in0:
shun-iwasawa ac6927
  if (GET_IN0 == 0xF) {
shun-iwasawa ac6927
    switch (in1) {
shun-iwasawa ac6927
    case 0x0:
shun-iwasawa ac6927
      *out++ = (USHORT)(outval | maxtone);
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x1:
shun-iwasawa ac6927
      count = GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x2:
shun-iwasawa ac6927
      count = *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x3:
shun-iwasawa ac6927
      count = *in++ << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x4:
shun-iwasawa ac6927
      count = *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x5:
shun-iwasawa ac6927
      count = *in++ << 12;
shun-iwasawa ac6927
      count += *in++ << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x6:
shun-iwasawa ac6927
      count = *in++ << 16;
shun-iwasawa ac6927
      count += *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x7:
shun-iwasawa ac6927
      count = *in++ << 20;
shun-iwasawa ac6927
      count += *in++ << 12;
shun-iwasawa ac6927
      count += *in++ << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x8:
shun-iwasawa ac6927
      outval &= not_colmask;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x9:
shun-iwasawa ac6927
      outval &= not_penmask;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xA:
shun-iwasawa ac6927
      outval = outval & not_colmask | GET_IN0 << col_offs;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xB:
shun-iwasawa ac6927
      outval = outval & not_penmask | GET_IN0 << pen_offs;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xC:
shun-iwasawa ac6927
      outval = outval & not_colmask | *in++ << col_offs;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xD:
shun-iwasawa ac6927
      outval = outval & not_penmask | *in++ << pen_offs;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xE:
shun-iwasawa ac6927
      *out++ = (USHORT)(outval | maxtone);
shun-iwasawa ac6927
      goto end_rle_decoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xF:
shun-iwasawa ac6927
      switch (GET_IN0) {
shun-iwasawa ac6927
      case 0:
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
      case 1:
shun-iwasawa ac6927
        tmp    = in1 << 12 | *in++ << 4;
shun-iwasawa ac6927
        *out++ = (USHORT)(tmp | GET_IN0);
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
      default:
shun-iwasawa ac6927
        goto rle_decoding_error;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      goto end_rle_decoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
    default:
shun-iwasawa ac6927
      goto rle_decoding_error;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  } else {
shun-iwasawa ac6927
    *out++ = (USHORT)(outval | in0);
shun-iwasawa ac6927
    goto start_from_in1;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
count_out_and_start_from_in1:
shun-iwasawa ac6927
  outval_maxtone = outval | maxtone;
shun-iwasawa ac6927
  *out++         = (USHORT)(outval_maxtone);
shun-iwasawa ac6927
  while (count--) *out++ = (USHORT)(outval_maxtone);
shun-iwasawa ac6927
shun-iwasawa ac6927
start_from_in1:
shun-iwasawa ac6927
  if (in1 == 0xF) {
shun-iwasawa ac6927
    switch (GET_IN0) {
shun-iwasawa ac6927
    case 0x0:
shun-iwasawa ac6927
      *out++ = (USHORT)(outval | maxtone);
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x1:
shun-iwasawa ac6927
      count = in1;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x2:
shun-iwasawa ac6927
      count = in1 << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x3:
shun-iwasawa ac6927
      count = in1 << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x4:
shun-iwasawa ac6927
      count = in1 << 12;
shun-iwasawa ac6927
      count += *in++ << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x5:
shun-iwasawa ac6927
      count = in1 << 16;
shun-iwasawa ac6927
      count += *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x6:
shun-iwasawa ac6927
      count = in1 << 20;
shun-iwasawa ac6927
      count += *in++ << 12;
shun-iwasawa ac6927
      count += *in++ << 4;
shun-iwasawa ac6927
      count += GET_IN0;
shun-iwasawa ac6927
      goto count_out_and_start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x7:
shun-iwasawa ac6927
      count = in1 << 24;
shun-iwasawa ac6927
      count += *in++ << 16;
shun-iwasawa ac6927
      count += *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out_and_start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x8:
shun-iwasawa ac6927
      outval &= not_colmask;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0x9:
shun-iwasawa ac6927
      outval &= not_penmask;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xA:
shun-iwasawa ac6927
      outval = outval & not_colmask | in1 << col_offs;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xB:
shun-iwasawa ac6927
      outval = outval & not_penmask | in1 << pen_offs;
shun-iwasawa ac6927
      goto start_from_in0;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xC:
shun-iwasawa ac6927
      tmp    = in1 << 4;
shun-iwasawa ac6927
      outval = outval & not_colmask | (tmp | GET_IN0) << col_offs;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xD:
shun-iwasawa ac6927
      tmp    = in1 << 4;
shun-iwasawa ac6927
      outval = outval & not_penmask | (tmp | GET_IN0) << pen_offs;
shun-iwasawa ac6927
      goto start_from_in1;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xE:
shun-iwasawa ac6927
      *out++ = (USHORT)(outval | maxtone);
shun-iwasawa ac6927
      goto end_rle_decoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xF:
shun-iwasawa ac6927
      switch (in1) {
shun-iwasawa ac6927
      case 0:
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
      case 1:
shun-iwasawa ac6927
        tmp    = *in++ << 8;
shun-iwasawa ac6927
        *out++ = (USHORT)(tmp | *in++);
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
      default:
shun-iwasawa ac6927
        goto rle_decoding_error;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      goto end_rle_decoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
    default:
shun-iwasawa ac6927
      return 0;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  } else {
shun-iwasawa ac6927
    *out++ = (USHORT)(outval | in1);
shun-iwasawa ac6927
    goto start_from_in0;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
end_rle_decoding:
shun-iwasawa ac6927
  if (buf_in_len) *buf_in_len = (int)(in - buf_in);
shun-iwasawa ac6927
  return (int)(out - buf_out);
shun-iwasawa ac6927
shun-iwasawa ac6927
rle_decoding_error:
shun-iwasawa ac6927
  if (buf_in_len) *buf_in_len = 0;
shun-iwasawa ac6927
  return 0;
shun-iwasawa ac6927
}
shun-iwasawa ac6927
shun-iwasawa ac6927
//-------------------- DECODE CM24 ----------------------------
shun-iwasawa ac6927
shun-iwasawa ac6927
static int tif_toonz1_decode_cm24(UCHAR* buf_in, int* buf_in_len,
shun-iwasawa ac6927
                                  TUINT32* buf_out) {
shun-iwasawa ac6927
  UCHAR* in;
shun-iwasawa ac6927
  TUINT32* out;
shun-iwasawa ac6927
  int count;
shun-iwasawa ac6927
  TUINT32 inval, tmp;
shun-iwasawa ac6927
  TUINT32 outval, outval_maxtone;
shun-iwasawa ac6927
  const int col_offs        = 8;
shun-iwasawa ac6927
  const int pen_offs        = 16;
shun-iwasawa ac6927
  const int xub_offs        = 24;
shun-iwasawa ac6927
  const TUINT32 not_colmask = 0xffff00ff;
shun-iwasawa ac6927
  const TUINT32 not_penmask = 0xff00ffff;
shun-iwasawa ac6927
  const TUINT32 not_xubmask = 0x00ffffff;
shun-iwasawa ac6927
  const TUINT32 maxtone     = 0x000000ff;
shun-iwasawa ac6927
shun-iwasawa ac6927
  outval         = 0;
shun-iwasawa ac6927
  outval_maxtone = outval | maxtone;
shun-iwasawa ac6927
shun-iwasawa ac6927
  in  = buf_in;
shun-iwasawa ac6927
  out = buf_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
  for (;;) {
shun-iwasawa ac6927
    inval = *in++;
shun-iwasawa ac6927
    switch (inval) {
shun-iwasawa ac6927
    case 0xF6:
shun-iwasawa ac6927
      outval = outval & not_xubmask | *in++ << xub_offs;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xF7:
shun-iwasawa ac6927
      count = *in++;
shun-iwasawa ac6927
      goto count_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xF8:
shun-iwasawa ac6927
      count = *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xF9:
shun-iwasawa ac6927
      count = *in++ << 16;
shun-iwasawa ac6927
      count += *in++ << 8;
shun-iwasawa ac6927
      count += *in++;
shun-iwasawa ac6927
      goto count_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFA:
shun-iwasawa ac6927
      outval &= not_colmask;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFB:
shun-iwasawa ac6927
      outval &= not_penmask;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFC:
shun-iwasawa ac6927
      outval = outval & not_colmask | *in++ << col_offs;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFD:
shun-iwasawa ac6927
      outval = outval & not_penmask | *in++ << pen_offs;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFE:
shun-iwasawa ac6927
      switch (*in++) {
shun-iwasawa ac6927
      case 0:
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
      case 1:
shun-iwasawa ac6927
        *out++ = outval | maxtone;
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
shun-iwasawa ac6927
      case 2:
shun-iwasawa ac6927
        tmp    = *in++;
shun-iwasawa ac6927
        tmp    = tmp << 8 | *in++;
shun-iwasawa ac6927
        tmp    = tmp << 8 | *in++;
shun-iwasawa ac6927
        *out++ = tmp << 8 | *in++;
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
shun-iwasawa ac6927
      default:
shun-iwasawa ac6927
        goto rle_decoding_error;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      goto end_rle_decoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 0xFF:
shun-iwasawa ac6927
      *out++ = outval | *in++;
shun-iwasawa ac6927
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    default:
shun-iwasawa ac6927
      *out++ = outval | inval;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    continue;
shun-iwasawa ac6927
shun-iwasawa ac6927
  count_out:
shun-iwasawa ac6927
    outval_maxtone = outval | maxtone;
shun-iwasawa ac6927
    *out++         = outval_maxtone;
shun-iwasawa ac6927
    while (count--) *out++ = outval_maxtone;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
end_rle_decoding:
shun-iwasawa ac6927
  if (buf_in_len) *buf_in_len = (int)(in - buf_in);
shun-iwasawa ac6927
  return (int)(out - buf_out);
shun-iwasawa ac6927
shun-iwasawa ac6927
rle_decoding_error:
shun-iwasawa ac6927
  if (buf_in_len) *buf_in_len = 0;
shun-iwasawa ac6927
  return 0;
shun-iwasawa ac6927
}
shun-iwasawa ac6927
}
shun-iwasawa ac6927
shun-iwasawa ac6927
//=============================================================================
shun-iwasawa ac6927
shun-iwasawa ac6927
namespace {
shun-iwasawa ac6927
shun-iwasawa ac6927
class ToonzRleCodecRegisterer {
shun-iwasawa ac6927
  static TIFFCodec* m_codec;
shun-iwasawa ac6927
shun-iwasawa ac6927
public:
shun-iwasawa ac6927
  ToonzRleCodecRegisterer() {
shun-iwasawa ac6927
    uint16 scheme    = 32881;
shun-iwasawa ac6927
    const char* name = "TOONZ4RLE";
shun-iwasawa ac6927
    m_codec          = TIFFRegisterCODEC(scheme, name, &TIFFInitToonz1);
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
  ~ToonzRleCodecRegisterer() {
shun-iwasawa ac6927
    TIFFUnRegisterCODEC(m_codec);
shun-iwasawa ac6927
    m_codec = 0;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
};
shun-iwasawa ac6927
shun-iwasawa ac6927
TIFFCodec* ToonzRleCodecRegisterer::m_codec = 0;
shun-iwasawa ac6927
shun-iwasawa ac6927
ToonzRleCodecRegisterer registerer;
shun-iwasawa ac6927
}  // namespace
shun-iwasawa ac6927
shun-iwasawa ac6927
//=============================================================================
shun-iwasawa ac6927
shun-iwasawa ac6927
#if 0
shun-iwasawa ac6927
shun-iwasawa ac6927
/*
shun-iwasawa ac6927
Queste stanno qui piu' che altro per memoria...
shun-iwasawa ac6927
*/
shun-iwasawa ac6927
shun-iwasawa ac6927
extern "C" {
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*===========================================================================*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
#define GET_INVAL                                                              \
shun-iwasawa ac6927
  {                                                                            \
shun-iwasawa ac6927
    inval = *in++;                                                             \
shun-iwasawa ac6927
    remain--;                                                                  \
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
#define PUT_OUTVAL                                                             \
shun-iwasawa ac6927
  { *out++ = (UCHAR)outval; }
shun-iwasawa ac6927
shun-iwasawa ac6927
/*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  int tif_toonz1_safe_bytes_for_pixels(int n_pix, int pixbytes)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    if (n_pix < 0)
shun-iwasawa ac6927
      return 0;
shun-iwasawa ac6927
    switch (pixbytes)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
    case 1: return 2 * n_pix + 1;
shun-iwasawa ac6927
    case 2: return 5 * n_pix + 2;   /* == 2.5 * n_bytes + 2 */
shun-iwasawa ac6927
    case 4: return 8 * n_pix + 6;   /* == 2   * n_bytes + 6 */
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    return 0;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  static int tif_toonz1_encode_cm16(USHORT* buf_in, int buf_in_len,
shun-iwasawa ac6927
    UCHAR* buf_out,
shun-iwasawa ac6927
    int tone_bits,
shun-iwasawa ac6927
    int color_offs, int color_bits,
shun-iwasawa ac6927
    int pencil_offs, int pencil_bits,
shun-iwasawa ac6927
    USHORT offset_mask)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    int  count, prevremain, remain;
shun-iwasawa ac6927
    UINT inval, outval;
shun-iwasawa ac6927
    UINT lastcol__, lastpen__, lastval, lastcolpen;
shun-iwasawa ac6927
    int  col_offs, pen_offs;
shun-iwasawa ac6927
    UINT colpenmask, colmask, penmask, maxtone;
shun-iwasawa ac6927
    UINT incolpen, incol__, incol, inpen__, inpen, tone;
shun-iwasawa ac6927
    USHORT* in, save;
shun-iwasawa ac6927
    UCHAR* out;
shun-iwasawa ac6927
shun-iwasawa ac6927
    maxtone = (1U << tone_bits) - 1U;
shun-iwasawa ac6927
    col_offs = color_offs;
shun-iwasawa ac6927
    pen_offs = pencil_offs;
shun-iwasawa ac6927
    colmask = ((1U << color_bits) - 1U) << color_offs;
shun-iwasawa ac6927
    penmask = ((1U << pencil_bits) - 1U) << pencil_offs;
shun-iwasawa ac6927
    colpenmask = colmask | penmask;
shun-iwasawa ac6927
shun-iwasawa ac6927
    remain = buf_in_len;
shun-iwasawa ac6927
    lastcol__ = 0;
shun-iwasawa ac6927
    lastpen__ = 0;
shun-iwasawa ac6927
    lastval = offset_mask | maxtone;
shun-iwasawa ac6927
    lastcolpen = 0;
shun-iwasawa ac6927
shun-iwasawa ac6927
    in = buf_in;
shun-iwasawa ac6927
    out = buf_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
    if (buf_in_len <= 1)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      if (buf_in_len < 0)
shun-iwasawa ac6927
        return 0;
shun-iwasawa ac6927
      if (buf_in_len == 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = 0xFF;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = 0x00;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        GET_INVAL
shun-iwasawa ac6927
          if (inval == (offset_mask | maxtone))
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xFE;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xFF;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = 0x10 | inval >> 12;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval >> 4;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval << 4;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      return out - buf_out;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
shun-iwasawa ac6927
    save = buf_in[buf_in_len - 1];
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ colpenmask;
shun-iwasawa ac6927
shun-iwasawa ac6927
    GET_INVAL
shun-iwasawa ac6927
shun-iwasawa ac6927
      check_colpen_on_out0 :
shun-iwasawa ac6927
    incolpen = inval & colpenmask;
shun-iwasawa ac6927
    if (incolpen == lastcolpen)
shun-iwasawa ac6927
      goto check_tone_on_out0;
shun-iwasawa ac6927
    lastcolpen = incolpen;
shun-iwasawa ac6927
    if (!remain)
shun-iwasawa ac6927
      goto end_toonz1_encoding_on_out0;
shun-iwasawa ac6927
shun-iwasawa ac6927
    /*check_col_on_out0:*/
shun-iwasawa ac6927
    incol__ = inval & colmask;
shun-iwasawa ac6927
    if (incol__ != lastcol__)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastcol__ = incol__;
shun-iwasawa ac6927
      if (incol__ == 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x8;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          goto check_pen_on_out0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        incol = incol__ >> col_offs;
shun-iwasawa ac6927
        if (incol <= 0xF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0xA;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = incol << 4;
shun-iwasawa ac6927
          goto check_pen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0xC;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = incol;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_pen_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  check_pen_on_out0:
shun-iwasawa ac6927
    inpen__ = inval & penmask;
shun-iwasawa ac6927
    if (inpen__ != lastpen__)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastpen__ = inpen__;
shun-iwasawa ac6927
      if (inpen__ == 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x9;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          goto check_tone_on_out0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        inpen = inpen__ >> pen_offs;
shun-iwasawa ac6927
        if (inpen <= 0xF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0xB;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = inpen << 4;
shun-iwasawa ac6927
          goto check_tone_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0xD;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = inpen;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_tone_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  check_tone_on_out0:
shun-iwasawa ac6927
    tone = inval & maxtone;
shun-iwasawa ac6927
    if (tone == maxtone)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastval = inval;
shun-iwasawa ac6927
      prevremain = remain;
shun-iwasawa ac6927
      do
shun-iwasawa ac6927
        GET_INVAL
shun-iwasawa ac6927
        while (inval == lastval);
shun-iwasawa ac6927
      count = prevremain - remain - 1;
shun-iwasawa ac6927
      if (count <= 0xF)
shun-iwasawa ac6927
        if (count == 0)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0x0;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_colpen_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0xF << 4) | 0x1;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count << 4;
shun-iwasawa ac6927
          goto check_colpen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
      else if (count <= 0xFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x2;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          goto check_colpen_on_out0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count <= 0xFFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x3;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 4;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count << 4;
shun-iwasawa ac6927
        goto check_colpen_on_out1;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count <= 0xFFFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x4;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 8;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          goto check_colpen_on_out0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count <= 0xFFFFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x5;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 12;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 4;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count << 4;
shun-iwasawa ac6927
        goto check_colpen_on_out1;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count <= 0xFFFFFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x6;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 16;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 8;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          goto check_colpen_on_out0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count <= 0xFFFFFFF)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = (0xF << 4) | 0x7;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 20;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 12;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count >> 4;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = count << 4;
shun-iwasawa ac6927
        goto check_colpen_on_out1;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
        return 0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    else
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval = tone << 4;
shun-iwasawa ac6927
      GET_INVAL
shun-iwasawa ac6927
        /*goto check_colpen_on_out1;*/
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
shun-iwasawa ac6927
  check_colpen_on_out1:
shun-iwasawa ac6927
    incolpen = inval & colpenmask;
shun-iwasawa ac6927
    if (incolpen == lastcolpen)
shun-iwasawa ac6927
      goto check_tone_on_out1;
shun-iwasawa ac6927
    lastcolpen = incolpen;
shun-iwasawa ac6927
    if (!remain)
shun-iwasawa ac6927
      goto end_toonz1_encoding_on_out1;
shun-iwasawa ac6927
shun-iwasawa ac6927
    /*check_col_on_out1:*/
shun-iwasawa ac6927
    incol__ = inval & colmask;
shun-iwasawa ac6927
    if (incol__ != lastcol__)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastcol__ = incol__;
shun-iwasawa ac6927
      outval |= 0xF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        if (incol__ == 0)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = 0x8 << 4;
shun-iwasawa ac6927
          goto check_pen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          incol = incol__ >> col_offs;
shun-iwasawa ac6927
          if (incol <= 0xF)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = (0xA << 4) | incol;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              goto check_pen_on_out0;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = (0xC << 4) | (incol >> 4);
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = incol << 4;
shun-iwasawa ac6927
            goto check_pen_on_out1;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  check_pen_on_out1:
shun-iwasawa ac6927
    inpen__ = inval & penmask;
shun-iwasawa ac6927
    if (inpen__ != lastpen__)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastpen__ = inpen__;
shun-iwasawa ac6927
      outval |= 0xF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        if (inpen__ == 0)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = 0x9 << 4;
shun-iwasawa ac6927
          goto check_tone_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          inpen = inpen__ >> pen_offs;
shun-iwasawa ac6927
          if (inpen <= 0xF)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = (0xB << 4) | inpen;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              goto check_tone_on_out0;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = (0xD << 4) | (inpen >> 4);
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inpen << 4;
shun-iwasawa ac6927
            goto check_tone_on_out1;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
  check_tone_on_out1:
shun-iwasawa ac6927
    tone = inval & maxtone;
shun-iwasawa ac6927
    if (tone == maxtone)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      lastval = inval;
shun-iwasawa ac6927
      prevremain = remain;
shun-iwasawa ac6927
      do
shun-iwasawa ac6927
        GET_INVAL
shun-iwasawa ac6927
        while (inval == lastval);
shun-iwasawa ac6927
      count = prevremain - remain - 1;
shun-iwasawa ac6927
      outval |= 0xF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        if (count <= 0xF)
shun-iwasawa ac6927
          if (count == 0)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0x0 << 4;
shun-iwasawa ac6927
            goto check_colpen_on_out1;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = (0x1 << 4) | count;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              goto check_colpen_on_out0;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
        else if (count <= 0xFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x2 << 4) | (count >> 4);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count << 4;
shun-iwasawa ac6927
          goto check_colpen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else if (count <= 0xFFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x3 << 4) | (count >> 8);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_colpen_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else if (count <= 0xFFFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x4 << 4) | (count >> 12);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 4;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count << 4;
shun-iwasawa ac6927
          goto check_colpen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else if (count <= 0xFFFFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x5 << 4) | (count >> 16);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 8;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_colpen_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else if (count <= 0xFFFFFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x6 << 4) | (count >> 20);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 12;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 4;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count << 4;
shun-iwasawa ac6927
          goto check_colpen_on_out1;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else if (count <= 0xFFFFFFF)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          outval = (0x7 << 4) | (count >> 24);
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 16;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count >> 8;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            outval = count;
shun-iwasawa ac6927
          PUT_OUTVAL
shun-iwasawa ac6927
            goto check_colpen_on_out0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
          return 0;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    else
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval |= tone;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        GET_INVAL
shun-iwasawa ac6927
        goto check_colpen_on_out0;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
shun-iwasawa ac6927
  end_toonz1_encoding_on_out0:
shun-iwasawa ac6927
    if (save == (in[-2] | maxtone))
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval = 0xFE;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    else
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval = 0xFF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = 0x10 | save >> 12;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save >> 4;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save << 4;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
    return out - buf_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
  end_toonz1_encoding_on_out1:
shun-iwasawa ac6927
    if (save == (in[-2] | maxtone))
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval |= 0xF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = 0xE0;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    else
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval |= 0xF;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = 0xF1;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save >> 8;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
    return out - buf_out;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  static int tif_toonz1_encode_cm24(TUINT32* buf_in, int buf_in_len,
shun-iwasawa ac6927
    UCHAR* buf_out)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    int   count, prevremain, remain;
shun-iwasawa ac6927
    TUINT32 inval, outval;
shun-iwasawa ac6927
    TUINT32 lastcol__, lastpen__, lastxub__, lastval, lastenot;
shun-iwasawa ac6927
    TUINT32 inenot, incol__, incol, inpen__, inpen, inxub__, inxub, tone;
shun-iwasawa ac6927
    TUINT32* in, save;
shun-iwasawa ac6927
    UCHAR* out;
shun-iwasawa ac6927
    const int col_offs = 8;
shun-iwasawa ac6927
    const int pen_offs = 16;
shun-iwasawa ac6927
    const int xub_offs = 24;
shun-iwasawa ac6927
    const TUINT32 enotmask = 0xffffff00;
shun-iwasawa ac6927
    const TUINT32 colmask = 0x0000ff00;
shun-iwasawa ac6927
    const TUINT32 penmask = 0x00ff0000;
shun-iwasawa ac6927
    const TUINT32 xubmask = 0xff000000;
shun-iwasawa ac6927
    const TUINT32 maxtone = 0x000000ff;
shun-iwasawa ac6927
shun-iwasawa ac6927
    /* enot == ~tone , xub == extra upper byte */
shun-iwasawa ac6927
shun-iwasawa ac6927
    remain = buf_in_len;
shun-iwasawa ac6927
    lastcol__ = 0;
shun-iwasawa ac6927
    lastpen__ = 0;
shun-iwasawa ac6927
    lastxub__ = 0;
shun-iwasawa ac6927
    lastval = maxtone;
shun-iwasawa ac6927
    lastenot = 0;
shun-iwasawa ac6927
shun-iwasawa ac6927
    in = buf_in;
shun-iwasawa ac6927
    out = buf_out;
shun-iwasawa ac6927
shun-iwasawa ac6927
    if (buf_in_len <= 1)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      if (buf_in_len < 0)
shun-iwasawa ac6927
        return 0;
shun-iwasawa ac6927
      if (buf_in_len == 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        outval = 0xFE;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
          outval = 0x00;
shun-iwasawa ac6927
        PUT_OUTVAL
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        GET_INVAL
shun-iwasawa ac6927
          if (inval == maxtone)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xFE;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = 0x01;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xFE;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = 0x02;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval >> 24;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval >> 16;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval >> 8;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inval;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      return out - buf_out;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
shun-iwasawa ac6927
    save = buf_in[buf_in_len - 1];
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ enotmask;
shun-iwasawa ac6927
shun-iwasawa ac6927
    GET_INVAL
shun-iwasawa ac6927
shun-iwasawa ac6927
      for (;;)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        inenot = inval & enotmask;
shun-iwasawa ac6927
        if (inenot != lastenot)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          lastenot = inenot;
shun-iwasawa ac6927
          if (!remain)
shun-iwasawa ac6927
            goto end_toonz1_encoding;
shun-iwasawa ac6927
shun-iwasawa ac6927
          incol__ = inval & colmask;
shun-iwasawa ac6927
          if (incol__ != lastcol__)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            lastcol__ = incol__;
shun-iwasawa ac6927
            if (incol__ == 0)
shun-iwasawa ac6927
            {
shun-iwasawa ac6927
              outval = 0xFA;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
            }
shun-iwasawa ac6927
            else
shun-iwasawa ac6927
            {
shun-iwasawa ac6927
              incol = incol__ >> col_offs;
shun-iwasawa ac6927
              outval = 0xFC;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
                outval = incol;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
            }
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          inpen__ = inval & penmask;
shun-iwasawa ac6927
          if (inpen__ != lastpen__)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            lastpen__ = inpen__;
shun-iwasawa ac6927
            if (inpen__ == 0)
shun-iwasawa ac6927
            {
shun-iwasawa ac6927
              outval = 0xFB;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
            }
shun-iwasawa ac6927
            else
shun-iwasawa ac6927
            {
shun-iwasawa ac6927
              inpen = inpen__ >> pen_offs;
shun-iwasawa ac6927
              outval = 0xFD;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
                outval = inpen;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
            }
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          inxub__ = inval & xubmask;
shun-iwasawa ac6927
          if (inxub__ != lastxub__)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            lastxub__ = inxub__;
shun-iwasawa ac6927
            inxub = inxub__ >> xub_offs;
shun-iwasawa ac6927
            outval = 0xF6;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = inxub;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        tone = inval & maxtone;
shun-iwasawa ac6927
        if (tone == maxtone)
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          lastval = inval;
shun-iwasawa ac6927
          prevremain = remain;
shun-iwasawa ac6927
          do
shun-iwasawa ac6927
            GET_INVAL
shun-iwasawa ac6927
            while (inval == lastval);
shun-iwasawa ac6927
          count = prevremain - remain - 1;
shun-iwasawa ac6927
          if (count <= 0xFF)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xF7;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else if (count <= 0xFFFF)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xF8;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count >> 8;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            while (count > 0xFFFFFF)
shun-iwasawa ac6927
            {
shun-iwasawa ac6927
              outval = 0xF9;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
                outval = 0xFF;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
                outval = 0xFF;
shun-iwasawa ac6927
              PUT_OUTVAL
shun-iwasawa ac6927
                outval = 0xFF;
shun-iwasawa ac6927
              count -= 0xFFFFFF + 1;
shun-iwasawa ac6927
            }
shun-iwasawa ac6927
            outval = 0xF9;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count >> 16;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count >> 8;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = count;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          if (tone < 0xF6)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = tone;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            outval = 0xFF;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
              outval = tone;
shun-iwasawa ac6927
            PUT_OUTVAL
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          GET_INVAL
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
shun-iwasawa ac6927
  end_toonz1_encoding:
shun-iwasawa ac6927
    if (save == (in[-2] | maxtone))
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval = 0xFE;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = 0x01;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    else
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      outval = 0xFE;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = 0x02;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save >> 24;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save >> 16;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save >> 8;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
        outval = save;
shun-iwasawa ac6927
      PUT_OUTVAL
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
    return out - buf_out;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  static int tif_toonz1_encode_extra(UCHAR* buf_in, int buf_in_len,
shun-iwasawa ac6927
    UCHAR* buf_out)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    UCHAR* in, inval, lastval, save;
shun-iwasawa ac6927
    UCHAR* out;
shun-iwasawa ac6927
    int count, remain;
shun-iwasawa ac6927
shun-iwasawa ac6927
    remain = buf_in_len;
shun-iwasawa ac6927
    in = buf_in;
shun-iwasawa ac6927
    out = buf_out;
shun-iwasawa ac6927
    if (buf_in_len < 2)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      if (!buf_in_len)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        *out = 0;
shun-iwasawa ac6927
        return 1;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        *out++ = 1;
shun-iwasawa ac6927
        *out++ = *in;
shun-iwasawa ac6927
        *out = 0;
shun-iwasawa ac6927
        return 3;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    save = buf_in[buf_in_len - 1];
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = buf_in[buf_in_len - 2] ^ 0xff;
shun-iwasawa ac6927
shun-iwasawa ac6927
    lastval = *buf_in;
shun-iwasawa ac6927
    count = 0;
shun-iwasawa ac6927
    for (;;)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      GET_INVAL
shun-iwasawa ac6927
        if (inval == lastval)
shun-iwasawa ac6927
          count++;
shun-iwasawa ac6927
        else
shun-iwasawa ac6927
        {
shun-iwasawa ac6927
          while (count >> 15)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            *out++ = 0xff;
shun-iwasawa ac6927
            *out++ = 0xff;
shun-iwasawa ac6927
            *out++ = lastval;
shun-iwasawa ac6927
            count -= 0x7fff;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          if (count >> 7)
shun-iwasawa ac6927
          {
shun-iwasawa ac6927
            *out++ = 0x80 | count & 0x7f;
shun-iwasawa ac6927
            *out++ = count >> 7;
shun-iwasawa ac6927
          }
shun-iwasawa ac6927
          else
shun-iwasawa ac6927
            *out++ = count;
shun-iwasawa ac6927
          *out++ = lastval;
shun-iwasawa ac6927
          lastval = inval;
shun-iwasawa ac6927
          count = 1;
shun-iwasawa ac6927
          if (!remain)
shun-iwasawa ac6927
            break;
shun-iwasawa ac6927
        }
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    *out++ = 1;
shun-iwasawa ac6927
    *out++ = save;
shun-iwasawa ac6927
    *out++ = 0;
shun-iwasawa ac6927
shun-iwasawa ac6927
    buf_in[buf_in_len - 1] = save;
shun-iwasawa ac6927
    return out - buf_out;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
  static int tif_toonz1_decode_extra(UCHAR* buf_in, int* buf_in_len,
shun-iwasawa ac6927
    UCHAR* buf_out)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    UCHAR* in, val;
shun-iwasawa ac6927
    UCHAR* out;
shun-iwasawa ac6927
    int count;
shun-iwasawa ac6927
shun-iwasawa ac6927
    in = buf_in;
shun-iwasawa ac6927
    out = buf_out;
shun-iwasawa ac6927
    for (;;)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
      count = (SCHAR)*in++;
shun-iwasawa ac6927
      if (count > 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        val = *in++;
shun-iwasawa ac6927
        while (count--)
shun-iwasawa ac6927
          *out++ = val;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else if (count < 0)
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        count &= 0x7f;
shun-iwasawa ac6927
        count |= *in++ << 7;
shun-iwasawa ac6927
        val = *in++;
shun-iwasawa ac6927
        while (count--)
shun-iwasawa ac6927
          *out++ = val;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
        break;
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    if (buf_in_len)
shun-iwasawa ac6927
      *buf_in_len = in - buf_in;
shun-iwasawa ac6927
    return out - buf_out;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
  /*---------------------------------------------------------------------------*/
shun-iwasawa ac6927
shun-iwasawa ac6927
  static int
shun-iwasawa ac6927
    DECLARE4(Toonz1Encode, TIFF*, tif, u_char*, buffer, u_long, bytes, u_int, s)
shun-iwasawa ac6927
  {
shun-iwasawa ac6927
    int enc;
shun-iwasawa ac6927
    short bitspersample;
shun-iwasawa ac6927
    USHORT* palette;
shun-iwasawa ac6927
    int tone_bits, color_offs, color_bits, pencil_offs, pencil_bits;
shun-iwasawa ac6927
    USHORT offset_mask;
shun-iwasawa ac6927
shun-iwasawa ac6927
    if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample))
shun-iwasawa ac6927
      assert(0);
shun-iwasawa ac6927
    enc = 0;
shun-iwasawa ac6927
    switch (bitspersample)
shun-iwasawa ac6927
    {
shun-iwasawa ac6927
    case 8:
shun-iwasawa ac6927
      enc = tif_toonz1_encode_extra((UCHAR*)buffer, (int)bytes,
shun-iwasawa ac6927
        (UCHAR*)tif->tif_rawcp);
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 16:
shun-iwasawa ac6927
      if (TIFFGetField(tif, TIFFTAG_TOONZPALETTE, &palette))
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        tone_bits = palette[4];
shun-iwasawa ac6927
        color_offs = palette[5];  color_bits = palette[6];
shun-iwasawa ac6927
        pencil_offs = palette[7];  pencil_bits = palette[8];
shun-iwasawa ac6927
        offset_mask = palette[9];
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      else
shun-iwasawa ac6927
      {
shun-iwasawa ac6927
        tone_bits = 4;
shun-iwasawa ac6927
        color_offs = 4;  color_bits = 7;
shun-iwasawa ac6927
        pencil_offs = 11;  pencil_bits = 5;
shun-iwasawa ac6927
        offset_mask = 0;
shun-iwasawa ac6927
      }
shun-iwasawa ac6927
      enc = tif_toonz1_encode_cm16((USHORT*)buffer, (int)(bytes >> 1),
shun-iwasawa ac6927
        (UCHAR*)tif->tif_rawcp,
shun-iwasawa ac6927
        tone_bits,
shun-iwasawa ac6927
        color_offs, color_bits,
shun-iwasawa ac6927
        pencil_offs, pencil_bits, offset_mask);
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    case 32:
shun-iwasawa ac6927
      enc = tif_toonz1_encode_cm24((TUINT32*)buffer, (int)(bytes >> 2),
shun-iwasawa ac6927
        (UCHAR*)tif->tif_rawcp);
shun-iwasawa ac6927
      break;
shun-iwasawa ac6927
shun-iwasawa ac6927
    default:
shun-iwasawa ac6927
      assert(0);
shun-iwasawa ac6927
    }
shun-iwasawa ac6927
    assert(enc);
shun-iwasawa ac6927
    tif->tif_rawcc += enc;
shun-iwasawa ac6927
    tif->tif_rawcp += enc;
shun-iwasawa ac6927
    if (tif->tif_rawcc >= tif->tif_rawdatasize)
shun-iwasawa ac6927
      if (!TIFFFlushData1(tif))
shun-iwasawa ac6927
        return -1;
shun-iwasawa ac6927
shun-iwasawa ac6927
    return 1;
shun-iwasawa ac6927
  }
shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
shun-iwasawa ac6927
}
shun-iwasawa ac6927
#endif