-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimageconvert.cpp
252 lines (213 loc) · 6.27 KB
/
imageconvert.cpp
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
#include "image.h"
#include "os.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
static uint32_t * table_xrgb1555_xrgb8888=NULL;
static uint32_t * table_rgb565_xrgb8888=NULL;
static void create_tbl_xrgb1555_xrgb8888(void* location)
{
if (table_xrgb1555_xrgb8888)
{
memcpy(location, table_xrgb1555_xrgb8888, sizeof(uint32_t)*65536);
return;
}
uint32_t * table=(uint32_t*)location;
for (int r=0;r<32;r++)
for (int g=0;g<32;g++)
for (int b=0;b<32;b++)
{
int index=r*32*32+g*32+b;
int col=(r<<19)|(g<<11)|(b<<3);
col|=(col>>5)&0x070707;
table[index]=col;
}
memset(table+32768, 0, sizeof(uint32_t)*32768);
}
static void create_tbl_rgb565_xrgb8888(void* location)
{
if (table_rgb565_xrgb8888)
{
memcpy(location, table_rgb565_xrgb8888, sizeof(uint32_t)*65536);
return;
}
uint32_t * table=(uint32_t*)location;
for (int r=0;r<32;r++)
for (int g=0;g<64;g++)
for (int b=0;b<32;b++)
{
int index=r*64*32+g*32+b;
int col=(r<<19)|(g<<10)|(b<<3);
col|=(col>>5)&0x070007;
col|=(col>>6)&0x000300;
table[index]=col;
}
}
static void convert_2_4(const struct image * src, struct image * dst)
{
const uint32_t * conv=(const uint32_t*)image_get_convert_table(src->format, fmt_xrgb8888);
uint16_t * in=(uint16_t*)src->pixels;
uint32_t * out=(uint32_t*)dst->pixels;
for (unsigned int y=0;y<src->height;y++)
{
for (unsigned int x=0;x<src->width;x++)
{
out[x]=conv[in[x]];
}
in+=src->pitch/sizeof(uint16_t);
out+=dst->pitch/sizeof(uint32_t);
}
}
static void convert_2_3(const struct image * src, struct image * dst)
{
const uint32_t * conv=(const uint32_t*)image_get_convert_table(src->format, fmt_xrgb8888);
uint16_t * in=(uint16_t*)src->pixels;
uint8_t * out=(uint8_t*)dst->pixels;
for (unsigned int y=0;y<src->height;y++)
{
for (unsigned int x=0;x<src->width;x++)
{
out[x*3+0]=conv[in[x]]>>0;
out[x*3+1]=conv[in[x]]>>8;
out[x*3+2]=conv[in[x]]>>16;
}
in+=src->pitch/sizeof(uint16_t);
out+=dst->pitch/sizeof(uint8_t);
}
}
#define FMT(src, dst) ((src)<<8 | (dst))
void image_create_convert_table(videoformat srcfmt, videoformat dstfmt, void* dst)
{
switch (FMT(srcfmt, dstfmt))
{
case FMT(fmt_xrgb1555, fmt_xrgb8888): create_tbl_xrgb1555_xrgb8888(dst); break;
case FMT(fmt_rgb565, fmt_xrgb8888): create_tbl_rgb565_xrgb8888(dst); break;
}
}
const void * image_get_convert_table(videoformat srcfmt, videoformat dstfmt)
{
switch (FMT(srcfmt, dstfmt))
{
#define IMPL(src, dst, size) \
case FMT(fmt_##src, fmt_##dst): \
if (!table_##src##_##dst) \
{ \
void * tmp=malloc(size); \
create_tbl_##src##_##dst(tmp); \
table_##src##_##dst=(uint32_t*)tmp; \
} \
return table_##src##_##dst;
IMPL(xrgb1555, xrgb8888, sizeof(uint32_t)*65536);
IMPL(rgb565, xrgb8888, sizeof(uint32_t)*65536);
#undef IMPL
}
return NULL;
}
void image_convert(const struct image * src, struct image * dst)
{
switch (FMT(src->format, dst->format))
{
case FMT(fmt_xrgb1555, fmt_xrgb1555):
case FMT(fmt_rgb565, fmt_rgb565):
case FMT(fmt_rgb888, fmt_rgb888):
case FMT(fmt_xrgb8888, fmt_xrgb8888):
case FMT(fmt_argb8888, fmt_argb8888):
{
unsigned int pixelsize=videofmt_byte_per_pixel(src->format);
unsigned int linelen=pixelsize*src->width;
uint8_t * srcdata=(uint8_t*)src->pixels;
uint8_t * dstdata=(uint8_t*)dst->pixels;
unsigned int srcpitch=src->pitch;
unsigned int dstpitch=dst->pitch;
for (unsigned int y=src->height;y>0;y--)//loop from height to 1; we don't use y in the loop
{
memcpy(dstdata, srcdata, linelen);
srcdata+=srcpitch;
dstdata+=dstpitch;
}
break;
}
case FMT(fmt_xrgb1555, fmt_xrgb8888):
case FMT(fmt_rgb565, fmt_xrgb8888):
convert_2_4(src, dst);
break;
case FMT(fmt_xrgb1555, fmt_rgb888):
case FMT(fmt_rgb565, fmt_rgb888):
convert_2_3(src, dst);
break;
default: debug_abort();
}
}
static void convert_resize_2_2_self(const struct image * src, struct image * dst)
{
float xstep=(float)src->width/dst->width;
float ystep=(float)src->height/dst->height;
for (unsigned int y=0;y<dst->height;y++)
{
const uint16_t* srcdat=((uint16_t*)src->pixels)+((unsigned int)(ystep*y))*src->pitch/sizeof(uint16_t);
uint16_t* dstdat=((uint16_t*)dst->pixels)+(y*dst->pitch/sizeof(uint16_t));
float xpos=(float)0.5/dst->width;
for (unsigned int x=0;x<dst->width;x++)
{
*(dstdat++)=srcdat[(unsigned int)xpos];
xpos+=xstep;
}
}
}
static void convert_resize_2_4(const struct image * src, struct image * dst)
{
const uint32_t * conv=(const uint32_t*)image_get_convert_table(src->format, fmt_xrgb8888);
float xstep=(float)src->width/dst->width;
float ystep=(float)src->height/dst->height;
for (unsigned int y=0;y<dst->height;y++)
{
const uint16_t* srcdat=((uint16_t*)src->pixels)+((unsigned int)(ystep*y))*src->pitch/sizeof(uint16_t);
uint32_t* dstdat=((uint32_t*)dst->pixels)+(y*dst->pitch/sizeof(uint32_t));
float xpos=(float)0.5/dst->width;
for (unsigned int x=0;x<dst->width;x++)
{
*(dstdat++)=conv[srcdat[(unsigned int)xpos]];
xpos+=xstep;
}
}
}
static void convert_resize_4_4_self(const struct image * src, struct image * dst)
{
float xstep=(float)src->width/dst->width;
float ystep=(float)src->height/dst->height;
for (unsigned int y=0;y<dst->height;y++)
{
const uint32_t* srcdat=((uint32_t*)src->pixels)+((unsigned int)(ystep*y))*src->pitch/sizeof(uint32_t);
uint32_t* dstdat=((uint32_t*)dst->pixels)+(y*dst->pitch/sizeof(uint32_t));
float xpos=(float)0.5/dst->width;
for (unsigned int x=0;x<dst->width;x++)
{
*(dstdat++)=srcdat[(unsigned int)xpos];
xpos+=xstep;
}
}
}
void image_convert_resize(const struct image * src, struct image * dst)
{
switch (FMT(src->format, dst->format))
{
case FMT(fmt_xrgb1555, fmt_xrgb1555):
case FMT(fmt_rgb565, fmt_rgb565):
convert_resize_2_2_self(src, dst);
break;
case FMT(fmt_xrgb1555, fmt_xrgb8888):
case FMT(fmt_rgb565, fmt_xrgb8888):
convert_resize_2_4(src, dst);
break;
case FMT(fmt_xrgb8888, fmt_xrgb8888):
case FMT(fmt_argb8888, fmt_argb8888):
convert_resize_4_4_self(src, dst);
break;
default: debug_abort();
}
}
bool image_decode(const void * imagedata, size_t imglen, struct image * out, videoformat format)
{
if (imglen>=8 && !memcmp(imagedata, "\x89PNG\r\n\x1A\n", 8)) return png_decode(imagedata, imglen, out, format);
return false;
}