photo_editor_common.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "editor/photo_editor_common.h"
  8. #include "editor/scene/scene.h"
  9. #include "ui/painter.h"
  10. namespace Editor {
  11. QImage ImageModified(QImage image, const PhotoModifications &mods) {
  12. Expects(!image.isNull());
  13. if (!mods) {
  14. return image;
  15. }
  16. if (mods.paint) {
  17. if (image.format() != QImage::Format_ARGB32_Premultiplied) {
  18. image = image.convertToFormat(
  19. QImage::Format_ARGB32_Premultiplied);
  20. }
  21. Painter p(&image);
  22. PainterHighQualityEnabler hq(p);
  23. mods.paint->render(&p, image.rect());
  24. }
  25. auto cropped = mods.crop.isValid()
  26. ? image.copy(mods.crop)
  27. : image;
  28. QTransform transform;
  29. if (mods.flipped) {
  30. transform.scale(-1, 1);
  31. }
  32. if (mods.angle) {
  33. transform.rotate(mods.angle);
  34. }
  35. return cropped.transformed(transform);
  36. }
  37. bool PhotoModifications::empty() const {
  38. return !angle && !flipped && !crop.isValid() && !paint;
  39. }
  40. PhotoModifications::operator bool() const {
  41. return !empty();
  42. }
  43. PhotoModifications::~PhotoModifications() {
  44. if (paint && (paint.use_count() == 1)) {
  45. paint->deleteLater();
  46. }
  47. }
  48. } // namespace Editor