
00001 /*!@file Raster/GenericFrame.H Discriminated union of rgb, grayscale, floating-point, and video-yuv images */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Raster/GenericFrame.H $ 00035 // $Id: GenericFrame.H 11089 2009-04-05 13:00:36Z mviswana $ 00036 // 00037 00038 #ifndef RASTER_GENERICFRAME_H_DEFINED 00039 #define RASTER_GENERICFRAME_H_DEFINED 00040 00041 #include "Image/Image.H" 00042 #include "Image/Layout.H" 00043 #include "Image/Pixels.H" 00044 #include "Video/VideoFrame.H" 00045 #include "rutz/shared_ptr.h" 00046 00047 #include <string> 00048 #include <map> 00049 00050 struct GenericFrameSpec; 00051 00052 /// Discriminated union of rgb, grayscale, floating-point, and video-yuv images 00053 /** This class allows frame sources (raster files, movie clips, 00054 frame-grabbers, etc.) to return frames in their optimal native 00055 representation, while deferring to the caller the ability to 00056 subsequently convert the frame to any convenient type. Similarly, 00057 GenericFrame objects can be passed to frame destinations (raster 00058 files, movie files, onscreen windows) in their native format, 00059 incurring as few conversions as possible. */ 00060 class GenericFrame 00061 { 00062 public: 00063 enum NativeType 00064 { 00065 NONE = 0, ///< no image 00066 RGB_U8 = 1, ///< rgb color, 8 bits unsigned int per component 00067 RGB_F32 = 5, ///< rgb color, 32 bits floating point per component 00068 GRAY_U8 = 2, ///< grayscale, 8 bits unsigned int per pixel 00069 GRAY_F32 = 3, ///< grayscale, 32 bits floating point per pixel 00070 VIDEO = 4, 00071 GRAY_U16 = 6, ///< grayscale, 16 bits unsigned uint per pixel 00072 RGB_U16 = 7 ///< rgb color, 16 bits unsigned int per componet 00073 }; 00074 00075 /// MetaData for storing frame spacific information 00076 class MetaData 00077 { 00078 public: 00079 MetaData(); 00080 00081 virtual ~MetaData(); 00082 }; 00083 00084 /// Construct an empty frame 00085 GenericFrame(); 00086 00087 /// Construct an 8-bit integer rgb-native frame 00088 explicit GenericFrame(const Image<PixRGB<byte> >& rgbimg); 00089 00090 /// Construct an 16-bit integer rgb-native frame 00091 explicit GenericFrame(const Image<PixRGB<uint16> >& rgbimg); 00092 00093 /// Construct an 8-bit integer rgb-native frame from a layout 00094 explicit GenericFrame(const Layout<PixRGB<byte> >& rgbimg); 00095 00096 /// Construct a 32-bit floating point rgb-native frame 00097 /** @param flags a bitwise-or'ed combination of FLOAT_NORM_0_255, 00098 FLOAT_NORM_WITH_SCALE, and FLOAT_NORM_PRESERVE (see 00099 Image/Normalize.H). */ 00100 explicit GenericFrame(const Image<PixRGB<float> >& rgbimg, 00101 const int flags); 00102 00103 /// Construct an 8-bit integer grayscale-native frame 00104 explicit GenericFrame(const Image<byte>& grayimg); 00105 00106 /// Construct an 16-bit integer grayscale-native frame 00107 explicit GenericFrame(const Image<uint16>& grayimg); 00108 00109 /// Construct an 8-bit integer grayscale-native frame from a layout 00110 explicit GenericFrame(const Layout<byte>& grayimg); 00111 00112 /// Construct a 32-bit floating point grayscale-native frame 00113 /** @param flags a bitwise-or'ed combination of FLOAT_NORM_0_255, 00114 FLOAT_NORM_WITH_SCALE, and FLOAT_NORM_PRESERVE (see 00115 Image/Normalize.H). */ 00116 explicit GenericFrame(const Image<float>& floatimg, 00117 const int flags); 00118 00119 /// Construct an video-native frame 00120 explicit GenericFrame(const VideoFrame& vidframe); 00121 00122 // default destruct, assign, copy operators OK 00123 00124 /// Check if we have a valid non-empty image, regardless of format 00125 bool initialized() const; 00126 00127 /// Make a new GenericFrame object with an internal private copy of any transient data 00128 static GenericFrame deepCopyOf(const GenericFrame& f); 00129 00130 /// Get the frame's specifications 00131 GenericFrameSpec frameSpec() const; 00132 00133 /// Get the frame's native image type 00134 NativeType nativeType() const { return itsNativeType; } 00135 00136 /// Get the frame's native image typename 00137 std::string nativeTypeName() const; 00138 00139 /// Get the dimensions of the native image 00140 Dims getDims() const; 00141 00142 /// Get the width of the native image 00143 int getWidth() const { return this->getDims().w(); } 00144 00145 /// Get the height of the native image 00146 int getHeight() const { return this->getDims().h(); } 00147 00148 /// Get the float-normalization flags 00149 int floatFlags() const { return itsFloatFlags; } 00150 00151 /// Change the float-normalization flags 00152 void setFloatFlags(int v); 00153 00154 /// Get an 8-bit integer rgb version of the frame, converting as necessary 00155 Image<PixRGB<byte> > asRgbU8() const { return this->asRgbU8Layout().render(); } 00156 Image<PixRGB<byte> > asRgb() const { return this->asRgbU8Layout().render(); } 00157 00158 // Get a 16-bit interger rgb version of the frame, converting as necessary 00159 Image<PixRGB<uint16> > asRgbU16() const; 00160 00161 /// Get an 8-bit integer rgb layout version of the frame, converting as necessary 00162 const Layout<PixRGB<byte> >& asRgbU8Layout() const; 00163 00164 /// Get a 32-bit float rgb version of the frame, converting as necessary 00165 Image<PixRGB<float> > asRgbF32() const; 00166 00167 /// Get an 8-bit integer grayscale version of the frame, converting as necessary 00168 Image<byte> asGrayU8() const { return this->asGrayU8Layout().render(); } 00169 Image<byte> asGray() const { return this->asGrayU8Layout().render(); } 00170 00171 // Get a 16-bit interger grayscale version of the frame, converting as necessary 00172 Image<uint16> asGrayU16() const; 00173 00174 /// Get an 8-bit integer grayscale layout version of the frame, converting as necessary 00175 const Layout<byte>& asGrayU8Layout() const; 00176 00177 /// Get an 8-bit integer grayscale NTSC layout version of the frame, converting as necessary 00178 const Layout<byte>& asGrayU8NTSCLayout() const; 00179 00180 /// Get a 32-bit float grayscale version of the frame, converting as necessary 00181 Image<float> asGrayF32() const; 00182 Image<float> asFloat() const { return this->asGrayF32(); } 00183 00184 /// Convert the image from rgb to luminance in NTSC coordinates 00185 Image<byte> asGrayNTSC() const { return this->asGrayU8NTSCLayout().render();} 00186 /// Get a video version of the frame, converting as necessary 00187 VideoFrame asVideo() const; 00188 00189 //Metadata support functions 00190 00191 /// Check whether we have a given piece of metadata 00192 bool hasMetaData(const std::string& tag) const; 00193 00194 /// Get a given piece of metadata or throw an exception if it doesn't exist 00195 rutz::shared_ptr<MetaData> getMetaData(const std::string& tag); 00196 00197 /// Insert a given piece of metadata, overwriting any previous metadata associated with that tag 00198 void addMetaData(const std::string& tag, rutz::shared_ptr<MetaData> d); 00199 00200 private: 00201 // NOTE: this implementation has sizeof(GenericFrame)==108 on a 00202 // 32-bit machine; it may be possible to optimize this if necessary 00203 // (at a potentially slight cost in runtime efficiency), by storing 00204 // pointers to dynamically-allocated objects for each of the image 00205 // types instead of storing them by value here 00206 00207 NativeType itsNativeType; 00208 mutable Layout<PixRGB<byte> > itsRgbU8; 00209 mutable Image<PixRGB<uint16> > itsRgbU16; 00210 mutable Image<PixRGB<float> > itsRgbF32; 00211 mutable Layout<byte> itsGrayU8; 00212 mutable Image<uint16> itsGrayU16; 00213 mutable Image<float> itsGrayF32; 00214 mutable VideoFrame itsVideo; 00215 int itsFloatFlags; 00216 00217 typedef std::map<std::string, rutz::shared_ptr<MetaData> > MetaDataMap; 00218 rutz::shared_ptr<MetaDataMap> itsMetaDataMap; 00219 00220 }; 00221 00222 /// Gives specification of a GenericFrame 00223 struct GenericFrameSpec 00224 { 00225 GenericFrameSpec(); 00226 00227 GenericFrame::NativeType nativeType; 00228 VideoFormat videoFormat; // applies only if nativeType == VIDEO 00229 bool videoByteSwap; // applies only if nativeType == VIDEO 00230 Dims dims; 00231 int floatFlags; 00232 float frameRate ; 00233 00234 bool operator==(const GenericFrameSpec& that) const; 00235 00236 bool operator!=(const GenericFrameSpec& that) const 00237 { return !(this->operator==(that)); } 00238 00239 /// Return a human-readable description of this frame spec 00240 std::string getDescription() const; 00241 00242 /// Get the frame's native image typename 00243 std::string nativeTypeName() const; 00244 00245 /// Get the VideoFormat that would result from applying asVideo() to a corresponding GenericFrame 00246 VideoFormat getActualVideoFormat() const; 00247 }; 00248 00249 /// Check equality of two GenericFrame objects, return true if they are identical 00250 /** If the objects are of the same native type (and same video mode, 00251 if the native type is video), then we check equality of the native 00252 images; otherwise, we convert both images to RGB_F32 and compare 00253 the resulting images. */ 00254 bool operator==(const GenericFrame& f1, const GenericFrame& f2); 00255 00256 // ###################################################################### 00257 /* So things look consistent in everyone's emacs... */ 00258 /* Local Variables: */ 00259 /* mode: c++ */ 00260 /* indent-tabs-mode: nil */ 00261 /* End: */ 00262 00263 #endif // RASTER_GENERICFRAME_H_DEFINED
1.4.4