Beispiel #1
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);
        }
Beispiel #2
0
        void MakeList(ref Bitmap bmp, OpenGL gl, double halfW, double halfH, double height)
        {
            BitmapData bData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, pixelFormat);

            unsafe
            {
                byte *pData = (byte *)bData.Scan0.ToPointer();
                int   len   = Math.Abs(bData.Stride);
                int   lenW  = len / bytesPerPixel * bytesPerPixel / interlacing;

                for (int i = 0; i < bData.Height / interlacing - 1; i++)
                {
                    int i1 = i * interlacing;
                    int i2 = (i + 1) * interlacing;

                    double y1 = i1 - halfH;
                    double y2 = i2 - halfH;

                    gl.Begin(OpenGL.GL_QUAD_STRIP);

                    for (int j = 0; j < lenW; j += bytesPerPixel)
                    {
                        int j1 = j * interlacing;

                        double x = j1 / bytesPerPixel - halfW;

                        byte *p1 = pData + i1 * len + j1;
                        byte *p2 = pData + i2 * len + j1;

                        byte r1 = *(p1 + 2);
                        byte g1 = *(p1 + 1);
                        byte b1 = *p1;

                        byte r2 = *(p2 + 2);
                        byte g2 = *(p2 + 1);
                        byte b2 = *p2;

                        double z1 = (ColorTransform.GetBrightness(r1, g1, b1) / 255.0 - 0.5) * height;
                        double z2 = (ColorTransform.GetBrightness(r2, g2, b2) / 255.0 - 0.5) * height;

                        if (UseEq && paletteFirst)
                        {
                            r1 = paletteEq.GetValue(ColorChannel.Red, r1);
                            g1 = paletteEq.GetValue(ColorChannel.Green, g1);
                            b1 = paletteEq.GetValue(ColorChannel.Blue, b1);

                            r2 = paletteEq.GetValue(ColorChannel.Red, r2);
                            g2 = paletteEq.GetValue(ColorChannel.Green, g2);
                            b2 = paletteEq.GetValue(ColorChannel.Blue, b2);
                        }

                        int cr1 = ColorTransform.Brightness(r1, g1, b1, brightness);
                        int cr2 = ColorTransform.Brightness(r2, g2, b2, brightness);
                        cr1 = ColorTransform.Contrast((byte)(cr1 & 0x0000ff), (byte)((cr1 >> 8) & 0x00ff), (byte)(cr1 >> 16), contrast);
                        cr2 = ColorTransform.Contrast((byte)(cr2 & 0x0000ff), (byte)((cr2 >> 8) & 0x00ff), (byte)(cr2 >> 16), contrast);

                        r1 = (byte)(cr1 & 0x0000ff);
                        g1 = (byte)((cr1 >> 8) & 0x00ff);
                        b1 = (byte)(cr1 >> 16);

                        r2 = (byte)(cr2 & 0x0000ff);
                        g2 = (byte)((cr2 >> 8) & 0x00ff);
                        b2 = (byte)(cr2 >> 16);

                        bool bt1 = ColorTransform.BlackTolerance(r1, g1, b1, blackTolerance);
                        bool bt2 = ColorTransform.BlackTolerance(r2, g2, b2, blackTolerance);
                        bool wt1 = ColorTransform.WhiteTolerance(r1, g1, b1, whiteTolerance);
                        bool wt2 = ColorTransform.WhiteTolerance(r2, g2, b2, whiteTolerance);
                        if (UseEq && !paletteFirst)
                        {
                            r1 = paletteEq.GetValue(ColorChannel.Red, r1);
                            g1 = paletteEq.GetValue(ColorChannel.Green, g1);
                            b1 = paletteEq.GetValue(ColorChannel.Blue, b1);

                            r2 = paletteEq.GetValue(ColorChannel.Red, r2);
                            g2 = paletteEq.GetValue(ColorChannel.Green, g2);
                            b2 = paletteEq.GetValue(ColorChannel.Blue, b2);
                        }

                        gl.Color(r1 / 255.0, g1 / 255.0, b1 / 255.0, bt1 && wt1 ? 1.0 : 0.0);
                        gl.Vertex(x, bData.Stride > 0 ? y1 : -y1, -z1);

                        gl.Color(r2 / 255.0, g2 / 255.0, b2 / 255.0, bt2 && wt2 ? 1.0 : 0.0);
                        gl.Vertex(x, bData.Stride > 0 ? y2 : -y2, -z2);
                    }

                    gl.End();
                }
            }
            bmp.UnlockBits(bData);
        }