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.

108 lines
2.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. struct DefaultImageFormats
  20. {
  21. static ImageFileFormat** get()
  22. {
  23. static DefaultImageFormats formats;
  24. return formats.formats;
  25. }
  26. private:
  27. DefaultImageFormats() noexcept
  28. {
  29. formats[0] = &png;
  30. formats[1] = &jpg;
  31. formats[2] = &gif;
  32. formats[3] = nullptr;
  33. }
  34. PNGImageFormat png;
  35. JPEGImageFormat jpg;
  36. GIFImageFormat gif;
  37. ImageFileFormat* formats[4];
  38. };
  39. ImageFileFormat* ImageFileFormat::findImageFormatForStream (InputStream& input)
  40. {
  41. const int64 streamPos = input.getPosition();
  42. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  43. {
  44. const bool found = (*i)->canUnderstand (input);
  45. input.setPosition (streamPos);
  46. if (found)
  47. return *i;
  48. }
  49. return nullptr;
  50. }
  51. ImageFileFormat* ImageFileFormat::findImageFormatForFileExtension (const File& file)
  52. {
  53. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  54. if ((*i)->usesFileExtension (file))
  55. return *i;
  56. return nullptr;
  57. }
  58. //==============================================================================
  59. Image ImageFileFormat::loadFrom (InputStream& input)
  60. {
  61. if (ImageFileFormat* format = findImageFormatForStream (input))
  62. return format->decodeImage (input);
  63. return Image();
  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();
  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();
  83. }