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.

juce_ImageFileFormat.h 7.8KB

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