-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
234 lines (184 loc) · 5.26 KB
/
Copy pathmain.c
File metadata and controls
234 lines (184 loc) · 5.26 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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include "stb_image.h"
#include "dxtencode.h"
#define DDPF_FOURCC 0x4
typedef struct {
int dwSize;
int dwFlags;
char dwFourCC[4];
int dwRGBBitCount;
int dwRBitMask;
int dwGBitMask;
int dwBBitMask;
int dwABitMask;
} DDS_PIXELFORMAT;
#define DDSD_CAPS 0x1
#define DDSD_HEIGHT 0x2
#define DDSD_WIDTH 0x4
#define DDSD_PIXELFORMAT 0x1000
#define DDSD_MIPMAPCOUNT 0x20000
#define DDSCAPS_COMPLEX 0x8
#define DDSCAPS_MIPMAP 0x400000
#define DDSCAPS_TEXTURE 0x1000
typedef struct {
int dwSize;
int dwFlags;
int dwHeight;
int dwWidth;
int dwPitchOrLinearSize;
int dwDepth;
int dwMipMapCount;
int dwReserved1[11];
DDS_PIXELFORMAT ddspf;
int dwCaps;
int dwCaps2;
int dwCaps3;
int dwCaps4;
int dwReserved2;
} DDS_HEADER;
#define DDS_MAGIC 0x20534444
typedef struct {
int dwMagic;
DDS_HEADER header;
} DDS_FILE;
int main(int argc, char **argv) {
if(argc == 1) {
printf("dxt_cpt - BC1 encoder for channel packed textures\n");
printf("dxt_cpt [options] input.png [output.dds]\n");
printf("\nOptions:\n");
printf("-mip - Generate mipmaps\n");
return 0;
}
//Parse arguments
bool mipMapping = false;
int fileArgIndex = 0;
for(int i = 1; i < argc; i++) {
fileArgIndex = i;
if(argv[i][0] != '-') break;
if(strcmp(argv[i], "-mip") == 0) mipMapping = true;
}
//Load image
char* filename = argv[fileArgIndex];
int w, h, n;
uint8_t* image = stbi_load(filename, &w, &h, &n, 3);
if(!image) {
printf("Can't load image %s!\n", filename);
return 1;
}
//Allocate memory and fill dds header
int blocksW = w / 4 + ((w & 3) > 0 ? 1 : 0);
int blocksH = h / 4 + ((h & 3) > 0 ? 1 : 0);
int mipLevels = 1;
size_t dataSize = sizeof(DDS_FILE) + blocksW * blocksH * 8;
if(mipMapping) {
for(
int mW = w / 2, mH = h / 2;
mW > 0 || mH > 0;
mW /= 2, mH /= 2
) {
int mipBW = mW / 4 + ((mW & 3) > 0 ? 1 : 0);
int mipBH = mH / 4 + ((mH & 3) > 0 ? 1 : 0);
if(mipBW <= 0) mipBW = 1;
if(mipBH <= 0) mipBH = 1;
dataSize += mipBW * mipBH * 8;
mipLevels++;
}
}
uint8_t* outputData = malloc(dataSize);
assert(outputData);
DDS_FILE* ddsFile = (DDS_FILE*) outputData;
*ddsFile = (DDS_FILE) {
.dwMagic = DDS_MAGIC,
.header = {
.dwSize = sizeof(DDS_HEADER),
.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT,
.dwHeight = h,
.dwWidth = w,
.dwDepth = 1,
.dwPitchOrLinearSize = blocksH * blocksW / 2,
.dwMipMapCount = mipLevels,
.ddspf = {
.dwSize = sizeof(DDS_PIXELFORMAT),
.dwFlags = DDPF_FOURCC,
.dwFourCC = {'D', 'X', 'T', '1'}
},
.dwCaps = DDSCAPS_TEXTURE
}
};
//Encode texture
encodeDXT1(outputData + sizeof(DDS_FILE), image, w, h);
if(mipMapping) {
ddsFile->header.dwFlags |= DDSD_MIPMAPCOUNT;
ddsFile->header.dwCaps |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
uint8_t* outputMip = outputData + sizeof(DDS_FILE) + blocksW * blocksH * 8;
for(int i = 1; i < mipLevels; i++) {
int prevW = w >> (i - 1), nextW = w >> i;
int prevH = h >> (i - 1), nextH = h >> i;
if(prevW <= 0) prevW = 1;
if(prevH <= 0) prevH = 1;
if(nextW <= 0) nextW = 1;
if(nextH <= 0) nextH = 1;
for(int y = 0; y < nextH; y++) {
int y1 = y * prevH / nextH;
int y2 = (y + 1) * prevH / nextH;
for(int x = 0; x < nextW; x++) {
int x1 = x * prevW / nextW;
int x2 = (x + 1) * prevW / nextW;
int r = 0, g = 0, b = 0;
for(int yy = y1; yy < y2; yy++) {
uint8_t* imgPtr = &image[(x1 + yy * prevW) * 3];
for(int xx = x1; xx < x2; xx++, imgPtr += 3) {
r += imgPtr[0];
g += imgPtr[1];
b += imgPtr[2];
}
}
int pxCnt = (x2 - x1) * (y2 - y1);
uint8_t* imgPtr = &image[(x + y * nextW) * 3];
imgPtr[0] = (r + (pxCnt >> 1)) / pxCnt;
imgPtr[1] = (g + (pxCnt >> 1)) / pxCnt;
imgPtr[2] = (b + (pxCnt >> 1)) / pxCnt;
}
}
encodeDXT1(outputMip, image, nextW, nextH);
int mipBW = nextW / 4 + ((nextW & 3) > 0 ? 1 : 0);
int mipBH = nextH / 4 + ((nextH & 3) > 0 ? 1 : 0);
outputMip += mipBW * mipBH * 8;
}
}
//Save file
char* outputFname;
if(argc > fileArgIndex + 1) {
outputFname = argv[fileArgIndex + 1];
} else {
char* extension = ".dds";
size_t nameLen = strlen(filename);
size_t extLen = strlen(extension);
char* dotPtr = strrchr(filename, '.');
if(!dotPtr) dotPtr = filename + nameLen;
outputFname = malloc(dotPtr - filename + extLen + 1);
assert(outputFname);
memcpy(outputFname, filename, dotPtr - filename);
memcpy(&outputFname[dotPtr - filename], extension, extLen + 1);
}
FILE* file = fopen(outputFname, "wb");
if(!file) {
printf("Can't open file %s\n", outputFname);
} else {
size_t writeSize = fwrite(outputData, 1, dataSize, file);
if(writeSize != dataSize) {
printf("Can't write file %s\n", outputData);
}
fclose(file);
}
//Free memory & exit
free(image);
free(outputData);
if(!(argc > fileArgIndex + 1)) free(outputFname);
return 0;
}