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.

220 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_IMAGEFILEFORMAT_JUCEHEADER__
  19. #define __JUCE_IMAGEFILEFORMAT_JUCEHEADER__
  20. #include "juce_Image.h"
  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. #endif // __JUCE_IMAGEFILEFORMAT_JUCEHEADER__