MCPcopy Create free account
hub / github.com/DFHack/dfhack / preProcessScanlines

Function preProcessScanlines

depends/lodepng/lodepng.cpp:5709–5780  ·  view source on GitHub ↗

out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image. return value is error**/

Source from the content-addressed store, hash-verified

5707/*out must be buffer big enough to contain uncompressed IDAT chunk data, and in must contain the full image.
5708return value is error**/
5709static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in,
5710 unsigned w, unsigned h,
5711 const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings) {
5712 /*
5713 This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
5714 *) if no Adam7: 1) add padding bits (= possible extra bits per scanline if bpp < 8) 2) filter
5715 *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
5716 */
5717 unsigned bpp = lodepng_get_bpp(&info_png->color);
5718 unsigned error = 0;
5719
5720 if(info_png->interlace_method == 0) {
5721 *outsize = h + (h * ((w * bpp + 7u) / 8u)); /*image size plus an extra byte per scanline + possible padding bits*/
5722 *out = (unsigned char*)lodepng_malloc(*outsize);
5723 if(!(*out) && (*outsize)) error = 83; /*alloc fail*/
5724
5725 if(!error) {
5726 /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
5727 if(bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) {
5728 unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7u) / 8u));
5729 if(!padded) error = 83; /*alloc fail*/
5730 if(!error) {
5731 addPaddingBits(padded, in, ((w * bpp + 7u) / 8u) * 8u, w * bpp, h);
5732 error = filter(*out, padded, w, h, &info_png->color, settings);
5733 }
5734 lodepng_free(padded);
5735 } else {
5736 /*we can immediately filter into the out buffer, no other steps needed*/
5737 error = filter(*out, in, w, h, &info_png->color, settings);
5738 }
5739 }
5740 } else /*interlace_method is 1 (Adam7)*/ {
5741 unsigned passw[7], passh[7];
5742 size_t filter_passstart[8], padded_passstart[8], passstart[8];
5743 unsigned char* adam7;
5744
5745 Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
5746
5747 *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
5748 *out = (unsigned char*)lodepng_malloc(*outsize);
5749 if(!(*out)) error = 83; /*alloc fail*/
5750
5751 adam7 = (unsigned char*)lodepng_malloc(passstart[7]);
5752 if(!adam7 && passstart[7]) error = 83; /*alloc fail*/
5753
5754 if(!error) {
5755 unsigned i;
5756
5757 Adam7_interlace(adam7, in, w, h, bpp);
5758 for(i = 0; i != 7; ++i) {
5759 if(bpp < 8) {
5760 unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]);
5761 if(!padded) ERROR_BREAK(83); /*alloc fail*/
5762 addPaddingBits(padded, &adam7[passstart[i]],
5763 ((passw[i] * bpp + 7u) / 8u) * 8u, passw[i] * bpp, passh[i]);
5764 error = filter(&(*out)[filter_passstart[i]], padded,
5765 passw[i], passh[i], &info_png->color, settings);
5766 lodepng_free(padded);

Callers 1

lodepng_encodeFunction · 0.85

Calls 7

lodepng_get_bppFunction · 0.85
lodepng_mallocFunction · 0.85
addPaddingBitsFunction · 0.85
filterFunction · 0.85
lodepng_freeFunction · 0.85
Adam7_getpassvaluesFunction · 0.85
Adam7_interlaceFunction · 0.85

Tested by

no test coverage detected