//--------------------------------------------- // MakeAnaglyph.cs (c) 2007 by Charles Petzold //--------------------------------------------- using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; class MakeAnaglyph { [STAThread] public static void Main() { Environment.CurrentDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "WTC Photos"); // Left-Eye Bitmap. FileStream stream = new FileStream("East0.png", FileMode.Open); BitmapDecoder decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapFrame bitmap = decoder.Frames[0]; if (bitmap.Format != PixelFormats.Bgr32) { Console.WriteLine("Expecting format to be Bgr32"); return; } uint[] arrayLeft = new uint[bitmap.PixelWidth * bitmap.PixelHeight]; bitmap.CopyPixels(arrayLeft, bitmap.PixelWidth * 4, 0); stream.Close(); // Right-Eye Bitmap. stream = new FileStream("East3.png", FileMode.Open); decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.Default); bitmap = decoder.Frames[0]; if (bitmap.Format != PixelFormats.Bgr32) { Console.WriteLine("Expecting format to be Bgr32"); return; } uint[] arrayRight = new uint[bitmap.PixelWidth * bitmap.PixelHeight]; bitmap.CopyPixels(arrayRight, bitmap.PixelWidth * 4, 0); stream.Close(); if (arrayLeft.Length != arrayRight.Length) { Console.WriteLine("Expecting bitmaps to be the same size"); return; } // Combine pixel bits. uint[] arrayAnaglyph = new uint[arrayLeft.Length]; for (int i = 0; i < arrayAnaglyph.Length; i++) { arrayAnaglyph[i] = (arrayLeft[i] & 0xFF0000) | (arrayRight[i] & 0x0FFFF); } // Create new bitmap. BitmapSource bitmapAnaglyph = BitmapSource.Create(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Bgr32, null, arrayAnaglyph, bitmap.PixelWidth * 4); // Save bitmap. PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapAnaglyph)); stream = new FileStream("WTCeastAnaglyph.png", FileMode.Create); encoder.Save(stream); stream.Close(); // Display bitmap. Window win = new Window(); Image img = new Image(); img.Source = bitmapAnaglyph; img.Stretch = Stretch.None; win.Content = img; Application app = new Application(); app.Run(win); } }