mirror of
https://git.code.sf.net/p/libpng/code.git
synced 2025-07-10 18:04:09 +02:00
[libpng16] Added example programs for the new 'simplified' API.
This commit is contained in:
parent
e1bb124baa
commit
36082cffcd
7
ANNOUNCE
7
ANNOUNCE
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
Libpng 1.6.0alpha01 - November 23, 2011
|
Libpng 1.6.0alpha01 - November 24, 2011
|
||||||
|
|
||||||
This is not intended to be a public release. It will be replaced
|
This is not intended to be a public release. It will be replaced
|
||||||
within a few weeks by a public version or by another test version.
|
within a few weeks by a public version or by another test version.
|
||||||
@ -99,7 +99,7 @@ Version 1.5.7beta04 [November 17, 2011]
|
|||||||
Also removed a duplicate setting of this flag.
|
Also removed a duplicate setting of this flag.
|
||||||
Added files that were omitted from the libpng-1.5.7beta03 zip distribution.
|
Added files that were omitted from the libpng-1.5.7beta03 zip distribution.
|
||||||
|
|
||||||
Version 1.5.7beta05 [November 23, 2011]
|
Version 1.5.7beta05 [(PENDING RELEASE)]
|
||||||
Removed "zTXt" from warning in generic chunk decompression function.
|
Removed "zTXt" from warning in generic chunk decompression function.
|
||||||
Validate time settings passed to pngset() and png_convert_to_rfc1123()
|
Validate time settings passed to pngset() and png_convert_to_rfc1123()
|
||||||
(Frank Busse).
|
(Frank Busse).
|
||||||
@ -107,7 +107,8 @@ Version 1.5.7beta05 [November 23, 2011]
|
|||||||
Added MINGW support to CMakeLists.txt
|
Added MINGW support to CMakeLists.txt
|
||||||
Reject invalid compression flag or method when reading the iTXt chunk.
|
Reject invalid compression flag or method when reading the iTXt chunk.
|
||||||
|
|
||||||
Version 1.6.0alpha01 [November 23, 2011]
|
Version 1.6.0alpha01 [November 24, 2011]
|
||||||
|
Added example programs for the new 'simplified' API.
|
||||||
|
|
||||||
Send comments/corrections/commendations to png-mng-implement at lists.sf.net:
|
Send comments/corrections/commendations to png-mng-implement at lists.sf.net:
|
||||||
(subscription required; visit
|
(subscription required; visit
|
||||||
|
|||||||
5
CHANGES
5
CHANGES
@ -3744,7 +3744,7 @@ Version 1.5.7beta04 [November 17, 2011]
|
|||||||
Also removed a duplicate setting of this flag.
|
Also removed a duplicate setting of this flag.
|
||||||
Added files that were omitted from the libpng-1.5.7beta03 zip distribution.
|
Added files that were omitted from the libpng-1.5.7beta03 zip distribution.
|
||||||
|
|
||||||
Version 1.5.7beta05 [November 23, 2011]
|
Version 1.5.7beta05 [(PENDING RELEASE)]
|
||||||
Removed "zTXt" from warning in generic chunk decompression function.
|
Removed "zTXt" from warning in generic chunk decompression function.
|
||||||
Validate time settings passed to pngset() and png_convert_to_rfc1123()
|
Validate time settings passed to pngset() and png_convert_to_rfc1123()
|
||||||
(Frank Busse).
|
(Frank Busse).
|
||||||
@ -3752,7 +3752,8 @@ Version 1.5.7beta05 [November 23, 2011]
|
|||||||
Added MINGW support to CMakeLists.txt
|
Added MINGW support to CMakeLists.txt
|
||||||
Reject invalid compression flag or method when reading the iTXt chunk.
|
Reject invalid compression flag or method when reading the iTXt chunk.
|
||||||
|
|
||||||
Version 1.6.0alpha01 [November 23, 2011]
|
Version 1.6.0alpha01 [November 24, 2011]
|
||||||
|
Added example programs for the new 'simplified' API.
|
||||||
|
|
||||||
Send comments/corrections/commendations to png-mng-implement at lists.sf.net
|
Send comments/corrections/commendations to png-mng-implement at lists.sf.net
|
||||||
(subscription required; visit
|
(subscription required; visit
|
||||||
|
|||||||
20
contrib/examples/README.txt
Normal file
20
contrib/examples/README.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
This directory contains examples of libpng usage.
|
||||||
|
|
||||||
|
NO COPYRIGHT IS CLAIMED TO ANY OF THE FILES IN THIS DIRECTORY.
|
||||||
|
They have been placed in the public domain by the authors.
|
||||||
|
|
||||||
|
The files may be used freely in any way. The intention is that appropriate
|
||||||
|
parts of the files be used in other libpng-using programs without any need for
|
||||||
|
the authors of the using code to seek copyright or license from the original
|
||||||
|
authors.
|
||||||
|
|
||||||
|
The source code and comments in this directory are the original work of the
|
||||||
|
people named below. No other person or organization has made contributions to
|
||||||
|
the work in this directory.
|
||||||
|
|
||||||
|
ORIGINAL AUTHORS
|
||||||
|
The following people have contributed to the code in this directory. None
|
||||||
|
of the people below claim any rights with regard to the contents of this
|
||||||
|
directory.
|
||||||
|
|
||||||
|
John Bowler <jbowler@acm.org>
|
||||||
178
contrib/examples/iccfrompng.c
Normal file
178
contrib/examples/iccfrompng.c
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
/*- iccfrompng
|
||||||
|
*
|
||||||
|
* COPYRIGHT: please read the file "README.txt" from the containing directory..
|
||||||
|
* This file has been placed in the public domain by the authors.
|
||||||
|
*
|
||||||
|
* Extract any icc profiles found in the given PNG files. This is a simple
|
||||||
|
* example of a program which extracts information from the header of a PNG file
|
||||||
|
* without processing the image. Notice that some header information may occur
|
||||||
|
* after the image data, textual data and comments are an example; the approach
|
||||||
|
* in this file won't work reliably for such data because it only looks for the
|
||||||
|
* information in the section of the file that preceeds the image data.
|
||||||
|
*
|
||||||
|
* Compile and link against libpng and zlib, plus anything else required on the
|
||||||
|
* system you use.
|
||||||
|
*
|
||||||
|
* To use supply a list of PNG files containing iCCP chunks, the chunks will be
|
||||||
|
* extracted to a similarly named file with the extension replaced by 'icc',
|
||||||
|
* which will be overwritten without warning.
|
||||||
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <png.h>
|
||||||
|
|
||||||
|
static int verbose = 1;
|
||||||
|
static png_byte no_profile[] = "no profile";
|
||||||
|
|
||||||
|
static png_bytep
|
||||||
|
extract(FILE *fp, png_uint_32 *proflen)
|
||||||
|
{
|
||||||
|
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
|
||||||
|
png_infop info_ptr = NULL;
|
||||||
|
png_bytep result = NULL;
|
||||||
|
|
||||||
|
/* Initialize for error or no profile: */
|
||||||
|
*proflen = 0;
|
||||||
|
|
||||||
|
if (png_ptr == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "iccfrompng: version library mismatch?\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setjmp(png_jmpbuf(png_ptr)))
|
||||||
|
{
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_init_io(png_ptr, fp);
|
||||||
|
|
||||||
|
info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
if (info_ptr == NULL)
|
||||||
|
png_error(png_ptr, "OOM allocating info structure");
|
||||||
|
|
||||||
|
png_read_info(png_ptr, info_ptr);
|
||||||
|
|
||||||
|
{
|
||||||
|
png_charp name;
|
||||||
|
int compression_type;
|
||||||
|
png_bytep profile;
|
||||||
|
|
||||||
|
if (png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile,
|
||||||
|
proflen) & PNG_INFO_iCCP)
|
||||||
|
{
|
||||||
|
result = malloc(*proflen);
|
||||||
|
if (result != NULL)
|
||||||
|
memcpy(result, profile, *proflen);
|
||||||
|
|
||||||
|
else
|
||||||
|
png_error(png_ptr, "OOM allocating profile buffer");
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
result = no_profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
extract_one_file(const char *filename)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
|
||||||
|
if (fp != NULL)
|
||||||
|
{
|
||||||
|
png_uint_32 proflen = 0;
|
||||||
|
png_bytep profile = extract(fp, &proflen);
|
||||||
|
|
||||||
|
if (profile != NULL && profile != no_profile)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
char *output;
|
||||||
|
|
||||||
|
{
|
||||||
|
const char *ep = strrchr(filename, '.');
|
||||||
|
|
||||||
|
if (ep != NULL)
|
||||||
|
len = ep-filename;
|
||||||
|
|
||||||
|
else
|
||||||
|
len = strlen(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
output = malloc(len + 5);
|
||||||
|
if (output != NULL)
|
||||||
|
{
|
||||||
|
FILE *of;
|
||||||
|
|
||||||
|
memcpy(output, filename, len);
|
||||||
|
strcpy(output+len, ".icc");
|
||||||
|
|
||||||
|
of = fopen(output, "wb");
|
||||||
|
if (of != NULL)
|
||||||
|
{
|
||||||
|
if (fwrite(profile, proflen, 1, of) == 1 &&
|
||||||
|
fflush(of) == 0 &&
|
||||||
|
fclose(of) == 0)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
printf("%s -> %s\n", filename, output);
|
||||||
|
/* Success return */
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: error writing profile\n", output);
|
||||||
|
if (remove(output))
|
||||||
|
fprintf(stderr, "%s: could not remove file\n", output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "%s: failed to open output file\n", output);
|
||||||
|
|
||||||
|
free(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "%s: OOM allocating string!\n", filename);
|
||||||
|
|
||||||
|
free(profile);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (verbose && profile == no_profile)
|
||||||
|
printf("%s has no profile\n", filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "%s: could not open file\n", filename);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int extracted = 0;
|
||||||
|
|
||||||
|
for (i=1; i<argc; ++i)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[i], "-q") == 0)
|
||||||
|
verbose = 0;
|
||||||
|
|
||||||
|
else if (extract_one_file(argv[i]))
|
||||||
|
extracted = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Exit code is true if any extract succeeds */
|
||||||
|
return extracted == 0;
|
||||||
|
}
|
||||||
365
contrib/examples/pngpixel.c
Normal file
365
contrib/examples/pngpixel.c
Normal file
@ -0,0 +1,365 @@
|
|||||||
|
/*- pngpixel
|
||||||
|
*
|
||||||
|
* COPYRIGHT: please read the file "README.txt" from the containing directory.
|
||||||
|
* This file has been placed in the public domain by the authors.
|
||||||
|
*
|
||||||
|
* Read a single pixel value from a PNG file.
|
||||||
|
*
|
||||||
|
* This code illustrates basic 'by-row' reading of a PNG file using libpng.
|
||||||
|
* Rows are read until a particular pixel is found, the value of this pixel is
|
||||||
|
* then printed on stdou.
|
||||||
|
*
|
||||||
|
* The code illustrates how to do this on interlaced as well as non-interlaced
|
||||||
|
* images. Normally you would call png_set_interlace_handling() to have libpng
|
||||||
|
* deal with the interlace for you, but that obliges you to buffer half of the
|
||||||
|
* image to assemble the interlaced rows. In this code
|
||||||
|
* png_set_interlace_handling() is not called and, instead, the code handles the
|
||||||
|
* interlace passes directly looking for the required pixel.
|
||||||
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <setjmp.h> /* required for error handling */
|
||||||
|
|
||||||
|
/* Normally use <png.h> here to get the installed libpng, but this is done to
|
||||||
|
* ensure the code picks up the local libpng implementation:
|
||||||
|
*/
|
||||||
|
#include "../../png.h"
|
||||||
|
|
||||||
|
/* Return component 'c' of pixel 'x' from the given row. */
|
||||||
|
static unsigned int
|
||||||
|
component(png_const_bytep row, png_uint_32 x, unsigned int c,
|
||||||
|
unsigned int bit_depth, unsigned int channels)
|
||||||
|
{
|
||||||
|
/* PNG images can be up to 2^31 pixels wide, but this means they can be up to
|
||||||
|
* 2^37 bits wide (for a 64-bit pixel - the largest possible) and hence 2^34
|
||||||
|
* bytes wide. Since the row fitted into memory, however, the following must
|
||||||
|
* work:
|
||||||
|
*/
|
||||||
|
png_uint_32 bit_offset_hi = bit_depth * ((x >> 6) * channels + c);
|
||||||
|
png_uint_32 bit_offset_lo = bit_depth * ((x & 0x3f) * channels + c);
|
||||||
|
|
||||||
|
row = (png_const_bytep)(((PNG_CONST png_byte (*)[8])row) + bit_offset_hi);
|
||||||
|
row += bit_offset_lo >> 3;
|
||||||
|
bit_offset_lo &= 0x07;
|
||||||
|
|
||||||
|
/* PNG pixels are packed into bytes to put the first pixel in the highest
|
||||||
|
* bits of the byte and into two bytes for 16-bit values with the high 8 bits
|
||||||
|
* first, so:
|
||||||
|
*/
|
||||||
|
switch (bit_depth)
|
||||||
|
{
|
||||||
|
case 1: return (row[0] >> (7-bit_offset_lo)) & 0x01;
|
||||||
|
case 2: return (row[0] >> (6-bit_offset_lo)) & 0x03;
|
||||||
|
case 4: return (row[0] >> (4-bit_offset_lo)) & 0x0f;
|
||||||
|
case 8: return row[0];
|
||||||
|
case 16: return (row[0] << 8) + row[1];
|
||||||
|
default:
|
||||||
|
/* This should never happen, it indicates a bug in this program or in
|
||||||
|
* libpng itself:
|
||||||
|
*/
|
||||||
|
fprintf(stderr, "pngpixel: invalid bit depth %u\n", bit_depth);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Print a pixel from a row returned by libpng; determine the row format, find
|
||||||
|
* the pixel, and print the relevant information to stdout.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
print_pixel(png_structp png_ptr, png_infop info_ptr, png_const_bytep row,
|
||||||
|
png_uint_32 x)
|
||||||
|
{
|
||||||
|
PNG_CONST unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||||
|
|
||||||
|
switch (png_get_color_type(png_ptr, info_ptr))
|
||||||
|
{
|
||||||
|
case PNG_COLOR_TYPE_GRAY:
|
||||||
|
printf("GRAY %u\n", component(row, x, 0, bit_depth, 1));
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* The palette case is slightly more difficult - the palette and, if
|
||||||
|
* present, the tRNS ('transparency', though the values are really
|
||||||
|
* opacity) data must be read to give the full picture:
|
||||||
|
*/
|
||||||
|
case PNG_COLOR_TYPE_PALETTE:
|
||||||
|
{
|
||||||
|
PNG_CONST unsigned int index = component(row, x, 0, bit_depth, 1);
|
||||||
|
png_colorp palette = NULL;
|
||||||
|
int num_palette = 0;
|
||||||
|
|
||||||
|
if ((png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) &
|
||||||
|
PNG_INFO_PLTE) && num_palette > 0 && palette != NULL)
|
||||||
|
{
|
||||||
|
png_bytep trans_alpha = NULL;
|
||||||
|
int num_trans = 0;
|
||||||
|
if ((png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans,
|
||||||
|
NULL) & PNG_INFO_tRNS) && num_trans > 0 &&
|
||||||
|
trans_alpha != NULL)
|
||||||
|
printf("INDEXED %u = %d %d %d %d\n", index,
|
||||||
|
palette[index].red, palette[index].green,
|
||||||
|
palette[index].blue,
|
||||||
|
index < num_trans ? trans_alpha[index] : 255);
|
||||||
|
|
||||||
|
else /* no transparency */
|
||||||
|
printf("INDEXED %u = %d %d %d\n", index,
|
||||||
|
palette[index].red, palette[index].green,
|
||||||
|
palette[index].blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
printf("INDEXED %u = invalid index\n", index);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PNG_COLOR_TYPE_RGB:
|
||||||
|
printf("RGB %u %u %u\n", component(row, x, 0, bit_depth, 3),
|
||||||
|
component(row, x, 1, bit_depth, 3),
|
||||||
|
component(row, x, 2, bit_depth, 3));
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
||||||
|
printf("GRAY+ALPHA %u %u\n", component(row, x, 0, bit_depth, 2),
|
||||||
|
component(row, x, 1, bit_depth, 2));
|
||||||
|
return;
|
||||||
|
|
||||||
|
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||||
|
printf("RGBA %u %u %u %u\n", component(row, x, 0, bit_depth, 4),
|
||||||
|
component(row, x, 1, bit_depth, 4),
|
||||||
|
component(row, x, 2, bit_depth, 4),
|
||||||
|
component(row, x, 3, bit_depth, 4));
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
png_error(png_ptr, "invalid color type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
/* This program uses the default, <setjmp.h> based, libpng error handling
|
||||||
|
* mechanism, therefore any local variable that exists before the call to
|
||||||
|
* setjmp and is changed after the call to setjmp returns successfully must
|
||||||
|
* be declared with 'volatile' to ensure that their values don't get
|
||||||
|
* destroyed by longjmp:
|
||||||
|
*/
|
||||||
|
volatile int result = 1/*fail*/;
|
||||||
|
|
||||||
|
if (argc == 4)
|
||||||
|
{
|
||||||
|
long x = atol(argv[1]);
|
||||||
|
long y = atol(argv[2]);
|
||||||
|
FILE *f = fopen(argv[3], "rb");
|
||||||
|
volatile png_bytep row = NULL;
|
||||||
|
|
||||||
|
if (f != NULL)
|
||||||
|
{
|
||||||
|
/* libpng requires a callback function for handling errors; this
|
||||||
|
* callback must not return. The default callback function uses a
|
||||||
|
* stored <setjmp.h> style jmp_buf which is held in a png_struct and
|
||||||
|
* writes error messages to stderr. Creating the png_struct is a
|
||||||
|
* little tricky; just copy the following code.
|
||||||
|
*/
|
||||||
|
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
||||||
|
NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (png_ptr != NULL)
|
||||||
|
{
|
||||||
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
|
||||||
|
if (info_ptr != NULL)
|
||||||
|
{
|
||||||
|
/* Declare stack variables to hold pointers to locally allocated
|
||||||
|
* data.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Initialize the error control buffer: */
|
||||||
|
if (setjmp(png_jmpbuf(png_ptr)) == 0)
|
||||||
|
{
|
||||||
|
png_uint_32 width, height;
|
||||||
|
int bit_depth, color_type, interlace_method,
|
||||||
|
compression_method, filter_method;
|
||||||
|
png_bytep row_tmp;
|
||||||
|
|
||||||
|
/* Now associate the recently opened (FILE*) with the default
|
||||||
|
* libpng initialization functions. Sometimes libpng is
|
||||||
|
* compiled without stdio support (it can be difficult to do
|
||||||
|
* in some environments); in that case you will have to write
|
||||||
|
* your own read callback to read data from the (FILE*).
|
||||||
|
*/
|
||||||
|
png_init_io(png_ptr, f);
|
||||||
|
|
||||||
|
/* And read the first part of the PNG file - the header and
|
||||||
|
* all the information up to the first pixel.
|
||||||
|
*/
|
||||||
|
png_read_info(png_ptr, info_ptr);
|
||||||
|
|
||||||
|
/* This fills in enough information to tell us the width of
|
||||||
|
* each row in bytes, allocate the appropriate amount of
|
||||||
|
* space. In this case png_malloc is used - it will not
|
||||||
|
* return if memory isn't available.
|
||||||
|
*/
|
||||||
|
row = png_malloc(png_ptr, png_get_rowbytes(png_ptr,
|
||||||
|
info_ptr));
|
||||||
|
|
||||||
|
/* To avoid the overhead of using a volatile auto copy row_tmp
|
||||||
|
* to a local here - just use row for the png_free below.
|
||||||
|
*/
|
||||||
|
row_tmp = row;
|
||||||
|
|
||||||
|
/* All the information we need is in the header is returned by
|
||||||
|
* png_get_IHDR, if this fails we can now use 'png_error' to
|
||||||
|
* signal the error and return control to the setjmp above.
|
||||||
|
*/
|
||||||
|
if (png_get_IHDR(png_ptr, info_ptr, &width, &height,
|
||||||
|
&bit_depth, &color_type, &interlace_method,
|
||||||
|
&compression_method, &filter_method))
|
||||||
|
{
|
||||||
|
int passes, pass;
|
||||||
|
|
||||||
|
/* png_set_interlace_handling returns the number of
|
||||||
|
* passes required as well as turning on libpng's
|
||||||
|
* handling, but since we do it ourselves this is
|
||||||
|
* necessary:
|
||||||
|
*/
|
||||||
|
switch (interlace_method)
|
||||||
|
{
|
||||||
|
case PNG_INTERLACE_NONE:
|
||||||
|
passes = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PNG_INTERLACE_ADAM7:
|
||||||
|
passes = PNG_INTERLACE_ADAM7_PASSES;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
png_error(png_ptr, "pngpixel: unknown interlace");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now read the pixels, pass-by-pass, row-by-row: */
|
||||||
|
png_start_read_image(png_ptr);
|
||||||
|
|
||||||
|
for (pass=0; pass<passes; ++pass)
|
||||||
|
{
|
||||||
|
png_uint_32 ystart, xstart, ystep, xstep;
|
||||||
|
png_uint_32 py;
|
||||||
|
|
||||||
|
if (interlace_method == PNG_INTERLACE_ADAM7)
|
||||||
|
{
|
||||||
|
/* Sometimes the whole pass is empty because the
|
||||||
|
* image is too narrow or too short. libpng
|
||||||
|
* expects to be called for each row that is
|
||||||
|
* present in the pass, so it may be necessary to
|
||||||
|
* skip the loop below (over py) if the image is
|
||||||
|
* too narrow.
|
||||||
|
*/
|
||||||
|
if (PNG_PASS_COLS(width, pass) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* We need the starting pixel and the offset
|
||||||
|
* between each pixel in this pass; use the macros
|
||||||
|
* in png.h:
|
||||||
|
*/
|
||||||
|
xstart = PNG_PASS_START_COL(pass);
|
||||||
|
ystart = PNG_PASS_START_ROW(pass);
|
||||||
|
xstep = PNG_PASS_COL_OFFSET(pass);
|
||||||
|
ystep = PNG_PASS_ROW_OFFSET(pass);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ystart = xstart = 0;
|
||||||
|
ystep = xstep = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* To find the pixel loop over 'py' for each pass
|
||||||
|
* reading a row and then checking to see if it
|
||||||
|
* contains the pixel.
|
||||||
|
*/
|
||||||
|
for (py = ystart; py < height; py += ystep)
|
||||||
|
{
|
||||||
|
png_uint_32 px, ppx;
|
||||||
|
|
||||||
|
/* png_read_row takes two pointers. When libpng
|
||||||
|
* handles the interlace the first is filled in
|
||||||
|
* pixel-by-pixel, the second receives the same
|
||||||
|
* pixels but they are replicated across the
|
||||||
|
* unwritten pixels so far for each pass. When we
|
||||||
|
* do the interlace, however, they just contain
|
||||||
|
* the pixels from the interlace pass - giving
|
||||||
|
* both is wasteful and pointless.
|
||||||
|
*/
|
||||||
|
png_read_row(png_ptr, row_tmp, NULL);
|
||||||
|
|
||||||
|
/* Now find the pixel if it is in this row; there
|
||||||
|
* are, of course, much better ways of doing this
|
||||||
|
* than using a for loop:
|
||||||
|
*/
|
||||||
|
if (y == py) for (px = xstart, ppx = 0;
|
||||||
|
px < width; px += xstep, ++ppx) if (x == px)
|
||||||
|
{
|
||||||
|
/* 'ppx' is the index of the pixel in the row
|
||||||
|
* buffer.
|
||||||
|
*/
|
||||||
|
print_pixel(png_ptr, info_ptr, row_tmp, ppx);
|
||||||
|
|
||||||
|
/* Now terminate the loops early - we have
|
||||||
|
* found and handled the required data.
|
||||||
|
*/
|
||||||
|
goto pass_loop_end;
|
||||||
|
} /* x loop */
|
||||||
|
} /* y loop */
|
||||||
|
} /* pass loop */
|
||||||
|
|
||||||
|
/* Finally free the temporary buffer: */
|
||||||
|
pass_loop_end:
|
||||||
|
row = NULL;
|
||||||
|
png_free(png_ptr, row_tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
png_error(png_ptr, "pngpixel: png_get_IHDR failed");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Else libpng has raised an error. An error message has
|
||||||
|
* already been output, it is only necessary to clean up
|
||||||
|
* locally allocated data:
|
||||||
|
*/
|
||||||
|
if (row != NULL)
|
||||||
|
{
|
||||||
|
/* The default implementation of png_free never errors out
|
||||||
|
* (it just crashes if something goes wrong), but the safe
|
||||||
|
* way of using it is still to clear 'row' before calling
|
||||||
|
* png_free:
|
||||||
|
*/
|
||||||
|
png_bytep row_tmp = row;
|
||||||
|
row = NULL;
|
||||||
|
png_free(png_ptr, row_tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
png_destroy_info_struct(png_ptr, &info_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngpixel: out of memory allocating png_info\n");
|
||||||
|
|
||||||
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngpixel: out of memory allocating png_struct\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngpixel: %s: could not open file\n", argv[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
/* Wrong number of arguments */
|
||||||
|
fprintf(stderr, "pngpixel: usage: pngpixel x y png-file\n");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
73
contrib/examples/pngtopng.c
Normal file
73
contrib/examples/pngtopng.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*- pngtopng
|
||||||
|
*
|
||||||
|
* COPYRIGHT: please read the file "README.txt" from the containing directory.
|
||||||
|
* This file has been placed in the public domain by the authors.
|
||||||
|
*
|
||||||
|
* Read a PNG and write it out in a fixed format
|
||||||
|
*
|
||||||
|
* This sample code is just the code from the top of 'example.c' with some error
|
||||||
|
* handling added. See example.c for more comments.
|
||||||
|
*/
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Normally use <png.h> here to get the installed libpng, but this is done to
|
||||||
|
* ensure the code picks up the local libpng implementation:
|
||||||
|
*/
|
||||||
|
#include "../../png.h"
|
||||||
|
|
||||||
|
int main(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
int result = 1;
|
||||||
|
|
||||||
|
if (argc == 3)
|
||||||
|
{
|
||||||
|
png_image image;
|
||||||
|
|
||||||
|
memset(&image, 0, sizeof image);
|
||||||
|
|
||||||
|
if (png_image_begin_read_from_file(&image, argv[1]))
|
||||||
|
{
|
||||||
|
png_bytep buffer;
|
||||||
|
|
||||||
|
/* Change this to try different formats! */
|
||||||
|
image.format = PNG_FORMAT_RGBA;
|
||||||
|
|
||||||
|
buffer = malloc(PNG_IMAGE_SIZE(image));
|
||||||
|
|
||||||
|
if (buffer != NULL)
|
||||||
|
{
|
||||||
|
if (png_image_finish_read(&image, NULL/*background*/, buffer,
|
||||||
|
0/*row_stride*/))
|
||||||
|
{
|
||||||
|
if (png_image_write_to_file(&image, argv[2],
|
||||||
|
0/*convert_to_8bit*/, buffer, 0/*row_stride*/))
|
||||||
|
result = 0;
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
|
||||||
|
image.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngtopng: read %s: %s\n", argv[1], image.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
|
||||||
|
(unsigned long)PNG_IMAGE_SIZE(image));
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
/* Failed to read the first argument: */
|
||||||
|
fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
/* Wrong number of arguments */
|
||||||
|
fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user