Skip to content

Conversation

@RunDevelopment
Copy link
Collaborator

@RunDevelopment RunDevelopment commented Jul 14, 2025

Resolves #56

This PR adds support for row pitch in image views, which makes cropping and en/decoding image rectangles possible. This is quite useful, but was a pain to implement, because the assumption row_pitch == width * bytes_per_pixel as built into a lot of en/decoders. (It was such a pain that this PR is actually my third attempt at implementing this. But I'm quite happy that I did. The library is better for it.)

I tested the new row pitch changes by en/decoding random images/data with a both contiguous and non-contiguous views. If everything is implemented correctly, they'll produce the same result. This is done for all possible combinations of format and color (currently 73*12).

The performance impact of this feature is negligible. It's maybe 1% on average. Truth be told, there was a lot of noise in my benchmark timings, so 1% is my best guesstimate. However, I can say with certainty that there are no dramatic slowdowns.

Changes:

  • Add a row_pitch: usize field to ImageView and ImageViewMut. This makes it possible to use ImageView* not just for whole (contiguous) images, but also for cropped sub-views of images.
  • Added public cropped and is_contiguous functions to the view types.
  • Changed the decode_rect and Decoder::read_surface_rect APIs to make use of row pitch support in ImageViewMut. I also updated DecodeError to remove 2 error variants that are no longer possible and to add 1 new one.
  • Removed the Rect type and replaced it with an Offset type.
  • Updated all encoder and decoder implementations to support row pitch. Despite adding functionality, some implementations actually got simpler, which is very nice.
  • A few minor fly-by improvements.

@RunDevelopment RunDevelopment marked this pull request as ready for review July 15, 2025 13:21
@RunDevelopment RunDevelopment requested a review from Copilot July 15, 2025 13:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for custom row pitches to ImageView and ImageViewMut, enabling encoded/decoded operations on cropped or non-contiguous sub-views. It refactors encoders and decoders to consume these views instead of raw buffers and updates tests to validate that contiguous vs. non‐contiguous workflows produce identical results.

  • Introduce row_pitch in ImageView/ImageViewMut plus new_with(), is_contiguous(), cropped(), and row iterators.
  • Refactor decoder/encoder APIs to use image views and new utilities (for_each_chunk, for_each_f32_rgba_rows).
  • Update and add tests (test_row_pitch) to verify contiguous and non-contiguous decode/encode yield the same output.

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/util.rs Switched read_dds_rect_as_u8 to the new read_surface_rect(image.view_mut(), rect) API
tests/encode.rs Replaced manual buffer/stride logic with read_surface + .cropped(); added test_row_pitch
tests/decode.rs Updated decode_rect tests to use ImageViewMut; added a decoding test_row_pitch
src/lib.rs Added row_pitch field, constructors (new_with), is_contiguous, cropped, and row iterators
src/error.rs Removed legacy pitch/buffer errors; added UnexpectedRectSize
src/resize.rs Adjusted resizing to handle non‐contiguous views via row iterators
src/split.rs Refactored split_surface_into_lines to use iterator-based ranges and cropping
src/encode/write_util.rs New helpers: for_each_f32_rgba_rows and for_each_chunk
src/encode/* Updated all encoder modules to accept ImageView, use chunk/row utilities
src/decode/* Updated all decoder modules to accept ImageViewMut, use offset abstraction and row iterators
Comments suppressed due to low confidence (2)

src/error.rs:127

  • [nitpick] The message "Unexpected rectangle size" is vague—consider including the expected vs. actual sizes or clarifying that the image view size must match the rectangle size to aid debugging.
            DecodingError::UnexpectedRectSize => {

src/split.rs:6

  • [nitpick] Returning Option<impl Iterator<...>> can obscure control flow and make callers less flexible. Consider returning a Vec<Range<u32>> directly (or Box<dyn Iterator<...>>) to simplify the code and improve readability.
fn split_surface_into_lines(

@RunDevelopment
Copy link
Collaborator Author

RunDevelopment commented Jul 15, 2025

Not even bad. I didn't expect copilot's reviews to be very good, but it actually found a bug my tests didn't catch. Sure, the fix wasn't 100% correct, but just pointing out the problem is good enough.

As for the bug itself, I fixed it and changed the tests to cover the changed code paths.

@RunDevelopment RunDevelopment requested a review from fintelia July 15, 2025 13:47
@RunDevelopment RunDevelopment mentioned this pull request Jul 15, 2025
2 tasks
@RunDevelopment RunDevelopment merged commit 7140296 into image-rs:main Aug 1, 2025
10 checks passed
@RunDevelopment RunDevelopment deleted the row-pitch branch August 1, 2025 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Row pitch for ImageView and ImageViewMut

1 participant