HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TIL_Stitch.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: TIL_Stitch.h (TIL Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __TIL_Stitch__
12 #define __TIL_Stitch__
13 
14 #include "TIL_API.h"
15 #include <UT/UT_Rect.h>
16 #include <UT/UT_Span.h>
17 #include <UT/UT_Array.h>
18 #include <UT/UT_NonCopyable.h>
19 #include <IMG/IMG_File.h>
20 
21 /// @brief Combine a number of separate tiles into a single image.
22 ///
23 /// The stitcher takes a list of readers (one for each tile) and a single image
24 /// writer. The readers and writers need to have the same AOV definitions.
25 namespace TIL_Stitch
26 {
27  /// Class to store scanline data.
29  : public UT_NonCopyable
30  {
31  public:
32  Scanline();
33  virtual ~Scanline();
34  };
35 
36  /// Class to represent an input "tile" which is stitched into the final
37  /// image
39  : public UT_NonCopyable
40  {
41  public:
42  TileReader();
43  virtual ~TileReader();
44 
45  /// Return the data window of the tile in pixels
46  virtual const UT_DimRect &dataWindow() const = 0;
47 
48  virtual bool canRead(const Scanline *scanline) const = 0;
49 
50  /// Open tile for reading
51  virtual bool open() = 0;
52 
53  /// Read an AOV from the tile
54  virtual bool readScanline(int plane, int xoff, int scanline,
55  Scanline *buffer) = 0;
56 
57  /// Close tile when finished reading
58  virtual bool close() = 0;
59  };
60 
61  /// Class to read the tile data and write it to an output sink.
63  : public UT_NonCopyable
64  {
65  public:
66  StitchWriter();
67  virtual ~StitchWriter();
68 
69  // Return the dimensions of the data window
70  virtual const UT_DimRect &dataWindow() const = 0;
71 
72  // Start a new row of tiles (i.e. this tile has a different y value
73  // than the previous tile). By default this does nothing. Return @c
74  // false if there's an error.
75  virtual bool startRow(const TileReader &first);
76 
77  // Called when the last tile has been written
78  virtual bool finishImage();
79 
80  // Process a tile reader
81  virtual bool process(TileReader &tile) = 0;
82  };
83 
85 
87  bool top_down);
88 
89  /// Convenience class to represent a raster scanline
90  class TIL_API IMGScanline : public Scanline
91  {
92  public:
93  struct ScanData
94  {
95  const uint8 *data(exint xoff) const { return myData.get() + myBPP*xoff; }
96  uint8 *data(exint xoff) { return myData.get() + myBPP*xoff; }
97 
99  const IMG_Plane *myPlane = nullptr;
100  exint myBPP = 0;
101  };
102  IMGScanline();
103  ~IMGScanline() override;
104 
105  void allocate(int width,
106  const UT_Array<const IMG_Plane *> &planes);
107  const uint8 *data(int plane, int xoff) const
108  {
109  return myPlanes[plane].data(xoff);
110  }
111  uint8 *data(int plane, int xoff)
112  {
113  return myPlanes[plane].data(xoff);
114  }
115 
116  int numPlanes() const { return myNumPlanes; }
117  const IMG_Plane *plane(int i) const { return myPlanes[i].myPlane; }
118 
119  private:
120  UT_UniquePtr<ScanData[]> myPlanes;
121  int myNumPlanes = 0;
122  int myWidth = 0;
123  };
124 
125  /// Convenience class to read a tile from a file
126  /// @note this class expects the scanlines to be interleaved (rgbrgb...)
128  {
129  public:
130  IMGTileReader();
131  ~IMGTileReader() override;
132 
133  struct OpenStyle
134  {
135  bool isValid() const { return myFilename != nullptr; }
136  IMG_FilePtr open();
137 
138  const char *myFilename = nullptr;
139  const IMG_FileParms *myOptions = nullptr;
140  const IMG_Format *myFormat = nullptr;
142  };
143 
144  bool setFile(const char *filename,
145  const IMG_FileParms *options = nullptr,
146  const IMG_Format *fmt = nullptr)
147  {
148  myOpen.myFilename = filename;
149  myOpen.myOptions = options;
150  myOpen.myFormat = fmt;
151  myOpen.myFormatList.clear();
152  return initStat();
153  }
154  bool setFile(const char *filename,
155  const UT_Array<const IMG_Format *> &formats,
156  const IMG_FileParms *options = nullptr)
157  {
158  myOpen.myFilename = filename;
159  myOpen.myOptions = options;
160  myOpen.myFormat = nullptr;
161  myOpen.myFormatList = formats;
162  return initStat();
163  }
164 
165  const UT_DimRect &dataWindow() const override;
166  bool open() override;
167  bool canRead(const Scanline *scanline) const override;
168  bool readScanline(int plane, int xoff, int scanline,
169  Scanline *buffer) override;
170  bool close() override;
171 
172  bool isValid() const { return myFile.get() != nullptr; }
173 
174  IMG_FilePtr stealStream() { return std::move(myFile); }
175  const char *getFilename() const { return myOpen.myFilename; }
176  protected:
177  bool init(IMG_FilePtr fp);
178  bool initStat();
179  virtual void error(const char *msg) const;
180 
184  };
185 
186  /// Convenience class to write to an image file
187  /// @note this class expects the scanlines to be interleaved (rgbrgb...)
189  {
190  public:
191  // Takes ownership of the file
193  ~IMGWriter() override;
194 
195  const UT_DimRect &dataWindow() const override;
196  bool startRow(const TileReader &row) override;
197  bool finishImage() override;
198  bool process(TileReader &tile) override;
199 
200  int tilesRead() const { return myTilesRead; }
201 
202  protected:
203  virtual bool flushScanlines();
204  virtual void error(const char *msg) const;
205  virtual void warning(const char *msg) const;
206 
211  exint myBytesPerPixel; // Total bytes per pixel
212  int myY0, myY1; // Span of scanlines of current row
214  };
215 };
216 
217 #endif
GLint first
Definition: glcorearb.h:405
Class to store scanline data.
Definition: TIL_Stitch.h:28
bool setFile(const char *filename, const IMG_FileParms *options=nullptr, const IMG_Format *fmt=nullptr)
Definition: TIL_Stitch.h:144
GT_API const UT_StringHolder filename
UT_DimRect myDataWindow
Definition: TIL_Stitch.h:208
SYS_FORCE_INLINE void clear()
const uint8 * data(exint xoff) const
Definition: TIL_Stitch.h:95
TIL_API bool stitch(StitchWriter &w, const UT_Span< TileReader * > &tiles, bool top_down)
int64 exint
Definition: SYS_Types.h:125
void close() override
Describes the format and layout of a single plane in an image The plane specifies the format and name...
Definition: IMG_Plane.h:48
GLuint buffer
Definition: glcorearb.h:660
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
< returns > If no error
Definition: snippets.dox:2
unsigned char uint8
Definition: SYS_Types.h:36
Convenience class to represent a raster scanline.
Definition: TIL_Stitch.h:90
int open(float queuesize) override
Class to read the tile data and write it to an output sink.
Definition: TIL_Stitch.h:62
UT_Array< const IMG_Plane * > myPlanes
Definition: TIL_Stitch.h:209
const uint8 * data(int plane, int xoff) const
Definition: TIL_Stitch.h:107
UT_UniquePtr< uint8[]> myData
Definition: TIL_Stitch.h:98
IMG_FilePtr stealStream()
Definition: TIL_Stitch.h:174
int numPlanes() const
Definition: TIL_Stitch.h:116
UT_Array< IMGScanline > myScanBuffer
Definition: TIL_Stitch.h:210
UT_Array< const IMG_Format * > myFormatList
Definition: TIL_Stitch.h:141
uint8 * data(int plane, int xoff)
Definition: TIL_Stitch.h:111
File options for manipulating image data on load or save. This class allows you to modify the incomin...
Definition: IMG_FileParms.h:39
const IMG_Plane * plane(int i) const
Definition: TIL_Stitch.h:117
uint8 * data(exint xoff)
Definition: TIL_Stitch.h:96
bool setFile(const char *filename, const UT_Array< const IMG_Format * > &formats, const IMG_FileParms *options=nullptr)
Definition: TIL_Stitch.h:154
bool process(T &func, UT_WorkBuffer &fullpath, exint fullpath_len, const UT_StringArray &paths, const UT_Array< FS_Stat > &stats)
Utility function to process the contents of the traverse() function.
Definition: FS_Traverse.h:24
GLint GLsizei width
Definition: glcorearb.h:103
TIL_API UT_DimRect unionDataWindow(const UT_Span< const TileReader * > &tiles)
int tilesRead() const
Definition: TIL_Stitch.h:200
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GLenum GLenum GLsizei void * row
Definition: glad.h:5135
IMG_FilePtr myFile
Definition: TIL_Stitch.h:207
const char * getFilename() const
Definition: TIL_Stitch.h:175
UT_UniquePtr< IMG_File > IMG_FilePtr
Definition: IMG_File.h:54
#define TIL_API
Definition: TIL_API.h:10