unsigned getFilterTypes(std::vector<unsigned char>& filterTypes, const std::vector<unsigned char>& png) { std::vector<std::vector<unsigned char> > passes; unsigned error = getFilterTypesInterlaced(passes, png); if(error) return error; if(passes.size() == 1) { filterTypes.swap(passes[0]); } else { lodepng::State state; unsigned w, h; lodepng_inspect(&w, &h, &state, &png[0], png.size()); /* Interlaced. Simplify it: put pass 6 and 7 alternating in the one vector so that one filter per scanline of the uninterlaced image is given, with that filter corresponding the closest to what it would be for non-interlaced image. */ for(size_t i = 0; i < h; i++) { filterTypes.push_back(i % 2 == 0 ? passes[5][i / 2] : passes[6][i / 2]); } } return 0; /* OK */ }
SDL_Surface *TCOD_sys_read_png(const char *filename) { unsigned error; unsigned char* image; unsigned width, height, y, bpp; unsigned char* png; size_t pngsize; LodePNGState state; SDL_Surface *bitmap; unsigned char *source; unsigned int rowsize; lodepng_state_init(&state); /*optionally customize the state*/ if (!TCOD_sys_read_file(filename,&png,&pngsize)) return NULL; lodepng_inspect(&width,&height,&state, png, pngsize); bpp=lodepng_get_bpp(&state.info_png.color); if ( bpp == 24 ) { /* don't convert to 32 bits because libtcod's 24bits renderer is faster */ state.info_raw.colortype=LCT_RGB; } else if ( bpp != 24 && bpp != 32 ) { /* paletted png. convert to 24 bits */ state.info_raw.colortype=LCT_RGB; state.info_raw.bitdepth=8; bpp=24; } error = lodepng_decode(&image, &width, &height, &state, png, pngsize); free(png); if(error) { printf("error %u: %s\n", error, lodepng_error_text(error)); lodepng_state_cleanup(&state); return NULL; } /* create the SDL surface */ bitmap=TCOD_sys_get_surface(width,height,bpp==32); source=image; rowsize=width*bpp/8; for (y=0; y< height; y++ ) { Uint8 *row_pointer=(Uint8 *)(bitmap->pixels) + y * bitmap->pitch; memcpy(row_pointer,source,rowsize); source+=rowsize; } lodepng_state_cleanup(&state); free(image); return bitmap; }
/* Show the filtertypes of each scanline in this PNG image. */ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_checksums) { //Get color type and interlace type lodepng::State state; if(ignore_checksums) { state.decoder.ignore_crc = 1; state.decoder.zlibsettings.ignore_adler32 = 1; } unsigned w, h; unsigned error; error = lodepng_inspect(&w, &h, &state, &buffer[0], buffer.size()); if(error) { std::cout << "inspect error " << error << ": " << lodepng_error_text(error) << std::endl; return; } if(state.info_png.interlace_method == 1) { std::cout << "showing filtertypes for interlaced PNG not supported by this example" << std::endl; return; } //Read literal data from all IDAT chunks const unsigned char *chunk, *begin, *end, *next; end = &buffer.back() + 1; begin = chunk = &buffer.front() + 8; std::vector<unsigned char> zdata; while(chunk + 8 < end && chunk >= begin) { char type[5]; lodepng_chunk_type(type, chunk); if(std::string(type).size() != 4) { std::cout << "this is probably not a PNG" << std::endl; return; } if(std::string(type) == "IDAT") { const unsigned char* cdata = lodepng_chunk_data_const(chunk); unsigned clength = lodepng_chunk_length(chunk); if(chunk + clength + 12 > end || clength > buffer.size() || chunk + clength + 12 < begin) { std::cout << "invalid chunk length" << std::endl; return; } for(unsigned i = 0; i < clength; i++) { zdata.push_back(cdata[i]); } } next = lodepng_chunk_next_const(chunk); if (next <= chunk) break; // integer overflow chunk = next; } //Decompress all IDAT data std::vector<unsigned char> data; error = lodepng::decompress(data, &zdata[0], zdata.size()); if(error) { std::cout << "decompress error " << error << ": " << lodepng_error_text(error) << std::endl; return; } //A line is 1 filter byte + all pixels size_t linebytes = 1 + lodepng_get_raw_size(w, 1, &state.info_png.color); if(linebytes == 0) { std::cout << "error: linebytes is 0" << std::endl; return; } std::cout << "Filter types: "; for(size_t i = 0; i < data.size(); i += linebytes) { std::cout << (int)(data[i]) << " "; } std::cout << std::endl; }
unsigned getFilterTypesInterlaced(std::vector<std::vector<unsigned char> >& filterTypes, const std::vector<unsigned char>& png) { //Get color type and interlace type lodepng::State state; unsigned w, h; unsigned error; error = lodepng_inspect(&w, &h, &state, &png[0], png.size()); if(error) return 1; //Read literal data from all IDAT chunks const unsigned char *chunk, *begin, *end, *next; end = &png.back() + 1; begin = chunk = &png.front() + 8; std::vector<unsigned char> zdata; while(chunk + 8 < end && chunk >= begin) { char type[5]; lodepng_chunk_type(type, chunk); if(std::string(type).size() != 4) return 1; //Probably not a PNG file if(std::string(type) == "IDAT") { const unsigned char* cdata = lodepng_chunk_data_const(chunk); unsigned clength = lodepng_chunk_length(chunk); if(chunk + clength + 12 > end || clength > png.size() || chunk + clength + 12 < begin) { // corrupt chunk length return 1; } for(unsigned i = 0; i < clength; i++) { zdata.push_back(cdata[i]); } } next = lodepng_chunk_next_const(chunk); if (next <= chunk) return 1; // integer overflow chunk = next; } //Decompress all IDAT data std::vector<unsigned char> data; error = lodepng::decompress(data, &zdata[0], zdata.size()); if(error) return 1; if(state.info_png.interlace_method == 0) { filterTypes.resize(1); //A line is 1 filter byte + all pixels size_t linebytes = 1 + lodepng_get_raw_size(w, 1, &state.info_png.color); for(size_t i = 0; i < data.size(); i += linebytes) { filterTypes[0].push_back(data[i]); } } else { //Interlaced filterTypes.resize(7); static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/ static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/ static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/ static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/ size_t pos = 0; for(size_t j = 0; j < 7; j++) { unsigned w2 = (w - ADAM7_IX[j] + ADAM7_DX[j] - 1) / ADAM7_DX[j]; unsigned h2 = (h - ADAM7_IY[j] + ADAM7_DY[j] - 1) / ADAM7_DY[j]; if(ADAM7_IX[j] >= w || ADAM7_IY[j] >= h) w2 = h2 = 0; size_t linebytes = 1 + lodepng_get_raw_size(w2, 1, &state.info_png.color); for(size_t i = 0; i < h2; i++) { filterTypes[j].push_back(data[pos]); pos += linebytes; } } } return 0; /* OK */ }