Compare commits

...

7 Commits

Author SHA1 Message Date
Mikhail Morozov ed9ef0fecc
Merge 2b25f2f8cd into ae721c50ea 2024-04-03 20:39:06 +00:00
Mikhail Morozov 2b25f2f8cd stb_image: fix warning in clang 2024-04-03 23:38:14 +03:00
Mikhail Morozov 4187a39b85 stb_image: fix endianness for 16-bit ppm 2024-04-03 23:38:14 +03:00
Mikhail Morozov d805b402ef stb_image: fix previously introduced warnings 2024-04-03 23:38:13 +03:00
Mikhail Morozov a8b4d31395 stb_image: add pbm support 2024-04-03 23:38:13 +03:00
Mikhail Morozov 9629715904 stb_image: add pbm support 2024-04-03 23:38:13 +03:00
Mikhail Morozov 53e8427e90 stb_image: add pbm tests derived from pngsuite 2024-04-03 23:38:13 +03:00
5 changed files with 66 additions and 11 deletions

View File

@ -1018,7 +1018,7 @@ static int stbi__mul2sizes_valid(int a, int b)
return a <= INT_MAX/b;
}
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
static int stbi__mad2sizes_valid(int a, int b, int add)
{
@ -1042,7 +1042,7 @@ static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
}
#endif
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
// mallocs with size overflow checking
static void *stbi__malloc_mad2(int a, int b, int add)
{
@ -7493,7 +7493,7 @@ static int stbi__pnm_test(stbi__context *s)
char p, t;
p = (char) stbi__get8(s);
t = (char) stbi__get8(s);
if (p != 'P' || (t != '5' && t != '6')) {
if (p != 'P' || (t != '4' && t != '5' && t != '6')) {
stbi__rewind( s );
return 0;
}
@ -7503,12 +7503,12 @@ static int stbi__pnm_test(stbi__context *s)
static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
stbi_uc *out;
int bytes_per_channel;
STBI_NOTUSED(ri);
ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n);
if (ri->bits_per_channel == 0)
return 0;
if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
@ -7519,11 +7519,65 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req
if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0))
return stbi__errpuc("too large", "PNM too large");
out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0);
if (ri->bits_per_channel == 16) bytes_per_channel = 2; else bytes_per_channel = 1;
out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, bytes_per_channel, 0);
if (!out) return stbi__errpuc("outofmem", "Out of memory");
if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) {
STBI_FREE(out);
return stbi__errpuc("bad PNM", "PNM file truncated");
if (ri->bits_per_channel == 1) {
stbi_uc *filebuf, *p_in, *p_out;
stbi__uint32 line, x, y, offset;
ri->bits_per_channel = 8;
line = s->img_x/8+((s->img_x%8>0)?1:0);
filebuf = (stbi_uc *) stbi__malloc_mad2(line, s->img_y, 0);
if (!filebuf) {
STBI_FREE(out);
return stbi__errpuc("outofmem", "Out of memory");
}
if (!stbi__getn(s, filebuf, line*s->img_y)) {
STBI_FREE(out);
STBI_FREE(filebuf);
return stbi__errpuc("bad PNM", "PNM file truncated");
}
p_in = filebuf;
p_out = out;
offset = 8;
for(y = 0; y < s->img_y; y++) {
for(x = 0; x < s->img_x; x++) {
offset--;
*(p_out++) = ((*p_in>>offset)&0x1)?0:255;
if(offset == 0) {
offset = 8;
p_in++;
}
}
if(offset != 8) {
offset = 8;
p_in++;
}
}
STBI_FREE(filebuf);
} else {
if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) {
STBI_FREE(out);
return stbi__errpuc("bad PNM", "PNM file truncated");
}
if (ri->bits_per_channel == 16) {
stbi_uc *p8;
stbi__uint16 *p16;
size_t filesize;
p8 = out;
p16 = (stbi__uint16 *) out;
filesize = s->img_n * s->img_x * s->img_y;
while(filesize--) {
// Convert from BIG-ENDIAN
*(p16++) = p8[1] + p8[0]*256;
p8 += 2;
}
}
}
if (req_comp && req_comp != s->img_n) {
@ -7589,12 +7643,12 @@ static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
// Get identifier
p = (char) stbi__get8(s);
t = (char) stbi__get8(s);
if (p != 'P' || (t != '5' && t != '6')) {
if (p != 'P' || (t != '4' && t != '5' && t != '6')) {
stbi__rewind(s);
return 0;
}
*comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm
*comp = (t == '6') ? 3 : 1; // '4' is monochrome .pbm, '5' is 1-component .pgm; '6' is 3-component .ppm
c = (char) stbi__get8(s);
stbi__pnm_skip_whitespace(s, &c);
@ -7607,6 +7661,7 @@ static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
*y = stbi__pnm_getinteger(s, &c); // read height
if (*y == 0)
return stbi__err("invalid width", "PPM image header had zero or overflowing width");
if(t == '4') return 1; // Header ends here for PBM files
stbi__pnm_skip_whitespace(s, &c);
maxv = stbi__pnm_getinteger(s, &c); // read max value

View File

@ -22,7 +22,7 @@ unzip $SRC/stbi/tga.zip -d $SRC/stb/tests
find $SRC/stb/tests -name "*.png" -o -name "*.jpg" -o -name "*.gif" \
-o -name "*.bmp" -o -name "*.tga" -o -name "*.TGA" \
-o -name "*.ppm" -o -name "*.pgm" \
-o -name "*.ppm" -o -name "*.pgm" -o -name "*.pbm" \
| xargs zip $OUT/stbi_read_fuzzer_seed_corpus.zip
echo "" >> $SRC/stbi/gif.dict

BIN
tests/pbm/basi0g01.pbm Normal file

Binary file not shown.

BIN
tests/pbm/basi0g01_31.pbm Normal file

Binary file not shown.

BIN
tests/pbm/basi0g01_33.pbm Normal file

Binary file not shown.