# stolen from http://code.google.com/p/gnuxaos/issues/detail?id=55
old
|
new
|
|
3 | 3 | #include <aconfig.h> |
4 | 4 | #ifdef USE_PNG |
5 | 5 | #include <png.h> |
| 6 | #include <zlib.h> |
6 | 7 | #endif |
7 | 8 | #include <stdlib.h> |
8 | 9 | #include <stdio.h> |
… |
… |
|
31 | 32 | png_structp png_ptr; |
32 | 33 | png_infop info_ptr; |
33 | 34 | png_color palette[256]; |
| 35 | png_color_8 sig_bit; |
| 36 | int color_type; |
| 37 | int bit_depth; |
34 | 38 | volatile unsigned short a = 255; |
35 | 39 | volatile unsigned char *b = (volatile unsigned char *) &a; |
36 | 40 | #ifdef _undefined_ |
… |
… |
|
59 | 63 | png_destroy_write_struct(&png_ptr, (png_infopp) NULL); |
60 | 64 | return "No memory to create png info structure"; |
61 | 65 | } |
62 | | if (setjmp(png_ptr->jmpbuf)) { |
| 66 | if (setjmp(png_jmpbuf(png_ptr))) { |
63 | 67 | png_destroy_write_struct(&png_ptr, &info_ptr); |
64 | 68 | fclose(file); |
65 | 69 | return strerror(errno); |
… |
… |
|
78 | 82 | png_set_compression_window_bits(png_ptr, 15); |
79 | 83 | png_set_compression_method(png_ptr, 8); |
80 | 84 | |
81 | | info_ptr->width = image->width; |
82 | | info_ptr->height = image->height; |
83 | | /*info_ptr->gamma=1.0; */ |
84 | | info_ptr->gamma = 0.5; |
85 | | info_ptr->valid |= PNG_INFO_gAMA | PNG_INFO_pHYs; |
86 | | info_ptr->x_pixels_per_unit = (png_uint_32) (100 / image->pixelwidth); |
87 | | info_ptr->y_pixels_per_unit = (png_uint_32) (100 / image->pixelheight); |
| 85 | switch (image->palette->type) |
| 86 | { |
| 87 | case C256: |
| 88 | color_type = PNG_COLOR_TYPE_PALETTE; |
| 89 | bit_depth = image->bytesperpixel * 8; |
| 90 | break; |
| 91 | case SMALLITER: |
| 92 | case LARGEITER: |
| 93 | case GRAYSCALE: |
| 94 | color_type = PNG_COLOR_TYPE_GRAY; |
| 95 | bit_depth = image->bytesperpixel * 8; |
| 96 | break; |
| 97 | case TRUECOLOR: |
| 98 | case TRUECOLOR24: |
| 99 | case TRUECOLOR16: |
| 100 | color_type = PNG_COLOR_TYPE_RGB; |
| 101 | bit_depth = 8; |
| 102 | break; |
| 103 | } |
88 | 104 | |
| 105 | png_set_IHDR (png_ptr, info_ptr, image->width, image->height, bit_depth, |
| 106 | color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, |
| 107 | PNG_FILTER_TYPE_BASE); |
89 | 108 | |
90 | 109 | switch (image->palette->type) { |
91 | 110 | case C256: |
92 | 111 | { |
93 | 112 | int i; |
94 | | info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
95 | | info_ptr->bit_depth = image->bytesperpixel * 8; |
96 | | info_ptr->palette = palette; |
97 | | info_ptr->valid |= PNG_INFO_PLTE; |
| 113 | png_color png_palette[257]; |
| 114 | int png_num_palette; |
98 | 115 | for (i = 0; i < image->palette->end; i++) |
99 | | info_ptr->palette[i].red = image->palette->rgb[i][0], |
100 | | info_ptr->palette[i].green = image->palette->rgb[i][1], |
101 | | info_ptr->palette[i].blue = image->palette->rgb[i][2], |
102 | | info_ptr->num_palette = image->palette->end; |
| 116 | png_palette[i].red = image->palette->rgb[i][0], |
| 117 | png_palette[i].green = image->palette->rgb[i][1], |
| 118 | png_palette[i].blue = image->palette->rgb[i][2], |
| 119 | png_num_palette = image->palette->end; |
| 120 | png_set_PLTE(png_ptr, info_ptr, png_palette, png_num_palette); |
103 | 121 | } |
104 | 122 | break; |
105 | | case SMALLITER: |
106 | | case LARGEITER: |
107 | | case GRAYSCALE: |
108 | | info_ptr->color_type = PNG_COLOR_TYPE_GRAY; |
109 | | info_ptr->bit_depth = image->bytesperpixel * 8; |
110 | | break; |
111 | 123 | case TRUECOLOR: |
112 | 124 | case TRUECOLOR24: |
113 | 125 | case TRUECOLOR16: |
114 | | info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
115 | | info_ptr->bit_depth = 8; |
116 | | info_ptr->sig_bit.red = 8 - image->palette->info.truec.rprec; |
117 | | info_ptr->sig_bit.green = 8 - image->palette->info.truec.gprec; |
118 | | info_ptr->sig_bit.blue = 8 - image->palette->info.truec.bprec; |
| 126 | sig_bit.red = 8 - image->palette->info.truec.rprec; |
| 127 | sig_bit.green = 8 - image->palette->info.truec.gprec; |
| 128 | sig_bit.blue = 8 - image->palette->info.truec.bprec; |
| 129 | png_set_sBIT(png_ptr, info_ptr, &sig_bit); |
119 | 130 | break; |
120 | 131 | } |
121 | | info_ptr->interlace_type = 0; |
| 132 | |
| 133 | png_write_info (png_ptr, info_ptr); |
| 134 | |
122 | 135 | #ifdef _undefined_ |
123 | 136 | png_set_text(png_ptr, info_ptr, comments, |
124 | 137 | sizeof(comments) / sizeof(png_text)); |
125 | 138 | #endif |
126 | 139 | |
127 | | png_write_info(png_ptr, info_ptr); |
| 140 | png_set_gAMA(png_ptr, info_ptr, 0.5 /* 1.0 */); |
| 141 | png_set_pHYs(png_ptr, info_ptr, (png_uint_32) (100 / image->pixelwidth), |
| 142 | (png_uint_32) (100 / image->pixelheight), |
| 143 | PNG_RESOLUTION_UNKNOWN); |
| 144 | |
128 | 145 | /*png_set_filler(png_ptr,0,PNG_FILLER_AFTER); */ |
129 | 146 | png_set_packing(png_ptr); |
130 | 147 | if (image->palette->type & (TRUECOLOR | TRUECOLOR24 | TRUECOLOR16)) |
131 | | png_set_shift(png_ptr, &(info_ptr->sig_bit)); |
| 148 | png_set_shift (png_ptr, &sig_bit); |
132 | 149 | if (*b == 255) |
133 | 150 | png_set_swap(png_ptr); |
134 | 151 | png_set_bgr(png_ptr); |