internal override void SavePNG( Stream stream, int width, int height, int imgWidth, int imgHeight, byte[] data ) { // Create an SDL_Surface*, write the pixel data IntPtr surface = SDL.SDL_CreateRGBSurface( 0, imgWidth, imgHeight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_LockSurface(surface); unsafe { SDL_Surface *surPtr = (SDL_Surface *)surface; Marshal.Copy( data, 0, surPtr->pixels, data.Length ); } SDL.SDL_UnlockSurface(surface); data = null; // We're done with the original pixel data. // Blit to a scaled surface of the size we want, if needed. if (width != imgWidth || height != imgHeight) { IntPtr scaledSurface = SDL.SDL_CreateRGBSurface( 0, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_BlitScaled( surface, IntPtr.Zero, scaledSurface, IntPtr.Zero ); SDL.SDL_FreeSurface(surface); surface = scaledSurface; } // Create an SDL_RWops*, save PNG to RWops const int pngHeaderSize = 41; const int pngFooterSize = 57; byte[] pngOut = new byte[ (width * height * 4) + pngHeaderSize + pngFooterSize + 256 // FIXME: Arbitrary zlib data padding for low-res images ]; // Max image size IntPtr dst = SDL.SDL_RWFromMem(pngOut, pngOut.Length); SDL_image.IMG_SavePNG_RW(surface, dst, 1); SDL.SDL_FreeSurface(surface); // We're done with the surface. // Get PNG size, write to Stream int size = ( (pngOut[33] << 24) | (pngOut[34] << 16) | (pngOut[35] << 8) | (pngOut[36]) ) + pngHeaderSize + pngFooterSize; stream.Write(pngOut, 0, size); }
public void SaveAsPng(Stream stream, int width, int height) { // Get the Texture2D pixels byte[] data = new byte[Width * Height * 4]; GetData(data); // Create an SDL_Surface*, write the pixel data IntPtr surface = SDL.SDL_CreateRGBSurface( 0, Width, Height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_LockSurface(surface); Marshal.Copy( data, 0, INTERNAL_getSurfacePixels(surface), data.Length ); SDL.SDL_UnlockSurface(surface); data = null; // We're done with the original pixel data. // Blit to a scaled surface of the size we want, if needed. if (width != Width || height != Height) { IntPtr scaledSurface = SDL.SDL_CreateRGBSurface( 0, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_BlitScaled( surface, IntPtr.Zero, scaledSurface, IntPtr.Zero ); SDL.SDL_FreeSurface(surface); surface = scaledSurface; } // Create an SDL_RWops*, save PNG to RWops byte[] pngOut = new byte[width * height * 4]; // Max image size IntPtr dst = SDL.SDL_RWFromMem(pngOut, pngOut.Length); SDL_image.IMG_SavePNG_RW(surface, dst, 1); SDL.SDL_FreeSurface(surface); // We're done with the surface. // Get PNG size, write to Stream int size = ( (pngOut[33] << 24) | (pngOut[34] << 16) | (pngOut[35] << 8) | (pngOut[36]) ) + 41 + 57; // 41 - header, 57 - footer stream.Write(pngOut, 0, size); }
public static void SavePNG(SDLSurface Surface, IntPtr Dst, bool Freedst) { Util.ThrowIfResultIsError(SDL_image.IMG_SavePNG_RW(Surface.Ptr, Dst, Convert.ToInt32(Freedst))); }