A very small library for reading image files
1#pragma once
2
3typedef unsigned long int ImgSz;
4typedef signed long int ImgOff;
5typedef unsigned int ImgU32;
6typedef signed int ImgS32;
7typedef unsigned short int ImgU16;
8typedef signed char ImgS8;
9typedef unsigned char ImgU8;
10typedef void *ImgAny;
11
12enum ImgStatusE {
13 ImgOK,
14 ImgErrUnimplemented,
15 ImgErrIO,
16 ImgErrUnknownFormat,
17 ImgErrUnopened,
18 ImgErrBufferTooSmall,
19 ImgErrQoiInvalidChannelCount,
20 ImgErrQoiInvalidColorSpace,
21 ImgErrPbmValueTooLarge,
22 ImgErrPbmInvalidInteger,
23 ImgErrBmpMalformedHeader,
24 ImgErrBmpUnsupportedVariant,
25 ImgErrBmpPlaneCount,
26 ImgErrBmpUnsupportedCompression,
27 ImgErrBmpUnsupportedBitDepth,
28 ImgErrBmpUnexpectedEndOfBitmap,
29 ImgErrTgaMissingColorMap,
30 ImgErrTgaColorMapTooLong,
31 ImgErrTgaNoImageData,
32 ImgErrTgaUnsupportedPixelDepth,
33 ImgErrGifExpectedImageDescriptor,
34 ImgErrGifExpectedTrailer,
35};
36
37typedef enum ImgStatusE ImgStatus;
38
39typedef ImgStatus(*ImgReadFunc)(ImgAny user, ImgAny buf, ImgSz size);
40typedef ImgStatus(*ImgSeekFunc)(ImgAny user, ImgOff off);
41typedef ImgStatus(*ImgCloseFunc)(ImgAny user);
42
43struct ImgFuncsS {
44 ImgAny user;
45 ImgReadFunc read;
46 ImgSeekFunc seek;
47 ImgCloseFunc close;
48};
49
50typedef struct ImgFuncsS ImgFuncs;
51
52enum ImgFormatE {
53 ImgFormatUnknown,
54 ImgFormatQoi,
55 ImgFormatPbm,
56 ImgFormatBmp,
57 ImgFormatTga,
58 ImgFormatGif,
59};
60
61typedef enum ImgFormatE ImgFormat;
62
63struct ImgQoi {
64 ImgU8 colorSpace;
65};
66
67struct ImgPbm {
68 char variant;
69 ImgU16 max;
70};
71
72struct ImgBmpRGBQuad {
73 ImgU8 blue;
74 ImgU8 green;
75 ImgU8 red;
76 ImgU8 reserved;
77};
78
79#define IMG_BMP_COMPRESSION_RGB 0
80#define IMG_BMP_COMPRESSION_RLE8 1
81#define IMG_BMP_COMPRESSION_RLE4 2
82#define IMG_BMP_COMPRESSION_BITFIELDS 3
83
84struct ImgBmp {
85 ImgU32 bitmapOffset;
86 ImgS32 width;
87 ImgS32 height;
88 ImgU32 headerSize;
89 ImgU16 bitCount;
90 ImgU32 compression;
91 ImgU32 imageSize;
92 ImgS32 xPPM; /* x pixels per meter */
93 ImgS32 yPPM; /* y pixels per meter */
94 ImgU32 colorsUsed;
95 ImgU32 colorTableSize;
96 struct ImgBmpRGBQuad colorTable[256];
97 ImgU32 redMask;
98 ImgU32 greenMask;
99 ImgU32 blueMask;
100 ImgU32 alphaMask;
101};
102
103/* for color map, converted to RGBA32 for convenience */
104struct ImgTgaColor {
105 ImgU8 red;
106 ImgU8 green;
107 ImgU8 blue;
108 ImgU8 alpha;
109};
110
111/* just use true-color at that point */
112#define IMG_TGA_MAX_COLOR_MAP_SIZE 256
113
114struct ImgTga {
115 ImgU8 idLength;
116 /* 1 extra for null byte */
117 char id[256];
118 /* 1 if has color map, 0 otherwise */
119 ImgU8 colorMapType;
120 ImgU8 imageType;
121 ImgU16 colorMapFirstIndex;
122 ImgU16 colorMapLength;
123 ImgU8 colorMapEntrySize;
124 ImgU16 originX;
125 ImgU16 originY;
126 ImgU16 width;
127 ImgU16 height;
128 ImgU8 imageOrigin;
129 ImgU8 pixelDepth;
130 ImgU8 alphaBits;
131 struct ImgTgaColor colorMap[IMG_TGA_MAX_COLOR_MAP_SIZE];
132};
133
134/* for color table, converted to RGB24 for convenience */
135struct ImgGifColor {
136 ImgU8 red;
137 ImgU8 green;
138 ImgU8 blue;
139};
140
141struct ImgGif {
142 ImgU8 globalColorTableFlag;
143 ImgU8 colorResolution;
144 ImgU8 sortFlag;
145 ImgU16 globalColorTableSize;
146 ImgU8 backgroundColorIndex;
147 ImgU8 pixelAspectRatio;
148 struct ImgGifColor globalColorTable[256];
149};
150
151struct ImgS {
152 ImgFormat format;
153 ImgU32 width;
154 ImgU32 height;
155 ImgU8 channelCount;
156 ImgFuncs funcs;
157 union {
158 struct ImgQoi qoi;
159 struct ImgPbm pbm;
160 struct ImgBmp bmp;
161 struct ImgTga tga;
162 struct ImgGif gif;
163 };
164};
165
166typedef struct ImgS Img;
167
168ImgStatus imgOpen(Img *img, const ImgFuncs *funcs);
169ImgStatus imgRead(Img *img, ImgAny buf, ImgSz bufSz);
170ImgStatus imgClose(Img *img);
171
172const char *imgFormatName(ImgFormat format);
173const char *imgStatusMessage(ImgStatus status);