X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1f0299c17dc878540bf190c290392db241b425ad..c18ac572dba92039e133a42a5412b4541a315edd:/src/png/example.c diff --git a/src/png/example.c b/src/png/example.c index 4b22852ee2..088aad3a76 100644 --- a/src/png/example.c +++ b/src/png/example.c @@ -13,10 +13,13 @@ * working PNG reader/writer, see pngtest.c, included in this distribution. */ -#include "../png/png.h" +#include "png.h" -/* Check to see if a file is a PNG file using png_sig_cmp(). Returns - * non-zero if the image is a PNG, and 0 if it isn't a PNG. +/* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp() + * returns zero if the image is a PNG and nonzero if it isn't a PNG. + * + * The function check_if_png() shown here, but not used, returns nonzero (true) + * if the file can be opened and is a PNG, 0 (false) otherwise. * * If this call is successful, and you are going to keep the file open, * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once @@ -41,12 +44,14 @@ int check_if_png(char *file_name, FILE **fp) if ((*fp = fopen(file_name, "rb")) != NULL); return 0; - /* Read in the signature bytes */ + /* Read in some of the signature bytes */ if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0; - /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */ - return(png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); + /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. + Return nonzero (true) if they match */ + + return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); } /* Read a PNG file. You may want to return an error code if the read @@ -83,7 +88,7 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ * was compiled with a compatible version of the library. REQUIRED */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, - (void *)user_error_ptr, user_error_fn, user_warning_fn); + png_voidp user_error_ptr, user_error_fn, user_warning_fn); if (png_ptr == NULL) { @@ -220,11 +225,14 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ if (png_get_sRGB(png_ptr, info_ptr, &intent)) png_set_sRGB(png_ptr, intent, 0); - else + else + { + double image_gamma; if (png_get_gAMA(png_ptr, info_ptr, &image_gamma)) png_set_gamma(png_ptr, screen_gamma, image_gamma); else - png_set_gamma(png_ptr, screen_gamma, 0.50); + png_set_gamma(png_ptr, screen_gamma, 0.45455); + } /* Dither RGB files down to 8 bit palette or reduce palettes * to the number of colors available on your screen. @@ -316,21 +324,21 @@ void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ #ifdef single /* Read the image a single row at a time */ for (y = 0; y < height; y++) { - png_bytep row_pointers = row[y]; - png_read_rows(png_ptr, &row_pointers, NULL, 1); + png_read_rows(png_ptr, &row_pointers[y], NULL, 1); } #else no_single /* Read the image several rows at a time */ for (y = 0; y < height; y += number_of_rows) { #ifdef sparkle /* Read the image using the "sparkle" effect. */ - png_read_rows(png_ptr, row_pointers, NULL, number_of_rows); - + png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows); + + png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows); #else no_sparkle /* Read the image using the "rectangle" effect */ - png_read_rows(png_ptr, NULL, row_pointers, number_of_rows); + png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows); #endif no_sparkle /* use only one of these two methods */ } - + /* if you want to display the image after every pass, do so here */ #endif no_single /* use only one of these two methods */ @@ -362,7 +370,7 @@ initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) * linked libraries. */ *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, - (void *)user_error_ptr, user_error_fn, user_warning_fn); + png_voidp user_error_ptr, user_error_fn, user_warning_fn); if (*png_ptr == NULL) { @@ -384,8 +392,11 @@ initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) return ERROR; } - /* this one's new. You will need to provide all three + /* This one's new. You will need to provide all three * function callbacks, even if you aren't using them all. + * If you aren't using all functions, you can specify NULL + * parameters. Even when all three functions are NULL, + * you need to call png_set_progressive_read_fn(). * These functions shouldn't be dependent on global or * static variables if you are decoding several images * simultaneously. You should store stream specific data @@ -498,7 +509,7 @@ void write_png(char *file_name /* , ... other image information ... */) * in case we are using dynamically linked libraries. REQUIRED. */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, - (void *)user_error_ptr, user_error_fn, user_warning_fn); + png_voidp user_error_ptr, user_error_fn, user_warning_fn); if (png_ptr == NULL) { @@ -565,7 +576,7 @@ void write_png(char *file_name /* , ... other image information ... */) sig_bit.alpha = true_alpha_bit_depth; png_set_sBIT(png_ptr, info_ptr, sig_bit); - + /* Optional gamma chunk is strongly suggested if you have any guess * as to the correct gamma of the image. */ @@ -581,7 +592,7 @@ void write_png(char *file_name /* , ... other image information ... */) text_ptr[2].key = "Description"; text_ptr[2].text = ""; text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; - png_set_text(png_ptr, info_ptr, text_ptr, 2); + png_set_text(png_ptr, info_ptr, text_ptr, 3); /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */ /* note that if sRGB is present the cHRM chunk must be ignored @@ -638,7 +649,11 @@ void write_png(char *file_name /* , ... other image information ... */) * layout, however, so choose what fits your needs best). You need to * use the first method if you aren't handling interlacing yourself. */ - png_byte row_pointers[height][width]; + png_uint_32 k, height, width; + png_byte image[height][width]; + png_bytep row_pointers[height]; + for (k = 0; k < height; k++) + row_pointers[k] = image + k*width; /* One of the following output methods is REQUIRED */ #ifdef entire /* write out the entire image data in one call */ @@ -653,13 +668,12 @@ void write_png(char *file_name /* , ... other image information ... */) for (pass = 0; pass < number_passes; pass++) { /* Write a few rows at a time. */ - png_write_rows(png_ptr, row_pointers, number_of_rows); + png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows); /* If you are only writing one row at a time, this works */ for (y = 0; y < height; y++) { - png_bytep row_pointers = row[y]; - png_write_rows(png_ptr, &row_pointers, 1); + png_write_rows(png_ptr, &row_pointers[y], 1); } } #endif no_entire /* use only one output method */