Example #1
0
        /// <summary>
        /// Loads the BitmapBuffer from a source buffer, which is expected to be the right pixel format
        /// </summary>
        public unsafe void LoadFrom(int width, int stride, int height, byte *data, BitmapLoadOptions options)
        {
            bool cleanup = options.CleanupAlpha0;

            Width  = width;
            Height = height;
            Pixels = new int[width * height];
            fixed(int *pPtr = &Pixels[0])
            {
                for (int idx = 0, y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        int src    = y * stride + x;
                        int srcVal = ((int *)data)[src];

                        //make transparent pixels turn into black to avoid filtering issues and other annoying issues with stray junk in transparent pixels
                        if (cleanup)
                        {
                            if ((srcVal & 0xFF000000) == 0)
                            {
                                srcVal = 0;
                            }
                            pPtr[idx++] = srcVal;
                        }
                    }
                }
            }

            if (options.Pad)
            {
                Pad();
            }
        }
Example #2
0
 /// <summary>
 /// Initializes the BitmapBuffer from a System.Drawing.Bitmap
 /// </summary>
 public BitmapBuffer(sd.Bitmap bitmap, BitmapLoadOptions options)
 {
     if (options.AllowWrap && bitmap.PixelFormat == PixelFormat.Format32bppArgb)
     {
         Width = bitmap.Width;
         Height = bitmap.Height;
         WrappedBitmap = bitmap;
     }
     else LoadInternal(null, bitmap, options);
 }
Example #3
0
        public unsafe BitmapBuffer ResolveTexture2d(Texture2d tex)
        {
            var tw   = tex.Opaque as TextureWrapper;
            var blow = new BitmapLoadOptions()
            {
                AllowWrap = false                 //must be an independent resource
            };
            var bb = new BitmapBuffer(tw.SDBitmap, blow);

            return(bb);
        }
Example #4
0
 /// <summary>
 /// Initializes the BitmapBuffer from a System.Drawing.Bitmap
 /// </summary>
 public BitmapBuffer(sd.Bitmap bitmap, BitmapLoadOptions options)
 {
     if (options.AllowWrap && bitmap.PixelFormat == PixelFormat.Format32bppArgb)
     {
         Width         = bitmap.Width;
         Height        = bitmap.Height;
         WrappedBitmap = bitmap;
     }
     else
     {
         LoadInternal(null, bitmap, options);
     }
 }
Example #5
0
        void LoadInternal(Stream stream, sd.Bitmap bitmap, BitmapLoadOptions options)
        {
            bool cleanup  = options.CleanupAlpha0;
            bool needsPad = true;

            var colorKey24bpp = options.ColorKey24bpp;

            using (Bitmap loadedBmp = bitmap == null ? new Bitmap(stream) : null)             //sneaky!
            {
                Bitmap bmp = loadedBmp;
                if (bmp == null)
                {
                    bmp = bitmap;
                }

                //if we have a 24bpp image and a colorkey callback, the callback can choose a colorkey color and we'll use that
                if (bmp.PixelFormat == PixelFormat.Format24bppRgb && colorKey24bpp != null)
                {
                    int colorKey = colorKey24bpp(bmp);
                    int w        = bmp.Width;
                    int h        = bmp.Height;
                    InitSize(w, h);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                    Color[]    palette = bmp.Palette.Entries;
                    int *      ptr     = (int *)bmpdata.Scan0.ToPointer();
                    int        stride  = bmpdata.Stride;
                    fixed(int *pPtr = &Pixels[0])
                    {
                        for (int idx = 0, y = 0; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                int srcPixel = ptr[idx];
                                if (srcPixel == colorKey)
                                {
                                    srcPixel = 0;
                                }
                                pPtr[idx++] = srcPixel;
                            }
                        }
                    }

                    bmp.UnlockBits(bmpdata);
                }
                if (bmp.PixelFormat == PixelFormat.Format8bppIndexed || bmp.PixelFormat == PixelFormat.Format4bppIndexed)
                {
                    int w = bmp.Width;
                    int h = bmp.Height;
                    InitSize(w, h);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                    Color[]    palette = bmp.Palette.Entries;
                    byte *     ptr     = (byte *)bmpdata.Scan0.ToPointer();
                    int        stride  = bmpdata.Stride;
                    fixed(int *pPtr = &Pixels[0])
                    {
                        for (int idx = 0, y = 0; y < h; y++)
                        {
                            for (int x = 0; x < w; x++)
                            {
                                int srcPixel = ptr[idx];
                                if (srcPixel != 0)
                                {
                                    int color = palette[srcPixel].ToArgb();

                                    //make transparent pixels turn into black to avoid filtering issues and other annoying issues with stray junk in transparent pixels.
                                    //(yes, we can have palette entries with transparency in them (PNGs support this, annoyingly))
                                    if (cleanup)
                                    {
                                        if ((color & 0xFF000000) == 0)
                                        {
                                            color = 0;
                                        }
                                        pPtr[idx] = color;
                                    }
                                }
                                idx++;
                            }
                        }
                    }

                    bmp.UnlockBits(bmpdata);
                }
                else
                {
                    //dump the supplied bitmap into our pixels array
                    int width  = bmp.Width;
                    int height = bmp.Height;
                    InitSize(width, height);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                    int *      ptr     = (int *)bmpdata.Scan0;
                    int        stride  = bmpdata.Stride / 4;
                    LoadFrom(width, stride, height, (byte *)ptr, options);
                    bmp.UnlockBits(bmpdata);
                    needsPad = false;
                }
            }

            if (needsPad && options.Pad)
            {
                Pad();
            }
        }
