$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #ifndef VLRIMAGING_IMAGE_H_ 00037 #define VLRIMAGING_IMAGE_H_ 00038 00039 #include <inttypes.h> 00040 #include <algorithm> 00041 #include <map> 00042 00043 #include "vlrTime.h" 00044 #include "vlrImageTags.h" 00045 00046 namespace vlr { 00047 00048 class ImageBase 00049 { 00050 public: 00051 // Struct for (float) complex data compatible to Ipp32fc 00052 typedef struct {float re, im;} complex; 00053 00054 // Struct for (double) complex data compatible to Ipp64fc 00055 typedef struct {double re, im;} dcomplex; 00056 00057 typedef enum {BOMO_IGNORE=0, BOMO_CONSTANT, BOMO_REPLICATE, BOMO_REFLECT, BOMO_WRAP} borderMode_t; 00058 typedef enum {PAD_LEFT, PAD_SYM, PAD_RIGHT, PAD_TOP, PAD_BOTTOM, PAD_CUSTOM} padMode_t; 00059 00060 typedef enum 00061 { 00062 CS_GRAY, // Gray level 00063 CS_RGB, // RGB 00064 CS_CMY, // CMY 00065 CS_CMYK, // CMYK 00066 CS_HSV, // HSV 00067 CS_HLS, // HLS 00068 CS_XYZ, // XYZ 00069 CS_LUV, // LUV 00070 CS_LAB, // LAB 00071 CS_YCR, // YCR 00072 CS_YCC, // YCC 00073 CS_YUV, // YUV 00074 CS_RGB_C // RGB CHUNKY 00075 } colorSpace_t; 00076 00077 typedef std::multimap<uint32_t, TagBase*>::const_iterator tagCIter; 00078 00079 protected: 00080 ImageBase(uint32_t width, uint32_t height, 00081 uint32_t channels, uint32_t padded_width, 00082 bool manage_buffer, colorSpace_t colorSpace) : 00083 width_(width), 00084 height_(height), 00085 channels_(channels), 00086 padded_width_(padded_width), 00087 manage_buffer_(manage_buffer), 00088 color_space_(colorSpace), 00089 time_stamp_(currentTime()) 00090 { 00091 num_elements_ = padded_width_*height_*channels_; 00092 } 00093 00094 public: 00095 virtual ~ImageBase() {} 00096 00097 public: 00098 inline uint32_t width() const {return width_;} 00099 inline uint32_t height() const {return height_;} 00100 inline uint32_t channels() const {return channels_;} 00101 inline uint32_t paddedWidth() const {return padded_width_;} 00102 inline bool manageBuffer() const {return manage_buffer_;} 00103 inline uint32_t numElements() const {return num_elements_;} 00104 inline colorSpace_t colorSpace() const {return color_space_;} 00105 inline double timeStamp() const {return time_stamp_;} 00106 inline void setTimeStamp(double time_stamp) {time_stamp_=time_stamp;} 00107 inline const std::string& name() const {return name_;} 00108 inline void setName(const std::string& name) {name_ = name;} 00109 inline const std::string& typeName() const {return type_name_;} 00110 00111 inline TagPoint* addPoint(const float x, const float y, const std::string& label, 00112 const float r, const float g, const float b, const float a) { 00113 TagPoint* pt = new TagPoint(x, y, label, r, g, b, a); 00114 tags_.insert(std::make_pair((uint32_t)tidPoint, pt)); 00115 return pt; 00116 } 00117 00118 inline TagPoint* addPoint(const float x, const float y, const std::string& label) { 00119 TagPoint* pt = new TagPoint(x, y, label); 00120 tags_.insert(std::make_pair((uint32_t)tidPoint, pt)); 00121 return pt; 00122 } 00123 00124 inline TagPoint* addPoint(const float x, const float y) { 00125 TagPoint* pt = new TagPoint(x, y); 00126 tags_.insert(std::make_pair((uint32_t)tidPoint, pt)); 00127 return pt; 00128 } 00129 00130 inline void points(std::pair<tagCIter, tagCIter>& range) { 00131 range=tags_.equal_range(tidPoint); 00132 } 00133 00134 inline TagLine* addLine(const float x0, const float y0, const float x1, const float y1, 00135 const std::string& label, const float r, const float g, 00136 const float b, const float a) { 00137 TagLine* ln = new TagLine(x0, y0, x1, y1, label, r, g, b, a); 00138 tags_.insert(std::make_pair((uint32_t)tidLine, ln)); 00139 return ln; 00140 } 00141 00142 inline TagLine* addLine(const float x0, const float y0, const float x1, const float y1, 00143 const std::string& label) { 00144 TagLine* ln = new TagLine(x0, y0, x1, y1, label); 00145 tags_.insert(std::make_pair((uint32_t)tidLine, ln)); 00146 return ln; 00147 } 00148 00149 inline TagLine* addLine(const float x0, const float y0, const float x1, const float y1) { 00150 TagLine* ln = new TagLine(x0, y0, x1, y1); 00151 tags_.insert(std::make_pair((uint32_t)tidLine, ln)); 00152 return ln; 00153 } 00154 00155 inline void lines(std::pair<tagCIter, tagCIter>& range) { 00156 range=tags_.equal_range(tidLine); 00157 } 00158 00159 inline TagBase* addTag(TagBase* tag) { 00160 tags_.insert(std::make_pair((uint32_t)tag->id(), tag)); 00161 return tag; 00162 } 00163 00164 inline void clearTags() { 00165 for(std::multimap<uint32_t, TagBase*>::iterator it = tags_.begin(); it!=tags_.end(); ++it) { 00166 delete it->second; 00167 } 00168 tags_.clear(); 00169 } 00170 00171 protected: 00172 uint32_t width_; 00173 uint32_t height_; 00174 uint32_t channels_; 00175 uint32_t padded_width_; 00176 uint32_t num_elements_; 00177 bool manage_buffer_; 00178 colorSpace_t color_space_; 00179 double time_stamp_; 00180 00181 std::string name_; 00182 std::string description_; 00183 00184 std::multimap<uint32_t, TagBase*> tags_; 00185 std::string type_name_; // type name is used to make e.g. displays work with derived classes 00186 }; 00187 00188 template <class T> class Image : public ImageBase 00189 { 00190 public: 00191 Image(uint32_t width, uint32_t height, uint32_t channels, uint32_t padded_width, 00192 bool manage_buffer, colorSpace_t colorSpace); 00193 Image(uint32_t width, uint32_t height, uint32_t channels, colorSpace_t colorSpace); 00194 Image(uint32_t width, uint32_t height, uint32_t channels); 00195 Image(uint32_t width, uint32_t height); 00196 Image(const Image& img, bool manage_buffer=true, bool copyData=true, bool copyTags=true); 00197 00198 Image& operator=(const Image& img); 00199 00200 virtual ~Image(); 00201 00202 // inline functions 00203 inline uint32_t elementSize() const {return element_size_;} 00204 inline T* data() const {return data_;} 00205 inline const T* constData() const {return (const T*)data_;} 00206 inline T* setData(T* new_data, bool manage_new_buffer=false) { 00207 T* tmp=data_; 00208 manage_buffer_= manage_new_buffer; 00209 data_= new_data; 00210 if(manage_buffer_ && tmp) {delete[] tmp; tmp=NULL;} 00211 00212 return tmp; 00213 } 00214 00215 inline bool sameDims(const Image& img) const { 00216 return (img.width() == width_ && img.height() == height_ && 00217 img.channels() == channels_ && img.paddedWidth() == padded_width_); 00218 } 00219 00220 template <class nT> operator Image<nT>() { 00221 Image<nT> res(width_, height_, channels_, padded_width_, true, color_space_); 00222 00223 T* data_ptr = data_; 00224 nT* resdata = res.data(); 00225 00226 uint32_t align_gap = padded_width_ - width_; 00227 for (uint32_t c = 0; c < channels_; c++) { 00228 for (uint32_t y = 0; y < height_; y++) { 00229 for (uint32_t x = 0; x < width_; x++) { 00230 *resdata = nT(*data_ptr); 00231 resdata++; 00232 data_ptr++; 00233 } 00234 00235 resdata += align_gap; 00236 data_ptr += align_gap; 00237 } 00238 } 00239 00240 return res; 00241 } 00242 00243 void bounds(T& lower_bound, T& upper_bound); 00244 void normalize(T new_min, T new_max); 00245 bool reformat(uint32_t new_width, uint32_t new_height, uint32_t new_channels, 00246 uint32_t new_padded_width, colorSpace_t new_color_space); 00247 00248 Image<T> operator + (const Image<T>& img) const; 00249 00250 inline T& operator [] (const uint32_t pos) const {return data_[pos];} 00251 inline T& operator () (const uint32_t x, const uint32_t y) const {return data_[y*padded_width_+x];} 00252 inline T& operator () (const uint32_t x, const uint32_t y, const uint32_t c) const 00253 {return data_[c*padded_width_*height_+y*padded_width_+x];} 00254 Image<T> operator () (uint32_t x, uint32_t y, uint32_t roi_width, uint32_t roi_height) const; 00255 00256 private: 00257 void create(); 00258 00259 private: 00260 uint32_t element_size_; 00261 T* data_; 00262 }; 00263 00264 } // namespace vlr 00265 00266 #endif // VLRIMAGING_IMAGE_H_