Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_image: pbm support #1476

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 65 additions & 10 deletions stb_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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?)");

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ossfuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added tests/pbm/basi0g01.pbm
Binary file not shown.
Binary file added tests/pbm/basi0g01_31.pbm
Binary file not shown.
Binary file added tests/pbm/basi0g01_33.pbm
Binary file not shown.