//namespace edu.toronto.psi.vincent.util { using System.Drawing; using System.Drawing.Imaging; /** * Class for reading images from files to multidimensional arrays * and writing images to files from multidimensional arrays. * * The supported image formats are those natively supported by C#, i.e. BMP, JPEG, GIF, PNG * * * Copyright (C) 2004 Vincent Cheung (vincent@psi.toronto.edu, http://www.psi.toronto.edu/~vincent/) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * @author Vincent Cheung * @version 1.0 08/03/04 */ public class ImageReaderWriter { /** * Reads an image from a file to a rectangular jagged array. * * @param filename the name of the image file * @return a rectangular jagged array holding the separate RGB components normalized between 0 and 1 */ public static double[][][] read(string filename) { // load the image from the file Bitmap image = (Bitmap)Image.FromFile(filename); Color pixel; int pixelVal; double[][][] samples = new double[image.Height][][]; for(int row = 0; row < image.Height; row++) { samples[row] = new double[image.Width][]; for(int col = 0; col < image.Width; col++) { samples[row][col] = new double[3]; pixel = image.GetPixel(col, row); pixelVal = pixel.ToArgb(); // pixel is AARRGGBB samples[row][col][0] = ((pixelVal & 0x00FF0000) >> 16) / 255.0; samples[row][col][1] = ((pixelVal & 0x0000FF00) >> 8) / 255.0; samples[row][col][2] = (pixelVal & 0x000000FF) / 255.0; } } return(samples); } // end read method /** * Reads a video sequence from a set of image files to a rectangular jagged array. * * The filename of the images are formatted as: * prefix + frame# + postfix * * @param prefix the prefix of the name of the image file * @param postfix the postfix of the name of the image file * @param numFrames the number of frames to read * @return a rectangular jagged array holding the separate RGB components normalized between 0 and 1 */ public static double[][][][] read(string prefix, string postfix, int numFrames) { if(numFrames < 1) return(null); double[][][][] samples = new double[numFrames][][][]; for(int t = 0; t < samples.Length; t++) samples[t] = read(prefix + t + postfix); return(samples); } // end read method /** * Reads an image from a file to a multidimensional array. * * @param filename the name of the image file * @return a multidimensional array holding the separate RGB components normalized between 0 and 1 */ public static double[, ,] readMD(string filename) { // load the image from the file Bitmap image = (Bitmap)Image.FromFile(filename); double[, ,] samples = new double[image.Height, image.Width, 3]; Color pixel; int pixelVal; for(int row = 0; row < image.Height; row++) { for(int col = 0; col < image.Width; col++) { pixel = image.GetPixel(col, row); pixelVal = pixel.ToArgb(); // pixel is AARRGGBB samples[row, col, 0] = ((pixelVal & 0x00FF0000) >> 16) / 255.0; samples[row, col, 1] = ((pixelVal & 0x0000FF00) >> 8) / 255.0; samples[row, col, 2] = (pixelVal & 0x000000FF) / 255.0; } } return(samples); } // end read method /** * Reads a video sequence from a set of image files to a multidimensional array. * * The filename of the images are formatted as: * prefix + frame# + postfix * * @param prefix the prefix of the name of the image file * @param postfix the postfix of the name of the image file * @param numFrames the number of frames to read */ public static double[, , ,] readMD(string prefix, string postfix, int numFrames) { if(numFrames < 1) return(null); // load the first image from the file to get the dimensions Bitmap image = (Bitmap)Image.FromFile(prefix + "0" + postfix); double[, , ,] samples = new double[numFrames, image.Height, image.Width, 3]; Color pixel; int pixelVal; for(int t = 0; t < numFrames; t++) { image = (Bitmap)Image.FromFile(prefix + t + postfix); for(int row = 0; row < image.Height; row++) { for(int col = 0; col < image.Width; col++) { pixel = image.GetPixel(col, row); pixelVal = pixel.ToArgb(); // pixel is AARRGGBB samples[t, row, col, 0] = ((pixelVal & 0x00FF0000) >> 16) / 255.0; samples[t, row, col, 1] = ((pixelVal & 0x0000FF00) >> 8) / 255.0; samples[t, row, col, 2] = (pixelVal & 0x000000FF) / 255.0; } } } return(samples); } // end read method /** * Writes an image to a file. * * @param samples a rectangular jagged array holding the separate RGB components normalized between 0 and 1 * @param filename the name of the image file */ public static void write(double[][][] samples, string filename) { Bitmap image = new Bitmap(samples.Length, samples[0].Length); for(int row = 0; row < image.Height; row++) for(int col = 0; col < image.Width; col++) image.SetPixel(col, row, Color.FromArgb((int)(samples[row][col][0] * 255), (int)(samples[row][col][1] * 255), (int)(samples[row][col][2] * 255))); image.Save(filename); } // end write method /** * Writes a video sequence as a set of images. * * The filename of the images are formatted as: * prefix + frame# + postfix * * @param samples a rectangular jagged array holding the separate RGB components normalized between 0 and 1, * where the first dimension indexes the frame and the remaining dimensions represent the frame image * @param prefix the prefix of the name of the image file * @param postfix the postfix of the name of the image file */ public static void write(double[][][][] samples, string prefix, string postfix) { for(int t = 0; t < samples.Length; t++) write(samples[t], prefix + t + postfix); } // end write method /** * Writes an image to a file. * * @param samples a multidimensional array holding the separate RGB components normalized between 0 and 1 * @param filename the name of the image file */ public static void write(double[, ,] samples, string filename) { Bitmap image = new Bitmap(samples.GetLength(1), samples.GetLength(0)); for(int row = 0; row < image.Height; row++) for(int col = 0; col < image.Width; col++) image.SetPixel(col, row, Color.FromArgb((int)(samples[row, col, 0] * 255), (int)(samples[row, col, 1] * 255), (int)(samples[row, col, 2] * 255))); image.Save(filename); } // end write method /** * Writes a video sequence as a set of images. * * The filename of the images are formatted as: * prefix + frame# + postfix * * @param samples a multidimensional array holding the separate RGB components normalized between 0 and 1, * where the first dimension indexes the frame and the remaining dimensions represent the frame image * @param prefix the prefix of the name of the image file * @param postfix the postfix of the name of the image file */ public static void write(double[, , ,] samples, string prefix, string postfix) { Bitmap image = new Bitmap(samples.GetLength(2), samples.GetLength(1)); for(int t = 0; t < samples.GetLength(0); t++) { for(int row = 0; row < image.Height; row++) for(int col = 0; col < image.Width; col++) image.SetPixel(col, row, Color.FromArgb((int)(samples[t, row, col, 0] * 255), (int)(samples[t, row, col, 1] * 255), (int)(samples[t, row, col, 2] * 255))); image.Save(prefix + t + postfix); } } // end write method } // end ImageReaderWriter class //} // end namespace