ladybird/Userland/Libraries/LibImageDecoderClient/Client.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

62 lines
1.7 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/AnonymousBuffer.h>
#include <LibImageDecoderClient/Client.h>
namespace ImageDecoderClient {
Client::Client(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket))
{
}
void Client::die()
{
if (on_death)
on_death();
}
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<ByteString> mime_type)
{
if (encoded_data.is_empty())
return {};
auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
if (encoded_buffer_or_error.is_error()) {
dbgln("Could not allocate encoded buffer");
return {};
}
auto encoded_buffer = encoded_buffer_or_error.release_value();
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
auto response_or_error = try_decode_image(move(encoded_buffer), mime_type);
if (response_or_error.is_error()) {
dbgln("ImageDecoder died heroically");
return {};
}
auto& response = response_or_error.value();
if (response.bitmaps().is_empty())
return {};
DecodedImage image;
image.is_animated = response.is_animated();
image.loop_count = response.loop_count();
image.frames.ensure_capacity(response.bitmaps().size());
auto bitmaps = response.take_bitmaps();
for (size_t i = 0; i < bitmaps.size(); ++i) {
if (!bitmaps[i].is_valid())
return {};
image.frames.empend(*bitmaps[i].bitmap(), response.durations()[i]);
}
return image;
}
}