Better photo size comparison.

GitOrigin-RevId: 630e32213169122a3c67ac8c35b2e8bc5c2a6ae5
This commit is contained in:
levlam 2018-04-03 10:35:04 +03:00
parent 96b6dfde3f
commit 1035a5a6c0
3 changed files with 31 additions and 4 deletions

View File

@ -21171,7 +21171,7 @@ unique_ptr<MessageContent> 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<MessageContent> 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;
}
}

View File

@ -43,6 +43,10 @@ Dimensions get_dimensions(int32 width, int32 height) {
return result;
}
static uint32 get_pixel_count(const Dimensions &dimensions) {
return static_cast<uint32>(dimensions.width) * static_cast<uint32>(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<td_api::object_ptr<td_api::photoSize>> &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<uint32>(lhs->width_) * static_cast<uint32>(lhs->height_) <
static_cast<uint32>(rhs->width_) * static_cast<uint32>(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 << "}";

View File

@ -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<td_api::object_ptr<td_api::photoSize>> &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<telegram_api::photo> &&photo, DialogId owner_dialog_id);