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.

137 lines
3.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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_ImageFileFormat.h"
  21. #include "../../../io/streams/juce_MemoryInputStream.h"
  22. #include "../../../io/files/juce_FileInputStream.h"
  23. #include "../../../io/streams/juce_BufferedInputStream.h"
  24. #include "image_file_formats/juce_GIFLoader.h"
  25. //==============================================================================
  26. class GIFImageFormat : public ImageFileFormat
  27. {
  28. public:
  29. GIFImageFormat() {}
  30. ~GIFImageFormat() {}
  31. const String getFormatName()
  32. {
  33. return "GIF";
  34. }
  35. bool canUnderstand (InputStream& in)
  36. {
  37. const int bytesNeeded = 4;
  38. char header [bytesNeeded];
  39. return (in.read (header, bytesNeeded) == bytesNeeded)
  40. && header[0] == 'G'
  41. && header[1] == 'I'
  42. && header[2] == 'F';
  43. }
  44. const Image decodeImage (InputStream& in)
  45. {
  46. const ScopedPointer <GIFLoader> loader (new GIFLoader (in));
  47. return loader->getImage();
  48. }
  49. bool writeImageToStream (const Image& /*sourceImage*/, OutputStream& /*destStream*/)
  50. {
  51. return false;
  52. }
  53. };
  54. //==============================================================================
  55. ImageFileFormat* ImageFileFormat::findImageFormatForStream (InputStream& input)
  56. {
  57. static PNGImageFormat png;
  58. static JPEGImageFormat jpg;
  59. static GIFImageFormat gif;
  60. ImageFileFormat* formats[4];
  61. int numFormats = 0;
  62. formats [numFormats++] = &png;
  63. formats [numFormats++] = &jpg;
  64. formats [numFormats++] = &gif;
  65. const int64 streamPos = input.getPosition();
  66. for (int i = 0; i < numFormats; ++i)
  67. {
  68. const bool found = formats[i]->canUnderstand (input);
  69. input.setPosition (streamPos);
  70. if (found)
  71. return formats[i];
  72. }
  73. return 0;
  74. }
  75. //==============================================================================
  76. const Image ImageFileFormat::loadFrom (InputStream& input)
  77. {
  78. ImageFileFormat* const format = findImageFormatForStream (input);
  79. if (format != 0)
  80. return format->decodeImage (input);
  81. return Image();
  82. }
  83. const Image ImageFileFormat::loadFrom (const File& file)
  84. {
  85. InputStream* const in = file.createInputStream();
  86. if (in != 0)
  87. {
  88. BufferedInputStream b (in, 8192, true);
  89. return loadFrom (b);
  90. }
  91. return Image();
  92. }
  93. const Image ImageFileFormat::loadFrom (const void* rawData, const int numBytes)
  94. {
  95. if (rawData != 0 && numBytes > 4)
  96. {
  97. MemoryInputStream stream (rawData, numBytes, false);
  98. return loadFrom (stream);
  99. }
  100. return Image();
  101. }
  102. END_JUCE_NAMESPACE