SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.45k stars 853 forks source link

Conversion from RgbaVector Incorrect #2048

Closed PeteMichaud closed 2 years ago

PeteMichaud commented 2 years ago

Prerequisites

ImageSharp version

2.0.0

Other ImageSharp packages and versions

none

Environment (Operating system, version and so on)

win10 pro (Version 10.0.19043 Build 19043)

.NET Framework version

4.7.2

Description

I might be misunderstanding what's supposed to happen, but I think this is a bug:

var sourceImg = GetImage<RgbaVector>();

using (var clonedImg = sourceImg.CloneAs<HalfVector4>()) //<- should load the equivalent color values in the new HalfVector4 format?
{
    Rgba32 srcColor = new Rgba32();
    Rgba32 convertedColor = new Rgba32();

    sourceImg[0, 0].ToRgba32(ref srcColor);
    clonedImg[0, 0].ToRgba32(ref convertedColor);

    Console.WriteLine($"{sourceImg[0, 0]} -> {srcColor}");
    Console.WriteLine($"{clonedImg[0, 0]} -> {convertedColor}");
}

Outputs:

RgbaVector(0.04, 0.04, 0.2, 1) -> Rgba32(10, 10, 51, 255)
HalfVector4(0.04, 0.04, 0.2, 1) -> Rgba32(132, 132, 153, 255)

Am I supposed to manually convert from the 32 bit RgbaVector values to the 16 bit HalfVector4?

Steps to Reproduce

var sourceImg = GetImage<RgbaVector>();

using (var clonedImg = sourceImg.CloneAs<HalfVector4>()) //<- should load the equivalent color values in the new HalfVector4 format?
{
    Rgba32 srcColor = new Rgba32();
    Rgba32 convertedColor = new Rgba32();

    sourceImg[0, 0].ToRgba32(ref srcColor);
    clonedImg[0, 0].ToRgba32(ref convertedColor);

    Console.WriteLine($"{sourceImg[0, 0]} -> {srcColor}");
    Console.WriteLine($"{clonedImg[0, 0]} -> {convertedColor}");
}

Images

No response

JimBobSquarePants commented 2 years ago

Thanks for raising this @PeteMichaud !!

This looks like a very serious bug which will be affecting other pixels formats. We're not applying the PixelConversionModifiers.Scale modifier to the operation which means values are incorrect for pixel formats that don't match the original format scale.