ImageDecoder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGfx/BMPLoader.h>
  27. #include <LibGfx/GIFLoader.h>
  28. #include <LibGfx/ICOLoader.h>
  29. #include <LibGfx/ImageDecoder.h>
  30. #include <LibGfx/JPGLoader.h>
  31. #include <LibGfx/PBMLoader.h>
  32. #include <LibGfx/PGMLoader.h>
  33. #include <LibGfx/PNGLoader.h>
  34. #include <LibGfx/PPMLoader.h>
  35. namespace Gfx {
  36. ImageDecoder::ImageDecoder(const u8* data, size_t size)
  37. {
  38. m_plugin = make<PNGImageDecoderPlugin>(data, size);
  39. if (m_plugin->sniff())
  40. return;
  41. m_plugin = make<GIFImageDecoderPlugin>(data, size);
  42. if (m_plugin->sniff())
  43. return;
  44. m_plugin = make<BMPImageDecoderPlugin>(data, size);
  45. if (m_plugin->sniff())
  46. return;
  47. m_plugin = make<PBMImageDecoderPlugin>(data, size);
  48. if (m_plugin->sniff())
  49. return;
  50. m_plugin = make<PGMImageDecoderPlugin>(data, size);
  51. if (m_plugin->sniff())
  52. return;
  53. m_plugin = make<PPMImageDecoderPlugin>(data, size);
  54. if (m_plugin->sniff())
  55. return;
  56. m_plugin = make<ICOImageDecoderPlugin>(data, size);
  57. if (m_plugin->sniff())
  58. return;
  59. m_plugin = make<JPGImageDecoderPlugin>(data, size);
  60. if (m_plugin->sniff())
  61. return;
  62. m_plugin = nullptr;
  63. }
  64. ImageDecoder::~ImageDecoder()
  65. {
  66. }
  67. RefPtr<Gfx::Bitmap> ImageDecoder::bitmap() const
  68. {
  69. if (!m_plugin)
  70. return nullptr;
  71. return m_plugin->bitmap();
  72. }
  73. }