Commit Graph

457 Commits

Author SHA1 Message Date
Sean Barrett 0bc88af4de stb_image: optimizations 2023-12-14 03:13:59 -08:00
Fabian Giesen 4da08a1dbd stb_image: create_png_image_raw restructuring
Allocate filter_buf for two scan lines that we do all the filter
processing in, then do all other conversions (bit depth,
endianness, inserting alpha=255 values) on the way out.

Separating the two concerns makes everything much clearer.
2023-12-14 03:13:59 -08:00
Fabian Giesen c6d7c32e5d stb_image: Two warning fixes 2023-12-14 03:13:59 -08:00
Fabian Giesen 07268cbf36 stb_image: New Paeth filter
This formulation is equivalent to the original (reference)
implementation but runs _significantly_ faster - this speeds up
the filtering portion of a Paeth-heavy 8192x8192 16-bit/channel
image by a factor of more than 2 on a Zen2 CPU.

I'm investigating doing a more thorough restructuring of this pass,
but this seems like a good first step.
2023-12-14 03:13:59 -08:00
Fabian Giesen e5f0e18d0f stb_image: Small PNG filter simplification
Paeth on the first scanline is not a separate filter, it
happens to be equivalent to the "sub" filter which is already
an option.
2023-12-14 03:13:59 -08:00
Fabian Giesen d373674115 stb_image: Fix zlib decoder end-of-stream handling
We speculatively try to fill our bit buffer to always contain
at least 16 bits for stbi__zhuffman_decode. It's not a sign of
a malformed stream for us to be reading past the end there,
because the contents of that bit buffer are speculative; it's
only a malformed stream if we actually _consume_ the extra bits.

This fix adds some extra logic where we the first time we hit
zeof, we add an explicit 16 extra zero bits at the top of the
bit buffer just so that for the purposes of the decoder, we have
16 bits in the buffer.

However, if at the end of stream, we have the "hit zeof once"
flag set and less than 16 bits remaining in the bit buffer, we
know some of those implicit zero bits got read, which indicates
we actually had a past-end-of-stream read. In that case, flag
it as an error.

While I'm in here, also rephrase the length-too-large check to
not do any potentially-overflowing pointer arithmetic.

Fixes issue #1456.
2023-12-14 03:13:59 -08:00
Sean Barrett 3ecc60f25a add version history update for stb_image 2023-01-29 10:19:12 -08:00
Sean Barrett 6199bf7713 update stb_image to 2.28 2023-01-29 06:24:13 -08:00
Sean Barrett 7c14c47e2f Merge branch 'sean_image' 2023-01-29 06:19:13 -08:00
Sean Barrett 1891060782 a fix 2023-01-29 06:17:45 -08:00
Fabian Giesen 40eb865b3b stb_image: header scan mode always needs to check for tRNS
For paletted images, header-scanning mode (used by stbi_info) kept
going after the image header to see if a tRNS (transparency) chunk
shows up to correctly determine the channel count, but we would
immediately return after IHDR for non-paletted images.

This is incorrect. We also change our reported channel count on
RGB images with a tRNS chunk, therefore non-paletted images also
have to resume scanning further chunks.

Update the logic to keep scanning regardless and report the
correct channel count from stbi_info for RGB images with tRNS
(constant alpha channel).

Fixes issue #1419.
2023-01-23 00:46:33 -08:00
Fabian Giesen 24fa1ff2e9 stb_image: Fix broken indenting introduced earlier 2023-01-22 19:44:49 -08:00
Fabian Giesen 0613e9c043 stb_image: Tweak end-of-JPEG junk scanning loop.
Be a bit more picky about what we consider a valid marker.
0xff 0x00 (stuffed 0 byte) should not count.

Fixes issue #1409.
2023-01-22 19:42:55 -08:00
Fabian Giesen 1096389590 stb_image: Accept some extra data between BMP header and payload
Limit to 1k, which is the maximum size of a 256-entry palette that
would ordinarily go there. It feels sensible to relax this a bit but
we don't want to go overboard.

