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.

89 lines
2.7KB

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