image.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/image/image_prepare.h"
  9. class QPainterPath;
  10. class Image final {
  11. public:
  12. explicit Image(const QString &path);
  13. explicit Image(const QByteArray &content);
  14. explicit Image(QImage &&data);
  15. [[nodiscard]] static not_null<Image*> Empty(); // 1x1 transparent
  16. [[nodiscard]] static not_null<Image*> BlankMedia(); // 1x1 black
  17. [[nodiscard]] int width() const {
  18. return _data.width();
  19. }
  20. [[nodiscard]] int height() const {
  21. return _data.height();
  22. }
  23. [[nodiscard]] QSize size() const {
  24. return { width(), height() };
  25. }
  26. [[nodiscard]] bool isNull() const {
  27. return (this == Empty());
  28. }
  29. [[nodiscard]] QImage original() const;
  30. [[nodiscard]] const QPixmap &pix(
  31. QSize size,
  32. const Images::PrepareArgs &args = {}) const {
  33. return cached(size.width(), size.height(), args, false);
  34. }
  35. [[nodiscard]] const QPixmap &pix(
  36. int w,
  37. int h,
  38. const Images::PrepareArgs &args = {}) const {
  39. return cached(w, h, args, false);
  40. }
  41. [[nodiscard]] const QPixmap &pix(
  42. int w = 0,
  43. const Images::PrepareArgs &args = {}) const {
  44. return cached(w, 0, args, false);
  45. }
  46. [[nodiscard]] const QPixmap &pixSingle(
  47. QSize size,
  48. const Images::PrepareArgs &args = {}) const {
  49. return cached(size.width(), size.height(), args, true);
  50. }
  51. [[nodiscard]] const QPixmap &pixSingle(
  52. int w,
  53. int h,
  54. const Images::PrepareArgs &args = {}) const {
  55. return cached(w, h, args, true);
  56. }
  57. [[nodiscard]] const QPixmap &pixSingle(
  58. int w = 0,
  59. const Images::PrepareArgs &args = {}) const {
  60. return cached(w, 0, args, true);
  61. }
  62. [[nodiscard]] QPixmap pixNoCache(
  63. QSize size,
  64. const Images::PrepareArgs &args = {}) const {
  65. return prepare(size.width(), size.height(), args);
  66. }
  67. [[nodiscard]] QPixmap pixNoCache(
  68. int w,
  69. int h,
  70. const Images::PrepareArgs &args = {}) const {
  71. return prepare(w, h, args);
  72. }
  73. [[nodiscard]] QPixmap pixNoCache(
  74. int w = 0,
  75. const Images::PrepareArgs &args = {}) const {
  76. return prepare(w, 0, args);
  77. }
  78. private:
  79. [[nodiscard]] QPixmap prepare(
  80. int w,
  81. int h,
  82. const Images::PrepareArgs &args) const;
  83. [[nodiscard]] const QPixmap &cached(
  84. int w,
  85. int h,
  86. const Images::PrepareArgs &args,
  87. bool single) const;
  88. const QImage _data;
  89. mutable base::flat_map<uint64, QPixmap> _cache;
  90. };