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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. struct DefaultImageFormats
  18. {
  19. static ImageFileFormat** get()
  20. {
  21. static DefaultImageFormats formats;
  22. return formats.formats;
  23. }
  24. private:
  25. DefaultImageFormats() noexcept
  26. {
  27. formats[0] = &png;
  28. formats[1] = &jpg;
  29. formats[2] = &gif;
  30. formats[3] = nullptr;
  31. }
  32. PNGImageFormat png;
  33. JPEGImageFormat jpg;
  34. GIFImageFormat gif;
  35. ImageFileFormat* formats[4];
  36. };
  37. ImageFileFormat* ImageFileFormat::findImageFormatForStream (InputStream& input)
  38. {
  39. const int64 streamPos = input.getPosition();
  40. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  41. {
  42. const bool found = (*i)->canUnderstand (input);
  43. input.setPosition (streamPos);
  44. if (found)
  45. return *i;
  46. }
  47. return nullptr;
  48. }
  49. ImageFileFormat* ImageFileFormat::findImageFormatForFileExtension (const File& file)
  50. {
  51. for (ImageFileFormat** i = DefaultImageFormats::get(); *i != nullptr; ++i)
  52. if ((*i)->usesFileExtension (file))
  53. return *i;
  54. return nullptr;
  55. }
  56. //==============================================================================
  57. Image ImageFileFormat::loadFrom (InputStream& input)
  58. {
  59. ImageFileFormat* const format = findImageFormatForStream (input);
  60. if (format != nullptr)
  61. return format->decodeImage (input);
  62. return Image::null;
  63. }
  64. Image ImageFileFormat::loadFrom (const File& file)
  65. {
  66. FileInputStream stream (file);
  67. if (stream.openedOk())
  68. {
  69. BufferedInputStream b (stream, 8192);
  70. return loadFrom (b);
  71. }
  72. return Image::null;
  73. }
  74. Image ImageFileFormat::loadFrom (const void* rawData, const size_t numBytes)
  75. {
  76. if (rawData != nullptr && numBytes > 4)
  77. {
  78. MemoryInputStream stream (rawData, numBytes, false);
  79. return loadFrom (stream);
  80. }
  81. return Image::null;
  82. }