Skip to content

Commit ed40859

Browse files
authored
fix(jpeg): Fix wrong pointers/crashing when decodng CMYK jpeg files (#4963)
Was just wrong. Presumably broken recently. In the process, switched internal cmyk->rgb conversion to be span based. Fixes #4962 --------- Signed-off-by: Larry Gritz <[email protected]>
1 parent 5f72880 commit ed40859

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed

src/cmake/testing.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ macro (oiio_add_all_tests)
149149
oiiotool-xform
150150
diff
151151
dither dup-channels
152-
jpeg-corrupt jpeg-metadata
152+
jpeg jpeg-corrupt jpeg-metadata
153153
maketx oiiotool-maketx
154154
misnamed-file
155155
missingcolor

src/jpeg.imageio/jpeginput.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -532,22 +532,24 @@ JpgInput::read_uhdr(Filesystem::IOProxy* ioproxy)
532532

533533

534534

535+
template<typename Tcmyk, typename Trgb>
535536
static void
536-
cmyk_to_rgb(int n, const unsigned char* cmyk, size_t cmyk_stride,
537-
unsigned char* rgb, size_t rgb_stride)
537+
cmyk_to_rgb(cspan<Tcmyk> cmyk, span<Trgb> rgb)
538538
{
539-
for (; n; --n, cmyk += cmyk_stride, rgb += rgb_stride) {
539+
size_t n = cmyk.size() / 4;
540+
OIIO_ASSERT(rgb.size() == n * 3);
541+
for (size_t i = 0; i < n; ++i) {
540542
// JPEG seems to store CMYK as 1-x
541-
float C = convert_type<unsigned char, float>(cmyk[0]);
542-
float M = convert_type<unsigned char, float>(cmyk[1]);
543-
float Y = convert_type<unsigned char, float>(cmyk[2]);
544-
float K = convert_type<unsigned char, float>(cmyk[3]);
545-
float R = C * K;
546-
float G = M * K;
547-
float B = Y * K;
548-
rgb[0] = convert_type<float, unsigned char>(R);
549-
rgb[1] = convert_type<float, unsigned char>(G);
550-
rgb[2] = convert_type<float, unsigned char>(B);
543+
float C = convert_type<Tcmyk, float>(cmyk[4 * i + 0]);
544+
float M = convert_type<Tcmyk, float>(cmyk[4 * i + 1]);
545+
float Y = convert_type<Tcmyk, float>(cmyk[4 * i + 2]);
546+
float K = convert_type<Tcmyk, float>(cmyk[4 * i + 3]);
547+
float R = C * K;
548+
float G = M * K;
549+
float B = Y * K;
550+
rgb[3 * i + 0] = convert_type<float, Trgb>(R);
551+
rgb[3 * i + 1] = convert_type<float, Trgb>(G);
552+
rgb[3 * i + 2] = convert_type<float, Trgb>(B);
551553
}
552554
}
553555

@@ -682,10 +684,12 @@ JpgInput::read_native_scanlines(int subimage, int miplevel, int ybegin,
682684
}
683685
m_next_scanline = yend;
684686

685-
if (m_cmyk)
686-
cmyk_to_rgb(m_spec.width * nscanlines,
687-
reinterpret_cast<unsigned char*>(readdata), 4,
688-
reinterpret_cast<unsigned char*>(data.data()), 3);
687+
if (m_cmyk) {
688+
for (int i = 0; i < nscanlines; ++i)
689+
cmyk_to_rgb(make_cspan(readdata[i], m_spec.width * 4),
690+
span_cast<unsigned char>(data).subspan(
691+
m_spec.width * 3 * i, m_spec.width * 3));
692+
}
689693

690694
return true;
691695
}

testsuite/jpeg/ref/out.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Reading src/YCbCrK.jpg
2+
src/YCbCrK.jpg : 52 x 52, 3 channel, uint8 jpeg
3+
SHA-1: 888C1DD026F9C9A613A94BE5053292AC7010E79C
4+
channel list: R, G, B
5+
jpeg:ColorSpace: "YCbCrK"
6+
jpeg:subsampling: "4:4:4"
7+
oiio:ColorSpace: "srgb_rec709_scene"
8+
Comparing "rgb-from-YCbCrK.tif" and "ref/rgb-from-YCbCrK.tif"
9+
PASS
4.13 KB
Binary file not shown.

testsuite/jpeg/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright Contributors to the OpenImageIO project.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# https://github.com/AcademySoftwareFoundation/OpenImageIO
6+
7+
8+
# Test ability to read a JPEG YCbCrK encoded file
9+
command += info_command ("src/YCbCrK.jpg", safematch=True)
10+
command += oiiotool ("src/YCbCrK.jpg -o rgb-from-YCbCrK.tif")
11+
12+
outputs = [ "rgb-from-YCbCrK.tif", "out.txt" ]

testsuite/jpeg/src/YCbCrK.jpg

2.64 KB
Loading

0 commit comments

Comments
 (0)