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)); }
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(); }
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)); }