Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactor, fixed bugs and add Win64 func #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions basisu_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ static bool compress_mode(command_line_params &opts)
if (opts.m_output_path.size())
string_combine_path(filename, opts.m_output_path.c_str(), filename.c_str());

params.m_out_filename = filename;
params.m_out_filename = std::move(filename);
}

if (opts.m_parallel_compression)
Expand Down Expand Up @@ -1244,40 +1244,41 @@ static bool compress_mode(command_line_params &opts)
}
}

if ((pCSV_file) && (c.get_stats().size()))
const auto& stats = c.get_stats();
if ((pCSV_file) && (stats.size()))
{
if (c.get_stats().size())
if (stats.size())
{
float rgb_avg_psnr_min = 1e+9f, rgb_avg_psnr_avg = 0.0f;
float a_avg_psnr_min = 1e+9f, a_avg_psnr_avg = 0.0f;
float luma_709_psnr_min = 1e+9f, luma_709_psnr_avg = 0.0f;

for (size_t slice_index = 0; slice_index < c.get_stats().size(); slice_index++)
for (size_t slice_index = 0; slice_index < stats.size(); slice_index++)
{
rgb_avg_psnr_min = basisu::minimum(rgb_avg_psnr_min, c.get_stats()[slice_index].m_basis_rgb_avg_psnr);
rgb_avg_psnr_avg += c.get_stats()[slice_index].m_basis_rgb_avg_psnr;
rgb_avg_psnr_min = basisu::minimum(rgb_avg_psnr_min, stats[slice_index].m_basis_rgb_avg_psnr);
rgb_avg_psnr_avg += stats[slice_index].m_basis_rgb_avg_psnr;

a_avg_psnr_min = basisu::minimum(a_avg_psnr_min, c.get_stats()[slice_index].m_basis_a_avg_psnr);
a_avg_psnr_avg += c.get_stats()[slice_index].m_basis_a_avg_psnr;
a_avg_psnr_min = basisu::minimum(a_avg_psnr_min, stats[slice_index].m_basis_a_avg_psnr);
a_avg_psnr_avg += stats[slice_index].m_basis_a_avg_psnr;

luma_709_psnr_min = basisu::minimum(luma_709_psnr_min, c.get_stats()[slice_index].m_basis_luma_709_psnr);
luma_709_psnr_avg += c.get_stats()[slice_index].m_basis_luma_709_psnr;
luma_709_psnr_min = basisu::minimum(luma_709_psnr_min, stats[slice_index].m_basis_luma_709_psnr);
luma_709_psnr_avg += stats[slice_index].m_basis_luma_709_psnr;
}

rgb_avg_psnr_avg /= c.get_stats().size();
a_avg_psnr_avg /= c.get_stats().size();
luma_709_psnr_avg /= c.get_stats().size();
rgb_avg_psnr_avg /= stats.size();
a_avg_psnr_avg /= stats.size();
luma_709_psnr_avg /= stats.size();

fprintf(pCSV_file, "\"%s\", %u, %u, %u, %u, %u, %f, %f, %f, %f, %f, %u, %u, %f, %f, %f, %f, %f, %f, %f\n",
params.m_out_filename.c_str(),
c.get_basis_file_size(),
(uint32_t)c.get_stats().size(),
c.get_stats()[0].m_width, c.get_stats()[0].m_height, (uint32_t)c.get_any_source_image_has_alpha(),
(uint32_t)stats.size(),
stats[0].m_width, stats[0].m_height, (uint32_t)c.get_any_source_image_has_alpha(),
c.get_basis_bits_per_texel(),
c.get_stats()[0].m_basis_rgb_avg_psnr,
c.get_stats()[0].m_basis_rgba_avg_psnr,
c.get_stats()[0].m_basis_luma_709_psnr,
c.get_stats()[0].m_best_etc1s_luma_709_psnr,
stats[0].m_basis_rgb_avg_psnr,
stats[0].m_basis_rgba_avg_psnr,
stats[0].m_basis_luma_709_psnr,
stats[0].m_best_etc1s_luma_709_psnr,
params.m_quality_level, (int)params.m_compression_level, tm.get_elapsed_secs(),
rgb_avg_psnr_min, rgb_avg_psnr_avg,
a_avg_psnr_min, a_avg_psnr_avg,
Expand Down Expand Up @@ -1425,18 +1426,20 @@ static bool unpack_and_validate_ktx2_file(
printf("Total key values: %u\n", dec.get_key_values().size());
for (uint32_t i = 0; i < dec.get_key_values().size(); i++)
{
printf("%u. Key: \"%s\", Value length in bytes: %u", i, (const char*)dec.get_key_values()[i].m_key.data(), dec.get_key_values()[i].m_value.size());
const auto& key = dec.get_key_values()[i].m_key;
const auto& value = dec.get_key_values()[i].m_value;
printf("%u. Key: \"%s\", Value length in bytes: %u", i, (const char*)key.data(), value.size());

if (dec.get_key_values()[i].m_value.size() > 256)
if (value.size() > 256)
continue;

bool is_ascii = true;
for (uint32_t j = 0; j < dec.get_key_values()[i].m_value.size(); j++)
for (uint32_t j = 0; j < value.size(); j++)
{
uint8_t c = dec.get_key_values()[i].m_value[j];
uint8_t c = value[j];
if (!(
((c >= ' ') && (c < 0x80)) ||
((j == dec.get_key_values()[i].m_value.size() - 1) && (!c))
((j == value.size() - 1) && (!c))
))
{
is_ascii = false;
Expand All @@ -1446,18 +1449,18 @@ static bool unpack_and_validate_ktx2_file(

if (is_ascii)
{
uint8_vec s(dec.get_key_values()[i].m_value);
uint8_vec s(value);
s.push_back(0);
printf(" Value String: \"%s\"", (const char *)s.data());
}
else
{
printf(" Value Bytes: ");
for (uint32_t j = 0; j < dec.get_key_values()[i].m_value.size(); j++)
for (uint32_t j = 0; j < value.size(); j++)
{
if (j)
printf(",");
printf("0x%X", dec.get_key_values()[i].m_value[j]);
printf("0x%X", value[j]);
}
}
printf("\n");
Expand All @@ -1467,10 +1470,11 @@ static bool unpack_and_validate_ktx2_file(
{
printf("ETC1S header:\n");

const auto& header = dec.get_etc1s_header();
printf("Endpoint Count: %u, Selector Count: %u, Endpoint Length: %u, Selector Length: %u, Tables Length: %u, Extended Length: %u\n",
(uint32_t)dec.get_etc1s_header().m_endpoint_count, (uint32_t)dec.get_etc1s_header().m_selector_count,
(uint32_t)dec.get_etc1s_header().m_endpoints_byte_length, (uint32_t)dec.get_etc1s_header().m_selectors_byte_length,
(uint32_t)dec.get_etc1s_header().m_tables_byte_length, (uint32_t)dec.get_etc1s_header().m_extended_byte_length);
(uint32_t)header.m_endpoint_count, (uint32_t)header.m_selector_count,
(uint32_t)header.m_endpoints_byte_length, (uint32_t)header.m_selectors_byte_length,
(uint32_t)header.m_tables_byte_length, (uint32_t)header.m_extended_byte_length);

printf("Total ETC1S image descs: %u\n", dec.get_etc1s_image_descs().size());
for (uint32_t i = 0; i < dec.get_etc1s_image_descs().size(); i++)
Expand Down
4 changes: 1 addition & 3 deletions encoder/basisu_basis_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ namespace basisu
BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(basisu_file);

public:
basisu_file()
{
}
basisu_file() = default;

void clear()
{
Expand Down
17 changes: 7 additions & 10 deletions encoder/basisu_comp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ namespace basisu
enlarge_vector(m_slice_images, 1);
enlarge_vector(m_slice_descs, 1);

m_stats[dest_image_index].m_filename = source_filename.c_str();
m_stats[dest_image_index].m_filename = source_filename;
m_stats[dest_image_index].m_width = orig_width;
m_stats[dest_image_index].m_height = orig_height;

Expand Down Expand Up @@ -1619,16 +1619,13 @@ namespace basisu
if ((!m_params.m_uastc) && (m_frontend.get_params().m_debug_images))
{
// Write "best" ETC1S debug images
if (!m_params.m_uastc)
{
gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]);
best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_best_etc1s.ktx").c_str(), best_etc1s_gpu_image);
gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]);
best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_best_etc1s.ktx").c_str(), best_etc1s_gpu_image);

image best_etc1s_unpacked;
best_etc1s_gpu_image.unpack(best_etc1s_unpacked);
save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked);
}
image best_etc1s_unpacked;
best_etc1s_gpu_image.unpack(best_etc1s_unpacked);
save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked);
}

if (m_params.m_debug_images)
Expand Down
11 changes: 8 additions & 3 deletions encoder/basisu_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,19 +514,24 @@ namespace basisu
#endif
if (!pFile)
return false;
fseek(pFile, 0, SEEK_END);
#ifdef _WIN32

#ifdef _WIN32
_fseeki64(pFile, 0, SEEK_END);
int64_t filesize = _ftelli64(pFile);
#else
fseek(pFile, 0, SEEK_END);
int64_t filesize = ftello(pFile);
#endif
if (filesize < 0)
{
fclose(pFile);
return false;
}
#ifdef _WIN32
_fseeki64(pFile, 0, SEEK_SET);
#else
fseek(pFile, 0, SEEK_SET);
#endif

if (sizeof(size_t) == sizeof(uint32_t))
{
Expand Down
12 changes: 4 additions & 8 deletions encoder/basisu_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ namespace basisu
std::mt19937 m_mt;

public:
rand() { }
rand() = default;

rand(uint32_t s) { seed(s); }
void seed(uint32_t s) { m_mt.seed(s); }
Expand Down Expand Up @@ -2267,9 +2267,7 @@ namespace basisu
class huffman_encoding_table
{
public:
huffman_encoding_table()
{
}
huffman_encoding_table() = default;

void clear()
{
Expand Down Expand Up @@ -2464,7 +2462,7 @@ namespace basisu
class huff2D
{
public:
huff2D() { }
huff2D() = default;
huff2D(uint32_t bits_per_sym, uint32_t total_syms_per_group) { init(bits_per_sym, total_syms_per_group); }

inline const histogram &get_histogram() const { return m_histogram; }
Expand Down Expand Up @@ -2561,9 +2559,7 @@ namespace basisu
class palette_index_reorderer
{
public:
palette_index_reorderer()
{
}
palette_index_reorderer() = default;

void clear()
{
Expand Down
8 changes: 4 additions & 4 deletions encoder/basisu_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,8 @@ namespace basisu
} // block_index_iter

#ifndef __EMSCRIPTEN__
m_params.m_pJob_pool->wait_for_all();
if (m_params.m_pJob_pool)
m_params.m_pJob_pool->wait_for_all();
#endif

vec16F_clusterizer selector_clusterizer;
Expand Down Expand Up @@ -2861,7 +2862,6 @@ namespace basisu
total_subblocks_examined += total_pixels / 8;

etc1_optimizer optimizer;
etc1_solution_coordinates solutions[2];

etc1_optimizer::params cluster_optimizer_params;
cluster_optimizer_params.m_num_src_pixels = total_pixels;
Expand Down Expand Up @@ -3242,8 +3242,8 @@ namespace basisu

debug_printf("basisu_frontend::reoptimize_remapped_endpoints: stage 2\n");

m_endpoint_clusters = new_endpoint_clusters;
m_endpoint_cluster_etc_params = new_endpoint_cluster_etc_params;
m_endpoint_clusters = std::move(new_endpoint_clusters);
m_endpoint_cluster_etc_params = std::move(new_endpoint_cluster_etc_params);

eliminate_redundant_or_empty_endpoint_clusters();

Expand Down
2 changes: 1 addition & 1 deletion encoder/basisu_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace basisu
{
uint32_t m_comps[2];

vec2U() { }
vec2U() = default;
vec2U(uint32_t a, uint32_t b) { set(a, b); }

void set(uint32_t a, uint32_t b) { m_comps[0] = a; m_comps[1] = b; }
Expand Down
6 changes: 2 additions & 4 deletions encoder/basisu_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ namespace basisu
m_ocl_mutex.unlock();
}

~ocl()
{
}
~ocl() = default;

bool is_initialized() const { return m_device_id != nullptr; }

Expand Down Expand Up @@ -891,7 +889,7 @@ namespace basisu

g_ocl.destroy_command_queue(pContext->m_command_queue);

memset(pContext, 0, sizeof(opencl_context));
memset(pContext, 0, sizeof(opencl_context_ptr));

free(pContext);

Expand Down
2 changes: 1 addition & 1 deletion encoder/basisu_uastc_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3789,7 +3789,7 @@ namespace basisu
{
uint64_t m_sel;
uint32_t m_ofs;
selector_bitsequence() { }
selector_bitsequence() = default;
selector_bitsequence(uint32_t bit_ofs, uint64_t sel) : m_sel(sel), m_ofs(bit_ofs) { }
bool operator== (const selector_bitsequence& other) const
{
Expand Down
3 changes: 2 additions & 1 deletion encoder/pvpngreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ void* png_decoder::png_malloc(uint32_t len)
break;

if (j == PNG_MAX_ALLOC_BLOCKS)
delete p;
return nullptr;

m_pMalloc_blocks[j] = p;
Expand Down Expand Up @@ -666,7 +667,7 @@ int64_t png_decoder::fetch_next_chunk_dword()
return status;

if (status != 4)
terminate(PNG_BAD_CHUNK_SIZE);
return terminate(PNG_BAD_CHUNK_SIZE);

uint32_t v = buf[3] + ((uint32_t)buf[2] << 8) + ((uint32_t)buf[1] << 16) + ((uint32_t)buf[0] << 24);
return (int64_t)v;
Expand Down
4 changes: 3 additions & 1 deletion transcoder/basisu.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,15 @@ namespace basisu
cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block)
cBC4, // DXT5A
cBC5, // 3DC/DXN (two BC4/DXT5A blocks)
cBC7,
cBC7_ALT,
cBC7_RGBA,
cASTC4x4, // LDR only
cPVRTC1_4_RGB,
cPVRTC1_4_RGBA,
cATC_RGB,
cATC_RGBA_INTERPOLATED_ALPHA,
cFXT1_RGB,
cPVRTC2_4_RGB,
cPVRTC2_4_RGBA,
cETC2_R11_EAC,
cETC2_RG11_EAC,
Expand Down
8 changes: 4 additions & 4 deletions transcoder/basisu_transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10840,7 +10840,7 @@ namespace basist
case transcoder_texture_format::cTFPVRTC1_4_RGB: return "PVRTC1_4_RGB";
case transcoder_texture_format::cTFPVRTC1_4_RGBA: return "PVRTC1_4_RGBA";
case transcoder_texture_format::cTFBC7_RGBA: return "BC7_RGBA";
case transcoder_texture_format::cTFBC7_ALT: return "BC7_RGBA";
case transcoder_texture_format::cTFBC7_ALT: return "BC7_ALT";
case transcoder_texture_format::cTFETC2_RGBA: return "ETC2_RGBA";
case transcoder_texture_format::cTFBC3_RGBA: return "BC3_RGBA";
case transcoder_texture_format::cTFBC5_RG: return "BC5_RG";
Expand Down Expand Up @@ -10942,8 +10942,8 @@ namespace basist
case transcoder_texture_format::cTFBC4_R: return basisu::texture_format::cBC4;
case transcoder_texture_format::cTFPVRTC1_4_RGB: return basisu::texture_format::cPVRTC1_4_RGB;
case transcoder_texture_format::cTFPVRTC1_4_RGBA: return basisu::texture_format::cPVRTC1_4_RGBA;
case transcoder_texture_format::cTFBC7_RGBA: return basisu::texture_format::cBC7;
case transcoder_texture_format::cTFBC7_ALT: return basisu::texture_format::cBC7;
case transcoder_texture_format::cTFBC7_RGBA: return basisu::texture_format::cBC7_RGBA;
case transcoder_texture_format::cTFBC7_ALT: return basisu::texture_format::cBC7_ALT;
case transcoder_texture_format::cTFETC2_RGBA: return basisu::texture_format::cETC2_RGBA;
case transcoder_texture_format::cTFBC3_RGBA: return basisu::texture_format::cBC3;
case transcoder_texture_format::cTFBC5_RG: return basisu::texture_format::cBC5;
Expand All @@ -10955,7 +10955,7 @@ namespace basist
case transcoder_texture_format::cTFBGR565: return basisu::texture_format::cBGR565;
case transcoder_texture_format::cTFRGBA4444: return basisu::texture_format::cRGBA4444;
case transcoder_texture_format::cTFFXT1_RGB: return basisu::texture_format::cFXT1_RGB;
case transcoder_texture_format::cTFPVRTC2_4_RGB: return basisu::texture_format::cPVRTC2_4_RGBA;
case transcoder_texture_format::cTFPVRTC2_4_RGB: return basisu::texture_format::cPVRTC2_4_RGB;
case transcoder_texture_format::cTFPVRTC2_4_RGBA: return basisu::texture_format::cPVRTC2_4_RGBA;
case transcoder_texture_format::cTFETC2_EAC_R11: return basisu::texture_format::cETC2_R11_EAC;
case transcoder_texture_format::cTFETC2_EAC_RG11: return basisu::texture_format::cETC2_RG11_EAC;
Expand Down
4 changes: 1 addition & 3 deletions transcoder/basisu_transcoder_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ namespace basist
friend class bitwise_decoder;

public:
huffman_decoding_table()
{
}
huffman_decoding_table() = default;

void clear()
{
Expand Down