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