Skip to content

Commit

Permalink
Update Wuffs to v0.4.0-alpha.9
Browse files Browse the repository at this point in the history
  • Loading branch information
dev0x13 committed Nov 4, 2024
1 parent 81dbfd3 commit b2370d0
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 32 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: Google
SortIncludes: true
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include src/wuffs-aux-image-wrapper.h src/wuffs-aux-json-wrapper.h libs/wuffs/release/c/wuffs-unsupported-snapshot.c
include src/wuffs-aux-image-wrapper.h src/wuffs-aux-json-wrapper.h src/wuffs-aux-utils.h libs/wuffs/release/c/wuffs-unsupported-snapshot.c

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ the [Auxiliary C++ API](https://github.com/google/wuffs/blob/main/doc/note/auxil
interest since it provides for "ridiculously fast" decoding of images of some types.

Current version of Wuffs library used in this project is **unsupported snapshot** taken from
[this](https://github.com/google/wuffs/tree/13c72db3508d33b9416a22a0ab8a8d4d8d5cd7be) commit. The primary
[this](https://github.com/google/wuffs/releases/tag/v0.4.0-alpha.9) tag. The primary
rationale for using the snapshot version instead of a stable release is that it provides JPEG decoder.

## Installation
Expand Down
2 changes: 1 addition & 1 deletion libs/wuffs
Submodule wuffs updated 199 files
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def build_extensions(self):
]

setup(name="pywuffs",
version="1.2.1",
version="2.0.0",
description="Python bindings for Wuffs the Library",
author="Georgiy Manuilov",
url="https://github.com/dev0x13/pywuffs",
Expand Down
17 changes: 9 additions & 8 deletions src/wuffs-aux-image-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <pybind11/numpy.h>

#include <map>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include <wuffs-unsupported-snapshot.c>

#include "wuffs-aux-utils.h"

// This API wraps the wuffs_aux API for image decoding. The wrapper is needed
// since the wuffs_aux API uses the callback-based approach which doesn't play
// well with Python-C++ interop.
Expand Down Expand Up @@ -119,7 +121,7 @@ struct ImageDecoderConfig {
std::vector<ImageDecoderFlags> flags;
PixelBlend pixel_blend = static_cast<PixelBlend>(
wuffs_aux::DecodeImageArgPixelBlend::DefaultValue().repr);
std::vector<ImageDecoderQuirks> quirks;
std::map<ImageDecoderQuirks, uint64_t> quirks;
uint32_t background_color =
wuffs_aux::DecodeImageArgBackgroundColor::DefaultValue().repr;
uint32_t max_incl_dimension =
Expand Down Expand Up @@ -227,16 +229,15 @@ const std::string ImageDecoderError::UnsupportedPixelFormat =
const std::string ImageDecoderError::FailedToOpenFile =
"wuffs_aux_wrap::ImageDecoder::Decode: failed to open file";

class ImageDecoder : public wuffs_aux::DecodeImageCallbacks {
class ImageDecoder : public wuffs_aux::DecodeImageCallbacks {
public:
explicit ImageDecoder(const ImageDecoderConfig& config)
: quirks_vector_({config.quirks.begin(), config.quirks.end()}),
: quirks_vector_(utils::ConvertQuirks(config.quirks)),
enabled_decoders_(
{config.enabled_decoders.begin(), config.enabled_decoders.end()}),
pixel_format_(wuffs_base__make_pixel_format(config.pixel_format)),
quirks_(wuffs_aux::DecodeImageArgQuirks(
reinterpret_cast<uint32_t*>(quirks_vector_.data()),
quirks_vector_.size())),
quirks_(wuffs_aux::DecodeImageArgQuirks(quirks_vector_.data(),
quirks_vector_.size())),
flags_(GetFlagsBitmask(config.flags)),
pixel_blend_(wuffs_aux::DecodeImageArgPixelBlend(
static_cast<uint32_t>(config.pixel_blend))),
Expand Down Expand Up @@ -354,7 +355,7 @@ const std::string ImageDecoderError::FailedToOpenFile =

private:
ImageDecodingResult decoding_result_;
std::vector<ImageDecoderQuirks> quirks_vector_;
std::vector<wuffs_aux::QuirkKeyValuePair> quirks_vector_;
std::unordered_set<ImageDecoderType> enabled_decoders_;
wuffs_base__pixel_format pixel_format_;
wuffs_aux::DecodeImageArgQuirks quirks_;
Expand Down
33 changes: 20 additions & 13 deletions src/wuffs-aux-json-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <pybind11/numpy.h>
#include <pybind11/pytypes.h>

#include <map>
#include <string>
#include <utility>
#include <vector>

#include <wuffs-unsupported-snapshot.c>

#include "wuffs-aux-utils.h"

// This API wraps the wuffs_aux API for JSON decoding. The wrapper is needed
// since the wuffs_aux API uses the callback-based approach which doesn't play
// well with Python-C++ interop.
Expand Down Expand Up @@ -45,7 +47,7 @@ enum class JsonDecoderQuirks : uint32_t {
// This struct hosts wuffs_aux::DecodeJson arguments in more user- and
// Python- friendly fashion
struct JsonDecoderConfig {
std::vector<JsonDecoderQuirks> quirks;
std::map<JsonDecoderQuirks, uint64_t> quirks;
std::string json_pointer;
};

Expand Down Expand Up @@ -107,14 +109,20 @@ const std::string JsonDecoderError::BadDepth =
const std::string JsonDecoderError::FailedToOpenFile =
"wuffs_aux_wrap::JsonDecoder::Decode: failed to open file";
// + 1 is for stripping leading '#'
const std::string JsonDecoderError::BadC0ControlCode = wuffs_json__error__bad_c0_control_code + 1;
const std::string JsonDecoderError::BadC0ControlCode =
wuffs_json__error__bad_c0_control_code + 1;
const std::string JsonDecoderError::BadUtf8 = wuffs_json__error__bad_utf_8 + 1;
const std::string JsonDecoderError::BadBackslashEscape = wuffs_json__error__bad_backslash_escape + 1;
const std::string JsonDecoderError::BadBackslashEscape =
wuffs_json__error__bad_backslash_escape + 1;
const std::string JsonDecoderError::BadInput = wuffs_json__error__bad_input + 1;
const std::string JsonDecoderError::BadNewLineInAString = wuffs_json__error__bad_new_line_in_a_string + 1;
const std::string JsonDecoderError::BadQuirkCombination = wuffs_json__error__bad_quirk_combination + 1;
const std::string JsonDecoderError::UnsupportedNumberLength = wuffs_json__error__unsupported_number_length + 1;
const std::string JsonDecoderError::UnsupportedRecursionDepth = wuffs_json__error__unsupported_recursion_depth + 1;
const std::string JsonDecoderError::BadNewLineInAString =
wuffs_json__error__bad_new_line_in_a_string + 1;
const std::string JsonDecoderError::BadQuirkCombination =
wuffs_json__error__bad_quirk_combination + 1;
const std::string JsonDecoderError::UnsupportedNumberLength =
wuffs_json__error__unsupported_number_length + 1;
const std::string JsonDecoderError::UnsupportedRecursionDepth =
wuffs_json__error__unsupported_recursion_depth + 1;

class JsonDecoder : public wuffs_aux::DecodeJsonCallbacks {
public:
Expand All @@ -132,10 +140,9 @@ class JsonDecoder : public wuffs_aux::DecodeJsonCallbacks {
};

explicit JsonDecoder(const JsonDecoderConfig& config)
: quirks_vector_({config.quirks.begin(), config.quirks.end()}),
quirks_(wuffs_aux::DecodeJsonArgQuirks(
reinterpret_cast<uint32_t*>(quirks_vector_.data()),
quirks_vector_.size())),
: quirks_vector_(utils::ConvertQuirks(config.quirks)),
quirks_(wuffs_aux::DecodeJsonArgQuirks(quirks_vector_.data(),
quirks_vector_.size())),
json_pointer_(config.json_pointer) {}

/* DecodeJsonCallbacks methods implementation */
Expand Down Expand Up @@ -247,7 +254,7 @@ class JsonDecoder : public wuffs_aux::DecodeJsonCallbacks {
}

private:
std::vector<JsonDecoderQuirks> quirks_vector_;
std::vector<wuffs_aux::QuirkKeyValuePair> quirks_vector_;
wuffs_aux::DecodeJsonArgQuirks quirks_;
wuffs_aux::DecodeJsonArgJsonPointer json_pointer_;
std::vector<Entry> stack_;
Expand Down
21 changes: 21 additions & 0 deletions src/wuffs-aux-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <cstdint>
#include <map>
#include <vector>

namespace utils {

template <typename QuirkType>
std::vector<wuffs_aux::QuirkKeyValuePair> ConvertQuirks(
const std::map<QuirkType, uint64_t>& quirks_map) {
std::vector<wuffs_aux::QuirkKeyValuePair> quirks_vector;
quirks_vector.reserve(quirks_map.size());
for (const auto& quirk : quirks_map) {
quirks_vector.emplace_back(static_cast<uint32_t>(quirk.first),
quirk.second);
}
return quirks_vector;
}

} // namespace utils
8 changes: 4 additions & 4 deletions src/wuffs-bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ py::enum_<wuffs_aux_wrap::PixelFormat>(
"PixelBlend: pixel blend mode, default is PixelBlend.SRC.")
.def_readwrite(
"quirks", &wuffs_aux_wrap::ImageDecoderConfig::quirks,
"list: list of ImageDecoderQuirks, empty by default (PNG "
"decoder will always have ImageDecoderQuirks.IGNORE_CHECKSUM quirk "
"enabled implicitly).")
"dict: dict of ImageDecoderQuirks:<quirk-value> pairs, empty by "
"default (PNG decoder will always have "
"ImageDecoderQuirks.IGNORE_CHECKSUM quirk enabled implicitly).")
.def_readwrite(
"background_color",
&wuffs_aux_wrap::ImageDecoderConfig::background_color,
Expand Down Expand Up @@ -437,7 +437,7 @@ py::enum_<wuffs_aux_wrap::PixelFormat>(
"JsonDecoderError on error.");

py::class_<wuffs_aux_wrap::JsonDecoder>(aux_m, "JsonDecoder",
"JSON decoder class.")
"JSON decoder class.")
.def(py::init<const wuffs_aux_wrap::JsonDecoderConfig&>(),
"Sole constructor.\n\n"
"Args:"
Expand Down
2 changes: 1 addition & 1 deletion test/test_aux_image_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_decode_background_color(background_color, test_image):
])
def test_decode_image_quirks(test_image, quirk):
config = ImageDecoderConfig()
config.quirks = [quirk]
config.quirks = {quirk: 1}
decoder = ImageDecoder(config)
decoding_result = decoder.decode(test_image[1])
assert_decoded(decoding_result)
Expand Down
4 changes: 2 additions & 2 deletions test/test_aux_json_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_decode_default_config(file_path):

def test_decode_json_quirks():
config = JsonDecoderConfig()
config.quirks = [JsonDecoderQuirks.ALLOW_COMMENT_BLOCK,
JsonDecoderQuirks.ALLOW_EXTRA_COMMA]
config.quirks = {JsonDecoderQuirks.ALLOW_COMMENT_BLOCK: 1,
JsonDecoderQuirks.ALLOW_EXTRA_COMMA: 1}
decoder = JsonDecoder(config)
data = b"{\"test\": \"value\", \"test1\": 123,}"
decoding_result = decoder.decode(data)
Expand Down

0 comments on commit b2370d0

Please sign in to comment.