//---------------------------------------------------------------- /** \brief Given an instance (of a subclass) of ImageData, construct * and return a clone of it. * * Note that this static function returns the appropriate subclass of * ImageData depending upon the type of image data (color or gray). * * \param other is the object to clone * \returns an instance of the ImageData class (actually the correct * subclass of ImageData because ImageData is abstract) */ static public ImageData clone(ImageData other) { if (other is GrayImageData) { GrayImageData copy = new GrayImageData(other.mOriginalData, other.mW, other.mH); copy.mIsAudio = other.mIsAudio; copy.mRate = other.mRate; return(copy); } if (other is ColorImageData) { ColorImageData copy = new ColorImageData(other.mOriginalData, other.mW, other.mH); copy.mIsAudio = other.mIsAudio; copy.mRate = other.mRate; return(copy); } return(null); }
//---------------------------------------------------------------- /** \brief Given the name of an input image file, this method * determines the type of image and then invokes the appropriate * constructor. * * Note that this static function returns the appropriate subclass of * ImageData depending upon the type of image data (color or gray). * * \param fileName name of input image file * \returns an instance of the ImageData class (actually the correct * subclass of ImageData because ImageData is abstract) */ static public ImageData load(String fileName) { Timer t = new Timer(); ImageData id = null; String up = fileName.ToUpper(); if (up.EndsWith(".PPM") || up.EndsWith(".PNM") || up.EndsWith(".PGM")) { //technique (trick? kludge?) to implement pass-by-reference int w, h, spp, min, max; int[] originalData = pnmHelper.read_pnm_file(fileName, out w, out h, out spp, out min, out max); t.print(); if (max > 255) { MessageBox.Show("Warning:\n\nMax value of " + max + " exceeds limit of 255."); } if (min < 0) { MessageBox.Show("Warning:\n\nMin value of " + min + " is less than 0."); } if (spp == 3) { id = new ColorImageData(originalData, w, h); id.mFname = fileName; Console.WriteLine(id); return(id); } if (spp == 1) { id = new GrayImageData(originalData, w, h); id.mFname = fileName; Console.WriteLine(id); return(id); } MessageBox.Show("Error:\n\n Cannot read this file!"); return(null); } if (up.EndsWith(".WAV")) { //basically treat audio wave files like 2d gray files (which are probably wide but not very high) int w, h, min, max, sampleRate; int[] originalData = wavHelper.read(fileName, out w, out h, out min, out max, out sampleRate); t.print(); if (max > 255) { MessageBox.Show("Warning:\n\nMax value of " + max + " exceeds limit of 255."); } if (min < 0) { MessageBox.Show("Warning:\n\nMin value of " + min + " is less than 0."); } id = new GrayImageData(originalData, w, h); id.mFname = fileName; id.mIsAudio = true; id.mRate = sampleRate; Console.WriteLine(id); return(id); } //here for image file types such as gif, jpg, bmp, etc. (but not ppm, pnm, pgm, or wav). //see http://www.bobpowell.net/lockingbits.htm for description of Bitmap pixel access. Bitmap bm = (Bitmap)Bitmap.FromFile(fileName, false); t.print(); switch (bm.PixelFormat) { case PixelFormat.Format1bppIndexed: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format1bppIndexed."); break; case PixelFormat.Format4bppIndexed: case PixelFormat.Format8bppIndexed: if (bm.PixelFormat == PixelFormat.Format4bppIndexed) { MessageBox.Show("Caution!\n\n This hasn't been tested on 4 bpp indexed images."); } //this is a bit tricky. each scalar value in the image is used as an index // into a table (Palette). Palette entries are rgb but if r==g==b, then this is // actually gray data. so let's first deteremine if this is color or gray. bool isGray = true; for (int i = 0; i < bm.Palette.Entries.Length; i++) { Color c = bm.Palette.Entries[i]; if (c.R == c.G && c.G == c.B) { continue; } isGray = false; break; } int bpp = 0; if (bm.PixelFormat == PixelFormat.Format8bppIndexed) { bpp = 8; } else if (bm.PixelFormat == PixelFormat.Format4bppIndexed) { bpp = 4; } if (isGray) { id = new GrayImageData(bm, bm.Palette.Entries, bpp); } else { id = new ColorImageData(bm, bm.Palette.Entries, bpp); } id.mFname = fileName; Console.WriteLine(id); return(id); case PixelFormat.Format16bppGrayScale: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format16bppGrayScale."); break; case PixelFormat.Format16bppRgb555: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format16bppRgb555."); break; case PixelFormat.Format16bppRgb565: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format16bppRgb565."); break; case PixelFormat.Format24bppRgb: case PixelFormat.Format32bppRgb: id = new ColorImageData(bm); id.mFname = fileName; Console.WriteLine(id); return(id); case PixelFormat.Format48bppRgb: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format48bppRgb."); break; case PixelFormat.Format16bppArgb1555: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format16bppArgb1555."); break; case PixelFormat.Format32bppArgb: id = new ColorImageData(bm); id.mFname = fileName; Console.WriteLine(id); return(id); case PixelFormat.Format64bppArgb: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format64bppArgb."); break; case PixelFormat.Format32bppPArgb: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format32bppPArgb."); break; case PixelFormat.Format64bppPArgb: MessageBox.Show("Sorry!\n\n Unsupported PixelFormat - Format64bppPArgb."); break; } MessageBox.Show("Error:\n\n Cannot read this file!"); return(null); }