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 8.1KB

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