Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Libpng
提交
0f71645d
T
Third Party Libpng
项目概览
OpenHarmony
/
Third Party Libpng
接近 2 年 前同步成功
通知
4
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Libpng
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0f71645d
编写于
11月 28, 1995
作者:
G
Guy Schalnat
提交者:
Glenn Randers-Pehrson
4月 06, 2009
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Imported from libpng-0.82.tar
上级
51f0eb45
变更
16
展开全部
隐藏空白更改
内联
并排
Showing
16 changed file
with
1261 addition
and
503 deletion
+1261
-503
example.c
example.c
+43
-7
libpng.txt
libpng.txt
+194
-64
lpfaq.txt
lpfaq.txt
+190
-0
makefile
makefile
+7
-7
png.h
png.h
+71
-27
pngchang.txt
pngchang.txt
+10
-0
pngconf.h
pngconf.h
+2
-1
pngerror.c
pngerror.c
+0
-46
pngio.c
pngio.c
+269
-38
pngmem.c
pngmem.c
+11
-11
pngread.c
pngread.c
+93
-75
pngrtran.c
pngrtran.c
+46
-31
pngrutil.c
pngrutil.c
+64
-30
pngtest.c
pngtest.c
+145
-142
pngwrite.c
pngwrite.c
+99
-8
pngwutil.c
pngwutil.c
+17
-16
未找到文件。
example.c
浏览文件 @
0f71645d
...
...
@@ -79,9 +79,19 @@ void read_png(char *file_name)
png_info_init
(
info_ptr
);
png_read_init
(
png_ptr
);
/* set up the input control */
/* set up the input control for the default input and message functions.
* If we were to replace both the input and message functions we don't
* need to call png_init_io first. */
png_init_io
(
png_ptr
,
fp
);
/* if you are using replacement read functions, here you would call */
io_ptr
=
(
user_io_struct
*
)
malloc
(
sizeof
(
user_io_struct
));
png_set_read_fn
(
png_ptr
,
(
void
*
)
io_ptr
,
user_read_fn
);
/* if you are using replacement message functions, here you would call */
msg_ptr
=
(
user_msg_struct
*
)
malloc
(
sizeof
(
user_msg_struct
));
png_set_read_fn
(
png_ptr
,
(
void
*
)
msg_ptr
,
user_error_fn
,
user_warning_fn
);
/* read the file information */
png_read_info
(
png_ptr
,
info_ptr
);
...
...
@@ -146,8 +156,7 @@ void read_png(char *file_name)
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
)
if
(
info_ptr
->
valid
&
PNG_INFO_sBIT
)
png_set_shift
(
png_ptr
,
&
(
info_ptr
->
sig_bit
));
/* pack pixels into bytes */
...
...
@@ -212,6 +221,11 @@ void read_png(char *file_name)
in info_ptr */
png_read_end
(
png_ptr
,
info_ptr
);
/* if you had allocated any memory structures for custom input or
messaging routines you need to free them before png_read_destroy */
free
(
png_get_io_ptr
(
png_ptr
));
free
(
png_get_msg_ptr
(
png_ptr
));
/* clean up after the read, and free any memory allocated */
png_read_destroy
(
png_ptr
,
info_ptr
,
(
png_info
*
)
0
);
...
...
@@ -269,9 +283,19 @@ void write_png(char *file_name, ... other image information ...)
png_info_init
(
info_ptr
);
png_write_init
(
png_ptr
);
/* set up the output control */
/* set up the output control for the default output and message functions.
* If we were to replace both the output and message functions we don't
* need to call png_init_io first. */
png_init_io
(
png_ptr
,
fp
);
/* if you are using replacement write functions, here you would call */
io_ptr
=
(
user_io_struct
*
)
malloc
(
sizeof
(
user_io_struct
));
png_set_write_fn
(
png_ptr
,
(
void
*
)
io_ptr
,
user_write_fn
,
user_flush_fn
);
/* if you are using replacement message functions, here you would call */
msg_ptr
=
(
user_msg_struct
*
)
malloc
(
sizeof
(
user_msg_struct
));
png_set_read_fn
(
png_ptr
,
(
void
*
)
msg_ptr
,
user_error_fn
,
user_warning_fn
);
/* set the file information here */
info_ptr
->
width
=
;
info_ptr
->
height
=
;
...
...
@@ -285,9 +309,16 @@ void write_png(char *file_name, ... other image information ...)
/* optional significant bit chunk */
info_ptr
->
valid
|=
PNG_INFO_sBIT
;
info_ptr
->
sig_bit
=
true_bit_depth
;
/* optional gamma chunk */
/* if we are dealing with a grayscale image then */
info_ptr
->
sig_bit
.
gray
=
true_bit_depth
;
/* otherwise, if we are dealing with a color image then */
info_ptr
->
sig_bit
.
red
=
true_red_bit_depth
;
info_ptr
->
sig_bit
.
green
=
true_green_bit_depth
;
info_ptr
->
sig_bit
.
blue
=
true_blue_bit_depth
;
/* if the image has an alpha channel then */
info_ptr
->
sig_bit
.
alpha
=
true_alpha_bit_depth
;
/* optional gamma chunk is a good idea if you can write one */
info_ptr
->
valid
|=
PNG_INFO_gAMA
;
info_ptr
->
gamma
=
gamma
;
...
...
@@ -347,6 +378,11 @@ void write_png(char *file_name, ... other image information ...)
/* write the rest of the file */
png_write_end
(
png_ptr
,
info_ptr
);
/* if you had allocated any memory structures for custom output or
messaging routines you need to free them before png_write_destroy */
free
(
png_get_io_ptr
(
png_ptr
));
free
(
png_get_msg_ptr
(
png_ptr
));
/* clean up after the write, and free any memory allocated */
png_write_destroy
(
png_ptr
);
...
...
libpng.txt
浏览文件 @
0f71645d
此差异已折叠。
点击以展开。
lpfaq.txt
0 → 100644
浏览文件 @
0f71645d
libpng FAQ - August 20, 1995
Maintainer:
Guy Eric Schalnat: schalnat@group42.com CompuServe: 75501,1625
Contents:
1) General Questions
1.1) How official is this FAQ?
1.2) How often is this FAQ posted?
1.3) Where can I get this FAQ, libpng, zlib, the PNG
specification, test images, etc.?
1.4) How ready is libpng?
1.5) How portable is libpng?
1.6) When will libpng be out of Beta?
1.7) What remains to be done in libpng for the 1.0 release?
1.8) Can I help?
1.9) Why don't you answer your mail?
2) Libpng questions
2.1) Does the order of functions calls matter?
2.2) When I try to read/write a png image, it doesn't work.
2.3) Do I have to start with example.c?
2.4) What things should I be careful of?
2.5) Why doesn't example.c compile?
3) Critical Patches
No known bugs (for now :-).
4) Non-critical patches
No known bugs (for now :-).
Questions:
1) General Questions
1.1) How official is this FAQ?
As it was written by the author of libpng (that's me :), it's
about offical as one can get. However, it is not ready for
prime time yet, as libpng is still in beta, and thus its
users are of the sort that are willing to deal with beta
software. Thus, I assume all of you reading this have a
basic familiarity with PNG, libpng, and C programming. I'll
write a basic section for libpng later, as well as some quick
stuff on PNG itself (but this will not be the PNG FAQ. I just
don't have that kind of time).
1.2) How often is this FAQ posted?
As often as events warrant. I doubt it'll get stale, and as
it is not (and may never be) actually posted to a newsgroup,
I feel no need to make monthy postings.
1.3) Where can I get this FAQ, libpng, zlib, the PNG
specification, test images, etc.?
Everything should be at ftp://ftp.uu.net/graphics/png and
mirror sites. On CompuServe, it should be at GO GRAPHSUPPORT
in the Png Development library (lib 20). Failing that, try
my ftp site at ftp://ftp.group42.com/pub/png or web site at
http://www.group42.com/ If you have trouble getting to these,
the numerical address is 204.94.158.25
1.4) How ready is libpng?
In its current incarnation (0.8), it still may have a few bugs.
I think I got all of the bugs this time, but one never knows.
Also, the dithering currently in libpng is horrid, so that
needs work. Finally, I have yet to implement medium model
support.
1.5) How portable is libpng?
As portable as I can make it. It needs int's of at least
16 bits, and longs of at least 32 bits. It has not been
tried for char's larger then 8 bits. It should support
any reasonable K&R C compiler, as well as any ANSI C
compiler. The most you should need to do is change the
includes in pngconf.h. So far, I know of no compiler in use
that libpng will not compile on.
1.6) When will libpng be out of Beta?
That depends on my workload here. I'm writing libpng for
free, which generates tons of good will, but doesn't help
put food on the table, so there are times I just can't do
any real work on it. There is still some work to be done
(see question 1.7), so don't expect the official version
before September (and that may be pushing it). I'll try
to get the next (last?) beta version out my early
September. Be warned, I always seem to take twice as
long as I think I will.
1.7) What remains to be done in libpng for the 1.0 release?
Add Tim Wegner's medium memory model support. (Thanks, Tim).
Improve the dithering.
Rewrite libpng.txt, and comment the code better.
Test with a final zlib.
1.8) Can I help?
Sweet words. The most helpful thing you can do for the
1.0 release is help testing on your compiler. After this
release, though, there is a large list of things that ought
to be done to make libpng be more full featured. We'll
talk about this later, as I don't want to add tons of new
code until after 1.0 gets out the door. Please send me
any makefile changes and/or other changes needed to support
your compiler (with a #ifdef for the code changes).
If any of you are good at technical writing, take a look at
libpng.txt and correct it (or rewrite it) as necessary. I
am not a writer, I'm a programmer. It shows.
1.9) Why don't you answer your mail?
I've been busy. I'm sorry about that, and I've got to get
into the habit of at least replying that I got the message.
Also, some of you have sent me problems that I haven't gotten
back to you yet. If it is still a problem, you may want to
give me a short message and remind me that I need to look into
it. Libpng is being tested by more people than I dared hope
(which is a good thing), and I was unprepared to deal with so
many problems so soon.
2) Libpng questions
2.1) Does the order of functions calls matter?
In general, yes. If you stick with the order I used in libpng.txt
and example.c, you should be safe. Be warned, however, that I
messed up the order of png_set_interlace_handling(). It needs
to be called *before* png_start_read_image(), not after. Oops.
The exceptions to this rule are all the png_set ... calls.
They can be done in any order, but they all have to be done
in the area indicated by libpng.txt/example.c.
2.2) When I try to read/write a png image, it doesn't work.
Does zlib's test program work (example.c)? Does libpng's test
program work (testpng.c)? Have you installed the patches at
the end of this FAQ? After all that, if the problem continues,
e-mail me, and we'll try to fix it. If you don't have much
programming experience, be patient about the fix. Code
fragments help, as do *small* images (uuencoded please). If
I don't get back to you right away, give me some time. Thanks.
2.3) Do I have to start with example.c?
No, but most people have had much better luck doing that. Seems
I didn't explain things well in libpng.txt, so things are not
intuitive to people (which could be an api design problem also).
Sometime in the future, we'll revisit the api design, and try
to make it easier to use. I'll rewrite libpng.txt before the
1.0 release.
2.4) What things should I be careful of?
The main gotcha is calling png_read_init() before png_info_init().
Also, always make sure you have a correct setjmp buffer. That's
all I can think of for now.
2.5) Why doesn't example.c compile?
I wrote example.c as a staring point for using libpng. It doesn't
compile because I don't know how you want to do things like allocating
memory to hold the image, so I left that code out. If you go in and
finish it up, it should work fine. The file pngtest.c gives a
very simple example of reading and writing png files.
3) Critical Patches
No known bugs (for now :-).
4) Non-critical patches
No known bugs (for now :-).
That's it so far as I know. If I missed something, yell.
-Guy
makefile
浏览文件 @
0f71645d
...
...
@@ -2,25 +2,26 @@
# 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
CC
=
g
cc
CFLAGS
=
-I
../zlib
-O
2
-Wall
-ansi
-pedantic
LDFLAGS
=
-L
.
-L
../zlib/
-lpng
-lz
-lm
#
RANLIB=ranlib
RANLIB
=
echo
RANLIB
=
ranlib
#
RANLIB=echo
# where make install puts libpng.a and png.h
prefix
=
/
usr
/local
prefix
=
/
home/munet-d2/sun
/local
OBJS
=
png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o
\
pngread.o pngio.o pngwrite.o pngrtran.o pngwtran.o
\
pngmem.o
pngerror.o
pngmem.o
all
:
libpng.a pngtest
libpng.a
:
$(OBJS)
ar rc
$@
$(OBJS)
$(RANLIB)
$@
rcp libpng.a vlsi:bin/lib/libpng.a
pngtest
:
pngtest.o libpng.a
$(CC)
-o
pngtest
$(CCFLAGS)
pngtest.o
$(LDFLAGS)
...
...
@@ -44,7 +45,6 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it.
png.o
:
png.h pngconf.h
pngerror.o
:
png.h pngconf.h
pngio.o
:
png.h pngconf.h
pngmem.o
:
png.h pngconf.h
pngrcb.o
:
png.h pngconf.h
...
...
png.h
浏览文件 @
0f71645d
...
...
@@ -267,7 +267,19 @@ typedef struct png_row_info_struct
The only people who need to care about what is inside of this are the
people who will be modifying the library for their own special needs.
*/
typedef
struct
png_struct_def
typedef
struct
png_struct_def
png_struct
;
typedef
png_struct
FAR
png_structf
;
/* These are the function types for the I/O functions, and the functions which
* modify the default I/O functions to user I/O functions. The png_msg_ptr
* type should match that of user supplied warning and error functions, while
* the png_rw_ptr type should match that of the user read/write data functions.
*/
typedef
void
(
*
png_msg_ptr
)
PNGARG
((
png_struct
*
,
char
*
));
typedef
void
(
*
png_rw_ptr
)
PNGARG
((
png_struct
*
,
png_bytef
*
,
png_uint_32
));
typedef
void
(
*
png_flush_ptr
)
PNGARG
((
png_struct
*
));
struct
png_struct_def
{
jmp_buf
jmpbuf
;
/* used in png_error */
png_byte
mode
;
/* used to determine where we are in the png file */
...
...
@@ -314,6 +326,10 @@ typedef struct png_struct_def
png_uint_32
width
;
/* width of file */
png_uint_32
height
;
/* height of file */
png_uint_32
num_rows
;
/* number of rows in current pass */
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
png_uint_32
flush_dist
;
/* how many rows apart to flush, 0 for no flush */
png_uint_32
flush_rows
;
/* number of rows written since last flush */
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
png_uint_32
rowbytes
;
/* size of row in bytes */
png_uint_32
usr_width
;
/* width of row at start of write */
png_uint_32
iwidth
;
/* interlaced width */
...
...
@@ -322,13 +338,14 @@ typedef struct png_struct_def
png_uint_32
idat_size
;
/* current idat size for read */
png_uint_32
zbuf_size
;
/* size of zbuf */
png_color
*
palette
;
/* files palette */
png_uint_32
do_free
;
/* flags indicating if libpng should free memory */
#if defined(PNG_READ_DITHER_SUPPORTED)
png_bytef
*
palette_lookup
;
/* lookup table for dithering */
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
png_byte
*
gamma_table
;
/* gamma table for 8 bit depth files */
#endif
#if defined(PNG_READ_
BACKGROUND
_SUPPORTED)
#if defined(PNG_READ_
GAMMA
_SUPPORTED)
png_byte
*
gamma_from_1
;
/* converts from 1.0 to screen */
png_byte
*
gamma_to_1
;
/* converts from file to 1.0 */
#endif
...
...
@@ -363,7 +380,7 @@ typedef struct png_struct_def
#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
png_color_8
shift
;
/* shift for significant bit tranformation */
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
#if defined(PNG_READ_GAMMA_SUPPORTED)
|| defined (PNG_READ_sBIT_SUPPORTED)
png_color_8
sig_bit
;
/* significant bits in file */
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
...
...
@@ -375,10 +392,17 @@ typedef struct png_struct_def
#endif
png_row_info
row_info
;
/* used for transformation routines */
z_stream
zstream_struct
;
/* decompression structure */
FILE
*
fp
;
/* used for png_read and png_write */
}
png_struct
;
typedef
png_struct
FAR
png_structf
;
FILE
*
fp
;
/* used for default png_read and png_write */
png_msg_ptr
error_fn
;
/* Function for printing errors and aborting */
png_msg_ptr
warning_fn
;
/* Function for printing warnings */
png_rw_ptr
write_data_fn
;
/* Function for writing output data */
png_rw_ptr
read_data_fn
;
/* Function for reading input data */
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
png_flush_ptr
output_flush_fn
;
/* Function for flushing output */
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
void
*
io_ptr
;
/* Pointer to user supplied struct for I/O functions */
void
*
msg_ptr
;
/* Pointer to user supplied struct for message functions */
};
/* Here are the function definitions most commonly used. This is not
the place to find out how to use libpng. See libpng.txt for the
...
...
@@ -403,7 +427,7 @@ extern void png_write_info PNGARG((png_struct *png_ptr, png_info *info));
/* read the information before the actual image data. */
extern
void
png_read_info
PNGARG
((
png_struct
*
png_ptr
,
png_info
*
info
));
#if defined(PNG_
READ
_tIME_SUPPORTED)
#if defined(PNG_
WRITE
_tIME_SUPPORTED)
/* convert from a struct tm to png_time */
extern
void
png_convert_from_struct_tm
PNGARG
((
png_time
*
ptime
,
struct
tm
*
ttime
));
...
...
@@ -500,6 +524,14 @@ extern void png_set_gamma PNGARG((png_struct *png_ptr, double screen_gamma,
double
default_file_gamma
));
#endif
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
/* Set how many lines between output flushes - 0 for no flushing */
extern
void
png_set_flush
PNGARG
((
png_struct
*
png_ptr
,
int
nrows
));
/* Flush the current PNG output buffer */
extern
void
png_write_flush
PNGARG
((
png_struct
*
png_ptr
));
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
/* optional update palette with requested transformations */
extern
void
png_start_read_image
PNGARG
((
png_struct
*
png_ptr
));
...
...
@@ -570,21 +602,8 @@ extern void png_set_compression_window_bits PNGARG((png_struct *png_ptr,
extern
void
png_set_compression_method
PNGARG
((
png_struct
*
png_ptr
,
int
method
));
/* These next functions are stubs of typical c functions for input/output,
memory, and error handling. They are in the file pngstub.c, and are
set up to be easily modified for users that need to. See the file
pngstub.c for more information */
/* Write the data to whatever output you are using. */
extern
void
png_write_data
PNGARG
((
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
));
/* Read data from whatever input you are using */
extern
void
png_read_data
PNGARG
((
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
));
/* Initialize the input/output for the png file. */
extern
void
png_init_io
PNGARG
((
png_struct
*
png_ptr
,
FILE
*
fp
));
/* These next functions are prototypes of the functions libpng uses for
memory allocation. */
/* Allocate memory in larger chunks. */
extern
voidpf
png_large_malloc
PNGARG
((
png_structf
*
png_ptr
,
png_uint_32
size
));
...
...
@@ -602,12 +621,32 @@ extern void *png_realloc PNGARG((png_struct *png_ptr, void *ptr,
/* free's a pointer allocated by png_malloc() */
extern
void
png_free
PNGARG
((
png_struct
*
png_ptr
,
void
*
ptr
));
/* Fatal error in libpng - can't continue */
extern
void
png_error
PNGARG
((
png_structf
*
png_ptr
,
char
*
error
));
/* Initialize the I/O and message handling for the png file to the defaults. */
extern
void
png_init_io
PNGARG
((
png_struct
*
png_ptr
,
FILE
*
fp
));
/* Replace the error message and abort, and warning functions with user
supplied functions. If no messages are to be printed, NULL can be
supplied for error_fn and warning_fn, although error_fn will still do
a longjmp to the last setjmp location. */
extern
void
png_set_msg_fn
PNGARG
((
png_struct
*
png_ptr
,
void
*
msg_ptr
,
png_msg_ptr
error_fn
,
png_msg_ptr
warning_fn
));
/* Return the user pointer associated with the message functions */
extern
void
*
png_get_msg_ptr
PNGARG
((
png_struct
*
png_ptr
));
/* Non-fatal error in libpng. Can continue, but may have a problem. */
extern
void
png_warning
PNGARG
((
png_struct
*
png_ptr
,
char
*
message
));
/* Replace the default data output functions with a user supplied one(s).
If buffered output is not used, then output_flush_fn can be set to NULL.
if PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
output_flush_fn will be ignored (but must be supplied for compatability). */
extern
void
png_set_write_fn
PNGARG
((
png_struct
*
png_ptr
,
void
*
io_ptr
,
png_rw_ptr
write_data_fn
,
png_flush_ptr
output_flush_fn
));
/* Replace the default data input function with a user supplied one. */
extern
void
png_set_read_fn
PNGARG
((
png_struct
*
png_ptr
,
void
*
io_ptr
,
png_rw_ptr
read_data_fn
));
/* Return the user pointer associated with the I/O functions */
extern
void
*
png_get_io_ptr
PNGARG
((
png_struct
*
png_ptr
));
/* These next functions are used internally in the code. If you use
them, make sure you read and understand the png spec. More information
...
...
@@ -646,6 +685,11 @@ extern void png_warning PNGARG((png_struct *png_ptr, char *message));
#define PNG_GRAY_TO_RGB 0x2000
#define PNG_FILLER 0x4000
/* defines for memory alloced by libpng which should be freed */
#define PNG_FREE_TRANS 0x0001
#define PNG_FREE_HIST 0x0002
#define PNG_FREE_PALETTE 0x0004
/* save typing and make code easier to understand */
#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
abs((int)((c1).green) - (int)((c2).green)) + \
...
...
pngchang.txt
浏览文件 @
0f71645d
...
...
@@ -55,6 +55,16 @@ version 0.8
put all the configurable stuff in pngconf.h
enabled png_set_shift to work with paletted images on read
added png_read_update_info() - updates info structure with transformations
version 0.81
added support for medium memory model
version 0.82
added ability to flush output stream automatically or manually
added ability to change I/O and error functions dynamically
moved pngerror.c into pngio.c to make all the default functions static
when freeing memory in png_read_destroy don't free memory we didn't allocate
added warnings when reading blocks of incorrect length
limit tEXt and zTXt keywords to 80 chars
pngconf.h
浏览文件 @
0f71645d
...
...
@@ -27,7 +27,7 @@
Unless this gets smaller then the size of a row (compressed),
it should not make much difference how big this is. */
#define PNG_ZBUF_SIZE
8192
#define PNG_ZBUF_SIZE
32768
/* While libpng currently uses zlib for it's compression, it has been designed
to stand on it's own. Towards this end, there are two defines that are
...
...
@@ -166,6 +166,7 @@
#define PNG_WRITE_SWAP_SUPPORTED
#define PNG_WRITE_INVERT_SUPPORTED
#define PNG_WRITE_FILLER_SUPPORTED
#define PNG_WRITE_FLUSH_SUPPORTED
/* any chunks you are not interested in, you can undef here. The
ones that allocate memory may be expecially important (hIST,
...
...
pngerror.c
已删除
100644 → 0
浏览文件 @
51f0eb45
/* pngstub.c - stub functions for i/o and memory allocation
libpng 1.0 beta 2 - version 0.81
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
August 24, 1995
This file provides a location for all error handling. Users which
need special error handling 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"
/* 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_structf
*
png_ptr
,
char
*
message
)
{
fprintf
(
stderr
,
"libpng error: %s
\n
"
,
message
);
#ifdef USE_FAR_KEYWORD
{
jmp_buf
jmpbuf
;
png_memcpy
(
jmpbuf
,
png_ptr
->
jmpbuf
,
sizeof
(
jmp_buf
));
longjmp
(
jmpbuf
,
1
);
}
#else
longjmp
(
png_ptr
->
jmpbuf
,
1
);
#endif
}
/* 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
);
}
pngio.c
浏览文件 @
0f71645d
/* png
stub.c - stub functions for i/o and memory allocation
/* png
io.c - default functions for data I/O and error/warning messages
libpng 1.0 beta
2 - version 0.81
libpng 1.0 beta
3 - version 0.82
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
August 24, 1995
Some portions Copyright (C) 1995 Andreas Dilger
Sept 24, 1995
This file provides a location for all input/output. Users which need
special handling are expected to modify the code in this file to meet
their needs. See the instructions at each function. */
special handling are expected to write functions which have the same
arguments as these, and perform similar functions, but possibly have
different I/O methods. Note that you shouldn't change these functions,
but rather write replacement functions and then change them at run
time with png_set_write_fn(...) or png_set_read_fn(...), etc */
#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. */
else, this is a good example of how to do it. 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. */
#ifndef USE_FAR_KEYWORD
void
static
void
png_write_data
(
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
)
{
png_uint_32
check
;
...
...
@@ -34,7 +33,7 @@ png_write_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
check
=
fwrite
(
data
,
1
,
(
png_size_t
)
length
,
png_ptr
->
fp
);
if
(
check
!=
length
)
{
png_error
(
png_ptr
,
"Write E
rror"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Write e
rror"
);
}
}
#else
...
...
@@ -51,7 +50,7 @@ png_write_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
#include <dos.h>
#endif
void
static
void
png_write_data
(
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
)
{
png_uint_32
check
;
...
...
@@ -92,25 +91,22 @@ png_write_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
}
if
(
check
!=
length
)
{
png_error
(
png_ptr
,
"Write E
rror"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Write e
rror"
);
}
}
#endif
/* 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. */
else, this is a good example of how to do it. 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. */
#ifndef USE_FAR_KEYWORD
void
static
void
png_read_data
(
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
)
{
png_uint_32
check
;
...
...
@@ -118,11 +114,11 @@ png_read_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
check
=
fread
(
data
,
1
,
(
size_t
)
length
,
png_ptr
->
fp
);
if
(
check
!=
length
)
{
png_error
(
png_ptr
,
"Read E
rror"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Read e
rror"
);
}
}
#else
void
static
void
png_read_data
(
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
)
{
png_uint_32
check
;
...
...
@@ -163,18 +159,253 @@ png_read_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
}
if
(
check
!=
length
)
{
png_error
(
png_ptr
,
"read E
rror"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Read e
rror"
);
}
}
#endif
/* 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. */
/* This function does nothing, and is used for the write function on
a read, and vice-versa. It is an error if this function is
actually called. */
static
void
png_empty_rw
(
png_struct
*
png_ptr
,
png_bytef
*
data
,
png_uint_32
length
)
{
if
(
png_ptr
->
read_data_fn
==
png_empty_rw
)
{
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Invalid read on a write device"
);
}
else
/* if (png_ptr->write_data_fn == png_empty_rw) */
{
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Invalid write on a read device"
);
}
}
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
/* This function does nothing, and is supplied for user I/O functions which
do not do any buffering. This function is set when NULL is supplied for
the flush function pointer. */
static
void
png_empty_flush
(
png_struct
*
png_ptr
)
{
}
/* Write out any remaining output that is stored in the output buffers.
If you supply a new write routine, you will probably need to supply a
replacement for this routine as well. */
static
void
png_output_flush
(
png_struct
*
png_ptr
)
{
fflush
(
png_ptr
->
fp
);
}
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
/* This function does nothing, and is called if the user supplies null
when setting the error function. */
static
void
png_empty_error
(
png_structf
*
png_ptr
,
char
*
message
)
{
#ifdef USE_FAR_KEYWORD
{
jmp_buf
jmpbuf
;
png_memcpy
(
jmpbuf
,
png_ptr
->
jmpbuf
,
sizeof
(
jmp_buf
));
longjmp
(
jmpbuf
,
1
);
}
#else
longjmp
(
png_ptr
->
jmpbuf
,
1
);
#endif
}
/* This function is called whenever there is an error. Replace with a
function you wish to handle the error. Note that this function
MUST NOT return, or the program will crash. To be consistent with
the examples for the library and this function, you could call
longjmp(pnt_ptr->jmpbuf) to return to the program at the location of
the last setjmp(png_ptr->jmpbuf) after an error. */
static
void
png_error
(
png_structf
*
png_ptr
,
char
*
message
)
{
fprintf
(
stderr
,
"libpng error: %s
\n
"
,
message
);
png_empty_error
(
png_ptr
,
message
);
}
/* This function does nothing, and is called if the user supplies null
when setting the warning function. */
static
void
png_empty_warning
(
png_struct
*
png_ptr
,
char
*
message
)
{
}
/* 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. */
static
void
png_warning
(
png_struct
*
png_ptr
,
char
*
message
)
{
fprintf
(
stderr
,
"libpng warning: %s
\n
"
,
message
);
}
/* This function allows the application to supply new output functions for
libpng if standard C streams aren't being used. The new write function
should call (*(png_ptr->error_fn))("Error message") to exit and output
any fatal error messages if it cannot output the correct number of bytes.
This function takes as its arguments:
png_ptr - pointer to a png output data structure
io_ptr - pointer to user supplied structure containing info about
the output functions. May be NULL.
write_data_fn - pointer to a new output function which takes as its
arguments a pointer to a png_struct, a pointer to
data to be written, and a 32-bit unsigned int which is
the number of bytes to be written. The new write
function should call (*(png_ptr->error_fn))("Error msg")
to exit and output any fatal error messages.
flush_data_fn - pointer to a new flush function which takes as its
arguments a pointer to a png_struct. After a call to
the flush function, there should be no data in any buffers
or pending transmission. If the output method doesn't do
any buffering of ouput, this parameter can be NULL. If
PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng
compile time, output_flush_fn will be ignored, although
it must be supplied for compatibility. */
void
png_set_write_fn
(
png_struct
*
png_ptr
,
void
*
io_ptr
,
png_rw_ptr
write_data_fn
,
png_flush_ptr
output_flush_fn
)
{
png_ptr
->
io_ptr
=
io_ptr
;
if
(
write_data_fn
==
NULL
)
{
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"NULL write function pointer given"
);
}
png_ptr
->
write_data_fn
=
write_data_fn
;
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
if
(
output_flush_fn
==
NULL
)
{
png_ptr
->
output_flush_fn
=
png_empty_flush
;
}
else
{
png_ptr
->
output_flush_fn
=
output_flush_fn
;
}
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
/* It is an error to write to a read device */
png_ptr
->
read_data_fn
=
png_empty_rw
;
}
/* This function allows the application to supply a new input function
for libpng if standard C streams aren't being used. The new read function
should call (*(png_ptr->error_fn))("Error message") to exit and output
any fatal error messages if the desired number of bytes is not available.
This function takes as its arguments:
png_ptr - pointer to a png input data structure
io_ptr - pointer to user supplied structure containing info about
the input functions. May be NULL.
read_data_fn - pointer to a new input function which takes as it's
arguments a pointer to a png_struct, a pointer to
a location where input data can be stored, and a 32-bit
unsigned int which is the number of bytes to be read. */
void
png_set_read_fn
(
png_struct
*
png_ptr
,
void
*
io_ptr
,
png_rw_ptr
read_data_fn
)
{
png_ptr
->
io_ptr
=
io_ptr
;
if
(
read_data_fn
==
NULL
)
{
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"NULL read function pointer given"
);
}
png_ptr
->
read_data_fn
=
read_data_fn
;
/* It is an error to write to a read device */
png_ptr
->
write_data_fn
=
png_empty_rw
;
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
png_ptr
->
output_flush_fn
=
png_empty_flush
;
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
}
/* This function returns a pointer to the io_ptr associated with the user
functions. The application should free any memory associated with this
pointer before png_write_destroy and png_read_destroy are called. */
void
*
png_get_io_ptr
(
png_struct
*
png_ptr
)
{
return
png_ptr
->
io_ptr
;
}
/* This function is called when the application wants to use another
method of handling errors and warnings. Note that the error function must
NOT return to the calling routine or serious problems will occur. The
error return method used in the default routine calls
longjmp(png_ptr->jmpbuf, 1), or exits if setjmp(png_ptr->jmpbuf) has never
been called. */
void
png_set_msg_fn
(
png_struct
*
png_ptr
,
void
*
msg_ptr
,
png_msg_ptr
error_fn
,
png_msg_ptr
warning_fn
)
{
png_ptr
->
msg_ptr
=
msg_ptr
;
if
(
error_fn
==
NULL
)
{
png_ptr
->
error_fn
=
png_empty_error
;
}
else
{
png_ptr
->
error_fn
=
error_fn
;
}
if
(
warning_fn
==
NULL
)
{
png_ptr
->
warning_fn
=
png_empty_warning
;
}
else
{
png_ptr
->
warning_fn
=
warning_fn
;
}
}
/* This function returns a pointer to the msg_ptr associated with the user
functions. The application should free any memory associated with this
pointer before png_write_destroy and png_read_destroy are called. */
void
*
png_get_msg_ptr
(
png_struct
*
png_ptr
)
{
return
png_ptr
->
msg_ptr
;
}
/* Initialize the default input/output functions for the png file.
If you change the read, write or message routines, can call
either png_set_read_fn(...), png_set_write_fn(...), etc. to change
individual functions after a call to png_init_io(...). You can also
call one of png_set_read_fn(...) or png_set_write_fn(...), AND
png_set_msg_fn(...) instead of png_init_io(...) if you aren't using
any of the default libpng functions. */
void
png_init_io
(
png_struct
*
png_ptr
,
FILE
*
fp
)
{
png_ptr
->
fp
=
fp
;
}
png_ptr
->
error_fn
=
png_error
;
png_ptr
->
warning_fn
=
png_warning
;
png_ptr
->
write_data_fn
=
png_write_data
;
png_ptr
->
read_data_fn
=
png_read_data
;
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
png_ptr
->
output_flush_fn
=
png_output_flush
;
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
}
pngmem.c
浏览文件 @
0f71645d
/* png
stub.c - stub functions for i/o and
memory allocation
/* png
mem.c - stub functions for
memory allocation
libpng 1.0 beta 2 - version 0.81
For conditions of distribution and use, see copyright notice in png.h
...
...
@@ -61,7 +61,7 @@ png_large_malloc(png_structf *png_ptr, png_uint_32 size)
#ifdef PNG_MAX_MALLOC_64K
if
(
size
>
(
png_uint_32
)
65536L
)
png_error
(
png_ptr
,
"Cannot Allocate > 64K"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Cannot Allocate > 64K"
);
#endif
#ifdef __TURBOC__
...
...
@@ -99,11 +99,11 @@ png_large_malloc(png_structf *png_ptr, png_uint_32 size)
num_save_array
=
1
;
save_array
=
malloc
(
num_save_array
*
sizeof
(
borland_seg
));
if
(
!
save_array
)
png_error
(
png_ptr
,
"Out of Memory 1"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 1"
);
save_array
->
mem_ptr
=
farmalloc
(
(
unsigned
long
)(
NUM_SEG
)
*
65536L
+
65532L
);
if
(
!
save_array
->
mem_ptr
)
png_error
(
png_ptr
,
"Out of Memory 2"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 2"
);
offset
=
(
unsigned
long
)(
ret
);
offset
&=
0xffffL
;
ptr
=
save_array
->
mem_ptr
;
...
...
@@ -141,11 +141,11 @@ png_large_malloc(png_structf *png_ptr, png_uint_32 size)
save_array
=
realloc
(
save_array
,
(
num_save_array
+
1
)
*
sizeof
(
borland_seg
));
if
(
!
save_array
)
png_error
(
png_ptr
,
"Out of Memory 3"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 3"
);
save_array
[
num_save_array
].
mem_ptr
=
farmalloc
(
(
unsigned
long
)(
NUM_SEG
)
*
65536L
+
65532L
);
if
(
!
save_array
[
num_save_array
].
mem_ptr
)
png_error
(
png_ptr
,
"Out of Memory 4"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 4"
);
offset
=
(
unsigned
long
)(
ret
);
offset
&=
0xffffL
;
ptr
=
save_array
[
num_save_array
].
mem_ptr
;
...
...
@@ -180,7 +180,7 @@ png_large_malloc(png_structf *png_ptr, png_uint_32 size)
if
(
ret
==
NULL
)
{
png_error
(
png_ptr
,
"Out of Memory"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory"
);
}
return
ret
;
...
...
@@ -259,14 +259,14 @@ png_malloc(png_struct *png_ptr, png_uint_32 size)
#ifdef PNG_MAX_MALLOC_64K
if
(
size
>
(
png_uint_32
)
65536L
)
png_error
(
png_ptr
,
"Cannot Allocate > 64K"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Cannot Allocate > 64K"
);
#endif
ret
=
malloc
((
png_size_t
)
size
);
if
(
!
ret
)
{
png_error
(
png_ptr
,
"Out of Memory 6"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 6"
);
}
return
ret
;
...
...
@@ -285,14 +285,14 @@ png_realloc(png_struct *png_ptr, void *ptr, png_uint_32 size,
#ifdef PNG_MAX_MALLOC_64K
if
(
size
>
(
png_uint_32
)
65536L
)
png_error
(
png_ptr
,
"Cannot Allocate > 64K"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Cannot Allocate > 64K"
);
#endif
ret
=
realloc
(
ptr
,
(
png_size_t
)
size
);
if
(
!
ret
)
{
png_error
(
png_ptr
,
"Out of Memory 7"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Memory 7"
);
}
return
ret
;
...
...
pngread.c
浏览文件 @
0f71645d
...
...
@@ -38,22 +38,22 @@ png_read_info(png_struct *png_ptr, png_info *info)
png_byte
chunk_start
[
8
];
png_uint_32
length
;
png_read_data
(
png_ptr
,
chunk_start
,
8
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
8
);
if
(
memcmp
(
chunk_start
,
png_sig
,
8
))
png_error
(
png_ptr
,
"Not a Png
File"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Not a PNG
File"
);
while
(
1
)
{
png_uint_32
crc
;
png_read_data
(
png_ptr
,
chunk_start
,
8
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
8
);
length
=
png_get_uint_32
(
chunk_start
);
png_reset_crc
(
png_ptr
);
png_calculate_crc
(
png_ptr
,
chunk_start
+
4
,
4
);
if
(
!
memcmp
(
chunk_start
+
4
,
png_IHDR
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_BEFORE_IHDR
)
png_error
(
png_ptr
,
"Out of Place IHDR"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place IHDR"
);
png_handle_IHDR
(
png_ptr
,
info
,
length
);
png_ptr
->
mode
=
PNG_HAVE_IHDR
;
...
...
@@ -61,7 +61,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_PLTE
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
)
png_error
(
png_ptr
,
"Missing IHDR"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Missing IHDR"
);
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
if
(
png_ptr
->
color_type
!=
PNG_COLOR_TYPE_PALETTE
)
...
...
@@ -82,13 +82,13 @@ png_read_info(png_struct *png_ptr, png_info *info)
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_IEND
,
4
))
{
png_error
(
png_ptr
,
"No Image in File"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"No Image in File"
);
}
#if defined(PNG_READ_gAMA_SUPPORTED)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_gAMA
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
)
png_error
(
png_ptr
,
"Out of Place PLTE"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place PLTE"
);
png_handle_gAMA
(
png_ptr
,
info
,
length
);
}
...
...
@@ -97,7 +97,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_sBIT
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
)
png_error
(
png_ptr
,
"Out of Place sBIT"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place sBIT"
);
png_handle_sBIT
(
png_ptr
,
info
,
length
);
}
...
...
@@ -106,7 +106,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_cHRM
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
)
png_error
(
png_ptr
,
"Out of Place cHRM"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place cHRM"
);
png_handle_cHRM
(
png_ptr
,
info
,
length
);
}
...
...
@@ -116,7 +116,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
&&
png_ptr
->
mode
!=
PNG_HAVE_PLTE
)
png_error
(
png_ptr
,
"Out of Place tRNS"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place tRNS"
);
png_handle_tRNS
(
png_ptr
,
info
,
length
);
}
...
...
@@ -126,7 +126,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
&&
png_ptr
->
mode
!=
PNG_HAVE_PLTE
)
png_error
(
png_ptr
,
"Out of Place bKGD"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place bKGD"
);
png_handle_bKGD
(
png_ptr
,
info
,
length
);
}
...
...
@@ -135,7 +135,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_hIST
,
4
))
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_PLTE
)
png_error
(
png_ptr
,
"Out of Place hIST"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place hIST"
);
png_handle_hIST
(
png_ptr
,
info
,
length
);
}
...
...
@@ -145,7 +145,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
&&
png_ptr
->
mode
!=
PNG_HAVE_PLTE
)
png_error
(
png_ptr
,
"Out of Place pHYs"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place pHYs"
);
png_handle_pHYs
(
png_ptr
,
info
,
length
);
}
...
...
@@ -155,7 +155,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
!=
PNG_HAVE_IHDR
&&
png_ptr
->
mode
!=
PNG_HAVE_PLTE
)
png_error
(
png_ptr
,
"Out of Place oFFs"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place oFFs"
);
png_handle_oFFs
(
png_ptr
,
info
,
length
);
}
...
...
@@ -165,7 +165,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place tIME"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place tIME"
);
png_handle_tIME
(
png_ptr
,
info
,
length
);
}
...
...
@@ -175,7 +175,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place tEXt"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place tEXt"
);
png_handle_tEXt
(
png_ptr
,
info
,
length
);
}
...
...
@@ -185,7 +185,7 @@ png_read_info(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place zTXt"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place zTXt"
);
png_handle_zTXt
(
png_ptr
,
info
,
length
);
}
...
...
@@ -193,15 +193,15 @@ png_read_info(png_struct *png_ptr, png_info *info)
else
{
if
((
chunk_start
[
4
]
&
0x20
)
==
0
)
png_error
(
png_ptr
,
"Unknown Critical Chunk"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Unknown Critical Chunk"
);
png_crc_skip
(
png_ptr
,
length
);
}
png_read_data
(
png_ptr
,
chunk_start
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
4
);
crc
=
png_get_uint_32
(
chunk_start
);
if
(((
crc
^
0xffffffffL
)
&
0xffffffffL
)
!=
(
png_ptr
->
crc
&
0xffffffffL
))
png_error
(
png_ptr
,
"Bad CRC value"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Bad CRC value"
);
}
}
...
...
@@ -310,7 +310,7 @@ png_read_row(png_struct *png_ptr, png_bytef *row, png_byte *dsp_row)
#endif
if
(
png_ptr
->
mode
!=
PNG_HAVE_IDAT
)
png_error
(
png_ptr
,
"i
nvalid attempt to read row data"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid attempt to read row data"
);
png_ptr
->
zstream
->
next_out
=
png_ptr
->
row_buf
;
png_ptr
->
zstream
->
avail_out
=
(
uInt
)
png_ptr
->
irowbytes
;
...
...
@@ -323,19 +323,19 @@ png_read_row(png_struct *png_ptr, png_bytef *row, png_byte *dsp_row)
png_byte
buf
[
4
];
png_uint_32
crc
;
png_read_data
(
png_ptr
,
buf
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
buf
,
4
);
crc
=
png_get_uint_32
(
buf
);
if
(((
crc
^
0xffffffffL
)
&
0xffffffffL
)
!=
(
png_ptr
->
crc
&
0xffffffffL
))
png_error
(
png_ptr
,
"Bad CRC value"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Bad CRC value"
);
png_read_data
(
png_ptr
,
buf
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
buf
,
4
);
png_ptr
->
idat_size
=
png_get_uint_32
(
buf
);
png_reset_crc
(
png_ptr
);
png_crc_read
(
png_ptr
,
buf
,
4
);
if
(
memcmp
(
buf
,
png_IDAT
,
4
))
png_error
(
png_ptr
,
"Not enough image data"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Not enough image data"
);
}
png_ptr
->
zstream
->
avail_in
=
(
uInt
)
png_ptr
->
zbuf_size
;
...
...
@@ -350,12 +350,12 @@ png_read_row(png_struct *png_ptr, png_bytef *row, png_byte *dsp_row)
{
if
(
png_ptr
->
zstream
->
avail_out
||
png_ptr
->
zstream
->
avail_in
||
png_ptr
->
idat_size
)
png_error
(
png_ptr
,
"Extra compressed data"
);
(
*
(
png_ptr
->
warning_fn
))
(
png_ptr
,
"Extra compressed data"
);
png_ptr
->
mode
=
PNG_AT_LAST_IDAT
;
break
;
}
if
(
ret
!=
Z_OK
)
png_error
(
png_ptr
,
"Compression Error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Compression Error"
);
}
while
(
png_ptr
->
zstream
->
avail_out
);
...
...
@@ -433,31 +433,32 @@ png_read_rows(png_struct *png_ptr, png_bytef **row,
png_bytef
**
rp
;
png_byte
**
dp
;
rp
=
row
;
dp
=
display_row
;
for
(
i
=
0
;
i
<
num_rows
;
i
++
)
{
png_bytef
*
rptr
;
png_byte
*
dptr
;
if
(
rp
)
rptr
=
*
rp
;
else
rptr
=
NULL
;
if
(
dp
)
dptr
=
*
dp
;
else
dptr
=
NULL
;
png_read_row
(
png_ptr
,
rptr
,
dptr
);
if
(
row
)
rp
++
;
if
(
display_row
)
dp
++
;
}
rp
=
row
;
dp
=
display_row
;
for
(
i
=
0
;
i
<
num_rows
;
i
++
)
{
png_bytef
*
rptr
;
png_byte
*
dptr
;
if
(
rp
)
rptr
=
*
rp
;
else
rptr
=
NULL
;
if
(
dp
)
dptr
=
*
dp
;
else
dptr
=
NULL
;
png_read_row
(
png_ptr
,
rptr
,
dptr
);
if
(
row
)
rp
++
;
if
(
display_row
)
dp
++
;
}
}
/* read the image. If the image has an alpha channel or a transparency
chunk, and you have called png_handle_alpha(), you will need to
chunk, and you have called png_handle_alpha(), you will need to
initialize the image to the current image that png will be overlaying.
Note that png_set_interlace_handling() has no effect on this call.
You only need to call this function once. If you desire to have
...
...
@@ -491,70 +492,70 @@ png_read_end(png_struct *png_ptr, png_info *info)
png_uint_32
length
;
png_uint_32
crc
;
png_read_data
(
png_ptr
,
chunk_start
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
4
);
crc
=
png_get_uint_32
(
chunk_start
);
if
(((
crc
^
0xffffffffL
)
&
0xffffffffL
)
!=
(
png_ptr
->
crc
&
0xffffffffL
))
png_error
(
png_ptr
,
"Bad CRC value"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Bad CRC value"
);
do
{
png_read_data
(
png_ptr
,
chunk_start
,
8
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
8
);
length
=
png_get_uint_32
(
chunk_start
);
png_reset_crc
(
png_ptr
);
png_calculate_crc
(
png_ptr
,
chunk_start
+
4
,
4
);
if
(
!
memcmp
(
chunk_start
+
4
,
png_IHDR
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_PLTE
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_gAMA
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_sBIT
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_cHRM
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_tRNS
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_bKGD
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_hIST
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_IDAT
,
4
))
{
if
(
length
>
0
||
png_ptr
->
mode
!=
PNG_AT_LAST_IDAT
)
png_error
(
png_ptr
,
"t
oo many IDAT's found"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"T
oo many IDAT's found"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_pHYs
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_oFFs
,
4
))
{
png_error
(
png_ptr
,
"i
nvalid chunk after IDAT"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"I
nvalid chunk after IDAT"
);
}
#if defined(PNG_READ_tIME_SUPPORTED)
else
if
(
!
memcmp
(
chunk_start
+
4
,
png_tIME
,
4
))
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place tIME"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place tIME"
);
if
(
info
)
png_handle_tIME
(
png_ptr
,
info
,
length
);
...
...
@@ -567,7 +568,7 @@ png_read_end(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place tEXt"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place tEXt"
);
if
(
info
)
png_handle_tEXt
(
png_ptr
,
info
,
length
);
...
...
@@ -580,7 +581,7 @@ png_read_end(png_struct *png_ptr, png_info *info)
{
if
(
png_ptr
->
mode
==
PNG_BEFORE_IHDR
||
png_ptr
->
mode
==
PNG_AFTER_IEND
)
png_error
(
png_ptr
,
"Out of Place zTXt"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Out of Place zTXt"
);
if
(
info
)
png_handle_zTXt
(
png_ptr
,
info
,
length
);
...
...
@@ -594,16 +595,25 @@ png_read_end(png_struct *png_ptr, png_info *info)
}
else
{
char
msg
[
80
];
if
((
chunk_start
[
4
]
&
0x20
)
==
0
)
png_error
(
png_ptr
,
"Unknown Critical Chunk"
);
{
png_strcpy
(
msg
,
"Unknown critical chunk "
);
png_memcpy
(
msg
+
strlen
(
msg
),
chunk_start
+
4
,
4
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
msg
);
}
png_strcpy
(
msg
,
"Unknown extension chunk "
);
png_memcpy
(
msg
+
strlen
(
msg
),
chunk_start
+
4
,
4
);
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
msg
);
png_crc_skip
(
png_ptr
,
length
);
}
png_read_data
(
png_ptr
,
chunk_start
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
chunk_start
,
4
);
crc
=
png_get_uint_32
(
chunk_start
);
if
(((
crc
^
0xffffffffL
)
&
0xffffffffL
)
!=
(
png_ptr
->
crc
&
0xffffffffL
))
png_error
(
png_ptr
,
"Bad CRC value"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Bad CRC value"
);
if
(
png_ptr
->
mode
==
PNG_AT_LAST_IDAT
)
png_ptr
->
mode
=
PNG_AFTER_IDAT
;
}
while
(
png_ptr
->
mode
!=
PNG_AFTER_IEND
);
...
...
@@ -618,19 +628,19 @@ png_read_destroy(png_struct *png_ptr, png_info *info, png_info *end_info)
if
(
info
)
{
/* I'm not sure I should be freeing this */
if
(
info
->
palette
!=
png_ptr
->
palette
)
if
(
png_ptr
->
do_free
&
PNG_FREE_PALETTE
)
png_free
(
png_ptr
,
info
->
palette
);
/* I'm not sure I should be freeing this */
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_bKGD_SUPPORTED)
if
(
info
->
trans
!=
png_ptr
->
trans
)
if
(
png_ptr
->
do_free
&
PNG_FREE_TRANS
)
png_free
(
png_ptr
,
info
->
trans
);
#endif
/* I'm not sure I should be freeing this */
#if defined(PNG_READ_hIST_SUPPORTED)
if
(
info
->
hist
!=
png_ptr
->
hist
)
if
(
png_ptr
->
do_free
&
PNG_FREE_HIST
)
png_free
(
png_ptr
,
info
->
hist
);
#endif
#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
for
(
i
=
0
;
i
<
info
->
num_text
;
i
++
)
{
...
...
@@ -658,17 +668,21 @@ png_read_destroy(png_struct *png_ptr, png_info *info, png_info *end_info)
png_large_free
(
png_ptr
,
png_ptr
->
zbuf
);
png_large_free
(
png_ptr
,
png_ptr
->
row_buf
);
png_large_free
(
png_ptr
,
png_ptr
->
prev_row
);
#if defined(PNG_READ_DITHER_SUPPORTED)
png_large_free
(
png_ptr
,
png_ptr
->
palette_lookup
);
png_free
(
png_ptr
,
png_ptr
->
dither_index
);
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
png_free
(
png_ptr
,
png_ptr
->
gamma_table
);
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
png_free
(
png_ptr
,
png_ptr
->
gamma_from_1
);
png_free
(
png_ptr
,
png_ptr
->
gamma_to_1
);
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
if
(
png_ptr
->
gamma_16_table
)
{
...
...
@@ -678,6 +692,7 @@ png_read_destroy(png_struct *png_ptr, png_info *info, png_info *end_info)
}
}
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
png_free
(
png_ptr
,
png_ptr
->
gamma_16_table
);
if
(
png_ptr
->
gamma_16_from_1
)
...
...
@@ -697,12 +712,15 @@ png_read_destroy(png_struct *png_ptr, png_info *info, png_info *end_info)
}
png_free
(
png_ptr
,
png_ptr
->
gamma_16_to_1
);
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
png_free
(
png_ptr
,
png_ptr
->
trans
);
#endif
#if defined(PNG_READ_DITHER_SUPPORTED)
png_free
(
png_ptr
,
png_ptr
->
hist
);
#endif
if
(
!
png_ptr
->
user_palette
)
png_free
(
png_ptr
,
png_ptr
->
palette
);
...
...
pngrtran.c
浏览文件 @
0f71645d
...
...
@@ -572,7 +572,7 @@ png_init_read_transformations(png_struct *png_ptr)
}
#endif
#if defined(PNG_READ_SHIFT_SUPPORTED)
#if defined(PNG_READ_SHIFT_SUPPORTED)
&& defined(PNG_READ_sBIT_SUPPORTED)
if
((
png_ptr
->
transformations
&
PNG_SHIFT
)
&&
png_ptr
->
color_type
==
PNG_COLOR_TYPE_PALETTE
)
{
...
...
@@ -876,19 +876,33 @@ png_do_unshift(png_row_info *row_info, png_bytef *row,
channels
=
0
;
if
(
row_info
->
color_type
&
PNG_COLOR_MASK_COLOR
)
{
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
red
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
green
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
blue
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
red
<
0
?
0
:
row_info
->
bit_depth
-
sig_bits
->
red
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
green
<
0
?
0
:
row_info
->
bit_depth
-
sig_bits
->
green
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
blue
<
0
?
0
:
row_info
->
bit_depth
-
sig_bits
->
blue
;
}
else
{
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
gray
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
gray
<
0
?
0
:
row_info
->
bit_depth
-
sig_bits
->
gray
;
}
if
(
row_info
->
color_type
&
PNG_COLOR_MASK_ALPHA
)
{
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
alpha
;
shift
[
channels
++
]
=
row_info
->
bit_depth
-
sig_bits
->
alpha
<
0
?
0
:
row_info
->
bit_depth
-
sig_bits
->
alpha
;
}
value
=
1
;
for
(
i
=
0
;
i
<
channels
;
i
++
)
{
if
(
shift
[
i
]
!=
0
)
value
=
0
;
}
if
(
value
==
1
)
return
;
switch
(
row_info
->
bit_depth
)
{
case
2
:
...
...
@@ -905,6 +919,7 @@ png_do_unshift(png_row_info *row_info, png_bytef *row,
case
4
:
{
png_byte
mask
;
mask
=
(
png_byte
)(((
int
)
0xf0
>>
shift
[
0
])
&
(
int
)
0xf0
)
|
((
int
)
0xf
>>
shift
[
0
]);
for
(
bp
=
row
,
i
=
0
;
...
...
@@ -957,17 +972,15 @@ png_do_unshift(png_row_info *row_info, png_bytef *row,
void
png_do_chop
(
png_row_info
*
row_info
,
png_bytef
*
row
)
{
png_bytef
*
sp
,
*
dp
;
png_uint_32
i
;
if
(
row
&&
row_info
&&
row_info
->
bit_depth
==
16
)
{
sp
=
row
+
2
;
dp
=
row
+
1
;
for
(
i
=
1
;
i
<
row_info
->
width
*
row_info
->
channels
;
i
++
)
png_bytef
*
sp
=
row
,
*
dp
=
row
;
png_uint_32
i
;
for
(
i
=
0
;
i
<
row_info
->
width
*
row_info
->
channels
;
i
++
)
{
*
dp
=
*
sp
;
sp
+=
2
;
dp
++
;
*
dp
++
=
((((
*
sp
<<
8
|
*
(
sp
+
1
))
-
*
sp
)
+
0x7F
)
>>
8
)
&
0xFF
;
sp
+=
2
;
}
row_info
->
bit_depth
=
8
;
row_info
->
pixel_depth
=
8
*
row_info
->
channels
;
...
...
@@ -1147,6 +1160,7 @@ png_build_grayscale_palette(int bit_depth, png_color *palette)
break
;
default:
num_palette
=
0
;
color_inc
=
0
;
break
;
}
...
...
@@ -2002,7 +2016,7 @@ png_do_background(png_row_info *row_info, png_bytef *row,
you do this after you deal with the trasparency issue on grayscale
or rgb images. If your bit depth is 8, use gamma_table, if it is 16,
use gamma_16_table and gamma_shift. Build these with
build_gamma_table(). If your bit depth < 8, gamma correct a
build_gamma_table(). If your bit depth <
=
8, gamma correct a
palette, not the data. */
void
png_do_gamma
(
png_row_info
*
row_info
,
png_bytef
*
row
,
...
...
@@ -2623,8 +2637,9 @@ png_build_gamma_table(png_struct *png_ptr)
{
double
g
;
int
i
,
j
,
shift
,
num
;
int
sig_bit
;
png_uint_32
ig
;
#if defined(PNG_READ_sBIT_SUPPORTED)
int
sig_bit
;
if
(
png_ptr
->
color_type
&
PNG_COLOR_MASK_COLOR
)
{
...
...
@@ -2642,6 +2657,7 @@ png_build_gamma_table(png_struct *png_ptr)
if
(
sig_bit
>
0
)
shift
=
16
-
sig_bit
;
else
#endif
/* PNG_READ_sBIT_SUPPORTED */
shift
=
0
;
if
(
png_ptr
->
transformations
&
PNG_16_TO_8
)
...
...
@@ -2677,26 +2693,25 @@ png_build_gamma_table(png_struct *png_ptr)
}
g
=
1
.
0
/
g
;
last
=
0
;
for
(
i
=
0
;
i
<
256
;
i
++
)
last
=
0
;
for
(
i
=
0
;
i
<
256
;
i
++
)
{
fout
=
((
double
)
i
+
0
.
5
)
/
256
.
0
;
fin
=
pow
(
fout
,
g
);
max
=
(
png_uint_32
)(
fin
*
(
double
)(
num
<<
8
));
while
(
last
<=
max
)
fout
=
((
double
)
i
+
0
.
5
)
/
256
.
0
;
fin
=
pow
(
fout
,
g
);
max
=
(
png_uint_32
)(
fin
*
(
double
)(
num
<<
8
));
while
(
last
<=
max
)
{
png_ptr
->
gamma_16_table
[(
int
)(
last
>>
8
)]
[(
int
)(
last
&
0xff
)]
=
png_ptr
->
gamma_16_table
[(
int
)(
last
>>
8
)][(
int
)(
last
&
0xff
)]
=
(
png_uint_16
)
i
|
((
png_uint_16
)
i
<<
8
);
last
++
;
}
}
while
(
last
<
(
num
<<
8
))
{
png_ptr
->
gamma_16_table
[(
int
)(
last
>>
8
)][(
int
)(
last
&
0xff
)]
=
(
png_uint_16
)
65535L
;
last
++
;
}
}
while
(
last
<
(
num
<<
8
))
{
png_ptr
->
gamma_16_table
[(
int
)(
last
>>
8
)][(
int
)(
last
&
0xff
)]
=
(
png_uint_16
)
65535L
;
last
++
;
}
}
else
{
...
...
pngrutil.c
浏览文件 @
0f71645d
...
...
@@ -40,7 +40,7 @@ png_get_uint_16(png_byte *buf)
void
png_crc_read
(
png_struct
*
png_ptr
,
png_bytef
*
buf
,
png_uint_32
length
)
{
png_read_data
(
png_ptr
,
buf
,
length
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
buf
,
length
);
png_calculate_crc
(
png_ptr
,
buf
,
length
);
}
...
...
@@ -52,12 +52,12 @@ png_crc_skip(png_struct *png_ptr, png_uint_32 length)
for
(
i
=
length
;
i
>
png_ptr
->
zbuf_size
;
i
-=
png_ptr
->
zbuf_size
)
{
png_read_data
(
png_ptr
,
png_ptr
->
zbuf
,
png_ptr
->
zbuf_size
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
png_ptr
->
zbuf
,
png_ptr
->
zbuf_size
);
png_calculate_crc
(
png_ptr
,
png_ptr
->
zbuf
,
png_ptr
->
zbuf_size
);
}
if
(
i
)
{
png_read_data
(
png_ptr
,
png_ptr
->
zbuf
,
i
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
png_ptr
->
zbuf
,
i
);
png_calculate_crc
(
png_ptr
,
png_ptr
->
zbuf
,
i
);
}
}
...
...
@@ -73,7 +73,7 @@ png_handle_IHDR(png_struct *png_ptr, png_info *info, png_uint_32 length)
/* check the length */
if
(
length
!=
13
)
png_error
(
png_ptr
,
"Invalid IHDR chunk"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid IHDR chunk"
);
png_crc_read
(
png_ptr
,
buf
,
13
);
...
...
@@ -87,36 +87,36 @@ png_handle_IHDR(png_struct *png_ptr, png_info *info, png_uint_32 length)
/* check for width and height valid values */
if
(
width
==
0
||
height
==
0
)
png_error
(
png_ptr
,
"Invalid Width or Height Found"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid Width or Height Found"
);
/* check other values */
if
(
bit_depth
!=
1
&&
bit_depth
!=
2
&&
bit_depth
!=
4
&&
bit_depth
!=
8
&&
bit_depth
!=
16
)
png_error
(
png_ptr
,
"Invalid Bit Depth Found"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid Bit Depth Found"
);
if
(
color_type
<
0
||
color_type
==
1
||
color_type
==
5
||
color_type
>
6
)
png_error
(
png_ptr
,
"Invalid Color Type Found"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid Color Type Found"
);
if
(
color_type
==
PNG_COLOR_TYPE_PALETTE
&&
bit_depth
==
16
)
png_error
(
png_ptr
,
"Found Invalid Color Type and Bit Depth Combination"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Found Invalid Color Type and Bit Depth Combination"
);
if
((
color_type
==
PNG_COLOR_TYPE_RGB
||
color_type
==
PNG_COLOR_TYPE_GRAY_ALPHA
||
color_type
==
PNG_COLOR_TYPE_RGB_ALPHA
)
&&
bit_depth
<
8
)
png_error
(
png_ptr
,
"Found Invalid Color Type and Bit Depth Combination"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Found Invalid Color Type and Bit Depth Combination"
);
if
(
interlace_type
>
1
)
png_error
(
png_ptr
,
"Found Invali
d Interlace Value"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Found Unsupporte
d Interlace Value"
);
if
(
compression_type
>
0
)
png_error
(
png_ptr
,
"Found Invali
d Compression Value"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Found Unsupporte
d Compression Value"
);
if
(
filter_type
>
0
)
png_error
(
png_ptr
,
"Found Invali
d Filter Value"
);
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"Found Unsupporte
d Filter Value"
);
/* set internal variables */
png_ptr
->
width
=
width
;
...
...
@@ -160,10 +160,12 @@ png_handle_PLTE(png_struct *png_ptr, png_info *info, png_uint_32 length)
png_color
*
palette
;
if
(
length
%
3
)
png_error
(
png_ptr
,
"Invalid Palette Chunk"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid Palette Chunk"
);
num
=
(
int
)
length
/
3
;
palette
=
(
png_color
*
)
png_malloc
(
png_ptr
,
num
*
sizeof
(
png_color
));
png_ptr
->
do_free
|=
PNG_FREE_PALETTE
;
for
(
i
=
0
;
i
<
num
;
i
++
)
{
png_byte
buf
[
3
];
...
...
@@ -189,6 +191,7 @@ png_handle_gAMA(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
4
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect gAMA chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -210,6 +213,7 @@ void
png_handle_sBIT
(
png_struct
*
png_ptr
,
png_info
*
info
,
png_uint_32
length
)
{
int
slen
;
register
int
index
=
0
;
png_byte
buf
[
4
];
if
(
png_ptr
->
color_type
==
PNG_COLOR_TYPE_PALETTE
)
...
...
@@ -219,6 +223,7 @@ png_handle_sBIT(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
(
png_uint_32
)
slen
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect sBIT chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -226,16 +231,28 @@ png_handle_sBIT(png_struct *png_ptr, png_info *info, png_uint_32 length)
png_crc_read
(
png_ptr
,
buf
,
length
);
if
(
png_ptr
->
color_type
&
PNG_COLOR_MASK_COLOR
)
{
png_ptr
->
sig_bit
.
red
=
buf
[
0
];
png_ptr
->
sig_bit
.
green
=
buf
[
1
];
png_ptr
->
sig_bit
.
blue
=
buf
[
2
];
png_ptr
->
sig_bit
.
alpha
=
buf
[
3
]
;
png_ptr
->
sig_bit
.
red
=
buf
[
index
++
];
png_ptr
->
sig_bit
.
green
=
buf
[
index
++
];
png_ptr
->
sig_bit
.
blue
=
buf
[
index
++
];
png_ptr
->
sig_bit
.
gray
=
0
;
}
else
{
png_ptr
->
sig_bit
.
red
=
0
;
png_ptr
->
sig_bit
.
green
=
0
;
png_ptr
->
sig_bit
.
blue
=
0
;
png_ptr
->
sig_bit
.
gray
=
buf
[
0
];
png_ptr
->
sig_bit
.
alpha
=
buf
[
1
];
}
if
(
png_ptr
->
color_type
&
PNG_COLOR_MASK_ALPHA
)
{
png_ptr
->
sig_bit
.
alpha
=
buf
[
index
++
];
}
else
{
png_ptr
->
sig_bit
.
alpha
=
0
;
}
png_read_sBIT
(
png_ptr
,
info
,
&
(
png_ptr
->
sig_bit
));
}
#endif
...
...
@@ -250,6 +267,7 @@ png_handle_cHRM(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
32
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect cHRM chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -299,11 +317,13 @@ png_handle_tRNS(png_struct *png_ptr, png_info *info, png_uint_32 length)
{
if
(
length
>
png_ptr
->
num_palette
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect tRNS chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
png_ptr
->
trans
=
png_malloc
(
png_ptr
,
length
);
png_ptr
->
do_free
|=
PNG_FREE_TRANS
;
png_crc_read
(
png_ptr
,
png_ptr
->
trans
,
length
);
png_ptr
->
num_trans
=
(
int
)
length
;
}
...
...
@@ -313,6 +333,7 @@ png_handle_tRNS(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
6
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect tRNS chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -329,6 +350,7 @@ png_handle_tRNS(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
2
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect tRNS chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -338,7 +360,7 @@ png_handle_tRNS(png_struct *png_ptr, png_info *info, png_uint_32 length)
png_ptr
->
trans_values
.
gray
=
png_get_uint_16
(
buf
);
}
else
png_error
(
png_ptr
,
"Invalid tRNS chunk"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Invalid tRNS chunk"
);
png_read_tRNS
(
png_ptr
,
info
,
png_ptr
->
trans
,
png_ptr
->
num_trans
,
&
(
png_ptr
->
trans_values
));
...
...
@@ -361,6 +383,7 @@ png_handle_bKGD(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
(
png_uint_32
)
truelen
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect bKGD chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -389,12 +412,15 @@ png_handle_hIST(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
2
*
png_ptr
->
num_palette
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect hIST chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
num
=
(
int
)
length
/
2
;
png_ptr
->
hist
=
png_malloc
(
png_ptr
,
num
*
sizeof
(
png_uint_16
));
png_ptr
->
do_free
|=
PNG_FREE_HIST
;
for
(
i
=
0
;
i
<
num
;
i
++
)
{
png_byte
buf
[
2
];
...
...
@@ -416,6 +442,7 @@ png_handle_pHYs(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
9
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect pHYS chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -439,6 +466,7 @@ png_handle_oFFs(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
9
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect oFFs chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -461,6 +489,7 @@ png_handle_tIME(png_struct *png_ptr, png_info *info, png_uint_32 length)
if
(
length
!=
7
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Incorrect tIME chunk length"
);
png_crc_skip
(
png_ptr
,
length
);
return
;
}
...
...
@@ -509,7 +538,7 @@ png_handle_zTXt(png_struct *png_ptr, png_info *info, png_uint_32 length)
{
charf
*
key
;
charf
*
text
;
int
ret
;
int
ret
=
Z_STREAM_END
;
png_uint_32
text_size
,
key_size
;
text
=
NULL
;
...
...
@@ -524,6 +553,7 @@ png_handle_zTXt(png_struct *png_ptr, png_info *info, png_uint_32 length)
/* zTXt can't have zero text */
if
(
text
==
key
+
(
png_size_t
)
length
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"Zero length zTXt chunk"
);
png_large_free
(
png_ptr
,
key
);
return
;
}
...
...
@@ -552,6 +582,10 @@ png_handle_zTXt(png_struct *png_ptr, png_info *info, png_uint_32 length)
ret
=
inflate
(
png_ptr
->
zstream
,
Z_PARTIAL_FLUSH
);
if
(
ret
!=
Z_OK
&&
ret
!=
Z_STREAM_END
)
{
if
(
png_ptr
->
zstream
->
msg
)
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"zTXt decompression error"
);
inflateReset
(
png_ptr
->
zstream
);
png_ptr
->
zstream
->
avail_in
=
0
;
png_large_free
(
png_ptr
,
key
);
...
...
@@ -1104,19 +1138,19 @@ png_read_finish_row(png_struct *png_ptr)
png_byte
buf
[
4
];
png_uint_32
crc
;
png_read_data
(
png_ptr
,
buf
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
buf
,
4
);
crc
=
png_get_uint_32
(
buf
);
if
(((
crc
^
0xffffffffL
)
&
0xffffffffL
)
!=
(
png_ptr
->
crc
&
0xffffffffL
))
png_error
(
png_ptr
,
"Bad CRC value"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Bad CRC value"
);
png_read_data
(
png_ptr
,
buf
,
4
);
(
*
(
png_ptr
->
read_data_fn
))
(
png_ptr
,
buf
,
4
);
png_ptr
->
idat_size
=
png_get_uint_32
(
buf
);
png_reset_crc
(
png_ptr
);
png_crc_read
(
png_ptr
,
buf
,
4
);
if
(
memcmp
(
buf
,
png_IDAT
,
4
))
png_error
(
png_ptr
,
"Not enough image data"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Not enough image data"
);
}
png_ptr
->
zstream
->
avail_in
=
(
uInt
)
png_ptr
->
zbuf_size
;
...
...
@@ -1131,22 +1165,22 @@ png_read_finish_row(png_struct *png_ptr)
{
if
(
!
(
png_ptr
->
zstream
->
avail_out
)
||
png_ptr
->
zstream
->
avail_in
||
png_ptr
->
idat_size
)
png_error
(
png_ptr
,
"Extra compressed data"
);
(
*
(
png_ptr
->
warning_fn
))
(
png_ptr
,
"Extra compressed data"
);
png_ptr
->
mode
=
PNG_AT_LAST_IDAT
;
break
;
}
if
(
ret
!=
Z_OK
)
png_error
(
png_ptr
,
"Compression Error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Compression Error"
);
if
(
!
(
png_ptr
->
zstream
->
avail_out
))
png_error
(
png_ptr
,
"Extra compressed data"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"Extra compressed data"
);
}
while
(
1
);
png_ptr
->
zstream
->
avail_out
=
0
;
}
if
(
png_ptr
->
idat_size
||
png_ptr
->
zstream
->
avail_in
)
png_error
(
png_ptr
,
"Extra compression data"
);
(
*
(
png_ptr
->
warning_fn
))
(
png_ptr
,
"Extra compression data"
);
inflateReset
(
png_ptr
->
zstream
);
...
...
@@ -1258,13 +1292,13 @@ png_read_start_row(png_struct *png_ptr)
1
+
((
max_pixel_depth
+
7
)
>>
3
);
#ifdef PNG_MAX_MALLOC_64K
if
(
rowbytes
>
65536L
)
png_error
(
png_ptr
,
"This image requires a row greater then 64KB"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"This image requires a row greater then 64KB"
);
#endif
png_ptr
->
row_buf
=
(
png_bytef
*
)
png_large_malloc
(
png_ptr
,
rowbytes
);
#ifdef PNG_MAX_MALLOC_64K
if
(
png_ptr
->
rowbytes
+
1
>
65536L
)
png_error
(
png_ptr
,
"This image requires a row greater then 64KB"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"This image requires a row greater then 64KB"
);
#endif
png_ptr
->
prev_row
=
png_large_malloc
(
png_ptr
,
png_ptr
->
rowbytes
+
1
);
...
...
pngtest.c
浏览文件 @
0f71645d
...
...
@@ -19,7 +19,7 @@
/* input and output filenames */
char
inname
[]
=
"pngtest.png"
;
char
outname
[]
=
"
png
out.png"
;
char
outname
[]
=
"
test
out.png"
;
png_struct
read_ptr
;
png_struct
write_ptr
;
...
...
@@ -30,13 +30,13 @@ char inbuf[256], outbuf[256];
int
main
()
{
FILE
*
fpin
,
*
fpout
;
png_byte
*
row_buf
;
png_uint_32
rowbytes
;
FILE
*
fpin
,
*
fpout
;
png_byte
*
row_buf
;
png_uint_32
rowbytes
;
png_uint_32
y
;
int
channels
,
num_pass
,
pass
;
int
channels
,
num_pass
,
pass
;
row_buf
=
(
png_byte
*
)
0
;
row_buf
=
(
png_byte
*
)
0
;
fprintf
(
STDERR
,
"Testing libpng version %s
\n
"
,
PNG_LIBPNG_VER_STRING
);
...
...
@@ -48,142 +48,145 @@ int main()
fprintf
(
STDERR
,
" png.c version: %s
\n\n
"
,
png_libpng_ver
);
}
fpin
=
fopen
(
inname
,
"rb"
);
if
(
!
fpin
)
{
fprintf
(
STDERR
,
"Could not find input file %s
\n
"
,
inname
);
return
1
;
}
fpout
=
fopen
(
outname
,
"wb"
);
if
(
!
fpout
)
{
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
,
(
png_bytef
**
)
&
row_buf
,
(
png_bytef
**
)
0
,
1
);
png_write_rows
(
&
write_ptr
,
(
png_bytef
**
)
&
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
);
fpin
=
fopen
(
inname
,
"rb"
);
if
(
!
fpin
)
{
fprintf
(
STDERR
,
"Could not find input file %s
\n
"
,
inname
);
return
1
;
}
fpout
=
fopen
(
outname
,
"wb"
);
if
(
!
fpout
)
{
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_info_init
(
&
info_ptr
);
png_info_init
(
&
end_info
);
png_read_init
(
&
read_ptr
);
png_write_init
(
&
write_ptr
);
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
++
;
png_set_flush
(
&
write_ptr
,
20
);
png_set_compression_level
(
&
write_ptr
,
9
);
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
,
(
png_bytef
**
)
&
row_buf
,
(
png_bytef
**
)
0
,
1
);
png_write_rows
(
&
write_ptr
,
(
png_bytef
**
)
&
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
;
...
...
pngwrite.c
浏览文件 @
0f71645d
...
...
@@ -57,25 +57,30 @@ png_write_info(png_struct *png_ptr, png_info *info)
if
(
info
->
valid
&
PNG_INFO_bKGD
)
png_write_bKGD
(
png_ptr
,
&
(
info
->
background
),
info
->
color_type
);
#endif
#if defined(PNG_WRITE_hIST_SUPPORTED)
if
(
info
->
valid
&
PNG_INFO_hIST
)
png_write_hIST
(
png_ptr
,
info
->
hist
,
info
->
num_palette
);
#endif
#if defined(PNG_WRITE_pHYs_SUPPORTED)
if
(
info
->
valid
&
PNG_INFO_pHYs
)
png_write_pHYs
(
png_ptr
,
info
->
x_pixels_per_unit
,
info
->
y_pixels_per_unit
,
info
->
phys_unit_type
);
#endif
#if defined(PNG_WRITE_oFFs_SUPPORTED)
if
(
info
->
valid
&
PNG_INFO_oFFs
)
png_write_oFFs
(
png_ptr
,
info
->
x_offset
,
info
->
y_offset
,
info
->
offset_unit_type
);
#endif
#if defined(PNG_WRITE_tIME_SUPPORTED)
if
(
info
->
valid
&
PNG_INFO_tIME
)
png_write_tIME
(
png_ptr
,
&
(
info
->
mod_time
));
/* Check to see if we need to write text chunks */
#endif
#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
if
(
info
->
num_text
)
{
...
...
@@ -84,6 +89,12 @@ png_write_info(png_struct *png_ptr, png_info *info)
/* loop through the text chunks */
for
(
i
=
0
;
i
<
info
->
num_text
;
i
++
)
{
if
(
strlen
(
info
->
text
[
i
].
key
)
>
80
)
{
(
*
(
png_ptr
->
warning_fn
))(
png_ptr
,
"tEXt keyword more than 80 chars"
);
continue
;
}
/* if chunk is compressed */
if
(
info
->
text
[
i
].
compression
>=
0
)
{
...
...
@@ -104,7 +115,8 @@ png_write_info(png_struct *png_ptr, png_info *info)
}
}
}
#endif
#endif
/* PNG_WRITE_tEXt_SUPPORTED || PNG_WRITE_zTXt_SUPPORTED */
png_ptr
->
mode
=
PNG_HAVE_IHDR
;
}
/* writes the end of the png file. If you don't want to write comments or
...
...
@@ -114,6 +126,9 @@ png_write_info(png_struct *png_ptr, png_info *info)
void
png_write_end
(
png_struct
*
png_ptr
,
png_info
*
info
)
{
if
(
info
&&
png_ptr
->
mode
==
PNG_AFTER_IEND
)
return
;
/* see if user wants us to write information chunks */
if
(
info
)
{
...
...
@@ -122,6 +137,7 @@ png_write_end(png_struct *png_ptr, png_info *info)
if
(
info
->
valid
&
PNG_INFO_tIME
)
png_write_tIME
(
png_ptr
,
&
(
info
->
mod_time
));
#endif
#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
/* check to see if we need to write comment chunks */
if
(
info
->
num_text
)
...
...
@@ -151,10 +167,12 @@ png_write_end(png_struct *png_ptr, png_info *info)
}
}
}
#endif
#endif
/* PNG_WRITE_tEXt_SUPPORTED || PNG_WRITE_zTXt_SUPPORTED */
}
/* write end of png file */
png_write_IEND
(
png_ptr
);
png_ptr
->
mode
=
PNG_AFTER_IEND
;
}
#if defined(PNG_WRITE_tIME_SUPPORTED)
...
...
@@ -177,7 +195,7 @@ png_convert_from_time_t(png_time *ptime, time_t ttime)
tbuf
=
gmtime
(
&
ttime
);
png_convert_from_struct_tm
(
ptime
,
tbuf
);
}
#endif
#endif
/* PNG_WRITE_tIME_SUPPORTED */
/* initialize png structure, and allocate any memory needed */
void
...
...
@@ -243,11 +261,13 @@ void
png_write_row
(
png_struct
*
png_ptr
,
png_bytef
*
row
)
{
/* initialize transformations and other stuff if first time */
if
(
png_ptr
->
row_number
==
0
&&
png_ptr
->
pass
==
0
)
if
(
png_ptr
->
mode
<
PNG_HAVE_IDAT
)
{
png_write_start_row
(
png_ptr
);
}
png_ptr
->
mode
=
PNG_HAVE_IDAT
;
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
/* if interlaced and not interested in row, return */
if
(
png_ptr
->
interlaced
&&
(
png_ptr
->
transformations
&
PNG_INTERLACE
))
...
...
@@ -305,7 +325,7 @@ png_write_row(png_struct *png_ptr, png_bytef *row)
break
;
}
}
#endif
#endif
/* PNG_WRITE_INTERLACE_SUPPORTED */
/* set up row info for transformations */
png_ptr
->
row_info
.
color_type
=
png_ptr
->
color_type
;
...
...
@@ -334,7 +354,7 @@ png_write_row(png_struct *png_ptr, png_bytef *row)
return
;
}
}
#endif
#endif
/* PNG_WRITE_INTERLACE_SUPPORTED */
/* handle other transformations */
if
(
png_ptr
->
transformations
)
...
...
@@ -367,6 +387,7 @@ png_write_row(png_struct *png_ptr, png_bytef *row)
/* set up the zlib input buffer */
png_ptr
->
zstream
->
next_in
=
png_ptr
->
row_buf
;
png_ptr
->
zstream
->
avail_in
=
(
uInt
)
png_ptr
->
row_info
.
rowbytes
+
1
;
/* repeat until we have compressed all the data */
do
{
...
...
@@ -378,9 +399,9 @@ png_write_row(png_struct *png_ptr, png_bytef *row)
if
(
ret
!=
Z_OK
)
{
if
(
png_ptr
->
zstream
->
msg
)
png_error
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
png_error
(
png_ptr
,
"zlib error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"zlib error"
);
}
/* see if it is time to write another IDAT */
...
...
@@ -396,7 +417,76 @@ png_write_row(png_struct *png_ptr, png_bytef *row)
/* finish row - updates counters and flushes zlib if last row */
png_write_finish_row
(
png_ptr
);
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
png_ptr
->
flush_rows
++
;
if
(
png_ptr
->
flush_dist
>
0
&&
png_ptr
->
flush_rows
>=
png_ptr
->
flush_dist
)
{
png_write_flush
(
png_ptr
);
}
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
}
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
/* Set the automatic flush interval or 0 to turn flushing off */
void
png_set_flush
(
png_struct
*
png_ptr
,
int
nrows
)
{
png_ptr
->
flush_dist
=
(
nrows
<
0
?
0
:
nrows
);
}
/* flush the current output buffers now */
void
png_write_flush
(
png_struct
*
png_ptr
)
{
char
wrote_IDAT
;
if
(
png_ptr
->
mode
!=
PNG_HAVE_IDAT
)
return
;
do
{
int
ret
;
/* compress the data */
ret
=
deflate
(
png_ptr
->
zstream
,
Z_SYNC_FLUSH
);
wrote_IDAT
=
0
;
/* check for compression errors */
if
(
ret
!=
Z_OK
)
{
if
(
png_ptr
->
zstream
->
msg
)
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
(
*
(
png_ptr
->
error_fn
))(
png_ptr
,
"zlib error"
);
}
if
(
!
png_ptr
->
zstream
->
avail_out
)
{
/* write the IDAT and reset the zlib output buffer */
png_write_IDAT
(
png_ptr
,
png_ptr
->
zbuf
,
png_ptr
->
zbuf_size
);
png_ptr
->
zstream
->
next_out
=
png_ptr
->
zbuf
;
png_ptr
->
zstream
->
avail_out
=
(
uInt
)
png_ptr
->
zbuf_size
;
wrote_IDAT
=
1
;
}
}
while
(
wrote_IDAT
==
1
);
/* If there is any data left to be output, write it into a new IDAT */
if
(
png_ptr
->
zbuf_size
!=
png_ptr
->
zstream
->
avail_out
)
{
/* write the IDAT and reset the zlib output buffer */
png_write_IDAT
(
png_ptr
,
png_ptr
->
zbuf
,
png_ptr
->
zbuf_size
-
png_ptr
->
zstream
->
avail_out
);
png_ptr
->
zstream
->
next_out
=
png_ptr
->
zbuf
;
png_ptr
->
zstream
->
avail_out
=
(
uInt
)
png_ptr
->
zbuf_size
;
}
png_ptr
->
flush_rows
=
0
;
(
*
(
png_ptr
->
output_flush_fn
))(
png_ptr
);
}
#endif
/* PNG_WRITE_FLUSH_SUPPORTED */
/* free any memory used in png struct */
void
...
...
@@ -416,6 +506,7 @@ png_write_destroy(png_struct *png_ptr)
png_memset
(
png_ptr
,
0
,
sizeof
(
png_struct
));
png_memcpy
(
png_ptr
->
jmpbuf
,
tmp_jmp
,
sizeof
(
jmp_buf
));
}
void
png_set_filtering
(
png_struct
*
png_ptr
,
int
filter
)
{
...
...
pngwutil.c
浏览文件 @
0f71645d
...
...
@@ -39,7 +39,7 @@ png_write_uint_32(png_struct *png_ptr, png_uint_32 i)
buf
[
1
]
=
(
png_byte
)((
i
>>
16
)
&
0xff
);
buf
[
2
]
=
(
png_byte
)((
i
>>
8
)
&
0xff
);
buf
[
3
]
=
(
png_byte
)(
i
&
0xff
);
png_write_data
(
png_ptr
,
buf
,
4
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
buf
,
4
);
}
/* write a 16 bit number */
...
...
@@ -50,7 +50,7 @@ png_write_uint_16(png_struct *png_ptr, png_uint_16 i)
buf
[
0
]
=
(
png_byte
)((
i
>>
8
)
&
0xff
);
buf
[
1
]
=
(
png_byte
)(
i
&
0xff
);
png_write_data
(
png_ptr
,
buf
,
2
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
buf
,
2
);
}
/* Write a png chunk all at once. The type is an array of ASCII characters
...
...
@@ -68,7 +68,7 @@ png_write_chunk(png_struct *png_ptr, png_byte *type,
/* write length */
png_write_uint_32
(
png_ptr
,
length
);
/* write chunk name */
png_write_data
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
/* reset the crc and run the chunk name over it */
png_reset_crc
(
png_ptr
);
png_calculate_crc
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
...
...
@@ -76,7 +76,7 @@ png_write_chunk(png_struct *png_ptr, png_byte *type,
if
(
length
)
{
png_calculate_crc
(
png_ptr
,
data
,
length
);
png_write_data
(
png_ptr
,
data
,
length
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
data
,
length
);
}
/* write the crc */
png_write_uint_32
(
png_ptr
,
~
png_ptr
->
crc
);
...
...
@@ -92,7 +92,7 @@ png_write_chunk_start(png_struct *png_ptr, png_byte *type,
/* write the length */
png_write_uint_32
(
png_ptr
,
total_length
);
/* write the chunk name */
png_write_data
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
/* reset the crc and run it over the chunk name */
png_reset_crc
(
png_ptr
);
png_calculate_crc
(
png_ptr
,
type
,
(
png_uint_32
)
4
);
...
...
@@ -109,7 +109,7 @@ png_write_chunk_data(png_struct *png_ptr, png_bytef *data, png_uint_32 length)
if
(
length
)
{
png_calculate_crc
(
png_ptr
,
data
,
length
);
png_write_data
(
png_ptr
,
data
,
length
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
data
,
length
);
}
}
...
...
@@ -126,7 +126,7 @@ void
png_write_sig
(
png_struct
*
png_ptr
)
{
/* write the 8 byte signature */
png_write_data
(
png_ptr
,
png_sig
,
(
png_uint_32
)
8
);
(
*
(
png_ptr
->
write_data_fn
))
(
png_ptr
,
png_sig
,
(
png_uint_32
)
8
);
}
/* Write the IHDR chunk, and update the png_struct with the necessary
...
...
@@ -175,10 +175,10 @@ png_write_IHDR(png_struct *png_ptr, png_uint_32 width, png_uint_32 height,
/* set the usr info, so any transformations can modify it */
png_ptr
->
usr_width
=
png_ptr
->
width
;
png_ptr
->
usr_bit_depth
=
png_ptr
->
bit_depth
;
png_ptr
->
usr_channels
=
png_ptr
->
channels
;
png_ptr
->
usr_channels
=
png_ptr
->
channels
;
/* write the chunk */
png_write_chunk
(
png_ptr
,
png_IHDR
,
buf
,
(
png_uint_32
)
13
);
png_write_chunk
(
png_ptr
,
png_IHDR
,
buf
,
(
png_uint_32
)
13
);
/* initialize zlib with png info */
png_ptr
->
zstream
=
&
(
png_ptr
->
zstream_struct
);
...
...
@@ -462,9 +462,9 @@ png_write_zTXt(png_struct *png_ptr, charf *key, charf *text,
{
/* error */
if
(
png_ptr
->
zstream
->
msg
)
png_error
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
png_error
(
png_ptr
,
"zlib error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"zlib error"
);
}
/* check to see if we need more room */
if
(
!
png_ptr
->
zstream
->
avail_out
&&
png_ptr
->
zstream
->
avail_in
)
...
...
@@ -508,9 +508,9 @@ png_write_zTXt(png_struct *png_ptr, charf *key, charf *text,
{
/* we got an error */
if
(
png_ptr
->
zstream
->
msg
)
png_error
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
png_error
(
png_ptr
,
"zlib error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"zlib error"
);
}
/* check to see if we need more room */
...
...
@@ -689,7 +689,7 @@ png_write_finish_row(png_struct *png_ptr)
/* next row */
png_ptr
->
row_number
++
;
/* see if we are done */
if
(
png_ptr
->
row_number
<
png_ptr
->
num_rows
)
if
(
png_ptr
->
row_number
<
png_ptr
->
num_rows
||
png_ptr
->
mode
>
PNG_HAVE_IDAT
)
return
;
/* if interlaced, go to next pass */
...
...
@@ -740,9 +740,9 @@ png_write_finish_row(png_struct *png_ptr)
if
(
ret
!=
Z_OK
&&
ret
!=
Z_STREAM_END
)
{
if
(
png_ptr
->
zstream
->
msg
)
png_error
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
png_ptr
->
zstream
->
msg
);
else
png_error
(
png_ptr
,
"zlib error"
);
(
*
(
png_ptr
->
error_fn
))
(
png_ptr
,
"zlib error"
);
}
/* check to see if we need more room */
if
(
!
png_ptr
->
zstream
->
avail_out
&&
ret
==
Z_OK
)
...
...
@@ -761,6 +761,7 @@ png_write_finish_row(png_struct *png_ptr)
}
deflateReset
(
png_ptr
->
zstream
);
png_ptr
->
mode
=
PNG_AFTER_IDAT
;
}
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录