| 1 | /* Copyright (C) 2000-2012 by George Williams */ |
| 2 | /* |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <fontforge-config.h> |
| 29 | |
| 30 | #ifdef _NO_LIBPNG |
| 31 | |
| 32 | static int a_file_must_define_something=0; /* ANSI says so */ |
| 33 | |
| 34 | #else |
| 35 | |
| 36 | #include <png.h> |
| 37 | |
| 38 | #include <vector> |
| 39 | |
| 40 | #include "gimage.h" |
| 41 | |
| 42 | static void user_error_fn(png_structp png_ptr, png_const_charp error_msg) { |
| 43 | fprintf(stderr, "%s\n", error_msg ); |
| 44 | #if (PNG_LIBPNG_VER < 10500) |
| 45 | longjmp(png_ptr->jmpbuf,1); |
| 46 | #else |
| 47 | png_longjmp (png_ptr, 1); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | static void user_warning_fn(png_structp UNUSED(png_ptr), png_const_charp warning_msg) { |
| 52 | fprintf(stderr,"%s\n", warning_msg); |
| 53 | } |
| 54 | |
| 55 | static void mem_write_fn(png_structp png_ptr, png_bytep data, png_size_t sz) { |
| 56 | auto *vec = static_cast<std::vector<unsigned char>*>(png_get_io_ptr(png_ptr)); |
| 57 | vec->insert(vec->end(), data, data + sz); |
| 58 | } |
| 59 | |
| 60 | static void mem_flush_fn(png_structp UNUSED(png_ptr)) { |
| 61 | } |
| 62 | |
| 63 | /* Pack unpacked byte_data (1 byte per sample) into the proper bit-packed format. |
| 64 | * For example, 4bpp needs 2 samples per byte. Samples should be left-justified. |
| 65 | * Returns allocated packed buffer (must be freed by caller) or NULL if no packing needed. */ |
| 66 | static png_byte* PackByteDataForPNG(struct _GImage *base, int bit_depth, int *packed_bpl) { |
| 67 | int expected_bpl = (base->width * bit_depth + 7) / 8; |
| 68 | if (base->bytes_per_line <= expected_bpl) { |
| 69 | return NULL; /* Already packed or correct stride */ |
| 70 | } |
| 71 | |
| 72 | int samples_per_byte = 8 / bit_depth; |
| 73 | int mask = (1 << bit_depth) - 1; |
| 74 | png_byte *packed = (png_byte*)malloc(base->height * expected_bpl); |
| 75 | |
| 76 | for (int y = 0; y < base->height; ++y) { |
| 77 | png_byte *src_row = (png_byte*)(base->data + y * base->bytes_per_line); |
| 78 | png_byte *dst_row = packed + y * expected_bpl; |
| 79 | memset(dst_row, 0, expected_bpl); |
| 80 | |
| 81 | for (int x = 0; x < base->width; ++x) { |
| 82 | png_byte sample = src_row[x] & mask; |
| 83 | int byte_idx = x / samples_per_byte; |
| 84 | int sample_idx = x % samples_per_byte; |
| 85 | int shift = 8 - (sample_idx + 1) * bit_depth; |
| 86 | dst_row[byte_idx] |= (sample << shift); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | *packed_bpl = expected_bpl; |
| 91 | return packed; |
| 92 | } |
| 93 | |
| 94 | static int GImageWritePngFull(GImage *gi, void *io, bool in_memory, int compression_level, bool progressive) { |
| 95 | struct _GImage *base = gi->list_len==0?gi->u.image:gi->u.images[0]; |
| 96 | png_structp png_ptr; |
| 97 | png_infop info_ptr; |
| 98 | png_byte **rows; |
| 99 | png_byte *packed_data = NULL; |
| 100 | int packed_bpl = 0; |
| 101 | int i; |
| 102 | int bit_depth; |
| 103 | int color_type; |
| 104 | int num_palette; |
| 105 | png_bytep trans_alpha = NULL; |
| 106 | png_color_16p trans_color = NULL; |
| 107 | png_colorp palette = NULL; |
| 108 | |
| 109 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
| 110 | (void *)NULL, user_error_fn, user_warning_fn); |
| 111 | |
| 112 | if (!png_ptr) { |
| 113 | return(false); |
| 114 | } |
| 115 | |
| 116 | info_ptr = png_create_info_struct(png_ptr); |
| 117 | if (!info_ptr) { |
| 118 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
| 119 | return(false); |
| 120 | } |
| 121 | |
| 122 | #if (PNG_LIBPNG_VER < 10500) |
| 123 | if (setjmp(png_ptr->jmpbuf)) |
| 124 | #else |
| 125 | if (setjmp(*png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf)))) |
| 126 | #endif |
| 127 | { |
| 128 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
| 129 | return(false); |
| 130 | } |
| 131 | |
| 132 | if (in_memory) { |
| 133 | png_set_write_fn(png_ptr, io, mem_write_fn, mem_flush_fn); |
| 134 | } else { |
| 135 | png_init_io(png_ptr, (FILE*)io); |
| 136 | } |
| 137 | |
| 138 | if (compression_level >= 0 && compression_level <= 9) { |
| 139 | png_set_compression_level(png_ptr, compression_level); |
| 140 | } |
| 141 | |
| 142 | bit_depth = 8; |
| 143 | num_palette = base->clut==NULL?2:base->clut->clut_len; |
| 144 | if ( base->image_type==it_index || base->image_type==it_bitmap ) { |
| 145 | color_type = PNG_COLOR_TYPE_PALETTE; |
| 146 | if ( num_palette<=2 ) |
| 147 | bit_depth=1; |
| 148 | else if ( num_palette<=4 ) |
| 149 | bit_depth=2; |
| 150 | else if ( num_palette<=16 ) |
| 151 | bit_depth=4; |
| 152 | } else { |
| 153 | color_type = PNG_COLOR_TYPE_RGB; |
| 154 | if ( base->image_type == it_rgba ) |
| 155 | color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 156 | } |
| 157 | |
| 158 | png_set_IHDR(png_ptr, info_ptr, base->width, base->height, |
| 159 | bit_depth, color_type, progressive, |
| 160 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 161 | if ( base->image_type==it_index || base->image_type==it_bitmap ) { |
| 162 | palette = (png_color *) malloc(num_palette*sizeof(png_color)); |
| 163 | if ( base->clut==NULL ) { |
| 164 | palette[0].red = palette[0].green = palette[0].blue = 0; |
| 165 | palette[1].red = palette[1].green = palette[1].blue = 0xff; |
| 166 | } else { |
| 167 | for ( i=0; i<num_palette; ++i ) { |
| 168 | long col = base->clut->clut[i]; |
| 169 | palette[i].red = COLOR_RED(col); |
| 170 | palette[i].green = COLOR_GREEN(col); |
| 171 | palette[i].blue = COLOR_BLUE(col); |
| 172 | } |
| 173 | } |
| 174 | png_set_PLTE(png_ptr, info_ptr, palette, num_palette); |
| 175 | |
| 176 | /* For palette images with bit_depth < 8, we need to decide between: |
| 177 | * 1. Manual packing: pack byte_data ourselves (1 byte/sample -> multibit/byte) |
| 178 | * 2. PNG's packing: use png_set_packing() for already-correct byte data |
| 179 | * Detect which is needed by checking if data exceeds expected packed size */ |
| 180 | int expected_bpl = (base->width * bit_depth + 7) / 8; |
| 181 | if (num_palette <= 16 && base->bytes_per_line > expected_bpl) { |
| 182 | /* Data is unpacked (byte_data), we'll pack it manually; don't use png_set_packing() */ |
| 183 | } else if (num_palette <= 16) { |
| 184 | /* Data is already in the format png_set_packing() expects */ |
| 185 | png_set_packing(png_ptr); |
| 186 | } |
| 187 | |
| 188 | if ( base->trans!=(Color)-1 ) { |
| 189 | trans_alpha = (png_bytep) malloc(1); |
| 190 | trans_alpha[0] = base->trans; |
| 191 | } |
| 192 | } else { |
| 193 | if ( base->trans!=(Color)-1 ) { |
| 194 | trans_color = (png_color_16p) malloc(sizeof(png_color_16)); |
| 195 | trans_color->red = COLOR_RED(base->trans); |
| 196 | trans_color->green = COLOR_GREEN(base->trans); |
| 197 | trans_color->blue = COLOR_BLUE(base->trans); |
| 198 | } |
| 199 | } |
| 200 | if ( base->trans!=(Color)-1 ) { |
| 201 | png_set_tRNS(png_ptr, info_ptr, trans_alpha, 1, trans_color); |
| 202 | } |
| 203 | png_write_info(png_ptr, info_ptr); |
| 204 | |
| 205 | if (color_type == PNG_COLOR_TYPE_RGB) |
| 206 | png_set_filler(png_ptr, '\0', PNG_FILLER_AFTER); |
| 207 | |
| 208 | if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 209 | png_set_bgr(png_ptr); |
| 210 | |
| 211 | /* For palette images with bit_depth < 8, pack byte_data if needed. */ |
| 212 | if ((base->image_type==it_index || base->image_type==it_bitmap) && bit_depth < 8) { |
| 213 | packed_data = PackByteDataForPNG(base, bit_depth, &packed_bpl); |
| 214 | } |
| 215 | |
| 216 | rows = (png_byte **) malloc(base->height*sizeof(png_byte *)); |
| 217 | if (packed_data) { |
| 218 | for ( i=0; i<base->height; ++i ) |
| 219 | rows[i] = (png_byte *) (packed_data + i*packed_bpl); |
| 220 | } else { |
| 221 | for ( i=0; i<base->height; ++i ) |
| 222 | rows[i] = (png_byte *) (base->data + i*base->bytes_per_line); |
| 223 | } |
| 224 | |
| 225 | png_write_image(png_ptr,rows); |
| 226 | |
| 227 | png_write_end(png_ptr, info_ptr); |
| 228 | |
| 229 | free(trans_alpha); |
| 230 | free(trans_color); |
| 231 | free(palette); |
| 232 | free(packed_data); |
| 233 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 234 | free(rows); |
| 235 | return( 1 ); |
| 236 | } |
| 237 | |
| 238 | int GImageWritePngBuf(GImage *gi, char** buf, size_t* sz, int compression_level, int progressive) { |
| 239 | std::vector<unsigned char> vec; |
| 240 | *buf = NULL; |
| 241 | *sz = 0; |
| 242 | |
| 243 | if (!GImageWritePngFull(gi, &vec, true, compression_level, progressive)) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | *buf = (char*) malloc(vec.size()); |
| 248 | if (*buf == NULL) { |
| 249 | return false; |
| 250 | } |
| 251 | *sz = vec.size(); |
| 252 | |
| 253 | memcpy(*buf, vec.data(), vec.size()); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | int GImageWrite_Png(GImage *gi, FILE *fp, int progressive) { |
| 258 | return GImageWritePngFull(gi, fp, false, -1, progressive); |
| 259 | } |
| 260 | |
| 261 | int GImageWritePng(GImage *gi, char *filename, int progressive) { |
| 262 | FILE *fp; |
| 263 | int ret; |
| 264 | |
| 265 | /* open the file */ |
| 266 | fp = fopen(filename, "wb"); |
| 267 | if (!fp) |
| 268 | return(false); |
| 269 | ret = GImageWrite_Png(gi,fp,progressive); |
| 270 | fclose(fp); |
| 271 | return( ret ); |
| 272 | } |
| 273 | |
| 274 | #endif |