using System.Drawing; /// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara /// /// Permission is hereby granted, free of charge, to any person obtaining /// a copy of this software and associated documentation files (the /// "Software"), to deal in the Software without restriction, including /// without limitation the rights to use, copy, modify, merge, publish, /// distribute, sublicense, and/or sell copies of the Software, and to /// permit persons to whom the Software is furnished to do so, subject to /// the following conditions: /// /// The above copyright notice and this permission notice shall be included /// in all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, /// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. /// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY /// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, /// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE /// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System.Numerics; using System.IO; using System.Media; using System.Windows.Media.Imaging; using ImageMagick; namespace WPFOpenGL { public static class BitmapManager { public static Bitmap CreateBitmap(float[] data, int width, int height) { Bitmap bitmap = new Bitmap(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int r = (int) data[i*height + j]; int g = (int) data[i*height + j]; int b = (int) data[i*height + j]; Color color = Color.FromArgb(r, g, b); bitmap.SetPixel(i, j, color); } } return bitmap; } private static float[] scale(float[] data, float newmin, float newmax) { float oldmin = float.MaxValue; float oldmax = float.MinValue; foreach (float val in data) { oldmin = val <= oldmin ? val : oldmin; oldmax = val >= oldmax ? val : oldmax; } float[] newdata = new float[data.Length]; for (int i = 0; i < data.Length; i++) { newdata[i] = scale(data[i], oldmin, oldmax, newmin, newmax); } return newdata; } public static float scale(float val, float oldmin, float oldmax, float newmin, float newmax) { return (newmax - newmin) * ((val - oldmin) / (oldmax - oldmin)) + newmin; } } public static class TextureManager { public static System.Drawing.Bitmap TextureTiff1(string filepath) { // MagickImage -> BitmapImage -> Bitmap (current) return BitmapImageToBitmap(BytesToBitmapImage(MagickImageToBytes(filepath))); } public static System.Drawing.Bitmap TextureTiff2(string filepath) { // FileStream -> BitmapSource -> Bitmap (alternative) return BitmapSourceToBitmap(BitmapSourceFromTiff(filepath)); } private static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource) { System.Drawing.Bitmap bitmap; using (MemoryStream outStream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapsource)); enc.Save(outStream); bitmap = new System.Drawing.Bitmap(outStream); } return bitmap; } private static BitmapSource BitmapSourceFromTiff(string filepath) { Stream imageStreamSource = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read); var decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; return bitmapSource; } public static byte[] MagickImageToBytes(string filepath) { using (var magickImage = new MagickImage(filepath)) { return magickImage.ToByteArray(); } } public static BitmapImage BytesToBitmapImage(byte[] bytes) { var bitmapImage = new BitmapImage(); using (MemoryStream stream = new MemoryStream(bytes)) { stream.Position = 0; bitmapImage.BeginInit(); bitmapImage.CreateOptions = BitmapCreateOptions.PreservePixelFormat; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.UriSource = null; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } return bitmapImage; } public static System.Drawing.Bitmap BitmapImageToBitmap(BitmapImage bitmapImage) { System.Drawing.Bitmap bitmap; using(MemoryStream stream = new MemoryStream()) { BitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapImage)); encoder.Save(stream); bitmap = new System.Drawing.Bitmap(stream); } return bitmap; } } }