* 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
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
* 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)
{
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.
#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 */
* 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)
{
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
* 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)
{
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.
*/
text_ptr[2].key = "Description";
text_ptr[2].text = "<long 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
* 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 */
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 */