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.

224 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  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. @tags{Graphics}
  28. */
  29. class JUCE_API ImageFileFormat
  30. {
  31. protected:
  32. //==============================================================================
  33. /** Creates an ImageFormat. */
  34. ImageFileFormat() = default;
  35. public:
  36. /** Destructor. */
  37. virtual ~ImageFileFormat() = default;
  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. @tags{Graphics}
  109. */
  110. class JUCE_API PNGImageFormat : public ImageFileFormat
  111. {
  112. public:
  113. //==============================================================================
  114. PNGImageFormat();
  115. ~PNGImageFormat() override;
  116. //==============================================================================
  117. String getFormatName() override;
  118. bool usesFileExtension (const File&) override;
  119. bool canUnderstand (InputStream&) override;
  120. Image decodeImage (InputStream&) override;
  121. bool writeImageToStream (const Image&, OutputStream&) override;
  122. };
  123. //==============================================================================
  124. /**
  125. A subclass of ImageFileFormat for reading and writing JPEG files.
  126. @see ImageFileFormat, PNGImageFormat
  127. @tags{Graphics}
  128. */
  129. class JUCE_API JPEGImageFormat : public ImageFileFormat
  130. {
  131. public:
  132. //==============================================================================
  133. JPEGImageFormat();
  134. ~JPEGImageFormat() override;
  135. //==============================================================================
  136. /** Specifies the quality to be used when writing a JPEG file.
  137. @param newQuality a value 0 to 1.0, where 0 is low quality, 1.0 is best, or
  138. any negative value is "default" quality
  139. */
  140. void setQuality (float newQuality);
  141. //==============================================================================
  142. String getFormatName() override;
  143. bool usesFileExtension (const File&) override;
  144. bool canUnderstand (InputStream&) override;
  145. Image decodeImage (InputStream&) override;
  146. bool writeImageToStream (const Image&, OutputStream&) override;
  147. private:
  148. float quality;
  149. };
  150. //==============================================================================
  151. /**
  152. A subclass of ImageFileFormat for reading GIF files.
  153. @see ImageFileFormat, PNGImageFormat, JPEGImageFormat
  154. @tags{Graphics}
  155. */
  156. class JUCE_API GIFImageFormat : public ImageFileFormat
  157. {
  158. public:
  159. //==============================================================================
  160. GIFImageFormat();
  161. ~GIFImageFormat() override;
  162. //==============================================================================
  163. String getFormatName() override;
  164. bool usesFileExtension (const File&) override;
  165. bool canUnderstand (InputStream&) override;
  166. Image decodeImage (InputStream&) override;
  167. bool writeImageToStream (const Image&, OutputStream&) override;
  168. };
  169. } // namespace juce