The PNG Guide is an eBook based on Greg Roelofs' book, originally published by O'Reilly. |
Home Reading PNG Images Progressively readpng2_decode_data() | |
readpng2_decode_data()if (user_did_not_specify_a_background_color_or_pattern) rpng2_info.need_bgcolor = TRUE; rpng2_info.mainprog_init = rpng2_x_init; rpng2_info.mainprog_display_row = rpng2_x_display_row; rpng2_info.mainprog_finish_display = rpng2_x_finish_display; Unlike the basic viewer, where the main program called a special function to check for and retrieve the image's background color, the progressive viewer simply sets the need_bgcolor flag in the struct. It also sets three function pointers corresponding to the three readpng2 callbacks. The reason for this apparent duplication will become clear when we look at the callbacks in detail. Having prepared everything for decoding, the main program begins the data loop that is at its core, reading file data into a buffer and passing it to the PNG-decoding function: for (;;) { if (readpng2_decode_data(&rpng2_info, inbuf, incount)) ++error; if (error || feof(infile) || rpng2_info.done) break; if (timing) sleep(1); incount = fread(inbuf, 1, INBUFSIZE, infile); } Note the call to readpng2_decode_data() at the beginning of the loop, before fread(); it handles the initial chunk of data we read prior to calling readpng2_init(). int readpng2_decode_data(mainprog_info *mainprog_ptr, uch *rawbuf, ulg length) { png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr; png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr; if (setjmp(mainprog_ptr->jmpbuf)) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); mainprog_ptr->png_ptr = NULL; mainprog_ptr->info_ptr = NULL; return 2; } png_process_data(png_ptr, info_ptr, rawbuf, length); return 0; } The struct pointers are copied merely because the alternative is to typedef
them; the latter may be more efficient (though not necessarily, due to the
extra level of indirection inherent in the -> operator), but it
is also uglier and makes the code somewhat less readable.[101]
|
|
Home Reading PNG Images Progressively readpng2_decode_data() |