2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Implement progressive jpeg loading and sending.

This commit is contained in:
John Preston
2020-08-26 17:28:56 +04:00
parent 0888901d79
commit 437c9320cd
22 changed files with 201 additions and 96 deletions

View File

@@ -214,14 +214,14 @@ void LoadCloudFile(
Fn<void()> progress,
int downloadFrontPartSize = 0) {
const auto loadSize = downloadFrontPartSize
? downloadFrontPartSize
? std::min(downloadFrontPartSize, file.byteSize)
: file.byteSize;
if (file.loader) {
if (fromCloud == LoadFromCloudOrLocal) {
file.loader->permitLoadFromCloud();
}
if (file.loader->fullSize() < loadSize) {
file.loader->increaseLoadSize(loadSize);
if (file.loader->loadSize() < loadSize) {
file.loader->increaseLoadSize(loadSize, autoLoading);
}
return;
} else if ((file.flags & CloudFile::Flag::Failed)
@@ -236,6 +236,7 @@ void LoadCloudFile(
origin,
QString(),
loadSize,
file.byteSize,
UnknownFileLocation,
LoadToCacheAsWell,
fromCloud,

View File

@@ -1013,6 +1013,7 @@ void DocumentData::save(
&session(),
_urlLocation,
size,
size,
fromCloud,
autoLoading,
cacheTag());
@@ -1039,6 +1040,7 @@ void DocumentData::save(
locationType(),
toFile,
size,
size,
(saveToCache() ? LoadToCacheAsWell : LoadToFileOnly),
fromCloud,
autoLoading,

View File

@@ -104,7 +104,7 @@ bool PhotoData::loading(PhotoSize size) const {
} else if (valid == existing) {
return true;
}
return (_images[valid].loader->fullSize()
return (_images[valid].loader->loadSize()
>= _images[existing].progressivePartSize);
}
@@ -113,6 +113,10 @@ bool PhotoData::failed(PhotoSize size) const {
return (flags & Data::CloudFile::Flag::Failed);
}
void PhotoData::clearFailed(PhotoSize size) {
_images[validSizeIndex(size)].flags &= ~Data::CloudFile::Flag::Failed;
}
const ImageLocation &PhotoData::location(PhotoSize size) const {
return _images[validSizeIndex(size)].location;
}
@@ -144,10 +148,11 @@ int PhotoData::imageByteSize(PhotoSize size) const {
bool PhotoData::displayLoading() const {
const auto index = PhotoSizeIndex(PhotoSize::Large);
return _images[index].loader
? (!_images[index].loader->loadingLocal()
|| !_images[index].loader->autoLoading())
: (uploading() && !waitingForAlbum());
if (const auto loader = _images[index].loader.get()) {
return !loader->finished()
&& (!loader->loadingLocal() || !loader->autoLoading());
}
return (uploading() && !waitingForAlbum());
}
void PhotoData::cancel() {
@@ -262,7 +267,6 @@ void PhotoData::load(
// Could've changed, if the requested size didn't have a location.
const auto validSize = static_cast<PhotoSize>(valid);
const auto existingSize = static_cast<PhotoSize>(existing);
const auto finalCheck = [=] {
if (const auto active = activeMediaView()) {
return !active->image(size);
@@ -270,10 +274,25 @@ void PhotoData::load(
return true;
};
const auto done = [=](QImage result) {
if (const auto active = activeMediaView()) {
active->set(validSize, existingSize, std::move(result));
Expects(_images[valid].loader != nullptr);
// Find out what progressive photo size have we loaded exactly.
auto goodFor = validSize;
const auto loadSize = _images[valid].loader->loadSize();
if (valid > 0 && _images[valid].byteSize > loadSize) {
for (auto i = valid; i != 0;) {
--i;
const auto required = _images[i].progressivePartSize;
if (required > 0 && required <= loadSize) {
goodFor = static_cast<PhotoSize>(i);
break;
}
}
}
if (validSize == PhotoSize::Large) {
if (const auto active = activeMediaView()) {
active->set(validSize, goodFor, std::move(result));
}
if (validSize == PhotoSize::Large && goodFor == validSize) {
_owner->photoLoadDone(this);
}
};
@@ -453,6 +472,7 @@ void PhotoSaveClickHandler::onClickImpl() const {
if (!data->date) {
return;
} else {
data->clearFailed(PhotoSize::Large);
data->load(context());
}
}

View File

@@ -111,6 +111,7 @@ public:
[[nodiscard]] bool hasExact(Data::PhotoSize size) const;
[[nodiscard]] bool loading(Data::PhotoSize size) const;
[[nodiscard]] bool failed(Data::PhotoSize size) const;
void clearFailed(Data::PhotoSize size);
void load(
Data::PhotoSize size,
Data::FileOrigin origin,

View File

@@ -51,11 +51,15 @@ Image *PhotoMedia::thumbnailInline() const {
Image *PhotoMedia::image(PhotoSize size) const {
const auto &original = _images[PhotoSizeIndex(size)];
if (const auto image = original.data.get()) {
return (original.goodFor >= size) ? image : nullptr;
if (original.goodFor >= size) {
return image;
}
}
const auto &valid = _images[_owner->validSizeIndex(size)];
if (const auto image = valid.data.get()) {
return (valid.goodFor >= size) ? image : nullptr;
if (valid.goodFor >= size) {
return image;
}
}
return nullptr;
}

View File

@@ -2147,7 +2147,10 @@ not_null<PhotoData*> Session::processPhoto(
const auto i = find(levels);
return (i == thumbs.end())
? ImageWithLocation()
: Images::FromImageInMemory(i->second, "JPG");
: Images::FromImageInMemory(
i->second.image,
"JPG",
i->second.bytes);
};
const auto small = image(SmallLevels);
const auto thumbnail = image(ThumbnailLevels);
@@ -2271,7 +2274,7 @@ void Session::photoApplyFields(
return 0;
});
};
const auto found = ranges::max_element(sizes, std::greater<>(), area);
const auto found = ranges::max_element(sizes, std::less<>(), area);
return (found == sizes.end()
|| found->type() != mtpc_photoSizeProgressive)
? sizes.end()

View File

@@ -287,7 +287,11 @@ using PollId = uint64;
using WallPaperId = uint64;
constexpr auto CancelledWebPageId = WebPageId(0xFFFFFFFFFFFFFFFFULL);
using PreparedPhotoThumbs = base::flat_map<char, QImage>;
struct PreparedPhotoThumb {
QImage image;
QByteArray bytes;
};
using PreparedPhotoThumbs = base::flat_map<char, PreparedPhotoThumb>;
// [0] == -1 -- counting, [0] == -2 -- could not count
using VoiceWaveform = QVector<signed char>;