File: astc_blend_test.cpp

package info (click to toggle)
astc-encoder 5.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ansic: 44,622; cpp: 24,142; python: 3,403; sh: 78; makefile: 24
file content (298 lines) | stat: -rw-r--r-- 7,302 bytes parent folder | download | duplicates (4)
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
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2021-2024 Arm Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// ----------------------------------------------------------------------------

// This is a utility tool to test blend modes.

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

#include "astcenc_mathlib.h"

#define STB_IMAGE_IMPLEMENTATION
#include "ThirdParty/stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "ThirdParty/stb_image_write.h"

/**
 * @brief Linearize an sRGB value.
 *
 * @return The linearized value.
 */
static float srgb_to_linear(
	float a
) {
	if (a <= 0.04045f)
	{
		return a * (1.0f / 12.92f);
	}

	return powf((a + 0.055f) * (1.0f / 1.055f), 2.4f);
}

/**
 * @brief sRGB gamma-encode a linear value.
 *
 * @return The gamma encoded value.
 */
static float linear_to_srgb(
	float a
) {
	if (a <= 0.0031308f)
	{
		return a * 12.92f;
	}

	return 1.055f * powf(a, 1.0f / 2.4f) - 0.055f;
}

int main(int argc, char **argv)
{
	// Parse command line
	if (argc != 6)
	{
		printf("Usage: astc_blend_test <source> <dest> <format> <blend_mode> <filter>\n");
		exit(1);
	}

	const char* src_file = argv[1];
	const char* dst_file = argv[2];

	bool use_linear = false;
	if (!strcmp(argv[3], "linear"))
	{
		use_linear = true;
	}
	else if (!strcmp(argv[3], "srgb"))
	{
		use_linear = false;
	}
	else
	{
		printf("<format> must be either 'linear' or 'srgb'\n");
		exit(1);
	}

	bool use_post_blend = false;
	if (!strcmp(argv[4], "post"))
	{
		use_post_blend = true;
	}
	else if (!strcmp(argv[4], "pre"))
	{
		use_post_blend = false;
	}
	else
	{
		printf("<blend_mode> must be either 'post' or 'pre'\n");
		exit(1);
	}

	bool use_filter = false;
	if (!strcmp(argv[5], "on"))
	{
		use_filter = true;
	}
	else if (!strcmp(argv[5], "off"))
	{
		use_filter = false;
	}
	else
	{
		printf("<filter> must be either 'on' or 'off'\n");
		exit(1);
	}

	// Load the input image
	int dim_x;
	int dim_y;
	const uint8_t* data_in = stbi_load(src_file, &dim_x, &dim_y, nullptr, 4);
	if (!data_in)
	{
		printf("ERROR: Failed to load input image.\n");
		exit(1);
	}

	// Allocate the output image
	uint8_t* data_out = (uint8_t*)malloc(4 * dim_y * dim_x);
	if (!data_out)
	{
		printf("ERROR: Failed to allocate output image.\n");
		exit(1);
	}

	// For each pixel blending and filtering
	if (!use_filter)
	{
		for (int y = 0; y < dim_y; y++)
		{
			const uint8_t* row_in = data_in + (4 * dim_x * y);
			uint8_t* row_out = data_out + (4 * dim_x * y);

			for (int x = 0; x < dim_x; x++)
			{
				const uint8_t* pixel_in = row_in + 4 * x;
				uint8_t* pixel_out = row_out + 4 * x;

				float r_src = static_cast<float>(pixel_in[0]) / 255.0f;
				float g_src = static_cast<float>(pixel_in[1]) / 255.0f;
				float b_src = static_cast<float>(pixel_in[2]) / 255.0f;
				float a_src = static_cast<float>(pixel_in[3]) / 255.0f;

				if (use_linear == false)
				{
					r_src = srgb_to_linear(r_src);
					g_src = srgb_to_linear(g_src);
					b_src = srgb_to_linear(b_src);
				}

				float r_dst = 0.53f;
				float g_dst = 0.53f;
				float b_dst = 0.53f;

				float r_out;
				float g_out;
				float b_out;
				float a_out;

				// Post-multiply blending
				if (use_post_blend)
				{
					r_out = (r_dst * (1.0f - a_src)) + (r_src * a_src);
					g_out = (g_dst * (1.0f - a_src)) + (g_src * a_src);
					b_out = (b_dst * (1.0f - a_src)) + (b_src * a_src);
					a_out = 1.0f;
				}
				// Pre-multiply blending
				else
				{
					r_out = (r_dst * (1.0f - a_src)) + (r_src * 1.0f);
					g_out = (g_dst * (1.0f - a_src)) + (g_src * 1.0f);
					b_out = (b_dst * (1.0f - a_src)) + (b_src * 1.0f);
					a_out = 1.0f;
				}

				// Clamp color between 0 and 1.0f
				r_out = astc::min(r_out, 1.0f);
				g_out = astc::min(g_out, 1.0f);
				b_out = astc::min(b_out, 1.0f);

				if (use_linear == false)
				{
					r_out = linear_to_srgb(r_out);
					g_out = linear_to_srgb(g_out);
					b_out = linear_to_srgb(b_out);
				}

				pixel_out[0] = (uint8_t)(r_out * 255.0f);
				pixel_out[1] = (uint8_t)(g_out * 255.0f);
				pixel_out[2] = (uint8_t)(b_out * 255.0f);
				pixel_out[3] = (uint8_t)(a_out * 255.0f);
			}
		}
	}
	else
	{
		for (int y = 0; y < dim_y - 1; y++)
		{
			const uint8_t* row_in_0 = data_in + (4 * dim_x * y);
			const uint8_t* row_in_1 = data_in + (4 * dim_x * (y + 1));

			uint8_t* row_out = data_out + (4 * (dim_x - 1) * y);

			for (int x = 0; x < dim_x - 1; x++)
			{
				const uint8_t* pixel_in_00 = row_in_0 + 4 * x;
				const uint8_t* pixel_in_01 = row_in_0 + 4 * (x + 1);
				const uint8_t* pixel_in_10 = row_in_1 + 4 * x;
				const uint8_t* pixel_in_11 = row_in_1 + 4 * (x + 1);

				uint8_t* pixel_out = row_out + 4 * x;

				// Bilinear filter with a half-pixel offset
				float r_src = static_cast<float>(pixel_in_00[0] + pixel_in_01[0] + pixel_in_10[0] + pixel_in_11[0]) / (255.0f * 4.0f);
				float g_src = static_cast<float>(pixel_in_00[1] + pixel_in_01[1] + pixel_in_10[1] + pixel_in_11[1]) / (255.0f * 4.0f);
				float b_src = static_cast<float>(pixel_in_00[2] + pixel_in_01[2] + pixel_in_10[2] + pixel_in_11[2]) / (255.0f * 4.0f);
				float a_src = static_cast<float>(pixel_in_00[3] + pixel_in_01[3] + pixel_in_10[3] + pixel_in_11[3]) / (255.0f * 4.0f);

				if (use_linear == false)
				{
					r_src = srgb_to_linear(r_src);
					g_src = srgb_to_linear(g_src);
					b_src = srgb_to_linear(b_src);
				}

				float r_dst = 0.8f;
				float g_dst = 1.0f;
				float b_dst = 0.8f;

				float r_out;
				float g_out;
				float b_out;
				float a_out;

				// Post-multiply blending
				if (use_post_blend)
				{
					r_out = (r_dst * (1.0f - a_src)) + (r_src * a_src);
					g_out = (g_dst * (1.0f - a_src)) + (g_src * a_src);
					b_out = (b_dst * (1.0f - a_src)) + (b_src * a_src);
					a_out = 1.0f;
				}
				// Pre-multiply blending
				else
				{
					r_out = (r_dst * (1.0f - a_src)) + (r_src * 1.0f);
					g_out = (g_dst * (1.0f - a_src)) + (g_src * 1.0f);
					b_out = (b_dst * (1.0f - a_src)) + (b_src * 1.0f);
					a_out = 1.0f;
				}

				// Clamp color between 0 and 1.0f
				r_out = astc::min(r_out, 1.0f);
				g_out = astc::min(g_out, 1.0f);
				b_out = astc::min(b_out, 1.0f);

				if (use_linear == false)
				{
					r_out = linear_to_srgb(r_out);
					g_out = linear_to_srgb(g_out);
					b_out = linear_to_srgb(b_out);
				}

				pixel_out[0] = (uint8_t)(r_out * 255.0f);
				pixel_out[1] = (uint8_t)(g_out * 255.0f);
				pixel_out[2] = (uint8_t)(b_out * 255.0f);
				pixel_out[3] = (uint8_t)(a_out * 255.0f);
			}
		}
	}

	// Write out the result
	if (!use_filter)
	{
		stbi_write_png(dst_file, dim_x, dim_y, 4, data_out, 4 * dim_x);
	}
	else
	{
		stbi_write_png(dst_file, dim_x - 1, dim_y - 1, 4, data_out, 4 * (dim_x - 1));
	}


	return 0;
}