Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Campbell Barton d869b5
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
Toshihiro Shimizu 890ddd
#define _CRT_SECURE_NO_DEPRECATE 1
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys stat.h=""></sys>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "../compatibility/tnz4.h"
Toshihiro Shimizu 890ddd
#include "../compatibility/inforegion.h"
Toshihiro Shimizu 890ddd
#include "filequantel.h"
Toshihiro Shimizu 890ddd
#include "filequantelP.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 12c444
#ifndef min
Shinya Kitaoka 120a6e
#define min(a, b) (((a) < (b)) ? (a) : (b))
Shinya Kitaoka 12c444
#endif
Shinya Kitaoka 12c444
#ifndef max
Shinya Kitaoka 120a6e
#define max(a, b) (((a) > (b)) ? (a) : (b))
Shinya Kitaoka 12c444
#endif
Shinya Kitaoka 12c444
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#define STAT_BUF struct _stat
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define STAT_BUF struct stat
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static IMAGE *img_read_region_quantel_no_interlaced(T_CHAR *fname, int x1,
Shinya Kitaoka 120a6e
                                                    int y1, int x2, int y2,
Shinya Kitaoka 120a6e
                                                    int scale, int type);
Shinya Kitaoka 120a6e
static IMAGE *img_read_region_quantel_interlaced(T_CHAR *fname, int x1, int y1,
Shinya Kitaoka 120a6e
                                                 int x2, int y2, int scale,
Shinya Kitaoka 120a6e
                                                 int type);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
/*static char *Quantel_error[] = { "qnt locked format 720x576",
Shinya Kitaoka 120a6e
                                 "qtl locked format 720x486",
Shinya Kitaoka 120a6e
                                 "yuv locked format 720x486 / 720x576",
Shinya Kitaoka 120a6e
                                 "sdl locked format 720x486 / 720x576" };
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#define QUANTEL_BUILD_ROW                                                      \
Shinya Kitaoka 120a6e
                                                                               \
Shinya Kitaoka 120a6e
  for (x = 0; x < region.scanNcol / 2; x++) {                                  \
Shinya Kitaoka 120a6e
    u                   = *lineBuf++ - 128;                                    \
Shinya Kitaoka 120a6e
    j                   = *lineBuf++ - 16;                                     \
Shinya Kitaoka 120a6e
    if (j < 0) j        = 0;                                                   \
Shinya Kitaoka 120a6e
    v                   = *lineBuf++ - 128;                                    \
Shinya Kitaoka 120a6e
    k                   = *lineBuf++ - 16;                                     \
Shinya Kitaoka 120a6e
    r                   = 76310 * j + 104635 * v;                              \
Shinya Kitaoka 120a6e
    if (r > 0xFFFFFF) r = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (r <= 0xFFFF) r  = 0;                                                   \
Shinya Kitaoka 120a6e
    g                   = 76310 * j + -25690 * u + -53294 * v;                 \
Shinya Kitaoka 120a6e
    if (g > 0xFFFFFF) g = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (g <= 0xFFFF) g  = 0;                                                   \
Shinya Kitaoka 120a6e
    b                   = 76310 * j + 132278 * u;                              \
Shinya Kitaoka 120a6e
    if (b > 0xFFFFFF) b = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (b <= 0xFFFF) b  = 0;                                                   \
Shinya Kitaoka 120a6e
    bufRGB->r           = (unsigned char)(r >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->g           = (unsigned char)(g >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->b           = (unsigned char)(b >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->m           = 255;                                                 \
Shinya Kitaoka 120a6e
    bufRGB++;                                                                  \
Shinya Kitaoka 120a6e
    r                   = 76310 * k + 104635 * v;                              \
Shinya Kitaoka 120a6e
    if (r > 0xFFFFFF) r = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (r <= 0xFFFF) r  = 0;                                                   \
Shinya Kitaoka 120a6e
    g                   = 76310 * k + -25690 * u + -53294 * v;                 \
Shinya Kitaoka 120a6e
    if (g > 0xFFFFFF) g = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (g <= 0xFFFF) g  = 0;                                                   \
Shinya Kitaoka 120a6e
    b                   = 76310 * k + 132278 * u;                              \
Shinya Kitaoka 120a6e
    if (b > 0xFFFFFF) b = 0xFFFFFF;                                            \
Shinya Kitaoka 120a6e
    if (b <= 0xFFFF) b  = 0;                                                   \
Shinya Kitaoka 120a6e
    bufRGB->r           = (unsigned char)(r >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->g           = (unsigned char)(g >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->b           = (unsigned char)(b >> 16);                            \
Shinya Kitaoka 120a6e
    bufRGB->m           = 255;                                                 \
Shinya Kitaoka 120a6e
    bufRGB++;                                                                  \
Shinya Kitaoka 120a6e
    lineBuf += ((region.step - 1) * 4);                                        \
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
#define QUANTEL_GET_YSIZE(ysize)                                               \
Shinya Kitaoka 120a6e
  (abs((ysize - QNT_PAL_YSIZE)) > abs((ysize - QTL_NTSC_YSIZE)))               \
Shinya Kitaoka 120a6e
      ? QTL_NTSC_YSIZE                                                         \
Shinya Kitaoka 120a6e
      : QNT_PAL_YSIZE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void vpb_string(const char *str, int field_type, char **p_h,
shun-iwasawa 27b0cf
                       char *stop) {
Shinya Kitaoka 120a6e
  char *h;
Shinya Kitaoka 120a6e
  int len;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  h   = *p_h;
Shinya Kitaoka 120a6e
  len = strlen(str);
Shinya Kitaoka 120a6e
  NOT_MORE_THAN(255, len)
Shinya Kitaoka 120a6e
  if (h + 3 + len >= stop) return;
Shinya Kitaoka 120a6e
  *h++ = field_type;
Shinya Kitaoka 120a6e
  *h++ = 0;
Shinya Kitaoka 120a6e
  *h++ = len;
Shinya Kitaoka 120a6e
  strncpy(h, str, (size_t)len);
Shinya Kitaoka 120a6e
  h += len;
Shinya Kitaoka 120a6e
  *p_h = h;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void vpb_int(int val, char **p_h) {
Shinya Kitaoka 120a6e
  char *h;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  h    = *p_h;
Shinya Kitaoka 120a6e
  *h++ = (char)(val >> 24);
Shinya Kitaoka 120a6e
  *h++ = (char)(val >> 16);
Shinya Kitaoka 120a6e
  *h++ = (char)(val >> 8);
Shinya Kitaoka 120a6e
  *h++ = (char)(val);
Shinya Kitaoka 120a6e
  *p_h = h;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void vpb_short(int val, char **p_h) {
Shinya Kitaoka 120a6e
  char *h;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  h    = *p_h;
Shinya Kitaoka 120a6e
  *h++ = (char)(val >> 8);
Shinya Kitaoka 120a6e
  *h++ = (char)(val);
Shinya Kitaoka 120a6e
  *p_h = h;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static TBOOL write_vpb_header(FILE *file, int xsize, int ysize) {
Shinya Kitaoka 120a6e
  char header[1024], *h, *stop;
Shinya Kitaoka 120a6e
  int bytes, sqlx, sqly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  h    = header;
Shinya Kitaoka 120a6e
  stop = header + 1024;
Shinya Kitaoka 120a6e
  vpb_string("Picture", 0x03, &h, stop);
Shinya Kitaoka 120a6e
  vpb_string("Toonz", 0x97, &h, stop);
Shinya Kitaoka 120a6e
  if (ysize == 486) {
Shinya Kitaoka 120a6e
    sqlx = 2133; /* 711 * 3   NTSC */
