提交 0d580582 编写于 作者: G Guy Schalnat 提交者: Glenn Randers-Pehrson

Imported from libpng-0.71.tar

上级
/* Copyright (C) 1989, 1991, 1993 Aladdin Enterprises. All rights reserved. */
/* ansi2knr.c */
/* Convert ANSI function declarations to K&R syntax */
/*
ansi2knr is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
ansi2knr, but only under the conditions described in the GNU
General Public License. A copy of this license is supposed to have been
given to you along with ansi2knr so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies.
*/
/*
---------- Here is the GNU GPL file COPYING, referred to above ----------
----- These terms do NOT apply to the JPEG software itself; see README ------
GHOSTSCRIPT GENERAL PUBLIC LICENSE
(Clarified 11 Feb 1988)
Copyright (C) 1988 Richard M. Stallman
Everyone is permitted to copy and distribute verbatim copies of this
license, but changing it is not allowed. You can also use this wording
to make the terms for other programs.
The license agreements of most software companies keep you at the
mercy of those companies. By contrast, our general public license is
intended to give everyone the right to share Ghostscript. To make sure
that you get the rights we want you to have, we need to make
restrictions that forbid anyone to deny you these rights or to ask you
to surrender the rights. Hence this license agreement.
Specifically, we want to make sure that you have the right to give
away copies of Ghostscript, that you receive source code or else can get
it if you want it, that you can change Ghostscript or use pieces of it
in new free programs, and that you know you can do these things.
To make sure that everyone has such rights, we have to forbid you to
deprive anyone else of these rights. For example, if you distribute
copies of Ghostscript, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must tell them their rights.
Also, for our own protection, we must make certain that everyone finds
out that there is no warranty for Ghostscript. If Ghostscript is
modified by someone else and passed on, we want its recipients to know
that what they have is not what we distributed, so that any problems
introduced by others will not reflect on our reputation.
Therefore we (Richard M. Stallman and the Free Software Foundation,
Inc.) make the following terms which say what you must do to be allowed
to distribute or change Ghostscript.
COPYING POLICIES
1. You may copy and distribute verbatim copies of Ghostscript source
code as you receive it, in any medium, provided that you conspicuously
and appropriately publish on each copy a valid copyright and license
notice "Copyright (C) 1989 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc." (or with whatever year is
appropriate); keep intact the notices on all files that refer to this
License Agreement and to the absence of any warranty; and give any other
recipients of the Ghostscript program a copy of this License Agreement
along with the program. You may charge a distribution fee for the
physical act of transferring a copy.
2. You may modify your copy or copies of Ghostscript or any portion of
it, and copy and distribute such modifications under the terms of
Paragraph 1 above, provided that you also do the following:
a) cause the modified files to carry prominent notices stating
that you changed the files and the date of any change; and
b) cause the whole of any work that you distribute or publish,
that in whole or in part contains or is a derivative of Ghostscript
or any part thereof, to be licensed at no charge to all third
parties on terms identical to those contained in this License
Agreement (except that you may choose to grant more extensive
warranty protection to some or all third parties, at your option).
c) You may charge a distribution fee for the physical act of
transferring a copy, and you may at your option offer warranty
protection in exchange for a fee.
Mere aggregation of another unrelated program with this program (or its
derivative) on a volume of a storage or distribution medium does not bring
the other program under the scope of these terms.
3. You may copy and distribute Ghostscript (or a portion or derivative
of it, under Paragraph 2) in object code or executable form under the
terms of Paragraphs 1 and 2 above provided that you also do one of the
following:
a) accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of
Paragraphs 1 and 2 above; or,
b) accompany it with a written offer, valid for at least three
years, to give any third party free (except for a nominal
shipping charge) a complete machine-readable copy of the
corresponding source code, to be distributed under the terms of
Paragraphs 1 and 2 above; or,
c) accompany it with the information you received as to where the
corresponding source code may be obtained. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form alone.)
For an executable file, complete source code means all the source code for
all modules it contains; but, as a special exception, it need not include
source code for modules which are standard libraries that accompany the
operating system on which the executable file runs.
4. You may not copy, sublicense, distribute or transfer Ghostscript
except as expressly provided under this License Agreement. Any attempt
otherwise to copy, sublicense, distribute or transfer Ghostscript is
void and your rights to use the program under this License agreement
shall be automatically terminated. However, parties who have received
computer software programs from you with this License Agreement will not
have their licenses terminated so long as such parties remain in full
compliance.
5. If you wish to incorporate parts of Ghostscript into other free
programs whose distribution conditions are different, write to the Free
Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not
yet worked out a simple rule that can be stated here, but we will often
permit this. We will be guided by the two goals of preserving the free
status of all derivatives of our free software and of promoting the
sharing and reuse of software.
Your comments and suggestions about our licensing policies and our
software are welcome! Please contact the Free Software Foundation,
Inc., 675 Mass Ave, Cambridge, MA 02139, or call (617) 876-3296.
NO WARRANTY
BECAUSE GHOSTSCRIPT IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC, RICHARD
M. STALLMAN, ALADDIN ENTERPRISES, L. PETER DEUTSCH, AND/OR OTHER PARTIES
PROVIDE GHOSTSCRIPT "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF GHOSTSCRIPT IS WITH
YOU. SHOULD GHOSTSCRIPT PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., L. PETER DEUTSCH, ALADDIN
ENTERPRISES, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE
GHOSTSCRIPT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
BY ANY OTHER PARTY.
-------------------- End of file COPYING ------------------------------
*/
#include <stdio.h>
#include <ctype.h>
#ifdef BSD
#include <strings.h>
#else
#ifdef VMS
extern int strlen(), strncmp();
#else
#include <string.h>
#endif
#endif
/* malloc and free should be declared in stdlib.h, */
/* but if you've got a K&R compiler, they probably aren't. */
#ifdef MSDOS
#include <malloc.h>
#else
#ifdef VMS
extern char *malloc();
extern void free();
#else
extern char *malloc();
extern int free();
#endif
#endif
/* Usage:
ansi2knr input_file [output_file]
* If no output_file is supplied, output goes to stdout.
* There are no error messages.
*
* ansi2knr recognizes functions by seeing a non-keyword identifier
* at the left margin, followed by a left parenthesis,
* with a right parenthesis as the last character on the line.
* It will recognize a multi-line header provided that the last character
* of the last line of the header is a right parenthesis,
* and no intervening line ends with a left brace or a semicolon.
* These algorithms ignore whitespace and comments, except that
* the function name must be the first thing on the line.
* The following constructs will confuse it:
* - Any other construct that starts at the left margin and
* follows the above syntax (such as a macro or function call).
* - Macros that tinker with the syntax of the function header.
*/
/* Scanning macros */
#define isidchar(ch) (isalnum(ch) || (ch) == '_')
#define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
/* Forward references */
char *skipspace();
int writeblanks();
int test1();
int convert1();
/* The main program */
main(argc, argv)
int argc;
char *argv[];
{ FILE *in, *out;
#define bufsize 5000 /* arbitrary size */
char *buf;
char *line;
switch ( argc )
{
default:
printf("Usage: ansi2knr input_file [output_file]\n");
exit(0);
case 2:
out = stdout; break;
case 3:
out = fopen(argv[2], "w");
if ( out == NULL )
{ fprintf(stderr, "Cannot open %s\n", argv[2]);
exit(1);
}
}
in = fopen(argv[1], "r");
if ( in == NULL )
{ fprintf(stderr, "Cannot open %s\n", argv[1]);
exit(1);
}
fprintf(out, "#line 1 \"%s\"\n", argv[1]);
buf = malloc(bufsize);
line = buf;
while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
{ switch ( test1(buf) )
{
case 1: /* a function */
convert1(buf, out);
break;
case -1: /* maybe the start of a function */
line = buf + strlen(buf);
if ( line != buf + (bufsize - 1) ) /* overflow check */
continue;
/* falls through */
default: /* not a function */
fputs(buf, out);
break;
}
line = buf;
}
if ( line != buf ) fputs(buf, out);
free(buf);
fclose(out);
fclose(in);
return 0;
}
/* Skip over space and comments, in either direction. */
char *
skipspace(p, dir)
register char *p;
register int dir; /* 1 for forward, -1 for backward */
{ for ( ; ; )
{ while ( isspace(*p) ) p += dir;
if ( !(*p == '/' && p[dir] == '*') ) break;
p += dir; p += dir;
while ( !(*p == '*' && p[dir] == '/') )
{ if ( *p == 0 ) return p; /* multi-line comment?? */
p += dir;
}
p += dir; p += dir;
}
return p;
}
/*
* Write blanks over part of a string.
*/
int
writeblanks(start, end)
char *start;
char *end;
{ char *p;
for ( p = start; p < end; p++ ) *p = ' ';
return 0;
}
/*
* Test whether the string in buf is a function definition.
* The string may contain and/or end with a newline.
* Return as follows:
* 0 - definitely not a function definition;
* 1 - definitely a function definition;
* -1 - may be the beginning of a function definition,
* append another line and look again.
*/
int
test1(buf)
char *buf;
{ register char *p = buf;
char *bend;
char *endfn;
int contin;
if ( !isidfirstchar(*p) )
return 0; /* no name at left margin */
bend = skipspace(buf + strlen(buf) - 1, -1);
switch ( *bend )
{
case ')': contin = 1; break;
case '{':
case ';': return 0; /* not a function */
default: contin = -1;
}
while ( isidchar(*p) ) p++;
endfn = p;
p = skipspace(p, 1);
if ( *p++ != '(' )
return 0; /* not a function */
p = skipspace(p, 1);
if ( *p == ')' )
return 0; /* no parameters */
/* Check that the apparent function name isn't a keyword. */
/* We only need to check for keywords that could be followed */
/* by a left parenthesis (which, unfortunately, is most of them). */
{ static char *words[] =
{ "asm", "auto", "case", "char", "const", "double",
"extern", "float", "for", "if", "int", "long",
"register", "return", "short", "signed", "sizeof",
"static", "switch", "typedef", "unsigned",
"void", "volatile", "while", 0
};
char **key = words;
char *kp;
int len = endfn - buf;
while ( (kp = *key) != 0 )
{ if ( strlen(kp) == len && !strncmp(kp, buf, len) )
return 0; /* name is a keyword */
key++;
}
}
return contin;
}
int
convert1(buf, out)
char *buf;
FILE *out;
{ char *endfn;
register char *p;
char **breaks;
unsigned num_breaks = 2; /* for testing */
char **btop;
char **bp;
char **ap;
/* Pre-ANSI implementations don't agree on whether strchr */
/* is called strchr or index, so we open-code it here. */
for ( endfn = buf; *(endfn++) != '('; ) ;
top: p = endfn;
breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
if ( breaks == 0 )
{ /* Couldn't allocate break table, give up */
fprintf(stderr, "Unable to allocate break table!\n");
fputs(buf, out);
return -1;
}
btop = breaks + num_breaks * 2 - 2;
bp = breaks;
/* Parse the argument list */
do
{ int level = 0;
char *end = NULL;
if ( bp >= btop )
{ /* Filled up break table. */
/* Allocate a bigger one and start over. */
free((char *)breaks);
num_breaks <<= 1;
goto top;
}
*bp++ = p;
/* Find the end of the argument */
for ( ; end == NULL; p++ )
{ switch(*p)
{
case ',': if ( !level ) end = p; break;
case '(': level++; break;
case ')': if ( --level < 0 ) end = p; break;
case '/': p = skipspace(p, 1) - 1; break;
default: ;
}
}
p--; /* back up over terminator */
/* Find the name being declared. */
/* This is complicated because of procedure and */
/* array modifiers. */
for ( ; ; )
{ p = skipspace(p - 1, -1);
switch ( *p )
{
case ']': /* skip array dimension(s) */
case ')': /* skip procedure args OR name */
{ int level = 1;
while ( level )
switch ( *--p )
{
case ']': case ')': level++; break;
case '[': case '(': level--; break;
case '/': p = skipspace(p, -1) + 1; break;
default: ;
}
}
if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
{ /* We found the name being declared */
while ( !isidfirstchar(*p) )
p = skipspace(p, 1) + 1;
goto found;
}
break;
default: goto found;
}
}
found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
{ p++;
if ( bp == breaks + 1 ) /* sole argument */
writeblanks(breaks[0], p);
else
writeblanks(bp[-1] - 1, p);
bp--;
}
else
{ while ( isidchar(*p) ) p--;
*bp++ = p+1;
}
p = end;
}
while ( *p++ == ',' );
*bp = p;
/* Make a special check for 'void' arglist */
if ( bp == breaks+2 )
{ p = skipspace(breaks[0], 1);
if ( !strncmp(p, "void", 4) )
{ p = skipspace(p+4, 1);
if ( p == breaks[2] - 1 )
{ bp = breaks; /* yup, pretend arglist is empty */
writeblanks(breaks[0], p + 1);
}
}
}
/* Put out the function name */
p = buf;
while ( p != endfn ) putc(*p, out), p++;
/* Put out the declaration */
for ( ap = breaks+1; ap < bp; ap += 2 )
{ p = *ap;
while ( isidchar(*p) ) putc(*p, out), p++;
if ( ap < bp - 1 ) fputs(", ", out);
}
fputs(") ", out);
/* Put out the argument declarations */
for ( ap = breaks+2; ap <= bp; ap += 2 ) (*ap)[-1] = ';';
fputs(breaks[0], out);
free((char *)breaks);
return 0;
}
/* example.c - an example of using libpng */
/* this is an example of how to use libpng to read and write
png files. The file libpng.txt is much more verbose then
this. If you have not read it, do so first. This was
designed to be a starting point of an implementation.
This is not officially part of libpng, and therefore
does not require a copyright notice.
*/
#include <png.h>
/* check to see if a file is a png file using png_check_sig() */
int check_png(char *file_name)
{
FILE *fp;
char buf[8];
int ret;
fp = fopen(file_name, "rb");
if (!fp)
return 0;
ret = fread(buf, 1, 8, fp);
fclose(fp);
if (ret != 8)
return 0;
ret = png_check_sig(buf, 8);
return (ret);
}
/* read a png file. You may want to return an error code if the read
fails (depending upon the failure). */
void read_png(char *file_name)
{
FILE *fp;
png_struct *png_ptr;
png_info *info_ptr;
/* open the file */
fp = fopen(file_name, "rb");
if (!fp)
return;
/* allocate the necessary structures */
png_ptr = malloc(sizeof (png_struct));
if (!png_ptr)
{
fclose(fp);
return;
}
info_ptr = malloc(sizeof (png_info));
if (!info_ptr)
{
fclose(fp);
free(png_ptr);
return;
}
/* set error handling */
if (setjmp(png_ptr->jmpbuf))
{
png_read_destroy(png_ptr, info_ptr, (png_info *)0);
fclose(fp);
free(png_ptr);
free(info_ptr);
/* If we get here, we had a problem reading the file */
return;
}
/* initialize the structures, info first for error handling */
png_info_init(info_ptr);
png_read_init(png_ptr);
/* set up the input control */
png_init_io(png_ptr, fp);
/* read the file information */
png_read_info(png_ptr, info_ptr);
/* allocate the memory to hold the image using the fields
of png_info. */
/* set up the transformations you want. Note that these are
all optional. Only call them if you want them */
/* expand paletted colors into true rgb */
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
info_ptr->bit_depth < 8)
png_set_expand(png_ptr);
/* expand grayscale images to the full 8 bits */
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
info_ptr->bit_depth < 8)
png_set_expand(png_ptr);
/* expand images with transparency to full alpha channels */
if (info_ptr->valid & PNG_INFO_tRNS)
png_set_expand(png_ptr);
/* Set the background color to draw transparent and alpha
images over */
png_color_16 my_background;
if (info_ptr->valid & PNG_INFO_bKGD)
png_set_background(png_ptr, &(info_ptr->background),
PNG_GAMMA_FILE, 1, 1.0);
else
png_set_background(png_ptr, &my_background,
PNG_GAMMA_SCREEN, 0, 1.0);
/* tell libpng to handle the gamma conversion for you */
if (info_ptr->valid & PNG_INFO_gAMA)
png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma);
else
png_set_gamma(png_ptr, screen_gamma, 0.45);
/* tell libpng to strip 16 bit depth files down to 8 bits */
if (info_ptr->bit_depth == 16)
png_set_strip_16(png_ptr);
/* dither rgb files down to 8 bit palettes & reduce palettes
to the number of colors available on your screen */
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
{
if (info_ptr->valid & PNG_INFO_PLTE)
png_set_dither(png_ptr, info_ptr->palette,
info_ptr->num_palette, max_screen_colors,
info_ptr->histogram);
else
{
png_color std_color_cube[MAX_SCREEN_COLORS] =
{/* ... colors ... */};
png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
MAX_SCREEN_COLORS, NULL);
}
}
/* invert monocrome files */
if (info_ptr->bit_depth == 1 &&
info_ptr->color_type == PNG_COLOR_GRAY)
png_set_invert(png_ptr);
/* shift the pixels down to their true bit depth */
if (info_ptr->valid & PNG_INFO_sBIT &&
info_ptr->bit_depth > info_ptr->sig_bit)
png_set_shift(png_ptr, &(info_ptr->sig_bit));
/* pack pixels into bytes */
if (info_ptr->bit_depth < 8)
png_set_packing(png_ptr);
/* flip the rgb pixels to bgr */
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_bgr(png_ptr);
/* swap bytes of 16 bit files to least significant bit first */
if (info_ptr->bit_depth == 16)
png_set_swap(png_ptr);
/* add a filler byte to store rgb files as rgbx */
if (info_ptr->bit_depth == 8 &&
info_ptr->color_type == PNG_COLOR_TYPE_RGB)
png_set_rgbx(png_ptr);
/* optional call to update palette with transformations */
png_start_read_image(png_ptr);
/* the easiest way to read the image */
void *row_pointers[height];
png_read_image(png_ptr, row_pointers);
/* the other way to read images - deal with interlacing */
/* turn on interlace handling */
if (info_ptr->interlace_type)
number_passes = png_set_interlace_handling(png_ptr);
else
number_passes = 1;
for (pass = 0; pass < number_passes; pass++)
{
/* Read the image using the "sparkle" effect. */
png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
/* If you are only reading on row at a time, this works */
for (y = 0; y < height; y++)
{
char *row_pointers = row[y];
png_read_rows(png_ptr, &row_pointers, NULL, 1);
}
/* to get the rectangle effect, use the third parameter */
png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
/* if you want to display the image after every pass, do
so here */
}
/* read the rest of the file, getting any additional chunks
in info_ptr */
png_read_end(png_ptr, info_ptr);
/* clean up after the read, and free any memory allocated */
png_read_destroy(png_ptr, info_ptr, (png_info *)0);
/* free the structures */
free(png_ptr);
free(info_ptr);
/* close the file */
fclose(fp);
/* that's it */
return;
}
/* write a png file */
void write_png(char *file_name, ... other image information ...)
{
FILE *fp;
png_struct *png_ptr;
png_info *info_ptr;
/* open the file */
fp = fopen(file_name, "wb");
if (!fp)
return;
/* allocate the necessary structures */
png_ptr = malloc(sizeof (png_struct));
if (!png_ptr)
{
fclose(fp);
return;
}
info_ptr = malloc(sizeof (png_info));
if (!info_ptr)
{
fclose(fp);
free(png_ptr);
return;
}
/* set error handling */
if (setjmp(png_ptr->jmpbuf))
{
png_write_destroy(png_ptr);
fclose(fp);
free(png_ptr);
free(info_ptr);
/* If we get here, we had a problem reading the file */
return;
}
/* initialize the structures */
png_info_init(info_ptr);
png_write_init(png_ptr);
/* set up the output control */
png_init_io(png_ptr, fp);
/* set the file information here */
info_ptr->width = ;
info_ptr->height = ;
etc.
/* set the palette if there is one */
info_ptr->valid |= PNG_INFO_PLTE;
info_ptr->palette = malloc(256 * sizeof (png_color));
info_ptr->num_palette = 256;
... set palette colors ...
/* optional significant bit chunk */
info_ptr->valid |= PNG_INFO_sBIT;
info_ptr->sig_bit = true_bit_depth;
/* optional gamma chunk */
info_ptr->valid |= PNG_INFO_gAMA;
info_ptr->gamma = gamma;
/* other optional chunks */
/* write the file information */
png_write_info(png_ptr, info_ptr);
/* set up the transformations you want. Note that these are
all optional. Only call them if you want them */
/* invert monocrome pixels */
png_set_invert(png_ptr);
/* shift the pixels up to a legal bit depth and fill in
as appropriate to correctly scale the image */
png_set_shift(png_ptr, &(info_ptr->sig_bit));
/* pack pixels into bytes */
png_set_packing(png_ptr);
/* flip bgr pixels to rgb */
png_set_bgr(png_ptr);
/* swap bytes of 16 bit files to most significant bit first */
png_set_swap(png_ptr);
/* get rid of filler bytes, pack rgb into 3 bytes */
png_set_rgbx(png_ptr);
/* the easiest way to write the image */
void *row_pointers[height];
png_write_image(png_ptr, row_pointers);
/* the other way to write the image - deal with interlacing */
/* turn on interlace handling */
if (interlacing)
number_passes = png_set_interlace_handling(png_ptr);
else
number_passes = 1;
for (pass = 0; pass < number_passes; pass++)
{
/* Write a few rows at a time. */
png_write_rows(png_ptr, row_pointers, number_of_rows);
/* If you are only writing one row at a time, this works */
for (y = 0; y < height; y++)
{
char *row_pointers = row[y];
png_write_rows(png_ptr, &row_pointers, 1);
}
}
/* write the rest of the file */
png_write_end(png_ptr, info_ptr);
/* clean up after the write, and free any memory allocated */
png_write_destroy(png_ptr);
/* if you malloced the palette, free it here */
if (info_ptr->palette)
free(info_ptr->palette);
/* free the structures */
free(png_ptr);
free(info_ptr);
/* close the file */
fclose(fp);
/* that's it */
return;
}
此差异已折叠。
# makefile for libpng
# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
# For conditions of distribution and use, see copyright notice in png.h
CC=gcc
CFLAGS=-I../zlib -O3
LDFLAGS=-L. -L../zlib/ -lpng -lgz -lm
RANLIB=ranlib
#RANLIB=echo
# where make install puts libpng.a and png.h
prefix=/usr/local
OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
pngread.o pngstub.o pngwrite.o pngrtran.o pngwtran.o
all: libpng.a pngtest
libpng.a: $(OBJS)
ar rc $@ $(OBJS)
$(RANLIB) $@
pngtest: pngtest.o libpng.a
cc -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS)
install: libpng.a
-@mkdir $(prefix)/include
-@mkdir $(prefix)/lib
cp png.h $(prefix)/include
chmod 644 $(prefix)/include/png.h
cp libpng.a $(prefix)/lib
chmod 644 $(prefix)/lib/libpng.a
clean:
rm -f *.o libpng.a pngtest pngout.png
# DO NOT DELETE THIS LINE -- make depend depends on it.
pngrcb.o: png.h
pngread.o: png.h
pngrtran.o: png.h
pngrutil.o: png.h
pngstub.o: png.h
pngtest.o: png.h
pngtrans.o: png.h
pngwrite.o: png.h
pngwtran.o: png.h
pngwutil.o: png.h
# makefile for libpng
# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
# For conditions of distribution and use, see copyright notice in png.h
CC=cc
CFLAGS=-I../zlib -O
LDFLAGS=-L. -L../zlib/ -lpng -lgz -lm
# flags for ansi2knr
ANSI2KNRFLAGS=
RANLIB=ranlib
#RANLIB=echo
# where make install puts libpng.a and png.h
prefix=/usr/local
OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
pngread.o pngstub.o pngwrite.o pngrtran.o pngwtran.o
all: ansi2knr libpng.a pngtest
# general rule to allow ansi2knr to work
.c.o:
./ansi2knr $*.c T$*.c
$(CC) $(CFLAGS) -c T$*.c
rm -f T$*.c $*.o
mv T$*.o $*.o
ansi2knr: ansi2knr.c
$(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr ansi2knr.c
libpng.a: ansi2knr $(OBJS)
ar rc $@ $(OBJS)
$(RANLIB) $@
pngtest: pngtest.o libpng.a ansi2knr
cc -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS)
install: libpng.a
-@mkdir $(prefix)/include
-@mkdir $(prefix)/lib
cp png.h $(prefix)/include
chmod 644 $(prefix)/include/png.h
cp libpng.a $(prefix)/lib
chmod 644 $(prefix)/lib/libpng.a
clean:
rm -f *.o libpng.a pngtest pngout.png ansi2knr
# DO NOT DELETE THIS LINE -- make depend depends on it.
pngrcb.o: png.h
pngread.o: png.h
pngrtran.o: png.h
pngrutil.o: png.h
pngstub.o: png.h
pngtest.o: png.h
pngtrans.o: png.h
pngwrite.o: png.h
pngwtran.o: png.h
pngwutil.o: png.h
# makefile for libpng
# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
# For conditions of distribution and use, see copyright notice in png.h
CC=cc
CFLAGS=-I../zlib -O -systype sysv -DSYSV -w -Dmips
#CFLAGS=-O
LDFLAGS=-L. -L../zlib/ -lpng -lgz -lm
#RANLIB=ranlib
RANLIB=echo
# where make install puts libpng.a and png.h
prefix=/usr/local
OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
pngread.o pngstub.o pngwrite.o pngrtran.o pngwtran.o
all: libpng.a pngtest
libpng.a: $(OBJS)
ar rc $@ $(OBJS)
$(RANLIB) $@
pngtest: pngtest.o libpng.a
cc -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS)
install: libpng.a
-@mkdir $(prefix)/include
-@mkdir $(prefix)/lib
cp png.h $(prefix)/include
chmod 644 $(prefix)/include/png.h
cp libpng.a $(prefix)/lib
chmod 644 $(prefix)/lib/libpng.a
clean:
rm -f *.o libpng.a pngtest pngout.png
# DO NOT DELETE THIS LINE -- make depend depends on it.
pngrcb.o: png.h
pngread.o: png.h
pngrtran.o: png.h
pngrutil.o: png.h
pngstub.o: png.h
pngtest.o: png.h
pngtrans.o: png.h
pngwrite.o: png.h
pngwtran.o: png.h
pngwutil.o: png.h
# makefile for libpng
# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
# For conditions of distribution and use, see copyright notice in png.h
CC=cc
CFLAGS=-I../zlib -O
LDFLAGS=-L. -L../zlib/ -lpng -lgz -lm
#RANLIB=ranlib
RANLIB=echo
# where make install puts libpng.a and png.h
prefix=/usr/local
OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
pngread.o pngstub.o pngwrite.o pngrtran.o pngwtran.o
all: libpng.a pngtest
libpng.a: $(OBJS)
ar rc $@ $(OBJS)
$(RANLIB) $@
pngtest: pngtest.o libpng.a
cc -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS)
install: libpng.a
-@mkdir $(prefix)/include
-@mkdir $(prefix)/lib
cp png.h $(prefix)/include
chmod 644 $(prefix)/include/png.h
cp libpng.a $(prefix)/lib
chmod 644 $(prefix)/lib/libpng.a
clean:
rm -f *.o libpng.a pngtest pngout.png
# DO NOT DELETE THIS LINE -- make depend depends on it.
pngrcb.o: png.h
pngread.o: png.h
pngrtran.o: png.h
pngrutil.o: png.h
pngstub.o: png.h
pngtest.o: png.h
pngtrans.o: png.h
pngwrite.o: png.h
pngwtran.o: png.h
pngwutil.o: png.h
/* png.c - location for general purpose png functions
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
*/
#define PNG_INTERNAL
#define PNG_NO_EXTERN
#include "png.h"
/* place to hold the signiture string for a png file. */
png_byte png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
/* constant strings for known chunk types. If you need to add a chunk,
add a string holding the name here. If you want to make the code
portable to EBCDIC machines, use ASCII numbers, not characters. */
png_byte png_IHDR[4] = { 73, 72, 68, 82};
png_byte png_IDAT[4] = { 73, 68, 65, 84};
png_byte png_IEND[4] = { 73, 69, 78, 68};
png_byte png_PLTE[4] = { 80, 76, 84, 69};
png_byte png_gAMA[4] = {103, 65, 77, 65};
png_byte png_sBIT[4] = {115, 66, 73, 84};
png_byte png_cHRM[4] = { 99, 72, 82, 77};
png_byte png_tRNS[4] = {116, 82, 78, 83};
png_byte png_bKGD[4] = { 98, 75, 71, 68};
png_byte png_hIST[4] = {104, 73, 83, 84};
png_byte png_tEXt[4] = {116, 69, 88, 116};
png_byte png_zTXt[4] = {122, 84, 88, 116};
png_byte png_pHYs[4] = {112, 72, 89, 115};
png_byte png_oFFs[4] = {111, 70, 70, 115};
png_byte png_tIME[4] = {116, 73, 77, 69};
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
/* start of interlace block */
int png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
/* offset to next interlace block */
int png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
/* start of interlace block in the y direction */
int png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
/* offset to next interlace block in the y direction */
int png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
/* width of interlace block */
/* this is not currently used - if you need it, uncomment it here and
in png.h
int png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
*/
/* height of interlace block */
/* this is not currently used - if you need it, uncomment it here and
in png.h
int png_pass_height[] = {8, 8, 4, 4, 4, 2, 2, 1};
*/
/* mask to determine which pixels are valid in a pass */
int png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
/* mask to determine which pixels to overwrite while displaying */
int png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
int
png_check_sig(png_byte *sig, int num)
{
if (num > 8)
num = 8;
if (num < 1)
return 0;
return (!memcmp(sig, png_sig, num));
}
/* Function to allocate memory for zlib. */
voidp
png_zalloc(voidp png_ptr, uInt items, uInt size)
{
return ((voidp)png_large_malloc((png_struct *)png_ptr,
(png_uint_32)items * (png_uint_32)size));
}
/* function to free memory for zlib */
void
png_zfree(voidp png_ptr, voidp ptr)
{
png_large_free((png_struct *)png_ptr, (void *)ptr);
}
/* reset the crc variable to 32 bits of 1's. Care must be taken
in case crc is > 32 bits to leave the top bits 0 */
void
png_reset_crc(png_struct *png_ptr)
{
/* set crc to all 1's */
png_ptr->crc = 0xffffffffL;
}
/* Note: the crc code below was copied from the sample code in the
PNG spec, with appropriate modifications made to ensure the
variables are large enough */
/* table of crc's of all 8-bit messages. If you wish to png_malloc this
table, turn this into a pointer, and png_malloc it in make_crc_table().
You may then want to hook it into png_struct and free it with the
destroy functions. */
static png_uint_32 crc_table[256];
/* Flag: has the table been computed? Initially false. */
static int crc_table_computed = 0;
/* make the table for a fast crc */
static void
make_crc_table(void)
{
png_uint_32 c;
int n, k;
for (n = 0; n < 256; n++)
{
c = (png_uint_32)n;
for (k = 0; k < 8; k++)
c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
crc_table[n] = c;
}
crc_table_computed = 1;
}
/* update a running crc with the bytes buf[0..len-1]--the crc should be
initialized to all 1's, and the transmitted value is the 1's complement
of the final running crc. */
static png_uint_32
update_crc(png_uint_32 crc, png_byte *buf, png_uint_32 len)
{
png_uint_32 c;
png_byte *p;
png_uint_32 n;
c = crc;
p = buf;
n = len;
if (!crc_table_computed)
{
make_crc_table();
}
if (n > 0) do
{
c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
} while (--n);
return c;
}
/* calculate the crc over a section of data. Note that while we
are passing in a 32 bit value for length, on 16 bit machines, you
would need to use huge pointers to access all that data. If you
need this, put huge here and above. */
void
png_calculate_crc(png_struct *png_ptr, png_byte *ptr,
png_uint_32 length)
{
png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
}
此差异已折叠。
pngchange.txt - changes for libpng
version 0.2
added reader into png.h
fixed small problems in stub file
version 0.3
added pull reader
split up pngwrite.c to several files
added pnglib.txt
added example.c
cleaned up writer, adding a few new tranformations
fixed some bugs in writer
interfaced with zlib 0.5
added K&R support
added check for 64 KB blocks for 16 bit machines
version 0.4
cleaned up code and commented code
simplified time handling into png_time
created png_color_16 and png_color_8 to handle color needs
cleaned up color type defines
fixed various bugs
made various names more consistant
interfaced with zlib 0.71
cleaned up zTXt reader and writer (using zlib's Reset functions)
split transformations into pngrtran.c and pngwtran.c
version 0.5
interfaced with zlib 0.8
fixed many reading and writing bugs
saved using 3 spaces instead of tabs
version 0.6
added png_large_malloc() and png_large_free()
added png_size_t
cleaned up some compiler warnings
added png_start_read_image()
version 0.7
cleaned up lots of bugs
finished dithering and other stuff
added test program
changed name from pnglib to libpng
version 0.71
changed pngtest.png for zlib 0.93
fixed error in libpng.txt and example.c
/* pngrcb.c - callbacks while reading a png file
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
*/
#define PNG_INTERNAL
#include "png.h"
void
png_read_IHDR(png_struct *png_ptr, png_info *info,
png_uint_32 width, png_uint_32 height, int bit_depth,
int color_type, int compression_type, int filter_type,
int interlace_type)
{
if (!png_ptr || !info)
return;
info->width = width;
info->height = height;
info->bit_depth = bit_depth;
info->color_type = color_type;
info->compression_type = compression_type;
info->filter_type = filter_type;
info->interlace_type = interlace_type;
if (info->color_type == PNG_COLOR_TYPE_PALETTE)
info->channels = 1;
else if (info->color_type & PNG_COLOR_MASK_COLOR)
info->channels = 3;
else
info->channels = 1;
if (info->color_type & PNG_COLOR_MASK_ALPHA)
info->channels++;
info->pixel_depth = info->channels * info->bit_depth;
info->rowbytes = ((info->width * info->pixel_depth + 7) >> 3);
}
void
png_read_PLTE(png_struct *png_ptr, png_info *info,
png_color *palette, int num)
{
if (!png_ptr || !info)
return;
info->palette = palette;
info->num_palette = num;
info->valid |= PNG_INFO_PLTE;
}
void
png_read_gAMA(png_struct *png_ptr, png_info *info, float gamma)
{
if (!png_ptr || !info)
return;
info->gamma = gamma;
info->valid |= PNG_INFO_gAMA;
}
void
png_read_sBIT(png_struct *png_ptr, png_info *info,
png_color_8 *sig_bit)
{
if (!png_ptr || !info)
return;
memcpy(&(info->sig_bit), sig_bit, sizeof (png_color_8));
info->valid |= PNG_INFO_sBIT;
}
void
png_read_cHRM(png_struct *png_ptr, png_info *info,
float white_x, float white_y, float red_x, float red_y,
float green_x, float green_y, float blue_x, float blue_y)
{
if (!png_ptr || !info)
return;
info->x_white = white_x;
info->y_white = white_y;
info->x_red = red_x;
info->y_red = red_y;
info->x_green = green_x;
info->y_green = green_y;
info->x_blue = blue_x;
info->y_blue = blue_y;
info->valid |= PNG_INFO_cHRM;
}
void
png_read_tRNS(png_struct *png_ptr, png_info *info,
png_byte *trans, int num_trans, png_color_16 *trans_values)
{
if (!png_ptr || !info)
return;
if (trans)
{
info->trans = trans;
}
else
{
memcpy(&(info->trans_values), trans_values,
sizeof(png_color_16));
}
info->num_trans = num_trans;
info->valid |= PNG_INFO_tRNS;
}
void
png_read_bKGD(png_struct *png_ptr, png_info *info,
png_color_16 *background)
{
if (!png_ptr || !info)
return;
memcpy(&(info->background), background, sizeof(png_color_16));
info->valid |= PNG_INFO_bKGD;
}
void
png_read_hIST(png_struct *png_ptr, png_info *info, png_uint_16 *hist)
{
if (!png_ptr || !info)
return;
info->hist = hist;
info->valid |= PNG_INFO_hIST;
}
void
png_read_pHYs(png_struct *png_ptr, png_info *info,
png_uint_32 res_x, png_uint_32 res_y, int unit_type)
{
if (!png_ptr || !info)
return;
info->x_pixels_per_unit = res_x;
info->y_pixels_per_unit = res_y;
info->phys_unit_type = unit_type;
info->valid |= PNG_INFO_pHYs;
}
void
png_read_oFFs(png_struct *png_ptr, png_info *info,
png_uint_32 offset_x, png_uint_32 offset_y, int unit_type)
{
if (!png_ptr || !info)
return;
info->x_offset = offset_x;
info->y_offset = offset_y;
info->offset_unit_type = unit_type;
info->valid |= PNG_INFO_oFFs;
}
void
png_read_tIME(png_struct *png_ptr, png_info *info,
png_time *mod_time)
{
if (!png_ptr || !info)
return;
memcpy(&(info->mod_time), mod_time, sizeof (png_time));
info->valid |= PNG_INFO_tIME;
}
void
png_read_zTXt(png_struct *png_ptr, png_info *info,
char *key, char *text, png_uint_32 text_len, int compression)
{
if (!png_ptr || !info)
return;
if (info->max_text <= info->num_text)
{
if (info->text)
{
info->max_text = info->num_text + 16;
info->text = (png_text *)png_realloc(png_ptr,
info->text,
info->max_text * sizeof (png_text));
}
else
{
info->max_text = info->num_text + 16;
info->text = (png_text *)png_malloc(png_ptr,
info->max_text * sizeof (png_text));
info->num_text = 0;
}
}
info->text[info->num_text].key = key;
info->text[info->num_text].text = text;
info->text[info->num_text].text_length = text_len;
info->text[info->num_text].compression = compression;
info->num_text++;
}
void
png_read_tEXt(png_struct *png_ptr, png_info *info,
char *key, char *text, png_uint_32 text_len)
{
if (!png_ptr || !info)
return;
png_read_zTXt(png_ptr, info, key, text, text_len, -1);
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
/* pngstub.c - stub functions for i/o and memory allocation
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
This file provides a location for all input/output, memory location,
and error handling. Users which need special handling in these areas
are expected to modify the code in this file to meet their needs. See
the instructions at each function. */
#define PNG_INTERNAL
#include "png.h"
/* Write the data to whatever output you are using. The default
routine writes to a file pointer. If you need to write to something
else, this is the place to do it. We suggest saving the old code
for future use, possibly in a #define. Note that this routine sometimes
gets called with very small lengths, so you should implement some kind
of simple buffering if you are using unbuffered writes. This should
never be asked to write more then 64K on a 16 bit machine. The cast
to png_size_t is there for insurance, but if you are having problems
with it, you can take it out. Just be sure to cast length to whatever
fwrite needs in that spot if you don't have a function prototype for
it. */
void
png_write_data(png_struct *png_ptr, png_byte *data, png_uint_32 length)
{
png_uint_32 check;
check = fwrite(data, 1, (png_size_t)length, png_ptr->fp);
if (check != length)
{
png_error(png_ptr, "Write Error");
}
}
/* Read the data from whatever input you are using. The default
routine reads from a file pointer. If you need to read from something
else, this is the place to do it. We suggest saving the old code
for future use. Note that this routine sometimes gets called with
very small lengths, so you should implement some kind of simple
buffering if you are using unbuffered reads. This should
never be asked to read more then 64K on a 16 bit machine. The cast
to png_size_t is there for insurance, but if you are having problems
with it, you can take it out. Just be sure to cast length to whatever
fread needs in that spot if you don't have a function prototype for
it. */
void
png_read_data(png_struct *png_ptr, png_byte *data, png_uint_32 length)
{
png_uint_32 check;
check = fread(data, 1, (size_t)length, png_ptr->fp);
if (check != length)
{
png_error(png_ptr, "Read Error");
}
}
/* Initialize the input/output for the png file. If you change
the read and write routines, you will probably need to change
this routine (or write your own). If you change the parameters
of this routine, remember to change png.h also. */
void
png_init_io(png_struct *png_ptr, FILE *fp)
{
png_ptr->fp = fp;
}
/* Allocate memory. For reasonable files, size should never exceed
64K. However, zlib may allocate more then 64K if you don't tell
it not to. See zconf.h and png.h for more information. zlib does
need to allocate exactly 64K, so whatever you call here must
have the ability to do that. */
/* Borland compilers have this habit of not giving you 64K chunks
that start on the segment in DOS mode. This has not been observed
in Windows, and of course it doesn't matter in 32 bit mode, as there
are no segments. Now libpng doesn't need that much memory normally,
but zlib does, so we have to normalize it, if necessary. It would be
better if zlib worked in less then 64K, but it doesn't, so we
have to deal with it. Truely, we are misusing farmalloc here,
as it is designed for use with huge pointers, which don't care
about segments. So we allocate a large amount of memory, and
divvy off segments when needed.
*/
#ifdef __TURBOC__
#ifndef __WIN32__
/* NUM_SEG is the number of segments allocated at once */
#define NUM_SEG 4
typedef struct borland_seg_struct
{
void *mem_ptr;
void *seg_ptr[NUM_SEG];
int seg_used[NUM_SEG];
int num_used;
} borland_seg;
borland_seg *save_array;
int num_save_array;
int max_save_array;
#endif
#endif
void *
png_large_malloc(png_struct *png_ptr, png_uint_32 size)
{
void *ret;
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
#ifdef __TURBOC__
# ifdef __WIN32__
ret = farmalloc(size);
# else
if (size == 65536L)
{
unsigned long offset;
if (!save_array)
{
ret = farmalloc(size);
offset = (unsigned long)(ret);
offset &= 0xffffL;
}
else
{
ret = (void *)0;
}
if (save_array || offset)
{
int i, j;
if (ret)
farfree(ret);
ret = (void *)0;
if (!save_array)
{
unsigned long offset;
png_byte huge *ptr;
int i;
num_save_array = 1;
save_array = malloc(num_save_array * sizeof (borland_seg));
if (!save_array)
png_error(png_ptr, "Out of Memory");
save_array->mem_ptr = farmalloc(
(unsigned long)(NUM_SEG) * 65536L + 65528L);
if (!save_array->mem_ptr)
png_error(png_ptr, "Out of Memory");
offset = (unsigned long)(ret);
offset &= 0xffffL;
ptr = save_array->mem_ptr;
if (offset)
ptr += 65536L - offset;
for (i = 0; i < NUM_SEG; i++, ptr += 65536L)
{
save_array->seg_ptr[i] = ptr;
save_array->seg_used[i] = 0;
}
save_array->num_used = 0;
}
for (i = 0; i < num_save_array; i++)
{
for (j = 0; j < NUM_SEG; j++)
{
if (!save_array[i].seg_used[j])
{
ret = save_array[i].seg_ptr[j];
save_array[i].seg_used[j] = 1;
save_array[i].num_used++;
break;
}
}
if (ret)
break;
}
if (!ret)
{
unsigned long offset;
png_byte huge *ptr;
save_array = realloc(save_array,
(num_save_array + 1) * sizeof (borland_seg));
if (!save_array)
png_error(png_ptr, "Out of Memory");
save_array[num_save_array].mem_ptr = farmalloc(
(unsigned long)(NUM_SEG) * 65536L + 65528L);
if (!save_array[num_save_array].mem_ptr)
png_error(png_ptr, "Out of Memory");
offset = (unsigned long)(ret);
offset &= 0xffffL;
ptr = save_array[num_save_array].mem_ptr;
if (offset)
ptr += 65536L - offset;
for (i = 0; i < NUM_SEG; i++, ptr += 65536L)
{
save_array[num_save_array].seg_ptr[i] = ptr;
save_array[num_save_array].seg_used[i] = 0;
}
ret = save_array[num_save_array].seg_ptr[0];
save_array[num_save_array].seg_used[0] = 1;
save_array[num_save_array].num_used = 1;
num_save_array++;
}
}
}
else
{
ret = farmalloc(size);
}
# endif /* __WIN32__ */
#else /* __TURBOC__ */
# ifdef _MSC_VER
ret = halloc(size, 1);
# else
/* everybody else, so normal malloc should do it. */
ret = malloc(size);
# endif
#endif
if (!ret)
{
png_error(png_ptr, "Out of Memory");
}
return ret;
}
/* free a pointer allocated by png_large_malloc(). In the default
configuration, png_ptr is not used, but is passed in case it
is needed. If ptr is NULL, return without taking any action. */
void
png_large_free(png_struct *png_ptr, void *ptr)
{
if (!png_ptr)
return;
if (ptr != (void *)0)
{
#ifdef __TURBOC__
# ifndef __WIN32__
int i, j;
for (i = 0; i < num_save_array; i++)
{
for (j = 0; j < NUM_SEG; j++)
{
if (ptr == save_array[i].seg_ptr[j])
{
printf("freeing pointer: i, j: %d, %d\n", i, j);
save_array[i].seg_used[j] = 0;
ptr = 0;
save_array[i].num_used--;
if (!save_array[i].num_used)
{
int k;
printf("freeing array: %d\n", i);
num_save_array--;
farfree(save_array[i].mem_ptr);
for (k = i; k < num_save_array; k++)
save_array[k] = save_array[k + 1];
if (!num_save_array)
{
free(save_array);
save_array = 0;
}
}
break;
}
}
if (!ptr)
break;
}
# endif
if (ptr)
farfree(ptr);
#else
# ifdef _MSC_VER
hfree(ptr);
# else
free(ptr);
# endif
#endif
}
}
/* Allocate memory. This is called for smallish blocks only It
should not get anywhere near 64K. */
void *
png_malloc(png_struct *png_ptr, png_uint_32 size)
{
void *ret;
if (!png_ptr)
return ((void *)0);
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
ret = malloc((png_size_t)size);
if (!ret)
{
png_error(png_ptr, "Out of Memory");
}
return ret;
}
/* Reallocate memory. This will not get near 64K on a
even marginally reasonable file. */
void *
png_realloc(png_struct *png_ptr, void *ptr, png_uint_32 size)
{
void *ret;
if (!png_ptr)
return ((void *)0);
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
ret = realloc(ptr, (png_size_t)size);
if (!ret)
{
png_error(png_ptr, "Out of Memory");
}
return ret;
}
/* free a pointer allocated by png_malloc(). In the default
configuration, png_ptr is not used, but is passed incase it
is needed. If ptr is NULL, return without taking any action. */
void
png_free(png_struct *png_ptr, void *ptr)
{
if (!png_ptr)
return;
if (ptr != (void *)0)
free(ptr);
}
/* This function is called whenever there is an error. Replace with
however you wish to handle the error. Note that this function
MUST NOT return, or the program will crash */
void
png_error(png_struct *png_ptr, char *message)
{
fprintf(stderr, "libpng error: %s\n", message);
longjmp(png_ptr->jmpbuf, 1);
}
/* This function is called when there is a warning, but the library
thinks it can continue anyway. You don't have to do anything here
if you don't want to. In the default configuration, png_ptr is
not used, but it is passed in case it may be useful. */
void
png_warning(png_struct *png_ptr, char *message)
{
if (!png_ptr)
return;
fprintf(stderr, "libpng warning: %s\n", message);
}
/* pngtest.c - a simple test program to test libpng
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
*/
#include <stdio.h>
#include <stdlib.h>
#include "png.h"
#ifdef __TURBOC__
#include <mem.h>
#endif
/* defined so I can write to a file on gui/windowing platforms */
#define STDERR stderr
/* input and output filenames */
char inname[] = "pngtest.png";
char outname[] = "pngout.png";
png_struct read_ptr;
png_struct write_ptr;
png_info info_ptr;
png_info end_info;
char inbuf[256], outbuf[256];
int main()
{
FILE *fpin, *fpout;
png_byte *row_buf;
png_uint_32 rowbytes;
png_uint_32 y;
int channels, num_pass, pass;
row_buf = (png_byte *)0;
fpin = fopen(inname, "rb");
if (!fpin)
{
fprintf(STDERR, "Could not find input file %s\n", inname);
return -1;
}
fpout = fopen(outname, "wb");
if (!fpin)
{
fprintf(STDERR, "could not open output file %s\n", outname);
fclose(fpin);
return -1;
}
if (setjmp(read_ptr.jmpbuf))
{
fprintf(STDERR, "libpng read error\n");
fclose(fpin);
fclose(fpout);
return -1;
}
if (setjmp(write_ptr.jmpbuf))
{
fprintf(STDERR, "libpng write error\n");
fclose(fpin);
fclose(fpout);
return -1;
}
png_read_init(&read_ptr);
png_write_init(&write_ptr);
png_info_init(&info_ptr);
png_info_init(&end_info);
png_init_io(&read_ptr, fpin);
png_init_io(&write_ptr, fpout);
png_read_info(&read_ptr, &info_ptr);
png_write_info(&write_ptr, &info_ptr);
if ((info_ptr.color_type & 3) == 2)
channels = 3;
else
channels = 1;
if (info_ptr.color_type & 4)
channels++;
rowbytes = ((info_ptr.width * info_ptr.bit_depth * channels + 7) >> 3);
row_buf = (png_byte *)malloc((size_t)rowbytes);
if (!row_buf)
{
fprintf(STDERR, "no memory to allocate row buffer\n");
png_read_destroy(&read_ptr, &info_ptr, (png_info *)0);
png_write_destroy(&write_ptr);
fclose(fpin);
fclose(fpout);
return -1;
}
if (info_ptr.interlace_type)
{
num_pass = png_set_interlace_handling(&read_ptr);
num_pass = png_set_interlace_handling(&write_ptr);
}
else
{
num_pass = 1;
}
for (pass = 0; pass < num_pass; pass++)
{
for (y = 0; y < info_ptr.height; y++)
{
png_read_rows(&read_ptr, &row_buf, (png_byte **)0, 1);
png_write_rows(&write_ptr, &row_buf, 1);
}
}
png_read_end(&read_ptr, &end_info);
png_write_end(&write_ptr, &end_info);
png_read_destroy(&read_ptr, &info_ptr, &end_info);
png_write_destroy(&write_ptr);
fclose(fpin);
fclose(fpout);
free(row_buf);
fpin = fopen(inname, "rb");
if (!fpin)
{
fprintf(STDERR, "could not find file %s\n", inname);
return -1;
}
fpout = fopen(outname, "rb");
if (!fpout)
{
fprintf(STDERR, "could not find file %s\n", outname);
fclose(fpin);
return -1;
}
while (1)
{
int num_in, num_out;
num_in = fread(inbuf, 1, 256, fpin);
num_out = fread(outbuf, 1, 256, fpout);
if (num_in != num_out)
{
fprintf(STDERR, "files are of a different size\n");
fclose(fpin);
fclose(fpout);
return -1;
}
if (!num_in)
break;
if (memcmp(inbuf, outbuf, num_in))
{
fprintf(STDERR, "files are different\n");
fclose(fpin);
fclose(fpout);
return -1;
}
}
fclose(fpin);
fclose(fpout);
fprintf(STDERR, "libpng passes test\n");
return 0;
}
pngtodo.txt - list of things to do for libpng
allow user to #define out unused transformations
medium memory model support
overlaying one image on top of another
optional palette creation
histogram creation
text conversion between different code types
cHRM transformation
support for other chunks being defined (sCAl, the gIF series,
and others that people come up with).
push reader
pull writer
better dithering
keep up with public chunks
other compression libraries
more exotic interlace handling
better filtering
C++ wrapper
other languages
comments of > 64K
/* pngtrans.c - transforms the data in a row
routines used by both readers and writers
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
*/
#define PNG_INTERNAL
#include "png.h"
/* turn on bgr to rgb mapping */
void
png_set_bgr(png_struct *png_ptr)
{
png_ptr->transformations |= PNG_BGR;
}
/* turn on 16 bit byte swapping */
void
png_set_swap(png_struct *png_ptr)
{
if (png_ptr->bit_depth == 16)
png_ptr->transformations |= PNG_SWAP_BYTES;
}
/* turn on pixel packing */
void
png_set_packing(png_struct *png_ptr)
{
if (png_ptr->bit_depth < 8)
{
png_ptr->transformations |= PNG_PACK;
png_ptr->usr_bit_depth = 8;
}
}
void
png_set_shift(png_struct *png_ptr, png_color_8 *true_bits)
{
png_ptr->transformations |= PNG_SHIFT;
png_ptr->shift = *true_bits;
}
int
png_set_interlace_handling(png_struct *png_ptr)
{
if (png_ptr->interlaced)
{
png_ptr->transformations |= PNG_INTERLACE;
return 7;
}
return 1;
}
void
png_set_rgbx(png_struct *png_ptr)
{
png_ptr->transformations |= PNG_RGBA;
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB &&
png_ptr->bit_depth == 8)
png_ptr->usr_channels = 4;
}
void
png_set_xrgb(png_struct *png_ptr)
{
png_ptr->transformations |= PNG_XRGB;
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB &&
png_ptr->bit_depth == 8)
png_ptr->usr_channels = 4;
}
void
png_set_invert_mono(png_struct *png_ptr)
{
png_ptr->transformations |= PNG_INVERT_MONO;
}
/* invert monocrome grayscale data */
void
png_do_invert(png_row_info *row_info, png_byte *row)
{
if (row && row_info && row_info->bit_depth == 1 &&
row_info->color_type == PNG_COLOR_TYPE_GRAY)
{
png_byte *rp;
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->rowbytes;
i++, rp++)
{
*rp = ~(*rp);
}
}
}
/* swaps byte order on 16 bit depth images */
void
png_do_swap(png_row_info *row_info, png_byte *row)
{
if (row && row_info && row_info->bit_depth == 16)
{
png_byte *rp, t;
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width * row_info->channels;
i++, rp += 2)
{
t = *rp;
*rp = *(rp + 1);
*(rp + 1) = t;
}
}
}
/* swaps red and blue */
void
png_do_bgr(png_row_info *row_info, png_byte *row)
{
if (row && row_info && (row_info->color_type & 2))
{
if (row_info->color_type == 2 && row_info->bit_depth == 8)
{
png_byte *rp, t;
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 3)
{
t = *rp;
*rp = *(rp + 2);
*(rp + 2) = t;
}
}
else if (row_info->color_type == 6 && row_info->bit_depth == 8)
{
png_byte *rp, t;
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 4)
{
t = *rp;
*rp = *(rp + 2);
*(rp + 2) = t;
}
}
else if (row_info->color_type == 2 && row_info->bit_depth == 16)
{
png_byte *rp, t[2];
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 6)
{
t[0] = *rp;
t[1] = *(rp + 1);
*rp = *(rp + 4);
*(rp + 1) = *(rp + 5);
*(rp + 4) = t[0];
*(rp + 5) = t[1];
}
}
else if (row_info->color_type == 6 && row_info->bit_depth == 16)
{
png_byte *rp, t[2];
png_uint_32 i;
for (i = 0, rp = row;
i < row_info->width;
i++, rp += 8)
{
t[0] = *rp;
t[1] = *(rp + 1);
*rp = *(rp + 4);
*(rp + 1) = *(rp + 5);
*(rp + 4) = t[0];
*(rp + 5) = t[1];
}
}
}
}
此差异已折叠。
/* pngwtran.c - transforms the data in a row for png writers
libpng 1.0 beta 1 - version 0.71
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
June 26, 1995
*/
#define PNG_INTERNAL
#include "png.h"
/* transform the data according to the users wishes. The order of
transformations is significant. */
void
png_do_write_transformations(png_struct *png_ptr)
{
if (png_ptr->transformations & PNG_RGBA)
png_do_write_rgbx(&(png_ptr->row_info), png_ptr->row_buf + 1);
if (png_ptr->transformations & PNG_XRGB)
png_do_write_xrgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
if (png_ptr->transformations & PNG_PACK)
png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
png_ptr->bit_depth);
if (png_ptr->transformations & PNG_SHIFT)
png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
&(png_ptr->shift));
if (png_ptr->transformations & PNG_SWAP_BYTES)
png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
if (png_ptr->transformations & PNG_BGR)
png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
if (png_ptr->transformations & PNG_INVERT_MONO)
png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
}
/* pack pixels into bytes. Pass the true bit depth in bit_depth. The
row_info bit depth should be 8 (one pixel per byte). The channels
should be 1 (this only happens on grayscale and paletted images) */
void
png_do_pack(png_row_info *row_info, png_byte *row, png_byte bit_depth)
{
if (row_info && row && row_info->bit_depth == 8 &&
row_info->channels == 1)
{
switch (bit_depth)
{
case 1:
{
png_byte *sp;
png_byte *dp;
int mask;
png_int_32 i;
int v;
sp = row;
dp = row;
mask = 0x80;
v = 0;
for (i = 0; i < row_info->width; i++)
{
if (*sp)
v |= mask;
sp++;
if (mask > 1)
mask >>= 1;
else
{
mask = 0x80;
*dp = v;
dp++;
v = 0;
}
}
if (mask != 0x80)
*dp = v;
break;
}
case 2:
{
png_byte *sp;
png_byte *dp;
int shift;
png_int_32 i;
int v;
png_byte value;
sp = row;
dp = row;
shift = 6;
v = 0;
for (i = 0; i < row_info->width; i++)
{
value = *sp & 0x3;
v |= (value << shift);
if (shift == 0)
{
shift = 6;
*dp = v;
dp++;
v = 0;
}
else
shift -= 2;
sp++;
}
if (shift != 6)
*dp = v;
break;
}
case 4:
{
png_byte *sp;
png_byte *dp;
int shift;
png_int_32 i;
int v;
png_byte value;
sp = row;
dp = row;
shift = 4;
v = 0;
for (i = 0; i < row_info->width; i++)
{
value = *sp & 0xf;
v |= (value << shift);
if (shift == 0)
{
shift = 4;
*dp = v;
dp++;
v = 0;
}
else
shift -= 4;
sp++;
}
if (shift != 4)
*dp = v;
break;
}
}
row_info->bit_depth = bit_depth;
row_info->pixel_depth = bit_depth * row_info->channels;
row_info->rowbytes =
((row_info->width * row_info->pixel_depth + 7) >> 3);
}
}
/* shift pixel values to take advantage of whole range. Pass the
true number of bits in bit_depth. The row should be packed
according to row_info->bit_depth. Thus, if you had a row of
bit depth 4, but the pixels only had values from 0 to 7, you
would pass 3 as bit_depth, and this routine would translate the
data to 0 to 15. */
void
png_do_shift(png_row_info *row_info, png_byte *row, png_color_8 *bit_depth)
{
if (row && row_info &&
row_info->color_type != PNG_COLOR_TYPE_PALETTE)
{
int shift_start[4], shift_dec[4];
int channels;
channels = 0;
if (row_info->color_type & PNG_COLOR_MASK_COLOR)
{
shift_start[channels] = row_info->bit_depth - bit_depth->red;
shift_dec[channels] = bit_depth->red;
channels++;
shift_start[channels] = row_info->bit_depth - bit_depth->green;
shift_dec[channels] = bit_depth->green;
channels++;
shift_start[channels] = row_info->bit_depth - bit_depth->blue;
shift_dec[channels] = bit_depth->blue;
channels++;
}
else
{
shift_start[channels] = row_info->bit_depth - bit_depth->gray;
shift_dec[channels] = bit_depth->gray;
channels++;
}
if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
{
shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
shift_dec[channels] = bit_depth->alpha;
channels++;
}
/* with low row dephts, could only be grayscale, so one channel */
if (row_info->bit_depth < 8)
{
png_byte *bp;
png_uint_32 i;
int j;
png_byte mask;
if (bit_depth->gray == 1 && row_info->bit_depth == 2)
mask = 0x55;
else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
mask = 0x11;
else
mask = 0xff;
for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
{
int v;
v = *bp;
*bp = 0;
for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
{
if (j > 0)
*bp |= (png_byte)((v << j) & 0xff);
else
*bp |= (png_byte)((v >> (-j)) & mask);
}
}
}
else if (row_info->bit_depth == 8)
{
png_byte *bp;
png_uint_32 i;
int j;
for (bp = row, i = 0; i < row_info->width; i++)
{
int c;
for (c = 0; c < channels; c++, bp++)
{
int v;
v = *bp;
*bp = 0;
for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
{
if (j > 0)
*bp |= (png_byte)((v << j) & 0xff);
else
*bp |= (png_byte)((v >> (-j)) & 0xff);
}
}
}
}
else
{
png_byte *bp;
png_uint_32 i;
int j;
for (bp = row, i = 0;
i < row_info->width * row_info->channels;
i++)
{
int c;
for (c = 0; c < channels; c++, bp += 2)
{
png_uint_16 value, v;
v = ((png_uint_16)(*bp) << 8) + (png_uint_16)(*(bp + 1));
value = 0;
for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
{
if (j > 0)
value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
else
value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
}
*bp = value >> 8;
*(bp + 1) = value & 0xff;
}
}
}
}
}
/* remove filler byte after rgb */
void
png_do_write_rgbx(png_row_info *row_info, png_byte *row)
{
if (row && row_info && row_info->color_type == PNG_COLOR_TYPE_RGB &&
row_info->bit_depth == 8)
{
png_byte *sp, *dp;
png_uint_32 i;
for (i = 1, sp = row + 4, dp = row + 3;
i < row_info->width;
i++)
{
*dp++ = *sp++;
*dp++ = *sp++;
*dp++ = *sp++;
sp++;
}
row_info->channels = 3;
row_info->pixel_depth = 24;
row_info->rowbytes = row_info->width * 3;
}
}
/* remove filler byte before rgb */
void
png_do_write_xrgb(png_row_info *row_info, png_byte *row)
{
if (row && row_info && row_info->color_type == PNG_COLOR_TYPE_RGB &&
row_info->bit_depth == 8)
{
png_byte *sp, *dp;
png_uint_32 i;
for (i = 0, sp = row, dp = row;
i < row_info->width;
i++)
{
sp++;
*dp++ = *sp++;
*dp++ = *sp++;
*dp++ = *sp++;
}
row_info->channels = 3;
row_info->pixel_depth = 24;
row_info->rowbytes = row_info->width * 3;
}
}
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册