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.

109 lines
3.0KB

  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. struct DefaultImageFormats
  19. {
  20. static ImageFileFormat** get()
  21. {
  22. static DefaultImageFormats formats;
  23. return formats.formats;
  24. }
  25. private:
  26. DefaultImageFormats() noexcept
  27. {
  28. formats[0] = &png;
  29. formats[1] = &jpg;
  30. formats[2] = &gif;
  31. formats[3] = nullptr;
  32. }
  33. PNGImageFormat png;
  34. JPEGImageFormat jpg;
  35. GIFImageFormat gif;
  36. ImageFileFormat* formats[4];
  37. };
  38. ImageFileFormat* ImageFileFormat::findImageFormatForStream (InputStream& input)
  39. {
  40. const int64 streamPos = input.getPosition();
  41. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  42. {
  43. const bool found = (*i)->canUnderstand (input);
  44. input.setPosition (streamPos);
  45. if (found)
  46. return *i;
  47. }
  48. return nullptr;
  49. }
  50. ImageFileFormat* ImageFileFormat::findImageFormatForFileExtension (const File& file)
  51. {
  52. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  53. if ((*i)->usesFileExtension (file))
  54. return *i;
  55. return nullptr;
  56. }
  57. //==============================================================================
  58. Image ImageFileFormat::loadFrom (InputStream& input)
  59. {
  60. ImageFileFormat* const format = findImageFormatForStream (input);
  61. if (format != nullptr)
  62. return format->decodeImage (input);
  63. return Image::null;
  64. }
  65. Image ImageFileFormat::loadFrom (const File& file)
  66. {
  67. FileInputStream stream (file);
  68. if (stream.openedOk())
  69. {
  70. BufferedInputStream b (stream, 8192);
  71. return loadFrom (b);
  72. }
  73. return Image::null;
  74. }
  75. Image ImageFileFormat::loadFrom (const void* rawData, const size_t numBytes)
  76. {
  77. if (rawData != nullptr && numBytes > 4)
  78. {
  79. MemoryInputStream stream (rawData, numBytes, false);
  80. return loadFrom (stream);
  81. }
  82. return Image::null;
  83. }