void input_reader(png_structp png_ptr, png_bytep data, png_size_t length) { InputStream *istr = (InputStream*)png_get_io_ptr(png_ptr); int ct = istr->ReadChars(length, data); if(ct != length) png_error(png_ptr, "unexpected EOF"); }
// Gif helper function - loads the given number of bits from the file. File format: byte indicating // block length, followed by a block of that length. All blocks are logically concatenated until // a block of length 0 is reached. static int LoadGifBits(InputStream input, unsigned char *buffer, int num_bits, int *buffer_pos, int *buffer_end) { if (*buffer_pos + num_bits >= *buffer_end) { if (*buffer_end >= 16) buffer[0] = buffer[*buffer_end/8 - 2]; if (*buffer_end >= 8) buffer[1] = buffer[*buffer_end/8 - 1]; unsigned char block_size; if (!input.ReadChars(1, &block_size) || block_size == 0) return -1; if (input.ReadChars(block_size, buffer + 2) < block_size) return -1; *buffer_pos = 16 - (*buffer_end - *buffer_pos); *buffer_end = 8 * (int(block_size) + 2); } unsigned int value = 0; for (int i = *buffer_pos, j = 0; j < num_bits; i++, j++) value |= (((buffer[i / 8] & (1 << (i % 8))) > 0) << j); *buffer_pos += num_bits; return value; }