File: astc_rgbm_codec.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 (173 lines) | stat: -rw-r--r-- 4,658 bytes parent folder | download
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
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2021 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 encode HDR into RGBM, or decode RGBM into HDR.

#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"

#define MODE_ENCODE 0
#define MODE_DECODE 1

int main(int argc, char **argv)
{
	// Parse command line
	if (argc != 6)
	{
		printf("Usage: astc_rgbm_codec [-ch|-dh] <M> <low_clamp> <source> <dest>\n");
		exit(1);
	}

	int opmode;
	if (strcmp(argv[1], "-ch") == 0)
	{
		opmode = MODE_ENCODE;
	}
	else if (strcmp(argv[1], "-dh") == 0)
	{
		opmode = MODE_DECODE;
	}
	else
	{
		printf("ERROR: Bad operation mode\n");
		exit(1);
	}

 	float rgbm_multiplier = atof(argv[2]);
 	float low_clamp = atof(argv[3]);

	const char* src_file = argv[4];
	const char* dst_file = argv[5];

	// Convert an HDR input file into an RGBM encoded LDR file
	if (opmode == MODE_ENCODE)
	{
		// Load the input image
		int dim_x;
		int dim_y;
		const float* data_in = stbi_loadf(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 allow output image.\n");
			exit(1);
		}

		// For each pixel apply RGBM encoding
		for (int y = 0; y < dim_y; y++)
		{
			const float* 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 float* pixel_in = row_in + 4 * x;
				uint8_t* pixel_out = row_out + 4 * x;

				float r_in = pixel_in[0] / rgbm_multiplier;
				float g_in = pixel_in[1] / rgbm_multiplier;
				float b_in = pixel_in[2] / rgbm_multiplier;

				float max_rgb = astc::max(r_in, g_in, b_in);

				// Ensure we always round up to next largest M
				float m_scale = astc::min(1.0f, ceil(max_rgb * 255.0f) / 255.0f);

				// But keep well above zero to avoid clamps in the compressor
				m_scale = astc::max(m_scale, low_clamp / 255.0f);

				float r_scale = astc::min(1.0f, r_in / m_scale);
				float g_scale = astc::min(1.0f, g_in / m_scale);
				float b_scale = astc::min(1.0f, b_in / m_scale);

				pixel_out[0] = (uint8_t)(r_scale * 255.0f);
				pixel_out[1] = (uint8_t)(g_scale * 255.0f);
				pixel_out[2] = (uint8_t)(b_scale * 255.0f);
				pixel_out[3] = (uint8_t)(m_scale * 255.0f);
			}
		}

		// Write out the result
		stbi_write_png(dst_file, dim_x, dim_y, 4, data_out, 4 * dim_x);
	}
	// Convert an RGBM encoded LDR file into an HDR file
	else
	{
		// 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
		float* data_out = (float*)malloc(4 * dim_y * dim_x * sizeof(float));
		if (!data_out)
		{
			printf("ERROR: Failed to allow output image.\n");
			exit(1);
		}

		// For each pixel apply RGBM decoding
		for (int y = 0; y < dim_y; y++)
		{
			const uint8_t* row_in = data_in + (4 * dim_x * y);
			float* 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;
				float* pixel_out = row_out + 4 * x;

				float r_scale = ((float)pixel_in[0]) / 255.0f;
				float g_scale = ((float)pixel_in[1]) / 255.0f;
				float b_scale = ((float)pixel_in[2]) / 255.0f;

				float m_scale = ((float)pixel_in[3]) / 255.0f;

				pixel_out[0] = r_scale * (m_scale * rgbm_multiplier);
				pixel_out[1] = g_scale * (m_scale * rgbm_multiplier);
				pixel_out[2] = b_scale * (m_scale * rgbm_multiplier);
				pixel_out[3] = 1.0f;
			}
		}

		// Write out the result
		stbi_write_hdr(dst_file, dim_x, dim_y, 4, data_out);
	}

	return 0;
}