Example #1
0
        public override void DrawImage(ActualImage actualImage, double x, double y)
        {
            GLBitmap glBmp = new GLBitmap(actualImage.Width, actualImage.Height, ActualImage.GetBuffer(actualImage), false);

            _canvas.DrawImage(glBmp, (float)x, (float)y);
            glBmp.Dispose();
        }
Example #2
0
        public override void DrawImage(ActualImage actualImage, params AffinePlan[] affinePlans)
        {
            //create gl bmp
            GLBitmap glBmp = new GLBitmap(actualImage.Width, actualImage.Height, ActualImage.GetBuffer(actualImage), false);

            _canvas.DrawImage(glBmp, 0, 0);
            glBmp.Dispose();
        }
        void BlendScanline(ActualImage destImg, byte[] expandGreyBuffer,
                           PixelFarm.Drawing.Color color, int x, int y, int width)

        {
            byte[] rgb = new byte[3] {
                color.R,
                color.G,
                color.B
            };
            //-------------------------
            //destination
            byte[] destImgBuffer = ActualImage.GetBuffer(destImg);
            //start pixel
            int destImgIndex = (x * 4) + (destImg.Stride * y);
            //start img src
            int  srcImgIndex = x + (width * y);
            int  colorIndex  = 0;
            int  round       = 0;
            byte color_a     = color.alpha;

            while (width > 3)
            {
                //try
                //{

                int a0 = expandGreyBuffer[srcImgIndex] * color_a;
                int a1 = expandGreyBuffer[srcImgIndex + 1] * color_a;
                int a2 = expandGreyBuffer[srcImgIndex + 2] * color_a;

                byte ec0 = destImgBuffer[destImgIndex];     //existing color
                byte ec1 = destImgBuffer[destImgIndex + 1]; //existing color
                byte ec2 = destImgBuffer[destImgIndex + 2]; //existing color

                //------------------------------------------------------
                //please note that we swap a2 and a0 on the fly****
                //------------------------------------------------------
                byte n0 = (byte)((((rgb[colorIndex] - ec0) * a2) + (ec0 << 16)) >> 16);
                byte n1 = (byte)((((rgb[colorIndex + 1] - ec1) * a1) + (ec1 << 16)) >> 16);
                byte n2 = (byte)((((rgb[colorIndex + 2] - ec2) * a0) + (ec2 << 16)) >> 16);

                destImgBuffer[destImgIndex]     = n0;
                destImgBuffer[destImgIndex + 1] = n1;
                destImgBuffer[destImgIndex + 2] = n2;

                destImgIndex += 4;
                round         = 0;
                colorIndex    = 0;
                srcImgIndex  += 3;
                width        -= 3;

                //}
                //catch (Exception ex)
                //{

                //}
            }
        }
Example #4
0
 /// <summary>
 /// copy from actual image direct to hBmpScan0
 /// </summary>
 /// <param name="actualImage"></param>
 /// <param name="hBmpScan0"></param>
 public static void CopyToWindowsBitmapSameSize(
     ActualImage actualImage,
     IntPtr hBmpScan0)
 {
     //1st, fast
     int[] rawBuffer = ActualImage.GetBuffer(actualImage);
     System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
                                                 hBmpScan0, rawBuffer.Length);
 }
