Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is a format job. Pre-commit has a first-party GitHub action, so we use
# that: https://github.com/pre-commit/action

name: Format

on:
workflow_dispatch:
pull_request:
push:
branches:
- master

jobs:
pre-commit:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Cache pre-commit
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
pre-commit-
- uses: pre-commit/action@v3.0.0
18 changes: 17 additions & 1 deletion .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,28 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true

- name: Get pip cache dir
id: pip-cache
shell: bash
run: echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT

- name: Cache pip dependencies
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
${{ runner.os }}-pip-

- name: Build and install
run: pip install --verbose .[test]

- name: Test
if: ${{ startsWith(matrix.platform, 'ubuntu') }}
run: pytest
36 changes: 27 additions & 9 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ jobs:
- name: Check metadata
run: pipx run twine check dist/*

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: dist/*.tar.gz


Expand All @@ -43,27 +44,40 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-14]

steps:
- uses: actions/checkout@v4
with:
submodules: true

- uses: pypa/cibuildwheel@v2.16
- name: Cache cibuildwheel
uses: actions/cache@v4
with:
path: |
~/Library/Caches/pip
~/.cache/pip
~/AppData/Local/pip/Cache
key: cibw-pip-${{ matrix.os }}-${{ hashFiles('pyproject.toml') }}
restore-keys: |
cibw-pip-${{ matrix.os }}-

- uses: pypa/cibuildwheel@v3.3.1
env:
CIBW_ARCHS_MACOS: universal2
CIBW_ARCHS_WINDOWS: auto ARM64
CIBW_TEST_SKIP: "*macosx* *win* *aarch64 cp31* pp* *musllinux*"
CIBW_ARCHS_WINDOWS: AMD64 ARM64
CIBW_SKIP: "pp* *musllinux* *_i686 cp314t-win*"
CIBW_TEST_SKIP: "*macosx* *win* *aarch64 cp314t-*"
CIBW_ENVIRONMENT: "PIP_ONLY_BINARY=:all:"
# CIBW_BEFORE_BUILD: "pip install --prefer-binary fire numpy opencv-python"

- name: Verify clean directory
run: git diff --exit-code
shell: bash

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: cibw-wheels-${{ matrix.os }}
path: wheelhouse/*.whl


Expand All @@ -72,15 +86,19 @@ jobs:
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
environment: pypi
permissions:
id-token: write

steps:
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: artifact
pattern: cibw-*
merge-multiple: true
path: dist

- uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
31 changes: 0 additions & 31 deletions BUILD.bazel

This file was deleted.

46 changes: 0 additions & 46 deletions WORKSPACE

This file was deleted.

1 change: 0 additions & 1 deletion external

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "pybind11_pixelmatch"
version = "0.1.3"
version = "0.1.4"
description="A C++17 port of the JavaScript pixelmatch library (with python binding), providing a small pixel-level image comparison library."
readme = "README.md"
authors = [
Expand Down
20 changes: 0 additions & 20 deletions renovate.json

This file was deleted.

8 changes: 4 additions & 4 deletions src/pixelmatch/image_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace pixelmatch {

std::optional<Image> readRgbaImageFromPngFile(const char* filename) {
std::optional<Image> readRgbaImageFromPngFile(const char* filename) noexcept {
int width, height, channels;
auto data = stbi_load(filename, &width, &height, &channels, 4);
if (!data) {
Expand All @@ -23,7 +23,7 @@ std::optional<Image> readRgbaImageFromPngFile(const char* filename) {
}

bool writeRgbaPixelsToPngFile(const char* filename, span<const uint8_t> rgbaPixels, int width,
int height, size_t strideInPixels) {
int height, size_t strideInPixels) noexcept {
struct Context {
std::ofstream output;
};
Expand All @@ -37,7 +37,7 @@ bool writeRgbaPixelsToPngFile(const char* filename, span<const uint8_t> rgbaPixe
}

stbi_write_png_to_func(
[](void* context, void* data, int len) {
[](void* context, void* data, int len) noexcept {
Context* contextObj = reinterpret_cast<Context*>(context);
contextObj->output.write(static_cast<const char*>(data), len);
},
Expand All @@ -47,7 +47,7 @@ bool writeRgbaPixelsToPngFile(const char* filename, span<const uint8_t> rgbaPixe
}

bool imageEquals(span<const uint8_t> img1, span<const uint8_t> img2, int width, int height,
size_t strideInPixels) {
size_t strideInPixels) noexcept {
// Check for identical images, respecting stride.
for (int y = 0; y < height; ++y) {
const size_t rowStartIndex = y * strideInPixels;
Expand Down
6 changes: 3 additions & 3 deletions src/pixelmatch/image_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Image {
* @param filename Filename to load.
* @return std::optional<Image> containing the image, or std::nullopt if the file could not be read.
*/
std::optional<Image> readRgbaImageFromPngFile(const char* filename);
std::optional<Image> readRgbaImageFromPngFile(const char* filename) noexcept;

