The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_IMAGEFILEFORMAT_JUCEHEADER__
  18. #define __JUCE_IMAGEFILEFORMAT_JUCEHEADER__
  19. #include "juce_Image.h"
  20. //==============================================================================
  21. /**
  22. Base-class for codecs that can read and write image file formats such
  23. as PNG, JPEG, etc.
  24. This class also contains static methods to make it easy to load images
  25. from files, streams or from memory.
  26. @see Image, ImageCache
  27. */
  28. class JUCE_API ImageFileFormat
  29. {
  30. protected:
  31. //==============================================================================
  32. /** Creates an ImageFormat. */
  33. ImageFileFormat() {}
  34. public:
  35. /** Destructor. */
  36. virtual ~ImageFileFormat() {}
  37. //==============================================================================
  38. /** Returns a description of this file format.
  39. E.g. "JPEG", "PNG"
  40. */
  41. virtual String getFormatName() = 0;
  42. /** Returns true if the given stream seems to contain data that this format understands.
  43. The format class should only read the first few bytes of the stream and sniff
  44. for header bytes that it understands.
  45. Note that this will advance the stream and leave it in a new position, so if you're
  46. planning on re-using it, you may want to rewind it after calling this method.
  47. @see decodeImage
  48. */
  49. virtual bool canUnderstand (InputStream& input) = 0;
  50. /** Returns true if this format uses the file extension of the given file. */
  51. virtual bool usesFileExtension (const File& possibleFile) = 0;
  52. /** Tries to decode and return an image from the given stream.
  53. This will be called for an image format after calling its canUnderStand() method
  54. to see if it can handle the stream.
  55. @param input the stream to read the data from. The stream will be positioned
  56. at the start of the image data (but this may not necessarily
  57. be position 0)
  58. @returns the image that was decoded, or an invalid image if it fails.
  59. @see loadFrom
  60. */
  61. virtual Image decodeImage (InputStream& input) = 0;
  62. //==============================================================================
  63. /** Attempts to write an image to a stream.
  64. To specify extra information like encoding quality, there will be appropriate parameters
  65. in the subclasses of the specific file types.
  66. @returns true if it nothing went wrong.
  67. */
  68. virtual bool writeImageToStream (const Image& sourceImage,
  69. OutputStream& destStream) = 0;
  70. //==============================================================================
  71. /** Tries the built-in formats to see if it can find one to read this stream.
  72. There are currently built-in decoders for PNG, JPEG and GIF formats.
  73. The object that is returned should not be deleted by the caller.
  74. @see canUnderstand, decodeImage, loadFrom
  75. */
  76. static ImageFileFormat* findImageFormatForStream (InputStream& input);
  77. /** Looks for a format that can handle the given file extension.
  78. There are currently built-in formats for PNG, JPEG and GIF formats.
  79. The object that is returned should not be deleted by the caller.
  80. */
  81. static ImageFileFormat* findImageFormatForFileExtension (const File& file);
  82. //==============================================================================
  83. /** Tries to load an image from a stream.
  84. This will use the findImageFormatForStream() method to locate a suitable
  85. codec, and use that to load the image.
  86. @returns the image that was decoded, or an invalid image if it fails.
  87. */
  88. static Image loadFrom (InputStream& input);
  89. /** Tries to load an image from a file.
  90. This will use the findImageFormatForStream() method to locate a suitable
  91. codec, and use that to load the image.
  92. @returns the image that was decoded, or an invalid image if it fails.
  93. */
  94. static Image loadFrom (const File& file);
  95. /** Tries to load an image from a block of raw image data.
  96. This will use the findImageFormatForStream() method to locate a suitable
  97. codec, and use that to load the image.
  98. @returns the image that was decoded, or an invalid image if it fails.
  99. */
  100. static Image loadFrom (const void* rawData,
  101. size_t numBytesOfData);
  102. };
  103. //==============================================================================
  104. /**
  105. A subclass of ImageFileFormat for reading and writing PNG files.
  106. @see ImageFileFormat, JPEGImageFormat
  107. */
  108. class JUCE_API PNGImageFormat : public ImageFileFormat
  109. {
  110. public:
  111. //==============================================================================
  112. PNGImageFormat();
  113. ~PNGImageFormat();
  114. //==============================================================================
  115. String getFormatName() override;
  116. bool usesFileExtension (const File&) override;
  117. bool canUnderstand (InputStream&) override;
  118. Image decodeImage (InputStream&) override;
  119. bool writeImageToStream (const Image&, OutputStream&) override;
  120. };
  121. //==============================================================================
  122. /**
  123. A subclass of ImageFileFormat for reading and writing JPEG files.
  124. @see ImageFileFormat, PNGImageFormat
  125. */
  126. class JUCE_API JPEGImageFormat : public ImageFileFormat
  127. {
  128. public:
  129. //==============================================================================
  130. JPEGImageFormat();
  131. ~JPEGImageFormat();
  132. //==============================================================================
  133. /** Specifies the quality to be used when writing a JPEG file.
  134. @param newQuality a value 0 to 1.0, where 0 is low quality, 1.0 is best, or
  135. any negative value is "default" quality
  136. */
  137. void setQuality (float newQuality);
  138. //==============================================================================
  139. String getFormatName() override;
  140. bool usesFileExtension (const File&) override;
  141. bool canUnderstand (InputStream&) override;
  142. Image decodeImage (InputStream&) override;
  143. bool writeImageToStream (const Image&, OutputStream&) override;
  144. private:
  145. float quality;
  146. };
  147. //==============================================================================
  148. /**
  149. A subclass of ImageFileFormat for reading GIF files.
  150. @see ImageFileFormat, PNGImageFormat, JPEGImageFormat
  151. */
  152. class JUCE_API GIFImageFormat : public ImageFileFormat
  153. {
  154. public:
  155. //==============================================================================
  156. GIFImageFormat();
  157. ~GIFImageFormat();
  158. //==============================================================================
  159. String getFormatName() override;
  160. bool usesFileExtension (const File&) override;
  161. bool canUnderstand (InputStream&) override;
  162. Image decodeImage (InputStream&) override;
  163. bool writeImageToStream (const Image&, OutputStream&) override;
  164. };
  165. #endif // __JUCE_IMAGEFILEFORMAT_JUCEHEADER__