/// <summary> /// Loads an image from a stream. /// </summary> /// <remarks> /// This method allows loading an image from a stream, which is helpful for when /// images are being decompressed from an archive into a stream, which needs to be /// loaded as is. /// </remarks> /// <param name="stream">Stream serving as the data source.</param> /// <param name="type"> /// Type (i.e. file format) of image. Used to decide which image decompression codec to use. /// </param> public static Image FromStream(Stream stream, string type) { // find the codec for this file type ICodec codec = CodecManager.Instance.GetCodec(type); MemoryStream decoded = new MemoryStream(); ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode(stream, decoded); Image image = new Image(); // copy the image data image.height = data.height; image.width = data.width; image.depth = data.depth; image.format = data.format; image.flags = data.flags; image.numMipMaps = data.numMipMaps; // stuff the image data into an array byte[] buffer = new byte[decoded.Length]; decoded.Position = 0; decoded.Read(buffer, 0, buffer.Length); decoded.Close(); image.SetBuffer(buffer); return(image); }
/// <summary> /// Loads an image file from the file system. /// </summary> /// <param name="fileName">Full path to the image file on disk.</param> public static Image FromFile(string fileName) { int pos = fileName.LastIndexOf("."); if (pos == -1) { throw new AxiomException("Unable to load image file '{0}' due to invalid extension.", fileName); } // grab the extension from the filename string ext = fileName.Substring(pos + 1, fileName.Length - pos - 1); // find a registered codec for this type ICodec codec = CodecManager.Instance.GetCodec(ext); // TODO: Need ArchiveManager Stream encoded = ResourceManager.FindCommonResourceData(fileName); MemoryStream decoded = new MemoryStream(); // decode the image data ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode(encoded, decoded); Image image = new Image(); // copy the image data image.height = data.height; image.width = data.width; image.depth = data.depth; image.format = data.format; image.flags = data.flags; image.numMipMaps = data.numMipMaps; // stuff the image data into an array byte[] buffer = new byte[decoded.Length]; decoded.Position = 0; decoded.Read(buffer, 0, buffer.Length); decoded.Close(); image.SetBuffer(buffer); return(image); }
protected DecodeResult Decode( WriteableBitmap input ) { var img = new Image { Source = input }; var w = (int)ClosestPowerOfTwo( input.PixelWidth ); var h = (int)ClosestPowerOfTwo( input.PixelHeight ); var id = new ImageCodec.ImageData { width = w, height = h, depth = 1, size = 0, numMipMaps = -1, format = PixelFormat.BYTE_BGRA }; for ( int i = System.Math.Min( w, h ), s = w * h; i > 0; i >>= 1, s >>= 2, id.numMipMaps++ ) id.size += s; var bp = new byte[ id.size * 4 ]; var ofs = 0; #if DEBUGMIPMAPS var cval = new[] { 0xFFF00000, 0xFF00F100, 0xFF0000F2, 0xFFF3F300, 0xFF00F4F4, 0xFFF500F5, 0xFFF6F6F6 }; var cidx = 0; #endif while ( ofs < bp.Length ) { var wb = new WriteableBitmap( img, new ScaleTransform { ScaleX = ( (double)w ) / input.PixelWidth, ScaleY = ( (double)h ) / input.PixelHeight } ); wb.Invalidate(); #if DEBUGMIPMAPS var c=(int)cval[cidx%cval.Length]; for (var i = 0; i < wb.Pixels.Length; i++) wb.Pixels[i] = c; cidx++; #endif var len = w * h * 4; Buffer.BlockCopy( wb.Pixels, 0, bp, ofs, len ); ofs += len; w >>= 1; h >>= 1; } return new DecodeResult( new MemoryStream( bp ), id ); }
protected DecodeResult Decode( ExtendedImage input ) { var w = (int)ClosestPowerOfTwo( input.PixelWidth ); var h = (int)ClosestPowerOfTwo( input.PixelHeight ); var id = new ImageCodec.ImageData { width = w, height = h, depth = 1, size = 0, numMipMaps = -1, format = PixelFormat.A8B8G8R8 }; for ( int i = System.Math.Min( w, h ), s = w * h; i > 0; i >>= 1, s >>= 2, id.numMipMaps++ ) id.size += s; var bp = new byte[ id.size * 4 ]; var ofs = 0; #if DEBUGMIPMAPS var cval = new[] { 0xFFF00000, 0xFF00F100, 0xFF0000F2, 0xFFF3F300, 0xFF00F4F4, 0xFFF500F5, 0xFFF6F6F6 }; var cidx = 0; #endif while ( ofs < bp.Length ) { var wb = ExtendedImage.Resize( input, w, h, Resizer ); #if DEBUGMIPMAPS var c=(int)cval[cidx%cval.Length]; for (var i = 0; i < wb.Pixels.Length; i++) wb.Pixels[i] = c; cidx++; #endif var len = w * h * 4; Buffer.BlockCopy( wb.Pixels, 0, bp, ofs, len ); ofs += len; w >>= 1; h >>= 1; } return new DecodeResult( new MemoryStream( bp ), id ); }
public void Save(string fileName, PixelFormat requestedFormat) { // create a memory stream, setting the initial capacity MemoryStream bufferStream = new MemoryStream(width * height * 3); // save the data to the memory stream Save(bufferStream, requestedFormat); int pos = fileName.LastIndexOf('.'); // grab the file extension string extension = fileName.Substring(pos + 1); // grab the codec for the requested file extension ICodec codec = CodecManager.Instance.GetCodec(extension); // setup the image file information ImageCodec.ImageData imageData = new ImageCodec.ImageData(); imageData.width = width; imageData.height = height; imageData.format = requestedFormat; // reset the stream position bufferStream.Position = 0; // finally, save to file as an image codec.EncodeToFile(bufferStream, fileName, imageData); bufferStream.Close(); }
/// <summary> /// Saves the Image as a file /// </summary> /// <remarks> /// The codec used to save the file is determined by the extension of the filename passed in /// Invalid or unrecognized extensions will throw an exception. /// </remarks> /// <param name="filename">Filename to save as</param> public void Save( String filename ) { if ( this.buffer == null ) { throw new Exception( "No image data loaded" ); } String strExt = ""; int pos = filename.LastIndexOf( "." ); if ( pos == -1 ) throw new Exception( "Unable to save image file '" + filename + "' - invalid extension." ); while ( pos != filename.Length - 1 ) strExt += filename[ ++pos ]; ICodec pCodec = CodecManager.Instance.GetCodec( strExt ); if ( pCodec == null ) throw new Exception( "Unable to save image file '" + filename + "' - invalid extension." ); ImageCodec.ImageData imgData = new ImageCodec.ImageData(); imgData.format = Format; imgData.height = Height; imgData.width = Width; imgData.depth = Depth; imgData.size = Size; // Wrap memory, be sure not to delete when stream destroyed MemoryStream wrapper = new MemoryStream( buffer ); pCodec.EncodeToFile( wrapper, filename, imgData ); }