public BGRA Lerp(BGRA color1, int factor1, BGRA color2, int factor2, BGRA color3, int factor3)
        {
            int alphaFactor1 = factor1;
            int alphaFactor2 = factor2;
            int alphaFactor3 = factor3;

            if (color1.A == 0)
            {
                factor1 = 0;
            }
            if (color2.A == 0)
            {
                factor2 = 0;
            }
            if (color3.A == 0)
            {
                factor3 = 0;
            }

            if (factor1 == 0 && factor2 == 0 && factor3 == 0)
            {
                return(color1);
            }

            return(BGRA.FromArgb(
                       LerpChannel(color1.A, alphaFactor1, color2.A, alphaFactor2, color3.A, alphaFactor3),
                       LerpChannel(color1.R, factor1, color2.R, factor2, color3.R, factor3),
                       LerpChannel(color1.G, factor1, color2.G, factor2, color3.G, factor3),
                       LerpChannel(color1.B, factor1, color2.B, factor2, color3.B, factor3)
                       ));
        }
Exemplo n.º 2
0
        public StaticLight(byte[] inData)
        {
            using (MemoryStream ms = new MemoryStream(inData))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    Type           = (LightType)br.ReadByte();
                    UseAttenuation = br.ReadBoolean();
                    UseUnknown1    = br.ReadBoolean();
                    UseUnknown2    = br.ReadBoolean();

                    Colour    = br.ReadBGRA();
                    Position  = br.ReadVector3();
                    Intensity = br.ReadSingle();

                    AttenuationStartRadius = br.ReadSingle();
                    AttenuationEndRadius   = br.ReadSingle();

                    Unknown1StartRadius = br.ReadSingle();
                    Unknown1EndRadius   = br.ReadSingle();

                    Unknown2StartRadius = br.ReadSingle();
                    Unknown2EndRadius   = br.ReadSingle();
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Writes a 4-byte <see cref="ARGB"/> value to the data stream.
 /// </summary>
 /// <param name="binaryWriter">The current <see cref="BinaryWriter"/> object.</param>
 /// <param name="argb">The ARGB value to write.</param>
 public static void WriteARGB(this BinaryWriter binaryWriter, BGRA argb)
 {
     binaryWriter.Write(argb.A);
     binaryWriter.Write(argb.R);
     binaryWriter.Write(argb.G);
     binaryWriter.Write(argb.B);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Writes a 4-byte <see cref="BGRA"/> value to the data stream.
 /// </summary>
 /// <param name="binaryWriter">The current <see cref="BinaryWriter"/> object.</param>
 /// <param name="bgra">The BGRA value to write.</param>
 public static void WriteBGRA(this BinaryWriter binaryWriter, BGRA bgra)
 {
     binaryWriter.Write(bgra.B);
     binaryWriter.Write(bgra.G);
     binaryWriter.Write(bgra.R);
     binaryWriter.Write(bgra.A);
 }
Exemplo n.º 5
0
    public static Image ColorizeDepthImage(Image depthImage, Tuple <int, int> expectedValueRange)
    {
        ImageFormat imageFormat = depthImage.Format;

        if (imageFormat != ImageFormat.Depth16 && imageFormat != ImageFormat.IR16)
        {
            throw new Exception("Attempted to colorize a non-depth image!");
        }

        int width  = depthImage.WidthPixels;
        int height = depthImage.HeightPixels;

        Image colorizedDepthImage = new Image(ImageFormat.ColorBGRA32, width, height);

        for (int h = 0; h < height; ++h)
        {
            for (int w = 0; w < width; ++w)
            {
                BGRA colorDepthPixel = new BGRA(0, 0, 0, 0);
                colorDepthPixel = ColorizeBlueToRed(depthImage.GetPixel <short>(h, w), expectedValueRange);
                colorizedDepthImage.SetPixel <BGRA>(h, w, colorDepthPixel);
            }
        }
        return(colorizedDepthImage);
    }
Exemplo n.º 6
0
        public static IImage <BGRA> ToBgra(IImage <double> blue, IImage <double> green, IImage <double> red, IImage <double> alpha)
        {
            CheckEqualDimensions(blue, green, red, alpha);

            // Normalize to [0 - 255]
            blue  = blue.Normalize256();
            green = green.Normalize256();
            red   = red.Normalize256();
            alpha = alpha.Normalize256();

            // Construct BGRA values
            BGRA[] data = new BGRA[blue.Length];
            Parallel.For(0, blue.Length, i =>
            {
                data[i] = new BGRA()
                {
                    B = (byte)blue[i],
                    G = (byte)green[i],
                    R = (byte)red[i],
                    A = (byte)alpha[i]
                };
            });

            return(new BgraImage(blue.Width, blue.Height, data));
        }
        private static unsafe void MakeBackgroundTransparent(Bitmap bitmap, BGRA background)
        {
            Debug.Assert(bitmap.PixelFormat == PixelFormat.Format32bppArgb);

            int width  = bitmap.Width;
            int height = bitmap.Height;

            var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            try
            {
                for (int y = 0; y < height; y++)
                {
                    var pData = (BGRA *)((byte *)data.Scan0 + y * data.Stride);
                    for (int x = 0; x < width; x++)
                    {
                        var col = pData[x];

                        if (col == background)
                        {
                            *(int *)(pData + x) = 0; //transparent
                        }
                    }
                }
            }
            finally
            {
                bitmap.UnlockBits(data);
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FogDefinition"/> class.
 /// </summary>
 /// <param name="inData">The binary data.</param>
 public FogDefinition(byte[] inData)
 {
     using var ms    = new MemoryStream(inData);
     using var br    = new BinaryReader(ms);
     EndRadius       = br.ReadSingle();
     StartMultiplier = br.ReadSingle();
     Colour          = br.ReadBGRA();
 }
 public BGRA Lerp(BGRA color1, int factor1, BGRA color2, int factor2, BGRA color3, int factor3)
 {
     return(BGRA.FromArgb(
                LerpChannel(color1.R, factor1, color2.R, factor2, color3.R, factor3),
                LerpChannel(color1.G, factor1, color2.G, factor2, color3.G, factor3),
                LerpChannel(color1.B, factor1, color2.B, factor2, color3.B, factor3)
                ));
 }
Exemplo n.º 10
0
 private static Color32 Convert32(BGRA c)
 {
     return(new Color32(
                c.R,
                c.G,
                c.B,
                c.A
                ));
 }
Exemplo n.º 11
0
        public void TestBgraStruct()
        {
            BGRA black = BGRA.Black;

            int    blackInt32  = black;
            UInt32 blackUInt32 = black;

            Assert.IsTrue(((BGRA)blackInt32) == black);
            Assert.IsTrue(((BGRA)blackUInt32) == black);
        }
Exemplo n.º 12
0
    private static BGRA ReadBGRA(Stream stream)
    {
        BGRA BGRAColor = new BGRA();

        BGRAColor.B = (byte)stream.ReadByte();
        BGRAColor.G = (byte)stream.ReadByte();
        BGRAColor.R = (byte)stream.ReadByte();
        BGRAColor.A = (byte)stream.ReadByte();
        return(BGRAColor);
    }
Exemplo n.º 13
0
        /// <summary>
        /// Reads a 4-byte BGRA value from the data stream.
        /// </summary>
        /// <returns>The argument.</returns>
        /// <param name="binaryReader">binaryReader.</param>
        public static BGRA ReadBGRA(this BinaryReader binaryReader)
        {
            byte b = binaryReader.ReadByte();
            byte g = binaryReader.ReadByte();
            byte r = binaryReader.ReadByte();
            byte a = binaryReader.ReadByte();

            BGRA bgra = new BGRA(b, g, r, a);

            return(bgra);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Reads a 4-byte BGRA value from the data stream.
        /// </summary>
        /// <returns>The argument.</returns>
        /// <param name="binaryReader">The reader.</param>
        public static BGRA ReadBGRA(this BinaryReader binaryReader)
        {
            var b = binaryReader.ReadByte();
            var g = binaryReader.ReadByte();
            var r = binaryReader.ReadByte();
            var a = binaryReader.ReadByte();

            var bgra = new BGRA(b, g, r, a);

            return(bgra);
        }
Exemplo n.º 15
0
    } // loaded

    public static void ReadMOCV(Stream WMOrootstream, int MOCVsize)
    {
        groupDataBuffer.vertexColors = new List <Color32>();
        int count = MOCVsize / 4;

        for (int i = 0; i < count; i++)
        {
            BGRA    bgra  = ReadBGRA(WMOrootstream);
            Color32 color = new Color32(bgra.R, bgra.G, bgra.B, bgra.A);
            groupDataBuffer.vertexColors.Add(color);
        }
    }
Exemplo n.º 16
0
        // https://code.google.com/p/2dimagefilter/wiki/ImageScaling

        public static IImage <BGRA> PixelUpscale(this IImage <BGRA> img)
        {
            var upscaled = new BgraImage(img.Width << 1, img.Height << 1, new BGRA[img.Length << 2]);

            for (int x = 0, xu = 0; x < img.Width; x++, xu += 2)
            {
                for (int y = 0, yu = 0; y < img.Height; y++, yu += 2)
                {
                    //   A    --\ 1 2
                    // C P B  --/ 3 4
                    //   D
                    BGRA P = img[y, x];
                    BGRA A = img[y - 1, x];
                    BGRA B = img[y, x + 1];
                    BGRA D = img[y + 1, x];
                    BGRA C = img[y, x - 1];

                    // 1=P; 2=P; 3=P; 4=P;
                    BGRA _1 = P, _2 = P, _3 = P, _4 = P;
                    // IF C==A AND C!=D AND A!=B => 1=A
                    if (C == A && C != D && A != B)
                    {
                        _1 = A;
                    }

                    // IF A==B AND A!=C AND B!=D => 2=B
                    if (A == B && A != C && B != D)
                    {
                        _2 = B;
                    }

                    // IF B==D AND B!=A AND D!=C => 4=D
                    if (B == D && B != A && D != C)
                    {
                        _4 = D;
                    }

                    // IF D==C AND D!=B AND C!=A => 3=C
                    if (D == C && D != B && C != A)
                    {
                        _3 = C;
                    }

                    upscaled[yu, xu + 0] = _1;
                    upscaled[yu, xu + 1] = _2;
                    ++yu;
                    upscaled[yu, xu + 0] = _3;
                    upscaled[yu, xu + 1] = _4;
                }
            }

            return(null);
        }
Exemplo n.º 17
0
        public static BGRA ReadBGRA(this BinaryReader reader)
        {
            BGRA BGRAColor = new BGRA()
            {
                B = reader.ReadByte(),
                G = reader.ReadByte(),
                R = reader.ReadByte(),
                A = reader.ReadByte(),
            };

            return(BGRAColor);
        }
Exemplo n.º 18
0
 public FogDefinition(byte[] inData)
 {
     using (MemoryStream ms = new MemoryStream(inData))
     {
         using (BinaryReader br = new BinaryReader(ms))
         {
             this.EndRadius = br.ReadSingle();
             this.StartMultiplier = br.ReadSingle();
             this.Colour = br.ReadBGRA();
         }
     }
 }
Exemplo n.º 19
0
 public FogDefinition(byte[] inData)
 {
     using (MemoryStream ms = new MemoryStream(inData))
     {
         using (BinaryReader br = new BinaryReader(ms))
         {
             this.EndRadius       = br.ReadSingle();
             this.StartMultiplier = br.ReadSingle();
             this.Colour          = br.ReadBGRA();
         }
     }
 }
Exemplo n.º 20
0
        public static YCbCr ToYCbCr_JPEG(this BGRA color)
        {
            double R = color.R;
            double G = color.G;
            double B = color.B;

            double Y  = R * 0.29900 + G * 0.58700 + B * 0.11400;
            double Cb = R * -0.16874 + G * -0.33126 + B * 0.50000 + 128;
            double Cr = R * 0.50000 + G * -0.41869 + B * -0.08131 + 128;

            return(new YCbCr {
                Y = (byte)Y, Cb = (byte)Cb, Cr = (byte)Cr
            });
        }
Exemplo n.º 21
0
        private static void TestDataConsistency <T>(IImage <T> img)
            where T : struct, IEquatable <T>
        {
            byte[] rgb  = img.ToBGR();
            byte[] rgbA = img.ToBGRA();

            Assert.IsTrue((rgb.Length / 3) * 4 == rgbA.Length);

            // rgb and rgbA should be the same
            // except one lacks alpha
            for (int i = 0, j = 0; i < rgbA.Length; i++, j++)
            {
                // Alpha
                if ((i + 1) % 4 == 0)
                {
                    // Alpha should be 255
                    Assert.IsTrue(rgbA[i] == byte.MaxValue);
                    j--;
                    continue;
                }

                Assert.IsTrue(rgb[j] == rgbA[i]);
            }

            // Doing this should produce the same thing as
            // ToBGRA just as a struct in stead of 4 bytes
            BGRA[] bgra = new BGRA[img.Length];
            img.ToIndexedBgra((i, x) => bgra[i] = x);

            for (int i = 0, j = 0; i < img.Length; i++, j += 4)
            {
                BGRA val = bgra[i];

                Assert.IsTrue(val.B == rgbA[j]);
                Assert.IsTrue(val.G == rgbA[j + 1]);
                Assert.IsTrue(val.R == rgbA[j + 2]);
                Assert.IsTrue(val.A == rgbA[j + 3]);
            }

            // ToBGRA just as BGRA struct
            BGRA[] bgra2 = img.ToPixelColor();

            for (int i = 0; i < img.Length; i++)
            {
                Assert.IsTrue(bgra[i] == bgra2[i]);
            }
        }
        public void BGRA()
        {
            BGRA b1 = new BGRA(1, 2, 3, 4);
            BGRA b2 = new BGRA(1, 2, 3);

            Assert.AreEqual(1, b1.B);
            Assert.AreEqual(2, b1.G);
            Assert.AreEqual(3, b1.R);
            Assert.AreEqual(4, b1.A);

            Assert.AreEqual(0x04030201, b1.Value);

            Assert.AreNotEqual(b1, b2);
            b2.A = 4;
            Assert.AreEqual(b1, b2);

            Assert.AreEqual(true, b1.Equals(b2));
        }
Exemplo n.º 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DoodadInstance"/> class.
        /// </summary>
        /// <param name="inData">The binary data.</param>
        public DoodadInstance(byte[] inData)
        {
            using var ms = new MemoryStream(inData);
            using var br = new BinaryReader(ms);
            var finalNameBytes  = new byte[4];
            var nameOffsetBytes = br.ReadBytes(3);

            Buffer.BlockCopy(nameOffsetBytes, 0, finalNameBytes, 0, 3);

            NameOffset = BitConverter.ToUInt32(finalNameBytes, 0);

            Flags = (DoodadInstanceFlags)br.ReadByte();

            Position = br.ReadVector3();

            // TODO: Investigate whether or not this is a Quat16 in >= BC
            Orientation = br.ReadQuaternion32();

            Scale = br.ReadSingle();
            StaticLightingColour = br.ReadBGRA();
        }
Exemplo n.º 24
0
    public static BGRA ColorizeBlueToRed(short depthPixel, Tuple <int, int> min_max)
    {
        int min = min_max.Item1;
        int max = min_max.Item2;

        byte PixelMax = Byte.MaxValue;

        BGRA result = new BGRA(0, 0, 0, PixelMax);

        if (depthPixel == 0)
        {
            return(result);
        }

        int clampedValue = depthPixel;

        clampedValue = Math.Min(clampedValue, max);
        clampedValue = Math.Max(clampedValue, min);
        float hue = (float)(clampedValue - min) / (float)(max - min);

        float range = 2.0f / 3.0f;

        hue *= range;
        hue  = range - hue;

        float fRed   = 0.0f;
        float fGreen = 0.0f;
        float fBlue  = 0.0f;

        StaticImageProperties.ColorConvertHSVtoRGB(hue, 1.0f, 1.0f, ref fRed, ref fGreen, ref fBlue);

        result.R = Convert.ToByte(fRed * (float)PixelMax);
        result.G = Convert.ToByte(fGreen * (float)PixelMax);
        result.B = Convert.ToByte(fBlue * (float)PixelMax);

        return(result);
    }
Exemplo n.º 25
0
        public DoodadInstance(byte[] inData)
        {
            using (MemoryStream ms = new MemoryStream(inData))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    byte[] finalNameBytes = new byte[4];
                    byte[] nameOffsetBytes = br.ReadBytes(3);
                    Buffer.BlockCopy(nameOffsetBytes, 0, finalNameBytes, 0, 3);

                    this.NameOffset= BitConverter.ToUInt32(finalNameBytes, 0);

                    this.Flags = (DoodadInstanceFlags) br.ReadByte();

                    this.Position = br.ReadVector3f();

                    // TODO: Investigate whether or not this is a Quat16 in >= BC
                    this.Orientation = br.ReadQuaternion32();

                    this.Scale = br.ReadSingle();
                    this.StaticLightingColour = br.ReadBGRA();
                }
            }
        }
Exemplo n.º 26
0
        public static CMYK ToCmyk(this BGRA pixel)
        {
            // special case for all black (K = 1)
            // to avoid a NaN
            if (pixel == BGRA.Black)
            {
                return(new CMYK {
                    K = 1.0
                });
            }

            double R = pixel.R / 255.0;
            double G = pixel.G / 255.0;
            double B = pixel.B / 255.0;

            // K = 1-max(R', G', B')
            double K = 1 - (R > G ? (R > B ? R : B) : (G > B ? G : B));

            // 1 - K
            double oneMinK    = 1 - K;
            double oneMinDiv1 = 1.0 / oneMinK;

            // C = (1-R'-K) / (1-K)
            double C = (oneMinK - R) * oneMinDiv1;

            // M = (1-G'-K) / (1-K)
            double M = (oneMinK - G) * oneMinDiv1;

            // Y = (1-B'-K) / (1-K)
            double Y = (oneMinK - B) * oneMinDiv1;

            return(new CMYK()
            {
                C = C, M = M, Y = Y, K = K
            });
        }
        private unsafe void RenderInternal(Input input)
        {
            if (webBrowser.InvokeRequired)
            {
                //wait until MathJax is done with rendering
                while (!mathJaxRenderingDone)
                {
                    Thread.Sleep(WaitingIntervalMs);
                }
                webBrowser.Invoke(new Action <Input>(RenderInternal), input);
            }
            else
            {
                webBrowser.Width  = (int)(input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale * DefaultBrowserWidth);
                webBrowser.Height = (int)(input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale * DefaultBrowserHeight);

                const int ExtraMargin = 4;
                var       myDiv       = webBrowser.Document.GetElementById("myDiv");
                var       width       = (myDiv.OffsetRectangle.X + myDiv.ScrollRectangle.Width) + ExtraMargin;
                var       height      = (myDiv.OffsetRectangle.Y + myDiv.ScrollRectangle.Height) + ExtraMargin;

                webBrowser.Width              = width;
                webBrowser.Height             = height;
                webBrowser.Document.BackColor = Color.Transparent;

                const PixelFormat pixelFormat = PixelFormat.Format32bppArgb;
                using (var bitmap = new Bitmap(width, height, pixelFormat))
                {
                    if (webBrowser.Document.DomDocument is IViewObject viewObject)
                    {
                        var webBrowserRectangle = new TagRECT(myDiv.OffsetRectangle);
                        var bitmapRectangle     = new TagRECT(0, 0, width, height);

                        using (var gr = Graphics.FromImage(bitmap))
                        {
                            var hdc = gr.GetHdc();

                            try
                            {
                                int hr = viewObject.Draw(1 /*DVASPECT_CONTENT*/,
                                                         -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero,
                                                         hdc,
                                                         ref bitmapRectangle,
                                                         ref webBrowserRectangle,
                                                         IntPtr.Zero, 0);
                            }
                            finally
                            {
                                gr.ReleaseHdc();
                            }
                        }

                        var background = BGRA.From(input.Background);
                        using (var croppedBitmap = CropToContent(bitmap, background))
                        {
                            MakeBackgroundTransparent(croppedBitmap, background);

                            var bitmapSource = ResourcesManager.CreateBitmapSourceWithCurrentDpi(croppedBitmap);

                            var cacheInfo = new HtmlRendererCache.Info(
                                currentContent,
                                input.Foreground,
                                input.Background,
                                input.Font,
                                input.ZoomScale * ExtensionSettings.Instance.CustomZoomScale,
                                CacheVersion);

                            var errors          = objectForScripting.Errors.Count == 0 ? Array.Empty <string>() : objectForScripting.Errors.ToArray();
                            var cachedImagePath = errors.Length == 0 ? cache.Add(cacheInfo, croppedBitmap) : null;
                            rendererResult = new RendererResult(bitmapSource, cachedImagePath, errors);
                        }
                    }
                }
            }
        }
Exemplo n.º 28
0
 public static Color ToColor(this BGRA pixel)
 {
     return(Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B));
 }
Exemplo n.º 29
0
 public static HSV ToHsv(this BGRA pixel)
 {
     return(ToHsv((double)pixel.R, (double)pixel.G, (double)pixel.B));
 }
Exemplo n.º 30
0
 public static double ToGrayscale(this BGRA pixel)
 {
     return(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114);
 }
Exemplo n.º 31
0
 public static RGB ToRgb(this BGRA pixel)
 {
     return(new RGB(pixel.R, pixel.G, pixel.B));
 }
Exemplo n.º 32
0
 public static HSL ToHsl(this BGRA pixel)
 {
     return(ToHsl(pixel.R, pixel.G, pixel.B));
 }
Exemplo n.º 33
0
        public StaticLight(byte[] inData)
        {
            using (MemoryStream ms = new MemoryStream(inData))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    this.Type = (LightType) br.ReadByte();
                    this.bUseAttenuation = br.ReadBoolean();
                    this.bUseUnknown1 = br.ReadBoolean();
                    this.bUseUnknown2 = br.ReadBoolean();

                    this.Colour = br.ReadBGRA();
                    this.Position = br.ReadVector3f();
                    this.Intensity = br.ReadSingle();

                    this.AttenuationStartRadius = br.ReadSingle();
                    this.AttenuationEndRadius = br.ReadSingle();

                    this.Unknown1StartRadius = br.ReadSingle();
                    this.Unknown1EndRadius = br.ReadSingle();

                    this.Unknown2StartRadius = br.ReadSingle();
                    this.Unknown2EndRadius = br.ReadSingle();
                }
            }
        }