Add Dimensions.h.

This commit is contained in:
levlam 2022-06-02 17:52:12 +03:00
parent 4bd6bd377b
commit 014b458425
19 changed files with 126 additions and 67 deletions

View File

@ -317,6 +317,7 @@ set(TDLIB_SOURCE
td/telegram/DialogParticipant.cpp
td/telegram/DialogParticipantFilter.cpp
td/telegram/DialogSource.cpp
td/telegram/Dimensions.cpp
td/telegram/Document.cpp
td/telegram/DocumentsManager.cpp
td/telegram/DownloadManager.cpp
@ -520,6 +521,7 @@ set(TDLIB_SOURCE
td/telegram/DialogParticipant.h
td/telegram/DialogParticipantFilter.h
td/telegram/DialogSource.h
td/telegram/Dimensions.h
td/telegram/Document.h
td/telegram/DocumentsManager.h
td/telegram/DownloadManager.h
@ -693,6 +695,7 @@ set(TDLIB_SOURCE
td/telegram/AuthManager.hpp
td/telegram/BackgroundType.hpp
td/telegram/DialogFilter.hpp
td/telegram/Dimensions.hpp
td/telegram/Document.hpp
td/telegram/DocumentsManager.hpp
td/telegram/DraftMessage.hpp

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/PhotoSize.h"

View File

@ -0,0 +1,51 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/Dimensions.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
namespace td {
static uint16 get_dimension(int32 size, const char *source) {
if (size < 0 || size > 65535) {
if (source != nullptr) {
LOG(ERROR) << "Wrong image dimension = " << size << " from " << source;
}
return 0;
}
return narrow_cast<uint16>(size);
}
Dimensions get_dimensions(int32 width, int32 height, const char *source) {
Dimensions result;
result.width = get_dimension(width, source);
result.height = get_dimension(height, source);
if (result.width == 0 || result.height == 0) {
result.width = 0;
result.height = 0;
}
return result;
}
uint32 get_dimensions_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;
}
bool operator!=(const Dimensions &lhs, const Dimensions &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions) {
return string_builder << "(" << dimensions.width << ", " << dimensions.height << ")";
}
} // namespace td

28
td/telegram/Dimensions.h Normal file
View File

@ -0,0 +1,28 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
namespace td {
struct Dimensions {
uint16 width = 0;
uint16 height = 0;
};
Dimensions get_dimensions(int32 width, int32 height, const char *source);
uint32 get_dimensions_pixel_count(const Dimensions &dimensions);
bool operator==(const Dimensions &lhs, const Dimensions &rhs);
bool operator!=(const Dimensions &lhs, const Dimensions &rhs);
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions);
} // namespace td

View File

@ -0,0 +1,28 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/telegram/Dimensions.h"
#include "td/utils/tl_helpers.h"
namespace td {
template <class StorerT>
void store(Dimensions dimensions, StorerT &storer) {
store(static_cast<uint32>((static_cast<uint32>(dimensions.width) << 16) | dimensions.height), storer);
}
template <class ParserT>
void parse(Dimensions &dimensions, ParserT &parser) {
uint32 width_height;
parse(width_height, parser);
dimensions.width = static_cast<uint16>(width_height >> 16);
dimensions.height = static_cast<uint16>(width_height & 0xFFFF);
}
} // namespace td

View File

@ -9,6 +9,7 @@
#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AudiosManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/Document.h"
#include "td/telegram/files/FileEncryptionKey.h"
#include "td/telegram/files/FileLocation.h"

View File

@ -19,6 +19,7 @@
#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogAction.h"
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/Document.h"
#include "td/telegram/DocumentsManager.h"
#include "td/telegram/DocumentsManager.hpp"

View File

@ -8,6 +8,7 @@
#include "td/telegram/AccessRights.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/Global.h"

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/Photo.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileEncryptionKey.h"
#include "td/telegram/files/FileLocation.h"
#include "td/telegram/files/FileManager.h"

View File

@ -22,43 +22,6 @@
namespace td {
static uint16 get_dimension(int32 size, const char *source) {
if (size < 0 || size > 65535) {
if (source != nullptr) {
LOG(ERROR) << "Wrong image dimension = " << size << " from " << source;
}
return 0;
}
return narrow_cast<uint16>(size);
}
Dimensions get_dimensions(int32 width, int32 height, const char *source) {
Dimensions result;
result.width = get_dimension(width, source);
result.height = get_dimension(height, source);
if (result.width == 0 || result.height == 0) {
result.width = 0;
result.height = 0;
}
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;
}
bool operator!=(const Dimensions &lhs, const Dimensions &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions) {
return string_builder << "(" << dimensions.width << ", " << dimensions.height << ")";
}
static int32 get_minithumbnail_size(const string &packed) {
if (packed.size() < 3) {
return 0;
@ -431,8 +394,8 @@ 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);
auto lhs_pixels = get_dimensions_pixel_count(lhs.dimensions);
auto rhs_pixels = get_dimensions_pixel_count(rhs.dimensions);
if (lhs_pixels != rhs_pixels) {
return lhs_pixels < rhs_pixels;
}

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/net/DcId.h"
@ -24,11 +25,6 @@ namespace td {
class FileManager;
struct Dimensions {
uint16 width = 0;
uint16 height = 0;
};
struct PhotoSize {
int32 type = 0;
Dimensions dimensions;
@ -41,13 +37,6 @@ struct AnimationSize final : public PhotoSize {
double main_frame_timestamp = 0.0;
};
Dimensions get_dimensions(int32 width, int32 height, const char *source);
bool operator==(const Dimensions &lhs, const Dimensions &rhs);
bool operator!=(const Dimensions &lhs, const Dimensions &rhs);
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions);
bool need_update_dialog_photo_minithumbnail(const string &from, const string &to);
td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string &packed);

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/Dimensions.hpp"
#include "td/telegram/files/FileId.hpp"
#include "td/telegram/PhotoSize.h"
#include "td/telegram/Version.h"
@ -15,19 +16,6 @@
namespace td {
template <class StorerT>
void store(Dimensions dimensions, StorerT &storer) {
store(static_cast<uint32>((static_cast<uint32>(dimensions.width) << 16) | dimensions.height), storer);
}
template <class ParserT>
void parse(Dimensions &dimensions, ParserT &parser) {
uint32 width_height;
parse(width_height, parser);
dimensions.width = static_cast<uint16>(width_height >> 16);
dimensions.height = static_cast<uint16>(width_height & 0xFFFF);
}
template <class StorerT>
void store(const PhotoSize &photo_size, StorerT &storer) {
LOG(DEBUG) << "Store photo size " << photo_size;

View File

@ -6,7 +6,7 @@
//
#pragma once
#include "td/telegram/PhotoSize.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/secret_api.h"
#include "td/telegram/telegram_api.h"

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/FullMessageId.h"

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/PhotoSize.h"
#include "td/telegram/SecretInputMedia.h"

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/PhotoSize.h"
#include "td/telegram/SecretInputMedia.h"

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/VoiceNotesManager.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/secret_api.h"
#include "td/telegram/Td.h"

View File

@ -13,6 +13,7 @@
#include "td/telegram/ChannelId.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/Document.h"
#include "td/telegram/DocumentsManager.h"
#include "td/telegram/DocumentsManager.hpp"
@ -21,7 +22,6 @@
#include "td/telegram/Photo.h"
#include "td/telegram/Photo.hpp"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/PhotoSize.h"
#include "td/telegram/Td.h"
#include "td/telegram/Version.h"
#include "td/telegram/VideosManager.h"

View File

@ -9,6 +9,7 @@
#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AudiosManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/Dimensions.h"
#include "td/telegram/Document.h"
#include "td/telegram/Document.hpp"
#include "td/telegram/DocumentsManager.h"
@ -22,7 +23,6 @@
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Photo.h"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/PhotoSize.h"
#include "td/telegram/secret_api.h"
#include "td/telegram/StickersManager.h"
#include "td/telegram/Td.h"