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 14290 2010-12-01 21:44:03Z itti $ 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 RGBD = 8 ///< 8-bit rgb color plus 16-bit depth image 00074 }; 00075 00076 /// MetaData for storing frame spacific information 00077 class MetaData 00078 { 00079 public: 00080 MetaData(); 00081 00082 virtual ~MetaData(); 00083 }; 00084 00085 /// Construct an empty frame 00086 GenericFrame(); 00087 00088 /// Construct an 8-bit integer rgb-native frame 00089 explicit GenericFrame(const Image<PixRGB<byte> >& rgbimg); 00090 00091 /// Construct an 8-bit integer rgb-native frame plus 16-bit depth image 00092 explicit GenericFrame(const Image<PixRGB<byte> >& rgbimg, const Image<uint16>& dimg); 00093 00094 /// Construct an 16-bit integer rgb-native frame 00095 explicit GenericFrame(const Image<PixRGB<uint16> >& rgbimg); 00096 00097 /// Construct an 8-bit integer rgb-native frame from a layout 00098 explicit GenericFrame(const Layout<PixRGB<byte> >& rgbimg); 00099 00100 /// Construct a 32-bit floating point rgb-native frame 00101 /** @param flags a bitwise-or'ed combination of FLOAT_NORM_0_255, 00102 FLOAT_NORM_WITH_SCALE, and FLOAT_NORM_PRESERVE (see 00103 Image/Normalize.H). */ 00104 explicit GenericFrame(const Image<PixRGB<float> >& rgbimg, 00105 const int flags); 00106 00107 /// Construct an 8-bit integer grayscale-native frame 00108 explicit GenericFrame(const Image<byte>& grayimg); 00109 00110 /// Construct an 16-bit integer grayscale-native frame 00111 explicit GenericFrame(const Image<uint16>& grayimg); 00112 00113 /// Construct an 8-bit integer grayscale-native frame from a layout 00114 explicit GenericFrame(const Layout<byte>& grayimg); 00115 00116 /// Construct a 32-bit floating point grayscale-native frame 00117 /** @param flags a bitwise-or'ed combination of FLOAT_NORM_0_255, 00118 FLOAT_NORM_WITH_SCALE, and FLOAT_NORM_PRESERVE (see 00119 Image/Normalize.H). */ 00120 explicit GenericFrame(const Image<float>& floatimg, 00121 const int flags); 00122 00123 /// Construct an video-native frame 00124 explicit GenericFrame(const VideoFrame& vidframe); 00125 00126 // default destruct, assign, copy operators OK 00127 00128 /// Check if we have a valid non-empty image, regardless of format 00129 bool initialized() const; 00130 00131 /// Make a new GenericFrame object with an internal private copy of any transient data 00132 static GenericFrame deepCopyOf(const GenericFrame& f); 00133 00134 /// Get the frame's specifications 00135 GenericFrameSpec frameSpec() const; 00136 00137 /// Get the frame's native image type 00138 NativeType nativeType() const { return itsNativeType; } 00139 00140 /// Get the frame's native image typename 00141 std::string nativeTypeName() const; 00142 00143 /// Get the dimensions of the native image 00144 Dims getDims() const; 00145 00146 /// Get the width of the native image 00147 int getWidth() const { return this->getDims().w(); } 00148 00149 /// Get the height of the native image 00150 int getHeight() const { return this->getDims().h(); } 00151 00152 /// Get the float-normalization flags 00153 int floatFlags() const { return itsFloatFlags; } 00154 00155 /// Change the float-normalization flags 00156 void setFloatFlags(int v); 00157 00158 /// Get an 8-bit integer rgb version of the frame, converting as necessary 00159 Image<PixRGB<byte> > asRgbU8() const { return this->asRgbU8Layout().render(); } 00160 Image<PixRGB<byte> > asRgb() const { return this->asRgbU8Layout().render(); } 00161 00162 // Get a 16-bit interger rgb version of the frame, converting as necessary 00163 Image<PixRGB<uint16> > asRgbU16() const; 00164 00165 /// Get an 8-bit integer rgb layout version of the frame, converting as necessary 00166 const Layout<PixRGB<byte> >& asRgbU8Layout() const; 00167 00168 /// Get a 32-bit float rgb version of the frame, converting as necessary 00169 Image<PixRGB<float> > asRgbF32() const; 00170 00171 /// Get an 8-bit integer grayscale version of the frame, converting as necessary 00172 Image<byte> asGrayU8() const { return this->asGrayU8Layout().render(); } 00173 Image<byte> asGray() const { return this->asGrayU8Layout().render(); } 00174 00175 // Get a 16-bit interger grayscale version of the frame, converting as necessary 00176 Image<uint16> asGrayU16() const; 00177 00178 /// Get an 8-bit integer grayscale layout version of the frame, converting as necessary 00179 const Layout<byte>& asGrayU8Layout() const; 00180 00181 /// Get an 8-bit integer grayscale NTSC layout version of the frame, converting as necessary 00182 const Layout<byte>& asGrayU8NTSCLayout() const; 00183 00184 /// Get a 32-bit float grayscale version of the frame, converting as necessary 00185 Image<float> asGrayF32() const; 00186 Image<float> asFloat() const { return this->asGrayF32(); } 00187 00188 /// Convert the image from rgb to luminance in NTSC coordinates 00189 Image<byte> asGrayNTSC() const { return this->asGrayU8NTSCLayout().render();} 00190 /// Get a video version of the frame, converting as necessary 00191 VideoFrame asVideo() const; 00192 00193 //Metadata support functions 00194 00195 /// Check whether we have a given piece of metadata 00196 bool hasMetaData(const std::string& tag) const; 00197 00198 /// Get a given piece of metadata or throw an exception if it doesn't exist 00199 rutz::shared_ptr<MetaData> getMetaData(const std::string& tag); 00200 00201 /// Insert a given piece of metadata, overwriting any previous metadata associated with that tag 00202 void addMetaData(const std::string& tag, rutz::shared_ptr<MetaData> d); 00203 00204 private: 00205 // NOTE: this implementation has sizeof(GenericFrame)==108 on a 00206 // 32-bit machine; it may be possible to optimize this if necessary 00207 // (at a potentially slight cost in runtime efficiency), by storing 00208 // pointers to dynamically-allocated objects for each of the image 00209 // types instead of storing them by value here 00210 00211 NativeType itsNativeType; 00212 mutable Layout<PixRGB<byte> > itsRgbU8; 00213 mutable Image<PixRGB<uint16> > itsRgbU16; 00214 mutable Image<PixRGB<float> > itsRgbF32; 00215 mutable Layout<byte> itsGrayU8; 00216 mutable Image<uint16> itsGrayU16; 00217 mutable Image<float> itsGrayF32; 00218 mutable VideoFrame itsVideo; 00219 int itsFloatFlags; 00220 00221 typedef std::map<std::string, rutz::shared_ptr<MetaData> > MetaDataMap; 00222 rutz::shared_ptr<MetaDataMap> itsMetaDataMap; 00223 00224 }; 00225 00226 /// Gives specification of a GenericFrame 00227 struct GenericFrameSpec 00228 { 00229 GenericFrameSpec(); 00230 00231 GenericFrame::NativeType nativeType; 00232 VideoFormat videoFormat; // applies only if nativeType == VIDEO 00233 bool videoByteSwap; // applies only if nativeType == VIDEO 00234 Dims dims; 00235 int floatFlags; 00236 float frameRate ; 00237 00238 bool operator==(const GenericFrameSpec& that) const; 00239 00240 bool operator!=(const GenericFrameSpec& that) const 00241 { return !(this->operator==(that)); } 00242 00243 /// Return a human-readable description of this frame spec 00244 std::string getDescription() const; 00245 00246 /// Get the frame's native image typename 00247 std::string nativeTypeName() const; 00248 00249 /// Get the VideoFormat that would result from applying asVideo() to a corresponding GenericFrame 00250 VideoFormat getActualVideoFormat() const; 00251 }; 00252 00253 /// Check equality of two GenericFrame objects, return true if they are identical 00254 /** If the objects are of the same native type (and same video mode, 00255 if the native type is video), then we check equality of the native 00256 images; otherwise, we convert both images to RGB_F32 and compare 00257 the resulting images. */ 00258 bool operator==(const GenericFrame& f1, const GenericFrame& f2); 00259 00260 // ###################################################################### 00261 /* So things look consistent in everyone's emacs... */ 00262 /* Local Variables: */ 00263 /* mode: c++ */ 00264 /* indent-tabs-mode: nil */ 00265 /* End: */ 00266 00267 #endif // RASTER_GENERICFRAME_H_DEFINED