Audio plugin host https://kx.studio/carla
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.

217 lines
8.0KB

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