#include "image.h"
int
ImageReadPhotoCD(image_t *img,
FILE *fp,
int primary,
int secondary,
int saturation,
int hue,
const ib_t *lut)
{
int x, y;
int xdir,
xstart;
int bpp;
int pass;
int rotation;
int temp,
temp2,
cb, cr;
ib_t *in,
*iy,
*icb,
*icr,
*rgb,
*rgbptr,
*out;
(void)secondary;
fseek(fp, 72, SEEK_SET);
rotation = (getc(fp) & 63) != 8;
fseek(fp, 0x30000, SEEK_SET);
img->colorspace = (primary == IMAGE_RGB_CMYK) ? IMAGE_RGB : primary;
img->xppi = 128;
img->yppi = 128;
if (rotation)
{
img->xsize = 512;
img->ysize = 768;
}
else
{
img->xsize = 768;
img->ysize = 512;
}
ImageSetMaxTiles(img, 0);
bpp = ImageGetDepth(img);
in = malloc(768 * 3);
out = malloc(768 * bpp);
if (bpp > 1)
rgb = malloc(768 * 3);
else
rgb = NULL;
if (rotation)
{
xstart = 767 * bpp;
xdir = -2 * bpp;
}
else
{
xstart = 0;
xdir = 0;
}
for (y = 0; y < 512; y += 2)
{
if (fread(in, 1, 768 * 3, fp) < (768 * 3))
{
free(in);
free(out);
return (-1);
}
for (pass = 0, iy = in; pass < 2; pass ++)
{
if (bpp == 1)
{
if (primary == IMAGE_BLACK)
{
if (rotation)
{
for (rgbptr = out + xstart, x = 0; x < 768; x ++)
*rgbptr-- = 255 - *iy++;
if (lut)
ImageLut(out, 768, lut);
ImagePutCol(img, 511 - y - pass, 0, 768, out);
}
else
{
ImageWhiteToBlack(iy, out, 768);
if (lut)
ImageLut(out, 768, lut);
ImagePutRow(img, 0, y + pass, 768, out);
iy += 768;
}
}
else if (rotation)
{
for (rgbptr = out + xstart, x = 0; x < 768; x ++)
*rgbptr-- = 255 - *iy++;
if (lut)
ImageLut(out, 768, lut);
ImagePutCol(img, 511 - y - pass, 0, 768, out);
}
else
{
if (lut)
ImageLut(iy, 768, lut);
ImagePutRow(img, 0, y + pass, 768, iy);
iy += 768;
}
}
else
{
cb = cr = 0.0f;
for (x = 0, rgbptr = rgb + xstart, icb = in + 1536, icr = in + 1920;
x < 768;
x ++, iy ++, rgbptr += xdir)
{
if (!(x & 1))
{
cb = (float)(*icb - 156);
cr = (float)(*icr - 137);
}
temp = 92241 * (*iy);
temp2 = (temp + 86706 * cr) / 65536;
if (temp2 < 0)
*rgbptr++ = 0;
else if (temp2 > 255)
*rgbptr++ = 255;
else
*rgbptr++ = temp2;
temp2 = (temp - 25914 * cb - 44166 * cr) / 65536;
if (temp2 < 0)
*rgbptr++ = 0;
else if (temp2 > 255)
*rgbptr++ = 255;
else
*rgbptr++ = temp2;
temp2 = (temp + 133434 * cb) / 65536;
if (temp2 < 0)
*rgbptr++ = 0;
else if (temp2 > 255)
*rgbptr++ = 255;
else
*rgbptr++ = temp2;
if (x & 1)
{
icb ++;
icr ++;
}
}
if (saturation != 100 || hue != 0)
ImageRGBAdjust(rgb, 768, saturation, hue);
if (img->colorspace == IMAGE_RGB)
{
if (lut)
ImageLut(rgb, 768 * 3, lut);
if (rotation)
ImagePutCol(img, 511 - y - pass, 0, 768, rgb);
else
ImagePutRow(img, 0, y + pass, 768, rgb);
}
else
{
switch (img->colorspace)
{
case IMAGE_CMY :
ImageRGBToCMY(rgb, out, 768);
break;
case IMAGE_CMYK :
ImageRGBToCMYK(rgb, out, 768);
break;
}
if (lut)
ImageLut(out, 768 * bpp, lut);
if (rotation)
ImagePutCol(img, 511 - y - pass, 0, 768, out);
else
ImagePutRow(img, 0, y + pass, 768, out);
}
}
}
}
free(in);
free(out);
if (bpp > 1)
free(rgb);
return (0);
}