-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
156 lines (136 loc) · 3 KB
/
main.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
#include <stdio.h>
#include <string.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
enum {
AO_TEX = 0,
//DIFF_TEX,
DISP_TEX,
NOR_TEX,
ROUGH_TEX,
SPEC_TEX,
NUM_TEX_TYPES
};
char *substrings[NUM_TEX_TYPES] = {
"_AO_",
//"_diff_",
"_disp_",
"_nor_",
"_rough_",
"_spec_"
};
int main(int argc, char *argv[])
{
unsigned char *datas[NUM_TEX_TYPES];
unsigned char *newImage;
int i;
int w = 0, h = 0;
unsigned char *out;
for (i = 0; i < NUM_TEX_TYPES; i++)
datas[i] = NULL;
for (i = 1; i < argc; i++)
{
int j;
for (j = 0; j < NUM_TEX_TYPES; j++)
{
if (strstr(argv[i], substrings[j]))
{
int nw, nh, c;
//printf("loading %s\n", argv[i]);
//fflush(stdout);
datas[j] = stbi_load(argv[i], &nw, &nh, &c, 4);
if (!datas[j])
{
printf("invalid file, ignoring\n");
break;
}
if (w && w != nw)
{
printf("size mismatch! %s is %d pixels wide, expected %d", argv[i], nw, w);
return 0;
}
w = nw;
if (h && h != nh)
{
printf("size mismatch! %s is %d pixels high, expected %d", argv[i], nh, h);
return 0;
}
h = nh;
break;
}
}
}
newImage = malloc(w * h * 4);
if (!datas[AO_TEX])
printf("AO_TEX not found");
if (!datas[DISP_TEX])
printf("DISP_TEX not found");
if (!datas[NOR_TEX])
printf("NOR_TEX not found");
if (!datas[ROUGH_TEX])
printf("ROUGH_TEX not found");
if (!datas[SPEC_TEX])
printf("SPEC_TEX not found");
fflush(stdout);
//if (datas[DIFF_TEX])
//{
// printf("writing output.png\n");
// fflush(stdout);
// stbi_write_png("output.png", w, h, 4, datas[DIFF_TEX], w * 4);
//}
if (datas[NOR_TEX])
{
if (datas[DISP_TEX])
{
unsigned char *inNormal = datas[NOR_TEX];
unsigned char *inHeight = datas[DISP_TEX];
out = newImage;
for (i = 0; i < w * h; i++)
{
out[0] = inNormal[0];
out[1] = inNormal[1];
out[2] = inNormal[2];
out[3] = inHeight[0];
out += 4;
inNormal += 4;
inHeight += 4;
}
//printf("writing output_n.png\n");
//fflush(stdout);
stbi_write_png("output_n.png", w, h, 4, newImage, w * 4);
}
else
{
printf("DISP_TEX not found, not doing anything\n");
fflush(stdout);
//stbi_write_png("output_n.png", w, h, 4, datas[NOR_TEX], w * 4);
}
}
if (datas[SPEC_TEX] || datas[ROUGH_TEX] || datas[AO_TEX])
{
unsigned char *inSpec = datas[SPEC_TEX];
unsigned char *inRough = datas[ROUGH_TEX];
unsigned char *inAO = datas[AO_TEX];
out = newImage;
for (i = 0; i < w * h; i++)
{
out[0] = datas[SPEC_TEX] ? inSpec[0] : 0;
out[1] = datas[ROUGH_TEX] ? inRough[1] : 255;
out[2] = datas[AO_TEX] ? inAO[2] : 255;
out[3] = 255;
out += 4;
inSpec += 4;
inRough += 4;
inAO += 4;
}
//printf("writing output_s.png\n");
//fflush(stdout);
stbi_write_png("output_s.png", w, h, 4, newImage, w * 4);
}
// todo: enable this when drag&drop was used, but how to detect?
//printf("press enter to continue\n");
//fflush(stdout);
//getchar();
}