mm2 / Little-CMS

A free, open source, CMM engine. It provides fast transforms between ICC profiles.
https://www.littlecms.com
MIT License
549 stars 174 forks source link

Fast float plugin causes incorrect results in sRGB to XYZ transform #390

Closed ajeb78 closed 1 year ago

ajeb78 commented 1 year ago

Version of lcms2: git commit b82ef0d5d3759e782518f12ea85567bb2eb9e3de System: Kubuntu 22.04

The test program below returns incorrect results with the fast float plugin enabled, but correct results with it disabled:

#include "lcms2.h"
#include "lcms2_fast_float.h"

#define TYPE_RGB_FLT_PLANAR (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(4)|PLANAR_SH(1))
#define TYPE_XYZ_FLT_PLANAR (FLOAT_SH(1)|COLORSPACE_SH(PT_XYZ)|CHANNELS_SH(3)|BYTES_SH(4)|PLANAR_SH(1))

int main()
{
    cmsPlugin(cmsFastFloatExtensions());

    float rgb[] = { 0.75f, 0.5f, 0.3f };
    float xyz[3];

    cmsHPROFILE srgb_icc = cmsCreate_sRGBProfile();
    cmsHPROFILE xyz_icc = cmsCreateXYZProfile();

    cmsHTRANSFORM workingtoxyz = cmsCreateTransform(srgb_icc,
TYPE_RGB_FLT_PLANAR, xyz_icc, TYPE_XYZ_FLT_PLANAR, INTENT_PERCEPTUAL, 0);

    cmsCloseProfile(xyz_icc);
    cmsCloseProfile(srgb_icc);

    cmsDoTransform(workingtoxyz, rgb, xyz, 1);

    printf("LCMS rgb2xyz: %f %f %f\n", xyz[0], xyz[1], xyz[2]);
    cmsDeleteTransform(workingtoxyz);

   return 0;
}

With fast float: $ gcc -o test_out test.c -llcms2 -llcms2_fast_float $ ./test_out   LCMS rgb2xyz: 0.160393 0.137081 0.040251

With the cmsPlugin line commented out: $ ./test_out LCMS rgb2xyz: 0.320747 0.274139 0.080336