Shinya Kitaoka 120a6e
    sqly = 1940; /* 485 * 4        */
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    sqlx = 2103; /* 701 * 3   PAL  */
Shinya Kitaoka 120a6e
    sqly = 2300; /* 575 * 4        */
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bytes = xsize * ysize * 2;
Shinya Kitaoka 120a6e
  if (h + 3 + 20 < stop) {
Shinya Kitaoka 120a6e
    *h++ = 0x10;
Shinya Kitaoka 120a6e
    *h++ = 1;
Shinya Kitaoka 120a6e
    *h++ = 20;
Shinya Kitaoka 120a6e
    vpb_short(0x10, &h);  /* format code */
Shinya Kitaoka 120a6e
    vpb_short(0x00, &h);  /* format code modifier */
Shinya Kitaoka 120a6e
    vpb_short(xsize, &h); /* width of picture in pixels */
Shinya Kitaoka 120a6e
    vpb_short(ysize, &h); /* height of picture in pixels */
Shinya Kitaoka 120a6e
    vpb_short(sqlx, &h);  /* width of arbitrary square in pixels */
Shinya Kitaoka 120a6e
    vpb_short(sqly, &h);  /* height of arbitrary square in pixels */
Shinya Kitaoka 120a6e
    vpb_int(0, &h);       /* offset from end of 1st K for start of image */
Shinya Kitaoka 120a6e
    vpb_int(bytes, &h);   /* length in bytes of image within file */
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  *h++ = 0x04;
Shinya Kitaoka 120a6e
  *h++ = 1;
Shinya Kitaoka 120a6e
  *h++ = 4;
Shinya Kitaoka 120a6e
  vpb_int(bytes, &h);
Shinya Kitaoka 120a6e
  while (h < stop) *h++ = (char)0xFF;
Shinya Kitaoka 120a6e
  return fwrite(header, 1, 1024, file) == 1024;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int quantel_get_info(const T_CHAR *fname, int type, int *xsize,
Shinya Kitaoka 120a6e
                            int *ysize) {
Shinya Kitaoka 120a6e
  STAT_BUF f_stat;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (_wstat(fname, &f_stat) == -1) return FALSE;
Shinya Kitaoka 120a6e
  switch (type) {
Shinya Kitaoka 120a6e
  case QNT_FORMAT:
Shinya Kitaoka 120a6e
    *xsize = QNT_PAL_XSIZE;
Shinya Kitaoka 120a6e
    *ysize = QNT_PAL_YSIZE;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case QTL_FORMAT:
Shinya Kitaoka 120a6e
    *xsize = QTL_NTSC_XSIZE;
Shinya Kitaoka 120a6e
    *ysize = QTL_NTSC_YSIZE;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case YUV_FORMAT:
Shinya Kitaoka 120a6e
    *xsize = QUANTEL_XSIZE;
Shinya Kitaoka 120a6e
    switch (f_stat.st_size) {
Shinya Kitaoka 120a6e
    case QNT_PAL_FILE_SIZE:
Shinya Kitaoka 120a6e
    case QNT_PAL_W_FILE_SIZE:
Shinya Kitaoka 120a6e
      *ysize = QNT_PAL_YSIZE;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case QTL_NTSC_FILE_SIZE:
Shinya Kitaoka 120a6e
    case QTL_NTSC_W_FILE_SIZE:
Shinya Kitaoka 120a6e
      *ysize = QTL_NTSC_YSIZE;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      *ysize = f_stat.st_size / (QUANTEL_XSIZE * sizeof(short));
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case SDL_FORMAT:
Shinya Kitaoka 120a6e
    *xsize = QNT_PAL_XSIZE;
Shinya Kitaoka 120a6e
    switch (f_stat.st_size) {
Shinya Kitaoka 120a6e
    case QNT_PAL_FILE_SIZE:
Shinya Kitaoka 120a6e
      *ysize = QNT_PAL_YSIZE;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case QTL_NTSC_FILE_SIZE:
Shinya Kitaoka 120a6e
      *ysize = QTL_NTSC_YSIZE;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      /*printf("error: bad file dimension\n");*/
Shinya Kitaoka 120a6e
      return FALSE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    /*printf("error: bad file format\n");*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int vpb_get_info(FILE *file, int *xsize, int *ysize, int *imgoffs) {
Shinya Kitaoka 120a6e
  UCHAR buf[1024];
Shinya Kitaoka 120a6e
  unsigned int len;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  *xsize = *ysize = *imgoffs = 0;
Shinya Kitaoka 120a6e
  while (fread(buf, 1, 3, file) == 3) {
Shinya Kitaoka 120a6e
    if (buf[1] > 1) return FALSE;
Shinya Kitaoka 120a6e
    if (buf[0] == 0x10) {
Shinya Kitaoka 120a6e
      len = 20;
Shinya Kitaoka 120a6e
      if (fread(buf, 1, len, file) != len) return FALSE;
Shinya Kitaoka 120a6e
      *xsize   = buf[4] << 8 | buf[5];
Shinya Kitaoka 120a6e
      *ysize   = buf[6] << 8 | buf[7];
Shinya Kitaoka 120a6e
      *imgoffs = buf[12] << 24 | buf[13] << 16 | buf[14] << 8 | buf[15];
Shinya Kitaoka 120a6e
      *imgoffs += 1024;
Shinya Kitaoka 120a6e
      return TRUE;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    len = buf[2];
Shinya Kitaoka 120a6e
    if (fread(buf, 1, len, file) != len) return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return FALSE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static int quantel_write_buffer(FILE *outf, UCHAR *buf, int ysize) {
Shinya Kitaoka 120a6e
  int n;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  n = fwrite(buf, 1, ysize * BYTESPERROW, outf);
Shinya Kitaoka 120a6e
  if (n <= 0) {
Shinya Kitaoka 120a6e
    /*printf("quantel_write_frame error: write failed\n");*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TRUE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static void quantel_rgb_to_yuv(USHORT *rp, USHORT *gp, USHORT *bp, UCHAR *ap) {
Shinya Kitaoka 120a6e
  int i, r, g, b;
Shinya Kitaoka 120a6e
  TINT32 y1, y2, u, v, u1, u2, v1, v2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  y1 = y2 = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ap -= BYTESPERROW - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = QUANTEL_XSIZE / 2; i > 0; i--) {
Shinya Kitaoka 120a6e
    /* first pixel gives Y and 0.5 of chroma */
Shinya Kitaoka 120a6e
    r = *rp++;
Shinya Kitaoka 120a6e
    g = *gp++;
Shinya Kitaoka 120a6e
    b = *bp++;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    y1 = 16829 * r + 33039 * g + 6416 * b;
Shinya Kitaoka 120a6e
    u1 = -4831 * r + -9488 * g + 14319 * b;
Shinya Kitaoka 120a6e
    v1 = 14322 * r + -11992 * g + -2330 * b;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* second pixel gives Y and 0.5 of chroma */
Shinya Kitaoka 120a6e
    r = *rp++;
Shinya Kitaoka 120a6e
    g = *gp++;
Shinya Kitaoka 120a6e
    b = *bp++;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    y2 = 16829 * r + 33039 * g + 6416 * b;
Shinya Kitaoka 120a6e
    u2 = -4831 * r + -9488 * g + 14319 * b;
Shinya Kitaoka 120a6e
    v2 = 14322 * r + -11992 * g + -2330 * b;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* average the chroma */
Shinya Kitaoka 120a6e
    u = u1 + u2;
Shinya Kitaoka 120a6e
    v = v1 + v2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* round the chroma */
Shinya Kitaoka 120a6e
    u1 = (u + 0x008000) >> 16;
Shinya Kitaoka 120a6e
    v1 = (v + 0x008000) >> 16;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* limit the chroma */
Shinya Kitaoka 120a6e
    if (u1 < -112) u1 = -112;
Shinya Kitaoka 120a6e
    if (u1 > 111) u1  = 111;
Shinya Kitaoka 120a6e
    if (v1 < -112) v1 = -112;
Shinya Kitaoka 120a6e
    if (v1 > 111) v1  = 111;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* limit the lum */
Shinya Kitaoka 120a6e
    if (y1 > 0x00dbffff) y1 = 0x00dbffff;
Shinya Kitaoka 120a6e
    if (y2 > 0x00dbffff) y2 = 0x00dbffff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /* save the results */
Shinya Kitaoka 120a6e
    *ap++ = (u1 + 128);
Shinya Kitaoka 120a6e
    *ap++ = (y1 >> 16) + 16;
Shinya Kitaoka 120a6e
    *ap++ = (v1 + 128);
Shinya Kitaoka 120a6e
    *ap++ = (y2 >> 16) + 16;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Shinya Kitaoka 120a6e
void img_read_quantel_info(const T_CHAR *fname, int *w, int *h, int type) {
Shinya Kitaoka 120a6e
  int xsize, ysize, imgoffs;
Shinya Kitaoka 120a6e
  FILE *file;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  *w = 0;
Shinya Kitaoka 120a6e
  *h = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (type == VPB_FORMAT) {
Shinya Kitaoka 120a6e
    file = _wfopen(fname, L"rb");
Shinya Kitaoka 120a6e
    if (!file) {
Shinya Kitaoka 120a6e
      /*printf("img_read_quantel_info error: unable to open file %s\n",
Shinya Kitaoka 120a6e
       * fname);*/
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (!vpb_get_info(file, &xsize, &ysize, &imgoffs)) {
Shinya Kitaoka 120a6e
      fclose(file);
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    fclose(file);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (!quantel_get_info(fname, type, &xsize, &ysize)) return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  *w = xsize;
Shinya Kitaoka 120a6e
  *h = ysize;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void *img_read_quantel(const T_CHAR *fname, int *w, int *h, int type) {
Shinya Kitaoka 120a6e
  FILE *fileyuv;
Shinya Kitaoka 120a6e
  IMAGE *image;
Shinya Kitaoka 120a6e
  LPIXEL *bufout1, *bufout2, *buf;
Shinya Kitaoka 120a6e
  int xsize, ysize, imgoffs, quantel_xsize;
Shinya Kitaoka 120a6e
  int y, exit = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fileyuv = _wfopen(fname, L"rb");
Shinya Kitaoka 120a6e
  if (fileyuv == NULL) {
Shinya Kitaoka 120a6e
    /*printf("img_read_quantel error: unable to open file %s\n", fname);*/
Shinya Kitaoka 120a6e
    return NIL;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (type == VPB_FORMAT) {
Shinya Kitaoka 120a6e
    if (!vpb_get_info(fileyuv, &xsize, &ysize, &imgoffs)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    fseek(fileyuv, imgoffs, SEEK_SET);
Shinya Kitaoka 120a6e
    quantel_xsize = xsize;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (!quantel_get_info(fname, type, &xsize, &ysize)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    quantel_xsize = QUANTEL_XSIZE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image       = new_img();
Shinya Kitaoka 120a6e
  image->type = TOONZRGB;
Shinya Kitaoka 120a6e
  if (!allocate_pixmap(image, xsize, ysize)) return NIL;
Shinya Kitaoka 120a6e
  bufout1 = bufout2 = (LPIXEL *)image->buffer;
Shinya Kitaoka 120a6e
  bufout1 += ysize * xsize - 1;
Shinya Kitaoka 120a6e
  bufout2 += (ysize - 1) * xsize - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (type == SDL_FORMAT) {
Shinya Kitaoka 120a6e
    for (y = 0; y < ysize; y += 2) {
Shinya Kitaoka 120a6e
      buf = bufout1 - (quantel_xsize - 1);
Shinya Kitaoka 120a6e
      QUANTEL_GET_YUV_LINE(fileyuv, buf, quantel_xsize)
Shinya Kitaoka 120a6e
      if (exit) break;
Shinya Kitaoka 120a6e
      bufout1 -= quantel_xsize * 2;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    for (y = 1; y < ysize; y += 2) {
Shinya Kitaoka 120a6e
      buf = bufout2 - (quantel_xsize - 1);
Shinya Kitaoka 120a6e
      QUANTEL_GET_YUV_LINE(fileyuv, buf, quantel_xsize)
Shinya Kitaoka 120a6e
      if (exit) break;
Shinya Kitaoka 120a6e
      bufout2 -= quantel_xsize * 2;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    for (y = 0; y < ysize; y++) {
Shinya Kitaoka 120a6e
      buf = bufout1 - (quantel_xsize - 1);
Shinya Kitaoka 120a6e
      QUANTEL_GET_YUV_LINE(fileyuv, buf, quantel_xsize)
Shinya Kitaoka 120a6e
      if (exit) break;
Shinya Kitaoka 120a6e
      bufout1 -= quantel_xsize;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (ferror(fileyuv) || feof(fileyuv)) {
Shinya Kitaoka 120a6e
    fclose(fileyuv);
Shinya Kitaoka 120a6e
    return NIL;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fclose(fileyuv);
Shinya Kitaoka 120a6e
  *w = image->xsize;
Shinya Kitaoka 120a6e
  *h = image->ysize;
Shinya Kitaoka 120a6e
  return image->buffer;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int img_write_quantel(const T_CHAR *fname, void *buffer, int w, int h,
Shinya Kitaoka 120a6e
                      int type) {
Shinya Kitaoka 120a6e
  FILE *outf;
Shinya Kitaoka 120a6e
  UCHAR *picbuf, *ap;
Shinya Kitaoka 120a6e
  USHORT rbuffer[8192], gbuffer[8192], bbuffer[8192];
Shinya Kitaoka 120a6e
  USHORT *rbuf, *gbuf, *bbuf;
Shinya Kitaoka 120a6e
  int y, yuv_flag = 0;
Shinya Kitaoka 120a6e
  int xmarg, ymarg;
Shinya Kitaoka 120a6e
  LPIXEL *RGBbuf, *appo;
Shinya Kitaoka 120a6e
  int xsize, true_ysize, max_ysize = 0, ysize, ret, interlace = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rbuf = (USHORT *)&rbuffer;
Shinya Kitaoka 120a6e
  gbuf = (USHORT *)&gbuffer;
Shinya Kitaoka 120a6e
  bbuf = (USHORT *)&bbuffer;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xsize = w;
Shinya Kitaoka 120a6e
  ysize = h;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (xsize > QUANTEL_XSIZE) {
Shinya Kitaoka 120a6e
    /* printf("error: bad X size (%s)\n", Quantel_error[type-1]);*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  } else if (xsize < QUANTEL_XSIZE)
Shinya Kitaoka 120a6e
    xmarg = (QUANTEL_XSIZE - xsize) / 2;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    xmarg = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (type) {
Shinya Kitaoka 120a6e
  case QNT_FORMAT:
Shinya Kitaoka 120a6e
    interlace = FALSE;
Shinya Kitaoka 120a6e
    yuv_flag  = 0;
Shinya Kitaoka 120a6e
    max_ysize = QNT_PAL_YSIZE;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case QTL_FORMAT:
Shinya Kitaoka 120a6e
    interlace = FALSE;
Shinya Kitaoka 120a6e
    yuv_flag  = 0;
Shinya Kitaoka 120a6e
    max_ysize = QTL_NTSC_YSIZE;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case YUV_FORMAT:
Shinya Kitaoka 120a6e
  case VPB_FORMAT:
Shinya Kitaoka 120a6e
    interlace = FALSE;
Shinya Kitaoka 120a6e
    yuv_flag  = 0;
Shinya Kitaoka 120a6e
    max_ysize = QUANTEL_GET_YSIZE(ysize);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case SDL_FORMAT:
Shinya Kitaoka 120a6e
    interlace = TRUE;
Shinya Kitaoka 120a6e
    yuv_flag  = 0;
Shinya Kitaoka 120a6e
    max_ysize = QUANTEL_GET_YSIZE(ysize);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    /*printf("error: %d bad file format\n", fname);*/
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ysize > max_ysize) {
Shinya Kitaoka 120a6e
    /*printf("error: bad Y size (%s)\n", Quantel_error[type-1]);*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  } else if (!yuv_flag && ysize < max_ysize) {
Shinya Kitaoka 120a6e
    ymarg      = (max_ysize - ysize) / 2;
Shinya Kitaoka 120a6e
    true_ysize = max_ysize;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    ymarg      = 0;
Shinya Kitaoka 120a6e
    true_ysize = ysize;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  outf = _wfopen(fname, L"wb");
Shinya Kitaoka 120a6e
  if (outf == NULL) {
Shinya Kitaoka 120a6e
    /*printf("error: unable to open %s for writing\n",  fname);*/
Shinya Kitaoka 120a6e
    return FALSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (type == VPB_FORMAT) {
Shinya Kitaoka 120a6e
    write_vpb_header(outf, QUANTEL_XSIZE, true_ysize);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  picbuf = (UCHAR *)malloc(true_ysize * QUANTEL_XSIZE * sizeof(short));
Shinya Kitaoka 120a6e
  if (picbuf == NIL) {
Shinya Kitaoka 120a6e
    /*printf("img_write_quantel error: out of memory\n");*/
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ap = picbuf + (true_ysize * QUANTEL_XSIZE * sizeof(short)) - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RGBbuf = (LPIXEL *)buffer;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (interlace) {
Shinya Kitaoka 120a6e
    appo = RGBbuf;
Shinya Kitaoka 120a6e
    for (y = 0; y < true_ysize; y += 2) {
Shinya Kitaoka 120a6e
      if (y < ymarg || y > (true_ysize - ymarg - 1))
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_BLACK(rbuf, gbuf, bbuf, QUANTEL_XSIZE)
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_RGB(xmarg, xsize, rbuf, gbuf, bbuf, appo)
Shinya Kitaoka 120a6e
        appo += xsize;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      quantel_rgb_to_yuv(rbuf, gbuf, bbuf, ap);
Shinya Kitaoka 120a6e
      ap -= BYTESPERROW;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    appo = RGBbuf + xsize;
Shinya Kitaoka 120a6e
    for (y = 1; y < true_ysize; y += 2) {
Shinya Kitaoka 120a6e
      if (y < ymarg || y > (true_ysize - ymarg - 1))
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_BLACK(rbuf, gbuf, bbuf, QUANTEL_XSIZE)
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_RGB(xmarg, xsize, rbuf, gbuf, bbuf, appo)
Shinya Kitaoka 120a6e
        appo += xsize;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      quantel_rgb_to_yuv(rbuf, gbuf, bbuf, ap);
Shinya Kitaoka 120a6e
      ap -= BYTESPERROW;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    for (y = 0; y < true_ysize; y++) {
Shinya Kitaoka 120a6e
      if (y < ymarg || y > (true_ysize - ymarg - 1))
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_BLACK(rbuf, gbuf, bbuf, QUANTEL_XSIZE)
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        QUANTEL_FILL_LINE_OF_RGB(xmarg, xsize, rbuf, gbuf, bbuf, RGBbuf)
Shinya Kitaoka 120a6e
      quantel_rgb_to_yuv(rbuf, gbuf, bbuf, ap);
Shinya Kitaoka 120a6e
      ap -= BYTESPERROW;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ret = quantel_write_buffer(outf, picbuf, true_ysize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (picbuf) free(picbuf);
Shinya Kitaoka 120a6e
  fclose(outf);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return ret; /*  ??  */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static IMAGE *img_read_region_quantel(T_CHAR *fname, int x1, int y1, int x2,
shun-iwasawa 27b0cf
                                      int y2, int scale, int type) {
Shinya Kitaoka 120a6e
  if (type == SDL_FORMAT)
Shinya Kitaoka 120a6e
    return (img_read_region_quantel_no_interlaced(fname, x1, y1, x2, y2, scale,
Shinya Kitaoka 120a6e
                                                  type));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return (
Shinya Kitaoka 120a6e
        img_read_region_quantel_interlaced(fname, x1, y1, x2, y2, scale, type));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static IMAGE *img_read_region_quantel_no_interlaced(T_CHAR *fname, int x1,
Shinya Kitaoka 120a6e
                                                    int y1, int x2, int y2,
Shinya Kitaoka 120a6e
                                                    int scale, int type) {
Shinya Kitaoka 120a6e
  int offset, xsize, ysize, imgoffs;
Shinya Kitaoka 120a6e
  int rows_field1, rows_field2, ret, step, off_row;
Shinya Kitaoka 120a6e
  LPIXEL *bufout1, *bufout2, *buff;
Shinya Kitaoka 120a6e
  TINT32 r, g, b, j, k, u, v, x, y;
Shinya Kitaoka 120a6e
  unsigned char *linebuf, *head_linebuf = NULL;
Shinya Kitaoka 120a6e
  INFO_REGION region;
Shinya Kitaoka 120a6e
  IMAGE *image = NULL;
Shinya Kitaoka 120a6e
  FILE *fileyuv;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fileyuv = _wfopen(fname, L"rb");
Shinya Kitaoka 120a6e
  if (!fileyuv) return NIL;
Shinya Kitaoka 120a6e
  if (type == VPB_FORMAT) {
Shinya Kitaoka 120a6e
    if (!vpb_get_info(fileyuv, &xsize, &ysize, &imgoffs)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (!quantel_get_info(fname, type, &xsize, &ysize)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    imgoffs = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image       = new_img();
Shinya Kitaoka 120a6e
  image->type = TOONZRGB;
Shinya Kitaoka 120a6e
  getInfoRegion(®ion, x1, y1, x2, y2, scale, xsize, ysize);
Shinya Kitaoka 120a6e
  if (!allocate_pixmap(image, region.xsize, region.ysize)) return NIL;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bufout1 = bufout2 = (LPIXEL *)image->buffer;
Shinya Kitaoka 120a6e
  bufout1 += (region.ysize - 1) * region.xsize + region.x_offset;
Shinya Kitaoka 120a6e
  bufout2 += (region.ysize - 2) * region.xsize + region.x_offset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  linebuf = (unsigned char *)calloc(1, BYTESPERROW);
Shinya Kitaoka 120a6e
  if (!linebuf) {
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
printf("error: out of memory\n");
Shinya Kitaoka 120a6e
exit(1);
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (region.scanNrow & 0x1) /* se dispare */
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    rows_field1 = region.scanNrow / 2 + 1;
Shinya Kitaoka 120a6e
    rows_field2 = region.scanNrow / 2;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    rows_field1 = rows_field2 = region.scanNrow / 2;
Shinya Kitaoka 120a6e
  offset = (region.ly_in - (region.scanNrow * scale + max(y1, 0))) / 2;
Shinya Kitaoka 120a6e
  offset *= BYTESPERROW;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fseek(fileyuv, imgoffs + offset, SEEK_SET)) {
Shinya Kitaoka 120a6e
    /* printf("error: seek failed; inconsistent data\n");*/
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (y = 0; y < rows_field1; y++) {
Shinya Kitaoka 120a6e
    ret = fread(linebuf, 1, BYTESPERROW, fileyuv);
Shinya Kitaoka 120a6e
    if (!ret) goto end;
Shinya Kitaoka 120a6e
    head_linebuf = linebuf;
Shinya Kitaoka 120a6e
    off_row      = region.startScanCol * sizeof(TUINT32) / 2;
Shinya Kitaoka 120a6e
    if (off_row & 3) off_row += 2;
Shinya Kitaoka 120a6e
    linebuf += off_row;
Shinya Kitaoka 120a6e
    buff = bufout1;
Shinya Kitaoka 120a6e
    for (x = 0; x < region.scanNcol / 2; x++) {
Shinya Kitaoka 120a6e
      u                   = *linebuf++ - 128;
Shinya Kitaoka 120a6e
      j                   = *linebuf++ - 16;
Shinya Kitaoka 120a6e
      if (j < 0) j        = 0;
Shinya Kitaoka 120a6e
      v                   = *linebuf++ - 128;
Shinya Kitaoka 120a6e
      k                   = *linebuf++ - 16;
Shinya Kitaoka 120a6e
      r                   = 76310 * j + 104635 * v;
Shinya Kitaoka 120a6e
      if (r > 0xffffff) r = 0xffffff;
Shinya Kitaoka 120a6e
      if (r <= 0xffff) r  = 0;
Shinya Kitaoka 120a6e
      g                   = 76310 * j + -25690 * u + -53294 * v;
Shinya Kitaoka 120a6e
      if (g > 0xffffff) g = 0xffffff;
Shinya Kitaoka 120a6e
      if (g <= 0xffff) g  = 0;
Shinya Kitaoka 120a6e
      b                   = 76310 * j + 132278 * u;
Shinya Kitaoka 120a6e
      if (b > 0xffffff) b = 0xffffff;
Shinya Kitaoka 120a6e
      if (b <= 0xffff) b  = 0;
Shinya Kitaoka 120a6e
      buff->r             = (unsigned char)(r >> 16);
Shinya Kitaoka 120a6e
      buff->g             = (unsigned char)(g >> 16);
Shinya Kitaoka 120a6e
      buff->b             = (unsigned char)(b >> 16);
Shinya Kitaoka 120a6e
      buff->m             = 255;
Shinya Kitaoka 120a6e
      buff++;
Shinya Kitaoka 120a6e
      r                   = 76310 * k + 104635 * v;
Shinya Kitaoka 120a6e
      if (r > 0xffffff) r = 0xffffff;
Shinya Kitaoka 120a6e
      if (r <= 0xffff) r  = 0;
Shinya Kitaoka 120a6e
      g                   = 76310 * k + -25690 * u + -53294 * v;
Shinya Kitaoka 120a6e
      if (g > 0xffffff) g = 0xffffff;
Shinya Kitaoka 120a6e
      if (g <= 0xffff) g  = 0;
Shinya Kitaoka 120a6e
      b                   = 76310 * k + 132278 * u;
Shinya Kitaoka 120a6e
      if (b > 0xffffff) b = 0xffffff;
Shinya Kitaoka 120a6e
      if (b <= 0xffff) b  = 0;
Shinya Kitaoka 120a6e
      buff->r             = (unsigned char)(r >> 16);
Shinya Kitaoka 120a6e
      buff->g             = (unsigned char)(g >> 16);
Shinya Kitaoka 120a6e
      buff->b             = (unsigned char)(b >> 16);
Shinya Kitaoka 120a6e
      buff->m             = 255;
Shinya Kitaoka 120a6e
      buff++;
Shinya Kitaoka 120a6e
      linebuf += (region.step - 1) * sizeof(TUINT32);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    linebuf = head_linebuf;
Shinya Kitaoka 120a6e
    if (y * 2 > region.ly_in) break;
Shinya Kitaoka 120a6e
    for (step = 1; step < region.step; step++) {
Shinya Kitaoka 120a6e
      if (fseek(fileyuv, BYTESPERROW, SEEK_CUR)) {
Shinya Kitaoka 120a6e
        printf("error: seek failed; inconsistent data\n");
Shinya Kitaoka 120a6e
        goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    bufout1 -= region.xsize * 2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  offset = (region.ly_in - (region.scanNrow * scale + max(y1, 0))) / 2;
Shinya Kitaoka 120a6e
  offset = (region.ly_in / 2 + offset);
Shinya Kitaoka 120a6e
  offset *= BYTESPERROW;
Shinya Kitaoka 120a6e
  if (fseek(fileyuv, imgoffs + offset, SEEK_SET)) {
Shinya Kitaoka 120a6e
    /*printf("error: seek failed; inconsistent data\n");*/
Shinya Kitaoka 120a6e
    goto error;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (y = 0; y < rows_field2; y++) {
Shinya Kitaoka 120a6e
    ret = fread(linebuf, 1, BYTESPERROW, fileyuv);
Shinya Kitaoka 120a6e
    if (!ret) goto end;
Shinya Kitaoka 120a6e
    head_linebuf = linebuf;
Shinya Kitaoka 120a6e
    off_row      = region.startScanCol * sizeof(TUINT32) / 2;
Shinya Kitaoka 120a6e
    if (off_row & 3) off_row += 2;
Shinya Kitaoka 120a6e
    linebuf += off_row;
Shinya Kitaoka 120a6e
    buff = bufout2;
Shinya Kitaoka 120a6e
    for (x = 0; x < region.scanNcol / 2; x++) {
Shinya Kitaoka 120a6e
      u                   = *linebuf++ - 128;
Shinya Kitaoka 120a6e
      j                   = *linebuf++ - 16;
Shinya Kitaoka 120a6e
      if (j < 0) j        = 0;
Shinya Kitaoka 120a6e
      v                   = *linebuf++ - 128;
Shinya Kitaoka 120a6e
      k                   = *linebuf++ - 16;
Shinya Kitaoka 120a6e
      r                   = 76310 * j + 104635 * v;
Shinya Kitaoka 120a6e
      if (r > 0xffffff) r = 0xffffff;
Shinya Kitaoka 120a6e
      if (r <= 0xffff) r  = 0;
Shinya Kitaoka 120a6e
      g                   = 76310 * j + -25690 * u + -53294 * v;
Shinya Kitaoka 120a6e
      if (g > 0xffffff) g = 0xffffff;
Shinya Kitaoka 120a6e
      if (g <= 0xffff) g  = 0;
Shinya Kitaoka 120a6e
      b                   = 76310 * j + 132278 * u;
Shinya Kitaoka 120a6e
      if (b > 0xffffff) b = 0xffffff;
Shinya Kitaoka 120a6e
      if (b <= 0xffff) b  = 0;
Shinya Kitaoka 120a6e
      buff->r             = (unsigned char)(r >> 16);
Shinya Kitaoka 120a6e
      buff->g             = (unsigned char)(g >> 16);
Shinya Kitaoka 120a6e
      buff->b             = (unsigned char)(b >> 16);
Shinya Kitaoka 120a6e
      buff->m             = 255;
Shinya Kitaoka 120a6e
      buff++;
Shinya Kitaoka 120a6e
      r                   = 76310 * k + 104635 * v;
Shinya Kitaoka 120a6e
      if (r > 0xffffff) r = 0xffffff;
Shinya Kitaoka 120a6e
      if (r <= 0xffff) r  = 0;
Shinya Kitaoka 120a6e
      g                   = 76310 * k + -25690 * u + -53294 * v;
Shinya Kitaoka 120a6e
      if (g > 0xffffff) g = 0xffffff;
Shinya Kitaoka 120a6e
      if (g <= 0xffff) g  = 0;
Shinya Kitaoka 120a6e
      b                   = 76310 * k + 132278 * u;
Shinya Kitaoka 120a6e
      if (b > 0xffffff) b = 0xffffff;
Shinya Kitaoka 120a6e
      if (b <= 0xffff) b  = 0;
Shinya Kitaoka 120a6e
      buff->r             = (unsigned char)(r >> 16);
Shinya Kitaoka 120a6e
      buff->g             = (unsigned char)(g >> 16);
Shinya Kitaoka 120a6e
      buff->b             = (unsigned char)(b >> 16);
Shinya Kitaoka 120a6e
      buff->m             = 255;
Shinya Kitaoka 120a6e
      buff++;
Shinya Kitaoka 120a6e
      linebuf += (region.step - 1) * sizeof(TUINT32);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    linebuf = head_linebuf;
Shinya Kitaoka 120a6e
    if (y * 2 + 1 > region.ly_in) break;
Shinya Kitaoka 120a6e
    for (step = 1; step < region.step; step++) {
Shinya Kitaoka 120a6e
      if (fseek(fileyuv, BYTESPERROW, SEEK_CUR)) {
Shinya Kitaoka 120a6e
        printf("error: seek failed; inconsistent data\n");
Shinya Kitaoka 120a6e
        goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    bufout2 -= region.xsize * 2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ferror(fileyuv) || feof(fileyuv)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (head_linebuf) free(head_linebuf);
Shinya Kitaoka 120a6e
  fclose(fileyuv);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (head_linebuf) free(head_linebuf);
Shinya Kitaoka 120a6e
  free_img(image);
Shinya Kitaoka 120a6e
  fclose(fileyuv);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static IMAGE *img_read_region_quantel_interlaced(T_CHAR *fname, int x1, int y1,
Shinya Kitaoka 120a6e
                                                 int x2, int y2, int scale,
Shinya Kitaoka 120a6e
                                                 int type) {
Shinya Kitaoka 120a6e
  int offset, rgbbuf_offset, xsize, ysize, imgoffs, ret, step, x, y;
Shinya Kitaoka 120a6e
  LPIXEL *bufRGB, *headBufRGB;
Shinya Kitaoka 120a6e
  unsigned char *headLineBuf, *lineBuf;
Shinya Kitaoka 120a6e
  TINT32 r, g, b, j, k, u, v;
Shinya Kitaoka 120a6e
  INFO_REGION region;
Shinya Kitaoka 120a6e
  IMAGE *image = NULL;
Shinya Kitaoka 120a6e
  FILE *fileyuv;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fileyuv = _wfopen(fname, L"rb");
Shinya Kitaoka 120a6e
  if (!fileyuv) return NIL;
Shinya Kitaoka 120a6e
  if (type == VPB_FORMAT) {
Shinya Kitaoka 120a6e
    if (!vpb_get_info(fileyuv, &xsize, &ysize, &imgoffs)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (!quantel_get_info(fname, type, &xsize, &ysize)) {
Shinya Kitaoka 120a6e
      fclose(fileyuv);
Shinya Kitaoka 120a6e
      return NIL;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    imgoffs = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  image       = new_img();
Shinya Kitaoka 120a6e
  image->type = TOONZRGB;
Shinya Kitaoka 120a6e
  getInfoRegion(®ion, x1, y1, x2, y2, scale, xsize, ysize);
Shinya Kitaoka 120a6e
  if (!allocate_pixmap(image, region.xsize, region.ysize)) return NIL;
Shinya Kitaoka 120a6e
  rgbbuf_offset = (region.x_offset + (region.xsize * region.y_offset));
Shinya Kitaoka 120a6e
  headBufRGB    = (LPIXEL *)image->buffer;
Shinya Kitaoka 120a6e
  headLineBuf = lineBuf = (unsigned char *)calloc(1, BYTESPERROW);
Shinya Kitaoka 120a6e
  if (!headLineBuf) {
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (y2 < ysize - 1)
Shinya Kitaoka 120a6e
    offset = (ysize - 1 - y2) * BYTESPERROW;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    offset = 0;
Shinya Kitaoka 120a6e
  fseek(fileyuv, imgoffs + offset, SEEK_SET);
Shinya Kitaoka 120a6e
  for (y = 0; y < region.scanNrow; y++) {
Shinya Kitaoka 120a6e
    bufRGB =
Shinya Kitaoka 120a6e
        headBufRGB + rgbbuf_offset + (region.xsize * (region.scanNrow - y - 1));
Shinya Kitaoka 120a6e
    ret = fread(lineBuf, 1, BYTESPERROW, fileyuv);
Shinya Kitaoka 120a6e
    if (!ret) goto end;
Shinya Kitaoka 120a6e
    lineBuf += region.startScanCol * sizeof(LPIXEL);
Shinya Kitaoka 120a6e
    QUANTEL_BUILD_ROW; /* sta in testa al file */
Shinya Kitaoka 120a6e
    if (y >= region.scanNrow - 1) break;
Shinya Kitaoka 120a6e
    for (step = 1; step < region.step; step++) {
Shinya Kitaoka 120a6e
      if (fseek(fileyuv, BYTESPERROW, SEEK_CUR)) {
Shinya Kitaoka 120a6e
        /*printf("error: seek failed; inconsistent data\n");*/
Shinya Kitaoka 120a6e
        goto error;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    lineBuf = headLineBuf;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ferror(fileyuv) || feof(fileyuv)) goto error;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
end:
Shinya Kitaoka 120a6e
  if (headLineBuf) free(headLineBuf);
Shinya Kitaoka 120a6e
  fclose(fileyuv);
Shinya Kitaoka 120a6e
  return image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
error:
Shinya Kitaoka 120a6e
  if (headLineBuf) free(headLineBuf);
Shinya Kitaoka 120a6e
  free_img(image);
Shinya Kitaoka 120a6e
  fclose(fileyuv);
Shinya Kitaoka 120a6e
  return NIL;
Toshihiro Shimizu 890ddd
}