ImageIo.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/DataSource.h"
27 #include "cinder/DataTarget.h"
28 #include "cinder/Surface.h"
29 #include "cinder/Exception.h"
30 
31 #include <vector>
32 #include <map>
33 #include <utility>
34 
35 namespace cinder {
36 
37 typedef std::shared_ptr<class ImageSource> ImageSourceRef;
38 typedef std::shared_ptr<class ImageLoader> ImageLoaderRef;
39 typedef std::shared_ptr<class ImageTarget> ImageTargetRef;
40 typedef std::shared_ptr<class ImageTargetFile> ImageTargetFileRef;
41 
42 class ImageIo {
43  public:
49  typedef enum ChannelOrder { RGBA, BGRA, ARGB, ABGR, RGBX, BGRX, XRGB, XBGR, RGB, BGR, Y, YA, CUSTOM } ChannelOrder; // Y = Gray/Luminance, X = ignored
50 
51  int32_t getWidth() const { return mWidth; }
52  int32_t getHeight() const { return mHeight; }
53  ColorModel getColorModel() const { return mColorModel; }
54  DataType getDataType() const { return mDataType; }
56  virtual bool hasAlpha() const { return channelOrderHasAlpha( mChannelOrder ); }
57 
58  static void translateRgbColorModelToOffsets( ChannelOrder channelOrder, int8_t *red, int8_t *green, int8_t *blue, int8_t *alpha, int8_t *inc );
59  static void translateGrayColorModelToOffsets( ChannelOrder channelOrder, int8_t *gray, int8_t *alpha, int8_t *inc );
60  static bool channelOrderHasAlpha( ChannelOrder channelOrder );
61  static int8_t channelOrderNumChannels( ChannelOrder channelOrder );
62  static uint8_t dataTypeBytes( DataType dataType );
63 
65  static std::vector<std::string> getLoadExtensions();
67  static std::vector<std::string> getWriteExtensions();
68 
69  protected:
70  ImageIo();
71 
72  void setSize( int32_t width, int32_t height ) { mWidth = width; mHeight = height; }
73  void setColorModel( ColorModel colorModel ) { mColorModel = colorModel; }
74  void setDataType( DataType aDataType ) { mDataType = aDataType; }
75  void setChannelOrder( ChannelOrder aChannelOrder ) { mChannelOrder = aChannelOrder; }
76 
77  int32_t mWidth, mHeight;
81 };
82 
83 class ImageSource : public ImageIo {
84  public:
86  virtual ~ImageSource() {}
87 
88  class Options {
89  public:
90  Options() : mIndex( 0 ) {}
91 
93  Options& index( int32_t aIndex ) { mIndex = aIndex; return *this; }
94 
95  int32_t getIndex() const { return mIndex; }
96 
97  protected:
98  int32_t mIndex;
99  };
100 
102  float getPixelAspectRatio() const;
104  bool isPremultiplied() const;
105 
106  virtual void load( ImageTargetRef target ) = 0;
107 
108  typedef void (ImageSource::*RowFunc)(ImageTargetRef, int32_t, const void*);
109 
110  protected:
111  void setPixelAspectRatio( float pixelAspectRatio ) { mPixelAspectRatio = pixelAspectRatio; }
112  void setPremultiplied( bool premult = true ) { mIsPremultiplied = premult; }
114  void setCustomPixelInc( int8_t customPixelInc ) { mCustomPixelInc = customPixelInc; }
115 
117  void setupRowFuncRgbSource( ImageTargetRef target );
118  void setupRowFuncGraySource( ImageTargetRef target );
119  template<typename SD, typename TD, ColorModel TCS>
121  template<typename SD, typename TD>
123  template<typename SD>
125 
126  template<typename SD, typename TD, ImageIo::ColorModel TCM, bool ALPHA>
127  void rowFuncSourceRgb( ImageTargetRef target, int32_t row, const void *data );
128  template<typename SD, typename TD, ColorModel TCM, bool ALPHA>
129  void rowFuncSourceGray( ImageTargetRef target, int32_t row, const void *data );
130 
134 
139 };
140 
141 class ImageTarget : public ImageIo {
142  public:
143  virtual ~ImageTarget() {};
144 
145  virtual void* getRowPointer( int32_t row ) = 0;
146  virtual void setRow( int32_t row, const void *data ) { throw; }
147  virtual void finalize() { }
148 
149  class Options {
150  public:
151  Options() : mQuality( 0.9f ), mColorModelDefault( true ) {}
152 
153  Options& quality( float quality ) { mQuality = quality; return *this; }
154  Options& colorModel( ImageIo::ColorModel cm ) { mColorModelDefault = false; mColorModel = cm; return *this; }
155 
157 
158  float getQuality() const { return mQuality; }
159  bool isColorModelDefault() const { return mColorModelDefault; }
161 
162  protected:
163  float mQuality;
166  };
167 
168  protected:
170 };
171 
173 ImageSourceRef loadImage( const fs::path &path, ImageSource::Options options = ImageSource::Options(), std::string extension = "" );
175 ImageSourceRef loadImage( DataSourceRef dataSource, ImageSource::Options options = ImageSource::Options(), std::string extension = "" );
177 void writeImage( DataTargetRef dataTarget, const ImageSourceRef &imageSource, ImageTarget::Options options = ImageTarget::Options(), std::string extension = "" );
180 void writeImage( const fs::path &path, const ImageSourceRef &imageSource, ImageTarget::Options options = ImageTarget::Options(), std::string extension = "" );
182 void writeImage( ImageTargetRef imageTarget, const ImageSourceRef &imageSource );
183 
184 class ImageIoException : public Exception {
185 };
186 
188 };
189 
191 };
192 
194 };
195 
197 };
198 
200 };
201 
203 };
204 
205 
209 
210  static ImageSourceRef createSource( DataSourceRef dataSource, ImageSource::Options options, std::string extension );
211  static ImageTargetRef createTarget( DataTargetRef dataTarget, ImageSourceRef imageSource, ImageTarget::Options options, std::string extension );
212 
213  static void registerSourceType( std::string extension, SourceCreationFunc func, int32_t priority = 2 );
214  static void registerSourceGeneric( SourceCreationFunc func, int32_t priority = 2 );
215 
216  static void registerTargetType( std::string extension, TargetCreationFunc func, int32_t priority, const std::string &extensionData );
217 
218  private:
219 
220  struct Inst {
221  void registerSourceType( std::string extension, SourceCreationFunc func, int32_t priority );
222  void registerSourceGeneric( SourceCreationFunc func, int32_t priority );
223  void registerTargetType( std::string extension, TargetCreationFunc func, int32_t priority, const std::string &extensionData );
224 
225  ImageSourceRef createSource( DataSourceRef dataSource, ImageSource::Options options, std::string extension );
226  ImageTargetRef createTarget( DataTargetRef dataTarget, ImageSourceRef imageSource, ImageTarget::Options options, std::string extension );
227 
228  std::map<std::string, std::multimap<int32_t,SourceCreationFunc> > mSources;
229  std::map<int32_t, SourceCreationFunc> mGenericSources;
230  std::map<std::string, std::multimap<int32_t,std::pair<TargetCreationFunc,std::string> > > mTargets;
231  };
232 
233  static ImageIoRegistrar::Inst* instance();
234 
235  friend class ImageIo;
236 };
237 
238 template<typename T>
241  (void) register_object;
242  }
243  private:
244  struct exec_register {
245  exec_register() {
246  T::registerSelf();
247  }
248  };
249 
250  static exec_register register_object;
251 };
252 
253 template<typename D> typename ImageIoRegistrant<D>::exec_register ImageIoRegistrant<D>::register_object;
254 
255 #define REGISTER_IMAGE_IO_FILE_HANDLER( TYPE ) \
256 struct ImageIoRegisterT##TYPE : public ImageIoRegistrant<TYPE> { \
257  ImageIoRegisterT##TYPE() : ImageIoRegistrant<TYPE>() {} \
258 };
259 
260 } // namespace cinder