Esempio n. 1
0
        /// <summary>Extracts a sub image using the given mask and brightness.</summary>
        private Texture2D ExtractSubImage(Texture2D source, Texture2D mask, Desaturation.Mode desaturationMode, float brightness)
        {
            // Size checks.
            if (mask != null)
            {
                if (source.Width != mask.Width)
                {
                    throw new ArgumentException($"Widths of image and mask don't match: {source.Width} != {mask.Width}");
                }

                if (source.Height < mask.Height)
                {
                    monitor_.Log($"Height of the image is less than height of the mask: {source.Height} < {mask.Height}", LogLevel.Info);
                }
                else if (source.Height > mask.Height)
                {
                    monitor_.Log($"Height of the image is greater than height of the mask. Parts of the image not covered by mask won't be recolored: {source.Height} > {mask.Height}", LogLevel.Warn);
                }
            }

            if (brightness < 0.0f)
            {
                throw new ArgumentException("Brightness must not be negative");
            }

            Color[] sourcePixels = Utility.TextureToArray(source);
            Color[] maskPixels   = mask != null?Utility.TextureToArray(mask) : null;

            Color[] extractedPixels = new Color[source.Width * source.Height];

            // If mask is null recolor the whole image.
            var getMaskValue = maskPixels == null ? (Func <Color[], int, byte>)((p, i) => (byte)0xFF)
                                                  : GetMaskValue;

            for (int i = 0; i < sourcePixels.Length; i++)
            {
                Color pixel     = Desaturation.Desaturate(sourcePixels[i], desaturationMode);
                byte  maskValue = getMaskValue(maskPixels, i);
                // Multiplication is all we need: If maskValue is zero the resulting pixel is zero (TransparentBlack).
                // Clamping is done automatically on assignment.
                extractedPixels[i] = pixel * (maskValue / 255.0f) * brightness;
            }

            return(Utility.ArrayToTexture(extractedPixels, source.Width, source.Height));
        }
Esempio n. 2
0
        /// <summary>Extracts a sub image using the given mask.</summary>
        private Texture2D ExtractSubImage(Texture2D source, Texture2D mask, Desaturation.Mode desaturationMode)
        {
            if (mask.Width != source.Width || mask.Height != source.Height)
            {
                throw new ArgumentException("Sizes of image and mask don't match");
            }

            Color[] sourcePixels    = Utility.TextureToArray(source);
            Color[] maskPixels      = Utility.TextureToArray(mask);
            Color[] extractedPixels = new Color[source.Width * source.Height];

            for (int i = 0; i < sourcePixels.Length; i++)
            {
                Color pixel = Desaturation.Desaturate(sourcePixels[i], desaturationMode);
                // Treat mask as grayscale (luma).
                byte maskValue = Desaturation.Desaturate(maskPixels[i], Desaturation.Mode.DesaturateLuma).R;
                // Multiplication is all we need: If maskValue is zero the resulting pixel is zero (TransparentBlack).
                extractedPixels[i] = pixel * (maskValue / 255.0f);
            }

            return(Utility.ArrayToTexture(extractedPixels, source.Width, source.Height));
        }