-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtgafunc.h
280 lines (258 loc) · 9.26 KB
/
tgafunc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2021 Caden Ji
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef TGAFUNC_H_
#define TGAFUNC_H_
#include <stdint.h>
#define TGA_MAX_IMAGE_DIMENSIONS 65535
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
///
/// \brief Image pixel format.
///
/// The pixel data are all in little-endian. E.g. a TGA_PIXEL_ARGB32 format
/// image, a single pixel is stored in the memory in the order of
/// BBBBBBBB GGGGGGGG RRRRRRRR AAAAAAAA.
///
enum tga_pixel_format {
///
/// \brief Single channel format represents grayscale, 8-bit integer.
///
TGA_PIXEL_BW8,
///
/// \brief Single channel format represents grayscale, 16-bit integer.
///
TGA_PIXEL_BW16,
///
/// \brief A 16-bit pixel format.
/// The topmost bit is assumed to an attribute bit, usually ignored.
/// Because of little-endian, this format pixel is stored in the memory in
/// the order of GGGBBBBB ARRRRRGG.
///
TGA_PIXEL_RGB555,
///
/// \brief RGB color format, 8-bit per channel.
///
TGA_PIXEL_RGB24,
///
/// \brief RGB color with alpha format, 8-bit per channel.
///
TGA_PIXEL_ARGB32
};
///
/// \brief Error code list.
///
enum tga_error {
TGA_NO_ERROR = 0,
TGA_ERROR_OUT_OF_MEMORY,
TGA_ERROR_FILE_CANNOT_READ,
TGA_ERROR_FILE_CANNOT_WRITE,
TGA_ERROR_NO_DATA,
TGA_ERROR_UNSUPPORTED_COLOR_MAP_TYPE,
TGA_ERROR_UNSUPPORTED_IMAGE_TYPE,
TGA_ERROR_UNSUPPORTED_PIXEL_FORMAT,
TGA_ERROR_INVALID_IMAGE_DIMENSIONS,
TGA_ERROR_COLOR_MAP_INDEX_FAILED
};
///
/// \brief Structure for saving image information.
///
typedef struct tga_info tga_info;
///
/// \brief Creates a empty image.
///
/// The coordinates of the image start from the upper left corner. Image pixel
/// data is stored in a 1-dimensional array in a row major order.
/// ```
/// enum tga_error error_code;
/// uint8_t *image_data;
/// tga_info *image_info;
/// error_code = tga_create(&image_data, &image_info, 128, 128,
/// TGA_PIXEL_RGB24);
/// if (error_code == TGA_NO_ERROR) {
/// // Use the created image...
/// }
/// ```
///
/// \param data_out Returns the image pixels data, all pixel values are set
/// to 0x0. Uses tga_free_data() to release.
/// \param info_out Returns the information of the image. Uses tga_free_info()
/// to release.
/// \param width The width of the image, the value cannot be less than 1 or
/// greater than TGA_MAX_IMAGE_DIMENSISNS.
/// \param height The height of the image. the value cannot be less than 1 or
/// greater than TGA_MAX_IMAGE_DIMENSISNS.
/// \param format Image pixel format.
/// \return The result of the creation.
///
enum tga_error tga_create(uint8_t **data_out, tga_info **info_out, int width,
int height, enum tga_pixel_format format);
///
/// \brief Loads image data and information from TGA format file.
///
/// The coordinates of the image start from the upper left corner. Image pixel
/// data is stored in a 1-dimensional array in a row major order.
/// ```
/// enum tga_error error_code;
/// uint8_t *image_data;
/// tga_info *image_info;
/// error_code = tga_load(&image_data, &image_info, file_name);
/// if (error_code == TGA_NO_ERROR) {
/// // Use the loaded image...
/// }
/// ```
///
/// \param data_out Returns the image pixels data. Uses tga_free_data() to
/// release.
/// \param info_out Returns the information of the image. Uses tga_free_info()
/// to release.
/// \param file_name The TGA format file name to be loaded.
/// \return The result of loading the image.
///
enum tga_error tga_load(uint8_t **data_out, tga_info **info_out,
const char *file_name);
///
/// \brief Saves a image data as a TGA format file.
///
/// Same function as tga_save_from_info(). Generally used when the tga_info
/// structure has been released, or directly save the externally generated image
/// data as a TGA format file.
///
/// Note that if a file with the same name already exists, the save will fail.
/// If data or info is a null pointer, the function does nothing.
///
/// \param data The data of the image.
/// \param width The width of the image, the value cannot be less than 1 or
/// greater than TGA_MAX_IMAGE_DIMENSISNS.
/// \param height The height of the image. the value cannot be less than 1 or
/// greater than TGA_MAX_IMAGE_DIMENSISNS.
/// \param format Image pixel format.
/// \param file_name The name of the image file to be created.
/// \return The result of saving the image.
///
enum tga_error tga_save(const uint8_t *data, int width, int height,
enum tga_pixel_format format, const char *file_name);
///
/// \brief Saves a image data as a TGA format file.
///
/// Is the simplified parameter form of the tga_save() function.
///
/// Note that if a file with the same name already exists, the save will fail.
/// If data or info is a null pointer, the function does nothing.
///
/// \param data The data of the image.
/// \param info The tga_info structure of the image.
/// \param file_name The name of the image file to be created.
/// \return The result of saving the image.
///
enum tga_error tga_save_from_info(const uint8_t *data, const tga_info *info,
const char *file_name);
///
/// \brief Gets the image width.
///
/// \param info The tga_info structure of the image.
/// \return The width of the image.
///
int tga_get_image_width(const tga_info *info);
///
/// \brief Gets the image height.
///
/// \param info The tga_info structure of the image.
/// \return The height of the image.
///
int tga_get_image_height(const tga_info *info);
///
/// \brief Gets the image pixel format.
///
/// \param info The tga_info structure of the image.
/// \return The pixel format of the image.
///
enum tga_pixel_format tga_get_pixel_format(const tga_info *info);
///
/// \brief Gets the number of bytes per pixel, this value is based on the pixel
/// format.
///
/// \param info The tga_info structure of the image.
/// \return The number of bytes per pixel.
///
uint8_t tga_get_bytes_per_pixel(const tga_info *info);
///
/// \brief Returns the pointer to the pixel at coordinates (x,y) in the data for
/// reading or writing.
///
/// Note that the pixel data are all little-endian, e.g. a TGA_PIXEL_ARGB32
/// format image, a single pixel is stored in the memory in the order of
/// BBBBBBBB GGGGGGGG RRRRRRRR AAAAAAAA.
///
/// The coordinates start at upper left corner. If the pixel coordinates are out
/// of bounds (larger than width/height or small than 0), they will be clamped.
///
/// \param data The data pointer of the inmage.
/// \param info The tga_info structure of the image.
/// \param x The x coordinate of the pixel.
/// \param y The y coordinate of the pixel
/// \return Pointer to the lowest byte of the pixel, the pixel byte size can be
/// obtained by the tga_get_bytes_per_pixel() function.
///
uint8_t *tga_get_pixel(uint8_t *data, const tga_info *info, int x, int y);
///
/// \brief Releases the image data.
///
/// Releases the memory previously allocated by tga_load() or tga_create().
/// If the data is a null pointer, the function does nothing.
///
/// \param data The data to be released.
///
void tga_free_data(void *data);
///
/// \brief Releases the tga_info structure.
///
/// Releases the memory previously allocated by tga_load() or tga_create().
/// If the info is a null pointer, the function does nothing.
///
/// \param info The tga_info structure to be released.
///
void tga_free_info(tga_info *info);
///
/// \brief Flips the image horizontally.
///
/// Rearranges the order of pixels in data. If data or info is a null pointer,
/// the function does nothing.
///
/// \param data The image data to be flipped.
/// \param info The structure which contains the image information.
///
void tga_image_flip_h(uint8_t *data, const tga_info *info);
///
/// \brief Flip the image vertically.
///
/// Rearranges the order of pixels in data. If data or info is a null pointer,
/// the function does nothing.
///
/// \param data The image data to be flipped.
/// \param info The structure which contains the image information.
///
void tga_image_flip_v(uint8_t *data, const tga_info *info);
#ifdef __cplusplus
}
#endif //__cplusplus
#endif // TGAFUNC_H_