| 250 | } |
| 251 | |
| 252 | int savePalettePNG(rasterBufferObj *rb, streamInfo *info, int compression) { |
| 253 | png_infop info_ptr; |
| 254 | rgbPixel rgb[256]; |
| 255 | unsigned char a[256]; |
| 256 | int num_a; |
| 257 | int row,sample_depth; |
| 258 | png_structp png_ptr = png_create_write_struct( |
| 259 | PNG_LIBPNG_VER_STRING, NULL,NULL,NULL); |
| 260 | |
| 261 | assert(rb->type == MS_BUFFER_BYTE_PALETTE); |
| 262 | |
| 263 | if (!png_ptr) |
| 264 | return (MS_FAILURE); |
| 265 | |
| 266 | info_ptr = png_create_info_struct(png_ptr); |
| 267 | if (!info_ptr) |
| 268 | { |
| 269 | png_destroy_write_struct(&png_ptr, |
| 270 | (png_infopp)NULL); |
| 271 | return (MS_FAILURE); |
| 272 | } |
| 273 | |
| 274 | if (setjmp(png_jmpbuf(png_ptr))) |
| 275 | { |
| 276 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 277 | return (MS_FAILURE); |
| 278 | } |
| 279 | if(info->fp) |
| 280 | png_set_write_fn(png_ptr,info, png_write_data_to_stream, png_flush_data); |
| 281 | else |
| 282 | png_set_write_fn(png_ptr,info, png_write_data_to_buffer, png_flush_data); |
| 283 | |
| 284 | png_set_compression_level(png_ptr, compression); |
| 285 | |
| 286 | if (rb->data.palette.num_entries <= 2) |
| 287 | sample_depth = 1; |
| 288 | else if (rb->data.palette.num_entries <= 4) |
| 289 | sample_depth = 2; |
| 290 | else if (rb->data.palette.num_entries <= 16) |
| 291 | sample_depth = 4; |
| 292 | else |
| 293 | sample_depth = 8; |
| 294 | |
| 295 | png_set_IHDR(png_ptr, info_ptr, rb->width, rb->height, |
| 296 | sample_depth, PNG_COLOR_TYPE_PALETTE, |
| 297 | 0, PNG_COMPRESSION_TYPE_DEFAULT, |
| 298 | PNG_FILTER_TYPE_DEFAULT); |
| 299 | |
| 300 | remapPaletteForPNG(rb,rgb,a,&num_a); |
| 301 | |
| 302 | png_set_PLTE(png_ptr, info_ptr, (png_colorp)(rgb),rb->data.palette.num_entries); |
| 303 | if(num_a) |
| 304 | png_set_tRNS(png_ptr, info_ptr, a,num_a, NULL); |
| 305 | |
| 306 | png_write_info(png_ptr, info_ptr); |
| 307 | png_set_packing(png_ptr); |
| 308 | |
| 309 | for(row=0;row<rb->height;row++) { |
no test coverage detected