public void Init(IntPtr rendererRef) { if (_isInitialized) { return; } _rendererRef = rendererRef; var bytes = System.Convert.FromBase64String(Tileset8x16Base64); IntPtr d = Marshal.AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, d, bytes.Length); IntPtr fromMem = SDL.SDL_RWFromMem(d, bytes.Length); var surf = SDL_image.IMG_Load_RW(fromMem, 1); _tileset = SDL.SDL_CreateTextureFromSurface(_rendererRef, surf); int start = 0; for (int i = start; i < 256; i++) { CharData cd = new CharData(); cd.X = (i % TilesetCharWidth); cd.Y = i / TilesetCharHeight; _charDataByChar.Add((char)i, cd); } _isInitialized = true; }
/// <summary> /// Loads image data from a given stream. /// </summary> /// <remarks> /// This is an extension of XNA 4 and is not compatible with XNA. It exists to help with dynamically reloading /// textures while games are running. Games can use this method to read a stream into memory and then call /// SetData on a texture with that data, rather than having to dispose the texture and recreate it entirely. /// </remarks> /// <param name="stream">The stream from which to read the image data.</param> /// <param name="width">Outputs the width of the image.</param> /// <param name="height">Outputs the height of the image.</param> /// <param name="pixels">Outputs the pixel data of the image, in non-premultiplied RGBA format.</param> public static void TextureDataFromStreamEXT(Stream stream, out int width, out int height, out byte[] pixels) { // Load the Stream into an SDL_RWops* byte[] mem = new byte[stream.Length]; stream.Read(mem, 0, mem.Length); IntPtr rwops = SDL.SDL_RWFromMem(mem, mem.Length); // Load the SDL_Surface* from RWops, get the image data IntPtr surface = SDL_image.IMG_Load_RW(rwops, 1); surface = INTERNAL_convertSurfaceFormat(surface); width = INTERNAL_getSurfaceWidth(surface); height = INTERNAL_getSurfaceHeight(surface); pixels = new byte[width * height * 4]; // MUST be SurfaceFormat.Color! Marshal.Copy(INTERNAL_getSurfacePixels(surface), pixels, 0, pixels.Length); /* Ensure that the alpha pixels are... well, actual alpha. * You think this looks stupid, but be assured: Your paint program is * almost certainly even stupider. * -flibit */ for (int i = 0; i < pixels.Length; i += 4) { if (pixels[i + 3] == 0) { pixels[i] = 0; pixels[i + 1] = 0; pixels[i + 2] = 0; } } }
internal override void TextureDataFromStream( Stream stream, out int width, out int height, out byte[] pixels ) { // Load the Stream into an SDL_RWops* byte[] mem = new byte[stream.Length]; GCHandle handle = GCHandle.Alloc(mem, GCHandleType.Pinned); stream.Read(mem, 0, mem.Length); IntPtr rwops = SDL.SDL_RWFromMem(mem, mem.Length); // Load the SDL_Surface* from RWops, get the image data IntPtr surface = SDL_image.IMG_Load_RW(rwops, 1); if (surface == IntPtr.Zero) { // File not found, supported, etc. width = 0; height = 0; pixels = null; return; } surface = INTERNAL_convertSurfaceFormat(surface); unsafe { SDL_Surface *surPtr = (SDL_Surface *)surface; width = surPtr->w; height = surPtr->h; pixels = new byte[width * height * 4]; // MUST be SurfaceFormat.Color! Marshal.Copy(surPtr->pixels, pixels, 0, pixels.Length); } SDL.SDL_FreeSurface(surface); handle.Free(); /* Ensure that the alpha pixels are... well, actual alpha. * You think this looks stupid, but be assured: Your paint program is * almost certainly even stupider. * -flibit */ for (int i = 0; i < pixels.Length; i += 4) { if (pixels[i + 3] == 0) { pixels[i] = 0; pixels[i + 1] = 0; pixels[i + 2] = 0; } } }
public unsafe static Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle) { #if SILICONSTUDIO_XENKO_UI_WINFORMS || SILICONSTUDIO_XENKO_UI_WPF using (var memoryStream = new UnmanagedMemoryStream((byte *)pSource, size)) using (var bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream)) { var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height); // Lock System.Drawing.Bitmap var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); var image = Image.New2D(bitmap.Width, bitmap.Height, 1, PixelFormat.B8G8R8A8_UNorm, 1, bitmapData.Stride); // var dataRect = new DataRectangle(bitmapData.Stride, bitmapData.Scan0); try { #if SILICONSTUDIO_XENKO_GRAPHICS_API_OPENGLES && SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP // Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension). image.Description.Format = PixelFormat.R8G8B8A8_UNorm; CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride); #else Utilities.CopyMemory(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride); #endif } finally { bitmap.UnlockBits(bitmapData); if (handle != null) { handle.Value.Free(); } else if (!makeACopy) { Utilities.FreeMemory(pSource); } } return(image); } #else #if SILICONSTUDIO_XENKO_UI_SDL // FIXME: Manu: The following beginning of code shows that we can read images using SDL. // FIXME: We will do the implementation logic later. IntPtr rw = SDL.SDL_RWFromMemNative((byte *)pSource, size); IntPtr image = SDL_image.IMG_Load_RW(rw, 1); #elif SILICONSTUDIO_XENKO_UI_OPENTK #endif return(null); #endif }
private static unsafe IntPtr CreateSurfaceFromStream(Stream stream) { var data = stream.ReadToEnd(); fixed(byte *pData = data) { var rwops = SDL.SDL_RWFromMem(data, data.Length); App.CheckSDLResult("SDL_RWFromMem", rwops); var surface = SDL_image.IMG_Load_RW(rwops, 1); App.CheckSDLResult("IMG_Load_RW", surface); return(surface); } }
public static SDLSurface Load_RW(IntPtr Src, bool Freesrc) { return(new SDLSurface(SDL_image.IMG_Load_RW(Src, Convert.ToInt32(Freesrc)))); }
public static IntPtr Load_RWPtr(IntPtr Src, bool Freesrc) { return(SDL_image.IMG_Load_RW(Src, Convert.ToInt32(Freesrc))); }
internal override void TextureDataFromStream( Stream stream, out int width, out int height, out byte[] pixels, int reqWidth = -1, int reqHeight = -1, bool zoom = false ) { // Load the Stream into an SDL_RWops* byte[] mem = new byte[stream.Length]; GCHandle handle = GCHandle.Alloc(mem, GCHandleType.Pinned); stream.Read(mem, 0, mem.Length); IntPtr rwops = SDL.SDL_RWFromMem(mem, mem.Length); // Load the SDL_Surface* from RWops, get the image data IntPtr surface = SDL_image.IMG_Load_RW(rwops, 1); handle.Free(); if (surface == IntPtr.Zero) { // File not found, supported, etc. width = 0; height = 0; pixels = null; return; } surface = INTERNAL_convertSurfaceFormat(surface); // Image scaling, if applicable if (reqWidth != -1 && reqHeight != -1) { // Get the file surface dimensions now... int rw; int rh; unsafe { SDL_Surface *surPtr = (SDL_Surface *)surface; rw = surPtr->w; rh = surPtr->h; } // Calculate the image scale factor bool scaleWidth; if (zoom) { scaleWidth = rw < rh; } else { scaleWidth = rw > rh; } float scale; if (scaleWidth) { scale = reqWidth / (float)rw; } else { scale = reqHeight / (float)rh; } // Calculate the scaled image size, crop if zoomed int resultWidth; int resultHeight; SDL.SDL_Rect crop = new SDL.SDL_Rect(); if (zoom) { resultWidth = reqWidth; resultHeight = reqHeight; if (scaleWidth) { crop.x = 0; crop.w = rw; crop.y = (int)(rh / 2 - (reqHeight / scale) / 2); crop.h = (int)(reqHeight / scale); } else { crop.y = 0; crop.h = rh; crop.x = (int)(rw / 2 - (reqWidth / scale) / 2); crop.w = (int)(reqWidth / scale); } } else { resultWidth = (int)(rw * scale); resultHeight = (int)(rh * scale); } // Alloc surface, blit! IntPtr newSurface = SDL.SDL_CreateRGBSurface( 0, resultWidth, resultHeight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_SetSurfaceBlendMode( surface, SDL.SDL_BlendMode.SDL_BLENDMODE_NONE ); if (zoom) { SDL.SDL_BlitScaled( surface, ref crop, newSurface, IntPtr.Zero ); } else { SDL.SDL_BlitScaled( surface, IntPtr.Zero, newSurface, IntPtr.Zero ); } SDL.SDL_FreeSurface(surface); surface = newSurface; } // Copy surface data to output managed byte array unsafe { SDL_Surface *surPtr = (SDL_Surface *)surface; width = surPtr->w; height = surPtr->h; pixels = new byte[width * height * 4]; // MUST be SurfaceFormat.Color! Marshal.Copy(surPtr->pixels, pixels, 0, pixels.Length); } SDL.SDL_FreeSurface(surface); /* Ensure that the alpha pixels are... well, actual alpha. * You think this looks stupid, but be assured: Your paint program is * almost certainly even stupider. * -flibit */ for (int i = 0; i < pixels.Length; i += 4) { if (pixels[i + 3] == 0) { pixels[i] = 0; pixels[i + 1] = 0; pixels[i + 2] = 0; } } }