/**
* Save an image as a PNG file.
Expand All @@ -35,7 +35,7 @@ std::optional<Image> readRgbaImageFromPngFile(const char* filename);
* @return true If the image was successfully saved.
*/
bool writeRgbaPixelsToPngFile(const char* filename, span<const uint8_t> rgbaPixels, int width,
int height, size_t strideInPixels);
int height, size_t strideInPixels) noexcept;

/**
* Returns true if two images are bit-identical.
Expand All @@ -49,6 +49,6 @@ bool writeRgbaPixelsToPngFile(const char* filename, span<const uint8_t> rgbaPixe
* @return true If the image is bit-identical.
*/
bool imageEquals(span<const uint8_t> img1, span<const uint8_t> img2, int width, int height,
size_t strideInPixels);
size_t strideInPixels) noexcept;

} // namespace pixelmatch
19 changes: 10 additions & 9 deletions src/pixelmatch/pixelmatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace {

static constexpr size_t kPixelBytes = 4;

inline float rgb2y(uint8_t r, uint8_t g, uint8_t b) {
inline float rgb2y(uint8_t r, uint8_t g, uint8_t b) noexcept {
return r * 0.29889531f + g * 0.58662247f + b * 0.11448223f;
}

inline float rgb2i(uint8_t r, uint8_t g, uint8_t b) {
inline float rgb2i(uint8_t r, uint8_t g, uint8_t b) noexcept {
return r * 0.59597799f - g * 0.27417610f - b * 0.32180189f;
}

inline float rgb2q(uint8_t r, uint8_t g, uint8_t b) {
inline float rgb2q(uint8_t r, uint8_t g, uint8_t b) noexcept {
return r * 0.21147017f - g * 0.52261711f + b * 0.31114694f;
}

Expand All @@ -30,7 +30,7 @@ inline float rgb2q(uint8_t r, uint8_t g, uint8_t b) {
* @param alpha The alpha value of the color, between 0 and 1.
* @return The blended color.
*/
inline uint8_t blend(uint8_t c, float a) {
inline uint8_t blend(uint8_t c, float a) noexcept {
return static_cast<uint8_t>(255.0f + (static_cast<float>(c) - 255.0f) * a);
}

Expand All @@ -48,7 +48,7 @@ inline uint8_t blend(uint8_t c, float a) {
* or darkens (positive if img2 lightens). Returns 0 if the pixels are identical.
*/
float colorDelta(span<const uint8_t> img1, span<const uint8_t> img2, size_t pos1, size_t pos2,
bool yOnly) {
bool yOnly) noexcept {
uint8_t r1 = img1[pos1 + 0];
uint8_t g1 = img1[pos1 + 1];
uint8_t b1 = img1[pos1 + 2];
Expand Down Expand Up @@ -133,7 +133,7 @@ bool hasManySiblings(span<const uint8_t> img, int x1, int y1, int width, int hei
* based on "Anti-aliased Pixel and Intensity Slope Detector" paper by V. Vysniauskas, 2009
*/
bool antialiased(span<const uint8_t> img, int x1, int y1, int width, int height,
size_t strideInPixels, span<const uint8_t> img2) {
size_t strideInPixels, span<const uint8_t> img2) noexcept {
const int x0 = std::max(x1 - 1, 0);
const int y0 = std::max(y1 - 1, 0);
const int x2 = std::min(x1 + 1, width - 1);
Expand Down Expand Up @@ -194,14 +194,15 @@ bool antialiased(span<const uint8_t> img, int x1, int y1, int width, int height,
hasManySiblings(img2, maxX, maxY, width, height, strideInPixels));
}

inline void drawPixel(span<uint8_t> output, size_t pos, Color color) {
inline void drawPixel(span<uint8_t> output, size_t pos, Color color) noexcept {
output[pos + 0] = color.r;
output[pos + 1] = color.g;
output[pos + 2] = color.b;
output[pos + 3] = color.a;
}

void drawGrayPixel(span<const uint8_t> img, size_t pos, float alpha, span<uint8_t> output) {
void drawGrayPixel(span<const uint8_t> img, size_t pos, float alpha,
span<uint8_t> output) noexcept {
const uint8_t r = img[pos + 0];
const uint8_t g = img[pos + 1];
const uint8_t b = img[pos + 2];
Expand All @@ -212,7 +213,7 @@ void drawGrayPixel(span<const uint8_t> img, size_t pos, float alpha, span<uint8_
} // namespace

int pixelmatch(span<const uint8_t> img1, span<const uint8_t> img2, span<uint8_t> output, int width,
int height, size_t strideInPixels, Options options) {
int height, size_t strideInPixels, Options options) noexcept {
// In release builds, return -1 if a precondition fails since the asserts will not trigger.
if (width <= 0 || height <= 0 || strideInPixels < static_cast<size_t>(width)) {
assert(width > 0);
Expand Down
Loading
Loading