Exemplo n.º 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }
            //
            _basicOptions = basicFontOptionsUserControl1.Options;
            _basicOptions.TypefaceChanged += (s, e2) =>
            {
                if (_devVxsTextPrinter != null)
                {
                    _devVxsTextPrinter.Typeface = e2.SelectedTypeface;
                }
            };
            _basicOptions.UpdateRenderOutput += (s, e2) =>
            {
                UpdateRenderOutput();
            };
            //
            //----------
            _renderOptions = glyphRenderOptionsUserControl1.Options;
            _renderOptions.UpdateRenderOutput += (s, e2) =>
            {
                UpdateRenderOutput();
            };

            //share text printer to our sample textbox
            //but you can create another text printer that specific to text textbox control

            destImg = new ActualBitmap(800, 600);
            aggsx   = new AggRenderSurface(destImg);
            painter = new AggPainter(aggsx);

            winBmp = new Bitmap(destImg.Width, destImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            g      = this.sampleTextBox1.CreateGraphics();

            painter.CurrentFont = new PixelFarm.Drawing.RequestFont("tahoma", 14);

            _openFontTextService = new LayoutFarm.OpenFontTextService();
            //....
            _devVxsTextPrinter                   = new VxsTextPrinter(painter, _openFontTextService);
            _devVxsTextPrinter.ScriptLang        = _basicOptions.ScriptLang;
            _devVxsTextPrinter.PositionTechnique = Typography.TextLayout.PositionTechnique.OpenFont;
            _devVxsTextPrinter.FontSizeInPoints  = 10;

            _controllerForPixelFarm.BindHostGraphics(g);
            _controllerForPixelFarm.TextPrinter = _devVxsTextPrinter;

            this.sampleTextBox1.SetController(_controllerForPixelFarm);
            _readyToRender = true;
            _basicOptions.UpdateRenderOutput += (s, e2) => UpdateRenderOutput();
            //....
            //----------
            //txtInputChar.TextChanged += (s, e2) => UpdateRenderOutput();
            //----------
        }
Exemplo n.º 2
0
        public static void CopyFromGdiPlusBitmapSameSize(
            Bitmap windowsBitmap,
            ActualBitmap actualImage)
        {
            int h = windowsBitmap.Height;
            int w = windowsBitmap.Width;
            //byte[] targetBuffer = ActualImage.GetBuffer(actualImage);

            BitmapData bitmapData1 = windowsBitmap.LockBits(
                new Rectangle(0, 0,
                              w,
                              h),
                System.Drawing.Imaging.ImageLockMode.ReadWrite,
                windowsBitmap.PixelFormat);
            IntPtr scan0  = bitmapData1.Scan0;
            int    stride = bitmapData1.Stride;

            //TODO: review here
            //use buffer copy

            unsafe
            {
                //target
                TempMemPtr targetMemPtr = ActualBitmap.GetBufferPtr(actualImage);
                byte *     target       = (byte *)targetMemPtr.Ptr;
                int        startRowAt   = ((h - 1) * stride);
                byte *     src          = (byte *)scan0;
                for (int y = h; y > 0; --y)
                {
                    // byte* target = targetH + ((y - 1) * stride);
                    //System.Runtime.InteropServices.Marshal.Copy(
                    //      (IntPtr)src,//src
                    //      targetBuffer, startRowAt, stride);
                    MemMx.memcpy(target + startRowAt, src, stride);
                    startRowAt -= stride;
                    src        += stride;
                }
                targetMemPtr.Release();
                //////////////////////////////////////////////////////////////////
                //fixed (byte* targetH = &targetBuffer[0])
                //{
                //    byte* src = (byte*)scan0;
                //    for (int y = h; y > 0; --y)
                //    {
                //        byte* target = targetH + ((y - 1) * stride);
                //        for (int n = stride - 1; n >= 0; --n)
                //        {
                //            *target = *src;
                //            target++;
                //            src++;
                //        }
                //    }
                //}
            }
            windowsBitmap.UnlockBits(bitmapData1);
        }
 void SetupMaskPixelBlender(int width, int height)
 {
     //----------
     //same size
     _alphaBmp          = new ActualBitmap(width, height);
     _maskBufferPainter = AggPainter.Create(_alphaBmp, new PixelBlenderBGRA());
     _maskBufferPainter.Clear(Color.Black);
     //------------
     //draw glyph bmp to _alpha bmp
     //_maskBufferPainter.DrawImage(_glyphBmp, 0, 0);
     maskPixelBlender.SetMaskBitmap(_alphaBmp);
     maskPixelBlenderPerCompo.SetMaskBitmap(_alphaBmp);
 }
