From 1035a5a6c019b2d48597045f2a4a886a48596ecc Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 3 Apr 2018 10:35:04 +0300 Subject: [PATCH] Better photo size comparison. GitOrigin-RevId: 630e32213169122a3c67ac8c35b2e8bc5c2a6ae5 --- td/telegram/MessagesManager.cpp | 4 ++-- td/telegram/Photo.cpp | 27 ++++++++++++++++++++++++++- td/telegram/Photo.h | 4 +++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 23812605..38954885 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -21171,7 +21171,7 @@ unique_ptr MessagesManager::dup_message_content(DialogId dialog_ } if (photo.type == 0) { for (const auto &size : result->photo.photos) { - if (photo.type == 0 || photo.size < size.size) { + if (photo.type == 0 || photo < size) { photo = size; } } @@ -21186,7 +21186,7 @@ unique_ptr MessagesManager::dup_message_content(DialogId dialog_ } if (thumbnail.type == 0) { for (const auto &size : result->photo.photos) { - if (size.type != photo.type && (thumbnail.type == 0 || thumbnail.size > size.size)) { + if (size.type != photo.type && (thumbnail.type == 0 || size < thumbnail)) { thumbnail = size; } } diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index f08007a8..045ffd1e 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -43,6 +43,10 @@ Dimensions get_dimensions(int32 width, int32 height) { return result; } +static uint32 get_pixel_count(const Dimensions &dimensions) { + return static_cast(dimensions.width) * static_cast(dimensions.height); +} + bool operator==(const Dimensions &lhs, const Dimensions &rhs) { return lhs.width == rhs.width && lhs.height == rhs.height; } @@ -390,7 +394,8 @@ void sort_photo_sizes(vector> &sizes) { if (lhs->photo_->expected_size_ != rhs->photo_->expected_size_) { return lhs->photo_->expected_size_ < rhs->photo_->expected_size_; } - return lhs->width_ * lhs->height_ < rhs->width_ * rhs->height_; + return static_cast(lhs->width_) * static_cast(lhs->height_) < + static_cast(rhs->width_) * static_cast(rhs->height_); }); } @@ -402,6 +407,26 @@ bool operator!=(const PhotoSize &lhs, const PhotoSize &rhs) { return !(lhs == rhs); } +bool operator<(const PhotoSize &lhs, const PhotoSize &rhs) { + if (lhs.size != rhs.size) { + return lhs.size < rhs.size; + } + auto lhs_pixels = get_pixel_count(lhs.dimensions); + auto rhs_pixels = get_pixel_count(rhs.dimensions); + if (lhs_pixels != rhs_pixels) { + return lhs_pixels < rhs_pixels; + } + int32 lhs_type = lhs.type == 't' ? -1 : lhs.type; + int32 rhs_type = rhs.type == 't' ? -1 : rhs.type; + if (lhs_type != rhs_type) { + return lhs_type < rhs_type; + } + if (lhs.file_id != rhs.file_id) { + return lhs.file_id.get() < rhs.file_id.get(); + } + return lhs.dimensions.width < rhs.dimensions.width; +} + StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSize &photo_size) { return string_builder << "{type = " << photo_size.type << ", dimensions = " << photo_size.dimensions << ", size = " << photo_size.size << ", file_id = " << photo_size.file_id << "}"; diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index f91911fd..6678ac45 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -38,7 +38,7 @@ struct ProfilePhoto : public DialogPhoto { }; struct PhotoSize { - int32 type = 0; // TODO remove + int32 type = 0; Dimensions dimensions; int32 size = 0; FileId file_id; @@ -90,6 +90,8 @@ void sort_photo_sizes(vector> &sizes); bool operator==(const PhotoSize &lhs, const PhotoSize &rhs); bool operator!=(const PhotoSize &lhs, const PhotoSize &rhs); +bool operator<(const PhotoSize &lhs, const PhotoSize &rhs); + StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSize &photo_size); Photo get_photo(FileManager *file_manager, tl_object_ptr &&photo, DialogId owner_dialog_id);