Exemplo n.º 1
0
        public static Bitmap ToBitmap(this Image image)
        {
            int         pixelSize = (int)image.getPixelSizeInBits() / 8;
            PixelFormat format    = ((pixelSize == 4) ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
            Bitmap      bitmap    = new Bitmap(image.s(), image.t(), format);

            using (ByteImage src = ByteImage.New(image))
                using (ByteBitmap dst = ByteBitmap.New(bitmap))
                {
                    for (int y = 0; y < dst.H; y++)
                    {
                        int srcpix = y * src.Stride;
                        int dstpix = y * dst.Stride;
                        for (int x = 0; x < dst.W; x++)
                        {
                            for (int c = 0; c < pixelSize; c++)
                            {
                                byte s = src[srcpix++];
                                dst[dstpix++] = s;
                            }
                        }
                    }
                    dst.Update();
                }
            return(bitmap);
        }
Exemplo n.º 2
0
        public static Image ToImage(this Bitmap bitmap)
        {
            int pixelSize;

            switch (bitmap.PixelFormat)
            {
            case PixelFormat.Format32bppArgb:
                pixelSize = 4;
                break;

            case PixelFormat.Format24bppRgb:
                pixelSize = 3;
                break;

            default:
                throw new NotImplementedException();
            }

            Image image = new Image();

            image.setAllocationMode(Image.AllocationMode.USE_NEW_DELETE);
            uint pixelFormat = ((pixelSize == 4) ? (uint)OsgModule.GL_RGBA : (uint)OsgModule.GL_RGB);

            image.allocateImage(bitmap.Width,
                                bitmap.Height,
                                1,
                                pixelFormat,
                                (uint)OsgModule.GL_UNSIGNED_BYTE);

            using (ByteBitmap src = ByteBitmap.New(bitmap))
                using (ByteImage dst = ByteImage.New(image))
                {
                    for (int y = 0; y < dst.H; y++)
                    {
                        int srcpix = y * src.Stride;
                        int dstpix = y * dst.Stride;
                        for (int x = 0; x < dst.W; x++)
                        {
                            for (int c = 0; c < pixelSize; c++)
                            {
                                byte s = src[srcpix++];
                                dst[dstpix++] = s;
                            }
                        }
                    }
                    dst.Update();
                }
            return(image);
        }