Fixes issue #1325.
2023-01-22 18:52:04 -08:00
Fabian Giesen 51d295e33e stb_image: Add trailing semicolon to usage example
Fixes issue #1330 reported by lunasorcery.
2023-01-22 16:16:02 -08:00
Fabian Giesen 038b6ab9ec stb_image: Avoid stdint.h on 32-bit Symbian.
As suggested by "mupfdev" on Github. We don't have a way to test
here but seems simple enough.

Fixes issue #1321.
2023-01-22 15:51:38 -08:00
Fabian Giesen b15b04321d Merge branch 'NeilBickford-NV-neilbickford/additional-image-fixes' into dev 2023-01-22 15:33:45 -08:00
Fabian Giesen 4d160de463 stb_image: Fix name of stbi_set_unpremultiply_on_load_thread impl
Bug reported by N-R-K. Fixes #1276.
2023-01-22 15:29:01 -08:00
Fabian Giesen e5da6acacd Merge branch 'neilbickford/image_hdr_pgm_fixes' into dev 2023-01-22 15:12:38 -08:00
Neil Bickford 9f22cc9008 stb_image PNG: Checks for invalid DEFLATE codes.
Specifically, this rejects length codes 286 and 287, and distance codes 30 and 31.
This avoids a scenario in which a file could contain a table in which
0 corresponded to length code 287, which would result in writing 0 bits.

Signed-off-by: Neil Bickford <nbickford@nvidia.com>
2023-01-22 15:07:14 -08:00
Neil Bickford 5cfc2a744a Zero-initialize stbi__jpeg to avoid intermittent errors found by fuzz-testing 2022-11-29 00:36:36 -08:00
Neil Bickford 47164e4086 Add checks for signed integer overflow; further guard against cases where stbi__grow_buffer_unsafe doesn't read all bits required. 2022-11-29 00:36:36 -08:00
Neil Bickford 96fe76c213 Add range checks to fix a few crash issues in stb_image issues 1289 and 1291 2022-11-29 00:36:36 -08:00
Neil Bickford 84b94010a7 Add checks for PNM integer read overflows, add a 1GB limit on IDAT chunk sizes to fix an OOM issue, and check for a situation where a sequence of bad Huffman code reads could result in a left shift by a negative number. 2022-11-29 00:36:36 -08:00
Neil Bickford 2a02ff76b5 Fixes two stb_image issues that could occur with specially constructed HDR and PGM files.
Signed-off-by: Neil Bickford <nbickford@nvidia.com>
2022-11-29 00:36:23 -08:00
Fabian Giesen 5ba0baaa26 stb_image: Reject fractional JPEG component subsampling ratios
The component resamplers are not written to support this and I've
never seen it happen in a real (non-crafted) JPEG file so I'm
fine rejecting this as outright corrupt.

Fixes issue #1178.
2021-07-25 20:24:10 -07:00
Sean Barrett 7c65d5621f fix compiling with NO_HDR NO_LINEAR 2021-07-25 19:22:47 -07:00
Sean Barrett 1ee679ca2e update version numbers 2021-07-11 17:07:54 -07:00
Sean Barrett 08e4b18650 Merge branch 'dev' of https://github.com/nothings/stb into dev 2021-07-07 00:39:10 -07:00
Fabian Giesen 6ca560c9af stb_image: Update documentation for de-iPhone flag
It's default off, not default on.

Fixes issue #651.
2021-07-06 21:40:31 -07:00
Fabian Giesen 7e10880f53 stb_image: Update credits 2021-07-06 20:47:30 -07:00
Vladimír Vondruš 2c8cd33e2e stb_image: make unpremultiply and de-iPhone flags thread_local.
Follows the change done for vertical flipping in
eb48fbdced for these two options as well.
2021-07-06 20:46:41 -07:00
Fabian Giesen 1e82fd4a4e stb_image: BMP v4/v5 header parsing fixes
As per MS's own docs, should ignore the r/g/b bitmasks in the
header unless BI_BITFIELDS compression is selected. Factor out
setting the default masks since that now exists in two branches.

Add some more checking for unsupported compression formats and
illegal bpp/compression combinations while I'm at it.

