|
| 1 | +/* |
| 2 | +Copyright (C) 2017 Milo Yip. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are met: |
| 6 | +
|
| 7 | +* Redistributions of source code must retain the above copyright notice, this |
| 8 | + list of conditions and the following disclaimer. |
| 9 | +
|
| 10 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer in the documentation |
| 12 | + and/or other materials provided with the distribution. |
| 13 | +
|
| 14 | +* Neither the name of pngout nor the names of its |
| 15 | + contributors may be used to endorse or promote products derived from |
| 16 | + this software without specific prior written permission. |
| 17 | +
|
| 18 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +*/ |
| 29 | + |
| 30 | +/*! \file |
| 31 | + \brief svpng() is a minimalistic C function for saving RGB/RGBA image into uncompressed PNG. |
| 32 | + \author Milo Yip |
| 33 | + \version 0.1.1 |
| 34 | + \copyright MIT license |
| 35 | + \sa http://github.com/miloyip/svpng |
| 36 | +*/ |
| 37 | + |
| 38 | +#ifndef SVPNG_INC_ |
| 39 | +#define SVPNG_INC_ |
| 40 | + |
| 41 | +/*! \def SVPNG_LINKAGE |
| 42 | + \brief User customizable linkage for svpng() function. |
| 43 | + By default this macro is empty. |
| 44 | + User may define this macro as static for static linkage, |
| 45 | + and/or inline in C99/C++, etc. |
| 46 | +*/ |
| 47 | +#ifndef SVPNG_LINKAGE |
| 48 | +#define SVPNG_LINKAGE static |
| 49 | +#endif |
| 50 | + |
| 51 | +/*! \def SVPNG_OUTPUT |
| 52 | + \brief User customizable output stream. |
| 53 | + By default, it uses C file descriptor and fputc() to output bytes. |
| 54 | + In C++, for example, user may use std::ostream or std::vector instead. |
| 55 | +*/ |
| 56 | +#ifndef SVPNG_OUTPUT |
| 57 | +#include <stdio.h> |
| 58 | +#define SVPNG_OUTPUT FILE* fp |
| 59 | +#endif |
| 60 | + |
| 61 | +/*! \def SVPNG_PUT |
| 62 | + \brief Write a byte |
| 63 | +*/ |
| 64 | +#ifndef SVPNG_PUT |
| 65 | +#define SVPNG_PUT(u) fputc(u, fp) |
| 66 | +#endif |
| 67 | + |
| 68 | + |
| 69 | +/*! |
| 70 | + \brief Save a RGB/RGBA image in PNG format. |
| 71 | + \param SVPNG_OUTPUT Output stream (by default using file descriptor). |
| 72 | + \param w Width of the image. (<16383) |
| 73 | + \param h Height of the image. |
| 74 | + \param img Image pixel data in 24-bit RGB or 32-bit RGBA format. |
| 75 | + \param alpha Whether the image contains alpha channel. |
| 76 | +*/ |
| 77 | +SVPNG_LINKAGE void svpng(SVPNG_OUTPUT, unsigned w, unsigned h, const unsigned char* img, int alpha) { |
| 78 | + static const unsigned t[] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
| 79 | + /* CRC32 Table */ 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; |
| 80 | + unsigned a = 1, b = 0, c, p = w * (alpha ? 4 : 3) + 1, x, y, i; /* ADLER-a, ADLER-b, CRC, pitch */ |
| 81 | +#define SVPNG_U8A(ua, l) for (i = 0; i < l; i++) SVPNG_PUT((ua)[i]); |
| 82 | +#define SVPNG_U32(u) do { SVPNG_PUT((u) >> 24); SVPNG_PUT(((u) >> 16) & 255); SVPNG_PUT(((u) >> 8) & 255); SVPNG_PUT((u) & 255); } while(0) |
| 83 | +#define SVPNG_U8C(u) do { SVPNG_PUT(u); c ^= (u); c = (c >> 4) ^ t[c & 15]; c = (c >> 4) ^ t[c & 15]; } while(0) |
| 84 | +#define SVPNG_U8AC(ua, l) for (i = 0; i < l; i++) SVPNG_U8C((ua)[i]) |
| 85 | +#define SVPNG_U16LC(u) do { SVPNG_U8C((u) & 255); SVPNG_U8C(((u) >> 8) & 255); } while(0) |
| 86 | +#define SVPNG_U32C(u) do { SVPNG_U8C((u) >> 24); SVPNG_U8C(((u) >> 16) & 255); SVPNG_U8C(((u) >> 8) & 255); SVPNG_U8C((u) & 255); } while(0) |
| 87 | +#define SVPNG_U8ADLER(u) do { SVPNG_U8C(u); a = (a + (u)) % 65521; b = (b + a) % 65521; } while(0) |
| 88 | +#define SVPNG_BEGIN(s, l) do { SVPNG_U32(l); c = ~0U; SVPNG_U8AC(s, 4); } while(0) |
| 89 | +#define SVPNG_END() SVPNG_U32(~c) |
| 90 | + SVPNG_U8A("\x89PNG\r\n\32\n", 8); /* Magic */ |
| 91 | + SVPNG_BEGIN("IHDR", 13); /* IHDR chunk { */ |
| 92 | + SVPNG_U32C(w); SVPNG_U32C(h); /* Width & Height (8 bytes) */ |
| 93 | + SVPNG_U8C(8); SVPNG_U8C(alpha ? 6 : 2); /* Depth=8, Color=True color with/without alpha (2 bytes) */ |
| 94 | + SVPNG_U8AC("\0\0\0", 3); /* Compression=Deflate, Filter=No, Interlace=No (3 bytes) */ |
| 95 | + SVPNG_END(); /* } */ |
| 96 | + SVPNG_BEGIN("IDAT", 2 + h * (5 + p) + 4); /* IDAT chunk { */ |
| 97 | + SVPNG_U8AC("\x78\1", 2); /* Deflate block begin (2 bytes) */ |
| 98 | + for (y = 0; y < h; y++) { /* Each horizontal line makes a block for simplicity */ |
| 99 | + SVPNG_U8C(y == h - 1); /* 1 for the last block, 0 for others (1 byte) */ |
| 100 | + SVPNG_U16LC(p); SVPNG_U16LC(~p); /* Size of block in little endian and its 1's complement (4 bytes) */ |
| 101 | + SVPNG_U8ADLER(0); /* No filter prefix (1 byte) */ |
| 102 | + for (x = 0; x < p - 1; x += 4, img += 4) |
| 103 | + { |
| 104 | + SVPNG_U8ADLER(img[3]); /* Image pixel data */ |
| 105 | + SVPNG_U8ADLER(img[2]); /* Image pixel data */ |
| 106 | + SVPNG_U8ADLER(img[1]); /* Image pixel data */ |
| 107 | + SVPNG_U8ADLER(img[0]); /* Image pixel data */ |
| 108 | + } |
| 109 | + } |
| 110 | + SVPNG_U32C((b << 16) | a); /* Deflate block end with adler (4 bytes) */ |
| 111 | + SVPNG_END(); /* } */ |
| 112 | + SVPNG_BEGIN("IEND", 0); SVPNG_END(); /* IEND chunk {} */ |
| 113 | +} |
| 114 | + |
| 115 | +#endif /* SVPNG_INC_ */ |
0 commit comments