| 1 |
#ifndef QOI_H_INCLUDED |
| 2 |
#define QOI_H_INCLUDED |
| 3 |
/* |
| 4 |
|
| 5 |
QOI - The "Quite OK Image" format for fast, lossless image compression |
| 6 |
|
| 7 |
Dominic Szablewski - https://phoboslab.org |
| 8 |
|
| 9 |
|
| 10 |
-- LICENSE: The MIT License(MIT) |
| 11 |
|
| 12 |
Copyright(c) 2021 Dominic Szablewski |
| 13 |
|
| 14 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 15 |
this software and associated documentation files(the "Software"), to deal in |
| 16 |
the Software without restriction, including without limitation the rights to |
| 17 |
use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies |
| 18 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 19 |
so, subject to the following conditions : |
| 20 |
The above copyright notice and this permission notice shall be included in all |
| 21 |
copies or substantial portions of the Software. |
| 22 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
| 25 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 |
SOFTWARE. |
| 29 |
|
| 30 |
*/ |
| 31 |
|
| 32 |
|
| 33 |
typedef enum { |
| 34 |
QOI_SRGB = 0, |
| 35 |
QOI_LINEAR = 1 |
| 36 |
} qoi_Colorspace; |
| 37 |
|
| 38 |
|
| 39 |
typedef struct { |
| 40 |
unsigned int width; |
| 41 |
unsigned int height; |
| 42 |
unsigned int channelCt; |
| 43 |
qoi_Colorspace colorspace; |
| 44 |
} qoi_Desc; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define QOI_OP_INDEX 0x00 /* 00xxxxxx */ |
| 49 |
#define QOI_OP_DIFF 0x40 /* 01xxxxxx */ |
| 50 |
#define QOI_OP_LUMA 0x80 /* 10xxxxxx */ |
| 51 |
#define QOI_OP_RUN 0xc0 /* 11xxxxxx */ |
| 52 |
#define QOI_OP_RGB 0xfe /* 11111110 */ |
| 53 |
#define QOI_OP_RGBA 0xff /* 11111111 */ |
| 54 |
|
| 55 |
#define QOI_MASK_2 0xc0 /* 11000000 */ |
| 56 |
|
| 57 |
#define QOI_HEADER_SIZE 14 |
| 58 |
|
| 59 |
/* 2GB is the max file size that this implementation can safely handle. We |
| 60 |
guard against anything larger than that, assuming the worst case with 5 |
| 61 |
bytes per pixel, rounded down to a nice clean value. 400 million pixels |
| 62 |
ought to be enough for anybody. |
| 63 |
*/ |
| 64 |
#define QOI_PIXELS_MAX 400000000 |
| 65 |
|
| 66 |
static unsigned int const qoi_pixels_max = (unsigned int) QOI_PIXELS_MAX; |
| 67 |
|
| 68 |
#define QOI_MAXVAL 255 |
| 69 |
|
| 70 |
#define QOI_INDEX_SIZE 64 |
| 71 |
|
| 72 |
|
| 73 |
typedef union { |
| 74 |
struct { unsigned char r, g, b, a; } rgba; |
| 75 |
unsigned int v; |
| 76 |
} qoi_Rgba; |
| 77 |
|
| 78 |
static __inline__ unsigned int |
| 79 |
qoi_colorHash(qoi_Rgba const x) { |
| 80 |
|
| 81 |
return |
| 82 |
(x.rgba.r*3 + x.rgba.g*5 + x.rgba.b*7 + x.rgba.a*11) % QOI_INDEX_SIZE; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static __inline__ void |
| 88 |
qoi_clearQoiIndex(qoi_Rgba * index) { |
| 89 |
|
| 90 |
memset(index, 0, QOI_INDEX_SIZE * sizeof(qoi_Rgba)); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#define QOI_MAGIC_SIZE 4 |
| 97 |
|
| 98 |
static char const qoi_magic[QOI_MAGIC_SIZE + 1] = {'q','o','i','f','\0'}; |
| 99 |
|
| 100 |
#define QOI_PADDING_SIZE 8 |
| 101 |
|
| 102 |
static unsigned char const qoi_padding[QOI_PADDING_SIZE] = {0,0,0,0,0,0,0,1}; |
| 103 |
|
| 104 |
|
| 105 |
#endif |