1 | #include <png.h>
|
2 | #include <stdio.h>
|
3 | #include <setjmp.h>
|
4 |
|
5 | #define file_name "test.png"
|
6 |
|
7 | #define ERROR 1
|
8 |
|
9 | png_byte data0[] = { 0, 10, 20, 30 },
|
10 | data1[] = { 10, 25, 40, 55 },
|
11 | data2[] = { 20, 40, 60, 80 },
|
12 | data3[] = { 30, 55, 80, 105 };
|
13 |
|
14 | png_byte *row_pointers[4] = {
|
15 | data0, data1, data2, data3
|
16 | };
|
17 |
|
18 | void write_row_callback(png_structp png_ptr, png_uint_32 row,
|
19 | int pass)
|
20 | {
|
21 | /* put your code here */
|
22 | }
|
23 |
|
24 | int
|
25 | main(void)
|
26 | {
|
27 | FILE *fp = fopen(file_name, "wb");
|
28 | if (!fp)
|
29 | {
|
30 | return (ERROR);
|
31 | }
|
32 |
|
33 | png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
|
34 | NULL, NULL);
|
35 | if (!png_ptr)
|
36 | return (ERROR);
|
37 |
|
38 | png_infop info_ptr = png_create_info_struct(png_ptr);
|
39 | if (!info_ptr)
|
40 | {
|
41 | png_destroy_write_struct(&png_ptr,
|
42 | (png_infopp)NULL);
|
43 | return (ERROR);
|
44 | }
|
45 | if (setjmp(png_jmpbuf(png_ptr)))
|
46 | {
|
47 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
48 | fclose(fp);
|
49 | return (ERROR);
|
50 | }
|
51 | png_init_io(png_ptr, fp);
|
52 |
|
53 | png_set_write_status_fn(png_ptr, write_row_callback);
|
54 |
|
55 | /* turn on or off filtering, and/or choose
|
56 | specific filters. You can use either a single
|
57 | PNG_FILTER_VALUE_NAME or the bitwise OR of one
|
58 | or more PNG_FILTER_NAME masks. */
|
59 | #if 0
|
60 | png_set_filter(png_ptr, 0,
|
61 | PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE |
|
62 | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
|
63 | PNG_FILTER_UP | PNG_FILTER_VALUE_UP |
|
64 | PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
|
65 | PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
|
66 | PNG_ALL_FILTERS);
|
67 | #endif
|
68 | /* set the zlib compression level */
|
69 | png_set_compression_level(png_ptr,
|
70 | Z_BEST_COMPRESSION);
|
71 |
|
72 | /* set other zlib parameters */
|
73 | png_set_compression_mem_level(png_ptr, 8);
|
74 | png_set_compression_strategy(png_ptr,
|
75 | Z_DEFAULT_STRATEGY);
|
76 | png_set_compression_window_bits(png_ptr, 15);
|
77 | png_set_compression_method(png_ptr, 8);
|
78 | png_set_compression_buffer_size(png_ptr, 8192);
|
79 |
|
80 | png_set_IHDR(png_ptr, info_ptr,
|
81 | 4, 4, /* width, height */
|
82 | 8, /* bit_depth */
|
83 | PNG_COLOR_TYPE_GRAY,
|
84 | PNG_INTERLACE_NONE,
|
85 | PNG_COMPRESSION_TYPE_DEFAULT,
|
86 | PNG_FILTER_TYPE_DEFAULT);
|
87 | png_write_info(png_ptr, info_ptr);
|
88 | png_write_image(png_ptr, row_pointers);
|
89 |
|
90 | png_write_end(png_ptr, info_ptr);
|
91 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
92 |
|
93 | fclose(fp);
|
94 |
|
95 | return 0;
|
96 | }
|