summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/tools/makedisttex.cpp
blob: a4542462e69b9a21ae24c2ea00ace227ee395d39 (plain) (blame)
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
/*
 * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
 * License: http://www.opensource.org/licenses/BSD-2-Clause
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <edtaa3/edtaa3func.h>

#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.c>

#define BX_NAMESPACE 1
#include <bx/bx.h>
#include <bx/commandline.h>
#include <bx/uint32_t.h>

long int fsize(FILE* _file)
{
	long int pos = ftell(_file);
	fseek(_file, 0L, SEEK_END);
	long int size = ftell(_file);
	fseek(_file, pos, SEEK_SET);
	return size;
}

void edtaa3(double* _img, uint16_t _width, uint16_t _height, double* _out)
{
	uint32_t size = _width*_height;

	short* xdist = (short*)malloc(size*sizeof(short) );
	short* ydist = (short*)malloc(size*sizeof(short) );
	double* gx = (double*)malloc(size*sizeof(double) );
	double* gy = (double*)malloc(size*sizeof(double) );

	computegradient(_img, _width, _height, gx, gy);
	edtaa3(_img, gx, gy, _width, _height, xdist, ydist, _out);

	for (uint32_t ii = 0; ii < size; ++ii)
	{
		if (_out[ii] < 0.0)
		{
			_out[ii] = 0.0;
		}
	}

	free(xdist);
	free(ydist);
	free(gx);
	free(gy);
}

void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, bool _grayscale, const void* _data)
{
	FILE* file = fopen(_filePath, "wb");
	if ( NULL != file )
	{
		uint8_t type = _grayscale ? 3 : 2;
		uint8_t bpp = _grayscale ? 8 : 32;
		uint8_t xorig = 0;
		uint8_t yorig = 0;

		putc(0, file);
		putc(0, file);
		putc(type, file);
		putc(0, file);
		putc(0, file);
		putc(0, file);
		putc(0, file);
		putc(0, file);
		putc(0, file);
		putc(xorig, file);
		putc(0, file);
		putc(yorig, file);
		putc(_width&0xff, file);
		putc( (_width>>8)&0xff, file);
		putc(_height&0xff, file);
		putc( (_height>>8)&0xff, file);
		putc(bpp, file);
		putc(32, file);

		uint32_t width = _width * bpp / 8;
		uint8_t* data = (uint8_t*)_data;
		for (uint32_t yy = 0; yy < _height; ++yy)
		{
			fwrite(data, width, 1, file);
			data += _pitch;
		}

		fclose(file);
	}
}

inline double min(double _a, double _b)
{
	return _a > _b ? _b : _a;
}

inline double max(double _a, double _b)
{
	return _a > _b ? _a : _b;
}

inline double clamp(double _val, double _min, double _max)
{
	return max(min(_val, _max), _min);
}

inline double saturate(double _val)
{
	return clamp(_val, 0.0, 1.0);
}

int main(int _argc, const char* _argv[])
{
	bx::CommandLine cmdLine(_argc, _argv);

	const char* inFilePath = cmdLine.findOption('i');
	if (NULL == inFilePath)
	{
		fprintf(stderr, "Input file name must be specified.\n");
		return EXIT_FAILURE;
	}

	const char* outFilePath = cmdLine.findOption('o');
	if (NULL == outFilePath)
	{
		fprintf(stderr, "Output file name must be specified.\n");
		return EXIT_FAILURE;
	}

	double edge = 16.0;
	const char* edgeOpt = cmdLine.findOption('e');
	if (NULL != edgeOpt)
	{
		edge = atof(edgeOpt);
	}

	int width;
	int height;
	int comp;

	stbi_uc* img = stbi_load(inFilePath, &width, &height, &comp, 1);

	if (NULL == img)
	{
		fprintf(stderr, "Failed to load %s.\n", inFilePath);
		return EXIT_FAILURE;
	}

	uint32_t size = width*height;

	double* imgIn = (double*)malloc(size*sizeof(double) );
	double* outside = (double*)malloc(size*sizeof(double) );
	double* inside = (double*)malloc(size*sizeof(double) );

	for (uint32_t ii = 0; ii < size; ++ii)
	{
		imgIn[ii] = double(img[ii])/255.0;
	}

	edtaa3(imgIn, width, height, outside);

	for (uint32_t ii = 0; ii < size; ++ii)
	{
		imgIn[ii] = 1.0 - imgIn[ii];
	}

	edtaa3(imgIn, width, height, inside);

	free(imgIn);

	uint8_t* grayscale = (uint8_t*)malloc(size);

	double edgeOffset = edge*0.5;
	double invEdge = 1.0/edge;

	for (uint32_t ii = 0; ii < size; ++ii)
	{
		double dist = saturate( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge);
		grayscale[ii] = 255-uint8_t(dist * 255.0);
	}

	free(inside);
	free(outside);

	saveTga(outFilePath, width, height, width, true, grayscale);

	free(grayscale);

	return EXIT_SUCCESS;
}