Skip to content

Commit

Permalink
Use wuffs-unsupported-snapshot, support JPEG
Browse files Browse the repository at this point in the history
  • Loading branch information
dev0x13 committed Mar 12, 2024
1 parent b7b2d90 commit 1abddda
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build/
_build/
pywuffs.egg-info/
__pycache__/
src/wuffs-unsupported-snapshot.c
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ project(pywuffs)
set(CMAKE_CXX_STANDARD 14)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Release)
endif()

if(UNIX)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++14 -Wall -Wextra -s -fvisibility=hidden -Wl,--strip-all")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS
${CMAKE_CXX_FLAGS}
"-std=c++14 -Wall -Wextra -s -fvisibility=hidden -Wl,--strip-all")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
elseif(MSVC)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "/arch:AVX")
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "/arch:AVX")
endif()

file(
DOWNLOAD
https://raw.githubusercontent.com/google/wuffs/13c72db3508d33b9416a22a0ab8a8d4d8d5cd7be/release/c/wuffs-unsupported-snapshot.c
${CMAKE_SOURCE_DIR}/src/wuffs-unsupported-snapshot.c)

find_package(pybind11 REQUIRED)

pybind11_add_module(pywuffs src/wuffs-bindings.cpp)
target_include_directories(pywuffs PRIVATE libs/wuffs-mirror-release-c/release/)
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-mirror-release-c/release/c/wuffs-v0.3.c
include src/wuffs-aux-image-wrapper.h src/wuffs-aux-json-wrapper.h src/wuffs-unsupported-snapshot.c

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ it only provides bindings for image and JSON decoding parts of
the [Auxiliary C++ API](https://github.com/google/wuffs/blob/main/doc/note/auxiliary-code.md) as being of the most
interest since it provides for "ridiculously fast" decoding of images of some types.

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

## Installation

Expand Down
1 change: 0 additions & 1 deletion libs/wuffs-mirror-release-c
Submodule wuffs-mirror-release-c deleted from e3f919
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def build_extensions(self):
ext_modules = [
Pybind11Extension(
"pywuffs",
["src/wuffs-bindings.cpp"],
include_dirs=["libs/wuffs-mirror-release-c/release"]
["src/wuffs-bindings.cpp"]
),
]

Expand Down
10 changes: 6 additions & 4 deletions src/wuffs-aux-image-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include <pybind11/numpy.h>

#include <c/wuffs-v0.3.c>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include "wuffs-unsupported-snapshot.c"

// 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 @@ -127,13 +128,14 @@ struct ImageDecoderConfig {
wuffs_aux::DecodeImageArgMaxInclMetadataLength::DefaultValue().repr;
std::vector<ImageDecoderType> enabled_decoders = {
ImageDecoderType::BMP, ImageDecoderType::GIF, ImageDecoderType::NIE,
ImageDecoderType::PNG, ImageDecoderType::TGA, ImageDecoderType::WBMP};
ImageDecoderType::PNG, ImageDecoderType::TGA, ImageDecoderType::WBMP,
ImageDecoderType::JPEG};
uint32_t pixel_format = wuffs_base__make_pixel_format(
static_cast<uint32_t>(PixelFormat::BGRA_PREMUL))
.repr;
};

// This structs represents the wuffs_aux::DecodeImageCallbacks::HandleMetadata
// This struct represents the wuffs_aux::DecodeImageCallbacks::HandleMetadata
// input
struct MetadataEntry {
wuffs_base__more_information minfo{};
Expand Down Expand Up @@ -252,7 +254,7 @@ const std::string ImageDecoderError::FailedToOpenFile =
uint32_t fourcc, wuffs_base__slice_u8 prefix_data,
bool prefix_closed) override {
if (enabled_decoders_.count(static_cast<ImageDecoderType>(fourcc)) == 0) {
return {nullptr, &free};
return {nullptr};
}
return wuffs_aux::DecodeImageCallbacks::SelectDecoder(fourcc, prefix_data,
prefix_closed);
Expand Down
3 changes: 2 additions & 1 deletion src/wuffs-aux-json-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <pybind11/numpy.h>
#include <pybind11/pytypes.h>

#include <c/wuffs-v0.3.c>
#include <string>
#include <utility>
#include <vector>

#include "wuffs-unsupported-snapshot.c"

// 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
2 changes: 1 addition & 1 deletion src/wuffs-bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <c/wuffs-v0.3.c>
#include "wuffs-unsupported-snapshot.c"

#include "wuffs-aux-image-wrapper.h"
#include "wuffs-aux-json-wrapper.h"
Expand Down
Binary file added test/images/lena.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions test/test_aux_image_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
(ImageDecoderType.TGA, IMAGES_PATH + "/lena.tga"),
(ImageDecoderType.NIE, IMAGES_PATH + "/hippopotamus.nie"),
(ImageDecoderType.GIF, IMAGES_PATH + "/lena.gif"),
(ImageDecoderType.WBMP, IMAGES_PATH + "/lena.wbmp")
(ImageDecoderType.WBMP, IMAGES_PATH + "/lena.wbmp"),
(ImageDecoderType.JPEG, IMAGES_PATH + "/lena.jpeg")
]


Expand Down Expand Up @@ -243,7 +244,7 @@ def test_decode_image_unsupported_image_format(param):
@pytest.mark.parametrize("test_image", TEST_IMAGES)
def test_decode_image_unsupported_pixel_format(test_image):
config = ImageDecoderConfig()
config.pixel_format = PixelFormat.RGB
config.pixel_format = PixelFormat.YCOCGK
decoder = ImageDecoder(config)
decoding_result = decoder.decode(test_image[1])
assert_not_decoded(decoding_result, ImageDecoderError.UnsupportedPixelFormat)
Expand Down

0 comments on commit 1abddda

Please sign in to comment.