Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bitmap.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_BITMAP_H_)
21 #define __MITSUBA_CORE_BITMAP_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <mitsuba/core/stream.h>
25 #include <mitsuba/core/rfilter.h>
26 #include <mitsuba/core/half.h>
27 
29 
30 /**
31  * \brief General-purpose bitmap class with read and write support
32  * for several common file formats.
33  *
34  * This class handles loading of PNG, JPEG, BMP, TGA, as well as
35  * OpenEXR files, and it supports writing of PNG, JPEG and OpenEXR files.
36  *
37  * PNG and OpenEXR files are optionally annotated with string-valued
38  * metadata, and the gamma setting can be stored as well. Please see
39  * the class methods and enumerations for further detail.
40  *
41  * The Python version of this API contains thee additional member functions:
42  * <tt>fromByteArray</tt> and <tt>toByteArray</tt> copy image data
43  * between the Bitmap instance and a Python <tt>bytearray</tt>. The function
44  * <tt>buffer</tt> returns a <tt>memoryview</tt>-compatible buffer
45  * object that can also interoperate with <tt>numpy</tt>'s <tt>ndarray</tt>.
46  *
47  * \ingroup libcore
48  * \ingroup libpython
49  */
50 class MTS_EXPORT_CORE Bitmap : public Object {
51 public:
52  // ======================================================================
53  //! @{ \name Constructors and Enumerations
54  // ======================================================================
55 
56  /**
57  * This enumeration lists all pixel format types supported
58  * by the \ref Bitmap class. This both determines the
59  * number of channels, and how they should be interpreted
60  */
61  enum EPixelFormat {
62  /// Single-channel luminance bitmap
63  ELuminance = 0x00,
64 
65  /// Two-channel luminance + alpha bitmap
66  ELuminanceAlpha = 0x01,
67 
68  /// RGB bitmap
69  ERGB = 0x02,
70 
71  /// RGB bitmap + alpha channel
72  ERGBA = 0x03,
73 
74  /// XYZ tristimulus bitmap
75  EXYZ = 0x04,
76 
77  /// XYZ tristimulus + alpha channel
78  EXYZA = 0x05,
79 
80  /// Spectrum bitmap
81  ESpectrum = 0x06,
82 
83  /// Spectrum bitmap + alpha channel
84  ESpectrumAlpha = 0x07,
85 
86  /// Spectrum bitmap + alpha + weight channel (Mitsuba's internal render bucket representation)
87  ESpectrumAlphaWeight = 0x08,
88 
89  /// Bitmap with multiple spectra + alpha + weight channel (render buckets used by the 'multichannel' plugin)
90  EMultiSpectrumAlphaWeight = 0x09,
91 
92  /// Arbitrary multi-channel bitmap without a fixed interpretation
93  EMultiChannel = 0x10
94  };
95 
96  /// Supported per-component data formats
98  /**
99  * \brief 1-bit (mask) component encoding.
100  *
101  * Note that you can use this class to load, save, and access bitmasks -- however,
102  * many of the manipulation operations (i.e. \ref crop(), \ref accumulate(), etc.)
103  * do not currently handle them.
104  *
105  * Default gamma value: linear (1.0)
106  */
107  EBitmask = 0,
108 
109  /**
110  * \brief 8-bit unsigned integer (\c uint8_t) component encoding
111  *
112  * Default gamma value: sRGB (approx. 2.2)
113  */
115 
116  /**
117  * \brief 16-bit unsigned integer (\c uint16_t) component encoding
118  *
119  * Default gamma value: linear (1.0)
120  */
122 
123  /**
124  * \brief 32-bit unsigned integer (\c uint32_t) component encoding
125  *
126  * Default gamma value: linear (1.0)
127  */
129 
130  /**
131  * 16-bit floating point (\c half) HDR component encoding
132  *
133  * Default gamma value: linear (1.0)
134  */
136 
137  /**
138  * \brief 32-bit floating point (\c float) HDR component encoding
139  *
140  * Default gamma value: linear (1.0)
141  */
143 
144  /**
145  * \brief 64-bit floating point (\c double) HDR component encoding
146  *
147  * Default gamma value: linear (1.0)
148  */
150 
151  /**
152  * \brief Invalid component format (used to report error conditions)
153  */
155 
156  /**
157  * \brief Floating point (\c float or \c double depending on the
158  * compilation settings) HDR component encoding
159  *
160  * Default gamma value: linear (1.0)
161  */
162 #if defined(SINGLE_PRECISION)
163  EFloat = EFloat32
164 #else
165  EFloat = EFloat64
166 #endif
167  };
168 
169  /// Supported file formats
170  enum EFileFormat {
171  /**
172  * \brief Portable network graphics
173  *
174  * The following is supported:
175  * <ul>
176  * <li> Loading and saving of 8/16-bit per component bitmaps for
177  * all pixel formats (ELuminance, ELuminanceAlpha, ERGB, ERGBA)</li>
178  * <li> Loading and saving of 1-bit per component mask bitmaps</li>
179  * <li> Loading and saving of string-valued metadata fields</li>
180  * </ul>
181  */
182  EPNG = 0,
183 
184  /**
185  * \brief OpenEXR high dynamic range file format developed by
186  * Industrial Light & Magic (ILM)
187  *
188  * The following is supported:
189  * <ul>
190  * <li>Loading and saving of \ref Eloat16 / \ref EFloat32/ \ref
191  * EUInt32 bitmaps with all supported RGB/Luminance/Alpha combinations</li>
192  * <li>Loading and saving of spectral bitmaps</tt>
193  * <li>Loading and saving of XYZ tristimulus bitmaps</tt>
194  * <li>Loading and saving of string-valued metadata fields</li>
195  * </ul>
196  *
197  * The following is <em>not</em> supported:
198  * <ul>
199  * <li>Saving of tiled images, tile-based read access</li>
200  * <li>Display windows that are different than the data window</li>
201  * <li>Loading of spectrum-valued bitmaps</li>
202  * </ul>
203  */
205 
206  /**
207  * \brief RGBE image format by Greg Ward
208  *
209  * The following is supported
210  * <ul>
211  * <li>Loading and saving of \ref EFloat32 - based RGB bitmaps</li>
212  * </ul>
213  */
215 
216  /**
217  * \brief PFM (Portable Float Map) image format
218  *
219  * The following is supported
220  * <ul>
221  * <li>Loading and saving of \ref EFloat32 - based Luminance or RGB bitmaps</li>
222  * </ul>
223  */
225 
226  /**
227  * \brief PPM (Portable Pixel Map) image format
228  *
229  * The following is supported
230  * <ul>
231  * <li>Loading and saving of \ref EUInt8 and \ref EUInt16 - based RGB bitmaps</li>
232  * </ul>
233  */
235 
236  /**
237  * \brief Joint Photographic Experts Group file format
238  *
239  * The following is supported:
240  * <ul><li>
241  * Loading and saving of 8 bit per component RGB and
242  * luminance bitmaps
243  * </li></ul>
244  */
246 
247  /**
248  * \brief Truevision Advanced Raster Graphics Array file format
249  *
250  * The following is supported:
251  * <ul><li>Loading of uncompressed 8-bit RGB/RGBA files</ul></li>
252  */
254 
255  /**
256  * \brief Windows Bitmap file format
257  *
258  * The following is supported:
259  * <ul><li>Loading of uncompressed 8-bit luminance and RGBA bitmaps</ul></li>
260  */
262 
263  /**
264  * \brief Automatically detect the file format
265  *
266  * Note: this flag only applies when loading a file. In this case,
267  * the source stream must support the \c seek() operation.
268  */
269  EAuto
270  };
271 
272  /// List of different rotation/flip types that can be passed to \ref rotateFlip()
274  ERotateNoneFlipNone = 0,
275  ERotate180FlipXY = ERotateNoneFlipNone,
276  ERotate90FlipNone = 1,
277  ERotate270FlipXY = ERotate90FlipNone,
278  ERotate180FlipNone = 2,
279  ERotateNoneFlipXY = ERotate180FlipNone,
280  ERotate270FlipNone = 3,
281  ERotate90FlipXY = ERotate270FlipNone,
282  ERotateNoneFlipX = 4,
283  ERotate180FlipY = ERotateNoneFlipX,
284  ERotate90FlipX = 5,
285  ERotate270FlipY = ERotate90FlipX,
286  ERotate180FlipX = 6,
287  ERotateNoneFlipY = ERotate180FlipX,
288  ERotate270FlipX = 7,
289  ERotate90FlipY = ERotate270FlipX
290  };
291 
292  /// The four basic arithmetic operations for use with \ref arithmeticOperation()
294  EAddition = 0,
297  EDivision
298  };
299 
300  /**
301  * \brief Describes a sub-layer of a multilayer bitmap (e.g. OpenEXR)
302  *
303  * A layer is defined as a named collection of bitmap channels and a pixel format
304  *
305  * This data structure is used by \ref Bitmap::getLayers()
306  */
307  struct Layer {
308  /// Descriptive name of the bitmap layer
309  std::string name;
310  /// Pixel format that should be associated with underlying combination of bitmap channels
312  /// References to bitmap channels
313  std::vector<int> channels;
314  };
315 
316  /**
317  * \brief Create a bitmap of the specified type and allocate
318  * the necessary amount of memory
319  *
320  * \param pFmt
321  * Specifies the pixel format (e.g. RGBA or Luminance-only)
322  *
323  * \param cFmt
324  * Specifies how the per-pixel components are encoded
325  * (e.g. unsigned 8 bit integers or 32-bit floating point values)
326  *
327  * \param size
328  * Specifies the horizontal and vertical bitmap size in pixels
329  *
330  * \param channelCount
331  * Channel count of the image. This parameter is only required when
332  * \c pFmt = \ref EMultiChannel or \ref EMultiSpectrumAlphaWeight
333  *
334  * \param data
335  * External pointer to the image data. If set to \c NULL, the
336  * implementation will allocate memory itself.
337  */
338  Bitmap(EPixelFormat pFmt, EComponentFormat cFmt, const Vector2i &size,
339  uint8_t channelCount = 0, uint8_t *data = NULL);
340 
341  /**
342  * \brief Load a bitmap from an arbitrary stream data source
343  *
344  * \param format
345  * File format to be read (PNG/EXR/Auto-detect ...)
346  *
347  * \param stream
348  * Pointer to an arbitrary stream data source
349  *
350  * \param prefix
351  * Only consider image layers whose identifier begins with \c prefix.
352  * This is currently only supported by the OpenEXR format loader.
353  */
354  Bitmap(EFileFormat format, Stream *stream, const std::string &prefix = "");
355 
356  /**
357  * \brief Load a bitmap from a file on disk
358  *
359  * \param path
360  * Path to a bitmap file (format will be auto-detected)
361  *
362  * \param prefix
363  * Only consider image layers whose identifier begins with \c prefix.
364  * This is currently only supported by the OpenEXR format loader.
365  */
366  Bitmap(const fs::path &path, const std::string &prefix = "");
367 
368  //! @}
369  // ======================================================================
370 
371  // ======================================================================
372  //! @{ \name Miscellaneous
373  // ======================================================================
374 
375  /// Return the bitmap component format for a specified type
376  template <typename T> inline static EComponentFormat componentFormat();
377 
378  /// Return the pixel format of this bitmap
379  inline EPixelFormat getPixelFormat() const { return m_pixelFormat; }
380 
381  /// Return the component format of this bitmap
382  inline EComponentFormat getComponentFormat() const { return m_componentFormat; }
383 
384  /// Return the bitmap's resolution in pixels
385  inline const Vector2i &getSize() const { return m_size; }
386 
387  /// Return the total number of pixels
388  inline size_t getPixelCount() const { return (size_t) m_size.x * (size_t) m_size.y; }
389 
390  /// Return the bitmap's width in pixels
391  inline int getWidth() const { return m_size.x; }
392 
393  /// Return the bitmap's height in pixels
394  inline int getHeight() const { return m_size.y; }
395 
396  /// Return the number of channels used by this bitmap
397  inline int getChannelCount() const { return (int) m_channelCount; }
398 
399  /// Return whether this image has matching width and height
400  inline bool isSquare() const { return m_size.x == m_size.y; }
401 
402  /// Return a string representation of the name of a channel
403  std::string getChannelName(int channelIndex) const;
404 
405  /// Return whether this image has an alpha channel
406  inline bool hasAlpha() const {
407  return
408  m_pixelFormat == ELuminanceAlpha ||
409  m_pixelFormat == ERGBA ||
410  m_pixelFormat == EXYZA ||
411  m_pixelFormat == ESpectrumAlpha;
412  }
413 
414  /// Return whether this image has a weight channel
415  inline bool hasWeight() const {
416  return m_pixelFormat == ESpectrumAlphaWeight ||
417  m_pixelFormat == EMultiSpectrumAlphaWeight;
418  }
419 
420  /// Return whether this is a generic multi-channel image
421  inline bool isMultiChannel() const {
422  return m_pixelFormat == EMultiChannel ||
423  m_pixelFormat == EMultiSpectrumAlphaWeight;
424  }
425 
426  /**
427  * \brief Return the number bits per component
428  *
429  * When the component format is set to \c EBitmask,
430  * this function returns 1; \c EUInt8 maps to 8, etc.
431  */
432  int getBitsPerComponent() const;
433 
434  /**
435  * \brief Return the number bytes per component
436  *
437  * When the component format is set to \c EBitmask,
438  * this function throws an exception.
439  */
440  int getBytesPerComponent() const;
441 
442  /**
443  * \brief Return the number of bytes per pixel
444  *
445  * When the component format is set to \c EBitmask,
446  * this function throws an exception.
447  */
448  inline int getBytesPerPixel() const {
449  return getBytesPerComponent() * getChannelCount(); }
450 
451  /// Return the bitmap size in bytes
452  size_t getBufferSize() const;
453 
454  /**
455  * \brief Return the spectral color value associated with
456  * a given pixel.
457  *
458  * When this is not a spectrum image, conversions are applied
459  * as appropriate.
460  *
461  * \remark This function is provided here for convenience and
462  * debugging purposes. Its use is discouraged, particularly for
463  * performance critical code (direct buffer access will be
464  * faster by at least an order of magnitude)
465  */
466  Spectrum getPixel(const Point2i &pos) const;
467 
468  /**
469  * \brief Set the spectral color value associated with
470  * a given pixel.
471  *
472  * When this is not a spectrum image, conversions are applied
473  * as appropriate.
474  *
475  * \remark This function is provided here for convenience and
476  * debugging purposes. Its use is discouraged, particularly for
477  * performance critical code (direct buffer access will be
478  * faster by at least order of magnitude)
479  */
480  void setPixel(const Point2i &pos, const Spectrum &value);
481 
482  /**
483  * \brief Draw a horizontal line at the specified position
484  *
485  * Draws from x1<x2 up to and including x2.
486  */
487  void drawHLine(int y, int x1, int x2, const Spectrum &value);
488 
489  /**
490  * \brief Draw a vertical line at the specified position
491  *
492  * Draws from y1<y2 up to and including y2.
493  */
494  void drawVLine(int x, int y1, int y2, const Spectrum &value);
495 
496  /// Draw a rectangle with the specified position and size
497  void drawRect(const Point2i &offset, const Vector2i &size, const Spectrum &value);
498 
499  /// Draw a filled rectangle with the specified position and size
500  void fillRect(Point2i offset, Vector2i size, const Spectrum &value);
501 
502  /**
503  * \brief Convenience function to visually indicate that a thread is
504  * working on a certain part of an image
505  */
506  void drawWorkUnit(const Point2i &offset, const Vector2i &size, int worker);
507 
508  /// Bitmap equality operator (useful for unit-tests etc.)
509  bool operator==(const Bitmap &bitmap) const;
510 
511  /// Bitmap inequality operator (useful for unit-tests etc.)
512  inline bool operator!=(const Bitmap &bitmap) const
513  { return !operator==(bitmap); }
514 
515  /// Create an identical copy
516  ref<Bitmap> clone() const;
517 
518  /// Clear the bitmap to zero
519  void clear();
520 
521  /**
522  * Write an encoded form of the bitmap to a stream using the specified file format
523  *
524  * \param format
525  * Target file format (\ref EOpenEXR, \ref EPNG, or \ref EOpenEXR)
526  *
527  * \param stream
528  * Target stream that will receive the encoded output
529  *
530  * \param compression
531  * For PNG images, this parameter can be used to control how
532  * strongly libpng will try to compress the output (with 1 being
533  * the lowest and 9 denoting the highest compression). Note that
534  * saving files with the highest compression will be very slow.
535  * For JPEG files, this denotes the desired quality (between 0 and 100,
536  * the latter being best). The default argument (-1) uses compression
537  * 5 for PNG and 100 for JPEG files.
538  */
539  void write(EFileFormat format, Stream *stream, int compression = -1) const;
540 
541  /**
542  * Write an encoded form of the bitmap to a file using the specified file format
543  *
544  * \param format
545  * Target file format (\ref EOpenEXR, \ref EPNG, or \ref EOpenEXR)
546  *
547  * \param stream
548  * Target stream that will receive the encoded output
549  *
550  * \param compression
551  * For PNG images, this parameter can be used to control how
552  * strongly libpng will try to compress the output (with 1 being
553  * the lowest and 9 denoting the highest compression). Note that
554  * saving files with the highest compression will be very slow.
555  * For JPEG files, this denotes the desired quality (between 0 and 100,
556  * the latter being best). The default argument (-1) uses compression
557  * 5 for PNG and 100 for JPEG files.
558  */
559  void write(EFileFormat format, const fs::path &filename, int compression = -1) const;
560 
561  /**
562  * Write an encoded form of the bitmap to a file (auto-detecting the file format)
563  *
564  * \param stream
565  * Target stream that will receive the encoded output
566  *
567  * \param compression
568  * For PNG images, this parameter can be used to control how
569  * strongly libpng will try to compress the output (with 1 being
570  * the lowest and 9 denoting the highest compression). Note that
571  * saving files with the highest compression will be very slow.
572  * For JPEG files, this denotes the desired quality (between 0 and 100,
573  * the latter being best). The default argument (-1) uses compression
574  * 5 for PNG and 100 for JPEG files.
575  */
576  void write(const fs::path &filename, int compression = -1) const;
577 
578  //! @}
579  // ======================================================================
580 
581 
582  // ======================================================================
583  //! @{ \name Conversions and transformations
584  // ======================================================================
585 
586  /**
587  * \brief Convert the bitmap into another pixel and/or component format
588  *
589  * This helper function can be used to efficiently convert a bitmap
590  * between different underlying representations. For instance, it can
591  * translate a 24-bit sRGB bitmap to a linear spectrum-valued representation
592  * based on half-, single- or double-precision floating point-backed storage.
593  *
594  * This function roughly does the following:
595  *
596  * <ul>
597  * <li>For each pixel and channel, it converts the associated value
598  * into a normalized linear-space form (any gamma of the source
599  * bitmap is removed)</li>
600  * <li>The multiplier and gamma correction specified in
601  * \c targetGamma is applied</li>
602  * <li>The corrected value is clamped against the representable range
603  * of the desired component format.</li>
604  * <li>The clamped gamma-corrected value is then written to
605  * the new bitmap</li>
606  *
607  * If the pixel formats differ, this function will also perform basic
608  * conversions (e.g. spectrum to rgb, luminance to uniform spectrum
609  * values, etc.)
610  *
611  * Note that the alpha channel is assumed to be linear in both
612  * the source and target bitmap, hence it won't be affected by
613  * any gamma-related transformations.
614  *
615  * \remark This <tt>convert()</tt> variant takes a pointer to an existing
616  * bitmap, which must have a matching size. It then copies all data, while
617  * performing the necessary pixel/component format and gamma conversions.
618  *
619  * \ref target
620  * Pointer to the target bitmap, which should be of the
621  * same size.
622  * \ref multiplier
623  * An optional multiplicative factor that can be
624  * applied to all color/luminance values in linear
625  * space (alpha will not be affected).
626  * \ref intent
627  * When converting from RGB to spectral color values, this flag
628  * specifies how ambiguities in this highly under-constrained problem
629  * should be resolved.
630  */
631  void convert(Bitmap *target, Float multiplier = 1.0f,
632  Spectrum::EConversionIntent intent = Spectrum::EReflectance) const;
633 
634  /**
635  * \brief Convert the bitmap into another pixel and/or component format
636  *
637  * This helper function can be used to efficiently convert a bitmap
638  * between different underlying representations. For instance, it can
639  * translate a 24-bit sRGB bitmap to a linear spectrum-valued representation
640  * based on half-, single- or double-precision floating point-backed storage.
641  *
642  * This function roughly does the following:
643  *
644  * <ul>
645  * <li>For each pixel and channel, it converts the associated value
646  * into a normalized linear-space form (any gamma of the source
647  * bitmap is removed)</li>
648  * <li>The multiplier and gamma correction specified in
649  * \c targetGamma is applied</li>
650  * <li>The corrected value is clamped against the representable range
651  * of the desired component format.</li>
652  * <li>The clamped gamma-corrected value is then written to
653  * the new bitmap</li>
654  *
655  * If the pixel formats differ, this function will also perform basic
656  * conversions (e.g. spectrum to rgb, luminance to uniform spectrum
657  * values, etc.)
658  *
659  * Note that the alpha channel is assumed to be linear in both
660  * the source and target bitmap, hence it won't be affected by
661  * any gamma-related transformations.
662  *
663  * \remark This <tt>convert()</tt> variant usually returns a new
664  * bitmap instance. When the conversion would just involve copying
665  * the original bitmap, the function becomes a no-op and returns
666  * the current instance.
667  *
668  * \ref pixelFormat
669  * Specifies the desired pixel format
670  * \ref componentFormat
671  * Specifies the desired component format
672  * \ref gamma
673  * Specifies the desired gamma value.
674  * Special values: \c 1.0 denotes a linear space, and
675  * \c -1.0 corresponds to sRGB.
676  * \ref multiplier
677  * An optional multiplicative factor that can be
678  * applied to all color/luminance values in linear
679  * space (alpha will not be affected).
680  * \ref intent
681  * When converting from RGB to spectral color values, this flag
682  * specifies how ambiguities in this highly under-constrained problem
683  * should be resolved.
684  */
685  ref<Bitmap> convert(EPixelFormat pixelFormat, EComponentFormat componentFormat,
686  Float gamma = 1.0f, Float multiplier = 1.0f,
687  Spectrum::EConversionIntent intent = Spectrum::EReflectance);
688 
689  /**
690  * \brief Convert the bitmap into another pixel and/or component format
691  *
692  * This helper function can be used to efficiently convert a bitmap
693  * between different underlying representations. For instance, it can
694  * translate a 24-bit sRGB bitmap to a linear spectrum-valued representation
695  * based on half-, single- or double-precision floating point-backed storage.
696  *
697  * This function roughly does the following:
698  *
699  * <ul>
700  * <li>For each pixel and channel, it converts the associated value
701  * into a normalized linear-space form (any gamma of the source
702  * bitmap is removed)</li>
703  * <li>The multiplier and gamma correction specified in
704  * \c targetGamma is applied</li>
705  * <li>The corrected value is clamped against the representable range
706  * of the desired component format.</li>
707  * <li>The clamped gamma-corrected value is then written to
708  * the new bitmap</li>
709  *
710  * If the pixel formats differ, this function will also perform basic
711  * conversions (e.g. spectrum to rgb, luminance to uniform spectrum
712  * values, etc.)
713  *
714  * Note that the alpha channel is assumed to be linear in both
715  * the source and target bitmap, hence it won't be affected by
716  * any gamma-related transformations.
717  *
718  * \remark This <tt>convert()</tt> variant writes to a raw bitmap
719  * buffer provided as a pointer.
720  *
721  * \ref target
722  * A pointer to a "raw" image pixel buffer
723  * \ref pixelFormat
724  * Specifies the desired pixel format
725  * \ref componentFormat
726  * Specifies the desired component format
727  * \ref gamma
728  * Specifies the desired gamma value.
729  * Special values: \c 1.0 denotes a linear space, and
730  * \c -1.0 corresponds to sRGB.
731  * \ref multiplier
732  * An optional multiplicative factor that can be
733  * applied to all color/luminance values in linear
734  * space (alpha will not be affected).
735  * \ref intent
736  * When converting from RGB to spectral color values, this flag
737  * specifies how ambiguities in this highly under-constrained problem
738  * should be resolved.
739  */
740  void convert(void *target,
741  EPixelFormat pixelFormat, EComponentFormat componentFormat,
742  Float gamma = 1.0f, Float multiplier = 1.0f,
743  Spectrum::EConversionIntent intent = Spectrum::EReflectance) const;
744 
745  /**
746  * \brief Specialized conversion method for multi-channel HDR images
747  *
748  * The bitmap class pixel format \ref EMultiSpectrumAlphaWeight is used by the
749  * 'multichannel' plugin to render multiple related images at the same time.
750  * This function implements a conversion function analogous to \ref convert()
751  * that adjusts each of the sub-images so that it has a desired pixel format.
752  * The returned bitmap has the combined pixel format \ref EMultiChannel and the
753  * specified component format. Names for each of the resulting channels should
754  * be provided via the \c channelNames parameters.
755  *
756  * This feature is currently used by the \c hdrfilm and \c tiledhdrfilm plugins.
757  */
758  ref<Bitmap> convertMultiSpectrumAlphaWeight(const std::vector<EPixelFormat> &pixelFormats,
759  EComponentFormat componentFormat, const std::vector<std::string> &channelNames) const;
760 
761  /// Similar to the above, but writes to an already existing image
762  static void convertMultiSpectrumAlphaWeight(const Bitmap *source,
763  const uint8_t *sourcePtr, const Bitmap *target, uint8_t *targetPtr,
764  const std::vector<EPixelFormat> &pixelFormats,
765  EComponentFormat componentFormat, size_t count);
766 
767  /**
768  * \brief Apply Reinhard et al's tonemapper in chromaticity space
769  *
770  * This function
771  * <ol>
772  * <li>Computes and stores the maximum and log-average luminance of the image
773  * If the \c logAvgLuminace and \c maxLuminance parameters are nonzero, it is
774  * assumed that these have already been computed, and this step is omitted.
775  * Explicitly specifying them can be useful to prevent flickering when tonemapping
776  * multiple frames of an animation.</li>
777  *
778  * <li>Converts the image to xyY, applies Reinhard's photographic tonemapper in this
779  * space, and then transforms the result back to its original
780  * representation (i.e. RGB or XYZ). The global version of the
781  * tonemapping algorithm is used (i.e. the one without automatic
782  * dodging and burning).</li>
783  * </ol>
784  *
785  * The <tt>key</tt> parameter specifies whether a low-key or high-key image is
786  * desired. The value must be in the interval <tt>[0,1]</tt>. A good starting
787  * point is a middle-grey, i.e. <tt>key=0.18</tt>.
788  *
789  * When <tt>burn=0</tt>, the entire image is re-mapped into the displayable range. This
790  * may not always be desireable -- for instance, one might choose to let a highlight
791  * burn out, and this is achieved by choosing burn in (0, 1].
792  *
793  * For reference, see
794  * "Photographic Tone Reproduction for Digital Images" by Erik Reinhard, Michael Stark,
795  * Peter Shirley, and James Fewerda, in ACM Transactions on Graphics 2002, Vol. 21, 3
796  *
797  * \remark The implementation assumes that the image has a RGB(A), XYZ(A), or Luminance(Alpha)
798  * pixel format, that <tt>gamma=1</tt>, and that it uses a EFloat16/EFloat32/EFloat64
799  * component format. The conversion process is destructive in the sense that it overwrites
800  * the original image.
801  *
802  * \remark In the Python bindings, the signature of this function is:
803  * <tt>(logAvgLuminance, maxLuminance) = tonemapReinhard(logAvgLuminance, maxLuminance, key, burn)</tt>
804  */
805  void tonemapReinhard(Float &logAvgLuminance, Float &maxLuminance,
806  Float key, Float burn);
807 
808  /**
809  * \brief Expand bitmask images
810  *
811  * When this image is a bitmask, the implementation returns an expanded
812  * version in \ref EUInt8 format. Otherwise, it returns a pointer to
813  * the current instance.
814  */
815  ref<Bitmap> expand();
816 
817  /**
818  * \brief Extract one of the color channels in the
819  * bitmap and return it as a separate luminance bitmap
820  *
821  * When this is already a single-channel bitmap, the function
822  * returns a pointer to the current instance.
823  */
824  ref<Bitmap> extractChannel(int channelIndex) const;
825 
826  /**
827  * \brief Extract several color channels of a multi-channel
828  * bitmap and return them as a bitmap with the given pixel format
829  */
830  ref<Bitmap> extractChannels(Bitmap::EPixelFormat fmt, const std::vector<int> &channels) const;
831 
832  /**
833  * \brief Extracts layer information from the bitmap
834  *
835  * This is a convenience function which analyzes the bitmap's
836  * channel names and extracts groups matching a name
837  * convention specified by the OpenEXR standard.
838  *
839  * A series of channel names referred to as
840  * 'diffuse.R', 'diffuse.B', and 'diffuse.G'
841  * will be identified as a single \ref Bitmap::ERGB "layer".
842  * This works for RGB[A]/XYZ[A], and Luminance[A] images
843  */
844  std::vector<Layer> getLayers() const;
845 
846  /**
847  * \brief Split an multi-channel image buffer (e.g. from an OpenEXR image
848  * with lots of AOVs) into its constituent layers
849  *
850  * This operation internally calls \ref getLayers() to extract bitmap
851  * layers followed by one or more calls to \ref extractChannels()
852  */
853  std::map<std::string, Bitmap *> split() const;
854 
855  /**
856  * \brief Merges multiple bitmaps of the same type and resolution
857  * into one with a larger number of channels
858  *
859  * \param fmt The desired pixel format of the combined bitmap
860  *
861  * \param sourceBitmaps An array containing the input bitmaps
862  * They must all be of the same size and component format.
863  *
864  * \return A newly created bitmap containing all input images
865  * as color channels
866  */
867  static ref<Bitmap> join(EPixelFormat fmt,
868  const std::vector<Bitmap *> &sourceBitmaps);
869 
870  /// Crop the bitmap to the specified rectangle
871  ref<Bitmap> crop(const Point2i &offset, const Vector2i &size) const;
872 
873  /// Vertically flip the image contents
874  void flipVertically();
875 
876  /// Compute the average value of the bitmap
877  Spectrum average() const;
878 
879  /// Perform the specified rotatation & flip operation
880  ref<Bitmap> rotateFlip(ERotateFlipType type) const;
881 
882  /**
883  * \brief Scale the entire image by a certain value
884  *
885  * Skips the image's alpha channel, if it has one. When the image uses
886  * a fixed point representation and a pixel value overflows during the
887  * scale operation, it is clamped to the representable range.
888  */
889  void scale(Float value);
890 
891  /**
892  * \brief Raise the entire image to a certain value
893  *
894  * Skips the image's alpha channel, if it has one. When the image uses
895  * a fixed point representation and a pixel value overflows during the
896  * scale operation, it is clamped to the representable range.
897  */
898  void pow(Float value);
899 
900  /**
901  * \brief Color balancing: apply the given scale factors to the
902  * red, green, and blue channels of the image
903  *
904  * When the image is not an \c EFloat16, \c EFloat32, or
905  * \c EFloat64-based RGB/RGBA image, the function throws an exception
906  */
907  void colorBalance(Float r, Float g, Float b);
908 
909  /**
910  * Apply a color transformation matrix to the contents of the bitmap
911  *
912  * The implementation assumes that the contents have the
913  * RGB, RGBA, XYZ, or XYZA pixel format and a floating point
914  * component format.
915  */
916  void applyMatrix(Float matrix[3][3]);
917 
918  /**
919  * \brief Copy the contents of another bitmap into the
920  * region with the specified offset
921  *
922  * Out-of-bounds regions are ignored. It is assumed that
923  * <tt>bitmap != this</tt>.
924  *
925  * \remark This function throws an exception when the bitmaps
926  * use different component formats or channels, or when the
927  * component format is \ref EBitmask.
928  */
929  void copyFrom(const Bitmap *bitmap, Point2i sourceOffset,
930  Point2i targetOffset, Vector2i size);
931 
932  /**
933  * \brief Copy the contents of another bitmap into the
934  * region with the specified offset
935  *
936  * This convenience function calls the main <tt>copyFrom()</tt>
937  * implementation with <tt>size</tt> set to <tt>bitmap->getSize()</tt>
938  * and <tt>sourceOffset</tt> set to zero. Out-of-bounds regions are
939  * ignored. It is assumed that <tt>bitmap != this</tt>.
940  *
941  * \remark This function throws an exception when the bitmaps
942  * use different component formats or channels, or when the
943  * component format is \ref EBitmask.
944  */
945  inline void copyFrom(const Bitmap *bitmap, Point2i targetOffset) {
946  copyFrom(bitmap, Point2i(0), targetOffset, bitmap->getSize());
947  }
948 
949  /**
950  * \brief Copy the contents of another bitmap into the
951  * region with the specified offset
952  *
953  * This convenience function calls the main <tt>copyFrom()</tt>
954  * implementation with <tt>size</tt> set to <tt>bitmap->getSize()</tt>
955  * and <tt>sourceOffset</tt> and <tt>targetOffset</tt>tt> set to zero.
956  * Out-of-bounds regions are ignored. It is assumed
957  * that <tt>bitmap != this</tt>.
958  *
959  * \remark This function throws an exception when the bitmaps
960  * use different component formats or channels, or when the
961  * component format is \ref EBitmask.
962  */
963  inline void copyFrom(const Bitmap *bitmap) {
964  copyFrom(bitmap, Point2i(0), Point2i(0), bitmap->getSize());
965  }
966 
967  /**
968  * \brief Accumulate the contents of another bitmap into the
969  * region with the specified offset
970  *
971  * Out-of-bounds regions are ignored. It is assumed that
972  * <tt>bitmap != this</tt>.
973  *
974  * \remark This function throws an exception when the bitmaps
975  * use different component formats or channels, or when the
976  * component format is \ref EBitmask.
977  */
978  void accumulate(const Bitmap *bitmap, Point2i sourceOffset,
979  Point2i targetOffset, Vector2i size);
980 
981  /**
982  * \brief Accumulate the contents of another bitmap into the
983  * region with the specified offset
984  *
985  * This convenience function calls the main <tt>accumulate()</tt>
986  * implementation with <tt>size</tt> set to <tt>bitmap->getSize()</tt>
987  * and <tt>sourceOffset</tt> set to zero. Out-of-bounds regions are
988  * ignored. It is assumed that <tt>bitmap != this</tt>.
989  *
990  * \remark This function throws an exception when the bitmaps
991  * use different component formats or channels, or when the
992  * component format is \ref EBitmask.
993  */
994  inline void accumulate(const Bitmap *bitmap, Point2i targetOffset) {
995  accumulate(bitmap, Point2i(0), targetOffset, bitmap->getSize());
996  }
997 
998  /**
999  * \brief Accumulate the contents of another bitmap into the
1000  * region with the specified offset
1001  *
1002  * This convenience function calls the main <tt>accumulate()</tt>
1003  * implementation with <tt>size</tt> set to <tt>bitmap->getSize()</tt>
1004  * and <tt>sourceOffset</tt> and <tt>targetOffset</tt>tt> set to zero.
1005  * Out-of-bounds regions are ignored. It is assumed
1006  * that <tt>bitmap != this</tt>.
1007  *
1008  * \remark This function throws an exception when the bitmaps
1009  * use different component formats or channels, or when the
1010  * component format is \ref EBitmask.
1011  */
1012  inline void accumulate(const Bitmap *bitmap) {
1013  accumulate(bitmap, Point2i(0), Point2i(0), bitmap->getSize());
1014  }
1015 
1016  /**
1017  * \brief Convolve the image with a (centered) convolution kernel
1018  *
1019  * When compiled with FFTW, Mitsuba will do the convolution
1020  * in frequency space using three FFT operations. Otherwise,
1021  * it falls back to a brute force method with quadratic
1022  * complexity.
1023  *
1024  * The image can have any resolution; the kernel should be
1025  * square and of odd resolution. Both images must be of the
1026  * same floating point-valued component format. The kernel can
1027  * either have one color channel or as many color channels as
1028  * the image to be convolved.
1029  *
1030  * The convolution is always performed in double precision.
1031  * irrespective of the precision of the underlying data.
1032  */
1033  void convolve(const Bitmap *kernel);
1034 
1035  /**
1036  * \brief Perform an arithmetic operation using two images
1037  *
1038  * This function can add, subtract, multiply, or divide arbitrary
1039  * images. If the input images have different sizes or component
1040  * and pixel formats, the implementation first resamples and
1041  * converts them into the most "expressive" format that subsumes
1042  * both input images (at the cost of some temporary dynamic
1043  * memory allocations).
1044  *
1045  * To keep the implementation simple, there is currently no
1046  * special treatment of integer over/underflows if the component
1047  * format is \ref EUInt8, \ref EUInt16, or \ref EUInt32.
1048  */
1049  static ref<Bitmap> arithmeticOperation(EArithmeticOperation operation,
1050  const Bitmap *bitmap1, const Bitmap *bitmap2);
1051 
1052  /**
1053  * \brief Up- or down-sample this image to a different resolution
1054  *
1055  * Uses the provided reconstruction filter and observes the specified
1056  * horizontal and vertical boundary conditions when looking up data
1057  * outside of the input domain.
1058  *
1059  * A maximum and maximum image value can be specified to prevent to
1060  * prevent out-of-range values that are created by the resampling process.
1061  *
1062  * The optional 'temp' parameter can be used to pass an image of
1063  * resolution <tt>Vector2i(target->getWidth(), this->getHeight())</tt>
1064  * to avoid intermediate memory allocations.
1065  */
1066  void resample(const ReconstructionFilter *rfilter,
1069  Bitmap *target, Bitmap *temp = NULL,
1070  Float minValue = 0.0f, Float maxValue = 1.0f) const;
1071 
1072  /**
1073  * \brief Up- or down-sample this image to a different resolution
1074  *
1075  * Uses the provided reconstruction filter and observes the specified
1076  * horizontal and vertical boundary conditions when looking up data
1077  * outside of the input domain.
1078  *
1079  * A maximum and maximum image value can be specified to prevent to prevent
1080  * out-of-range values that are created by the resampling process.
1081  *
1082  * This function allocates a new output image and returns it.
1083  */
1084  ref<Bitmap> resample(const ReconstructionFilter *rfilter,
1087  const Vector2i &size, Float minValue = 0.0f,
1088  Float maxValue = 1.0f) const;
1089 
1090  /**
1091  * \brief Apply a separable convolution filter to the image
1092  *
1093  * Applies the provided filter while observing the specified
1094  * horizontal and vertical boundary conditions when looking up data
1095  * outside of the input domain.
1096  *
1097  * In comparison to \ref convolve(), this function operates
1098  * in the primal domain.
1099  *
1100  * A maximum and maximum image value can be specified to prevent to
1101  * prevent out-of-range values that are created by the filtering process.
1102  *
1103  * The optional 'temp' parameter can be used to pass an image of
1104  * the same dimensions as the source and target image.
1105  */
1106  void filter(const ReconstructionFilter *rfilter,
1109  Bitmap *target, Bitmap *temp = NULL,
1110  Float minValue = 0.0f, Float maxValue = 1.0f) const;
1111 
1112  /**
1113  * \brief Apply a separable convolution filter to the image
1114  *
1115  * Applies the provided filter while observing the specified
1116  * horizontal and vertical boundary conditions when looking up data
1117  * outside of the input domain.
1118  *
1119  * In comparison to \ref convolve(), this function operates
1120  * in the primal domain.
1121  *
1122  * A maximum and maximum image value can be specified to prevent to
1123  * prevent out-of-range values that are created by the filtering process.
1124  *
1125  * This function allocates a new output image and returns it.
1126  */
1127  ref<Bitmap> filter(const ReconstructionFilter *rfilter,
1130  Float minValue = 0.0f, Float maxValue = 1.0f) const;
1131 
1132  //! @}
1133  // ======================================================================
1134 
1135  // ======================================================================
1136  //! @{ \name Access to metadata associated with the bitmap
1137  // ======================================================================
1138 
1139  /// Return the bitmap's gamma identifier (-1: sRGB)
1140  inline Float getGamma() const { return m_gamma; }
1141 
1142  /// Set the bitmap's gamma identifier (-1: sRGB)
1143  inline void setGamma(Float gamma) { m_gamma = gamma; }
1144 
1145  /// Set a string-valued metadata field
1146  inline void setMetadataString(const std::string &key, const std::string &value) {
1147  m_metadata.setString(key, value);
1148  }
1149 
1150  /// Return a string-valued metadata field
1151  inline std::string getMetadataString(const std::string &key) const {
1152  return m_metadata.getAsString(key);
1153  }
1154 
1155  /// Return a \ref Properties object containing the image metadata
1156  inline Properties &getMetadata() { return m_metadata; }
1157 
1158  /// Return a \ref Properties object containing the image metadata (const version)
1159  inline const Properties &getMetadata() const { return m_metadata; }
1160 
1161  /// Set the a \ref Properties object containing the image metadata
1162  inline void setMetadata(const Properties &metadata) { m_metadata = metadata; }
1163 
1164  /**
1165  * \brief Explicitly set the names of the image channels
1166  *
1167  * The main use of this feature is when writing \ref EMultiChannel
1168  * images to OpenEXR files. In other cases, the names are ignored.
1169  */
1170  void setChannelNames(const std::vector<std::string> &names);
1171 
1172  /// Return the names of the image channels if explicitly specified (empty by default)
1173  inline std::vector<std::string> &getChannelNames() { return m_channelNames; }
1174 
1175  /// Return the names of the image channels if explicitly specified (empty by default)
1176  inline const std::vector<std::string> &getChannelNames() const { return m_channelNames; }
1177 
1178  //! @}
1179  // ======================================================================
1180 
1181  // ======================================================================
1182  //! @{ \name Bitmap raster data accessors
1183  // ======================================================================
1184 
1185  /// Access the underlying raster
1186  inline void *getData() { return m_data; }
1187 
1188  /// Access the underlying bit raster (const version)
1189  inline const void *getData() const { return m_data; }
1190 
1191  /// Access the underlying raster (for uint8 bitmaps)
1192  inline uint8_t *getUInt8Data() { return m_data; }
1193 
1194  /// Access the underlying bit raster (for uint8 bitmaps, const version)
1195  inline const uint8_t *getUInt8Data() const { return m_data; }
1196 
1197  /// Access the underlying raster data (for uint16 bitmaps)
1198  inline uint16_t *getUInt16Data() { return (uint16_t *) m_data; }
1199 
1200  /// Access the underlying raster data (for uint16 bitmaps, const version)
1201  inline const uint16_t *getUInt16Data() const { return (const uint16_t *) m_data; }
1202 
1203  /// Access the underlying raster data (for uint32 bitmaps)
1204  inline uint32_t *getUInt32Data() { return (uint32_t *) m_data; }
1205 
1206  /// Access the underlying raster data (for uint32 bitmaps, const version)
1207  inline const uint32_t *getUInt32Data() const { return (const uint32_t *) m_data; }
1208 
1209  /// Access the underlying raster data (for float16 bitmaps)
1210  inline half *getFloat16Data() { return (half *) m_data; }
1211 
1212  /// Access the underlying raster data (for float16 bitmaps, const version)
1213  inline const half *getFloat16Data() const { return (const half *) m_data; }
1214 
1215  /// Access the underlying raster data (for float32 bitmaps)
1216  inline float *getFloat32Data() { return (float *) m_data; }
1217 
1218  /// Access the underlying raster data (for float32 bitmaps, const version)
1219  inline const float *getFloat32Data() const { return (const float *) m_data; }
1220 
1221  /// Access the underlying raster data (for float64 bitmaps)
1222  inline double *getFloat64Data() { return (double *) m_data; }
1223 
1224  /// Access the underlying raster data (for float64 bitmaps, const version)
1225  inline const double *getFloat64Data() const { return (const double *) m_data; }
1226 
1227  /// Access the underlying raster data (for float32/float64 bitmaps)
1228  inline Float *getFloatData() { return (Float *) m_data; }
1229 
1230  /// Access the underlying raster data (for float/float64 bitmaps, const version)
1231  inline const Float *getFloatData() const { return (const Float *) m_data; }
1232 
1233  //! @}
1234  // ======================================================================
1235 
1236  /// Run static initialization code (sets up OpenEXR for multithreading)
1237  static void staticInitialization();
1238 
1239  /// Release any resources allocated in \ref staticInitialization
1240  static void staticShutdown();
1241 
1242  /// Return some human-readable information about this bitmap
1243  std::string toString() const;
1244 
1246 protected:
1247  /// Virtual destructor
1248  virtual ~Bitmap();
1249 
1250  /// Read a file stored using the PNG file format
1251  void readPNG(Stream *stream);
1252 
1253  /// Write a file using the PNG file format
1254  void writePNG(Stream *stream, int compression) const;
1255 
1256  /// Read a file stored using the JPEG file format
1257  void readJPEG(Stream *stream);
1258 
1259  /// Save a file using the JPEG file format
1260  void writeJPEG(Stream *stream, int quality = 100) const;
1261 
1262  /// Read a file stored using the RGBE file format
1263  void readRGBE(Stream *stream);
1264 
1265  /// Write a file using the RGBE file format
1266  void writeRGBE(Stream *stream) const;
1267 
1268  /// Read a file stored using the OpenEXR file format
1269  void readOpenEXR(Stream *stream, const std::string &prefix);
1270 
1271  /// Write a file using the OpenEXR file format
1272  void writeOpenEXR(Stream *stream) const;
1273 
1274  /// Read a file stored using the PFM file format
1275  void readPFM(Stream *stream);
1276 
1277  /// Write a file using the PFM file format
1278  void writePFM(Stream *stream) const;
1279 
1280  /// Read a file stored using the PPM file format
1281  void readPPM(Stream *stream);
1282 
1283  /// Write a file using the PPM file format
1284  void writePPM(Stream *stream) const;
1285 
1286  /// Read a file stored using the TGA file format
1287  void readTGA(Stream *stream);
1288 
1289  /// Read a file stored using the BMP file format
1290  void readBMP(Stream *stream);
1291 
1292  /// Update the internally cached channel count
1293  void updateChannelCount();
1294 
1295  /// Delegate for stream loading operations
1296  void readStream(EFileFormat format, Stream *stream, const std::string &prefix);
1297 protected:
1298  EPixelFormat m_pixelFormat;
1299  EComponentFormat m_componentFormat;
1300  Vector2i m_size;
1301  uint8_t *m_data;
1302  Float m_gamma;
1303  uint8_t m_channelCount;
1304  bool m_ownsData;
1305  Properties m_metadata;
1306  std::vector<std::string> m_channelNames;
1307 };
1308 
1309 /** \brief Bitmap format conversion helper class
1310  *
1311  * This class implements efficient conversions between different
1312  * bitmap component formats. For instance, to transform an
1313  * 8-bit based image to floating point values, a suitable
1314  * converter can be obtained as follows:
1315  *
1316  * \code
1317  * FormatConverter *cvt = FormatConverter::getInstance(
1318  * std::make_pair(Bitmap::EUInt8, Bitmap::EFloat)
1319  * );
1320  *
1321  * cvt->convert(...);
1322  * \endcode
1323  *
1324  * The \c convert methods in \ref Bitmap rely on this class
1325  * and may be more convenient to use.
1326  *
1327  * \remark This is not a color management system. Depending on the
1328  * target type, out-of-gamut values may be clipped component-wise.
1329  * If a luminance scale factor is applied, that is also done component-wise
1330  * (instead of scaling in a space that is based on human perception, such
1331  * as xyY or CIELab). If this and smarter gamut remapping are needed,
1332  * a library such as lcms2 will be more appropriate.
1333  *
1334  * \sa Bitmap::convert(EComponentFormat, Float);
1335  * \sa Bitmap::convert(Spectrum *);
1336  * \ingroup libcore
1337  */
1339 public:
1341  typedef std::pair<Format, Format> Conversion;
1342  typedef std::map<Conversion, FormatConverter *> ConverterMap;
1343 
1344  /**
1345  * \brief Transform pixels based on the conversion implemented
1346  * by this class
1347  *
1348  * Note that the alpha channel is assumed to be linear in both
1349  * the source and target bitmap, hence it won't be affected by
1350  * Gamma-related transformations.
1351  *
1352  * \param pixelFormat
1353  * Pixel format of the source bitmap
1354  * \param sourceGamma
1355  * Gamma value associated with pixels from the source bitmap.
1356  * Special values: 1.0 denotes a linear space, and -1.0
1357  * corresponds to sRGB.
1358  * \param source
1359  * Pointer to the first pixel of the source bitmap
1360  * \param destFormat
1361  * Pixel format of the destination bitmap
1362  * \param destGamma
1363  * Gamma value associated with pixels from the destination
1364  * bitmap. Special values: 1.0 denotes a linear space, and -1.0
1365  * corresponds to sRGB.
1366  * \param dest
1367  * Pointer to the first pixel of the destination bitmap
1368  * \param count
1369  * How many pixels should be transformed?
1370  * \param multiplier
1371  * An optional multiplicative factor that will be applied to all
1372  * color/luminance/spectrum values in linear space (alpha and weight
1373  * values will not be affected).
1374  * \param intent
1375  * When converting from RGB to spectral color values, this flag
1376  * specifies how ambiguities in this highly under-constrained problem
1377  * should be resolved.
1378  * \param channelCount
1379  * Number of channels (not including a weight channel, if any). Only
1380  * needs to be specified for general multi-channel images.
1381  * \sa getConversion()
1382  */
1383  virtual void convert(
1384  Bitmap::EPixelFormat sourceFormat, Float sourceGamma, const void *_source,
1385  Bitmap::EPixelFormat destFormat, Float destGamma, void *_dest,
1386  size_t count, Float multiplier = 1.0f,
1387  Spectrum::EConversionIntent intent = Spectrum::EReflectance,
1388  int channelCount = -1) const= 0;
1389 
1390  /**
1391  * \brief Return the format conversion implemented by this
1392  * \c FormatConverter instance.
1393  *
1394  * \return A format pair, where the first element indicates the
1395  * source format, and the second element is the target format.
1396  */
1397  virtual Conversion getConversion() const = 0;
1398 
1399  /// Virtual destructor to delete instances using pointers to the base type
1400  virtual ~FormatConverter() {}
1401 
1402  /**
1403  * \brief Return a \ref FormatConverter instance, which can convert
1404  * from \c conv.first to \c conv.second.
1405  */
1406  static const FormatConverter *getInstance(Conversion con);
1407 
1408  /// Execute static initialization code (run once at program startup)
1409  static void staticInitialization();
1410 
1411  /// Release any resources allocated in \ref staticInitialization
1412  static void staticShutdown();
1413 private:
1414  static ConverterMap m_converters;
1415 };
1416 
1417 //! \cond
1418 namespace detail {
1419  template <typename T> inline Bitmap::EComponentFormat cfmt() { return Bitmap::EInvalid; }
1420  template <> inline Bitmap::EComponentFormat cfmt<uint8_t>() { return Bitmap::EUInt8; }
1421  template <> inline Bitmap::EComponentFormat cfmt<uint16_t>() { return Bitmap::EUInt16; }
1422  template <> inline Bitmap::EComponentFormat cfmt<uint32_t>() { return Bitmap::EUInt32; }
1423  template <> inline Bitmap::EComponentFormat cfmt<half>() { return Bitmap::EFloat16; }
1424  template <> inline Bitmap::EComponentFormat cfmt<float>() { return Bitmap::EFloat32; }
1425  template <> inline Bitmap::EComponentFormat cfmt<double>() { return Bitmap::EFloat64; }
1426 };
1427 
1428 template <typename T> inline Bitmap::EComponentFormat Bitmap::componentFormat() {
1429  return detail::cfmt<T>();
1430 }
1431 
1432 extern MTS_EXPORT_CORE std::ostream &operator<<(std::ostream &os, const Bitmap::EPixelFormat &value);
1433 extern MTS_EXPORT_CORE std::ostream &operator<<(std::ostream &os, const Bitmap::EComponentFormat &value);
1434 //! \endcond
1435 
1437 
1438 #endif /* __MITSUBA_CORE_BITMAP_H_ */
Float * getFloatData()
Access the underlying raster data (for float32/float64 bitmaps)
Definition: bitmap.h:1228
EBoundaryCondition
When resampling data to a different resolution using Resampler::resample(), this enumeration specifie...
Definition: rfilter.h:53
uint16_t * getUInt16Data()
Access the underlying raster data (for uint16 bitmaps)
Definition: bitmap.h:1198
General-purpose bitmap class with read and write support for several common file formats.
Definition: bitmap.h:50
bool isSquare() const
Return whether this image has matching width and height.
Definition: bitmap.h:400
int getBytesPerPixel() const
Return the number of bytes per pixel.
Definition: bitmap.h:448
Definition: bitmap.h:296
void setMetadataString(const std::string &key, const std::string &value)
Set a string-valued metadata field.
Definition: bitmap.h:1146
const std::vector< std::string > & getChannelNames() const
Return the names of the image channels if explicitly specified (empty by default) ...
Definition: bitmap.h:1176
EArithmeticOperation
The four basic arithmetic operations for use with arithmeticOperation()
Definition: bitmap.h:293
Definition: bitmap.h:135
Properties & getMetadata()
Return a Properties object containing the image metadata.
Definition: bitmap.h:1156
static void staticInitialization()
Initializes the built-in reference count debugger (if enabled)
std::map< Conversion, FormatConverter * > ConverterMap
Definition: bitmap.h:1342
bool hasWeight() const
Return whether this image has a weight channel.
Definition: bitmap.h:415
bool operator!=(const Bitmap &bitmap) const
Bitmap inequality operator (useful for unit-tests etc.)
Definition: bitmap.h:512
Bitmap format conversion helper class.
Definition: bitmap.h:1338
const Vector2i & getSize() const
Return the bitmap&#39;s resolution in pixels.
Definition: bitmap.h:385
Definition: bitmap.h:295
EPixelFormat
Definition: bitmap.h:61
#define MTS_EXPORT_CORE
Definition: getopt.h:29
std::string name
Descriptive name of the bitmap layer.
Definition: bitmap.h:309
Invalid component format (used to report error conditions)
Definition: bitmap.h:154
const void * getData() const
Access the underlying bit raster (const version)
Definition: bitmap.h:1189
32-bit unsigned integer (uint32_t) component encoding
Definition: bitmap.h:128
half * getFloat16Data()
Access the underlying raster data (for float16 bitmaps)
Definition: bitmap.h:1210
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
std::ostream & operator<<(std::ostream &out, const DScalar1< Scalar, VecType > &s)
Definition: autodiff.h:392
void copyFrom(const Bitmap *bitmap, Point2i targetOffset)
Copy the contents of another bitmap into the region with the specified offset.
Definition: bitmap.h:945
bool hasAlpha() const
Return whether this image has an alpha channel.
Definition: bitmap.h:406
ERotateFlipType
List of different rotation/flip types that can be passed to rotateFlip()
Definition: bitmap.h:273
Describes a sub-layer of a multilayer bitmap (e.g. OpenEXR)
Definition: bitmap.h:307
Generic interface to separable image reconstruction filters.
Definition: rfilter.h:44
float * getFloat32Data()
Access the underlying raster data (for float32 bitmaps)
Definition: bitmap.h:1216
EPixelFormat getPixelFormat() const
Return the pixel format of this bitmap.
Definition: bitmap.h:379
std::vector< std::string > & getChannelNames()
Return the names of the image channels if explicitly specified (empty by default) ...
Definition: bitmap.h:1173
int getChannelCount() const
Return the number of channels used by this bitmap.
Definition: bitmap.h:397
RGBE image format by Greg Ward.
Definition: bitmap.h:214
Truevision Advanced Raster Graphics Array file format.
Definition: bitmap.h:253
std::vector< int > channels
References to bitmap channels.
Definition: bitmap.h:313
OpenEXR high dynamic range file format developed by Industrial Light &amp; Magic (ILM) ...
Definition: bitmap.h:204
size_t getPixelCount() const
Return the total number of pixels.
Definition: bitmap.h:388
EComponentFormat getComponentFormat() const
Return the component format of this bitmap.
Definition: bitmap.h:382
void accumulate(const Bitmap *bitmap)
Accumulate the contents of another bitmap into the region with the specified offset.
Definition: bitmap.h:1012
void setMetadata(const Properties &metadata)
Set the a Properties object containing the image metadata.
Definition: bitmap.h:1162
const half * getFloat16Data() const
Access the underlying raster data (for float16 bitmaps, const version)
Definition: bitmap.h:1213
void copyFrom(const Bitmap *bitmap)
Copy the contents of another bitmap into the region with the specified offset.
Definition: bitmap.h:963
virtual ~FormatConverter()
Virtual destructor to delete instances using pointers to the base type.
Definition: bitmap.h:1400
const float * getFloat32Data() const
Access the underlying raster data (for float32 bitmaps, const version)
Definition: bitmap.h:1219
8-bit unsigned integer (uint8_t) component encoding
Definition: bitmap.h:114
void accumulate(const Bitmap *bitmap, Point2i targetOffset)
Accumulate the contents of another bitmap into the region with the specified offset.
Definition: bitmap.h:994
bool isMultiChannel() const
Return whether this is a generic multi-channel image.
Definition: bitmap.h:421
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
32-bit floating point (float) HDR component encoding
Definition: bitmap.h:142
Definition: fwd.h:99
Reference counting helper.
Definition: ref.h:40
EConversionIntent
When converting from RGB reflectance values to discretized color spectra, the following `intent&#39; flag...
Definition: spectrum.h:673
const Float * getFloatData() const
Access the underlying raster data (for float/float64 bitmaps, const version)
Definition: bitmap.h:1231
Joint Photographic Experts Group file format.
Definition: bitmap.h:245
std::string getMetadataString(const std::string &key) const
Return a string-valued metadata field.
Definition: bitmap.h:1151
const Properties & getMetadata() const
Return a Properties object containing the image metadata (const version)
Definition: bitmap.h:1159
int getHeight() const
Return the bitmap&#39;s height in pixels.
Definition: bitmap.h:394
uint8_t * getUInt8Data()
Access the underlying raster (for uint8 bitmaps)
Definition: bitmap.h:1192
PPM (Portable Pixel Map) image format.
Definition: bitmap.h:234
std::pair< Format, Format > Conversion
Definition: bitmap.h:1341
int getWidth() const
Return the bitmap&#39;s width in pixels.
Definition: bitmap.h:391
EComponentFormat
Supported per-component data formats.
Definition: bitmap.h:97
EPixelFormat format
Pixel format that should be associated with underlying combination of bitmap channels.
Definition: bitmap.h:311
const uint16_t * getUInt16Data() const
Access the underlying raster data (for uint16 bitmaps, const version)
Definition: bitmap.h:1201
Associative parameter map for constructing subclasses of ConfigurableObject.
Definition: properties.h:46
PFM (Portable Float Map) image format.
Definition: bitmap.h:224
Parent of all Mitsuba classes.
Definition: object.h:38
16-bit unsigned integer (uint16_t) component encoding
Definition: bitmap.h:121
Windows Bitmap file format.
Definition: bitmap.h:261
const double * getFloat64Data() const
Access the underlying raster data (for float64 bitmaps, const version)
Definition: bitmap.h:1225
TPoint2< int > Point2i
Definition: fwd.h:131
Float getGamma() const
Return the bitmap&#39;s gamma identifier (-1: sRGB)
Definition: bitmap.h:1140
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
const uint32_t * getUInt32Data() const
Access the underlying raster data (for uint32 bitmaps, const version)
Definition: bitmap.h:1207
virtual std::string toString() const
Return a human-readable string representation of the object&#39;s contents.
const uint8_t * getUInt8Data() const
Access the underlying bit raster (for uint8 bitmaps, const version)
Definition: bitmap.h:1195
64-bit floating point (double) HDR component encoding
Definition: bitmap.h:149
static void staticShutdown()
Free the memory taken by staticInitialization()
Bitmap::EComponentFormat Format
Definition: bitmap.h:1340
Definition: fwd.h:95
Definition: half.h:96
#define MTS_NAMESPACE_END
Definition: platform.h:138
EFileFormat
Supported file formats.
Definition: bitmap.h:170
void * getData()
Access the underlying raster.
Definition: bitmap.h:1186
void setGamma(Float gamma)
Set the bitmap&#39;s gamma identifier (-1: sRGB)
Definition: bitmap.h:1143
double * getFloat64Data()
Access the underlying raster data (for float64 bitmaps)
Definition: bitmap.h:1222
uint32_t * getUInt32Data()
Access the underlying raster data (for uint32 bitmaps)
Definition: bitmap.h:1204