| 1 |
/* |
| 2 |
------------------------------------------------------------------------------ |
| 3 |
Licensing information can be found at the end of the file. |
| 4 |
------------------------------------------------------------------------------ |
| 5 |
|
| 6 |
cute_png.h - v1.05 |
| 7 |
|
| 8 |
To create implementation (the function definitions) |
| 9 |
#define CUTE_PNG_IMPLEMENTATION |
| 10 |
in *one* C/CPP file (translation unit) that includes this file |
| 11 |
|
| 12 |
|
| 13 |
SUMMARY: |
| 14 |
|
| 15 |
This header wraps some very nice functions by Richard Mitton from his |
| 16 |
tigr (Tiny Graphics) library, with some additional features and small |
| 17 |
bug-fixes. |
| 18 |
|
| 19 |
|
| 20 |
Revision history: |
| 21 |
1.00 (12/23/2016) initial release |
| 22 |
1.01 (03/08/2017) tRNS chunk support for paletted images |
| 23 |
1.02 (10/23/2017) support for explicitly loading paletted png images |
| 24 |
1.03 (11/12/2017) construct atlas in memory |
| 25 |
1.04 (08/23/2018) various bug fixes for filter and word decoder |
| 26 |
added `cp_load_blank` |
| 27 |
1.05 (11/10/2022) added `cp_save_png_to_memory` |
| 28 |
|
| 29 |
|
| 30 |
EXAMPLES: |
| 31 |
|
| 32 |
Loading a PNG from disk, then freeing it |
| 33 |
cp_image_t img = cp_load_png("images/pic.png"); |
| 34 |
... |
| 35 |
free(img.pix); |
| 36 |
CUTE_PNG_MEMSET(&img, 0, sizeof(img)); |
| 37 |
|
| 38 |
Loading a PNG from memory, then freeing it |
| 39 |
cp_image_t img = cp_load_png_mem(memory, sizeof(memory)); |
| 40 |
... |
| 41 |
free(img.pix); |
| 42 |
CUTE_PNG_MEMSET(&img, 0, sizeof(img)); |
| 43 |
|
| 44 |
Saving a PNG to disk |
| 45 |
cp_save_png("images/example.png", &img); |
| 46 |
// img is just a raw RGBA buffer, and can come from anywhere, |
| 47 |
// not only from cp_load*** functions |
| 48 |
|
| 49 |
Creating a texture atlas in memory |
| 50 |
int w = 1024; |
| 51 |
int h = 1024; |
| 52 |
cp_atlas_image_t* imgs_out = (cp_atlas_image_t*)malloc(sizeof(cp_atlas_image_t) * my_png_count); |
| 53 |
cp_image_t atlas_img = cp_make_atlas(w, int h, my_png_array, my_png_count, imgs_out); |
| 54 |
// just pass an array of pointers to images along with the image count. Make sure to also |
| 55 |
// provide an array of `cp_atlas_image_t` for `cp_make_atlas` to output important UV info for the |
| 56 |
// images that fit into the atlas. |
| 57 |
|
| 58 |
Using the default atlas saver |
| 59 |
int errors = cp_default_save_atlas("atlas.png", "atlas.txt", atlas_img, atlas_imgs, img_count, names_of_all_images ? names_of_all_images : 0); |
| 60 |
if (errors) { ... } |
| 61 |
// Atlas info (like uv coordinates) are in "atlas.txt", and the image was writen to "atlas.png". |
| 62 |
// atlas_imgs was an array of `cp_atlas_image_t` from the `cp_make_atlas` function. |
| 63 |
|
| 64 |
Inflating a DEFLATE block (decompressing memory stored in DEFLATE format) |
| 65 |
cp_inflate(in, in_bytes, out, out_bytes); |
| 66 |
// this function requires knowledge of the un-compressed size |
| 67 |
// does *not* do any internal realloc! Will return errors if an |
| 68 |
// attempt to overwrite the out buffer is made |
| 69 |
|
| 70 |
CUSTOMIZATION |
| 71 |
|
| 72 |
There are various macros in this header you can customize by defining them before |
| 73 |
including cute_png.h. Simply define one to override the default behavior. |
| 74 |
|
| 75 |
CUTE_PNG_ALLOCA |
| 76 |
CUTE_PNG_ALLOC |
| 77 |
CUTE_PNG_FREE |
| 78 |
CUTE_PNG_CALLOC |
| 79 |
CUTE_PNG_REALLOC |
| 80 |
CUTE_PNG_MEMCPY |
| 81 |
CUTE_PNG_MEMCMP |
| 82 |
CUTE_PNG_MEMSET |
| 83 |
CUTE_PNG_ASSERT |
| 84 |
CUTE_PNG_FPRINTF |
| 85 |
CUTE_PNG_SEEK_SET |
| 86 |
CUTE_PNG_SEEK_END |
| 87 |
CUTE_PNG_FILE |
| 88 |
CUTE_PNG_FOPEN |
| 89 |
CUTE_PNG_FSEEK |
| 90 |
CUTE_PNG_FREAD |
| 91 |
CUTE_PNG_FTELL |
| 92 |
CUTE_PNG_FWRITE |
| 93 |
CUTE_PNG_FCLOSE |
| 94 |
CUTE_PNG_FERROR |
| 95 |
CUTE_PNG_ATLAS_MUST_FIT |
| 96 |
CUTE_PNG_ATLAS_FLIP_Y_AXIS_FOR_UV |
| 97 |
CUTE_PNG_ATLAS_EMPTY_COLOR |
| 98 |
*/ |
| 99 |
|
| 100 |
/* |
| 101 |
Contributors: |
| 102 |
Zachary Carter 1.01 - bug catch for tRNS chunk in paletted images |
| 103 |
Dennis Korpel 1.03 - fix some pointer/memory related bugs |
| 104 |
Dennis Korpel 1.04 - fix for filter on first row of pixels |
| 105 |
*/ |
| 106 |
|
| 107 |
#if !defined(CUTE_PNG_H) |
| 108 |
|
| 109 |
#ifdef _WIN32 |
| 110 |
#if !defined(_CRT_SECURE_NO_WARNINGS) |
| 111 |
#define _CRT_SECURE_NO_WARNINGS |
| 112 |
#endif |
| 113 |
#endif |
| 114 |
|
| 115 |
#ifndef CUTE_PNG_ATLAS_MUST_FIT |
| 116 |
#define CUTE_PNG_ATLAS_MUST_FIT 1 // returns error from cp_make_atlas if *any* input image does not fit |
| 117 |
#endif // CUTE_PNG_ATLAS_MUST_FIT |
| 118 |
|
| 119 |
#ifndef CUTE_PNG_ATLAS_FLIP_Y_AXIS_FOR_UV |
| 120 |
#define CUTE_PNG_ATLAS_FLIP_Y_AXIS_FOR_UV 1 // flips output uv coordinate's y. Can be useful to "flip image on load" |
| 121 |
#endif // CUTE_PNG_ATLAS_FLIP_Y_AXIS_FOR_UV |
| 122 |
|
| 123 |
#ifndef CUTE_PNG_ATLAS_EMPTY_COLOR |
| 124 |
#define CUTE_PNG_ATLAS_EMPTY_COLOR 0x000000FF // the fill color for empty areas in a texture atlas (RGBA) |
| 125 |
#endif // CUTE_PNG_ATLAS_EMPTY_COLOR |
| 126 |
|
| 127 |
#include <stdint.h> |
| 128 |
#include <limits.h> |
| 129 |
|
| 130 |
typedef struct cp_pixel_t cp_pixel_t; |
| 131 |
typedef struct cp_image_t cp_image_t; |
| 132 |
typedef struct cp_indexed_image_t cp_indexed_image_t; |
| 133 |
typedef struct cp_atlas_image_t cp_atlas_image_t; |
| 134 |
|
| 135 |
// Read this in the event of errors from any function |
| 136 |
extern const char* cp_error_reason; |
| 137 |
|
| 138 |
// return 1 for success, 0 for failures |
| 139 |
int cp_inflate(void* in, int in_bytes, void* out, int out_bytes); |
| 140 |
int cp_save_png(const char* file_name, const cp_image_t* img); |
| 141 |
|
| 142 |
typedef struct cp_saved_png_t |
| 143 |
{ |
| 144 |
int size; // Size of the `data` buffer. |
| 145 |
void* data; // Pointer to the saved png in memory. |
| 146 |
// NULL if something went wrong. |
| 147 |
// Call CUTE_PNG_FREE on `data` when done. |
| 148 |
} cp_saved_png_t; |
| 149 |
|
| 150 |
// Saves a png file to memory. |
| 151 |
// Call CUTE_PNG_FREE on .data when done. |
| 152 |
cp_saved_png_t cp_save_png_to_memory(const cp_image_t* img); |
| 153 |
|
| 154 |
// Constructs an atlas image in-memory. The atlas pixels are stored in the returned image. free the pixels |
| 155 |
// when done with them. The user must provide an array of cp_atlas_image_t for the `imgs` param. `imgs` holds |
| 156 |
// information about uv coordinates for an associated image in the `pngs` array. Output image has NULL |
| 157 |
// pixels buffer in the event of errors. |
| 158 |
cp_image_t cp_make_atlas(int atlasWidth, int atlasHeight, const cp_image_t* pngs, int png_count, cp_atlas_image_t* imgs_out); |
| 159 |
|
| 160 |
// A decent "default" function, ready to use out-of-the-box. Saves out an easy to parse text formatted info file |
| 161 |
// along with an atlas image. `names` param can be optionally NULL. |
| 162 |
int cp_default_save_atlas(const char* out_path_image, const char* out_path_atlas_txt, const cp_image_t* atlas, const cp_atlas_image_t* imgs, int img_count, const char** names); |
| 163 |
|
| 164 |
// these two functions return cp_image_t::pix as 0 in event of errors |
| 165 |
// call free on cp_image_t::pix when done, or call cp_free_png |
| 166 |
cp_image_t cp_load_png(const char *file_name); |
| 167 |
cp_image_t cp_load_png_mem(const void *png_data, int png_length); |
| 168 |
cp_image_t cp_load_blank(int w, int h); // Alloc's pixels, but `pix` memory is uninitialized. |
| 169 |
void cp_free_png(cp_image_t* img); |
| 170 |
void cp_flip_image_horizontal(cp_image_t* img); |
| 171 |
|
| 172 |
// Reads the w/h of the png without doing any other decompression or parsing. |
| 173 |
void cp_load_png_wh(const void* png_data, int png_length, int* w, int* h); |
| 174 |
|
| 175 |
// loads indexed (paletted) pngs, but does not depalette the image into RGBA pixels |
| 176 |
// these two functions return cp_indexed_image_t::pix as 0 in event of errors |
| 177 |
// call free on cp_indexed_image_t::pix when done, or call cp_free_indexed_png |
| 178 |
cp_indexed_image_t cp_load_indexed_png(const char* file_name); |
| 179 |
cp_indexed_image_t cp_load_indexed_png_mem(const void *png_data, int png_length); |
| 180 |
void cp_free_indexed_png(cp_indexed_image_t* img); |
| 181 |
|
| 182 |
// converts paletted image into a standard RGBA image |
| 183 |
// call free on cp_image_t::pix when done |
| 184 |
cp_image_t cp_depallete_indexed_image(cp_indexed_image_t* img); |
| 185 |
|
| 186 |
// Pre-process the pixels to transform the image data to a premultiplied alpha format. |
| 187 |
// Resource: http://www.essentialmath.com/GDC2015/VanVerth_Jim_DoingMathwRGB.pdf |
| 188 |
void cp_premultiply(cp_image_t* img); |
| 189 |
|
| 190 |
struct cp_pixel_t |
| 191 |
{ |
| 192 |
uint8_t r; |
| 193 |
uint8_t g; |
| 194 |
uint8_t b; |
| 195 |
uint8_t a; |
| 196 |
}; |
| 197 |
|
| 198 |
struct cp_image_t |
| 199 |
{ |
| 200 |
int w; |
| 201 |
int h; |
| 202 |
cp_pixel_t* pix; |
| 203 |
}; |
| 204 |
|
| 205 |
struct cp_indexed_image_t |
| 206 |
{ |
| 207 |
int w; |
| 208 |
int h; |
| 209 |
uint8_t* pix; |
| 210 |
uint8_t palette_len; |
| 211 |
cp_pixel_t palette[256]; |
| 212 |
}; |
| 213 |
|
| 214 |
struct cp_atlas_image_t |
| 215 |
{ |
| 216 |
int img_index; // index into the `imgs` array |
| 217 |
int w, h; // pixel w/h of original image |
| 218 |
float minx, miny; // u coordinate |
| 219 |
float maxx, maxy; // v coordinate |
| 220 |
int fit; // non-zero if image fit and was placed into the atlas |
| 221 |
}; |
| 222 |
|
| 223 |
#define CUTE_PNG_H |
| 224 |
#endif |
| 225 |
|
| 226 |
#ifdef CUTE_PNG_IMPLEMENTATION |
| 227 |
#ifndef CUTE_PNG_IMPLEMENTATION_ONCE |
| 228 |
#define CUTE_PNG_IMPLEMENTATION_ONCE |
| 229 |
|
| 230 |
#if !defined(CUTE_PNG_ALLOCA) |
| 231 |
#define CUTE_PNG_ALLOCA alloca |
| 232 |
|
| 233 |
#ifdef _WIN32 |
| 234 |
#include <malloc.h> |
| 235 |
#elif defined(__linux__) |
| 236 |
#include <alloca.h> |
| 237 |
#endif |
| 238 |
#endif |
| 239 |
|
| 240 |
#if !defined(CUTE_PNG_ALLOC) |
| 241 |
#include <stdlib.h> |
| 242 |
#define CUTE_PNG_ALLOC malloc |
| 243 |
#endif |
| 244 |
|
| 245 |
#if !defined(CUTE_PNG_FREE) |
| 246 |
#include <stdlib.h> |
| 247 |
#define CUTE_PNG_FREE free |
| 248 |
#endif |
| 249 |
|
| 250 |
#if !defined(CUTE_PNG_CALLOC) |
| 251 |
#include <stdlib.h> |
| 252 |
#define CUTE_PNG_CALLOC calloc |
| 253 |
#endif |
| 254 |
|
| 255 |
#if !defined(CUTE_PNG_REALLOC) |
| 256 |
#include <stdlib.h> |
| 257 |
#define CUTE_PNG_REALLOC realloc |
| 258 |
#endif |
| 259 |
|
| 260 |
#if !defined(CUTE_PNG_MEMCPY) |
| 261 |
#include <string.h> |
| 262 |
#define CUTE_PNG_MEMCPY memcpy |
| 263 |
#endif |
| 264 |
|
| 265 |
#if !defined(CUTE_PNG_MEMCMP) |
| 266 |
#include <string.h> |
| 267 |
#define CUTE_PNG_MEMCMP memcmp |
| 268 |
#endif |
| 269 |
|
| 270 |
#if !defined(CUTE_PNG_MEMSET) |
| 271 |
#include <string.h> |
| 272 |
#define CUTE_PNG_MEMSET memset |
| 273 |
#endif |
| 274 |
|
| 275 |
#if !defined(CUTE_PNG_ASSERT) |
| 276 |
#include <assert.h> |
| 277 |
#define CUTE_PNG_ASSERT assert |
| 278 |
#endif |
| 279 |
|
| 280 |
#if !defined(CUTE_PNG_FPRINTF) |
| 281 |
#include <stdio.h> |
| 282 |
#define CUTE_PNG_FPRINTF fprintf |
| 283 |
#endif |
| 284 |
|
| 285 |
#if !defined(CUTE_PNG_SEEK_SET) |
| 286 |
#include <stdio.h> |
| 287 |
#define CUTE_PNG_SEEK_SET SEEK_SET |
| 288 |
#endif |
| 289 |
|
| 290 |
#if !defined(CUTE_PNG_SEEK_END) |
| 291 |
#include <stdio.h> |
| 292 |
#define CUTE_PNG_SEEK_END SEEK_END |
| 293 |
#endif |
| 294 |
|
| 295 |
#if !defined(CUTE_PNG_FILE) |
| 296 |
#include <stdio.h> |
| 297 |
#define CUTE_PNG_FILE FILE |
| 298 |
#endif |
| 299 |
|
| 300 |
#if !defined(CUTE_PNG_FOPEN) |
| 301 |
#include <stdio.h> |
| 302 |
#define CUTE_PNG_FOPEN fopen |
| 303 |
#endif |
| 304 |
|
| 305 |
#if !defined(CUTE_PNG_FSEEK) |
| 306 |
#include <stdio.h> |
| 307 |
#define CUTE_PNG_FSEEK fseek |
| 308 |
#endif |
| 309 |
|
| 310 |
#if !defined(CUTE_PNG_FREAD) |
| 311 |
#include <stdio.h> |
| 312 |
#define CUTE_PNG_FREAD fread |
| 313 |
#endif |
| 314 |
|
| 315 |
#if !defined(CUTE_PNG_FTELL) |
| 316 |
#include <stdio.h> |
| 317 |
#define CUTE_PNG_FTELL ftell |
| 318 |
#endif |
| 319 |
|
| 320 |
#if !defined(CUTE_PNG_FWRITE) |
| 321 |
#include <stdio.h> |
| 322 |
#define CUTE_PNG_FWRITE fwrite |
| 323 |
#endif |
| 324 |
|
| 325 |
#if !defined(CUTE_PNG_FCLOSE) |
| 326 |
#include <stdio.h> |
| 327 |
#define CUTE_PNG_FCLOSE fclose |
| 328 |
#endif |
| 329 |
|
| 330 |
#if !defined(CUTE_PNG_FERROR) |
| 331 |
#include <stdio.h> |
| 332 |
#define CUTE_PNG_FERROR ferror |
| 333 |
#endif |
| 334 |
|
| 335 |
static cp_pixel_t cp_make_pixel_a(uint8_t r, uint8_t g, uint8_t b, uint8_t a) |
| 336 |
{ |
| 337 |
cp_pixel_t p; |
| 338 |
p.r = r; p.g = g; p.b = b; p.a = a; |
| 339 |
return p; |
| 340 |
} |
| 341 |
|
| 342 |
static cp_pixel_t cp_make_pixel(uint8_t r, uint8_t g, uint8_t b) |
| 343 |
{ |
| 344 |
cp_pixel_t p; |
| 345 |
p.r = r; p.g = g; p.b = b; p.a = 0xFF; |
| 346 |
return p; |
| 347 |
} |
| 348 |
|
| 349 |
const char* cp_error_reason; |
| 350 |
#define CUTE_PNG_FAIL() do { goto cp_err; } while (0) |
| 351 |
#define CUTE_PNG_CHECK(X, Y) do { if (!(X)) { cp_error_reason = Y; CUTE_PNG_FAIL(); } } while (0) |
| 352 |
#define CUTE_PNG_CALL(X) do { if (!(X)) goto cp_err; } while (0) |
| 353 |
#define CUTE_PNG_LOOKUP_BITS 9 |
| 354 |
#define CUTE_PNG_LOOKUP_COUNT (1 << CUTE_PNG_LOOKUP_BITS) |
| 355 |
#define CUTE_PNG_LOOKUP_MASK (CUTE_PNG_LOOKUP_COUNT - 1) |
| 356 |
#define CUTE_PNG_DEFLATE_MAX_BITLEN 15 |
| 357 |
|
| 358 |
// DEFLATE tables from RFC 1951 |
| 359 |
uint8_t cp_fixed_table[288 + 32] = { |
| 360 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 361 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 362 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 363 |
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 364 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 365 |
}; // 3.2.6 |
| 366 |
uint8_t cp_permutation_order[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; // 3.2.7 |
| 367 |
uint8_t cp_len_extra_bits[29 + 2] = { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0, 0,0 }; // 3.2.5 |
| 368 |
uint32_t cp_len_base[29 + 2] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 0,0 }; // 3.2.5 |
| 369 |
uint8_t cp_dist_extra_bits[30 + 2] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13, 0,0 }; // 3.2.5 |
| 370 |
uint32_t cp_dist_base[30 + 2] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 0,0 }; // 3.2.5 |
| 371 |
|
| 372 |
typedef struct cp_state_t |
| 373 |
{ |
| 374 |
uint64_t bits; |
| 375 |
int count; |
| 376 |
uint32_t* words; |
| 377 |
int word_count; |
| 378 |
int word_index; |
| 379 |
int bits_left; |
| 380 |
|
| 381 |
int final_word_available; |
| 382 |
uint32_t final_word; |
| 383 |
|
| 384 |
char* out; |
| 385 |
char* out_end; |
| 386 |
char* begin; |
| 387 |
|
| 388 |
uint16_t lookup[CUTE_PNG_LOOKUP_COUNT]; |
| 389 |
uint32_t lit[288]; |
| 390 |
uint32_t dst[32]; |
| 391 |
uint32_t len[19]; |
| 392 |
uint32_t nlit; |
| 393 |
uint32_t ndst; |
| 394 |
uint32_t nlen; |
| 395 |
} cp_state_t; |
| 396 |
|
| 397 |
static int cp_would_overflow(cp_state_t* s, int num_bits) |
| 398 |
{ |
| 399 |
return (s->bits_left + s->count) - num_bits < 0; |
| 400 |
} |
| 401 |
|
| 402 |
static char* cp_ptr(cp_state_t* s) |
| 403 |
{ |
| 404 |
CUTE_PNG_ASSERT(!(s->bits_left & 7)); |
| 405 |
return (char*)(s->words + s->word_index) - (s->count / 8); |
| 406 |
} |
| 407 |
|
| 408 |
static uint64_t cp_peak_bits(cp_state_t* s, int num_bits_to_read) |
| 409 |
{ |
| 410 |
if (s->count < num_bits_to_read) |
| 411 |
{ |
| 412 |
if (s->word_index < s->word_count) |
| 413 |
{ |
| 414 |
uint32_t word = s->words[s->word_index++]; |
| 415 |
s->bits |= (uint64_t)word << s->count; |
| 416 |
s->count += 32; |
| 417 |
CUTE_PNG_ASSERT(s->word_index <= s->word_count); |
| 418 |
} |
| 419 |
|
| 420 |
else if (s->final_word_available) |
| 421 |
{ |
| 422 |
uint32_t word = s->final_word; |
| 423 |
s->bits |= (uint64_t)word << s->count; |
| 424 |
s->count += s->bits_left; |
| 425 |
s->final_word_available = 0; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
return s->bits; |
| 430 |
} |
| 431 |
|
| 432 |
static uint32_t cp_consume_bits(cp_state_t* s, int num_bits_to_read) |
| 433 |
{ |
| 434 |
CUTE_PNG_ASSERT(s->count >= num_bits_to_read); |
| 435 |
uint32_t bits = s->bits & (((uint64_t)1 << num_bits_to_read) - 1); |
| 436 |
s->bits >>= num_bits_to_read; |
| 437 |
s->count -= num_bits_to_read; |
| 438 |
s->bits_left -= num_bits_to_read; |
| 439 |
return bits; |
| 440 |
} |
| 441 |
|
| 442 |
static uint32_t cp_read_bits(cp_state_t* s, int num_bits_to_read) |
| 443 |
{ |
| 444 |
CUTE_PNG_ASSERT(num_bits_to_read <= 32); |
| 445 |
CUTE_PNG_ASSERT(num_bits_to_read >= 0); |
| 446 |
CUTE_PNG_ASSERT(s->bits_left > 0); |
| 447 |
CUTE_PNG_ASSERT(s->count <= 64); |
| 448 |
CUTE_PNG_ASSERT(!cp_would_overflow(s, num_bits_to_read)); |
| 449 |
cp_peak_bits(s, num_bits_to_read); |
| 450 |
uint32_t bits = cp_consume_bits(s, num_bits_to_read); |
| 451 |
return bits; |
| 452 |
} |
| 453 |
|
| 454 |
static char* cp_read_file_to_memory(const char* path, int* size) |
| 455 |
{ |
| 456 |
char* data = 0; |
| 457 |
CUTE_PNG_FILE* fp = CUTE_PNG_FOPEN(path, "rb"); |
| 458 |
int sizeNum = 0; |
| 459 |
|
| 460 |
if (fp) |
| 461 |
{ |
| 462 |
CUTE_PNG_FSEEK(fp, 0, CUTE_PNG_SEEK_END); |
| 463 |
sizeNum = CUTE_PNG_FTELL(fp); |
| 464 |
CUTE_PNG_FSEEK(fp, 0, CUTE_PNG_SEEK_SET); |
| 465 |
data = (char*)CUTE_PNG_ALLOC(sizeNum + 1); |
| 466 |
CUTE_PNG_FREAD(data, sizeNum, 1, fp); |
| 467 |
data[sizeNum] = 0; |
| 468 |
CUTE_PNG_FCLOSE(fp); |
| 469 |
} |
| 470 |
|
| 471 |
if (size) *size = sizeNum; |
| 472 |
return data; |
| 473 |
} |
| 474 |
|
| 475 |
static uint32_t cp_rev16(uint32_t a) |
| 476 |
{ |
| 477 |
a = ((a & 0xAAAA) >> 1) | ((a & 0x5555) << 1); |
| 478 |
a = ((a & 0xCCCC) >> 2) | ((a & 0x3333) << 2); |
| 479 |
a = ((a & 0xF0F0) >> 4) | ((a & 0x0F0F) << 4); |
| 480 |
a = ((a & 0xFF00) >> 8) | ((a & 0x00FF) << 8); |
| 481 |
return a; |
| 482 |
} |
| 483 |
|
| 484 |
// RFC 1951 section 3.2.2 |
| 485 |
static int cp_build(cp_state_t* s, uint32_t* tree, uint8_t* lens, int sym_count) |
| 486 |
{ |
| 487 |
int n, codes[16], first[16], counts[16] = { 0 }; |
| 488 |
|
| 489 |
// Frequency count |
| 490 |
for (n = 0; n < sym_count; n++) counts[lens[n]]++; |
| 491 |
|
| 492 |
// Distribute codes |
| 493 |
counts[0] = codes[0] = first[0] = 0; |
| 494 |
for (n = 1; n <= 15; ++n) |
| 495 |
{ |
| 496 |
codes[n] = (codes[n - 1] + counts[n - 1]) << 1; |
| 497 |
first[n] = first[n - 1] + counts[n - 1]; |
| 498 |
} |
| 499 |
|
| 500 |
if (s) CUTE_PNG_MEMSET(s->lookup, 0, sizeof(s->lookup)); |
| 501 |
for (int i = 0; i < sym_count; ++i) |
| 502 |
{ |
| 503 |
int len = lens[i]; |
| 504 |
|
| 505 |
if (len != 0) |
| 506 |
{ |
| 507 |
CUTE_PNG_ASSERT(len < 16); |
| 508 |
uint32_t code = codes[len]++; |
| 509 |
uint32_t slot = first[len]++; |
| 510 |
tree[slot] = (code << (32 - len)) | (i << 4) | len; |
| 511 |
|
| 512 |
if (s && len <= CUTE_PNG_LOOKUP_BITS) |
| 513 |
{ |
| 514 |
int j = cp_rev16(code) >> (16 - len); |
| 515 |
while (j < (1 << CUTE_PNG_LOOKUP_BITS)) |
| 516 |
{ |
| 517 |
s->lookup[j] = (uint16_t)((len << CUTE_PNG_LOOKUP_BITS) | i); |
| 518 |
j += (1 << len); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
int max_index = first[15]; |
| 525 |
return max_index; |
| 526 |
} |
| 527 |
|
| 528 |
static int cp_stored(cp_state_t* s) |
| 529 |
{ |
| 530 |
char* p; |
| 531 |
|
| 532 |
// 3.2.3 |
| 533 |
// skip any remaining bits in current partially processed byte |
| 534 |
cp_read_bits(s, s->count & 7); |
| 535 |
|
| 536 |
// 3.2.4 |
| 537 |
// read LEN and NLEN, should complement each other |
| 538 |
uint16_t LEN = (uint16_t)cp_read_bits(s, 16); |
| 539 |
uint16_t NLEN = (uint16_t)cp_read_bits(s, 16); |
| 540 |
CUTE_PNG_CHECK(LEN == (uint16_t)(~NLEN), "Failed to find LEN and NLEN as complements within stored (uncompressed) stream."); |
| 541 |
CUTE_PNG_CHECK(s->bits_left / 8 <= (int)LEN, "Stored block extends beyond end of input stream."); |
| 542 |
p = cp_ptr(s); |
| 543 |
CUTE_PNG_MEMCPY(s->out, p, LEN); |
| 544 |
s->out += LEN; |
| 545 |
return 1; |
| 546 |
|
| 547 |
cp_err: |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
// 3.2.6 |
| 552 |
static int cp_fixed(cp_state_t* s) |
| 553 |
{ |
| 554 |
s->nlit = cp_build(s, s->lit, cp_fixed_table, 288); |
| 555 |
s->ndst = cp_build(0, s->dst, cp_fixed_table + 288, 32); |
| 556 |
return 1; |
| 557 |
} |
| 558 |
|
| 559 |
static int cp_decode(cp_state_t* s, uint32_t* tree, int hi) |
| 560 |
{ |
| 561 |
uint64_t bits = cp_peak_bits(s, 16); |
| 562 |
uint32_t search = (cp_rev16((uint32_t)bits) << 16) | 0xFFFF; |
| 563 |
int lo = 0; |
| 564 |
while (lo < hi) |
| 565 |
{ |
| 566 |
int guess = (lo + hi) >> 1; |
| 567 |
if (search < tree[guess]) hi = guess; |
| 568 |
else lo = guess + 1; |
| 569 |
} |
| 570 |
|
| 571 |
uint32_t key = tree[lo - 1]; |
| 572 |
uint32_t len = (32 - (key & 0xF)); |
| 573 |
CUTE_PNG_ASSERT((search >> len) == (key >> len)); |
| 574 |
|
| 575 |
int code = cp_consume_bits(s, key & 0xF); |
| 576 |
(void)code; |
| 577 |
return (key >> 4) & 0xFFF; |
| 578 |
} |
| 579 |
|
| 580 |
// 3.2.7 |
| 581 |
static int cp_dynamic(cp_state_t* s) |
| 582 |
{ |
| 583 |
uint8_t lenlens[19] = { 0 }; |
| 584 |
|
| 585 |
int nlit = 257 + cp_read_bits(s, 5); |
| 586 |
int ndst = 1 + cp_read_bits(s, 5); |
| 587 |
int nlen = 4 + cp_read_bits(s, 4); |
| 588 |
|
| 589 |
for (int i = 0 ; i < nlen; ++i) |
| 590 |
lenlens[cp_permutation_order[i]] = (uint8_t)cp_read_bits(s, 3); |
| 591 |
|
| 592 |
// Build the tree for decoding code lengths |
| 593 |
s->nlen = cp_build(0, s->len, lenlens, 19); |
| 594 |
uint8_t lens[288 + 32]; |
| 595 |
|
| 596 |
for (int n = 0; n < nlit + ndst;) |
| 597 |
{ |
| 598 |
int sym = cp_decode(s, s->len, s->nlen); |
| 599 |
switch (sym) |
| 600 |
{ |
| 601 |
case 16: for (int i = 3 + cp_read_bits(s, 2); i; --i, ++n) lens[n] = lens[n - 1]; break; |
| 602 |
case 17: for (int i = 3 + cp_read_bits(s, 3); i; --i, ++n) lens[n] = 0; break; |
| 603 |
case 18: for (int i = 11 + cp_read_bits(s, 7); i; --i, ++n) lens[n] = 0; break; |
| 604 |
default: lens[n++] = (uint8_t)sym; break; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
s->nlit = cp_build(s, s->lit, lens, nlit); |
| 609 |
s->ndst = cp_build(0, s->dst, lens + nlit, ndst); |
| 610 |
return 1; |
| 611 |
} |
| 612 |
|
| 613 |
// 3.2.3 |
| 614 |
static int cp_block(cp_state_t* s) |
| 615 |
{ |
| 616 |
while (1) |
| 617 |
{ |
| 618 |
int symbol = cp_decode(s, s->lit, s->nlit); |
| 619 |
|
| 620 |
if (symbol < 256) |
| 621 |
{ |
| 622 |
CUTE_PNG_CHECK(s->out + 1 <= s->out_end, "Attempted to overwrite out buffer while outputting a symbol."); |
| 623 |
*s->out = (char)symbol; |
| 624 |
s->out += 1; |
| 625 |
} |
| 626 |
|
| 627 |
else if (symbol > 256) |
| 628 |
{ |
| 629 |
symbol -= 257; |
| 630 |
int length = cp_read_bits(s, cp_len_extra_bits[symbol]) + cp_len_base[symbol]; |
| 631 |
int distance_symbol = cp_decode(s, s->dst, s->ndst); |
| 632 |
int backwards_distance = cp_read_bits(s, cp_dist_extra_bits[distance_symbol]) + cp_dist_base[distance_symbol]; |
| 633 |
CUTE_PNG_CHECK(s->out - backwards_distance >= s->begin, "Attempted to write before out buffer (invalid backwards distance)."); |
| 634 |
CUTE_PNG_CHECK(s->out + length <= s->out_end, "Attempted to overwrite out buffer while outputting a string."); |
| 635 |
char* src = s->out - backwards_distance; |
| 636 |
char* dst = s->out; |
| 637 |
s->out += length; |
| 638 |
|
| 639 |
switch (backwards_distance) |
| 640 |
{ |
| 641 |
case 1: // very common in images |
| 642 |
CUTE_PNG_MEMSET(dst, *src, length); |
| 643 |
break; |
| 644 |
default: while (length--) *dst++ = *src++; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
else break; |
| 649 |
} |
| 650 |
|
| 651 |
return 1; |
| 652 |
|
| 653 |
cp_err: |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
// 3.2.3 |
| 658 |
int cp_inflate(void* in, int in_bytes, void* out, int out_bytes) |
| 659 |
{ |
| 660 |
cp_state_t* s = (cp_state_t*)CUTE_PNG_CALLOC(1, sizeof(cp_state_t)); |
| 661 |
s->bits = 0; |
| 662 |
s->count = 0; |
| 663 |
s->word_index = 0; |
| 664 |
s->bits_left = in_bytes * 8; |
| 665 |
|
| 666 |
// s->words is the in-pointer rounded up to a multiple of 4 |
| 667 |
int first_bytes = (int) ((( (size_t) in + 3) & ~3) - (size_t) in); |
| 668 |
s->words = (uint32_t*)((char*)in + first_bytes); |
| 669 |
s->word_count = (in_bytes - first_bytes) / 4; |
| 670 |
int last_bytes = ((in_bytes - first_bytes) & 3); |
| 671 |
|
| 672 |
for (int i = 0; i < first_bytes; ++i) |
| 673 |
s->bits |= (uint64_t)(((uint8_t*)in)[i]) << (i * 8); |
| 674 |
|
| 675 |
s->final_word_available = last_bytes ? 1 : 0; |
| 676 |
s->final_word = 0; |
| 677 |
for(int i = 0; i < last_bytes; i++) |
| 678 |
s->final_word |= ((uint8_t*)in)[in_bytes - last_bytes+i] << (i * 8); |
| 679 |
|
| 680 |
s->count = first_bytes * 8; |
| 681 |
|
| 682 |
s->out = (char*)out; |
| 683 |
s->out_end = s->out + out_bytes; |
| 684 |
s->begin = (char*)out; |
| 685 |
|
| 686 |
int count = 0; |
| 687 |
int bfinal; |
| 688 |
do |
| 689 |
{ |
| 690 |
bfinal = cp_read_bits(s, 1); |
| 691 |
int btype = cp_read_bits(s, 2); |
| 692 |
|
| 693 |
switch (btype) |
| 694 |
{ |
| 695 |
case 0: CUTE_PNG_CALL(cp_stored(s)); break; |
| 696 |
case 1: cp_fixed(s); CUTE_PNG_CALL(cp_block(s)); break; |
| 697 |
case 2: cp_dynamic(s); CUTE_PNG_CALL(cp_block(s)); break; |
| 698 |
case 3: CUTE_PNG_CHECK(0, "Detected unknown block type within input stream."); |
| 699 |
} |
| 700 |
|
| 701 |
++count; |
| 702 |
} |
| 703 |
while (!bfinal); |
| 704 |
|
| 705 |
CUTE_PNG_FREE(s); |
| 706 |
return 1; |
| 707 |
|
| 708 |
cp_err: |
| 709 |
CUTE_PNG_FREE(s); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
static uint8_t cp_paeth(uint8_t a, uint8_t b, uint8_t c) |
| 714 |
{ |
| 715 |
int p = a + b - c; |
| 716 |
int pa = abs(p - a); |
| 717 |
int pb = abs(p - b); |
| 718 |
int pc = abs(p - c); |
| 719 |
return (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c; |
| 720 |
} |
| 721 |
|
| 722 |
typedef struct cp_save_png_data_t |
| 723 |
{ |
| 724 |
uint32_t crc; |
| 725 |
uint32_t adler; |
| 726 |
uint32_t bits; |
| 727 |
uint32_t prev; |
| 728 |
uint32_t runlen; |
| 729 |
int buflen; |
| 730 |
int bufcap; |
| 731 |
char* buffer; |
| 732 |
} cp_save_png_data_t; |
| 733 |
|
| 734 |
uint32_t CP_CRC_TABLE[] = { |
| 735 |
0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
| 736 |
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
| 737 |
}; |
| 738 |
|
| 739 |
static void cp_put8(cp_save_png_data_t* s, uint32_t a) |
| 740 |
{ |
| 741 |
if (s->buflen >= s->bufcap) |
| 742 |
{ |
| 743 |
s->bufcap *= 2; |
| 744 |
s->buffer = (char*)CUTE_PNG_REALLOC(s->buffer, s->bufcap); |
| 745 |
} |
| 746 |
s->buffer[s->buflen++] = a; |
| 747 |
s->crc = (s->crc >> 4) ^ CP_CRC_TABLE[(s->crc & 15) ^ (a & 15)]; |
| 748 |
s->crc = (s->crc >> 4) ^ CP_CRC_TABLE[(s->crc & 15) ^ (a >> 4)]; |
| 749 |
} |
| 750 |
|
| 751 |
static void cp_update_adler(cp_save_png_data_t* s, uint32_t v) |
| 752 |
{ |
| 753 |
uint32_t s1 = s->adler & 0xFFFF; |
| 754 |
uint32_t s2 = (s->adler >> 16) & 0xFFFF; |
| 755 |
s1 = (s1 + v) % 65521; |
| 756 |
s2 = (s2 + s1) % 65521; |
| 757 |
s->adler = (s2 << 16) + s1; |
| 758 |
} |
| 759 |
|
| 760 |
static void cp_put32(cp_save_png_data_t* s, uint32_t v) |
| 761 |
{ |
| 762 |
cp_put8(s, (v >> 24) & 0xFF); |
| 763 |
cp_put8(s, (v >> 16) & 0xFF); |
| 764 |
cp_put8(s, (v >> 8) & 0xFF); |
| 765 |
cp_put8(s, v & 0xFF); |
| 766 |
} |
| 767 |
|
| 768 |
static void cp_put_bits(cp_save_png_data_t* s, uint32_t data, uint32_t bitcount) |
| 769 |
{ |
| 770 |
while (bitcount--) |
| 771 |
{ |
| 772 |
uint32_t prev = s->bits; |
| 773 |
s->bits = (s->bits >> 1) | ((data & 1) << 7); |
| 774 |
data >>= 1; |
| 775 |
|
| 776 |
if (prev & 1) |
| 777 |
{ |
| 778 |
cp_put8(s, s->bits); |
| 779 |
s->bits = 0x80; |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
static void cp_put_bitsr(cp_save_png_data_t* s, uint32_t data, uint32_t bitcount) |
| 785 |
{ |
| 786 |
while (bitcount--) |
| 787 |
cp_put_bits(s, data >> bitcount, 1); |
| 788 |
} |
| 789 |
|
| 790 |
static void cp_begin_chunk(cp_save_png_data_t* s, const char* id, uint32_t len) |
| 791 |
{ |
| 792 |
cp_put32(s, len); |
| 793 |
s->crc = 0xFFFFFFFF; |
| 794 |
cp_put8(s, (unsigned char)id[0]); |
| 795 |
cp_put8(s, (unsigned char)id[1]); |
| 796 |
cp_put8(s, (unsigned char)id[2]); |
| 797 |
cp_put8(s, (unsigned char)id[3]); |
| 798 |
} |
| 799 |
|
| 800 |
static void cp_encode_literal(cp_save_png_data_t* s, uint32_t v) |
| 801 |
{ |
| 802 |
// Encode a literal/length using the built-in tables. |
| 803 |
// Could do better with a custom table but whatever. |
| 804 |
if (v < 144) cp_put_bitsr(s, 0x030 + v - 0, 8); |
| 805 |
else if (v < 256) cp_put_bitsr(s, 0x190 + v - 144, 9); |
| 806 |
else if (v < 280) cp_put_bitsr(s, 0x000 + v - 256, 7); |
| 807 |
else cp_put_bitsr(s, 0x0c0 + v - 280, 8); |
| 808 |
} |
| 809 |
|
| 810 |
static void cp_encode_len(cp_save_png_data_t* s, uint32_t code, uint32_t bits, uint32_t len) |
| 811 |
{ |
| 812 |
cp_encode_literal(s, code + (len >> bits)); |
| 813 |
cp_put_bits(s, len, bits); |
| 814 |
cp_put_bits(s, 0, 5); |
| 815 |
} |
| 816 |
|
| 817 |
static void cp_end_run(cp_save_png_data_t* s) |
| 818 |
{ |
| 819 |
s->runlen--; |
| 820 |
cp_encode_literal(s, s->prev); |
| 821 |
|
| 822 |
if (s->runlen >= 67) cp_encode_len(s, 277, 4, s->runlen - 67); |
| 823 |
else if (s->runlen >= 35) cp_encode_len(s, 273, 3, s->runlen - 35); |
| 824 |
else if (s->runlen >= 19) cp_encode_len(s, 269, 2, s->runlen - 19); |
| 825 |
else if (s->runlen >= 11) cp_encode_len(s, 265, 1, s->runlen - 11); |
| 826 |
else if (s->runlen >= 3) cp_encode_len(s, 257, 0, s->runlen - 3); |
| 827 |
else while (s->runlen--) cp_encode_literal(s, s->prev); |
| 828 |
} |
| 829 |
|
| 830 |
static void cp_encode_byte(cp_save_png_data_t *s, uint8_t v) |
| 831 |
{ |
| 832 |
cp_update_adler(s, v); |
| 833 |
|
| 834 |
// Simple RLE compression. We could do better by doing a search |
| 835 |
// to find matches, but this works pretty well TBH. |
| 836 |
if (s->prev == v && s->runlen < 115) s->runlen++; |
| 837 |
|
| 838 |
else |
| 839 |
{ |
| 840 |
if (s->runlen) cp_end_run(s); |
| 841 |
|
| 842 |
s->prev = v; |
| 843 |
s->runlen = 1; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
static void cp_save_header(cp_save_png_data_t* s, cp_image_t* img) |
| 848 |
{ |
| 849 |
const unsigned char* hdr = (const unsigned char*)"\211PNG\r\n\032\n"; |
| 850 |
for (int i = 0; i < 8; ++i) { |
| 851 |
cp_put8(s, *hdr++); |
| 852 |
} |
| 853 |
cp_begin_chunk(s, "IHDR", 13); |
| 854 |
cp_put32(s, img->w); |
| 855 |
cp_put32(s, img->h); |
| 856 |
cp_put8(s, 8); // bit depth |
| 857 |
cp_put8(s, 6); // RGBA |
| 858 |
cp_put8(s, 0); // compression (deflate) |
| 859 |
cp_put8(s, 0); // filter (standard) |
| 860 |
cp_put8(s, 0); // interlace off |
| 861 |
cp_put32(s, ~s->crc); |
| 862 |
} |
| 863 |
|
| 864 |
static void cp_save_data(cp_save_png_data_t* s, cp_image_t* img, long dataPos, long* dataSize) |
| 865 |
{ |
| 866 |
cp_begin_chunk(s, "IDAT", 0); |
| 867 |
cp_put8(s, 0x08); // zlib compression method |
| 868 |
cp_put8(s, 0x1D); // zlib compression flags |
| 869 |
cp_put_bits(s, 3, 3); // zlib last block + fixed dictionary |
| 870 |
|
| 871 |
for (int y = 0; y < img->h; ++y) |
| 872 |
{ |
| 873 |
cp_pixel_t *row = &img->pix[y * img->w]; |
| 874 |
cp_pixel_t prev = cp_make_pixel_a(0, 0, 0, 0); |
| 875 |
|
| 876 |
cp_encode_byte(s, 1); // sub filter |
| 877 |
for (int x = 0; x < img->w; ++x) |
| 878 |
{ |
| 879 |
cp_encode_byte(s, row[x].r - prev.r); |
| 880 |
cp_encode_byte(s, row[x].g - prev.g); |
| 881 |
cp_encode_byte(s, row[x].b - prev.b); |
| 882 |
cp_encode_byte(s, row[x].a - prev.a); |
| 883 |
prev = row[x]; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
cp_end_run(s); |
| 888 |
cp_encode_literal(s, 256); // terminator |
| 889 |
while (s->bits != 0x80) cp_put_bits(s, 0, 1); |
| 890 |
cp_put32(s, s->adler); |
| 891 |
*dataSize = (s->buflen - dataPos) - 8; |
| 892 |
cp_put32(s, ~s->crc); |
| 893 |
} |
| 894 |
|
| 895 |
cp_saved_png_t cp_save_png_to_memory(const cp_image_t* img) |
| 896 |
{ |
| 897 |
cp_saved_png_t result = { 0 }; |
| 898 |
cp_save_png_data_t s = { 0 }; |
| 899 |
long dataPos, dataSize, fileSize; |
| 900 |
if (!img) return result; |
| 901 |
|
| 902 |
s.adler = 1; |
| 903 |
s.bits = 0x80; |
| 904 |
s.prev = 0xFFFF; |
| 905 |
s.bufcap = 1024; |
| 906 |
s.buffer = (char*)CUTE_PNG_ALLOC(1024); |
| 907 |
|
| 908 |
cp_save_header(&s, (cp_image_t*)img); |
| 909 |
dataPos = s.buflen; |
| 910 |
cp_save_data(&s, (cp_image_t*)img, dataPos, &dataSize); |
| 911 |
|
| 912 |
// End chunk. |
| 913 |
cp_begin_chunk(&s, "IEND", 0); |
| 914 |
cp_put32(&s, ~s.crc); |
| 915 |
|
| 916 |
// Write back payload size. |
| 917 |
fileSize = s.buflen; |
| 918 |
s.buflen = dataPos; |
| 919 |
cp_put32(&s, dataSize); |
| 920 |
|
| 921 |
result.size = fileSize; |
| 922 |
result.data = s.buffer; |
| 923 |
return result; |
| 924 |
} |
| 925 |
|
| 926 |
int cp_save_png(const char* file_name, const cp_image_t* img) |
| 927 |
{ |
| 928 |
cp_saved_png_t s; |
| 929 |
long err; |
| 930 |
CUTE_PNG_FILE* fp = CUTE_PNG_FOPEN(file_name, "wb"); |
| 931 |
if (!fp) return 1; |
| 932 |
s = cp_save_png_to_memory(img); |
| 933 |
CUTE_PNG_FWRITE(s.data, s.size, 1, fp); |
| 934 |
err = CUTE_PNG_FERROR(fp); |
| 935 |
CUTE_PNG_FCLOSE(fp); |
| 936 |
CUTE_PNG_FREE(s.data); |
| 937 |
return !err; |
| 938 |
} |
| 939 |
|
| 940 |
typedef struct cp_raw_png_t |
| 941 |
{ |
| 942 |
const uint8_t* p; |
| 943 |
const uint8_t* end; |
| 944 |
} cp_raw_png_t; |
| 945 |
|
| 946 |
static uint32_t cp_make32(const uint8_t* s) |
| 947 |
{ |
| 948 |
return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; |
| 949 |
} |
| 950 |
|
| 951 |
static const uint8_t* cp_chunk(cp_raw_png_t* png, const char* chunk, uint32_t minlen) |
| 952 |
{ |
| 953 |
uint32_t len = cp_make32(png->p); |
| 954 |
const uint8_t* start = png->p; |
| 955 |
|
| 956 |
if (!CUTE_PNG_MEMCMP(start + 4, chunk, 4) && len >= minlen) |
| 957 |
{ |
| 958 |
int offset = len + 12; |
| 959 |
|
| 960 |
if (png->p + offset <= png->end) |
| 961 |
{ |
| 962 |
png->p += offset; |
| 963 |
return start + 8; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
static const uint8_t* cp_find(cp_raw_png_t* png, const char* chunk, uint32_t minlen) |
| 971 |
{ |
| 972 |
const uint8_t *start; |
| 973 |
while (png->p < png->end) |
| 974 |
{ |
| 975 |
uint32_t len = cp_make32(png->p); |
| 976 |
start = png->p; |
| 977 |
png->p += len + 12; |
| 978 |
|
| 979 |
if (!CUTE_PNG_MEMCMP(start+4, chunk, 4) && len >= minlen && png->p <= png->end) |
| 980 |
return start + 8; |
| 981 |
} |
| 982 |
|
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
static int cp_unfilter(int w, int h, int bpp, uint8_t* raw) |
| 987 |
{ |
| 988 |
int len = w * bpp; |
| 989 |
uint8_t *prev; |
| 990 |
int x; |
| 991 |
|
| 992 |
if (h > 0) |
| 993 |
{ |
| 994 |
#define FILTER_LOOP_FIRST(A) for (x = bpp; x < len; x++) raw[x] += A; break |
| 995 |
switch (*raw++) |
| 996 |
{ |
| 997 |
case 0: break; |
| 998 |
case 1: FILTER_LOOP_FIRST(raw[x - bpp]); |
| 999 |
case 2: break; |
| 1000 |
case 3: FILTER_LOOP_FIRST(raw[x - bpp] / 2); |
| 1001 |
case 4: FILTER_LOOP_FIRST(cp_paeth(raw[x - bpp], 0, 0)); |
| 1002 |
default: return 0; |
| 1003 |
} |
| 1004 |
#undef FILTER_LOOP_FIRST |
| 1005 |
} |
| 1006 |
|
| 1007 |
prev = raw; |
| 1008 |
raw += len; |
| 1009 |
|
| 1010 |
for (int y = 1; y < h; y++, prev = raw, raw += len) |
| 1011 |
{ |
| 1012 |
#define FILTER_LOOP(A, B) for (x = 0 ; x < bpp; x++) raw[x] += A; for (; x < len; x++) raw[x] += B; break |
| 1013 |
switch (*raw++) |
| 1014 |
{ |
| 1015 |
case 0: break; |
| 1016 |
case 1: FILTER_LOOP(0 , raw[x - bpp] ); |
| 1017 |
case 2: FILTER_LOOP(prev[x] , prev[x]); |
| 1018 |
case 3: FILTER_LOOP(prev[x] / 2, (raw[x - bpp] + prev[x]) / 2); |
| 1019 |
case 4: FILTER_LOOP(prev[x] , cp_paeth(raw[x - bpp], prev[x], prev[x -bpp])); |
| 1020 |
default: return 0; |
| 1021 |
} |
| 1022 |
#undef FILTER_LOOP |
| 1023 |
} |
| 1024 |
|
| 1025 |
return 1; |
| 1026 |
} |
| 1027 |
|
| 1028 |
static void cp_convert(int bpp, int w, int h, uint8_t* src, cp_pixel_t* dst) |
| 1029 |
{ |
| 1030 |
for (int y = 0; y < h; y++) |
| 1031 |
{ |
| 1032 |
// skip filter byte |
| 1033 |
src++; |
| 1034 |
|
| 1035 |
for (int x = 0; x < w; x++, src += bpp) |
| 1036 |
{ |
| 1037 |
switch (bpp) |
| 1038 |
{ |
| 1039 |
case 1: *dst++ = cp_make_pixel(src[0], src[0], src[0]); break; |
| 1040 |
case 2: *dst++ = cp_make_pixel_a(src[0], src[0], src[0], src[1]); break; |
| 1041 |
case 3: *dst++ = cp_make_pixel(src[0], src[1], src[2]); break; |
| 1042 |
case 4: *dst++ = cp_make_pixel_a(src[0], src[1], src[2], src[3]); break; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
// http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.tRNS |
| 1049 |
static uint8_t cp_get_alpha_for_indexed_image(int index, const uint8_t* trns, uint32_t trns_len) |
| 1050 |
{ |
| 1051 |
if (!trns) return 255; |
| 1052 |
else if ((uint32_t)index >= trns_len) return 255; |
| 1053 |
else return trns[index]; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static void cp_depalette(int w, int h, uint8_t* src, cp_pixel_t* dst, const uint8_t* plte, const uint8_t* trns, uint32_t trns_len) |
| 1057 |
{ |
| 1058 |
for (int y = 0; y < h; ++y) |
| 1059 |
{ |
| 1060 |
// skip filter byte |
| 1061 |
++src; |
| 1062 |
|
| 1063 |
for (int x = 0; x < w; ++x, ++src) |
| 1064 |
{ |
| 1065 |
int c = *src; |
| 1066 |
uint8_t r = plte[c * 3]; |
| 1067 |
uint8_t g = plte[c * 3 + 1]; |
| 1068 |
uint8_t b = plte[c * 3 + 2]; |
| 1069 |
uint8_t a = cp_get_alpha_for_indexed_image(c, trns, trns_len); |
| 1070 |
*dst++ = cp_make_pixel_a(r, g, b, a); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
static uint32_t cp_get_chunk_byte_length(const uint8_t* chunk) |
| 1076 |
{ |
| 1077 |
return cp_make32(chunk - 8); |
| 1078 |
} |
| 1079 |
|
| 1080 |
static int cp_out_size(cp_image_t* img, int bpp) |
| 1081 |
{ |
| 1082 |
return (img->w + 1) * img->h * bpp; |
| 1083 |
} |
| 1084 |
|
| 1085 |
cp_image_t cp_load_png_mem(const void* png_data, int png_length) |
| 1086 |
{ |
| 1087 |
const char* sig = "\211PNG\r\n\032\n"; |
| 1088 |
const uint8_t* ihdr, *first, *plte, *trns; |
| 1089 |
int bit_depth, color_type, bpp, w, h, pix_bytes; |
| 1090 |
int compression, filter, interlace; |
| 1091 |
int datalen, offset; |
| 1092 |
uint8_t* out; |
| 1093 |
cp_image_t img = { 0 }; |
| 1094 |
uint8_t* data = 0; |
| 1095 |
cp_raw_png_t png; |
| 1096 |
png.p = (uint8_t*)png_data; |
| 1097 |
png.end = (uint8_t*)png_data + png_length; |
| 1098 |
|
| 1099 |
CUTE_PNG_CHECK(!CUTE_PNG_MEMCMP(png.p, sig, 8), "incorrect file signature (is this a png file?)"); |
| 1100 |
png.p += 8; |
| 1101 |
|
| 1102 |
ihdr = cp_chunk(&png, "IHDR", 13); |
| 1103 |
CUTE_PNG_CHECK(ihdr, "unable to find IHDR chunk"); |
| 1104 |
bit_depth = ihdr[8]; |
| 1105 |
color_type = ihdr[9]; |
| 1106 |
CUTE_PNG_CHECK(bit_depth == 8, "only bit-depth of 8 is supported"); |
| 1107 |
|
| 1108 |
switch (color_type) |
| 1109 |
{ |
| 1110 |
case 0: bpp = 1; break; // greyscale |
| 1111 |
case 2: bpp = 3; break; // RGB |
| 1112 |
case 3: bpp = 1; break; // paletted |
| 1113 |
case 4: bpp = 2; break; // grey+alpha |
| 1114 |
case 6: bpp = 4; break; // RGBA |
| 1115 |
default: CUTE_PNG_CHECK(0, "unknown color type"); |
| 1116 |
} |
| 1117 |
|
| 1118 |
// +1 for filter byte (which is dumb! just stick this at file header...) |
| 1119 |
w = cp_make32(ihdr) + 1; |
| 1120 |
h = cp_make32(ihdr + 4); |
| 1121 |
CUTE_PNG_CHECK(w >= 1, "invalid IHDR chunk found, image width was less than 1"); |
| 1122 |
CUTE_PNG_CHECK(h >= 1, "invalid IHDR chunk found, image height was less than 1"); |
| 1123 |
CUTE_PNG_CHECK((int64_t) w * h * sizeof(cp_pixel_t) < INT_MAX, "image too large"); |
| 1124 |
pix_bytes = w * h * sizeof(cp_pixel_t); |
| 1125 |
img.w = w - 1; |
| 1126 |
img.h = h; |
| 1127 |
img.pix = (cp_pixel_t*)CUTE_PNG_ALLOC(pix_bytes); |
| 1128 |
CUTE_PNG_CHECK(img.pix, "unable to allocate raw image space"); |
| 1129 |
|
| 1130 |
compression = ihdr[10]; |
| 1131 |
filter = ihdr[11]; |
| 1132 |
interlace = ihdr[12]; |
| 1133 |
CUTE_PNG_CHECK(!compression, "only standard compression DEFLATE is supported"); |
| 1134 |
CUTE_PNG_CHECK(!filter, "only standard adaptive filtering is supported"); |
| 1135 |
CUTE_PNG_CHECK(!interlace, "interlacing is not supported"); |
| 1136 |
|
| 1137 |
// PLTE must come before any IDAT chunk |
| 1138 |
first = png.p; |
| 1139 |
plte = cp_find(&png, "PLTE", 0); |
| 1140 |
if (!plte) png.p = first; |
| 1141 |
else first = png.p; |
| 1142 |
|
| 1143 |
// tRNS can come after PLTE |
| 1144 |
trns = cp_find(&png, "tRNS", 0); |
| 1145 |
if (!trns) png.p = first; |
| 1146 |
else first = png.p; |
| 1147 |
|
| 1148 |
// Compute length of the DEFLATE stream through IDAT chunk data sizes |
| 1149 |
datalen = 0; |
| 1150 |
for (const uint8_t* idat = cp_find(&png, "IDAT", 0); idat; idat = cp_chunk(&png, "IDAT", 0)) |
| 1151 |
{ |
| 1152 |
uint32_t len = cp_get_chunk_byte_length(idat); |
| 1153 |
datalen += len; |
| 1154 |
} |
| 1155 |
|
| 1156 |
// Copy in IDAT chunk data sections to form the compressed DEFLATE stream |
| 1157 |
png.p = first; |
| 1158 |
data = (uint8_t*)CUTE_PNG_ALLOC(datalen); |
| 1159 |
offset = 0; |
| 1160 |
for (const uint8_t* idat = cp_find(&png, "IDAT", 0); idat; idat = cp_chunk(&png, "IDAT", 0)) |
| 1161 |
{ |
| 1162 |
uint32_t len = cp_get_chunk_byte_length(idat); |
| 1163 |
CUTE_PNG_MEMCPY(data + offset, idat, len); |
| 1164 |
offset += len; |
| 1165 |
} |
| 1166 |
|
| 1167 |
// check for proper zlib structure in DEFLATE stream |
| 1168 |
CUTE_PNG_CHECK(data && datalen >= 6, "corrupt zlib structure in DEFLATE stream"); |
| 1169 |
CUTE_PNG_CHECK((data[0] & 0x0f) == 0x08, "only zlib compression method (RFC 1950) is supported"); |
| 1170 |
CUTE_PNG_CHECK((data[0] & 0xf0) <= 0x70, "innapropriate window size detected"); |
| 1171 |
CUTE_PNG_CHECK(!(data[1] & 0x20), "preset dictionary is present and not supported"); |
| 1172 |
|
| 1173 |
// check for integer overflow |
| 1174 |
CUTE_PNG_CHECK(cp_out_size(&img, 4) >= 1, "invalid image size found"); |
| 1175 |
CUTE_PNG_CHECK(cp_out_size(&img, bpp) >= 1, "invalid image size found"); |
| 1176 |
|
| 1177 |
out = (uint8_t*)img.pix + cp_out_size(&img, 4) - cp_out_size(&img, bpp); |
| 1178 |
CUTE_PNG_CHECK(cp_inflate(data + 2, datalen - 6, out, pix_bytes), "DEFLATE algorithm failed"); |
| 1179 |
CUTE_PNG_CHECK(cp_unfilter(img.w, img.h, bpp, out), "invalid filter byte found"); |
| 1180 |
|
| 1181 |
if (color_type == 3) |
| 1182 |
{ |
| 1183 |
CUTE_PNG_CHECK(plte, "color type of indexed requires a PLTE chunk"); |
| 1184 |
uint32_t trns_len = trns ? cp_get_chunk_byte_length(trns) : 0; |
| 1185 |
cp_depalette(img.w, img.h, out, img.pix, plte, trns, trns_len); |
| 1186 |
} |
| 1187 |
else cp_convert(bpp, img.w, img.h, out, img.pix); |
| 1188 |
|
| 1189 |
CUTE_PNG_FREE(data); |
| 1190 |
return img; |
| 1191 |
|
| 1192 |
cp_err: |
| 1193 |
CUTE_PNG_FREE(data); |
| 1194 |
CUTE_PNG_FREE(img.pix); |
| 1195 |
img.pix = 0; |
| 1196 |
|
| 1197 |
return img; |
| 1198 |
} |
| 1199 |
|
| 1200 |
cp_image_t cp_load_blank(int w, int h) |
| 1201 |
{ |
| 1202 |
cp_image_t img; |
| 1203 |
img.w = w; |
| 1204 |
img.h = h; |
| 1205 |
img.pix = (cp_pixel_t*)CUTE_PNG_ALLOC(w * h * sizeof(cp_pixel_t)); |
| 1206 |
return img; |
| 1207 |
} |
| 1208 |
|
| 1209 |
cp_image_t cp_load_png(const char *file_name) |
| 1210 |
{ |
| 1211 |
cp_image_t img = { 0 }; |
| 1212 |
int len; |
| 1213 |
void* data = cp_read_file_to_memory(file_name, &len); |
| 1214 |
if (!data) return img; |
| 1215 |
img = cp_load_png_mem(data, len); |
| 1216 |
CUTE_PNG_FREE(data); |
| 1217 |
return img; |
| 1218 |
} |
| 1219 |
|
| 1220 |
void cp_free_png(cp_image_t* img) |
| 1221 |
{ |
| 1222 |
CUTE_PNG_FREE(img->pix); |
| 1223 |
img->pix = 0; |
| 1224 |
img->w = img->h = 0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
void cp_flip_image_horizontal(cp_image_t* img) |
| 1228 |
{ |
| 1229 |
cp_pixel_t* pix = img->pix; |
| 1230 |
int w = img->w; |
| 1231 |
int h = img->h; |
| 1232 |
int flips = h / 2; |
| 1233 |
for (int i = 0; i < flips; ++i) |
| 1234 |
{ |
| 1235 |
cp_pixel_t* a = pix + w * i; |
| 1236 |
cp_pixel_t* b = pix + w * (h - i - 1); |
| 1237 |
for (int j = 0; j < w; ++j) |
| 1238 |
{ |
| 1239 |
cp_pixel_t t = *a; |
| 1240 |
*a = *b; |
| 1241 |
*b = t; |
| 1242 |
++a; |
| 1243 |
++b; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
void cp_load_png_wh(const void* png_data, int png_length, int* w_out, int* h_out) |
| 1249 |
{ |
| 1250 |
const char* sig = "\211PNG\r\n\032\n"; |
| 1251 |
const uint8_t* ihdr; |
| 1252 |
cp_raw_png_t png; |
| 1253 |
int w, h; |
| 1254 |
png.p = (uint8_t*)png_data; |
| 1255 |
png.end = (uint8_t*)png_data + png_length; |
| 1256 |
|
| 1257 |
if (w_out) *w_out = 0; |
| 1258 |
if (h_out) *h_out = 0; |
| 1259 |
|
| 1260 |
CUTE_PNG_CHECK(!CUTE_PNG_MEMCMP(png.p, sig, 8), "incorrect file signature (is this a png file?)"); |
| 1261 |
png.p += 8; |
| 1262 |
|
| 1263 |
ihdr = cp_chunk(&png, "IHDR", 13); |
| 1264 |
CUTE_PNG_CHECK(ihdr, "unable to find IHDR chunk"); |
| 1265 |
|
| 1266 |
// +1 for filter byte (which is dumb! just stick this at file header...) |
| 1267 |
w = cp_make32(ihdr) + 1; |
| 1268 |
h = cp_make32(ihdr + 4); |
| 1269 |
if (w_out) *w_out = w - 1; |
| 1270 |
if (h_out) *h_out = h; |
| 1271 |
|
| 1272 |
cp_err:; |
| 1273 |
} |
| 1274 |
|
| 1275 |
cp_indexed_image_t cp_load_indexed_png(const char* file_name) |
| 1276 |
{ |
| 1277 |
cp_indexed_image_t img = { 0 }; |
| 1278 |
int len; |
| 1279 |
void* data = cp_read_file_to_memory(file_name, &len); |
| 1280 |
if (!data) return img; |
| 1281 |
img = cp_load_indexed_png_mem(data, len); |
| 1282 |
CUTE_PNG_FREE(data); |
| 1283 |
return img; |
| 1284 |
} |
| 1285 |
|
| 1286 |
static void cp_unpack_indexed_rows(int w, int h, uint8_t* src, uint8_t* dst) |
| 1287 |
{ |
| 1288 |
for (int y = 0; y < h; ++y) |
| 1289 |
{ |
| 1290 |
// skip filter byte |
| 1291 |
++src; |
| 1292 |
|
| 1293 |
for (int x = 0; x < w; ++x, ++src) |
| 1294 |
{ |
| 1295 |
*dst++ = *src; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
void cp_unpack_palette(cp_pixel_t* dst, const uint8_t* plte, int plte_len, const uint8_t* trns, int trns_len) |
| 1301 |
{ |
| 1302 |
for (int i = 0; i < plte_len * 3; i += 3) |
| 1303 |
{ |
| 1304 |
unsigned char r = plte[i]; |
| 1305 |
unsigned char g = plte[i + 1]; |
| 1306 |
unsigned char b = plte[i + 2]; |
| 1307 |
unsigned char a = cp_get_alpha_for_indexed_image(i / 3, trns, trns_len); |
| 1308 |
cp_pixel_t p = cp_make_pixel_a(r, g, b, a); |
| 1309 |
*dst++ = p; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
cp_indexed_image_t cp_load_indexed_png_mem(const void *png_data, int png_length) |
| 1314 |
{ |
| 1315 |
const char* sig = "\211PNG\r\n\032\n"; |
| 1316 |
const uint8_t* ihdr, *first, *plte, *trns; |
| 1317 |
int bit_depth, color_type, bpp, w, h, pix_bytes; |
| 1318 |
int compression, filter, interlace; |
| 1319 |
int datalen, offset; |
| 1320 |
int plte_len; |
| 1321 |
uint8_t* out; |
| 1322 |
cp_indexed_image_t img = { 0 }; |
| 1323 |
uint8_t* data = 0; |
| 1324 |
cp_raw_png_t png; |
| 1325 |
png.p = (uint8_t*)png_data; |
| 1326 |
png.end = (uint8_t*)png_data + png_length; |
| 1327 |
|
| 1328 |
CUTE_PNG_CHECK(!CUTE_PNG_MEMCMP(png.p, sig, 8), "incorrect file signature (is this a png file?)"); |
| 1329 |
png.p += 8; |
| 1330 |
|
| 1331 |
ihdr = cp_chunk(&png, "IHDR", 13); |
| 1332 |
CUTE_PNG_CHECK(ihdr, "unable to find IHDR chunk"); |
| 1333 |
bit_depth = ihdr[8]; |
| 1334 |
color_type = ihdr[9]; |
| 1335 |
bpp = 1; // bytes per pixel |
| 1336 |
CUTE_PNG_CHECK(bit_depth == 8, "only bit-depth of 8 is supported"); |
| 1337 |
CUTE_PNG_CHECK(color_type == 3, "only indexed png images (images with a palette) are valid for cp_load_indexed_png_mem"); |
| 1338 |
|
| 1339 |
// +1 for filter byte (which is dumb! just stick this at file header...) |
| 1340 |
w = cp_make32(ihdr) + 1; |
| 1341 |
h = cp_make32(ihdr + 4); |
| 1342 |
CUTE_PNG_CHECK((int64_t) w * h * sizeof(uint8_t) < INT_MAX, "image too large"); |
| 1343 |
pix_bytes = w * h * sizeof(uint8_t); |
| 1344 |
img.w = w - 1; |
| 1345 |
img.h = h; |
| 1346 |
img.pix = (uint8_t*)CUTE_PNG_ALLOC(pix_bytes); |
| 1347 |
CUTE_PNG_CHECK(img.pix, "unable to allocate raw image space"); |
| 1348 |
|
| 1349 |
compression = ihdr[10]; |
| 1350 |
filter = ihdr[11]; |
| 1351 |
interlace = ihdr[12]; |
| 1352 |
CUTE_PNG_CHECK(!compression, "only standard compression DEFLATE is supported"); |
| 1353 |
CUTE_PNG_CHECK(!filter, "only standard adaptive filtering is supported"); |
| 1354 |
CUTE_PNG_CHECK(!interlace, "interlacing is not supported"); |
| 1355 |
|
| 1356 |
// PLTE must come before any IDAT chunk |
| 1357 |
first = png.p; |
| 1358 |
plte = cp_find(&png, "PLTE", 0); |
| 1359 |
if (!plte) png.p = first; |
| 1360 |
else first = png.p; |
| 1361 |
|
| 1362 |
// tRNS can come after PLTE |
| 1363 |
trns = cp_find(&png, "tRNS", 0); |
| 1364 |
if (!trns) png.p = first; |
| 1365 |
else first = png.p; |
| 1366 |
|
| 1367 |
// Compute length of the DEFLATE stream through IDAT chunk data sizes |
| 1368 |
datalen = 0; |
| 1369 |
for (const uint8_t* idat = cp_find(&png, "IDAT", 0); idat; idat = cp_chunk(&png, "IDAT", 0)) |
| 1370 |
{ |
| 1371 |
uint32_t len = cp_get_chunk_byte_length(idat); |
| 1372 |
datalen += len; |
| 1373 |
} |
| 1374 |
|
| 1375 |
// Copy in IDAT chunk data sections to form the compressed DEFLATE stream |
| 1376 |
png.p = first; |
| 1377 |
data = (uint8_t*)CUTE_PNG_ALLOC(datalen); |
| 1378 |
offset = 0; |
| 1379 |
for (const uint8_t* idat = cp_find(&png, "IDAT", 0); idat; idat = cp_chunk(&png, "IDAT", 0)) |
| 1380 |
{ |
| 1381 |
uint32_t len = cp_get_chunk_byte_length(idat); |
| 1382 |
CUTE_PNG_MEMCPY(data + offset, idat, len); |
| 1383 |
offset += len; |
| 1384 |
} |
| 1385 |
|
| 1386 |
// check for proper zlib structure in DEFLATE stream |
| 1387 |
CUTE_PNG_CHECK(data && datalen >= 6, "corrupt zlib structure in DEFLATE stream"); |
| 1388 |
CUTE_PNG_CHECK((data[0] & 0x0f) == 0x08, "only zlib compression method (RFC 1950) is supported"); |
| 1389 |
CUTE_PNG_CHECK((data[0] & 0xf0) <= 0x70, "innapropriate window size detected"); |
| 1390 |
CUTE_PNG_CHECK(!(data[1] & 0x20), "preset dictionary is present and not supported"); |
| 1391 |
|
| 1392 |
out = img.pix; |
| 1393 |
CUTE_PNG_CHECK(cp_inflate(data + 2, datalen - 6, out, pix_bytes), "DEFLATE algorithm failed"); |
| 1394 |
CUTE_PNG_CHECK(cp_unfilter(img.w, img.h, bpp, out), "invalid filter byte found"); |
| 1395 |
cp_unpack_indexed_rows(img.w, img.h, out, img.pix); |
| 1396 |
|
| 1397 |
plte_len = cp_get_chunk_byte_length(plte) / 3; |
| 1398 |
cp_unpack_palette(img.palette, plte, plte_len, trns, cp_get_chunk_byte_length(trns)); |
| 1399 |
img.palette_len = (uint8_t)plte_len; |
| 1400 |
|
| 1401 |
CUTE_PNG_FREE(data); |
| 1402 |
return img; |
| 1403 |
|
| 1404 |
cp_err: |
| 1405 |
CUTE_PNG_FREE(data); |
| 1406 |
CUTE_PNG_FREE(img.pix); |
| 1407 |
img.pix = 0; |
| 1408 |
|
| 1409 |
return img; |
| 1410 |
} |
| 1411 |
|
| 1412 |
void cp_free_indexed_png(cp_indexed_image_t* img) |
| 1413 |
{ |
| 1414 |
CUTE_PNG_FREE(img->pix); |
| 1415 |
img->pix = 0; |
| 1416 |
img->w = img->h = 0; |
| 1417 |
} |
| 1418 |
|
| 1419 |
cp_image_t cp_depallete_indexed_image(cp_indexed_image_t* img) |
| 1420 |
{ |
| 1421 |
cp_image_t out = { 0 }; |
| 1422 |
out.w = img->w; |
| 1423 |
out.h = img->h; |
| 1424 |
out.pix = (cp_pixel_t*)CUTE_PNG_ALLOC(sizeof(cp_pixel_t) * out.w * out.h); |
| 1425 |
|
| 1426 |
cp_pixel_t* dst = out.pix; |
| 1427 |
uint8_t* src = img->pix; |
| 1428 |
|
| 1429 |
for (int y = 0; y < out.h; ++y) |
| 1430 |
{ |
| 1431 |
for (int x = 0; x < out.w; ++x) |
| 1432 |
{ |
| 1433 |
int index = *src++; |
| 1434 |
cp_pixel_t p = img->palette[index]; |
| 1435 |
*dst++ = p; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
return out; |
| 1440 |
} |
| 1441 |
|
| 1442 |
typedef struct cp_v2i_t |
| 1443 |
{ |
| 1444 |
int x; |
| 1445 |
int y; |
| 1446 |
} cp_v2i_t; |
| 1447 |
|
| 1448 |
typedef struct cp_integer_image_t |
| 1449 |
{ |
| 1450 |
int img_index; |
| 1451 |
cp_v2i_t size; |
| 1452 |
cp_v2i_t min; |
| 1453 |
cp_v2i_t max; |
| 1454 |
int fit; |
| 1455 |
} cp_integer_image_t; |
| 1456 |
|
| 1457 |
static cp_v2i_t cp_v2i(int x, int y) |
| 1458 |
{ |
| 1459 |
cp_v2i_t v; |
| 1460 |
v.x = x; |
| 1461 |
v.y = y; |
| 1462 |
return v; |
| 1463 |
} |
| 1464 |
|
| 1465 |
static cp_v2i_t cp_sub(cp_v2i_t a, cp_v2i_t b) |
| 1466 |
{ |
| 1467 |
cp_v2i_t v; |
| 1468 |
v.x = a.x - b.x; |
| 1469 |
v.y = a.y - b.y; |
| 1470 |
return v; |
| 1471 |
} |
| 1472 |
|
| 1473 |
static cp_v2i_t cp_add(cp_v2i_t a, cp_v2i_t b) |
| 1474 |
{ |
| 1475 |
cp_v2i_t v; |
| 1476 |
v.x = a.x + b.x; |
| 1477 |
v.y = a.y + b.y; |
| 1478 |
return v; |
| 1479 |
} |
| 1480 |
|
| 1481 |
typedef struct cp_atlas_node_t |
| 1482 |
{ |
| 1483 |
cp_v2i_t size; |
| 1484 |
cp_v2i_t min; |
| 1485 |
cp_v2i_t max; |
| 1486 |
} cp_atlas_node_t; |
| 1487 |
|
| 1488 |
static cp_atlas_node_t* cp_best_fit(int sp, const cp_image_t* png, cp_atlas_node_t* nodes) |
| 1489 |
{ |
| 1490 |
int bestVolume = INT_MAX; |
| 1491 |
cp_atlas_node_t *best_node = 0; |
| 1492 |
int width = png->w; |
| 1493 |
int height = png->h; |
| 1494 |
int png_volume = width * height; |
| 1495 |
|
| 1496 |
for (int i = 0; i < sp; ++i) |
| 1497 |
{ |
| 1498 |
cp_atlas_node_t *node = nodes + i; |
| 1499 |
int can_contain = node->size.x >= width && node->size.y >= height; |
| 1500 |
if (can_contain) |
| 1501 |
{ |
| 1502 |
int node_volume = node->size.x * node->size.y; |
| 1503 |
if (node_volume == png_volume) return node; |
| 1504 |
if (node_volume < bestVolume) |
| 1505 |
{ |
| 1506 |
bestVolume = node_volume; |
| 1507 |
best_node = node; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
return best_node; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static int cp_perimeter_pred(cp_integer_image_t* a, cp_integer_image_t* b) |
| 1516 |
{ |
| 1517 |
int perimeterA = 2 * (a->size.x + a->size.y); |
| 1518 |
int perimeterB = 2 * (b->size.x + b->size.y); |
| 1519 |
return perimeterB < perimeterA; |
| 1520 |
} |
| 1521 |
|
| 1522 |
void cp_premultiply(cp_image_t* img) |
| 1523 |
{ |
| 1524 |
int w = img->w; |
| 1525 |
int h = img->h; |
| 1526 |
int stride = w * sizeof(cp_pixel_t); |
| 1527 |
uint8_t* data = (uint8_t*)img->pix; |
| 1528 |
|
| 1529 |
for(int i = 0; i < (int)stride * h; i += sizeof(cp_pixel_t)) |
| 1530 |
{ |
| 1531 |
float a = (float)data[i + 3] / 255.0f; |
| 1532 |
float r = (float)data[i + 0] / 255.0f; |
| 1533 |
float g = (float)data[i + 1] / 255.0f; |
| 1534 |
float b = (float)data[i + 2] / 255.0f; |
| 1535 |
r *= a; |
| 1536 |
g *= a; |
| 1537 |
b *= a; |
| 1538 |
data[i + 0] = (uint8_t)(r * 255.0f); |
| 1539 |
data[i + 1] = (uint8_t)(g * 255.0f); |
| 1540 |
data[i + 2] = (uint8_t)(b * 255.0f); |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
static void cp_qsort(cp_integer_image_t* items, int count) |
| 1545 |
{ |
| 1546 |
if (count <= 1) return; |
| 1547 |
|
| 1548 |
cp_integer_image_t pivot = items[count - 1]; |
| 1549 |
int low = 0; |
| 1550 |
for (int i = 0; i < count - 1; ++i) |
| 1551 |
{ |
| 1552 |
if (cp_perimeter_pred(items + i, &pivot)) |
| 1553 |
{ |
| 1554 |
cp_integer_image_t tmp = items[i]; |
| 1555 |
items[i] = items[low]; |
| 1556 |
items[low] = tmp; |
| 1557 |
low++; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
items[count - 1] = items[low]; |
| 1562 |
items[low] = pivot; |
| 1563 |
cp_qsort(items, low); |
| 1564 |
cp_qsort(items + low + 1, count - 1 - low); |
| 1565 |
} |
| 1566 |
|
| 1567 |
static void cp_write_pixel(char* mem, long color) { |
| 1568 |
mem[0] = (color >> 24) & 0xFF; |
| 1569 |
mem[1] = (color >> 16) & 0xFF; |
| 1570 |
mem[2] = (color >> 8) & 0xFF; |
| 1571 |
mem[3] = (color >> 0) & 0xFF; |
| 1572 |
} |
| 1573 |
|
| 1574 |
cp_image_t cp_make_atlas(int atlas_width, int atlas_height, const cp_image_t* pngs, int png_count, cp_atlas_image_t* imgs_out) |
| 1575 |
{ |
| 1576 |
float w0, h0, div, wTol, hTol; |
| 1577 |
int atlas_image_size, atlas_stride, sp; |
| 1578 |
void* atlas_pixels = 0; |
| 1579 |
int atlas_node_capacity = png_count * 2; |
| 1580 |
cp_image_t atlas_image; |
| 1581 |
cp_integer_image_t* images = 0; |
| 1582 |
cp_atlas_node_t* nodes = 0; |
| 1583 |
|
| 1584 |
atlas_image.w = atlas_width; |
| 1585 |
atlas_image.h = atlas_height; |
| 1586 |
atlas_image.pix = 0; |
| 1587 |
|
| 1588 |
CUTE_PNG_CHECK(pngs, "pngs array was NULL"); |
| 1589 |
CUTE_PNG_CHECK(imgs_out, "imgs_out array was NULL"); |
| 1590 |
|
| 1591 |
images = (cp_integer_image_t*)CUTE_PNG_ALLOCA(sizeof(cp_integer_image_t) * png_count); |
| 1592 |
nodes = (cp_atlas_node_t*)CUTE_PNG_ALLOC(sizeof(cp_atlas_node_t) * atlas_node_capacity); |
| 1593 |
CUTE_PNG_CHECK(images, "out of mem"); |
| 1594 |
CUTE_PNG_CHECK(nodes, "out of mem"); |
| 1595 |
|
| 1596 |
for (int i = 0; i < png_count; ++i) |
| 1597 |
{ |
| 1598 |
const cp_image_t* png = pngs + i; |
| 1599 |
cp_integer_image_t* image = images + i; |
| 1600 |
image->fit = 0; |
| 1601 |
image->size = cp_v2i(png->w, png->h); |
| 1602 |
image->img_index = i; |
| 1603 |
} |
| 1604 |
|
| 1605 |
// Sort PNGs from largest to smallest |
| 1606 |
cp_qsort(images, png_count); |
| 1607 |
|
| 1608 |
// stack pointer, the stack is the nodes array which we will |
| 1609 |
// allocate nodes from as necessary. |
| 1610 |
sp = 1; |
| 1611 |
|
| 1612 |
nodes[0].min = cp_v2i(0, 0); |
| 1613 |
nodes[0].max = cp_v2i(atlas_width, atlas_height); |
| 1614 |
nodes[0].size = cp_v2i(atlas_width, atlas_height); |
| 1615 |
|
| 1616 |
// Nodes represent empty space in the atlas. Placing a texture into the |
| 1617 |
// atlas involves splitting a node into two smaller pieces (or, if a |
| 1618 |
// perfect fit is found, deleting the node). |
| 1619 |
for (int i = 0; i < png_count; ++i) |
| 1620 |
{ |
| 1621 |
cp_integer_image_t* image = images + i; |
| 1622 |
const cp_image_t* png = pngs + image->img_index; |
| 1623 |
int width = png->w; |
| 1624 |
int height = png->h; |
| 1625 |
cp_atlas_node_t *best_fit = cp_best_fit(sp, png, nodes); |
| 1626 |
if (CUTE_PNG_ATLAS_MUST_FIT) CUTE_PNG_CHECK(best_fit, "Not enough room to place image in atlas."); |
| 1627 |
else if (!best_fit) |
| 1628 |
{ |
| 1629 |
image->fit = 0; |
| 1630 |
continue; |
| 1631 |
} |
| 1632 |
|
| 1633 |
image->min = best_fit->min; |
| 1634 |
image->max = cp_add(image->min, image->size); |
| 1635 |
|
| 1636 |
if (best_fit->size.x == width && best_fit->size.y == height) |
| 1637 |
{ |
| 1638 |
cp_atlas_node_t* last_node = nodes + --sp; |
| 1639 |
*best_fit = *last_node; |
| 1640 |
image->fit = 1; |
| 1641 |
|
| 1642 |
continue; |
| 1643 |
} |
| 1644 |
|
| 1645 |
image->fit = 1; |
| 1646 |
|
| 1647 |
if (sp == atlas_node_capacity) |
| 1648 |
{ |
| 1649 |
int new_capacity = atlas_node_capacity * 2; |
| 1650 |
cp_atlas_node_t* new_nodes = (cp_atlas_node_t*)CUTE_PNG_ALLOC(sizeof(cp_atlas_node_t) * new_capacity); |
| 1651 |
CUTE_PNG_CHECK(new_nodes, "out of mem"); |
| 1652 |
CUTE_PNG_MEMCPY(new_nodes, nodes, sizeof(cp_atlas_node_t) * sp); |
| 1653 |
|
| 1654 |
// best_fit became a dangling pointer, so relocate it |
| 1655 |
best_fit = new_nodes + (best_fit - nodes); |
| 1656 |
CUTE_PNG_FREE(nodes); |
| 1657 |
|
| 1658 |
nodes = new_nodes; |
| 1659 |
atlas_node_capacity = new_capacity; |
| 1660 |
} |
| 1661 |
|
| 1662 |
cp_atlas_node_t* new_node = nodes + sp++; |
| 1663 |
new_node->min = best_fit->min; |
| 1664 |
|
| 1665 |
// Split bestFit along x or y, whichever minimizes |
| 1666 |
// fragmentation of empty space |
| 1667 |
cp_v2i_t d = cp_sub(best_fit->size, cp_v2i(width, height)); |
| 1668 |
if (d.x < d.y) |
| 1669 |
{ |
| 1670 |
new_node->size.x = d.x; |
| 1671 |
new_node->size.y = height; |
| 1672 |
new_node->min.x += width; |
| 1673 |
|
| 1674 |
best_fit->size.y = d.y; |
| 1675 |
best_fit->min.y += height; |
| 1676 |
} |
| 1677 |
|
| 1678 |
else |
| 1679 |
{ |
| 1680 |
new_node->size.x = width; |
| 1681 |
new_node->size.y = d.y; |
| 1682 |
new_node->min.y += height; |
| 1683 |
|
| 1684 |
best_fit->size.x = d.x; |
| 1685 |
best_fit->min.x += width; |
| 1686 |
} |
| 1687 |
|
| 1688 |
new_node->max = cp_add(new_node->min, new_node->size); |
| 1689 |
} |
| 1690 |
|
| 1691 |
// Write the final atlas image, use CUTE_PNG_ATLAS_EMPTY_COLOR as base color |
| 1692 |
atlas_stride = atlas_width * sizeof(cp_pixel_t); |
| 1693 |
atlas_image_size = atlas_width * atlas_height * sizeof(cp_pixel_t); |
| 1694 |
atlas_pixels = CUTE_PNG_ALLOC(atlas_image_size); |
| 1695 |
CUTE_PNG_CHECK(atlas_pixels, "out of mem"); |
| 1696 |
|
| 1697 |
for(int i = 0; i < atlas_image_size; i += sizeof(cp_pixel_t)) { |
| 1698 |
cp_write_pixel((char*)atlas_pixels + i, CUTE_PNG_ATLAS_EMPTY_COLOR); |
| 1699 |
} |
| 1700 |
|
| 1701 |
for (int i = 0; i < png_count; ++i) |
| 1702 |
{ |
| 1703 |
cp_integer_image_t* image = images + i; |
| 1704 |
|
| 1705 |
if (image->fit) |
| 1706 |
{ |
| 1707 |
const cp_image_t* png = pngs + image->img_index; |
| 1708 |
char* pixels = (char*)png->pix; |
| 1709 |
cp_v2i_t min = image->min; |
| 1710 |
cp_v2i_t max = image->max; |
| 1711 |
int atlas_offset = min.x * sizeof(cp_pixel_t); |
| 1712 |
int tex_stride = png->w * sizeof(cp_pixel_t); |
| 1713 |
|
| 1714 |
for (int row = min.y, y = 0; row < max.y; ++row, ++y) |
| 1715 |
{ |
| 1716 |
void* row_ptr = (char*)atlas_pixels + (row * atlas_stride + atlas_offset); |
| 1717 |
CUTE_PNG_MEMCPY(row_ptr, pixels + y * tex_stride, tex_stride); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
atlas_image.pix = (cp_pixel_t*)atlas_pixels; |
| 1723 |
|
| 1724 |
// squeeze UVs inward by 128th of a pixel |
| 1725 |
// this prevents atlas bleeding. tune as necessary for good results. |
| 1726 |
w0 = 1.0f / (float)(atlas_width); |
| 1727 |
h0 = 1.0f / (float)(atlas_height); |
| 1728 |
div = 1.0f / 128.0f; |
| 1729 |
wTol = w0 * div; |
| 1730 |
hTol = h0 * div; |
| 1731 |
|
| 1732 |
for (int i = 0; i < png_count; ++i) |
| 1733 |
{ |
| 1734 |
cp_integer_image_t* image = images + i; |
| 1735 |
cp_atlas_image_t* img_out = imgs_out + i; |
| 1736 |
|
| 1737 |
img_out->img_index = image->img_index; |
| 1738 |
img_out->w = image->size.x; |
| 1739 |
img_out->h = image->size.y; |
| 1740 |
img_out->fit = image->fit; |
| 1741 |
|
| 1742 |
if (image->fit) |
| 1743 |
{ |
| 1744 |
cp_v2i_t min = image->min; |
| 1745 |
cp_v2i_t max = image->max; |
| 1746 |
|
| 1747 |
float min_x = (float)min.x * w0 + wTol; |
| 1748 |
float min_y = (float)min.y * h0 + hTol; |
| 1749 |
float max_x = (float)max.x * w0 - wTol; |
| 1750 |
float max_y = (float)max.y * h0 - hTol; |
| 1751 |
|
| 1752 |
// flip image on y axis |
| 1753 |
if (CUTE_PNG_ATLAS_FLIP_Y_AXIS_FOR_UV) |
| 1754 |
{ |
| 1755 |
float tmp = min_y; |
| 1756 |
min_y = max_y; |
| 1757 |
max_y = tmp; |
| 1758 |
} |
| 1759 |
|
| 1760 |
img_out->minx = min_x; |
| 1761 |
img_out->miny = min_y; |
| 1762 |
img_out->maxx = max_x; |
| 1763 |
img_out->maxy = max_y; |
| 1764 |
} |
| 1765 |
} |
| 1766 |
|
| 1767 |
CUTE_PNG_FREE(nodes); |
| 1768 |
return atlas_image; |
| 1769 |
|
| 1770 |
cp_err: |
| 1771 |
CUTE_PNG_FREE(atlas_pixels); |
| 1772 |
CUTE_PNG_FREE(nodes); |
| 1773 |
atlas_image.pix = 0; |
| 1774 |
return atlas_image; |
| 1775 |
} |
| 1776 |
|
| 1777 |
int cp_default_save_atlas(const char* out_path_image, const char* out_path_atlas_txt, const cp_image_t* atlas, const cp_atlas_image_t* imgs, int img_count, const char** names) |
| 1778 |
{ |
| 1779 |
CUTE_PNG_FILE* fp = CUTE_PNG_FOPEN(out_path_atlas_txt, "wt"); |
| 1780 |
CUTE_PNG_CHECK(fp, "unable to open out_path_atlas_txt in cp_default_save_atlas"); |
| 1781 |
|
| 1782 |
CUTE_PNG_FPRINTF(fp, "%s\n%d\n\n", out_path_image, img_count); |
| 1783 |
|
| 1784 |
for (int i = 0; i < img_count; ++i) |
| 1785 |
{ |
| 1786 |
const cp_atlas_image_t* image = imgs + i; |
| 1787 |
const char* name = names ? names[image->img_index] : 0; |
| 1788 |
|
| 1789 |
if (image->fit) |
| 1790 |
{ |
| 1791 |
int width = image->w; |
| 1792 |
int height = image->h; |
| 1793 |
float min_x = image->minx; |
| 1794 |
float min_y = image->miny; |
| 1795 |
float max_x = image->maxx; |
| 1796 |
float max_y = image->maxy; |
| 1797 |
|
| 1798 |
if (name) CUTE_PNG_FPRINTF(fp, "{ \"%s\", w = %d, h = %d, u = { %.10f, %.10f }, v = { %.10f, %.10f } }\n", name, width, height, min_x, min_y, max_x, max_y); |
| 1799 |
else CUTE_PNG_FPRINTF(fp, "{ w = %d, h = %d, u = { %.10f, %.10f }, v = { %.10f, %.10f } }\n", width, height, min_x, min_y, max_x, max_y); |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
// Save atlas image PNG to disk |
| 1804 |
CUTE_PNG_CHECK(cp_save_png(out_path_image, atlas), "failed to save atlas image to disk"); |
| 1805 |
|
| 1806 |
cp_err: |
| 1807 |
CUTE_PNG_FCLOSE(fp); |
| 1808 |
return 0; |
| 1809 |
} |
| 1810 |
|
| 1811 |
#endif // CUTE_PNG_IMPLEMENTATION_ONCE |
| 1812 |
#endif // CUTE_PNG_IMPLEMENTATION |
| 1813 |
|
| 1814 |
/* |
| 1815 |
------------------------------------------------------------------------------ |
| 1816 |
This software is available under 2 licenses - you may choose the one you like. |
| 1817 |
------------------------------------------------------------------------------ |
| 1818 |
ALTERNATIVE A - zlib license |
| 1819 |
Copyright (c) 2019 Randy Gaul http://www.randygaul.net |
| 1820 |
This software is provided 'as-is', without any express or implied warranty. |
| 1821 |
In no event will the authors be held liable for any damages arising from |
| 1822 |
the use of this software. |
| 1823 |
Permission is granted to anyone to use this software for any purpose, |
| 1824 |
including commercial applications, and to alter it and redistribute it |
| 1825 |
freely, subject to the following restrictions: |
| 1826 |
1. The origin of this software must not be misrepresented; you must not |
| 1827 |
claim that you wrote the original software. If you use this software |
| 1828 |
in a product, an acknowledgment in the product documentation would be |
| 1829 |
appreciated but is not required. |
| 1830 |
2. Altered source versions must be plainly marked as such, and must not |
| 1831 |
be misrepresented as being the original software. |
| 1832 |
3. This notice may not be removed or altered from any source distribution. |
| 1833 |
------------------------------------------------------------------------------ |
| 1834 |
ALTERNATIVE B - Public Domain (www.unlicense.org) |
| 1835 |
This is free and unencumbered software released into the public domain. |
| 1836 |
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this |
| 1837 |
software, either in source code form or as a compiled binary, for any purpose, |
| 1838 |
commercial or non-commercial, and by any means. |
| 1839 |
In jurisdictions that recognize copyright laws, the author or authors of this |
| 1840 |
software dedicate any and all copyright interest in the software to the public |
| 1841 |
domain. We make this dedication for the benefit of the public at large and to |
| 1842 |
the detriment of our heirs and successors. We intend this dedication to be an |
| 1843 |
overt act of relinquishment in perpetuity of all present and future rights to |
| 1844 |
this software under copyright law. |
| 1845 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 1846 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 1847 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 1848 |
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 1849 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 1850 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 1851 |
------------------------------------------------------------------------------ |
| 1852 |
*/ |