OpenImageIO
 All Classes Files Friends Macros Pages
py_oiio.h
1 /*
2  Copyright 2009 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 #ifndef PYOPENIMAGEIO_PY_OIIO_H
32 #define PYOPENIMAGEIO_PY_OIIO_H
33 
34 // Avoid a compiler warning from a duplication in tiffconf.h/pyconfig.h
35 #undef SIZEOF_LONG
36 #include <boost/python.hpp>
37 
38 #include "imageio.h"
39 #include "typedesc.h"
40 #include "imagecache.h"
41 #include "imagebuf.h"
42 
43 
44 #if PY_MAJOR_VERSION < 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5)
45 #define Py_ssize_t int
46 #endif
47 
48 namespace PyOpenImageIO
49 {
50 
51 using namespace boost::python;
52 
53 OIIO_NAMESPACE_USING
54 
55 void declare_imagespec();
56 void declare_imageinput();
57 void declare_imageoutput();
58 void declare_typedesc();
59 void declare_imagecache();
60 void declare_imagebuf();
61 void declare_paramvalue();
62 
63 bool PyProgressCallback(void*, float);
64 
66 private:
68  friend class ImageOutputWrap;
69  ImageInput *m_input;
70  void* make_write_buffer (object&, Py_ssize_t);
71 public:
72  virtual ~ImageInputWrap();
73  static boost::python::object create(const std::string&, const std::string&);
74  static boost::python::object open_static_regular(const std::string&);
75  static boost::python::object open_static_with_config(const std::string&,const ImageSpec&);
76  const char *format_name () const;
77  bool open_regular (const std::string&, ImageSpec&);
78  bool open_with_config(const std::string&, ImageSpec&, const ImageSpec&);
79  const ImageSpec &spec() const;
80  bool close();
81  int current_subimage() const;
82  bool seek_subimage (int, int, ImageSpec&);
83  bool read_image(TypeDesc, object&, stride_t, stride_t, stride_t, object);
84  bool read_image_simple(object&);
85  bool read_scanline(int, int, TypeDesc, object&, stride_t);
86  bool read_scanline_simple(int, int, object&);
87  bool read_tile(int, int, int, TypeDesc, object&,
88  stride_t, stride_t, stride_t);
89  bool read_tile_simple(int, int, int, object&);
90  bool read_native_scanline(int, int, object&);
91  bool read_native_tile(int, int, int, object&);
92  std::string geterror()const;
93 };
94 
95 
97 private:
98  friend class ImageBufWrap;
99  ImageOutput *m_output;
100  const void *make_read_buffer(object&);
101 public:
102  virtual ~ImageOutputWrap();
103  static boost::python::object create(const std::string&, const std::string&);
104  const ImageSpec &spec() const;
105  bool open (const std::string&, const ImageSpec&, ImageOutput::OpenMode);
106  bool close();
107  bool write_scanline(int, int, TypeDesc, boost::python::object&, stride_t);
108  bool write_tile(int, int, int, TypeDesc, boost::python::object&,
109  stride_t, stride_t, stride_t);
110  bool write_rectangle(int, int, int, int, int, int, TypeDesc,
111  boost::python::object&, stride_t, stride_t, stride_t);
112  bool write_image (TypeDesc, object&, stride_t, stride_t, stride_t, object);
113  void print_pointer(); //for testing only
114  bool copy_image (ImageInputWrap *iiw);
115  const char *format_name () const;
116  bool supports (const std::string&) const;
117  std::string geterror()const;
118 };
119 
120 
122 private:
123  friend class ImageBufWrap;
124  ImageCache *m_cache;
125 public:
126  static ImageCacheWrap *create (bool);
127  static void destroy (ImageCacheWrap*);
128  void clear ();
129  bool attribute (const std::string&, TypeDesc, const void*);
130  bool attribute_int (const std::string&, int );
131  bool attribute_float (const std::string&, float);
132  bool attribute_double (const std::string&, double);
133  bool attribute_char (const std::string&, const char*);
134  bool attribute_string (const std::string&, const std::string&);
135  bool getattribute(const std::string&, TypeDesc, void*);
136  bool getattribute_int (const std::string&, int&);
137  bool getattribute_float(const std::string&, float&);
138  bool getattribute_double(const std::string&, double&);
139  bool getattribute_char(const std::string&, char**);
140  bool getattribute_string(const std::string&, std::string&);
141  std::string resolve_filename (const std::string&);
142  bool get_image_info (ustring, ustring, TypeDesc, void*);
143  bool get_imagespec(ustring, ImageSpec&, int);
144  bool get_pixels (ustring, int, int, int, int, int, int,
145  int, int, TypeDesc, void*);
146 
147  //First needs to be exposed to python in imagecache.cpp
148  /*
149  Tile *get_tile (ustring filename, int subimage,
150  int x, int y, int z) {
151  return m_cache->get_tile(filename, subimage, x, y, z);
152  }
153  void release_tile (Tile *tile) const {
154  m_cache->release-tile(tile);
155  }
156  const void *tile_pixels (Tile *tile, TypeDesc &format) const {
157  m_cache->tile_pixels(tile, format);
158  }
159  */
160 
161  std::string geterror () const;
162  std::string getstats (int) const;
163  void invalidate (ustring);
164  void invalidate_all (bool);
165 };
166 
167 
169 private:
170  ImageBuf *m_buf;
171 public:
172 
173  ImageBufWrap (const std::string&name = std::string(),
174  ImageCacheWrap*icw = NULL);
175  ImageBufWrap (const std::string&, const ImageSpec&) ;
176  void clear ();
177  void reset_to_new_image (const std::string&, ImageCache*);
178  void reset_to_blank_image (const std::string&, const ImageSpec&);
179  void alloc (const ImageSpec&);
180  // TODO: How to wrap ProgressCallback?
181  /*
182  bool read (int subimage=0, bool force=false,
183  TypeDesc convert=TypeDesc::UNKNOWN,
184  ProgressCallback progress_callback=NULL,
185  void *progress_callback_data=NULL);
186 
187  bool save (const std::string &filename = std::string(),
188  const std::string &fileformat = std::string(),
189  ProgressCallback progress_callback=NULL,
190  void *progress_callback_data=NULL) const;
191 
192  bool write (ImageOutputWrap *out,
193  ProgressCallback progress_callback=NULL,
194  void *progress_callback_data=NULL) const;
195  */
196 
197  bool init_spec (const std::string&, int, int);
198  const ImageSpec &spec() const;
199  const std::string &name() const;
200  const std::string &file_format_name() const;
201  int subimage() const;
202  int nsubimages() const;
203  int nchannels() const;
204  float getchannel (int, int, int, int) const;
205  void getpixel (int, int, float*, int) const;
206  void interppixel (float, float, float*) const;
207  void interppixel_NDC (float, float, float*) const;
208  void setpixel_xy (int, int, const float*, int);
209  void setpixel_i (int, const float*, int);
210 
211  // These copy_pixel methods require the user to send an appropriate
212  // area of memory ("*result"), which would make little sense from Python.
213  // The user *could* create an appropriately sized array in Python by
214  // filling it with the correct amount of dummy data, but would
215  // this defeat the purpose of Python? Instead, the wrapper could
216  // allocate that array, fill it, and return it to Python. This is the way
217  // ImageInput.read_image() was wrapped.
218  bool get_pixels (int, int, int, int, int, int, TypeDesc, void*) const;
219 
220  // TODO: handle T and <T>. Don't know how to handle this with B.P,
221  // though haven't given it much thought yet.
222  /*
223  bool get_pixels_convert (int xbegin, int xend, int ybegin, int yend,
224  T *result) const;
225  bool get_pixels_convert_safer (int xbegin, int xend, int ybegin,
226  int yend, std::vector<T> &result) const;
227  */
228 
229  int orientation() const;
230  int oriented_width() const;
231  int oriented_height() const;
232  int oriented_x() const;
233  int oriented_y() const;
234  int oriented_full_width() const;
235  int oriented_full_height() const;
236  int oriented_full_x() const;
237  int oriented_full_y() const;
238  int xbegin() const;
239  int xend() const;
240  int ybegin() const;
241  int yend() const;
242  int xmin();
243  int xmax();
244  int ymin();
245  int ymax();
246  void zero();
247  bool pixels_valid () const;
248  bool localpixels () const;
249  //TODO: class Iterator and ConstIterator
250 
251  std::string geterror()const;
252 };
253 
254 
255 } // namespace PyOpenImageIO
256 
257 #endif // PYOPENIMAGEIO_PY_OIIO_H