HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMG_TileDevice.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: IMG_TileDevice.h ( IMG Library, C++)
7  *
8  * COMMENTS: Class to define tile based I/O for mantra.
9  * When writing an image which has fields, the data sent to
10  * writeTile will not be interlaced. It's the responsibility of
11  * the device to put in the appropriate data to the other fields.
12  */
13 
14 #pragma once
15 
16 #ifndef __IMG_TileDevice__
17 #define __IMG_TileDevice__
18 
19 #include "IMG_API.h"
20 #include "IMG_FileTypes.h"
21 #include "IMG_Tags.h"
22 #include <UT/UT_NonCopyable.h>
23 #include <UT/UT_Rect.h>
24 #include <UT/UT_String.h>
25 #include <UT/UT_StringArray.h>
26 
27 class UT_WorkBuffer;
28 class IMG_Raster;
29 class IMG_Format;
30 class IMG_TileInterface;
31 class IMG_TileOptions;
32 class IMG_TileOptionList;
33 class UT_Options;
34 
36 {
37 public:
39  virtual ~IMG_TileDevice();
40 
42 
44  {
45  IMG_TILE_BOTTOM_FIRST = 0,
46  IMG_TILE_RANDOM_ORDER = 1,
47  IMG_TILE_TOP_FIRST = 2,
48  IMG_TILE_HILBERT = 3
49  };
50 
51  typedef IMG_TileDevice *(*IMG_TileDeviceCreator)(const char *filename,
52  const UT_Options *options);
53 
54  /// Create a new device based on the filename. This checks:
55  /// - "null:" - Null device
56  /// - "imdisplay:" - MPlay device
57  /// - etc.
58  /// If no devices match exactly, the file device will be opened.
59  static IMG_TileDevice *newDevice(const char *filename,
60  const UT_Options *options = NULL);
61 
62  /// Returns true if filename represents a special device, not a regular
63  /// file, i.e. everything that newDevice has a special case for.
64  static bool isSpecialFileName(const char *filename);
65 
66  /// Returns true if filename represents a null device,
67  /// i.e. it will ignore everything sent to it.
68  /// This is useful for avoiding work that will be ignored.
69  static bool isNullDeviceName(const char *filename);
70 
71  /// Register a custom image device. The label should be specified @b
72  /// without the colon (':'). For example: @code
73  /// registerDevice(createSocket, "socket");
74  /// @endcode
75  /// The creator function will be called from newDevice() with the
76  /// filename/options passed. newDevice() will invoke the creator if the
77  /// filename @b begins with the label. For example, the following would
78  /// invoke the socket device registered above: @code
79  /// newDevice("socket:{'port':43}", NULL);
80  /// @endcode
81  /// @note This will replace any existing device
82  static void registerDevice(IMG_TileDeviceCreator creator,
83  const char *label);
84  /// Unregister a custom device
85  static void unregisterDevice(const char *label);
86 
87  /// @{
88  /// Static methods to allocate new devices of built-in device types
89  static IMG_TileDevice *newFileDevice();
90  static IMG_TileDevice *newNullDevice();
91  static IMG_TileDevice *newMPlayDevice(int from_middle=1);
92  static IMG_TileDevice *imdisplayFileDevice(const char *filename);
93  static IMG_TileDevice *newSocketDevice(const char *host);
94  /// @}
95 
96  /// @{
97  /// Replace the creators for the file or mplay devices.
98  /// @note The signatures for the constructors are different than the
99  /// callbacks uses in @c registerDevice().
100  static void setFileDevCreator(IMG_TileDevice *(*creator)());
101  static void setMPlayDevCreator(IMG_TileDevice *(*creator)(int ));
102  /// @}
103 
104  /// @{
105  /// Query information about the tile device
106  virtual const char *className() const = 0;
107  virtual void getDescription(UT_WorkBuffer &wbuf) const;
108  /// @}
109 
110  // The visible region is inclusive (i.e. there are (r-l+1) pixels per scan)
111  // Setting the visible region should be done before the image is opened.
112  SYS_DEPRECATED_REPLACE(16.0, "setDeviceDataWindow")
113  void setVisibleRegion(int l, int r, int b, int t);
114 
115  // The region is always bigger or equal to the visible region. It
116  // represents the region which needs to be "rendered". The renderable
117  // region is broken up into tiles of the tile size specified.
118  SYS_DEPRECATED_REPLACE(16.0, "setDeviceRenderWindow")
119  void setRenderSize(int rwidth, int rheight);
120 
121  /// Set the data window
122  void setDeviceDataWindow(const UT_DimRect &w)
123  { myDataWindow = w; }
124 
125  /// Set the window of pixels that need to be rendered
126  /// The render window is always bigger or equal to the data window. It
127  /// might be larger due to pixel filters (i.e. samples outside the data
128  /// window might need to be computed for filtering). The difference
129  /// between the data window and the render window size is used to determine
130  /// filter width.
132  { myRenderWindow = w; }
133 
134  /// Open the device
135  virtual int open(const IMG_TileOptions &info,
136  int xres, int yres,
137  int tile_device, int tile_height,
138  fpreal aspect) = 0;
139 
140  /// The default implementation will just open the first entry of the flist
141  /// by calling open().
142  virtual int openMulti(
143  IMG_TileOptionList &flist,
144  int xres, int yres,
145  int tile_width, int tile_height,
146  fpreal aspect);
147 
148  virtual void setWhitePoint(fpreal white);
149 
150  /// Allow the device to perform any "checkpointing" needed. This is
151  /// equivalent to "flushing" the device.
152  virtual void checkpoint();
153 
154  // where the render originated from, to provide a half-decent name to ip.
155  void setRenderSourceName( const char *name )
156  { myRenderSourceName.harden(name); }
157  const char *getRenderSourceName() const
158  { return myRenderSourceName; }
159 
160  /// After the open() is called, then it's possible to query the data type
161  /// that the device expects.
162  int getFileCount() const { return myFileCount; }
163  IMG_DataType getDataType(int idx=0) const;
164  bool isRandomAccess() const { return myRandomAccess; }
165 
166  /// Query tile width parameters
167  int getTileWidth() const { return myTileWidth; }
168  int getTileHeight() const { return myTileHeight; }
169  int getTileCountX() const { return myTileCountX; }
170  int getTileCountY() const { return myTileCountY; }
171 
172  // Default is to return IMG_RGBA
173  IMG_ColorModel getColorModel(int idx=0) const;
174 
175  IMG_DataType *getDataTypes() { return myDataType; }
176  IMG_ColorModel *getColorModels() { return myColorModel; }
177 
178  // Writes a custom tag to the device. The tag must have a name, and some
179  // number of string values (zero is acceptable).
180  virtual void writeCustomTag(const char *tagname, int size,
181  const char *const *values);
182 
183  // Short cut method to write a well-known tag.
184  void writeKnownTag(IMG_TagType tag_id, int size,
185  const char *const *values)
186  {
187  writeCustomTag(IMGtagName(tag_id), size, values);
188  }
189 
190 
191  /// The data written is in the format specified by the IMG_ColorModel and
192  /// IMG_DataType parameters in the open. The size of the tile is smaller
193  /// or equal to the sizes specified in the open. If the device was
194  /// opened using openMulti(), the data is provided as a sequence of
195  /// planes - first all pixels for plane 0, then all pixels for plane 1,
196  /// etc.
197  ///
198  /// The rectangle spectified by @c x0, @c x1, @c y0 and @c y1 is an
199  /// inclusive rectangle. That is, the number of pixels in @c data is:
200  /// @c (x1-x0+1)*(y1-y0+1).
201  ///
202  /// The first pixel in the data buffer is the bottom left corner of the
203  /// tile (i.e. @c (x0,y0)).
204  ///
205  /// The rectangle specified is also relative to the data window. So, to
206  /// offset to get the absolute pixel location you need to take the data
207  /// window (getDataWindow()) into account. You can also use the
208  /// convenience methods @c getDataX() and getDataY(). For example: @code
209  /// unsigned pixel_x0 = x0 + getDataX();
210  /// unsigned pixel_x1 = x1 + getDataX();
211  /// unsigned pixel_y0 = y0 + getDataY();
212  /// unsigned pixel_y1 = y1 + getDataY();
213  /// @endcode
214  virtual int writeTile(const void *data,
215  unsigned x0, unsigned x1,
216  unsigned y0, unsigned y1) = 0;
217 
218  /// This is just for sending sample data from Mantra to Houdini
219  /// via TIL_TileMPlay.
220  /// data contains nsamples tuples of floats_per_sample floats
221  /// samples contains nsamples (x,y,pixel_sample) tuples
222  virtual bool writeSamples(
223  int nsamples,
224  int floats_per_sample,
225  const UT_Vector3T<int> *samples,
226  const float *data)
227  {
228  UT_ASSERT_MSG(0, "Should be implemented by subclass in relevant cases!");
229  return false;
230  }
231 
232  virtual void flush();
233 
234  virtual int close(bool keep_alive=false) = 0;
235 
236  // If the flush method is unsafe for multi-processing (or very costly), the
237  // device should return 1 here.
238  virtual int unsafeFlush();
239 
240  /// The renderer will ask us for the next tile location to be rendered.
241  /// We give it the location (in renderable region coordinates). The
242  /// function returns true if this is a valid tile, otherwise false.
243  /// The tile mode maps to the enum of bottom/top first or random order.
244  /// (tx,ty) will be the unique tile index tuple, between (0,0) inclusive
245  /// and (myTileCountX,myTileCountY) exclusive.
246  ///
247  /// NOTE: xl, xr, yb, and yt, are *all* inclusive!
248  /// Also, xl <= yr and yb <= yt if the return value is true,
249  /// i.e. at least one pixel.
250  virtual bool getNextTile(int &xl, int &xr, int &yb, int &yt,
251  int &tx, int &ty);
252 
253  // Returns true if a tile has been clicked; otherwise, false. If a tile
254  // has been clicked then tx and ty will be set to that tile's coordinates;
255  // otherwise, the values of tx and ty are undefined.
256  // The radius is in pixels.
257  virtual bool getClickedTile(int &tx, int &ty, float &radius) const;
258 
259  virtual void resetTileRequestor();
260  virtual int getTileMode() const;
261 
262  virtual void terminateOnConnectionLost(bool ) {}
263 
264  // Whether this object wants the Houdini HScript port to be written
265  // to it via writeCustomTag("houdiniportnum", ...)
266  virtual bool wantsHoudiniPort() const { return false; }
267 
268  /// If this tile device is interactive (i.e. a flipbook), this method
269  /// should return true. When mantra runs in batch-mode, opening an
270  /// interactive device will fail.
271  virtual bool isInteractive() const { return false; }
272 
273  /// @{
274  /// Accessor
275  int xres() const { return myDisplayWindow.width(); }
276  int yres() const { return myDisplayWindow.height(); }
277  /// @}
278 
279  // Passes style information to this tile device so it can be sent out
280  // later on.
281  virtual void setStyleSheets(UT_StringArray&) {}
282 
283  /// Subclasses can override this method if they respond to mouse clicks.
284  /// x and y should be set to the mouse position (in pixels) if the mouse
285  /// has been clicked. The method returns true if the mouse has been
286  /// clicked; otherwise, false. If the method returns false then the
287  /// values of x and y are undefined.
288  virtual bool getMouseClickPosition(int &x, int &y) const
289  { return false; }
290 
291  /// Subclasses can override this method if they respond to mouse clicks
292  /// and specify a radius for the click.
293  virtual float getMouseClickRadius() const
294  { return -1.0f; }
295 
296 protected:
297  // The sub-class must call this function to initialize the base class
298  // correctly.
299  void setResolution(int xres, int yres,
300  int tilewidth, int tileheight,
301  int tile_order = IMG_TILE_BOTTOM_FIRST);
302 
303  void setModel(const IMG_TileOptions &finfo);
304  void setModel(IMG_TileOptionList &flist);
305  void setModel(const IMG_DataType *dt, const IMG_ColorModel *cm);
306 
307  // Some random access devices may want to tell the renderer which tile to
308  // render next. This function will get the coordinates of the tile (i.e.
309  // if there are N tiles in X and M tiles in Y, tx is (0,N) and ty is (0,M)
310  void mapImageToTile(int ix, int iy, int &tx, int &ty);
311  void getTileBounds(int tx, int ty,
312  int &xl, int &xr, int &yb, int &yt);
313 
314  bool hasDataWindow(int full_xres, int full_yres) const;
315  int getDataX() const { return myDataWindow.x1(); }
316  int getDataY() const { return myDataWindow.y1(); }
317  int getDataWidth() const { return myDataWindow.width(); }
318  int getDataHeight() const { return myDataWindow.height(); }
319 
320  /// Specify the desired tile order.
321  void setTileOrder(IMG_TILE_ORDER tile_order)
322  { myRandomAccess = tile_order; }
323 
324  /// In the open method, the sub-class should set the file count
325  void setFileCount(int count)
326  { myFileCount = count; }
327 
328  /// Set the tile width
329  void setTileSize(int width, int height);
330 
331  SYS_DEPRECATED_REPLACE(16.0, "dataWindow()")
332  UT_InclusiveRect getDataWindow() const;
333 
334  /// @{
335  /// Accessors to data, display render windows
336  const UT_DimRect &dataWindow() const { return myDataWindow; }
337  const UT_DimRect &displayWindow() const { return myDisplayWindow; }
338  const UT_DimRect &renderWindow() const { return myRenderWindow; }
339  /// @}
340 
341 private:
342  fpreal myAspectRatio;
343  int myFileCount;
344  int myTileWidth, myTileHeight;
345  UT_DimRect myDataWindow;
346  UT_DimRect myDisplayWindow;
347  UT_DimRect myRenderWindow;
348  IMG_TILE_ORDER myRandomAccess; // If device supports random access
349 
350  int myClickedTileXY[2];
351  float myClickedRadius;
352  int myFilterX, myFilterY;
353  int myTileCountX, myTileCountY;
354  int myRandomCheck;
355  IMG_DataType *myDataType;
356  IMG_ColorModel *myColorModel;
357  IMG_TileInterface *myTiles;
358  UT_String myRenderSourceName;
359 };
360 
361 #endif
const UT_DimRect & displayWindow() const
int getDataWidth() const
int xres() const
PXL_API const char * getDescription(const ColorSpace *space)
Return the description of the color space.
GT_API const UT_StringHolder filename
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
void setTileOrder(IMG_TILE_ORDER tile_order)
Specify the desired tile order.
void
Definition: png.h:1083
virtual float getMouseClickRadius() const
int getDataHeight() const
const UT_DimRect & renderWindow() const
void writeKnownTag(IMG_TagType tag_id, int size, const char *const *values)
int yres() const
GLint y
Definition: glcorearb.h:103
void close() override
virtual bool getMouseClickPosition(int &x, int &y) const
virtual void setStyleSheets(UT_StringArray &)
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:159
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
#define IMG_API
Definition: IMG_API.h:10
IMG_API UT_StringHolder IMGtagName(int id)
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
int getTileCountY() const
int open(float queuesize) override
virtual void terminateOnConnectionLost(bool)
virtual bool writeSamples(int nsamples, int floats_per_sample, const UT_Vector3T< int > *samples, const float *data)
void setDeviceRenderWindow(const UT_DimRect &w)
int getDataY() const
void setRenderSourceName(const char *name)
IMG_DataType
Definition: IMG_FileTypes.h:17
int getFileCount() const
GLdouble y1
Definition: glad.h:2349
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
IMG_DataType * getDataTypes()
virtual bool isInteractive() const
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
GLdouble t
Definition: glad.h:2397
GLsizei samples
Definition: glcorearb.h:1298
DeviceRef newDevice(DeviceType type=DeviceType::Default)
Definition: oidn.hpp:836
GLsizeiptr size
Definition: glcorearb.h:664
A map of string to various well defined value types.
Definition: UT_Options.h:84
bool isRandomAccess() const
IMG_TagType
Definition: IMG_Tags.h:20
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
fpreal64 fpreal
Definition: SYS_Types.h:277
void setFileCount(int count)
In the open method, the sub-class should set the file count.
int getTileHeight() const
virtual bool wantsHoudiniPort() const
IMG_ColorModel
Definition: IMG_FileTypes.h:52
GLint GLsizei width
Definition: glcorearb.h:103
int getTileWidth() const
Query tile width parameters.
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const char * getRenderSourceName() const
int getDataX() const
GLboolean r
Definition: glcorearb.h:1222
int getTileCountX() const
IMG_ColorModel * getColorModels()
GLint GLsizei count
Definition: glcorearb.h:405
Definition: format.h:895