read the information from the header and store it in the LodePNGInfo. return value is error*/
| 3949 | |
| 3950 | /*read the information from the header and store it in the LodePNGInfo. return value is error*/ |
| 3951 | unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, |
| 3952 | const unsigned char* in, size_t insize) |
| 3953 | { |
| 3954 | LodePNGInfo* info = &state->info_png; |
| 3955 | if(insize == 0 || in == 0) |
| 3956 | { |
| 3957 | CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/ |
| 3958 | } |
| 3959 | if(insize < 29) |
| 3960 | { |
| 3961 | CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/ |
| 3962 | } |
| 3963 | |
| 3964 | /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/ |
| 3965 | lodepng_info_cleanup(info); |
| 3966 | lodepng_info_init(info); |
| 3967 | |
| 3968 | if(in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 |
| 3969 | || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) |
| 3970 | { |
| 3971 | CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/ |
| 3972 | } |
| 3973 | if(in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') |
| 3974 | { |
| 3975 | CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/ |
| 3976 | } |
| 3977 | |
| 3978 | /*read the values given in the header*/ |
| 3979 | *w = lodepng_read32bitInt(&in[16]); |
| 3980 | *h = lodepng_read32bitInt(&in[20]); |
| 3981 | info->color.bitdepth = in[24]; |
| 3982 | info->color.colortype = (LodePNGColorType)in[25]; |
| 3983 | info->compression_method = in[26]; |
| 3984 | info->filter_method = in[27]; |
| 3985 | info->interlace_method = in[28]; |
| 3986 | |
| 3987 | if(!state->decoder.ignore_crc) |
| 3988 | { |
| 3989 | unsigned CRC = lodepng_read32bitInt(&in[29]); |
| 3990 | unsigned checksum = lodepng_crc32(&in[12], 17); |
| 3991 | if(CRC != checksum) |
| 3992 | { |
| 3993 | CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/ |
| 3994 | } |
| 3995 | } |
| 3996 | |
| 3997 | /*error: only compression method 0 is allowed in the specification*/ |
| 3998 | if(info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32); |
| 3999 | /*error: only filter method 0 is allowed in the specification*/ |
| 4000 | if(info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33); |
| 4001 | /*error: only interlace methods 0 and 1 exist in the specification*/ |
| 4002 | if(info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34); |
| 4003 | |
| 4004 | state->error = checkColorValidity(info->color.colortype, info->color.bitdepth); |
| 4005 | return state->error; |
| 4006 | } |
| 4007 | |
| 4008 | static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, |
no test coverage detected