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.

213 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #include "../../../io/files/juce_File.h"
  22. #include "../../../io/streams/juce_InputStream.h"
  23. #include "../../../io/streams/juce_OutputStream.h"
  24. //==============================================================================
  25. /**
  26. Base-class for codecs that can read and write image file formats such
  27. as PNG, JPEG, etc.
  28. This class also contains static methods to make it easy to load images
  29. from files, streams or from memory.
  30. @see Image, ImageCache
  31. */
  32. class JUCE_API ImageFileFormat
  33. {
  34. protected:
  35. //==============================================================================
  36. /** Creates an ImageFormat. */
  37. ImageFileFormat() {}
  38. public:
  39. /** Destructor. */
  40. virtual ~ImageFileFormat() {}
  41. //==============================================================================
  42. /** Returns a description of this file format.
  43. E.g. "JPEG", "PNG"
  44. */
  45. virtual const String getFormatName() = 0;
  46. /** Returns true if the given stream seems to contain data that this format
  47. understands.
  48. The format class should only read the first few bytes of the stream and sniff
  49. for header bytes that it understands.
  50. @see decodeImage
  51. */
  52. virtual bool canUnderstand (InputStream& input) = 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 const 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 decoders 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. //==============================================================================
  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 const 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 const 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 const Image loadFrom (const void* rawData,
  97. const int numBytesOfData);
  98. };
  99. //==============================================================================
  100. /**
  101. A subclass of ImageFileFormat for reading and writing PNG files.
  102. @see ImageFileFormat, JPEGImageFormat
  103. */
  104. class JUCE_API PNGImageFormat : public ImageFileFormat
  105. {
  106. public:
  107. //==============================================================================
  108. PNGImageFormat();
  109. ~PNGImageFormat();
  110. //==============================================================================
  111. const String getFormatName();
  112. bool canUnderstand (InputStream& input);
  113. const Image decodeImage (InputStream& input);
  114. bool writeImageToStream (const Image& sourceImage, OutputStream& destStream);
  115. };
  116. //==============================================================================
  117. /**
  118. A subclass of ImageFileFormat for reading and writing JPEG files.
  119. @see ImageFileFormat, PNGImageFormat
  120. */
  121. class JUCE_API JPEGImageFormat : public ImageFileFormat
  122. {
  123. public:
  124. //==============================================================================
  125. JPEGImageFormat();
  126. ~JPEGImageFormat();
  127. //==============================================================================
  128. /** Specifies the quality to be used when writing a JPEG file.
  129. @param newQuality a value 0 to 1.0, where 0 is low quality, 1.0 is best, or
  130. any negative value is "default" quality
  131. */
  132. void setQuality (float newQuality);
  133. //==============================================================================
  134. const String getFormatName();
  135. bool canUnderstand (InputStream& input);
  136. const Image decodeImage (InputStream& input);
  137. bool writeImageToStream (const Image& sourceImage, OutputStream& destStream);
  138. private:
  139. float quality;
  140. };
  141. //==============================================================================
  142. /**
  143. A subclass of ImageFileFormat for reading GIF files.
  144. @see ImageFileFormat, PNGImageFormat, JPEGImageFormat
  145. */
  146. class JUCE_API GIFImageFormat : public ImageFileFormat
  147. {
  148. public:
  149. //==============================================================================
  150. GIFImageFormat();
  151. ~GIFImageFormat();
  152. //==============================================================================
  153. const String getFormatName();
  154. bool canUnderstand (InputStream& input);
  155. const Image decodeImage (InputStream& input);
  156. bool writeImageToStream (const Image& sourceImage, OutputStream& destStream);
  157. };
  158. #endif // __JUCE_IMAGEFILEFORMAT_JUCEHEADER__