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