Audio plugin host https://kx.studio/carla
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.

juce_ImageFileFormat.cpp 2.8KB

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