QtImageLoader.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/ImageProcessing/ImageObject.h>
  9. #include <ImageLoader/ImageLoaders.h>
  10. #include <ImageBuilderBaseType.h>
  11. // warning C4251: class QT_Type needs to have dll-interface to be used by clients of class 'QT_Type'
  12. AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option")
  13. #include <QImage>
  14. #include <QImageReader>
  15. AZ_POP_DISABLE_WARNING
  16. ///////////////////////////////////////////////////////////////////////////////////
  17. namespace ImageProcessingAtom
  18. {
  19. namespace QtImageLoader
  20. {
  21. IImageObject* LoadImageFromFile(const AZStd::string& filename)
  22. {
  23. //try to open the image
  24. QImage qimage(filename.c_str());
  25. if (qimage.isNull())
  26. {
  27. AZ_Warning("ImageProcessing", false, "Failed to load [%s] via QImage", filename.c_str());
  28. return nullptr;
  29. }
  30. //convert to format which compatiable our pixel format
  31. if (qimage.format() != QImage::Format_RGBA8888)
  32. {
  33. qimage = qimage.convertToFormat(QImage::Format_RGBA8888);
  34. }
  35. //create a new image object
  36. IImageObject* pImage = IImageObject::CreateImage(qimage.width(), qimage.height(), 1,
  37. ePixelFormat_R8G8B8A8);
  38. //get a pointer to the image objects pixel data
  39. uint8* pDst;
  40. uint32 dwPitch;
  41. pImage->GetImagePointer(0, pDst, dwPitch);
  42. //copy the qImage into the image object
  43. for (uint32 dwY = 0; dwY < (uint32)qimage.height(); ++dwY)
  44. {
  45. uint8* dstLine = &pDst[dwPitch * dwY];
  46. uchar* srcLine = qimage.scanLine(dwY);
  47. memcpy(dstLine, srcLine, dwPitch);
  48. }
  49. return pImage;
  50. }
  51. bool IsExtensionSupported(const char* extension)
  52. {
  53. QList<QByteArray> imgFormats = QImageReader::supportedImageFormats();
  54. for (int i = 0; i < imgFormats.size(); ++i)
  55. {
  56. if (imgFormats[i].toLower().toStdString() == QString(extension).toLower().toStdString())
  57. {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. }//namespace QtImageLoader
  64. } //namespace ImageProcessingAtom