diff --git a/CMakeLists.txt b/CMakeLists.txt index 73c2d2aeb..0f7eb62e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/td/telegram/AnimationsManager.h b/td/telegram/AnimationsManager.h index e07856531..5cdf73e84 100644 --- a/td/telegram/AnimationsManager.h +++ b/td/telegram/AnimationsManager.h @@ -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" diff --git a/td/telegram/Dimensions.cpp b/td/telegram/Dimensions.cpp new file mode 100644 index 000000000..9fd1a714c --- /dev/null +++ b/td/telegram/Dimensions.cpp @@ -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(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(dimensions.width) * static_cast(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 diff --git a/td/telegram/Dimensions.h b/td/telegram/Dimensions.h new file mode 100644 index 000000000..59430d912 --- /dev/null +++ b/td/telegram/Dimensions.h @@ -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 diff --git a/td/telegram/Dimensions.hpp b/td/telegram/Dimensions.hpp new file mode 100644 index 000000000..19ffa8d42 --- /dev/null +++ b/td/telegram/Dimensions.hpp @@ -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 +void store(Dimensions dimensions, StorerT &storer) { + store(static_cast((static_cast(dimensions.width) << 16) | dimensions.height), storer); +} + +template +void parse(Dimensions &dimensions, ParserT &parser) { + uint32 width_height; + parse(width_height, parser); + dimensions.width = static_cast(width_height >> 16); + dimensions.height = static_cast(width_height & 0xFFFF); +} + +} // namespace td diff --git a/td/telegram/DocumentsManager.cpp b/td/telegram/DocumentsManager.cpp index be2e34e4d..30a5ec135 100644 --- a/td/telegram/DocumentsManager.cpp +++ b/td/telegram/DocumentsManager.cpp @@ -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" diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 6873d88cb..a85aa2ab6 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -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" diff --git a/td/telegram/Payments.cpp b/td/telegram/Payments.cpp index 82ebb7888..cff860453 100644 --- a/td/telegram/Payments.cpp +++ b/td/telegram/Payments.cpp @@ -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" diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index e4db76724..71e774617 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -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" diff --git a/td/telegram/PhotoSize.cpp b/td/telegram/PhotoSize.cpp index 6c8af6d0f..14b0acc9e 100644 --- a/td/telegram/PhotoSize.cpp +++ b/td/telegram/PhotoSize.cpp @@ -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(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(dimensions.width) * static_cast(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; } diff --git a/td/telegram/PhotoSize.h b/td/telegram/PhotoSize.h index 2510983ca..2c3eb90f6 100644 --- a/td/telegram/PhotoSize.h +++ b/td/telegram/PhotoSize.h @@ -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 get_minithumbnail_object(const string &packed); diff --git a/td/telegram/PhotoSize.hpp b/td/telegram/PhotoSize.hpp index c62bb35be..85184d25e 100644 --- a/td/telegram/PhotoSize.hpp +++ b/td/telegram/PhotoSize.hpp @@ -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 -void store(Dimensions dimensions, StorerT &storer) { - store(static_cast((static_cast(dimensions.width) << 16) | dimensions.height), storer); -} - -template -void parse(Dimensions &dimensions, ParserT &parser) { - uint32 width_height; - parse(width_height, parser); - dimensions.width = static_cast(width_height >> 16); - dimensions.height = static_cast(width_height & 0xFFFF); -} - template void store(const PhotoSize &photo_size, StorerT &storer) { LOG(DEBUG) << "Store photo size " << photo_size; diff --git a/td/telegram/SecretInputMedia.h b/td/telegram/SecretInputMedia.h index 35c96ee6a..e9c620758 100644 --- a/td/telegram/SecretInputMedia.h +++ b/td/telegram/SecretInputMedia.h @@ -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" diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 0d118ca96..cb0a12fb1 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -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" diff --git a/td/telegram/VideoNotesManager.h b/td/telegram/VideoNotesManager.h index 944e6d0dd..f9bcf8af2 100644 --- a/td/telegram/VideoNotesManager.h +++ b/td/telegram/VideoNotesManager.h @@ -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" diff --git a/td/telegram/VideosManager.h b/td/telegram/VideosManager.h index 304b5e56e..476c2fb8c 100644 --- a/td/telegram/VideosManager.h +++ b/td/telegram/VideosManager.h @@ -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" diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 5021007d3..ddbd6ad18 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -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" diff --git a/td/telegram/WebPageBlock.cpp b/td/telegram/WebPageBlock.cpp index a4abdee99..229c00bd1 100644 --- a/td/telegram/WebPageBlock.cpp +++ b/td/telegram/WebPageBlock.cpp @@ -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" diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 08438292a..dfe0bbd7a 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -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"