#include "image.h"
static short read_short(FILE *fp);
int
ImageReadPIX(image_t *img,
FILE *fp,
int primary,
int secondary,
int saturation,
int hue,
const ib_t *lut)
{
short width,
height,
depth;
int count,
bpp,
x, y;
ib_t r, g, b;
ib_t *in,
*out,
*ptr;
width = read_short(fp);
height = read_short(fp);
read_short(fp);
read_short(fp);
depth = read_short(fp);
if (depth == 8)
img->colorspace = secondary;
else
img->colorspace = (primary == IMAGE_RGB_CMYK) ? IMAGE_RGB : primary;
img->xsize = width;
img->ysize = height;
ImageSetMaxTiles(img, 0);
in = malloc(img->xsize * (depth / 8));
bpp = ImageGetDepth(img);
out = malloc(img->xsize * bpp);
if (depth == 8)
{
for (count = 0, y = 0, g = 0; y < img->ysize; y ++)
{
if (img->colorspace == IMAGE_WHITE)
ptr = out;
else
ptr = in;
for (x = img->xsize; x > 0; x --, count --)
{
if (count == 0)
{
count = getc(fp);
g = getc(fp);
}
*ptr++ = g;
}
if (img->colorspace != IMAGE_WHITE)
switch (img->colorspace)
{
case IMAGE_RGB :
ImageWhiteToRGB(in, out, img->xsize);
break;
case IMAGE_BLACK :
ImageWhiteToBlack(in, out, img->xsize);
break;
case IMAGE_CMY :
ImageWhiteToCMY(in, out, img->xsize);
break;
case IMAGE_CMYK :
ImageWhiteToCMYK(in, out, img->xsize);
break;
}
if (lut)
ImageLut(out, img->xsize * bpp, lut);
ImagePutRow(img, 0, y, img->xsize, out);
}
}
else
{
for (count = 0, y = 0, r = 0, g = 0, b = 0; y < img->ysize; y ++)
{
if (img->colorspace == IMAGE_RGB)
ptr = out;
else
ptr = in;
for (x = img->xsize; x > 0; x --, count --)
{
if (count == 0)
{
count = getc(fp);
b = getc(fp);
g = getc(fp);
r = getc(fp);
}
*ptr++ = r;
*ptr++ = g;
*ptr++ = b;
}
if (img->colorspace == IMAGE_RGB)
{
if (saturation != 100 || hue != 0)
ImageRGBAdjust(out, img->xsize, saturation, hue);
}
else
{
if (saturation != 100 || hue != 0)
ImageRGBAdjust(in, img->xsize, saturation, hue);
switch (img->colorspace)
{
case IMAGE_WHITE :
ImageRGBToWhite(in, out, img->xsize);
break;
case IMAGE_BLACK :
ImageRGBToBlack(in, out, img->xsize);
break;
case IMAGE_CMY :
ImageRGBToCMY(in, out, img->xsize);
break;
case IMAGE_CMYK :
ImageRGBToCMYK(in, out, img->xsize);
break;
}
}
if (lut)
ImageLut(out, img->xsize * bpp, lut);
ImagePutRow(img, 0, y, img->xsize, out);
}
}
fclose(fp);
free(in);
free(out);
return (0);
}
static short
read_short(FILE *fp)
{
int ch;
ch = getc(fp);
return ((ch << 8) | getc(fp));
}