Example #5
0
        public static void CopyFromWindowsBitmapSameSize(
            Bitmap windowsBitmap,
            ActualImage actualImage)
        {
            int h = windowsBitmap.Height;
            int w = windowsBitmap.Width;

            byte[]     buffer      = actualImage.GetBuffer();
            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;

            //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++;
            //            }
            //        }
            //    }
            //}
            unsafe
            {
                //target
                fixed(byte *targetH = &buffer[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);
        }
Example #6
0
        public override void DrawImage(ActualImage actualImage, double x, double y)
        {
            //create Gdi bitmap from actual image
            int w = actualImage.Width;
            int h = actualImage.Height;

            switch (actualImage.PixelFormat)
            {
            case Agg.PixelFormat.ARGB32:
            {
                using (SKBitmap newBmp = new SKBitmap(actualImage.Width, actualImage.Height))
                {
                    newBmp.LockPixels();
                    byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);
                    System.Runtime.InteropServices.Marshal.Copy(
                        actualImgBuffer,
                        0,
                        newBmp.GetPixels(),
                        actualImgBuffer.Length);
                    newBmp.UnlockPixels();
                }
                //newBmp.internalBmp.LockPixels();
                //byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);

                //System.Runtime.InteropServices.Marshal.Copy(
                //     actualImgBuffer,
                //     0,
                //      newBmp.internalBmp.GetPixels(),
                //      actualImgBuffer.Length);

                //newBmp.internalBmp.UnlockPixels();
                //return newBmp;

                //copy data from acutal buffer to internal representation bitmap
                //using (MySkBmp bmp = MySkBmp.CopyFrom(actualImage))
                //{
                //    _skCanvas.DrawBitmap(bmp.internalBmp, (float)x, (float)y);
                //}
            }
            break;

            case Agg.PixelFormat.RGB24:
            {
            }
            break;

            case Agg.PixelFormat.GrayScale8:
            {
            }
            break;

            default:
                throw new NotSupportedException();
            }
        }
Example #7
0
        public static void CopyFromGdiPlusBitmapSameSize(
            Bitmap windowsBitmap,
            ActualImage 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
                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);
                    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);
                //        for (int n = stride - 1; n >= 0; --n)
                //        {
                //            *target = *src;
                //            target++;
                //            src++;
                //        }
                //    }
                //}
            }
            windowsBitmap.UnlockBits(bitmapData1);
        }
        public static void CopyToWindowsBitmap(ActualImage backingImageBufferByte,
                                               Bitmap windowsBitmap,
                                               RectInt rect)
        {
            int offset = 0;

            byte[] buffer = backingImageBufferByte.GetBuffer();
            BitmapHelper.CopyToWindowsBitmap(buffer, offset,
                                             backingImageBufferByte.Stride, backingImageBufferByte.Height,
                                             backingImageBufferByte.BitDepth,
                                             windowsBitmap, rect);
        }
        static byte[] CreateGreyScaleBuffer(ActualImage img)
        {
            //assume img is 32 rgba img
            int imgW   = img.Width;
            int height = img.Height;

            //56 level grey scale buffer

            byte[] srcImgBuffer       = ActualImage.GetBuffer(img);
            int    greyScaleBufferLen = imgW * height;

            byte[] greyScaleBuffer = new byte[greyScaleBufferLen];

            //for (int i = greyScaleBufferLen - 1; i >= 0; --i)
            //{
            //    greyScaleBuffer[i] = 64;
            //}


            int destIndex    = 0;
            int srcImgIndex  = 0;
            int srcImgStride = img.Stride;

            for (int y = 0; y < height; ++y)
            {
                srcImgIndex = srcImgStride * y;
                destIndex   = imgW * y;
                for (int x = 0; x < imgW; ++x)
                {
                    byte r = srcImgBuffer[srcImgIndex];
                    byte g = srcImgBuffer[srcImgIndex + 1];
                    byte b = srcImgBuffer[srcImgIndex + 2];
                    byte a = srcImgBuffer[srcImgIndex + 3];
                    if (r != 0 || g != 0 || b != 0)
                    {
                    }
                    if (a != 255)
                    {
                    }
                    //skip alpha
                    //byte greyScaleValue =
                    //    (byte)((0.333f * (float)r) + (0.5f * (float)g) + (0.1666f * (float)b));

                    greyScaleBuffer[destIndex] = (byte)(((a + 1) / 256f) * 64f);

                    destIndex++;
                    srcImgIndex += 4;
                }
            }
            return(greyScaleBuffer);
        }
Example #10
0
        static System.Drawing.Bitmap CreateBmpBRGA(ActualImage actualImage)
        {
            int w = actualImage.Width;
            int h = actualImage.Height;
            //copy data to bitmap
            //bgra
            var bmp = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            byte[] acutalBuffer = ActualImage.GetBuffer(actualImage);
            var    bmpData      = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

            System.Runtime.InteropServices.Marshal.Copy(acutalBuffer, 0, bmpData.Scan0, acutalBuffer.Length);
            bmp.UnlockBits(bmpData);
            return(bmp);
        }
Example #11
0
        protected override void DrawBoxContent(Canvas canvas, Rectangle updateArea)
        {
            //if (this.image != null)
            //{
            //    canvas.DrawImage(this.image,
            //        new RectangleF(0, 0, this.Width, this.Height));
            //}
            //else
            //{
            //when no image
            //---------------------

            // canvas.FillRectangle(Color.White, 0, 0, this.Width, this.Height);
            if (needUpdate)
            {
                //default bg => transparent !,

                //gfx2d.Clear(ColorRGBA.White);//if want opaque bg
                ReleaseUnmanagedResources();

                int j = sprites.Count;
                for (int i = 0; i < j; ++i)
                {
                    sprites[i].OnDraw(gfx2d);
                }

                //---------------------------------
                var buffer = actualImage.GetBuffer();
                this.bmp = canvas.Platform.CreatePlatformBitmap(this.Width, this.Height, this.actualImage.GetBuffer(), true);

                needUpdate = false;
            }
            canvas.DrawImage(this.bmp, new RectangleF(0, 0, this.Width, this.Height));
            //---------------------
            //copy data from actual image to canvas

            //}
#if DEBUG
            //canvasPage.dbug_DrawCrossRect(PixelFarm.Drawing.Color.Black,
            //    new Rectangle(0, 0, this.Width, this.Height));
#endif
        }
