-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoaru_jpeg.c
74 lines (59 loc) · 1.61 KB
/
toaru_jpeg.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
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2019 K. Lange
*
* Sprti
*/
#include <stdio.h>
#include "sprite.h"
#define _RED(color) ((color & 0x00FF0000) / 0x10000)
#define _GRE(color) ((color & 0x0000FF00) / 0x100)
#define _BLU(color) ((color & 0x000000FF) / 0x1)
#define _ALP(color) ((color & 0xFF000000) / 0x1000000)
extern int load_sprite_jpg(sprite_t * sprite, char * filename);
sprite_t img = {0};
int main(int argc, char * argv[]) {
if (argc < 2) {
fprintf(stdout, "usage: %s IMAGE\n", argv[0]);
return 1;
}
load_sprite_jpg(&img, argv[1]);
FILE * f = fopen(argv[1], "r");
FILE * out = fopen("out.tga","w");
struct {
uint8_t id_length;
uint8_t color_map_type;
uint8_t image_type;
uint16_t color_map_first_entry;
uint16_t color_map_length;
uint8_t color_map_entry_size;
uint16_t x_origin;
uint16_t y_origin;
uint16_t width;
uint16_t height;
uint8_t depth;
uint8_t descriptor;
} __attribute__((packed)) header = {
0, /* No image ID field */
0, /* No color map */
2, /* Uncompressed truecolor */
0, 0, 0, /* No color map */
0, 0, /* Don't care about origin */
img.width, img.height, 24,
0,
};
fwrite(&header, 1, sizeof(header), out);
for (int y = img.height-1; y>=0; y--) {
for (int x = 0; x < img.width; ++x) {
uint8_t buf[3] = {
_BLU(img.bitmap[y * img.width + x]),
_GRE(img.bitmap[y * img.width + x]),
_RED(img.bitmap[y * img.width + x]),
};
fwrite(buf, 1, 3, out);
}
}
fclose(out);
return 0;
}