Exemplo n.º 4
0
        ActualBitmap GetTransformedBitmapNoInterpolation()
        {
            var    destCB     = new ActualBitmap(_destBounds.Width, _destBounds.Height);
            var    destWriter = new MyBitmapBlender(destCB);
            PointF ptInPlane  = new PointF();

            int    x1, y1;
            double dab, dbc, dcd, dda;

            int    rectWidth  = _destBounds.Width;
            int    rectHeight = _destBounds.Height;
            Vector ab_vec     = this.AB;
            Vector bc_vec     = this.BC;
            Vector cd_vec     = this.CD;
            Vector da_vec     = this.DA;
            int    rectLeft   = this._destBounds.Left;
            int    rectTop    = this._destBounds.Top;

            TempMemPtr bufferPtr = _srcBmp.GetBufferPtr();

            unsafe
            {
                BufferReader4 reader = new BufferReader4((int *)bufferPtr.Ptr, _srcBmp.Width, _srcBmp.Height);

                for (int y = 0; y < rectHeight; ++y)
                {
                    for (int x = 0; x < rectWidth; ++x)
                    {
                        PointF srcPt = new PointF(x, y);
                        srcPt.Offset(rectLeft, rectTop);
                        if (!IsOnPlaneABCD(srcPt))
                        {
                            continue;
                        }
                        x1 = (int)ptInPlane.X;
                        y1 = (int)ptInPlane.Y;

                        reader.SetStartPixel(x1, y1);
                        destWriter.SetPixel(x, y, reader.Read1());
                        //-------------------------------------
                        dab         = Math.Abs((MyVectorHelper.NewFromTwoPoints(_p0, srcPt)).CrossProduct(ab_vec));
                        dbc         = Math.Abs((MyVectorHelper.NewFromTwoPoints(_p1, srcPt)).CrossProduct(bc_vec));
                        dcd         = Math.Abs((MyVectorHelper.NewFromTwoPoints(_p2, srcPt)).CrossProduct(cd_vec));
                        dda         = Math.Abs((MyVectorHelper.NewFromTwoPoints(_p3, srcPt)).CrossProduct(da_vec));
                        ptInPlane.X = (float)(srcW * (dda / (dda + dbc)));
                        ptInPlane.Y = (float)(srcH * (dab / (dab + dcd)));
                    }
                }
                return(destCB);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// copy from actual image direct to hBmpScan0
 /// </summary>
 /// <param name="actualImage"></param>
 /// <param name="hBmpScan0"></param>
 public static void CopyToWindowsBitmapSameSize(
     ActualBitmap actualImage,
     IntPtr hBmpScan0)
 {
     //1st, fast
     //byte[] rawBuffer = ActualImage.GetBuffer(actualImage);
     unsafe
     {
         TempMemPtr memPtr = ActualBitmap.GetBufferPtr(actualImage);
         MemMx.memcpy((byte *)hBmpScan0, (byte *)memPtr.Ptr, actualImage.Stride * actualImage.Height);
         memPtr.Release();
     }
     //System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
     //   hBmpScan0, rawBuffer.Length);
 }
Exemplo n.º 6
0
        public void BindHostGraphics(Graphics hostControlGraphics)
        {
            g = hostControlGraphics;
            //
            destImg = new ActualBitmap(400, 300);
            p       = AggPainter.Create(destImg);
            winBmp  = new Bitmap(400, 300, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            _openFontTextServices = new LayoutFarm.OpenFontTextService();

            _printer    = new VxsTextPrinter(p, _openFontTextServices);
            _visualLine = new VisualLine();
            _visualLine.BindLine(_line);
            _visualLine.Y = 100;
        }
Exemplo n.º 7
0
        public override void Paint(PaintVisitor p)
        {
            if (Vxs != null)
            {
                //1.
                //convert vxs to bitmap
                //then render with bitmap cache**
                //or
                //2. convert vxs to path data

                if (backimg == null)
                {
                    var svgPart = new SvgPart(SvgRenderVxKind.Path);
                    svgPart.FillColor = fillColor;

                    svgPart.SetVxsAsOriginal(Vxs);
                    var svgVx = new SvgRenderVx(new SvgPart[] { svgPart });

                    if (svgVx != null && !svgVx.HasBitmapSnapshot)
                    {
                        var bounds = svgVx.GetBounds();
                        //create
                        backimg = new ActualBitmap((int)bounds.Width, (int)bounds.Height);
                        AggRenderSurface renderSurface = new AggRenderSurface(backimg);
                        AggPainter       painter       = new AggPainter(renderSurface);
                        svgVx.Render(painter);
                        svgVx.SetBitmapSnapshot(backimg);
                        //***
                    }
                }


                if (backimg != null)
                {
                    p.InnerCanvas.DrawImage(backimg, new RectangleF(0, 0, backimg.Width, backimg.Height));
                    return;
                }
            }

            if (fillColor.A > 0)
            {
                p.FillPath(this.myCachedPath, this.fillColor);
            }
            if (this.strokeColor.A > 0)
            {
                p.DrawPath(this.myCachedPath, this.strokeColor, this.ActualStrokeWidth);
            }
        }
Exemplo n.º 8
0
        void RenderWithMsdfImg(Typeface typeface, char testChar, float sizeInPoint)
        {
            painter.FillColor = PixelFarm.Drawing.Color.Black;
            //p.UseSubPixelRendering = chkLcdTechnique.Checked;
            painter.Clear(PixelFarm.Drawing.Color.White);
            //----------------------------------------------------
            var builder = new GlyphPathBuilder(typeface);

            builder.SetHintTechnique(_glyphRenderOptions.HintTechnique);

            //----------------------------------------------------
            builder.Build(testChar, sizeInPoint);
            //----------------------------------------------------
            var glyphToContour = new GlyphContourBuilder();
            var msdfGenPars    = new MsdfGenParams();

            builder.ReadShapes(glyphToContour);
            //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
            MsdfGenParams genParams = new MsdfGenParams();
            GlyphImage    glyphImg  = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams);

            ActualBitmap actualImg = ActualBitmap.CreateFromBuffer(glyphImg.Width, glyphImg.Height, glyphImg.GetImageBuffer());

            painter.DrawImage(actualImg, 0, 0);

            //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            //{
            //    var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
            //    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length);
            //    bmp.UnlockBits(bmpdata);
            //    bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png");
            //}

            if (_contourAnalysisOpts.ShowGrid)
            {
                //render grid
                RenderGrids(800, 600, _gridSize, painter);
            }

            //6. use this util to copy image from Agg actual image to System.Drawing.Bitmap
            PixelFarm.CpuBlit.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(destImg, winBmp);
            //---------------
            //7. just render our bitmap
            g.Clear(Color.White);
            g.DrawImage(winBmp, new Point(30, 20));
        }
Exemplo n.º 9
0
        public MainPage()
        {
            canvasView = new SKCanvasView();
            canvasView.PaintSurface += OnCanvasViewPaintSurface;
            Content = canvasView;

            //1. create bitmap backend for our canvas
            _myCanvasBmp = new SKBitmap(800, 600);
            //2. create agg render surface and painter

            unsafe
            {
                int *ptr = (int *)_myCanvasBmp.GetAddr(0, 0);
                int  w   = _myCanvasBmp.Width;
                int  h   = _myCanvasBmp.Height;

                _actualBmp = new ActualBitmap(w, h, (IntPtr)ptr);
                _painter   = new AggPainter(new AggRenderSurface(_actualBmp));

                _needContentUpdate = true;
            }
        }
Exemplo n.º 10
0
        public GlyphImage CreateGlyphImage(GlyphPathBuilder builder, float pxscale)
        {
            //1. create

            var txToVxs = new GlyphTranslatorToVxs();

            builder.ReadShapes(txToVxs);
            //
            //create new one
            var glyphVxs = new VertexStore();

            txToVxs.WriteOutput(glyphVxs, pxscale);
            //find bound
            //--------------------------------------------
            //GlyphImage glyphImg = new GlyphImage()
            RectD bounds = RectD.ZeroIntersection;

            PixelFarm.CpuBlit.VertexProcessing.BoundingRect.GetBoundingRect(new VertexStoreSnap(glyphVxs), ref bounds);

            ////--------------------------------------------
            int w = (int)System.Math.Ceiling(bounds.Width);
            int h = (int)System.Math.Ceiling(bounds.Height);

            if (w < 5)
            {
                w = 5;
            }
            if (h < 5)
            {
                h = 5;
            }

            //translate to positive quadrant
            //
            double dx = (bounds.Left < 0) ? -bounds.Left : 0;
            double dy = (bounds.Bottom < 0) ? -bounds.Bottom : 0;

            //
            dx = Math.Ceiling(dx); //since we need to move it, then move it with integer value
            dy = Math.Ceiling(dy); //since we need to move it, then move it with integer value

            //we need some borders
            int horizontal_margin = 1;                         //'margin' 1px
            int vertical_margin   = 1;                         //margin 1 px

            dx += horizontal_margin;                           //+ left margin
            dy += vertical_margin;                             //+ top margin
                                                               //--------------------------------------------
                                                               //create glyph img
            w = (int)Math.Ceiling(dx + w + horizontal_margin); //+right margin
            h = (int)Math.Ceiling(dy + h + vertical_margin);   //+bottom margin

            ActualBitmap img     = new ActualBitmap(w, h);
            AggPainter   painter = AggPainter.Create(img);

            if (TextureKind == TextureKind.StencilLcdEffect)
            {
                VertexStore vxs2 = new VertexStore();
                glyphVxs.TranslateToNewVxs(dx + 0.33f, dy, vxs2); //offset to proper x of subpixel rendering  ***
                glyphVxs = vxs2;
                //
                painter.UseSubPixelLcdEffect = true;

                //we use white glyph on black bg for this texture
                painter.Clear(Color.Black);
                painter.FillColor = Color.White;
                painter.Fill(glyphVxs);

                //apply sharpen filter
                //painter.DoFilter(new RectInt(0, h, w, 0), 2);
                //painter.DoFilter(new RectInt(0, h, w, 0), 2); //?
            }
            else
            {
                VertexStore vxs2 = new VertexStore();
                glyphVxs.TranslateToNewVxs(dx, dy, vxs2);
                glyphVxs = vxs2;

                painter.UseSubPixelLcdEffect = false;

                if (TextureKind == TextureKind.StencilGreyScale)
                {
                    painter.Clear(Color.Empty);
                    painter.FillColor = Color.Black;
                }
                else
                {
                    painter.Clear(BackGroundColor);
                    painter.FillColor = this.GlyphColor;
                }
                painter.Fill(glyphVxs);
            }
            //
            var glyphImage = new GlyphImage(w, h);

            glyphImage.TextureOffsetX = dx;
            glyphImage.TextureOffsetY = dy;
            glyphImage.SetImageBuffer(ActualBitmapExtensions.CopyImgBuffer(img, w), false);
            //copy data from agg canvas to glyph image
            return(glyphImage);
        }
Exemplo n.º 11
0
        void UpdateRenderOutput()
        {
            if (!_readyToRender)
            {
                return;
            }
            //
            if (g == null)
            {
                destImg = new ActualBitmap(800, 600);
                painter = AggPainter.Create(destImg);
                winBmp  = new Bitmap(destImg.Width, destImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                g       = this.CreateGraphics();

                painter.CurrentFont = new PixelFarm.Drawing.RequestFont("tahoma", 14);


                _textService = new LayoutFarm.OpenFontTextService();
                _textService.LoadFontsFromFolder("../../../TestFonts");

                _devVxsTextPrinter                   = new VxsTextPrinter(painter, _textService);
                _devVxsTextPrinter.ScriptLang        = _basicOptions.ScriptLang;
                _devVxsTextPrinter.PositionTechnique = Typography.TextLayout.PositionTechnique.OpenFont;
            }

            if (string.IsNullOrEmpty(this.txtInputChar.Text))
            {
                return;
            }

            //test option use be used with lcd subpixel rendering.
            //this demonstrate how we shift a pixel for subpixel rendering tech

            if (_contourAnalysisOpts.SetupPrinterLayoutForLcdSubPix)
            {
                //TODO: set lcd or not here
            }
            else
            {
                //TODO: set lcd or not here
            }

            //1. read typeface from font file
            TypographyTest.RenderChoice renderChoice = _basicOptions.RenderChoice;
            switch (renderChoice)
            {
            case TypographyTest.RenderChoice.RenderWithGdiPlusPath:
                //not render in this example
                //see more at ...
                break;

            case TypographyTest.RenderChoice.RenderWithTextPrinterAndMiniAgg:
            {
                //clear previous draw
                painter.Clear(PixelFarm.Drawing.Color.White);
                painter.UseSubPixelLcdEffect = _contourAnalysisOpts.LcdTechnique;
                painter.FillColor            = PixelFarm.Drawing.Color.Black;

                selectedTextPrinter                   = _devVxsTextPrinter;
                selectedTextPrinter.Typeface          = _basicOptions.Typeface;
                selectedTextPrinter.FontSizeInPoints  = _basicOptions.FontSizeInPoints;
                selectedTextPrinter.ScriptLang        = _basicOptions.ScriptLang;
                selectedTextPrinter.PositionTechnique = _basicOptions.PositionTech;

                selectedTextPrinter.HintTechnique  = _glyphRenderOptions.HintTechnique;
                selectedTextPrinter.EnableLigature = _glyphRenderOptions.EnableLigature;
                selectedTextPrinter.SimulateSlant  = _contourAnalysisOpts.SimulateSlant;

                //test print 3 lines
#if DEBUG
                GlyphDynamicOutline.dbugTestNewGridFitting    = _contourAnalysisOpts.EnableGridFit;
                GlyphDynamicOutline.dbugActualPosToConsole    = _contourAnalysisOpts.WriteFitOutputToConsole;
                GlyphDynamicOutline.dbugUseHorizontalFitValue = _contourAnalysisOpts.UseHorizontalFitAlignment;
#endif

                char[] printTextBuffer = this.txtInputChar.Text.ToCharArray();
                float  x_pos = 0, y_pos = 50;
                float  lineSpacingPx = selectedTextPrinter.FontLineSpacingPx;
                for (int i = 0; i < 1; ++i)
                {
                    selectedTextPrinter.DrawString(printTextBuffer, x_pos, y_pos);
                    y_pos -= lineSpacingPx;
                }


                //copy from Agg's memory buffer to gdi
                PixelFarm.CpuBlit.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(destImg, winBmp);
                g.Clear(Color.White);
                g.DrawImage(winBmp, new Point(10, 0));
            }
            break;

            //==============================================
            //render 1 glyph for debug and test
            case TypographyTest.RenderChoice.RenderWithMsdfGen:
            case TypographyTest.RenderChoice.RenderWithSdfGen:
            {
                char     testChar = this.txtInputChar.Text[0];
                Typeface typeFace = _basicOptions.Typeface;
                RenderWithMsdfImg(typeFace, testChar, _basicOptions.FontSizeInPoints);
            }
            break;

            case TypographyTest.RenderChoice.RenderWithMiniAgg_SingleGlyph:
            {
                selectedTextPrinter = _devVxsTextPrinter;
                //for test only 1 char
                RenderSingleCharWithMiniAgg(
                    _basicOptions.Typeface,
                    this.txtInputChar.Text[0],
                    _basicOptions.FontSizeInPoints);
            }
            break;

            default:
                throw new NotSupportedException();
            }
        }
 /// <summary>
 /// set mask image, please note that size of mask must be the same size of the dest buffer
 /// </summary>
 /// <param name="maskBmp"></param>
 public void SetMaskBitmap(ActualBitmap maskBmp)
 {
     //in this version
     //please note that size of mask must be the same size of the dest buffer
     _maskInnerBuffer = ActualBitmap.GetBuffer(maskBmp);
 }
Exemplo n.º 13
0
        public static void CopyFromGdiPlusBitmapSameSizeTo32BitsBuffer(
            Bitmap windowsBitmap,
            ActualBitmap actualImage)
        {
            int h = windowsBitmap.Height;
            int w = windowsBitmap.Width;
            //byte[] targetBuffer = ActualImage.GetBuffer(actualImage);
            TempMemPtr targetBufferPtr = ActualBitmap.GetBufferPtr(actualImage);
            BitmapData bitmapData1     = windowsBitmap.LockBits(
                new Rectangle(0, 0,
                              w,
                              h),
                System.Drawing.Imaging.ImageLockMode.ReadOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);           //read as 32 bits
            IntPtr scan0  = bitmapData1.Scan0;
            int    stride = bitmapData1.Stride;

            //test
            //in this version we decided that
            //Agg's image should use Big-endian bytes.

            //so we convert the byte order for

            unsafe
            {
                byte *targetH    = (byte *)targetBufferPtr.Ptr;
                int   startRowAt = ((h - 1) * stride);
                byte *src        = (byte *)scan0;
                for (int y = h; y > 0; --y)
                {
                    //System.Runtime.InteropServices.Marshal.Copy(
                    //      (IntPtr)src,//src
                    //      targetBuffer, startRowAt, stride);
                    MemMx.memcpy(targetH + startRowAt, src, stride);
                    startRowAt -= stride;
                    src        += stride;
                }

                //////////////////////////////////////////////////////////////////
                //fixed (byte* targetH = &targetBuffer[0])
                //{
                //    byte* src = (byte*)scan0;
                //    for (int y = h; y > 0; --y)
                //    {
                //        byte* target = targetH + ((y - 1) * stride); //start at first column of the current row

                //        for (int n = stride - 1; n >= 0;) //n steps
                //        {
                //            //*target = *src;
                //            //target++;
                //            //src++;

                //            //the win gdi+ is
                //            *(target + 2) = *src; //R, 0->2
                //            *(target + 1) = *(src + 1); //G 1->1
                //            *(target + 0) = *(src + 2); //B 2->0
                //            *(target + 3) = *(src + 3); //A 3->3

                //            //#if !RGBA
                //            //       //eg OpenGL,
                //            //       /// <summary>
                //            //        /// order b
                //            //        /// </summary>
                //            //        public const int B = 0;
                //            //        /// <summary>
                //            //        /// order g
                //            //        /// </summary>
                //            //        public const int G = 1;
                //            //        /// <summary>
                //            //        /// order b
                //            //        /// </summary>
                //            //        public const int R = 2;
                //            //        /// <summary>
                //            //        /// order a
                //            //        /// </summary>
                //            //        public const int A = 3;
                //            //#else
                //            //        //RGBA (Windows GDI+)

                //            //        /// <summary>
                //            //        /// order b
                //            //        /// </summary>
                //            //        public const int B = 2;
                //            //        /// <summary>
                //            //        /// order g
                //            //        /// </summary>
                //            //        public const int G = 1;
                //            //        /// <summary>
                //            //        /// order b
                //            //        /// </summary>
                //            //        public const int R = 0;
                //            //        /// <summary>
                //            //        /// order a
                //            //        /// </summary>
                //            //        public const int A = 3;
                //            //#endif

                //            target += 4;
                //            src += 4;
                //            //target++;
                //            //src++;
                //            n -= 4;
                //        }
                //    }
                //}
            }
            targetBufferPtr.Release();
            windowsBitmap.UnlockBits(bitmapData1);
        }
Exemplo n.º 14
0
        /////////////////////////////////////////////////////////////////////////////////////
        public static void CopyToGdiPlusBitmapSameSizeNotFlip(
            ActualBitmap actualImage,
            Bitmap bitmap)
        {
            //agg store image buffer head-down
            //when copy to window bmp we here to flip
            //style1: copy row by row *** (fastest)***
            {
                //System.GC.Collect();
                //System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
                //sss.Start();
                //for (int i = 0; i < 1000; ++i)
                //{
                int        h           = bitmap.Height;
                int        w           = bitmap.Width;
                BitmapData bitmapData1 = bitmap.LockBits(
                    new Rectangle(0, 0,
                                  w,
                                  h),
                    System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    bitmap.PixelFormat);
                IntPtr scan0  = bitmapData1.Scan0;
                int    stride = bitmapData1.Stride;
                //byte[] srcBuffer = ActualImage.GetBuffer(actualImage);
                TempMemPtr srcBufferPtr = ActualBitmap.GetBufferPtr(actualImage);
                unsafe
                {
                    //fixed (byte* bufferH = &srcBuffer[0])
                    byte *srcBufferH = (byte *)srcBufferPtr.Ptr;
                    {
                        byte *target     = (byte *)scan0;
                        int   startRowAt = 0;
                        for (int y = 0; y < h; ++y)
                        {
                            //byte* src = bufferH + ((y - 1) * stride);
                            byte *src = srcBufferH + startRowAt;
                            //System.Runtime.InteropServices.Marshal.Copy(
                            //   srcBuffer,//src
                            //   startRowAt,
                            //   (IntPtr)target,
                            //   stride);
                            MemMx.memcpy(target, src, stride);

                            startRowAt += stride;
                            target     += stride;
                        }
                    }
                }
                srcBufferPtr.Release();
                bitmap.UnlockBits(bitmapData1);
                //}
                //sss.Stop();
                //long ms = sss.ElapsedMilliseconds;
            }
            //-----------------------------------
            //style2: copy all, then flip again
            //{
            //    System.GC.Collect();
            //    System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
            //    sss.Start();
            //    for (int i = 0; i < 1000; ++i)
            //    {
            //        byte[] rawBuffer = ActualImage.GetBuffer(actualImage);
            //        var bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            //          System.Drawing.Imaging.ImageLockMode.ReadOnly,
            //         bitmap.PixelFormat);


            //        System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
            //            bmpdata.Scan0, rawBuffer.Length);

            //        bitmap.UnlockBits(bmpdata);
            //        bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            //    }

            //    sss.Stop();
            //    long ms = sss.ElapsedMilliseconds;
            //}
            //-----------------------------------

            //-----------------------------------
            //style3: copy row by row +
            //{
            //    System.GC.Collect();
            //    System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
            //    sss.Start();
            //    for (int i = 0; i < 1000; ++i)
            //    {
            //        int h = bitmap.Height;
            //        int w = bitmap.Width;
            //        BitmapData bitmapData1 = bitmap.LockBits(
            //                  new Rectangle(0, 0,
            //                      w,
            //                      h),
            //                      System.Drawing.Imaging.ImageLockMode.ReadWrite,
            //                      bitmap.PixelFormat);
            //        IntPtr scan0 = bitmapData1.Scan0;
            //        int stride = bitmapData1.Stride;
            //        byte[] buffer = ActualImage.GetBuffer(actualImage);
            //        unsafe
            //        {
            //            fixed (byte* bufferH = &buffer[0])
            //            {
            //                byte* target = (byte*)scan0;
            //                for (int y = h; y > 0; --y)
            //                {
            //                    byte* src = bufferH + ((y - 1) * stride);
            //                    for (int n = stride - 1; n >= 0; --n)
            //                    {
            //                        *target = *src;
            //                        target++;
            //                        src++;
            //                    }
            //                }
            //            }
            //        }
            //        bitmap.UnlockBits(bitmapData1);
            //    }
            //    sss.Stop();
            //    long ms = sss.ElapsedMilliseconds;
            //}
        }
 protected void Attach(ActualBitmap img)
 {
     Attach(img.Width, img.Height, img.BitDepth, ActualBitmap.GetBuffer(img), new PixelBlenderBGRA());
 }
Exemplo n.º 16
0
        //ActualBitmap GetTransformedBilinearInterpolation()
        //{
        //    //4 points sampling
        //    //weight between four point
        //    ActualBitmap destCB = new ActualBitmap(rect.Width, rect.Height);
        //    MyBitmapBlender destWriter = new MyBitmapBlender(destCB);
        //    PointF ptInPlane = new PointF();
        //    int x1, x2, y1, y2;
        //    double dab, dbc, dcd, dda;
        //    float dx1, dx2, dy1, dy2, dx1y1, dx1y2, dx2y1, dx2y2;
        //    int rectWidth = rect.Width;
        //    int rectHeight = rect.Height;
        //    Vector ab_vec = this.AB;
        //    Vector bc_vec = this.BC;
        //    Vector cd_vec = this.CD;
        //    Vector da_vec = this.DA;
        //    int rectLeft = this.rect.Left;
        //    int rectTop = this.rect.Top;



        //    for (int y = 0; y < rectHeight; ++y)
        //    {
        //        for (int x = 0; x < rectWidth; ++x)
        //        {
        //            PointF srcPt = new PointF(x, y);
        //            srcPt.Offset(rectLeft, rectTop);
        //            if (!IsOnPlaneABCD(srcPt))
        //            {
        //                continue;
        //            }
        //            //-------------------------------------
        //            dab = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p0, srcPt).CrossProduct(ab_vec));
        //            dbc = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p1, srcPt).CrossProduct(bc_vec));
        //            dcd = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p2, srcPt).CrossProduct(cd_vec));
        //            dda = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p3, srcPt).CrossProduct(da_vec));

        //            ptInPlane.X = (float)(srcW * (dda / (dda + dbc)));
        //            ptInPlane.Y = (float)(srcH * (dab / (dab + dcd)));
        //            x1 = (int)ptInPlane.X;
        //            y1 = (int)ptInPlane.Y;

        //            if (x1 >= 0 && x1 < srcW && y1 >= 0 && y1 < srcH)
        //            {
        //                //bilinear interpolation ***
        //                x2 = (x1 == srcW - 1) ? x1 : x1 + 1;
        //                y2 = (y1 == srcH - 1) ? y1 : y1 + 1;
        //                dx1 = ptInPlane.X - x1;
        //                if (dx1 < 0) dx1 = 0;
        //                dx1 = 1f - dx1;
        //                dx2 = 1f - dx1;
        //                dy1 = ptInPlane.Y - y1;
        //                if (dy1 < 0) dy1 = 0;
        //                dy1 = 1f - dy1;
        //                dy2 = 1f - dy1;
        //                dx1y1 = dx1 * dy1;
        //                dx1y2 = dx1 * dy2;
        //                dx2y1 = dx2 * dy1;
        //                dx2y2 = dx2 * dy2;
        //                //use 4 points


        //                Drawing.Color x1y1Color = srcCB.GetPixel(x1, y1);
        //                Drawing.Color x2y1Color = srcCB.GetPixel(x2, y1);
        //                Drawing.Color x1y2Color = srcCB.GetPixel(x1, y2);
        //                Drawing.Color x2y2Color = srcCB.GetPixel(x2, y2);
        //                float a = (x1y1Color.alpha * dx1y1) + (x2y1Color.alpha * dx2y1) + (x1y2Color.alpha * dx1y2) + (x2y2Color.alpha * dx2y2);
        //                float b = (x1y1Color.blue * dx1y1) + (x2y1Color.blue * dx2y1) + (x1y2Color.blue * dx1y2) + (x2y2Color.blue * dx2y2);
        //                float g = (x1y1Color.green * dx1y1) + (x2y1Color.green * dx2y1) + (x1y2Color.green * dx1y2) + (x2y2Color.green * dx2y2);
        //                float r = (x1y1Color.red * dx1y1) + (x2y1Color.red * dx2y1) + (x1y2Color.red * dx1y2) + (x2y2Color.red * dx2y2);
        //                destWriter.SetPixel(x, y, new Drawing.Color((byte)a, (byte)b, (byte)g, (byte)r));
        //            }
        //        }
        //    }
        //    return destCB;
        //}
        unsafe ActualBitmap GetTransformedBicubicInterpolation()
        {
            //4 points sampling
            //weight between four point
            PointF ptInPlane = new PointF();
            int    x1, x2, y1, y2;
            double dab, dbc, dcd, dda;
            //float dx1, dx2, dy1, dy2, dx1y1, dx1y2, dx2y1, dx2y2;
            int    destRectWidth  = _destBounds.Width;
            int    dectRectHeight = _destBounds.Height;
            Vector ab_vec         = this.AB;
            Vector bc_vec         = this.BC;
            Vector cd_vec         = this.CD;
            Vector da_vec         = this.DA;


            TempMemPtr    bufferPtr = _srcBmp.GetBufferPtr();
            BufferReader4 reader    = new BufferReader4((int *)bufferPtr.Ptr, _srcBmp.Width, _srcBmp.Height);

            ActualBitmap    destCB     = new ActualBitmap(_destBounds.Width, _destBounds.Height);
            MyBitmapBlender destWriter = new MyBitmapBlender(destCB);
            int             rectLeft   = this._destBounds.Left;
            int             rectTop    = this._destBounds.Top;

            //***
            PixelFarm.Drawing.Color[] colors = new PixelFarm.Drawing.Color[16];

            int srcW_lim = srcW - 2;
            int srcH_lim = srcH - 2;

            for (int y = 0; y < dectRectHeight; ++y)
            {
                for (int x = 0; x < destRectWidth; ++x)
                {
                    PointF srcPt = new PointF(x, y);
                    srcPt.Offset(rectLeft, 0);
                    if (!IsOnPlaneABCD(srcPt))
                    {
                        continue;
                    }
                    //-------------------------------------
                    dab         = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p0, srcPt).CrossProduct(ab_vec));
                    dbc         = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p1, srcPt).CrossProduct(bc_vec));
                    dcd         = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p2, srcPt).CrossProduct(cd_vec));
                    dda         = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p3, srcPt).CrossProduct(da_vec));
                    ptInPlane.X = (float)(srcW * (dda / (dda + dbc)));
                    ptInPlane.Y = (float)(srcH * (dab / (dab + dcd)));
                    x1          = (int)ptInPlane.X;
                    y1          = (int)ptInPlane.Y;
                    if (x1 >= 2 && x1 < srcW_lim &&
                        y1 >= 2 && y1 < srcH_lim)
                    {
                        reader.SetStartPixel(x1, y1);
                        //reader.Read16(pixelBuffer);
                        //do interpolate
                        //find src pixel and approximate
                        destWriter.SetPixel(x, y,

                                            GetApproximateColor_Bicubic(reader,
                                                                        colors,
                                                                        ptInPlane.X,
                                                                        ptInPlane.Y)); //TODO:review here blue switch to red channel
                    }
                }
                //newline
                // startLine += stride2;
                //targetPixelIndex = startLine;
            }

            bufferPtr.Release();
            //------------------------
            //System.Runtime.InteropServices.Marshal.Copy(
            //outputBuffer, 0,
            //bmpdata2.Scan0, outputBuffer.Length);
            //outputbmp.UnlockBits(bmpdata2);
            ////outputbmp.Save("d:\\WImageTest\\n_lion_bicubic.png");
            //return outputbmp;
            return(destCB);
        }