Fixes issue #783.
2021-07-04 22:17:57 -07:00
Fabian Giesen 17bc84e15d stb_image: stbi__bmp_info only rewind stream on error
To be consistent with the other info functions.

Fixes issue #892.
2021-07-04 21:47:13 -07:00
Fabian Giesen ab18d9b250 stb_image: Fix two bugs found via VC++ /analyze
Also fixes issue #366.
2021-07-04 21:42:44 -07:00
Fabian Giesen 82f9950cea stb_image: Update credits 2021-07-04 01:39:10 -07:00
Eugene Golushkov db864a1e30 stb_image: fix building by MSVC for Windows 10 on ARM 2021-07-04 01:39:10 -07:00
Fabian Giesen 2506215e8a stb_image: Key Win32 UTF-8 support off _WIN32 not _MSC_VER
So that it also works on MinGW.

Fixes issue #729.
2021-07-04 01:39:10 -07:00
Fabian Giesen 991f1f6419 stb_image: Fix wrong buffer sizes passed to MultiByteToWideChar
Fixes issue #772.
2021-07-04 01:39:10 -07:00
Fabian Giesen 56a7113cd0 stb_image: Reorder format test sequence
Put the formats that start with a clear magic number first,
the dodgy ones that don't have much of a distinctive header
should be tested for later after we've ruled out the clearer
ones.

Fixes issue #787, hopefully. (Never got a clean repro.)
2021-07-04 01:39:10 -07:00
Fabian Giesen 618dbd01c8 stb_image: Document image size limits
Both the buffer size limits and the image dimension limits.

Fixes issue #672.
2021-07-04 01:39:10 -07:00
Fabian Giesen 4d3b93f589 stb_image: Erorr in BMP should error, not assert.
There was both the assert and the error check; should just be
the error check.

Fixes issue #881 (or rather, part of it).
2021-07-04 01:39:09 -07:00
Fabian Giesen 31ba943e3f stb_image: UB fix in stbi__get32le
Need to do the second-part shift on uint32 not int.
2021-07-04 01:39:09 -07:00
Fabian Giesen d6ab7faec0 stb_image: Update comment
As per recent patches, we do support 16-bit PNMs.
2021-07-04 01:39:09 -07:00
Fabian Giesen bb09317445 stb_image: Avoid left-shifts of signed values
It's implementation-specified behavior. Writing this code and then
relying on compiler strength reduction to turn it back into shifts
feels extremely silly but it is what it is.

Fixes issue #1097.
2021-07-04 01:39:09 -07:00
Fabian Giesen 43b32c7bab stb_image: Avoid shift of signed values in extend_receive
Use an equivalent formulation that has sgn=0 or 1, not 0 or -1.
This avoids right-shifting signed values, at least in this place.

Fixes issue #1061.
2021-07-04 01:39:09 -07:00
Fabian Giesen 6d857933d5 stb_image, stb_image_write: Fix compare sign warnings
For the stb_image fix, also replace the magic 288 with a more
descriptive name while I'm at it.

Fixes #1100
2021-07-04 01:39:09 -07:00
Fabian Giesen 265b73bb0b stb_image: Fix lrot definition, small extend_receive tweak
Define lrot in a way that doesn't involve UB when n==0.
Also, the previous patch ensures that n <= 15 for all callers
of stbi__extend_receive, so can remove the (less restrictive)
bounds check for 0 <= n < 17 (the bounds of stbi__bmask)
entirely.

Fixes issue #1065.
2021-07-04 01:39:09 -07:00
Fabian Giesen 86b7570cfb stb_image: Fix bug on JPEGs with malformed DC deltas
extend_receive implicitly requires n <= 15 (code length);
the maximum that actually makes sense for 8-bit baseline JPEG is
11, but 15 is the natural limit for us because the AC coding path
stores the number of magnitude bits in a nibble.

Check that DC delta bits are in range before attempting to call
extend_receive.

Fixes issue #1108.
2021-07-04 01:39:09 -07:00
Fabian Giesen 6ab6303f98 stb_image: Check results of all mallocs.
A few were missing. Make sure to always report ouf-of-memory
errors.

Fixes issue #1056.
2021-07-04 01:39:09 -07:00