Example #6
0
 /// <summary>
 /// loads an image (png,bmp,etc) from the specified stream
 /// </summary>
 public BitmapBuffer(Stream stream, BitmapLoadOptions options)
 {
     LoadInternal(stream, null, options);
 }
Example #7
0
 /// <summary>
 /// Creates a BitmapBuffer image from the specified filename
 /// </summary>
 public BitmapBuffer(string fname, BitmapLoadOptions options)
 {
     using (var fs = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.Read))
         LoadInternal(fs, null, options);
 }
Example #8
0
 /// <summary>
 /// loads an image from the specified stream
 /// </summary>
 public BitmapBuffer(Stream stream, BitmapLoadOptions options)
 {
     LoadInternal(stream, null, options);
 }
Example #9
0
 /// <summary>
 /// Creates a BitmapBuffer image from the specified filename
 /// </summary>
 public BitmapBuffer(string fname, BitmapLoadOptions options)
 {
     using (var fs = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.Read))
         LoadInternal(fs, null, options);
 }
Example #10
0
        void LoadInternal(Stream stream, sd.Bitmap bitmap, BitmapLoadOptions options)
        {
            bool cleanup = options.CleanupAlpha0;
            bool needsPad = true;

            var colorKey24bpp = options.ColorKey24bpp;
            using (Bitmap loadedBmp = bitmap == null ? new Bitmap(stream) : null) //sneaky!
            {
                Bitmap bmp = loadedBmp;
                if (bmp == null)
                    bmp = bitmap;

                //if we have a 24bpp image and a colorkey callback, the callback can choose a colorkey color and we'll use that
                if (bmp.PixelFormat == PixelFormat.Format24bppRgb && colorKey24bpp != null)
                {
                    int colorKey = colorKey24bpp(bmp);
                    int w = bmp.Width;
                    int h = bmp.Height;
                    InitSize(w, h);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                    Color[] palette = bmp.Palette.Entries;
                    int* ptr = (int*)bmpdata.Scan0.ToPointer();
                    int stride = bmpdata.Stride;
                    fixed (int* pPtr = &Pixels[0])
                    {
                        for (int idx = 0, y = 0; y < h; y++)
                            for (int x = 0; x < w; x++)
                            {
                                int srcPixel = ptr[idx];
                                if (srcPixel == colorKey)
                                    srcPixel = 0;
                                pPtr[idx++] = srcPixel;
                            }
                    }

                    bmp.UnlockBits(bmpdata);
                }
                if (bmp.PixelFormat == PixelFormat.Format8bppIndexed || bmp.PixelFormat == PixelFormat.Format4bppIndexed)
                {
                    int w = bmp.Width;
                    int h = bmp.Height;
                    InitSize(w, h);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                    Color[] palette = bmp.Palette.Entries;
                    byte* ptr = (byte*)bmpdata.Scan0.ToPointer();
                    int stride = bmpdata.Stride;
                    fixed (int* pPtr = &Pixels[0])
                    {
                        for (int idx = 0, y = 0; y < h; y++)
                            for (int x = 0; x < w; x++)
                            {
                                int srcPixel = ptr[idx];
                                if (srcPixel != 0)
                                {
                                    int color = palette[srcPixel].ToArgb();

                                    //make transparent pixels turn into black to avoid filtering issues and other annoying issues with stray junk in transparent pixels.
                                    //(yes, we can have palette entries with transparency in them (PNGs support this, annoyingly))
                                    if (cleanup)
                                    {
                                        if ((color & 0xFF000000) == 0) color = 0;
                                        pPtr[idx] = color;
                                    }
                                }
                                idx++;
                            }
                    }

                    bmp.UnlockBits(bmpdata);
                }
                else
                {
                    //dump the supplied bitmap into our pixels array
                    int width = bmp.Width;
                    int height = bmp.Height;
                    InitSize(width, height);
                    BitmapData bmpdata = bmp.LockBits(new sd.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                    int* ptr = (int*)bmpdata.Scan0.ToInt32();
                    int stride = bmpdata.Stride / 4;
                    LoadFrom(width, stride, height, (byte*)ptr, options);
                    bmp.UnlockBits(bmpdata);
                    needsPad = false;
                }
            }

            if (needsPad && options.Pad)
                Pad();
        }
Example #11
0
        /// <summary>
        /// Loads the BitmapBuffer from a source buffer, which is expected to be the right pixel format
        /// </summary>
        public unsafe void LoadFrom(int width, int stride, int height, byte* data, BitmapLoadOptions options)
        {
            bool cleanup = options.CleanupAlpha0;
            Width = width;
            Height = height;
            Pixels = new int[width * height];
            fixed (int* pPtr = &Pixels[0])
            {
                for (int idx = 0, y = 0; y < Height; y++)
                    for (int x = 0; x < Width; x++)
                    {
                        int src = y * stride + x;
                        int srcVal = ((int*)data)[src];

                        //make transparent pixels turn into black to avoid filtering issues and other annoying issues with stray junk in transparent pixels
                        if (cleanup)
                        {
                            if ((srcVal & 0xFF000000) == 0) srcVal = 0;
                            pPtr[idx++] = srcVal;
                        }
                    }
            }

            if (options.Pad)
                Pad();
        }
Example #12
0
		public unsafe BitmapBuffer ResolveTexture2d(Texture2d tex)
		{
			var tw = tex.Opaque as TextureWrapper;
			var blow = new BitmapLoadOptions()
			{
				AllowWrap = false //must be an independent resource
			};
			var bb = new BitmapBuffer(tw.SDBitmap,blow); 
			return bb;
		}