Exemplo n.º 17
0
        unsafe ActualBitmap GetTransformedBilinearInterpolation()
        {
            //4 points sampling
            //weight between four point
            ActualBitmap    destCB = new ActualBitmap(_destBounds.Width, _destBounds.Height);
            MyBitmapBlender destWriter = new MyBitmapBlender(destCB);
            PointF          ptInPlane = new PointF();
            int             x1, x2, y1, y2;
            double          dab, dbc, dcd, dda;
            float           dx1, dx2, dy1, dy2, dx1y1, dx1y2, dx2y1, dx2y2;
            int             rectWidth  = _destBounds.Width;
            int             rectHeight = _destBounds.Height;
            Vector          ab_vec     = this.AB;
            Vector          bc_vec     = this.BC;
            Vector          cd_vec     = this.CD;
            Vector          da_vec     = this.DA;
            int             rectLeft   = this._destBounds.Left;
            int             rectTop    = this._destBounds.Top;

            int srcW_lim = srcW - 1;
            int srcH_lim = srcH - 1;


            TempMemPtr    bufferPtr = _srcBmp.GetBufferPtr();
            BufferReader4 reader    = new BufferReader4((int *)bufferPtr.Ptr, _srcBmp.Width, _srcBmp.Height);


            for (int y = 0; y < rectHeight; ++y)
            {
                for (int x = 0; x < rectWidth; ++x)
                {
                    PointF srcPt = new PointF(x, y);
                    srcPt.Offset(rectLeft, rectTop);
                    if (!IsOnPlaneABCD(srcPt))
                    {
                        continue;
                    }
                    //-------------------------------------
                    dab = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p0, srcPt).CrossProduct(ab_vec));
                    dbc = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p1, srcPt).CrossProduct(bc_vec));
                    dcd = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p2, srcPt).CrossProduct(cd_vec));
                    dda = Math.Abs(MyVectorHelper.NewFromTwoPoints(_p3, srcPt).CrossProduct(da_vec));

                    ptInPlane.X = (float)(srcW * (dda / (dda + dbc)));
                    ptInPlane.Y = (float)(srcH * (dab / (dab + dcd)));
                    x1          = (int)ptInPlane.X;
                    y1          = (int)ptInPlane.Y;

                    if (x1 >= 0 && x1 < srcW_lim &&
                        y1 >= 0 && y1 < srcH_lim)
                    {
                        //x2 = (x1 == srcW - 1) ? x1 : x1 + 1;
                        //y2 = (y1 == srcH - 1) ? y1 : y1 + 1;

                        x2 = x1 + 1;
                        y2 = y1 + 1;

                        dx1 = ptInPlane.X - x1;
                        if (dx1 < 0)
                        {
                            dx1 = 0;
                        }
                        dx1 = 1f - dx1;
                        dx2 = 1f - dx1;
                        //
                        //
                        dy1 = ptInPlane.Y - y1;
                        if (dy1 < 0)
                        {
                            dy1 = 0;
                        }
                        dy1 = 1f - dy1;
                        dy2 = 1f - dy1;
                        //
                        //
                        dx1y1 = dx1 * dy1;
                        dx1y2 = dx1 * dy2;
                        dx2y1 = dx2 * dy1;
                        dx2y2 = dx2 * dy2;
                        //use 4 points

                        reader.SetStartPixel(x1, y1);


                        Drawing.Color x1y1Color;
                        Drawing.Color x2y1Color;
                        Drawing.Color x1y2Color;
                        Drawing.Color x2y2Color;

                        reader.Read4(out x1y1Color, out x2y1Color, out x1y2Color, out x2y2Color);

                        //Drawing.Color x1y1Color = srcCB.GetPixel(x1, y1);
                        //Drawing.Color x2y1Color = srcCB.GetPixel(x2, y1);
                        //Drawing.Color x1y2Color = srcCB.GetPixel(x1, y2);
                        //Drawing.Color x2y2Color = srcCB.GetPixel(x2, y2);
                        float a = (x1y1Color.alpha * dx1y1) + (x2y1Color.alpha * dx2y1) + (x1y2Color.alpha * dx1y2) + (x2y2Color.alpha * dx2y2);
                        float b = (x1y1Color.blue * dx1y1) + (x2y1Color.blue * dx2y1) + (x1y2Color.blue * dx1y2) + (x2y2Color.blue * dx2y2);
                        float g = (x1y1Color.green * dx1y1) + (x2y1Color.green * dx2y1) + (x1y2Color.green * dx1y2) + (x2y2Color.green * dx2y2);
                        float r = (x1y1Color.red * dx1y1) + (x2y1Color.red * dx2y1) + (x1y2Color.red * dx1y2) + (x2y2Color.red * dx2y2);
                        destWriter.SetPixel(x, y, new Drawing.Color((byte)a, (byte)r, (byte)g, (byte)b));
                    }
                }
            }
            return(destCB);
        }
Exemplo n.º 18
0
 public MyBitmapBlender(ActualBitmap img)
 {
     Attach(img);
 }
Exemplo n.º 19
0
 public void Fill(ActualBitmap img, int x, int y)
 {
     Fill((IBitmapSrc)img, x, y);
 }
 public void SetFilterImage(ActualBitmap filterImg)
 {
     _filterImage = filterImg;
 }