Skip to content

Commit

Permalink
Fix Lab TIFF
Browse files Browse the repository at this point in the history
Fix handling of CIELab TIFF by tificc
  • Loading branch information
mm2 committed Nov 7, 2023
1 parent d2c7f43 commit d208524
Showing 1 changed file with 77 additions and 22 deletions.
99 changes: 77 additions & 22 deletions utils/tificc/tificc.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ int FromLabV4ToLabV2(int x)
// Formatter for 8bit Lab TIFF (photometric 8)
static
unsigned char* UnrollTIFFLab8(struct _cmstransform_struct* CMMcargo,
register cmsUInt16Number wIn[],
register cmsUInt8Number* accum,
register cmsUInt32Number Stride)
CMSREGISTER cmsUInt16Number wIn[],
CMSREGISTER cmsUInt8Number* accum,
CMSREGISTER cmsUInt32Number Stride)
{
wIn[0] = (cmsUInt16Number) FromLabV2ToLabV4((accum[0]) << 8);
wIn[1] = (cmsUInt16Number) FromLabV2ToLabV4(((accum[1] > 127) ? (accum[1] - 128) : (accum[1] + 128)) << 8);
Expand All @@ -165,9 +165,9 @@ unsigned char* UnrollTIFFLab8(struct _cmstransform_struct* CMMcargo,
// Formatter for 16bit Lab TIFF (photometric 8)
static
unsigned char* UnrollTIFFLab16(struct _cmstransform_struct* CMMcargo,
register cmsUInt16Number wIn[],
register cmsUInt8Number* accum,
register cmsUInt32Number Stride )
CMSREGISTER cmsUInt16Number wIn[],
CMSREGISTER cmsUInt8Number* accum,
CMSREGISTER cmsUInt32Number Stride )
{
cmsUInt16Number* accum16 = (cmsUInt16Number*) accum;

Expand All @@ -184,9 +184,9 @@ unsigned char* UnrollTIFFLab16(struct _cmstransform_struct* CMMcargo,

static
unsigned char* PackTIFFLab8(struct _cmstransform_struct* CMMcargo,
register cmsUInt16Number wOut[],
register cmsUInt8Number* output,
register cmsUInt32Number Stride)
CMSREGISTER cmsUInt16Number wOut[],
CMSREGISTER cmsUInt8Number* output,
CMSREGISTER cmsUInt32Number Stride)
{
int a, b;

Expand All @@ -197,18 +197,44 @@ unsigned char* PackTIFFLab8(struct _cmstransform_struct* CMMcargo,

*output++ = (cmsUInt8Number) ((a < 128) ? (a + 128) : (a - 128));
*output++ = (cmsUInt8Number) ((b < 128) ? (b + 128) : (b - 128));

return output;

UTILS_UNUSED_PARAMETER(Stride);
UTILS_UNUSED_PARAMETER(CMMcargo);
}


static
unsigned char* PackTIFFLabA8(struct _cmstransform_struct* CMMcargo,
CMSREGISTER cmsUInt16Number wOut[],
CMSREGISTER cmsUInt8Number* output,
CMSREGISTER cmsUInt32Number Stride)
{
int a, b;

*output++ = (cmsUInt8Number) (FromLabV4ToLabV2(wOut[0] + 0x0080) >> 8);

a = (FromLabV4ToLabV2(wOut[1]) + 0x0080) >> 8;
b = (FromLabV4ToLabV2(wOut[2]) + 0x0080) >> 8;

*output++ = (cmsUInt8Number) ((a < 128) ? (a + 128) : (a - 128));
*output++ = (cmsUInt8Number) ((b < 128) ? (b + 128) : (b - 128));

output++; // Alpha

return output;

UTILS_UNUSED_PARAMETER(Stride);
UTILS_UNUSED_PARAMETER(CMMcargo);
}


static
unsigned char* PackTIFFLab16(struct _cmstransform_struct* CMMcargo,
register cmsUInt16Number wOut[],
register cmsUInt8Number* output,
register cmsUInt32Number Stride)
CMSREGISTER cmsUInt16Number wOut[],
CMSREGISTER cmsUInt8Number* output,
CMSREGISTER cmsUInt32Number Stride)
{
int a, b;
cmsUInt16Number* output16 = (cmsUInt16Number*) output;
Expand All @@ -220,6 +246,31 @@ unsigned char* PackTIFFLab16(struct _cmstransform_struct* CMMcargo,

*output16++ = (cmsUInt16Number) ((a < 0x7f00) ? (a + 0x8000) : (a - 0x8000));
*output16++ = (cmsUInt16Number) ((b < 0x7f00) ? (b + 0x8000) : (b - 0x8000));

return (cmsUInt8Number*) output16;

UTILS_UNUSED_PARAMETER(Stride);
UTILS_UNUSED_PARAMETER(CMMcargo);
}

static
unsigned char* PackTIFFLabA16(struct _cmstransform_struct* CMMcargo,
CMSREGISTER cmsUInt16Number wOut[],
CMSREGISTER cmsUInt8Number* output,
CMSREGISTER cmsUInt32Number Stride)
{
int a, b;
cmsUInt16Number* output16 = (cmsUInt16Number*) output;

*output16++ = (cmsUInt16Number) FromLabV4ToLabV2(wOut[0]);

a = FromLabV4ToLabV2(wOut[1]);
b = FromLabV4ToLabV2(wOut[2]);

*output16++ = (cmsUInt16Number) ((a < 0x7f00) ? (a + 0x8000) : (a - 0x8000));
*output16++ = (cmsUInt16Number) ((b < 0x7f00) ? (b + 0x8000) : (b - 0x8000));

output16++; // Alpha

return (cmsUInt8Number*) output16;

Expand All @@ -244,7 +295,13 @@ cmsFormatter TiffFormatterFactory(cmsUInt32Number Type,
Result.Fmt16 = (bps == 1) ? UnrollTIFFLab8 : UnrollTIFFLab16;
}
else
Result.Fmt16 = (bps == 1) ? PackTIFFLab8 : PackTIFFLab16;
{
if (T_EXTRA(Type) == 1)
Result.Fmt16 = (bps == 1) ? PackTIFFLabA8 : PackTIFFLabA16;
else
if (T_EXTRA(Type) == 0)
Result.Fmt16 = (bps == 1) ? PackTIFFLab8 : PackTIFFLab16;
}
}

return Result;
Expand Down Expand Up @@ -349,7 +406,7 @@ cmsUInt32Number GetInputPixelType(TIFF *Bank)

// Two Lab flavours
case PHOTOMETRIC_ICCLAB:
pt = PT_LabV2;
pt = PT_Lab;
break;

case PHOTOMETRIC_CIELAB:
Expand Down Expand Up @@ -587,7 +644,7 @@ void WriteOutputTags(TIFF* out, int Colorspace, int BytesPerSample, int AlphaCha

case PT_Lab:
if (BitsPerSample == 16)
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_ICCLAB);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
else
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
break;
Expand Down Expand Up @@ -891,7 +948,7 @@ int TransformImage(TIFF* in, TIFF* out, const char *cDefInpProf)

// Assure both, input profile and input TIFF are on same colorspace
if (_cmsLCMScolorSpace(cmsGetColorSpace(hIn)) != (int) T_COLORSPACE(wInput))
FatalError("Input profile is not operating in proper color space");
FatalError("Input profile is not operating in proper color space (%d)", T_COLORSPACE(wInput));


if (!lIsDeviceLink)
Expand Down Expand Up @@ -976,6 +1033,10 @@ void Help(int level)
{
UTILS_UNUSED_PARAMETER(level);

fprintf(stderr, "Little CMS ICC profile applier for TIFF - v8.0 [LittleCMS %2.2f]\n", cmsGetEncodedCMMversion() / 1000.0);
fprintf(stderr, "Copyright (c) 1998-2023 Marti Maria Saguer. See COPYING file for details.\n");
fflush(stderr);

fprintf(stderr, "usage: tificc [flags] input.tif output.tif\n");

fprintf(stderr, "\nflags:\n\n");
Expand Down Expand Up @@ -1035,7 +1096,6 @@ void Help(int level)
"For suggestions, comments, bug reports etc. send mail to\n"
"[email protected]\n\n");


exit(0);
}

Expand Down Expand Up @@ -1188,11 +1248,6 @@ int main(int argc, char* argv[])
{
TIFF *in, *out;


fprintf(stderr, "Little CMS ICC profile applier for TIFF - v7.2 [LittleCMS %2.2f]\n\n", cmsGetEncodedCMMversion() / 1000.0);
fprintf(stderr, "Copyright (c) 1998-2023 Marti Maria Saguer. See COPYING file for details.\n");
fflush(stderr);

cmsPlugin(&TiffLabPlugin);

InitUtils("tificc");
Expand Down

0 comments on commit d208524

Please sign in to comment.