public static WriteableBitmap Resize(WriteableBitmap bmp, int width, int height, InterpolationTypes interpolation)
        {
            using (var srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                WriteableBitmap result = BitmapFactory.New(width, height);

                using (var dstContext = result.GetBitmapContext())
                {
                    int[] pd = new int[width * height];

                    if (interpolation == InterpolationTypes.NearestNeighbor)
                    {
                        NearstNeighborResize(srcContext.Pixels, srcContext.Width, srcContext.Height, width, height, pd);
                    }
                    else if (interpolation == InterpolationTypes.Bilinear)
                    {
                        BilinearResize(srcContext.Pixels, srcContext.Width, srcContext.Height, width, height, pd);
                    }

                    BitmapContext.BlockCopy(pd, 0, dstContext, 0, 4 * pd.Length);
                }

                return(result);
            }
        }
        /// <summary>
        /// Fills the whole WriteableBitmap with a color.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="color">The color used for filling.</param>
        public static void Clear(this WriteableBitmap bmp, Color color)
        {
            var col = ConvertColor(color);

            using (var context = bmp.GetBitmapContext())
            {
                var pixels = context.Pixels;
                var w      = context.Width;
                var h      = context.Height;
                var len    = w * SizeOfArgb;

                // Fill first line
                for (var x = 0; x < w; x++)
                {
                    pixels[x] = col;
                }

                // Copy first line
                var blockHeight = 1;
                var y           = 1;
                while (y < h)
                {
                    BitmapContext.BlockCopy(context, 0, context, y * len, blockHeight * len);
                    y          += blockHeight;
                    blockHeight = Math.Min(2 * blockHeight, h - y);
                }
            }
        }
 /// <summary>
 /// Copies color information from an ARGB byte array into this WriteableBitmap starting at a specific buffer index.
 /// </summary>
 /// <param name="bmp">The WriteableBitmap.</param>
 /// <param name="offset">The starting index in the buffer.</param>
 /// <param name="count">The number of bytes to copy from the buffer.</param>
 /// <param name="buffer">The color buffer as byte ARGB values.</param>
 /// <returns>The WriteableBitmap that was passed as parameter.</returns>
 public static WriteableBitmap FromByteArray(this WriteableBitmap bmp, byte[] buffer, int offset, int count)
 {
     using (var context = bmp.GetBitmapContext())
     {
         BitmapContext.BlockCopy(buffer, offset, context, 0, count);
         return(bmp);
     }
 }
 /// <summary>
 /// Clones the specified WriteableBitmap.
 /// </summary>
 /// <param name="bmp">The WriteableBitmap.</param>
 /// <returns>A copy of the WriteableBitmap.</returns>
 public static WriteableBitmap Clone(this WriteableBitmap bmp)
 {
     using (var srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
     {
         var result = BitmapFactory.New(srcContext.Width, srcContext.Height);
         using (var destContext = result.GetBitmapContext())
         {
             BitmapContext.BlockCopy(srcContext, 0, destContext, 0, srcContext.Length * SizeOfArgb);
         }
         return(result);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new resized WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="width">The new desired width.</param>
        /// <param name="height">The new desired height.</param>
        /// <param name="interpolation">The interpolation method that should be used.</param>
        /// <returns>A new WriteableBitmap that is a resized version of the input.</returns>
        public static WriteableBitmap Resize(this WriteableBitmap bmp, int width, int height, Interpolation interpolation)
        {
            using (var srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                var pd = Resize(srcContext, srcContext.Width, srcContext.Height, width, height, interpolation);

                var result = BitmapFactory.New(width, height);
                using (var dstContext = result.GetBitmapContext())
                {
                    BitmapContext.BlockCopy(pd, 0, dstContext, 0, SizeOfArgb * pd.Length);
                }
                return(result);
            }
        }
        /// <summary>
        /// Copies the Pixels from the WriteableBitmap into a ARGB byte array starting at a specific Pixels index.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="offset">The starting Pixels index.</param>
        /// <param name="count">The number of Pixels to copy, -1 for all</param>
        /// <returns>The color buffer as byte ARGB values.</returns>
        public static byte[] ToByteArray(this WriteableBitmap bmp, int offset, int count)
        {
            using (var context = bmp.GetBitmapContext())
            {
                if (count == -1)
                {
                    // Copy all to byte array
                    count = context.Length;
                }

                var len    = count * SizeOfArgb;
                var result = new byte[len]; // ARGB
                BitmapContext.BlockCopy(context, offset, result, 0, len);
                return(result);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Creates a new cropped WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="x">The x coordinate of the rectangle that defines the crop region.</param>
        /// <param name="y">The y coordinate of the rectangle that defines the crop region.</param>
        /// <param name="width">The width of the rectangle that defines the crop region.</param>
        /// <param name="height">The height of the rectangle that defines the crop region.</param>
        /// <returns>A new WriteableBitmap that is a cropped version of the input.</returns>
        public static WriteableBitmap Crop(this WriteableBitmap bmp, int x, int y, int width, int height)
        {
            using (var srcContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                var srcWidth  = srcContext.Width;
                var srcHeight = srcContext.Height;

                // If the rectangle is completely out of the bitmap
                if (x > srcWidth || y > srcHeight)
                {
                    return(BitmapFactory.New(0, 0));
                }

                // Clamp to boundaries
                if (x < 0)
                {
                    x = 0;
                }
                if (x + width > srcWidth)
                {
                    width = srcWidth - x;
                }
                if (y < 0)
                {
                    y = 0;
                }
                if (y + height > srcHeight)
                {
                    height = srcHeight - y;
                }

                // Copy the pixels line by line using fast BlockCopy
                var result = BitmapFactory.New(width, height);
                using (var destContext = result.GetBitmapContext())
                {
                    for (var line = 0; line < height; line++)
                    {
                        var srcOff = ((y + line) * srcWidth + x) * SizeOfArgb;
                        var dstOff = line * width * SizeOfArgb;
                        BitmapContext.BlockCopy(srcContext, srcOff, destContext, dstOff, width * SizeOfArgb);
                    }

                    return(result);
                }
            }
        }
        public static void FillRectangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            using (var context = bmp.GetBitmapContext())
            {
                // Use refs for faster access (really important!) speeds up a lot!
                var w      = context.Width;
                var h      = context.Height;
                var pixels = context.Pixels;

                // Check boundaries
                if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0) ||
                    (x1 >= w && x2 >= w) || (y1 >= h && y2 >= h))
                {
                    return;
                }

                // Clamp boundaries
                if (x1 < 0)
                {
                    x1 = 0;
                }
                if (y1 < 0)
                {
                    y1 = 0;
                }
                if (x2 < 0)
                {
                    x2 = 0;
                }
                if (y2 < 0)
                {
                    y2 = 0;
                }
                if (x1 > w)
                {
                    x1 = w;
                }
                if (y1 > h)
                {
                    y1 = h;
                }
                if (x2 > w)
                {
                    x2 = w;
                }
                if (y2 > h)
                {
                    y2 = h;
                }

                // Fill first line
                var startY       = y1 * w;
                var startYPlusX1 = startY + x1;
                var endOffset    = startY + x2;
                for (var x = startYPlusX1; x < endOffset; x++)
                {
                    pixels[x] = color;
                }

                // Copy first line
                var len            = (x2 - x1) * SizeOfArgb;
                var srcOffsetBytes = startYPlusX1 * SizeOfArgb;
                var offset2        = y2 * w + x1;
                for (var y = startYPlusX1 + w; y <= offset2; y += w)
                {
                    BitmapContext.BlockCopy(context, srcOffsetBytes, context, y * SizeOfArgb, len);
                }
            }
        }
        /// <summary>
        /// Copies (blits) the pixels from the WriteableBitmap source to the destination WriteableBitmap (this).
        /// </summary>
        /// <param name="bmp">The destination WriteableBitmap.</param>
        /// <param name="destRect">The rectangle that defines the destination region.</param>
        /// <param name="source">The source WriteableBitmap.</param>
        /// <param name="sourceRect">The rectangle that will be copied from the source to the destination.</param>
        /// <param name="color">If not Colors.White, will tint the source image. A partially transparent color and the image will be drawn partially transparent. If the BlendMode is ColorKeying, this color will be used as color key to mask all pixels with this value out.</param>
        /// <param name="blendMode">The blending mode <see cref="BlendMode"/>.</param>
        internal static void Blit(this WriteableBitmap bmp, Rect destRect, WriteableBitmap source, Rect sourceRect, Color color, BlendMode blendMode)
        {
            if (color.A == 0)
            {
                return;
            }
#if WPF
            var isPrgba = source.Format == PixelFormats.Pbgra32 || source.Format == PixelFormats.Prgba64 || source.Format == PixelFormats.Prgba128Float;
#endif
            var dw = (int)destRect.Width;
            var dh = (int)destRect.Height;

            using (var srcContext = source.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                using (var destContext = bmp.GetBitmapContext())
                {
                    var sourceWidth = srcContext.Width;
                    var dpw         = destContext.Width;
                    var dph         = destContext.Height;

                    var intersect = new Rect(0, 0, dpw, dph);
                    intersect.Intersect(destRect);
                    if (intersect.IsEmpty)
                    {
                        return;
                    }

                    var sourcePixels = srcContext.Pixels;
                    var destPixels   = destContext.Pixels;
                    var sourceLength = srcContext.Length;

                    int sourceIdx = -1;
                    int px        = (int)destRect.X;
                    int py        = (int)destRect.Y;

                    int    x;
                    int    y;
                    int    idx;
                    double ii;
                    double jj;
                    int    sr = 0;
                    int    sg = 0;
                    int    sb = 0;
                    int    dr, dg, db;
                    int    sourcePixel;
                    int    sa = 0;
                    int    da;
                    int    ca = color.A;
                    int    cr = color.R;
                    int    cg = color.G;
                    int    cb = color.B;
                    bool   tinted = color != Colors.White;
                    var    sw = (int)sourceRect.Width;
                    var    sdx = sourceRect.Width / destRect.Width;
                    var    sdy = sourceRect.Height / destRect.Height;
                    int    sourceStartX = (int)sourceRect.X;
                    int    sourceStartY = (int)sourceRect.Y;
                    int    lastii, lastjj;
                    lastii = -1;
                    lastjj = -1;
                    jj     = sourceStartY;
                    y      = py;
                    for (int j = 0; j < dh; j++)
                    {
                        if (y >= 0 && y < dph)
                        {
                            ii          = sourceStartX;
                            idx         = px + y * dpw;
                            x           = px;
                            sourcePixel = sourcePixels[0];

                            // Scanline BlockCopy is much faster (3.5x) if no tinting and blending is needed,
                            // even for smaller sprites like the 32x32 particles.
                            if (blendMode == BlendMode.None && !tinted)
                            {
                                sourceIdx = (int)ii + (int)jj * sourceWidth;
                                var offset = x < 0 ? -x : 0;
                                var xx     = x + offset;
                                var wx     = sourceWidth - offset;
                                var len    = xx + wx < dpw ? wx : dpw - xx;
                                if (len > sw)
                                {
                                    len = sw;
                                }
                                if (len > dw)
                                {
                                    len = dw;
                                }
                                BitmapContext.BlockCopy(srcContext, (sourceIdx + offset) * 4, destContext, (idx + offset) * 4, len * 4);
                            }

                            // Pixel by pixel copying
                            else
                            {
                                for (int i = 0; i < dw; i++)
                                {
                                    if (x >= 0 && x < dpw)
                                    {
                                        if ((int)ii != lastii || (int)jj != lastjj)
                                        {
                                            sourceIdx = (int)ii + (int)jj * sourceWidth;
                                            if (sourceIdx >= 0 && sourceIdx < sourceLength)
                                            {
                                                sourcePixel = sourcePixels[sourceIdx];
                                                sa          = ((sourcePixel >> 24) & 0xff);
                                                sr          = ((sourcePixel >> 16) & 0xff);
                                                sg          = ((sourcePixel >> 8) & 0xff);
                                                sb          = ((sourcePixel) & 0xff);
                                                if (tinted && sa != 0)
                                                {
                                                    sa          = (((sa * ca) * 0x8081) >> 23);
                                                    sr          = ((((((sr * cr) * 0x8081) >> 23) * ca) * 0x8081) >> 23);
                                                    sg          = ((((((sg * cg) * 0x8081) >> 23) * ca) * 0x8081) >> 23);
                                                    sb          = ((((((sb * cb) * 0x8081) >> 23) * ca) * 0x8081) >> 23);
                                                    sourcePixel = (sa << 24) | (sr << 16) | (sg << 8) | sb;
                                                }
                                            }
                                            else
                                            {
                                                sa = 0;
                                            }
                                        }
                                        if (blendMode == BlendMode.None)
                                        {
                                            destPixels[idx] = sourcePixel;
                                        }
                                        else if (blendMode == BlendMode.ColorKeying)
                                        {
                                            sr = ((sourcePixel >> 16) & 0xff);
                                            sg = ((sourcePixel >> 8) & 0xff);
                                            sb = ((sourcePixel) & 0xff);

                                            if (sr != color.R || sg != color.G || sb != color.B)
                                            {
                                                destPixels[idx] = sourcePixel;
                                            }
                                        }
                                        else if (blendMode == BlendMode.Mask)
                                        {
                                            int destPixel = destPixels[idx];
                                            da        = ((destPixel >> 24) & 0xff);
                                            dr        = ((destPixel >> 16) & 0xff);
                                            dg        = ((destPixel >> 8) & 0xff);
                                            db        = ((destPixel) & 0xff);
                                            destPixel = ((((da * sa) * 0x8081) >> 23) << 24) |
                                                        ((((dr * sa) * 0x8081) >> 23) << 16) |
                                                        ((((dg * sa) * 0x8081) >> 23) << 8) |
                                                        ((((db * sa) * 0x8081) >> 23));
                                            destPixels[idx] = destPixel;
                                        }
                                        else if (sa > 0)
                                        {
                                            int destPixel = destPixels[idx];
                                            da = ((destPixel >> 24) & 0xff);
                                            if ((sa == 255 || da == 0) &&
                                                blendMode != BlendMode.Additive &&
                                                blendMode != BlendMode.Subtractive &&
                                                blendMode != BlendMode.Multiply
                                                )
                                            {
                                                destPixels[idx] = sourcePixel;
                                            }
                                            else
                                            {
                                                dr = ((destPixel >> 16) & 0xff);
                                                dg = ((destPixel >> 8) & 0xff);
                                                db = ((destPixel) & 0xff);
                                                if (blendMode == BlendMode.Alpha)
                                                {
                                                    var isa = 255 - sa;
#if NETFX_CORE
                                                    // Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
                                                    destPixel = ((da & 0xff) << 24) |
                                                                ((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
                                                                ((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
                                                                (((sb * sa + isa * db) >> 8) & 0xff);
#elif WPF
                                                    if (isPrgba)
                                                    {
                                                        destPixel = ((da & 0xff) << 24) |
                                                                    (((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
                                                                    (((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
                                                                    ((((sb << 8) + isa * db) >> 8) & 0xff);
                                                    }
                                                    else
                                                    {
                                                        destPixel = ((da & 0xff) << 24) |
                                                                    (((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
                                                                    (((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
                                                                    ((((sb * sa) + isa * db) >> 8) & 0xff);
                                                    }
#else
                                                    destPixel = ((da & 0xff) << 24) |
                                                                (((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
                                                                (((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
                                                                ((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
                                                }
                                                else if (blendMode == BlendMode.Additive)
                                                {
                                                    int a = (255 <= sa + da) ? 255 : (sa + da);
                                                    destPixel = (a << 24) |
                                                                (((a <= sr + dr) ? a : (sr + dr)) << 16) |
                                                                (((a <= sg + dg) ? a : (sg + dg)) << 8) |
                                                                (((a <= sb + db) ? a : (sb + db)));
                                                }
                                                else if (blendMode == BlendMode.Subtractive)
                                                {
                                                    int a = da;
                                                    destPixel = (a << 24) |
                                                                (((sr >= dr) ? 0 : (sr - dr)) << 16) |
                                                                (((sg >= dg) ? 0 : (sg - dg)) << 8) |
                                                                (((sb >= db) ? 0 : (sb - db)));
                                                }
                                                else if (blendMode == BlendMode.Multiply)
                                                {
                                                    // Faster than a division like (s * d) / 255 are 2 shifts and 2 adds
                                                    int ta = (sa * da) + 128;
                                                    int tr = (sr * dr) + 128;
                                                    int tg = (sg * dg) + 128;
                                                    int tb = (sb * db) + 128;

                                                    int ba = ((ta >> 8) + ta) >> 8;
                                                    int br = ((tr >> 8) + tr) >> 8;
                                                    int bg = ((tg >> 8) + tg) >> 8;
                                                    int bb = ((tb >> 8) + tb) >> 8;

                                                    destPixel = (ba << 24) |
                                                                ((ba <= br ? ba : br) << 16) |
                                                                ((ba <= bg ? ba : bg) << 8) |
                                                                ((ba <= bb ? ba : bb));
                                                }

                                                destPixels[idx] = destPixel;
                                            }
                                        }
                                    }
                                    x++;
                                    idx++;
                                    ii += sdx;
                                }
                            }
                        }
                        jj += sdy;
                        y++;
                    }
                }
            }
        }
        async void ColorToWall_SelectedColorChanged(object sender, WallSelectorChangedEventArgs e)
        {
            // Check if we have an image loaded first.
            var bitmapData = this.DataContext.CurrentBitmapData;

            if (bitmapData == null)
            {
                return;
            }

            int width  = this.DataContext.ImageWidth;
            int height = this.DataContext.ImageHeight;

            // Get the pixels into temporary arrays in order to allow use to write all the pixels in a thread to avoid slowdowns from block copying to the writeable bitmap.
            int[] inGamePixels = new int[width * height * 256];
            using (var context = this.DataContext.InGameBitmap.GetBitmapContext(ReadWriteMode.ReadOnly))
                BitmapContext.BlockCopy(context, 0, inGamePixels, 0, width * height * 1024);
            int[] mapPixels = new int[width * height * 256];
            using (var context = this.DataContext.MapBitmap.GetBitmapContext(ReadWriteMode.ReadOnly))
                BitmapContext.BlockCopy(context, 0, mapPixels, 0, width * height * 1024);
            await Task.Run(async() =>
            {
                // Loop over all the pixels of the image.
                for (int x = 0; x < width; ++x)
                {
                    for (int y = 0; y < height; ++y)
                    {
                        // Check if the pixel's color matches the color that was just changed.
                        var wallInfo = bitmapData.PixelInfo[(x, y)];
                        if (wallInfo.Color != e.Color)
                        {
                            continue;
                        }

                        // Using the existing wall frame or getting a random one comes from Terraria.Framing.WallFrame().
                        int wallFrameNum = wallInfo.WallName == e.WallName ? wallInfo.WallFrame : MainWindow.rand.Next(0, 3);

                        // Get the in-game wall frame (or the original color).
                        int[] wallFramePixels = await App.GetWallFramePixels(e.WallName, e.ColorName, x, y, wallFrameNum);
                        if (wallFramePixels == null)
                        {
                            wallInfo.WallName = null;
                            wallFramePixels   = bitmapData.CachedColorPixels[e.Color];
                        }
                        else
                        {
                            wallInfo.WallName  = e.WallName;
                            wallInfo.WallFrame = wallFrameNum;
                        }

                        // Update the in-game image with the wall frame.
                        await PixelManip.CopyFrom16x16(wallFramePixels, inGamePixels, x, y, width);

                        // Get the map wall (or the original color).
                        int[] mapWallPixels = await App.GetColoredMapWallPixels(e.WallName, e.ColorName);
                        if (mapWallPixels == null)
                        {
                            mapWallPixels = bitmapData.CachedColorPixels[e.Color];
                        }

                        // Update the map image with the wall.
                        await PixelManip.CopyFrom16x16(mapWallPixels, mapPixels, x, y, width);
                    }
        public static unsafe void Blit(this WriteableBitmap bmp, Rect destRect, WriteableBitmap source, Rect sourceRect, Color color, WriteableBitmapExtensions.BlendMode blendMode)
        {
            if ((int)color.A == 0)
            {
                return;
            }
            bool flag1 = source.Format == PixelFormats.Pbgra32 || source.Format == PixelFormats.Prgba64 || source.Format == PixelFormats.Prgba128Float;
            int  num1  = (int)destRect.Width;
            int  num2  = (int)destRect.Height;

            using (BitmapContext bitmapContext1 = WriteableBitmapContextExtensions.GetBitmapContext(source, ReadWriteMode.ReadOnly))
            {
                using (BitmapContext bitmapContext2 = WriteableBitmapContextExtensions.GetBitmapContext(bmp))
                {
                    int  width1 = bitmapContext1.Width;
                    int  width2 = bitmapContext2.Width;
                    int  height = bitmapContext2.Height;
                    Rect rect   = new Rect(0.0, 0.0, (double)width2, (double)height);
                    rect.Intersect(destRect);
                    if (rect.IsEmpty)
                    {
                        return;
                    }
                    int *  pixels1 = bitmapContext1.Pixels;
                    int *  pixels2 = bitmapContext2.Pixels;
                    int    length  = bitmapContext1.Length;
                    int    num3    = (int)destRect.X;
                    int    num4    = (int)destRect.Y;
                    int    num5    = 0;
                    int    num6    = 0;
                    int    num7    = 0;
                    int    num8    = 0;
                    int    num9    = (int)color.A;
                    int    num10   = (int)color.R;
                    int    num11   = (int)color.G;
                    int    num12   = (int)color.B;
                    bool   flag2   = color != Colors.White;
                    int    num13   = (int)sourceRect.Width;
                    double num14   = sourceRect.Width / destRect.Width;
                    double num15   = sourceRect.Height / destRect.Height;
                    int    num16   = (int)sourceRect.X;
                    int    num17   = (int)sourceRect.Y;
                    int    num18   = -1;
                    int    num19   = -1;
                    double num20   = (double)num17;
                    int    num21   = num4;
                    for (int index1 = 0; index1 < num2; ++index1)
                    {
                        if (num21 >= 0 && num21 < height)
                        {
                            double num22  = (double)num16;
                            int    index2 = num3 + num21 * width2;
                            int    num23  = num3;
                            int    num24  = *pixels1;
                            if (blendMode == WriteableBitmapExtensions.BlendMode.None && !flag2)
                            {
                                int num25 = (int)num22 + (int)num20 * width1;
                                int num26 = num23 < 0 ? -num23 : 0;
                                int num27 = num23 + num26;
                                int num28 = width1 - num26;
                                int num29 = num27 + num28 < width2 ? num28 : width2 - num27;
                                if (num29 > num13)
                                {
                                    num29 = num13;
                                }
                                if (num29 > num1)
                                {
                                    num29 = num1;
                                }
                                BitmapContext.BlockCopy(bitmapContext1, (num25 + num26) * 4, bitmapContext2, (index2 + num26) * 4, num29 * 4);
                            }
                            else
                            {
                                for (int index3 = 0; index3 < num1; ++index3)
                                {
                                    if (num23 >= 0 && num23 < width2)
                                    {
                                        if ((int)num22 != num18 || (int)num20 != num19)
                                        {
                                            int index4 = (int)num22 + (int)num20 * width1;
                                            if (index4 >= 0 && index4 < length)
                                            {
                                                num24 = pixels1[index4];
                                                num8  = num24 >> 24 & (int)byte.MaxValue;
                                                num5  = num24 >> 16 & (int)byte.MaxValue;
                                                num6  = num24 >> 8 & (int)byte.MaxValue;
                                                num7  = num24 & (int)byte.MaxValue;
                                                if (flag2 && num8 != 0)
                                                {
                                                    num8  = num8 * num9 * 32897 >> 23;
                                                    num5  = (num5 * num10 * 32897 >> 23) * num9 * 32897 >> 23;
                                                    num6  = (num6 * num11 * 32897 >> 23) * num9 * 32897 >> 23;
                                                    num7  = (num7 * num12 * 32897 >> 23) * num9 * 32897 >> 23;
                                                    num24 = num8 << 24 | num5 << 16 | num6 << 8 | num7;
                                                }
                                            }
                                            else
                                            {
                                                num8 = 0;
                                            }
                                        }
                                        if (blendMode == WriteableBitmapExtensions.BlendMode.None)
                                        {
                                            pixels2[index2] = num24;
                                        }
                                        else if (blendMode == WriteableBitmapExtensions.BlendMode.ColorKeying)
                                        {
                                            num5 = num24 >> 16 & (int)byte.MaxValue;
                                            num6 = num24 >> 8 & (int)byte.MaxValue;
                                            num7 = num24 & (int)byte.MaxValue;
                                            if (num5 != (int)color.R || num6 != (int)color.G || num7 != (int)color.B)
                                            {
                                                pixels2[index2] = num24;
                                            }
                                        }
                                        else if (blendMode == WriteableBitmapExtensions.BlendMode.Mask)
                                        {
                                            int num25 = pixels2[index2];
                                            int num26 = num25 >> 24 & (int)byte.MaxValue;
                                            int num27 = num25 >> 16 & (int)byte.MaxValue;
                                            int num28 = num25 >> 8 & (int)byte.MaxValue;
                                            int num29 = num25 & (int)byte.MaxValue;
                                            int num30 = num26 * num8 * 32897 >> 23 << 24 | num27 * num8 * 32897 >> 23 << 16 | num28 * num8 * 32897 >> 23 << 8 | num29 * num8 * 32897 >> 23;
                                            pixels2[index2] = num30;
                                        }
                                        else if (num8 > 0)
                                        {
                                            int num25 = pixels2[index2];
                                            int num26 = num25 >> 24 & (int)byte.MaxValue;
                                            if ((num8 == (int)byte.MaxValue || num26 == 0) && (blendMode != WriteableBitmapExtensions.BlendMode.Additive && blendMode != WriteableBitmapExtensions.BlendMode.Subtractive) && blendMode != WriteableBitmapExtensions.BlendMode.Multiply)
                                            {
                                                pixels2[index2] = num24;
                                            }
                                            else
                                            {
                                                int num27 = num25 >> 16 & (int)byte.MaxValue;
                                                int num28 = num25 >> 8 & (int)byte.MaxValue;
                                                int num29 = num25 & (int)byte.MaxValue;
                                                if (blendMode == WriteableBitmapExtensions.BlendMode.Alpha)
                                                {
                                                    int num30 = (int)byte.MaxValue - num8;
                                                    num25 = !flag1 ? (num26 & (int)byte.MaxValue) << 24 | (num5 * num8 + num30 * num27 >> 8 & (int)byte.MaxValue) << 16 | (num6 * num8 + num30 * num28 >> 8 & (int)byte.MaxValue) << 8 | num7 * num8 + num30 * num29 >> 8 & (int)byte.MaxValue : (num26 & (int)byte.MaxValue) << 24 | ((num5 << 8) + num30 * num27 >> 8 & (int)byte.MaxValue) << 16 | ((num6 << 8) + num30 * num28 >> 8 & (int)byte.MaxValue) << 8 | (num7 << 8) + num30 * num29 >> 8 & (int)byte.MaxValue;
                                                }
                                                else if (blendMode == WriteableBitmapExtensions.BlendMode.Additive)
                                                {
                                                    int num30 = (int)byte.MaxValue <= num8 + num26 ? (int)byte.MaxValue : num8 + num26;
                                                    num25 = num30 << 24 | (num30 <= num5 + num27 ? num30 : num5 + num27) << 16 | (num30 <= num6 + num28 ? num30 : num6 + num28) << 8 | (num30 <= num7 + num29 ? num30 : num7 + num29);
                                                }
                                                else if (blendMode == WriteableBitmapExtensions.BlendMode.Subtractive)
                                                {
                                                    num25 = num26 << 24 | (num5 >= num27 ? 0 : num5 - num27) << 16 | (num6 >= num28 ? 0 : num6 - num28) << 8 | (num7 >= num29 ? 0 : num7 - num29);
                                                }
                                                else if (blendMode == WriteableBitmapExtensions.BlendMode.Multiply)
                                                {
                                                    int num30 = num8 * num26 + 128;
                                                    int num31 = num5 * num27 + 128;
                                                    int num32 = num6 * num28 + 128;
                                                    int num33 = num7 * num29 + 128;
                                                    int num34 = (num30 >> 8) + num30 >> 8;
                                                    int num35 = (num31 >> 8) + num31 >> 8;
                                                    int num36 = (num32 >> 8) + num32 >> 8;
                                                    int num37 = (num33 >> 8) + num33 >> 8;
                                                    num25 = num34 << 24 | (num34 <= num35 ? num34 : num35) << 16 | (num34 <= num36 ? num34 : num36) << 8 | (num34 <= num37 ? num34 : num37);
                                                }
                                                pixels2[index2] = num25;
                                            }
                                        }
                                    }
                                    ++num23;
                                    ++index2;
                                    num22 += num14;
                                }
                            }
                        }
                        num20 += num15;
                        ++num21;
                    }
                }
            }
        }