MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / stbi__parse_png_file

Function stbi__parse_png_file

Source/Utils/stb_image.h:5077–5258  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5075#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
5076
5077static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5078{
5079 stbi_uc palette[1024], pal_img_n=0;
5080 stbi_uc has_trans=0, tc[3]={0};
5081 stbi__uint16 tc16[3];
5082 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
5083 int first=1,k,interlace=0, color=0, is_iphone=0;
5084 stbi__context *s = z->s;
5085
5086 z->expanded = NULL;
5087 z->idata = NULL;
5088 z->out = NULL;
5089
5090 if (!stbi__check_png_header(s)) return 0;
5091
5092 if (scan == STBI__SCAN_type) return 1;
5093
5094 for (;;) {
5095 stbi__pngchunk c = stbi__get_chunk_header(s);
5096 switch (c.type) {
5097 case STBI__PNG_TYPE('C','g','B','I'):
5098 is_iphone = 1;
5099 stbi__skip(s, c.length);
5100 break;
5101 case STBI__PNG_TYPE('I','H','D','R'): {
5102 int comp,filter;
5103 if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
5104 first = 0;
5105 if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
5106 s->img_x = stbi__get32be(s);
5107 s->img_y = stbi__get32be(s);
5108 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
5109 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
5110 z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
5111 color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
5112 if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
5113 if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
5114 comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
5115 filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
5116 interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
5117 if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
5118 if (!pal_img_n) {
5119 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
5120 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
5121 } else {
5122 // if paletted, then pal_n is our final components, and
5123 // img_n is # components to decompress/filter.
5124 s->img_n = 1;
5125 if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
5126 }
5127 // even with SCAN_header, have to scan to see if we have a tRNS
5128 break;
5129 }
5130
5131 case STBI__PNG_TYPE('P','L','T','E'): {
5132 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
5133 if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
5134 pal_len = c.length / 3;

Callers 2

stbi__do_pngFunction · 0.85
stbi__png_info_rawFunction · 0.85

Calls 14

stbi__check_png_headerFunction · 0.85
stbi__get_chunk_headerFunction · 0.85
stbi__skipFunction · 0.85
stbi__errFunction · 0.85
stbi__get32beFunction · 0.85
stbi__get8Function · 0.85
stbi__get16beFunction · 0.85
stbi__getnFunction · 0.85
stbi__create_png_imageFunction · 0.85

Tested by

no test coverage detected