Example #12
0
        public override void DrawRect(CGRect dirtyRect)
        {
            if (!drawInit)
            {
                this.Window.Title = "PixelFarm";
                drawInit          = true;
                destImg           = new ActualImage(destImgW, destImgH, PixelFarm.Agg.PixelFormat.ARGB32);
                imgGfx2d          = new ImageGraphics2D(destImg);        //no platform
                p      = new AggCanvasPainter(imgGfx2d);
                stride = destImg.Stride;
                LoadGlyphs();
            }
            //base.Draw(rect);
            base.DrawRect(dirtyRect);


            p.Clear(Color.Yellow);
            p.FillColor = Color.Black;
            p.Fill(vxs);

            var            data     = Foundation.NSData.FromArray(ActualImage.GetBuffer(destImg));
            CGDataProvider provider = new CGDataProvider(data);

            using (var myImg2 = new CGImage(
                       destImgW, destImgH,
                       8, 32,
                       stride, CGColorSpace.CreateGenericRgb(),
                       CGBitmapFlags.PremultipliedLast,
                       provider,
                       null, true,
                       CGColorRenderingIntent.AbsoluteColorimetric))

                using (var nsGraphics = AppKit.NSGraphicsContext.CurrentContext)
                {
                    CGContext g      = nsGraphics.CGContext;
                    CGColor   color0 = new CGColor(1, 1, 1, 1);
                    g.SetFillColor(color0);
                    //g.ClearRect(new CGRect(0, 0, 800, 600));
                    //----------

                    CGColor color1 = new CGColor(1, 0, 0, 1);
                    g.SetFillColor(color1);

                    CGRect s1    = CGRect.FromLTRB(0, 0, 50, 50);
                    CGPath gpath = new CGPath();
                    gpath.AddRect(CGAffineTransform.MakeTranslation(20, 20), s1);
                    g.AddPath(gpath);
                    g.FillPath();


                    CGRect s2 = new CGRect(50, 50, destImgW, destImgH);
                    g.DrawImage(s2, myImg2);

                    //

                    //g.FillRect(s1);

                    CGColor color2 = new CGColor(0, 0, 1, 1);
                    g.SetFillColor(color2);
                    g.TranslateCTM(30, 30);


                    var strAttr = new CTStringAttributes
                    {
                        ForegroundColorFromContext = true,
                        Font = new CTFont("Arial", 24)
                    };


                    g.ScaleCTM(1, -1);            //flip
                    NSAttributedString a_str = new NSAttributedString("abcd", strAttr);
                    using (CTLine line = new CTLine(a_str))
                    {
                        line.Draw(g);
                    }


                    ////if (chkBorder.Checked)
                    ////{
                    ////	//5.4
                    ////	p.StrokeColor = PixelFarm.Drawing.Color.Green;
                    ////	//user can specific border width here...
                    ////	//p.StrokeWidth = 2;
                    ////	//5.5
                    ////	p.Draw(vxs);
                    ////}
                    ////6. use this util to copy image from Agg actual image to System.Drawing.Bitmap
                    //BitmapHelper.CopyToWindowsBitmap(destImg, winBmp, new RectInt(0, 0, 300, 300));
                    ////---------------
                    ////7. just render our bitmap
                    //g.ClearRect(rect);

                    //g.DrawImage(winBmp, new Point(10, 0));
                }



            //// scale and translate the CTM so the image appears upright
            //g.ScaleCTM (1, -1);
            //g.TranslateCTM (0, -Bounds.Height);
            //g.DrawImage (rect, UIImage.FromFile ("MyImage.png").CGImage);
            //// translate the CTM by the font size so it displays on screen
            //float fontSize = 35f;
            //g.TranslateCTM (0, fontSize);

            //// set general-purpose graphics state
            //g.SetLineWidth (1.0f);
            //g.SetStrokeColor (UIColor.Yellow.CGColor);
            //g.SetFillColor (UIColor.Red.CGColor);
            //g.SetShadow (new CGSize (5, 5), 0, UIColor.Blue.CGColor);

            //// set text specific graphics state
            //g.SetTextDrawingMode (CGTextDrawingMode.FillStroke);
            //g.SelectFont ("Helvetica", fontSize, CGTextEncoding.MacRoman);

            //// show the text
            //g.ShowText ("Hello Core Graphics");
        }
        void BlendWithLcdTechnique(ActualImage destImg, ActualImage glyphImg, PixelFarm.Drawing.Color color)
        {
            var g8Lut         = g8_1_2lcd;
            var forwardBuffer = new ScanlineSubPixelRasterizer.ForwardTemporaryBuffer();
            int glyphH        = glyphImg.Height;
            int glyphW        = glyphImg.Width;

            byte[] glyphBuffer = ActualImage.GetBuffer(glyphImg);
            int    srcIndex    = 0;
            int    srcStride   = glyphImg.Stride;

            byte[] destImgBuffer = ActualImage.GetBuffer(destImg);
            //start pixel
            int destImgIndex = 0;
            int destX        = 0;

            byte[] rgb = new byte[] {
                color.R,
                color.G,
                color.B
            };

            byte color_a = color.alpha;

            for (int y = 0; y < glyphH; ++y)
            {
                srcIndex     = srcStride * y;
                destImgIndex = (destImg.Stride * y) + (destX * 4); //4 color component
                int i     = 0;
                int round = 0;
                forwardBuffer.Reset();
                byte e0     = 0;
                int  prev_a = 0;

                for (int x = 0; x < glyphW; ++x)
                {
                    //1.
                    //read 1 pixel (4 bytes, 4 color components)
                    byte r = glyphBuffer[srcIndex];
                    byte g = glyphBuffer[srcIndex + 1];
                    byte b = glyphBuffer[srcIndex + 2];
                    byte a = glyphBuffer[srcIndex + 3];


                    //2.
                    //convert to grey scale and convert to 65 level grey scale value
                    byte greyScaleValue = g8Lut.Convert255ToLevel(a);

                    for (int n = 0; n < 3; ++n)
                    {
                        forwardBuffer.WriteAccumAndReadBack(
                            g8Lut.Tertiary(greyScaleValue),
                            g8Lut.Secondary(greyScaleValue),
                            g8Lut.Primary(greyScaleValue), out e0);

                        //5. blend this pixel to dest image (expand to 5 (sub)pixel)
                        BlendPixel(e0 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                    }
                    //------------------------------------------------------------
                    prev_a    = a;
                    srcIndex += 4;
                }
                //---------
                //when finish each line
                //we must draw extened 4 pixels
                //---------

                {
                    byte e1, e2, e3, e4;
                    forwardBuffer.ReadRemaining4(out e1, out e2, out e3, out e4);
                    int remainingEnergy = Math.Min(srcStride, 4);
                    switch (remainingEnergy)
                    {
                    default: throw new NotSupportedException();

                    case 4:
                        BlendPixel(e1 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e2 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e3 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e4 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        break;

                    case 3:
                        BlendPixel(e1 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e2 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e3 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        break;

                    case 2:
                        BlendPixel(e1 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        BlendPixel(e2 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        break;

                    case 1:
                        BlendPixel(e1 * color_a, rgb, ref i, destImgBuffer, ref destImgIndex, ref round);
                        break;

                    case 0:
                        //nothing
                        break;
                    }
                }
            }
        }
        unsafe internal static void CopyGlyphBitmap(NativeFontGlyph fontGlyph)
        {
            FT_Bitmap *ftBmp = (FT_Bitmap *)fontGlyph.nativeBmpPtr;
            //image is 8 bits grayscale
            int h      = ftBmp->rows;
            int w      = ftBmp->width;
            int stride = ftBmp->pitch;
            int size   = stride * h;

            //copy it to array
            //bmp glyph is bottom up
            //so .. invert it...

            byte[] buff = new byte[size];
            //------------------------------------------------
            byte *currentSrc = ftBmp->buffer;
            int   srcpos     = size;
            int   targetpos  = 0;

            for (int r = 1; r <= h; ++r)
            {
                srcpos    -= stride;
                currentSrc = ftBmp->buffer + srcpos;
                for (int c = 0; c < stride; ++c)
                {
                    buff[targetpos] = *(currentSrc + c);
                    targetpos++;
                }
            }

            ////------------------------------------------------
            //IntPtr bmpPtr = (IntPtr)ftBmp->buffer;
            //Marshal.Copy((IntPtr)ftBmp->buffer, buff, 0, size);
            ////------------------------------------------------

            //------------------------------------------------
            fontGlyph.glyImgBuffer8 = buff;
            //convert to 32bpp
            //make gray value as alpha channel color value
            ActualImage actualImage = new ActualImage(w, h, Agg.PixelFormat.ARGB32);

            byte[] newBmp32Buffer = ActualImage.GetBuffer(actualImage);
            int    src_p          = 0;
            int    target_p       = 0;

            for (int r = 0; r < h; ++r)
            {
                for (int c = 0; c < w; ++c)
                {
                    byte srcColor = buff[src_p + c];
                    //expand to 4 channel
                    newBmp32Buffer[target_p]     = 0;
                    newBmp32Buffer[target_p + 1] = 0;
                    newBmp32Buffer[target_p + 2] = 0;
                    newBmp32Buffer[target_p + 3] = srcColor; //A
                    target_p += 4;
                }
                src_p += stride;
            }
            fontGlyph.glyphImage32 = actualImage;
        }
Example #15
0
        /////////////////////////////////////////////////////////////////////////////////////

        public static void CopyToGdiPlusBitmapSameSize(
            ActualImage 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);
                unsafe
                {
                    fixed(byte *bufferH = &srcBuffer[0])
                    {
                        byte *target     = (byte *)scan0;
                        int   startRowAt = ((h - 1) * stride);

                        for (int y = h; y > 0; --y)
                        {
                            //byte* src = bufferH + ((y - 1) * stride);
                            System.Runtime.InteropServices.Marshal.Copy(
                                srcBuffer,//src
                                startRowAt,
                                (IntPtr)target,
                                stride);
                            startRowAt -= stride;
                            target     += stride;
                        }
                    }
                }
                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;
            //}
        }
Example #16
0
        unsafe internal static void CopyGlyphBitmap(FontGlyph fontGlyph, ExportGlyph *exportTypeFace)
        {
            FT_Bitmap *ftBmp = (FT_Bitmap *)exportTypeFace->bitmap;
            //image is 8 bits grayscale
            int h      = ftBmp->rows;
            int w      = ftBmp->width;
            int stride = ftBmp->pitch;
            int size   = stride * h;

            //copy it to array
            //bmp glyph is bottom up
            //so .. invert it...

            byte[] buff = new byte[size];
            //------------------------------------------------
            byte *currentSrc = ftBmp->buffer;
            int   srcpos     = size;
            int   targetpos  = 0;

            for (int r = 1; r <= h; ++r)
            {
                srcpos    -= stride;
                currentSrc = ftBmp->buffer + srcpos;
                for (int c = 0; c < stride; ++c)
                {
                    buff[targetpos] = *(currentSrc + c);
                    targetpos++;
                }
            }

            ////------------------------------------------------
            //IntPtr bmpPtr = (IntPtr)ftBmp->buffer;
            //Marshal.Copy((IntPtr)ftBmp->buffer, buff, 0, size);
            ////------------------------------------------------

            //------------------------------------------------
            fontGlyph.glyImgBuffer8 = buff;
            //convert to 32bpp
            //make gray value as alpha channel color value
            ActualImage actualImage = new ActualImage(w, h, Agg.Image.PixelFormat.Rgba32);
            int         newstride   = stride * 4;

            byte[] newBmp32Buffer = actualImage.GetBuffer();
            int    src_p          = 0;
            int    target_p       = 0;

            for (int r = 0; r < h; ++r)
            {
                for (int c = 0; c < w; ++c)
                {
                    byte srcColor = buff[src_p + c];
                    //expand to 4 channel
                    newBmp32Buffer[target_p]     = 0;        //R
                    newBmp32Buffer[target_p + 1] = 0;        //G
                    newBmp32Buffer[target_p + 2] = 0;        //B
                    newBmp32Buffer[target_p + 3] = srcColor; //A
                    target_p += 4;
                }
                src_p += stride;
            }
            fontGlyph.glyphImage32 = actualImage;
            ////------------------------------------------------
            //{
            //      //add System.Drawing to references
            //      //save image for debug***
            //
            //    byte[] buffer = new byte[size];
            //    Marshal.Copy((IntPtr)ftBmp->buffer, buffer, 0, size);
            //    ////save to
            //    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            //    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h),
            //        System.Drawing.Imaging.ImageLockMode.ReadWrite,
            //        bmp.PixelFormat);
            //    Marshal.Copy(buffer, 0, bmpdata.Scan0, size);
            //    bmp.UnlockBits(bmpdata);
            //    bmp.Save("d:\\WImageTest\\glyph.png");
            //}
        }