HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMG_Format.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_Format.h ( IMG Library, C++)
7  *
8  * COMMENTS: Declaration of a file format. This class is responsible
9  */
10 
11 #ifndef __IMG_Format__
12 #define __IMG_Format__
13 
14 #include "IMG_API.h"
15 #include "IMG_FileTypes.h"
16 #include <UT/UT_Array.h>
17 #include <UT/UT_UniquePtr.h>
18 #include <UT/UT_NonCopyable.h>
19 #include <UT/UT_StringHolder.h>
20 #include <SYS/SYS_Types.h>
21 
22 class UT_IStream;
23 class UT_String;
24 class IMG_File;
25 class IMG_Stat;
26 class IMG_FileOptionList;
27 class IMG_FileTagList;
28 class IMG_Metadata;
30 
31 extern "C" {
32  // DSO's should have this function declared. All it has to do is create a
33  // format of the new type. This will automatically install the format.
34  // The argument passed in will be a null pointer and can be ignored.
36 };
37 
38 /// Description of a specific image file format, describing the
39 /// characteristics and capabilities of a specific image file format.
40 /// It can be used to query the capabilities of that format (resolution limits,
41 /// data formats supported, image orientation, etc). Image file-specific data
42 /// (such as resolution of a given file) can be found in IMG_Stat. @n
43 /// When writing a new data format, many of the virtuals will need to be
44 /// overridden to properly describe its capabilities.
46 {
47 public:
48  IMG_Format();
49  /// This is a version of the constructor that lets you avoid adding
50  /// oneself to the list. This is used by the pass throug filter and
51  /// must be used if you are in multithreaded code as the underlying
52  /// list is not locked.
53  explicit IMG_Format(bool addtolist);
54  virtual ~IMG_Format();
55 
57 
58  // Types of access for image formats.
60  {
62  WRITE_ACCESS
63  };
64 
65  /// @{
66  /// number of currently installed image formats
67  static int getNFormats();
68  /// Access to the list of currently installed image formats
69  static const IMG_Format *getFormat(int idx);
70  /// @}
71 
72  /// @{
73  /// Find a format given a filename and a stat - only extension matching.
74  /// Will only return formats that can read files (isReadable()).
75  static const IMG_Format *findFormatReadable(const char *filename,
76  const IMG_Stat *stat = 0);
77  /// Find a format given a filename and a stat - only extension matching.
78  /// Will only return formats that can write files (isWritable()).
79  static const IMG_Format *findFormatWritable(const char *filename,
80  const IMG_Stat *stat = 0);
81 
82  static void findFormatReadable(
84  const char *filename,
85  const IMG_Stat *stat = nullptr,
86  const UT_Array<const IMG_Format*> *image_formats = nullptr);
87  static void findFormatWritable(
89  const char *filename,
90  const IMG_Stat *stat = nullptr,
91  const UT_Array<const IMG_Format*> *image_formats = nullptr);
92 
93  /// @}
94  static const IMG_Format *findFormatByName(const char *format_name);
95  static const IMG_Format *findFormatByLabel(const char *format_label);
96  // Find a format given a seekable istream.
97  static const IMG_Format *findFormatSeekable(UT_IStream &is);
98  static const IMG_Format *findFormatNonSeekable(UT_IStream &is);
99  static const IMG_Format *findFormatByExtension(const char *filename);
100  static const IMG_Format *getStdoutFormat();
101  IMG_File *allocValidFile() const;
102 
103  /// Find the format which has the metdata specific prefix. For example
104  /// passing "OpenEXR:compression" will return the IMG_Format used for
105  /// OpenEXR images. Returns @c nullptr if there's no format prefix.
106  static const IMG_Format *metadataPrefix(const char *key);
107 
108  /// @{
109  /// All formats must have a unique name (like "JPEG").
110  virtual const char *getFormatName() const = 0;
111  /// If getFormatLabel() is not overridden, the label will be the same as
112  /// the name.
113  virtual const char *getFormatLabel() const;
114 
115  /// When looking up metadata, this is the key to identify format specific
116  /// metadata. This method defaults to returning @c getFormatName().
117  virtual const char *getMetadataKey() const;
118  /// @}
119 
120  /// This can be optionally overridden to return a short description of the
121  /// format. By default, no description is given.
122  virtual const char *getFormatDescription() const;
123 
124  /// Flags whether the format is a pass through format like Flip or Scale.
125  /// Specific image formats (like JPEG) are never pass-through.
126  virtual bool isPassThrough() const { return false; }
127 
128  /// The default extension is used to determine whether the format should
129  /// appear in the device/format menus. It should not contain the '.'
130  /// (i.e. "tif" not ".tif")
131  virtual const char *getDefaultExtension() const;
132 
133  /// Describes how the format was loaded (i.e. DSO or internal formats)
134  virtual const char *getFormatLocation() const { return myLocation; }
135 
136  /// @{
137  /// Methods to determine if this is one of our recognized files.
138  /// The extension is the first try. If there are multiple matches,
139  /// then we resort to the magic number (when reading)
140  virtual int checkExtension(const char *filename) const = 0;
141  /// If possible, this magic numver checker will be called. This is
142  /// used if the file is "seekable" (i.e. that we can rewind after checking)
143  virtual int checkMagic(unsigned int) const;
144  /// If possible, this second magic number checker will be called. This is
145  /// used if the file is "seekable" (i.e. that we can rewind after checking)
146  virtual int checkMagicSeekable(UT_IStream &is) const;
147  /// @}
148 
149  /// The device method gives a higher priority to device checks. By default,
150  /// this method returns 0 (no match). This is used to allow things like
151  /// a60:3.pic (which uses the abekas device).
152  virtual int checkDevice(const char *) const;
153 
154  // ------------------------------------------------------------------
155  // Image Format features
156 
157  /// Returns a bitfield of image types supported by this format. By default
158  /// IMG_TYPE_2D is returned.
159  virtual IMG_ImageType getSupportedImageTypes() const;
160 
161  /// Returns a bitfield of data types supported by this format.
162  virtual IMG_DataType getSupportedTypes() const = 0;
163 
164  /// Returns a bitfield of supported color models. If IMG_CM_REVERSED is
165  /// set, this format stores data in BGR/ABGR format.
166  virtual IMG_ColorModel getSupportedColorModels() const = 0;
167 
168  /// Maximum allowable resolution for the file. If a user attempts to write
169  /// an image larger than this, the image will be scaled to this resolution.
170  virtual void getMaxResolution(unsigned &x,
171  unsigned &y) const = 0;
172 
173  /// @{
174  /// Some formats can be read and written in random order. Others require
175  /// strict sequential order. If these methods return false, you should
176  /// read and write scanlines in ascending order. When writing, they will
177  /// be cached until they can be written sequentially.
178  virtual int isReadRandomAccess() const;
179  /// If 0 is returned, the scanlines must be written in sequential order or
180  /// they will be cached until all scanlines can be written sequentially.
181  virtual int isWriteRandomAccess() const;
182  /// @}
183 
184  /// vertical data orientation. Where (0,0) lies in the image
185  /// (top-left by default).
186  virtual int isTopFirst() const;
187 
188  /// horizontal data orientation. Where (0,0) lies in the image
189  /// (top-left by default).
190  virtual int isLeftFirst() const;
191 
192  /// @{
193  /// Specifies if this format can read an image file. One of isReadable()
194  /// or isWritable() (or both) must return true.
195  virtual bool isReadable() const;
196  /// Specifies if this format can write an image file. One of isReadable()
197  /// or isWritable() (or both) must return true.
198  virtual bool isWritable() const;
199  //// @}
200 
201  // data organization.
202 
203  /// does this format support a data window or sub-area.
204  virtual bool isDataWindowSupported() const;
205 
206  /// if true, this format only supports data windows contained within
207  /// the image resolution (a crop region).
208  virtual bool isDataWindowCropOnly() const;
209 
210  /// does this format support saving whether the data outside the window
211  /// is streaked from the window edges?
212  virtual bool isDataWindowStreakSupported() const;
213 
214  /// does this format support deep rasters or multiple images?
215  /// If not, only one plane of the color models that this format supports
216  /// is allowed.
217  virtual bool isDeepRasterSupported() const;
218 
219  /// does this deep raster allow RGBA in one plane (true) or split into
220  /// 2 planes (RGB, A). (default false)
221  virtual IMG_DeepRasterColor getDeepRasterRGBASupport() const;
222 
223  /// Specifies whether this format stores its data interleaved (RGBRGBRGB)
224  /// or non-interleaved (RRRGGGBBB). If your format supports both, pick one.
225  virtual bool isDataInterleaved() const;
226 
227  /// if true, planes can have different data formats & components. Otherwise,
228  /// all planes must have the same data type and number of components.
229  virtual bool canPlaneTypesDiffer() const;
230 
231  /// Returns a list of options that can be set on the format when reading or
232  /// writing (like 'compression' or 'comment').
233  virtual const IMG_FileOptionList *getOptions() const;
234 
235  /// Returns list of well known metadata for the format
236  virtual const IMG_MetadataOptions &formatMetadata() const;
237 
238  /// @{
239  /// Convenience methods to get common items for metadata
240  static UT_StringHolder artistDefault();
241  static UT_StringHolder hostnameDefault();
242  /// @}
243 
244  /// @private
245  // Allocate default tags and their values
246  void SYS_DEPRECATED(20.0) defaultTags(IMG_FileTagList &tags) const;
247 
248  /// @private
249  // Allocate default metadata
250  void defaultMetadata(IMG_Stat &stat) const;
251 
252  /// Set an option globally for all formats. Use with extreme care.
253  static void setGlobalOption(const char *name, const char *value);
254  /// Read the value of a global option. May return a NULL string.
255  static void getGlobalOption(const char *name, UT_String &value);
256 
257  /// Allows you to change an option on a specific format. This will change
258  /// the option for all formats from this point on, whereas setGlobalOption
259  /// would not. Returns false if the format or the option couldn't be found.
260  static bool setFormatOptionDefault(const char *format_name,
261  const char *format_option,
262  const char *defvalue);
263  /// Returns the value of a format-specific option. Returns false if the
264  /// format or option does not exist.
265  static bool getFormatOptionDefault(const char *format_name,
266  const char *format_option,
267  UT_String &defvalue);
268 
269  /// Matches a filename against a single extension
270  static int matchExtension(const char *filename, const char *ext);
271 
272  /// Matches a filename against a list of extensions (the last entry must be
273  /// NULL)
274  static int matchExtensions(const char *filename, const char *ext[]);
275 
276  /// Between these two formats, which should be preferred? returns either
277  /// this or the format parameter. This method is used to resolve
278  /// conflicts between formats when files are being created.
279  virtual const IMG_Format *resolvePriority(const IMG_Format *format)
280  const;
281 
282  /// Some image formats have fixed color spaces, like JPEG (sRGB). The
283  /// default is PXL_CS_UNKNOWN. For formats that can have colorspaces per
284  /// plane, return PXL_CS_UNKNOWN and query the colorspace from IMG_Plane.
285  /// formatColorSpaceGamma() is ignored unless the color space is
286  /// PXL_CS_CUSTOM_GAMMA.
287  /// @{
288  virtual PXL_ColorSpace formatColorSpace() const;
289  virtual fpreal formatColorSpaceGamma() const;
290 
291  /// @}
292 
293  /// Return true if the image format can store the colorspace
294  virtual bool formatStoresColorSpace() const;
295 
296  /// @{
297  /// Returns a gamma correction factor when converting from a given color
298  /// space to this image format's space. 'src_data_is_8bit' indicates whether
299  /// data is fixed 8b, in which case it will often be converted to 2.2 gamma.
300  /// If the dest_data_is_8bit is provided, it will use that flag for
301  /// the destination 8bit type instead of the src_data_is_8bit.
302  fpreal adjustGammaForFormat(PXL_ColorSpace src_color_space,
303  fpreal src_color_space_gamma,
304  bool src_data_is_8bit,
305  bool *dest_data_is_8bit = NULL) const
306  {
307  return adjustGammaForFormat(&src_color_space,
308  src_color_space_gamma, src_data_is_8bit,
309  dest_data_is_8bit);
310  }
311  fpreal adjustGammaForFormat(PXL_ColorSpace *src_color_space,
312  fpreal src_color_space_gamma,
313  bool src_data_is_8bit,
314  bool *dest_data_is_8bit) const;
315  /// @}
316 
317  /// @private
318  static void installBasics();
319 
320 protected:
321  // Returns 1 if the stat's are ok for the format specified. For example,
322  // if there's a fixed resolution for a format, the format can nullify the
323  // validity of the format in the determination stage. By default, we
324  // simply check the image depth (i.e. number of images) and compare the max
325  // resolution.
326  virtual IMG_File *createFile() const = 0;
327  virtual int isFormatOk(const IMG_Stat &stat) const;
328  void removeFormatFromList();
329 
330  static const IMG_Format *findFormat(const char *filename,
331  const IMG_Stat *stat,
332  AccessType access);
333  static void findFormat(
335  const char *filename,
336  const IMG_Stat *stat,
337  AccessType access,
338  const UT_Array<const IMG_Format*> *image_formats = nullptr);
339 
340  const IMG_Metadata *getGlobalTags();
341 
342 private:
343  static const IMG_Format *lookupFormatByName(const char *name,
344  const UT_Array<const IMG_Format *> *list);
345  static void loadGlobalOptions();
346  static bool loadOldFBOptions();
347  static bool emptyFormats();
348  static void adjustPriorities();
349 
350  mutable UT_UniquePtr<IMG_MetadataOptions> myDeprecatedMetadata;
351  UT_StringHolder myLocation;
352 };
353 
354 #endif
355 
GT_API const UT_StringHolder filename
#define SYS_DEPRECATED(__V__)
IMG_ImageType
Type of image we want to create or have opened.
#define SYS_VISIBILITY_EXPORT
IMG_DeepRasterColor
GLint y
Definition: glcorearb.h:103
virtual const char * getFormatLocation() const
Describes how the format was loaded (i.e. DSO or internal formats)
Definition: IMG_Format.h:134
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
#define IMG_API
Definition: IMG_API.h:10
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:2222
IMG_DataType
Definition: IMG_FileTypes.h:17
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
SYS_VISIBILITY_EXPORT void newIMGFormat(void *)
Definition: IMG_Sample.C:321
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLenum GLint x
Definition: glcorearb.h:409
A list of well-known metdata that a format understands. The list is ordered.
Definition: IMG_Metadata.h:370
fpreal64 fpreal
Definition: SYS_Types.h:277
PXL_ColorSpace
Definition: PXL_Common.h:72
IMG_ColorModel
Definition: IMG_FileTypes.h:53
virtual bool isPassThrough() const
Definition: IMG_Format.h:126
Contains the details of a specific image file, used by IMG_File. This class contains all the high-lev...
Definition: IMG_Stat.h:38
Map of metadata items.
Definition: IMG_Metadata.h:208
Definition: core.h:1131
#define const
Definition: zconf.h:214