Skip to content

Commit 00736c5

Browse files
committed
fix(ImageBuf): Fix bug in ImageBuf construction from ptr + neg strides (#4630)
There was a bug in span_from_buffer when strides were negative (basically, we were subtracting, when adding was correct since those strides were negative values). Also added a negative stride test to imagebuf_test.cpp. Embarrassed that we didn't already have this, since it would have caught the problem. --------- Signed-off-by: Larry Gritz <[email protected]>
1 parent cab32b6 commit 00736c5

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/libOpenImageIO/imagebuf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ span_from_buffer(void* data, TypeDesc format, int nchannels, int width,
9999
if (xstride >= 0) {
100100
bufend += xstride * (width - 1);
101101
} else {
102-
bufstart -= xstride * (width - 1);
102+
bufstart += xstride * (width - 1);
103103
}
104104
// Expand to the span range for a whole image plane.
105105
if (ystride >= 0) {
106106
bufend += ystride * (height - 1);
107107
} else {
108-
bufstart -= ystride * (height - 1);
108+
bufstart += ystride * (height - 1);
109109
}
110110
// Expand to the span range for a whole volume.
111111
if (depth > 1 && zstride != 0) {
112112
if (zstride >= 0) {
113113
bufend += zstride * (depth - 1);
114114
} else {
115-
bufstart -= zstride * (depth - 1);
115+
bufstart += zstride * (depth - 1);
116116
}
117117
}
118118
return { bufstart, size_t(bufend - bufstart) };
@@ -905,7 +905,7 @@ ImageBufImpl::set_bufspan_localpixels(span<std::byte> bufspan,
905905
}
906906
m_bufspan = bufspan;
907907
m_localpixels = (char*)buforigin;
908-
OIIO_DASSERT(check_span(m_bufspan, m_localpixels, spec().format));
908+
OIIO_ASSERT(check_span(m_bufspan, m_localpixels, spec().format));
909909
}
910910

911911

src/libOpenImageIO/imagebuf_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,31 @@ ImageBuf_test_appbuffer_strided()
300300
}
301301
}
302302
}
303+
304+
// Test negative strides by filling with yellow, backwards
305+
{
306+
ImageBufAlgo::fill(wrapped, cspan<float>(green));
307+
// Use the ImageBuf constructor from a pointer to the last pixel and
308+
// negative strides. But don't include the edge pixels of the original
309+
// buffer.
310+
ImageBuf neg(ImageSpec(res - 2, res - 2, nchans, TypeFloat),
311+
&mem[res - 2][res - 2][0] /* point to last pixel */,
312+
-nchans * sizeof(float) /* negative x stride */,
313+
-res * nchans * sizeof(float) /* negative y stride*/);
314+
const float yellow[nchans] = { 1.0f, 1.0f, 0.0f };
315+
ImageBufAlgo::fill(neg, cspan<float>(yellow));
316+
317+
for (int y = 0; y < res; ++y) {
318+
for (int x = 0; x < res; ++x) {
319+
if (x == 0 || x == res - 1 || y == 0 || y == res - 1)
320+
OIIO_CHECK_ASSERT(make_cspan(mem[y][x], nchans)
321+
== make_cspan(green));
322+
else
323+
OIIO_CHECK_ASSERT(make_cspan(mem[y][x], nchans)
324+
== make_cspan(yellow));
325+
}
326+
}
327+
}
303328
}
304329

305330

0 commit comments

Comments
 (0)