forked from tom2238/apt-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
335 lines (314 loc) · 8.55 KB
/
image.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "main.h"
#include "image.h"
#include "aptcode.h"
int TestTGAImage(FILE *fp){
TgaImageHead TgaHead;
// 24 bpp, true color without alfa
TgaHead.IDLength = 0x00; //no ID array
TgaHead.ColorMapType = 0x00; //no palete
TgaHead.ImageType = 0x02; //true color
TgaHead.CMapStart = 0x0000; //zero
TgaHead.CMapLength = 0x0000; //zero
TgaHead.CMapDepth = 0x00; //zero
TgaHead.XOffset = 0x0000; //zero
TgaHead.YOffset = 0x0000; //zero
TgaHead.Width = 0x0100; //256 pixels
TgaHead.Height = 0x0100; //256 pixels
TgaHead.PixelDepth = 0x18; // 24 bits per pixel
TgaHead.ImageDescriptor = 0x20; //up to down
//write header
fwrite_int(TgaHead.IDLength,1,fp);
fwrite_int(TgaHead.ColorMapType,1,fp);
fwrite_int(TgaHead.ImageType,1,fp);
fwrite_int(TgaHead.CMapStart,2,fp);
fwrite_int(TgaHead.CMapLength,2,fp);
fwrite_int(TgaHead.CMapDepth,1,fp);
fwrite_int(TgaHead.XOffset,2,fp);
fwrite_int(TgaHead.YOffset,2,fp);
fwrite_int(TgaHead.Width,2,fp);
fwrite_int(TgaHead.Height,2,fp);
fwrite_int(TgaHead.PixelDepth,1,fp);
fwrite_int(TgaHead.ImageDescriptor,1,fp);
// Test image
int i,j;
for(i=0;i<256;i++) {
for(j=0;j<256;j++) {
WriteTGAPixel(i, i, j, fp);
}
}
return 0;
}
int WriteTGAPixel(uint8_t R, uint8_t G, uint8_t B, FILE *fp){
uint32_t RR = R << 16; //8 bit
uint32_t GG = G << 8; //8 bit
uint32_t BB = B; //8 bit
uint32_t data = BB + GG + RR;
return fwrite_int(data,3,fp);
}
unsigned int ReadTGAPixel(FILE *fp){
return fread_int(3,fp);
}
uint8_t GetRedSubPixel(unsigned int pixel) {
unsigned int pix = pixel & 0x00FF0000;
return pix >> 16;
}
uint8_t GetGreenSubPixel(unsigned int pixel) {
unsigned int pix = pixel & 0x0000FF00;
return pix >> 8;
}
uint8_t GetBlueSubPixel(unsigned int pixel) {
unsigned int pix = pixel & 0x000000FF;
return pix >> 0;
}
int fwrite_int(int val, char len, FILE *p){
unsigned int byte;
while (len-- > 0) {
byte = val & 0xFF;
fwrite(&byte, 1, 1, p);
val >>= 8;
}
return 0;
}
unsigned int fread_int(char len, FILE *p){
unsigned int byte=0;
unsigned int val=0;
unsigned int pc=0;
while (len-- > 0) {
fread(&byte, 1, 1, p);
val = val + (byte << 8*pc);
pc++;
}
return val;
}
TgaImageHead OpenTgaImage(char *filename) {
FILE *filein=NULL;
char msg[255];
TgaImageHead TgaImage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
if((filein = fopen(filename, "rb")) == NULL){
strcpy(msg, "Error opening ");
strcat(msg, filename);
perror(msg);
return TgaImage;
}
// read header
TgaImage.IDLength = fread_int(1,filein);
TgaImage.ColorMapType = fread_int(1,filein);
TgaImage.ImageType = fread_int(1,filein);
TgaImage.CMapStart = fread_int(2,filein);
TgaImage.CMapLength = fread_int(2,filein);
TgaImage.CMapDepth = fread_int(1,filein);
TgaImage.XOffset = fread_int(2,filein);
TgaImage.YOffset = fread_int(2,filein);
TgaImage.Width = fread_int(2,filein);
TgaImage.Height = fread_int(2,filein);
TgaImage.PixelDepth = fread_int(1,filein);
TgaImage.ImageDescriptor = fread_int(1,filein);
if (TgaImage.Width != APT_VIDEO_A) {
printf("%s : Image width is %d px\n",filename,TgaImage.Width);
printf("Only %d px width images are supported\n",APT_VIDEO_A);
fclose(filein);
return TgaImage;
}
TgaImage.File = filein;
//printf("X: %d, Y:%d Z: %d\n",TgaImage.Width,TgaImage.Height,TgaImage.PixelDepth);
return TgaImage; //now ready to read image data
}
TgaImageHead WriteTgaImage(char *filename, TgaImageHead WriteImage) {
FILE *fileout=NULL;
char msg[255];
WriteImage.File = NULL;
if((fileout = fopen(filename, "wb")) == NULL){
strcpy(msg, "Error opening ");
strcat(msg, filename);
perror(msg);
return WriteImage;
}
WriteImage.File = fileout;
fwrite_int(WriteImage.IDLength,1,fileout);
fwrite_int(WriteImage.ColorMapType,1,fileout);
fwrite_int(WriteImage.ImageType,1,fileout);
fwrite_int(WriteImage.CMapStart,2,fileout);
fwrite_int(WriteImage.CMapLength,2,fileout);
fwrite_int(WriteImage.CMapDepth,1,fileout);
fwrite_int(WriteImage.XOffset,2,fileout);
fwrite_int(WriteImage.YOffset,2,fileout);
fwrite_int(WriteImage.Width,2,fileout);
fwrite_int(WriteImage.Height,2,fileout);
fwrite_int(WriteImage.PixelDepth,1,fileout);
fwrite_int(WriteImage.ImageDescriptor,1,fileout);
return WriteImage; //now ready to write image data
}
HsvColor RgbToHsv(RgbColor rgb) {
HsvColor hsv;
uint8_t rgbMin, rgbMax;
// Get min/max
rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
// Value
hsv.v = rgbMax;
if (hsv.v == 0) {
hsv.h = 0;
hsv.s = 0;
return hsv;
}
// Saturation
hsv.s = 255 * (long)(rgbMax - rgbMin) / hsv.v;
if (hsv.s == 0) {
hsv.h = 0;
return hsv;
}
// Hue
if (rgbMax == rgb.r) {
hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
}
else if (rgbMax == rgb.g) {
hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
}
else {
hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
}
return hsv;
}
RgbColor HsvToRgb(HsvColor hsv) {
RgbColor rgb;
uint8_t region, remainder, p, q, t;
// Grayscale
if (hsv.s == 0) {
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
// 60 degrees region
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
switch (region) {
case 0:
rgb.r = hsv.v; rgb.g = t; rgb.b = p;
break;
case 1:
rgb.r = q; rgb.g = hsv.v; rgb.b = p;
break;
case 2:
rgb.r = p; rgb.g = hsv.v; rgb.b = t;
break;
case 3:
rgb.r = p; rgb.g = q; rgb.b = hsv.v;
break;
case 4:
rgb.r = t; rgb.g = p; rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v; rgb.g = p; rgb.b = q;
break;
}
return rgb;
}
AptColor HsvToApt(HsvColor hsv, uint8_t bits) {
AptColor apt;
apt.h = hsv.h;
switch (bits) {
case 0:
apt.sv = (hsv.s & 0x00)+(hsv.v >> 0);
break;
case 1:
apt.sv = (hsv.s & 0x80)+(hsv.v >> 1);
break;
case 2:
apt.sv = (hsv.s & 0xC0)+(hsv.v >> 2);
break;
case 4:
apt.sv = (hsv.s & 0xF0)+(hsv.v >> 4);
break;
case 5:
apt.sv = (hsv.s & 0xF8)+(hsv.v >> 5);
break;
case 6:
apt.sv = (hsv.s & 0xFC)+(hsv.v >> 6);
break;
case 7:
apt.sv = (hsv.s & 0xFE)+(hsv.v >> 7);
break;
case 8:
apt.sv = (hsv.s & 0xFF)+(hsv.v >> 8);
break;
case 3:
default:
apt.sv = (hsv.s & 0xE0)+(hsv.v >> 3);
break;
}
return apt;
}
HsvColor AptToHsv(AptColor apt, uint8_t bits) {
HsvColor hsv;
hsv.h = apt.h;
switch (bits) {
case 0:
hsv.s = (apt.sv & 0x00);
hsv.v = (apt.sv & 0xFF) << 0;
break;
case 1:
hsv.s = (apt.sv & 0x80);
hsv.v = (apt.sv & 0x7F) << 1;
break;
case 2:
hsv.s = (apt.sv & 0xC0);
hsv.v = (apt.sv & 0x3F) << 2;
break;
case 4:
hsv.s = (apt.sv & 0xF0);
hsv.v = (apt.sv & 0x0F) << 4;
break;
case 5:
hsv.s = (apt.sv & 0xF8);
hsv.v = (apt.sv & 0x07) << 5;
break;
case 6:
hsv.s = (apt.sv & 0xFC);
hsv.v = (apt.sv & 0x03) << 6;
break;
case 7:
hsv.s = (apt.sv & 0xFE);
hsv.v = (apt.sv & 0x01) << 7;
break;
case 8:
hsv.s = (apt.sv & 0xFF);
hsv.v = (apt.sv & 0x00) << 8;
break;
case 3:
default:
hsv.s = (apt.sv & 0xE0);
hsv.v = (apt.sv & 0x1F) << 3;
break;
}
return hsv;
}
AptColor RgbToApt(RgbColor rgb) {
AptColor apt;
uint8_t R = (rgb.r >> 4) & 0xF;
uint8_t G = (rgb.g >> 4) & 0xF;
uint8_t B = (rgb.b >> 4) & 0xF;
uint16_t val = (R << 8) + (G << 4) + B;
uint16_t pos = LUTFromRgb[val];
uint16_t y_val = pos % 64;
uint16_t x_val = (pos - y_val) / 64;
apt.h = (x_val & 0x3F) * 4;
apt.sv = (y_val & 0x3F) * 4;
return apt;
}
RgbColor AptToRgb(AptColor apt) {
RgbColor rgb;
uint8_t x_val = (apt.h+2)/4;
uint8_t y_val = (apt.sv+2)/4;
if(x_val > 255) {x_val = 255;}
if(y_val > 255) {y_val = 255;}
uint16_t val = LUTFromApt[x_val][y_val];
uint8_t R = (val >> 8) & 0xF;
uint8_t G = (val >> 4) & 0xF;
uint8_t B = (val >> 0) & 0xF;
rgb.r = R * 17; rgb.g = G * 17; rgb.b = B * 17;
return rgb;
}