Frage

I'm using Aforge.net HueModifier and SaturationCorrection filters to change image color. My problem is how to set Luminance alongside these filters to get the desired color?

War es hilfreich?

Lösung

I was able to get this issue solved by writing a custom Aforge.net filter. The filter takes HSL color value and apply color tint to the provided image.

protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
    {
        int pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat)/8;

        int startX = rect.Left;
        int startY = rect.Top;
        int stopX = startX + rect.Width;
        int stopY = startY + rect.Height;
        int offset = image.Stride - rect.Width*pixelSize;

        var rgb = new RGB();
        var hsl = new HSL();

        // do the job
        byte* ptr = (byte*) image.ImageData.ToPointer();

        // allign pointer to the first pixel to process
        ptr += (startY*image.Stride + startX*pixelSize);

        // for each row
        for (int y = startY; y < stopY; y++)
        {
            // for each pixel
            for (int x = startX; x < stopX; x++, ptr += pixelSize)
            {
                rgb.Red = ptr[RGB.R];
                rgb.Green = ptr[RGB.G];
                rgb.Blue = ptr[RGB.B];

                // convert to HSL
                HSL.FromRGB(rgb, hsl);

                // modify hsl values
                hsl.Hue = hue;
                hsl.Saturation = saturation;
                hsl.Luminance = Math.Min(0.97f, hsl.Luminance * (120 * luminance / 65));

                // convert back to RGB
                HSL.ToRGB(hsl, rgb);

                ptr[RGB.R] = (byte)rgb.Red;
                ptr[RGB.G] = (byte)rgb.Green;
                ptr[RGB.B] = (byte)rgb.Blue;
            }
            ptr += offset;
        }

    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top