-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstbimg.c
More file actions
247 lines (214 loc) · 7.08 KB
/
Copy pathstbimg.c
File metadata and controls
247 lines (214 loc) · 7.08 KB
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
/*
* Copyright 1995, 2021, 2022, 2025 Jörg Bakker
*
* 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.
*/
#include <stdlib.h>
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "3rd-party/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "3rd-party/stb_image_write.h"
#define STB_DS_IMPLEMENTATION
#include "3rd-party/stb_ds.h"
#include "sis.h"
#include "stbimg.h"
static unsigned char *inpic_p, *outpic_buf_p, *texpic_p;
static ind_t outpic_width = 0, outpic_height = 0;
static col_t **Tread_buf;
/* static ind_t cur_Dread=-1, */
static ind_t cur_Tread = -1;
static const int SISChannelCount = 3;
void
Stb_OpenDFile(char *DFileName, ind_t *width, ind_t *height)
{
stbi_set_unpremultiply_on_load(1);
stbi_convert_iphone_png_to_rgb(1);
int channel_count = 0, desired_channel_count = 1;
if (! (inpic_p = (unsigned char *)stbi_load(DFileName, (int *)width, (int *)height,
&channel_count, desired_channel_count))) {
fprintf(stderr, "Failed to load %s: %s\n", DFileName, stbi_failure_reason());
exit(1);
}
black_value = 0;
white_value = SIS_MAX_CMAP;
}
void
Stb_CreateSISBuffer(ind_t width, ind_t height, int SIStype)
{
outpic_width = width;
outpic_height = height;
// Allocate buffer for the output image data that can be directly written with stb_image_write()
if (! (outpic_buf_p = (unsigned char *)calloc(height * width, SISChannelCount))) {
fprintf(stderr, "Failed to allocate output image buffer.\n");
exit(1);
}
}
void
Stb_OpenTFile(char *TFileName, ind_t *width, ind_t *height)
{
int channel_count = 0, desired_channel_count = 3;
if (!(texpic_p = (unsigned char *)stbi_load(TFileName, (int *)width, (int *)height,
&channel_count, desired_channel_count))) {
fprintf(stderr, "Failed to load %s: %s\n", TFileName, stbi_failure_reason());
exit(1);
}
if (channel_count != desired_channel_count) {
fprintf(stderr,
"Input texture map image must have three color channels\n");
/// TODO: check if we can continue if channel count is not equal
/// to the desired channel count
exit(1);
}
/// Copy the interleaved color values and populate the texture image
/// planes SISred, SISgreen, SISblue and the image color index buffer
/// Tread_buf by creating a color map from the interleaved stb_image data.
struct {
uint32_t key;
col_t value;
} *colmap = NULL;
const col_t default_value = -1;
hmdefault(colmap, default_value);
col_t col_idx = 0;
if (!(Tread_buf = (col_t **) calloc(*height, sizeof(col_t *)))) {
fprintf(stderr, "Failed to allocate texture readbuf.\n");
exit(1);
}
for (ind_t r = 0; r < *height; ++r) {
if (!(Tread_buf[r] = (col_t *) calloc(*width, sizeof(col_t)))) {
fprintf(stderr, "Failed to allocate texture readbuf.\n");
exit(1);
}
for (ind_t c = 0; c < *width; ++c) {
ind_t row_pos = r * (*width) * channel_count;
ind_t col_base_pos = c * channel_count;
uint32_t col_rgb =
(uint32_t) (texpic_p[row_pos + col_base_pos + 0]) |
(uint32_t) (texpic_p[row_pos + col_base_pos + 1]) << 8 |
(uint32_t) (texpic_p[row_pos + col_base_pos + 2]) << 16;
col_t current_col_idx = hmget(colmap, col_rgb);
if (current_col_idx == default_value) {
hmput(colmap, col_rgb, col_idx);
current_col_idx = col_idx;
col_idx++;
}
Tread_buf[r][c] = current_col_idx;
SISred[current_col_idx] = texpic_p[row_pos + col_base_pos + 0];
SISgreen[current_col_idx] = texpic_p[row_pos + col_base_pos + 1];
SISblue[current_col_idx] = texpic_p[row_pos + col_base_pos + 2];
}
}
hmfree(colmap);
/// Number of unique colors is col_idx + black
// printf("texture unique color count: %d\n", col_idx + 1);
Tcolcount = col_idx + 1;
}
unsigned char *
Stb_GetDFileBuffer(void)
{
return inpic_p;
}
unsigned char *
Stb_GetTFileBuffer(void)
{
return texpic_p;
}
unsigned char *
Stb_GetSISFileBuffer(void)
{
return outpic_buf_p;
}
void
Stb_ReadDBuffer(ind_t r)
{
z_t zval = 0;
for (ind_t c = 0; c < Dwidth; c++) {
DBuffer[c] = inpic_p[r * Dwidth + c];
zval = DBuffer[c] << 8;
DaddEntry(DBuffer[c], zval);
}
}
// void
// Stb_WriteSISBuffer(ind_t r)
// {
// // Write each line of the generated SIS subsequently to the output file / buffer
// for (ind_t c = 0; c < outpic_width; c++) {
// ind_t row_pos = r * outpic_width * SISChannelCount;
// ind_t col_base_pos = c * SISChannelCount;
// // Write all color components of the ouput image. The color is taken from the color palette
// // and the index into the color palette is taken from SISBuffer.
// outpic_buf_p[row_pos + col_base_pos + 0] = SISred[SISBuffer[c]];
// outpic_buf_p[row_pos + col_base_pos + 1] = SISgreen[SISBuffer[c]];
// outpic_buf_p[row_pos + col_base_pos + 2] = SISblue[SISBuffer[c]];
// }
// }
void
Stb_WriteSISColorBuffer(ind_t r)
{
// Write each line of the generated SIS subsequently to the output file / buffer
for (ind_t c = 0; c < outpic_width; c++) {
ind_t row_pos = r * outpic_width * SISChannelCount;
ind_t col_base_pos = c * SISChannelCount;
// Write all color components of the ouput image. The color is taken from the color palette
// and the index into the color palette is taken from SISBuffer.
outpic_buf_p[row_pos + col_base_pos + 0] = SIScolorRGB[c].r;
outpic_buf_p[row_pos + col_base_pos + 1] = SIScolorRGB[c].g;
outpic_buf_p[row_pos + col_base_pos + 2] = SIScolorRGB[c].b;
}
}
// Return the index into the color palette of the pixel with coordinates (r, c)
// in the texture image
col_t
Stb_ReadTPixel(ind_t r, ind_t c)
{
// TODO is cur_Tread actually used in stbimg.c?
if (cur_Tread != r) {
cur_Tread = r;
}
return Tread_buf[r][c];
}
void
Stb_CloseDFile(void)
{
stbi_image_free(inpic_p);
}
void
Stb_CloseTFile(ind_t height)
{
for (ind_t r = 0; r < height; ++r) {
free(Tread_buf[r]);
}
free(Tread_buf);
stbi_image_free(texpic_p);
}
void
Stb_CloseSISFile(void)
{
free(outpic_buf_p);
}
void
Stb_WriteSISFile(void)
{
// TODO write to other image formats
stbi_write_png(SISFileName,
outpic_width,
outpic_height,
SISChannelCount,
outpic_buf_p, outpic_width * SISChannelCount);
}