Пример #1
0
        public void DrawBitmap(bool needOrigImg = false)
        {
            if (imagePreviewControl1.Img is null)
            {
                needOrigImg = true;
            }

            BitmapTransform bt = new BitmapTransform();

            bt.bmp        = needOrigImg ? null : imagePreviewControl1.Img;
            bt.bmpOrig    = bmpOrig;
            bt.sz         = imagePreviewControl1.Size;
            bt.usePalette = false;

            TransformBitmap(this, bt);

            if (needOrigImg)
            {
                imagePreviewControl1.Img = bt.bmp;
                bmpOrig = bt.bmpOrig;
            }

            imagePreviewControl1.Draw(null, needOrigImg);
        }
Пример #2
0
        public void imagePreview_BitmapTransform(object sender, BitmapTransform bt)
        {
            if (bt.bmp is null)
            {
                float ratio = (float)bmp.Width / bmp.Height;
                if (ratio < 1)
                {
                    bt.sz.Width = (int)(bt.sz.Height * ratio);
                }
                else
                {
                    bt.sz.Height = (int)(bt.sz.Width / ratio);
                }
                bt.bmp = new Bitmap(bt.sz.Width, bt.sz.Height, pixelFormat);
                using (Graphics gr = Graphics.FromImage(bt.bmp))
                    gr.DrawImage(bmp, 0, 0, bt.sz.Width, bt.sz.Height);

                bt.bmpOrig = new Bitmap(bt.sz.Width, bt.sz.Height, Scene.pixelFormat);
                using (Graphics gr = Graphics.FromImage(bt.bmpOrig))
                    gr.DrawImage(bt.bmp, 0, 0, bt.sz.Width, bt.sz.Height);
            }

            BitmapData bDataOrig = bt.bmpOrig.LockBits(new Rectangle(0, 0, bt.bmpOrig.Width, bt.bmpOrig.Height), ImageLockMode.ReadOnly, pixelFormat);
            BitmapData bData     = bt.bmp.LockBits(new Rectangle(0, 0, bt.bmp.Width, bt.bmp.Height), ImageLockMode.ReadWrite, pixelFormat);

            unsafe
            {
                byte *pDataOrig = (byte *)bDataOrig.Scan0.ToPointer();
                byte *pData     = (byte *)bData.Scan0.ToPointer();
                int   len       = Math.Abs(bDataOrig.Stride);
                int   lenW      = len / bytesPerPixel * bytesPerPixel;
                for (int i = 0; i < bDataOrig.Height; i++)
                {
                    for (int j = 0; j < lenW; j += bytesPerPixel)
                    {
                        byte *pOrig = pDataOrig + i * len + j;
                        byte *p     = pData + i * len + j;

                        byte r = *(pOrig + 2);
                        byte g = *(pOrig + 1);
                        byte b = *pOrig;

                        if (UseEq && paletteFirst)
                        {
                            r = paletteEq.GetValue(ColorChannel.Red, r);
                            g = paletteEq.GetValue(ColorChannel.Green, g);
                            b = paletteEq.GetValue(ColorChannel.Blue, b);
                        }
                        int cr = ColorTransform.Brightness(r, g, b, brightness);
                        cr = ColorTransform.Contrast((byte)(cr & 0x0000ff), (byte)((cr >> 8) & 0x00ff), (byte)(cr >> 16), contrast);

                        r = (byte)(cr & 0x0000ff);
                        g = (byte)((cr >> 8) & 0x00ff);
                        b = (byte)(cr >> 16);

                        bool transparent = false;
                        if (!ColorTransform.BlackTolerance(r, g, b, blackTolerance) || !ColorTransform.WhiteTolerance(r, g, b, whiteTolerance))
                        {
                            r           = 0;
                            g           = 0;
                            b           = 0;
                            transparent = true;
                        }
                        if (!transparent && UseEq && !paletteFirst)
                        {
                            r = paletteEq.GetValue(ColorChannel.Red, r);
                            g = paletteEq.GetValue(ColorChannel.Green, g);
                            b = paletteEq.GetValue(ColorChannel.Blue, b);
                        }

                        *(p + 2) = r;
                        *(p + 1) = g;
                        *(p + 0) = b;
                    }
                }
            }
            bt.bmp.UnlockBits(bData);
            bt.bmpOrig.UnlockBits(bDataOrig);
        }