#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)
{
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];
if (opmode == MODE_ENCODE)
{
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);
}
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 (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);
float m_scale = astc::min(1.0f, ceil(max_rgb * 255.0f) / 255.0f);
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);
}
}
stbi_write_png(dst_file, dim_x, dim_y, 4, data_out, 4 * dim_x);
}
else
{
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);
}
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 (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;
}
}
stbi_write_hdr(dst_file, dim_x, dim_y, 4, data_out);
}
return 0;
}