Example #1
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
            {
                TempMemPtr targetPtr = ActualImage.GetBufferPtr(actualImage);
                //target

                int   startRowAt = ((h - 1) * stride);
                byte *src        = (byte *)scan0;

                byte *targetBuffer = (byte *)targetPtr.Ptr;
                for (int y = h; y > 0; --y)
                {
                    // byte* target = targetH + ((y - 1) * stride);

                    //System.Runtime.InteropServices.Marshal.Copy(
                    //      (IntPtr)src,//src
                    //      targetBuffer, startRowAt, stride);
                    AggMemMx.memcpy(targetBuffer + 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);
                //        for (int n = stride - 1; n >= 0; --n)
                //        {
                //            *target = *src;
                //            target++;
                //            src++;
                //        }
                //    }
                //}
            }
            windowsBitmap.UnlockBits(bitmapData1);
        }
        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

            TempMemPtr memPtr = ActualImage.GetBufferPtr(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;

            unsafe
            {
                byte *destImgBuffer = (byte *)memPtr.Ptr;
                while (width > 3)
                {
                    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;
                }
                memPtr.Release();
            }
        }
        static byte[] CreateGreyScaleBuffer(ActualImage img)
        {
            //assume img is 32 rgba img
            int imgW   = img.Width;
            int height = img.Height;
            //56 level grey scale buffer


            TempMemPtr srcMemPtr = ActualImage.GetBufferPtr(img);

            int greyScaleBufferLen = imgW * height;

            byte[] greyScaleBuffer = new byte[greyScaleBufferLen];

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

            unsafe
            {
                byte *srcImgBuffer = (byte *)srcMemPtr.Ptr;
                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;
                    }
                }
            }

            srcMemPtr.Release();
            return(greyScaleBuffer);
        }
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
     //byte[] rawBuffer = ActualImage.GetBuffer(actualImage);
     unsafe
     {
         TempMemPtr memPtr = ActualImage.GetBufferPtr(actualImage);
         AggMemMx.memcpy((byte *)hBmpScan0, (byte *)memPtr.Ptr, actualImage.Stride * actualImage.Height);
         memPtr.Release();
     }
     //System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
     //   hBmpScan0, rawBuffer.Length);
 }
Example #5
0
        /////////////////////////////////////////////////////////////////////////////////////

        public static void CopyToGdiPlusBitmapSameSize(
            ActualImage actualImage,
            SkiaSharp.SKBitmap skBmp)
        {
            //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 = skBmp.Height;
                int w = skBmp.Width;


                //BitmapData bitmapData1 = bitmap.LockBits(
                //          new Rectangle(0, 0,
                //              w,
                //              h),
                //              System.Drawing.Imaging.ImageLockMode.ReadWrite,
                //              bitmap.PixelFormat);
                skBmp.LockPixels();
                IntPtr scan0  = skBmp.GetPixels();
                int    stride = actualImage.Stride;
                //byte[] srcBuffer = ActualImage.GetBuffer(actualImage);
                unsafe
                {
                    TempMemPtr srcBufferPtr = ActualImage.GetBufferPtr(actualImage);
                    //fixed (byte* bufferH = &srcBuffer[0])
                    byte *bufferH = (byte *)srcBufferPtr.Ptr;
                    {
                        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);
                            AggMemMx.memcpy(target, bufferH + startRowAt, stride);
                            startRowAt -= stride;
                            target     += stride;
                        }
                    }
                    srcBufferPtr.Release();
                }
                skBmp.UnlockPixels();
                //}
                //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 #6
0
        public override void DrawImage(Image img, double left, double top)
        {
            if (img is ActualImage)
            {
                ActualImage actualImage = (ActualImage)img;
                //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);
                        TempMemPtr bufferPtr = ActualImage.GetBufferPtr(actualImage);
                        unsafe
                        {
                            byte *actualImgH = (byte *)bufferPtr.Ptr;
                            AggMemMx.memcpy((byte *)newBmp.GetPixels(), actualImgH, actualImage.Stride * actualImage.Height);
                            //System.Runtime.InteropServices.Marshal.Copy(
                            //    actualImgBuffer,
                            //    0,
                            //    newBmp.GetPixels(),
                            //    actualImgBuffer.Length);
                        }
                        bufferPtr.Release();
                        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 CopyFromGdiPlusBitmapSameSizeTo32BitsBuffer(
            Bitmap windowsBitmap,
            ActualImage actualImage)
        {
            int h = windowsBitmap.Height;
            int w = windowsBitmap.Width;
            //byte[] targetBuffer = ActualImage.GetBuffer(actualImage);
            TempMemPtr targetBufferPtr = ActualImage.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);
                    AggMemMx.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);
        }
Example #8
0
        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);


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

            unsafe
            {
                byte *newBmp32Buffer = (byte *)memPtr.Ptr;
                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;
                }
            }
            memPtr.Release();



            fontGlyph.glyphImage32 = actualImage;
        }