Building png assets.
This commit is contained in:
parent
66a5e7db20
commit
95dbec3543
|
|
@ -5,8 +5,8 @@ ASSET_BUILDER_SOURCES := \
|
|||
|
||||
# Compiler and flags
|
||||
ASSET_BUILDER_CC = gcc
|
||||
#ASSET_BUILDER_CFLAGS = -std=c17 -pedantic -O3 # RELEASE
|
||||
ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g $(shell pkg-config --cflags libpng) \
|
||||
ASSET_BUILDER_CFLAGS = -std=c17 -pedantic -O3 $(shell pkg-config --cflags libpng) # RELEASE
|
||||
#ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g $(shell pkg-config --cflags libpng) \
|
||||
-Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \
|
||||
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
|
||||
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@
|
|||
#include <png.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int parse_png_for_header(const char *file_name, asset_def_t *asset_def);
|
||||
int parse_png_for_header(const char *input_file_name, asset_header_def_t *asset_header_def);
|
||||
int parse_png_for_content(const char *input_file_name, FILE *output_file);
|
||||
|
||||
int parse_file_for_header(const char *file_name, asset_def_t *asset_def)
|
||||
int parse_file_for_header(const char *input_file_name, asset_header_def_t *asset_header_def)
|
||||
{
|
||||
const char *file_name_dot = strrchr(file_name, '.');
|
||||
const char *file_name_dot = strrchr(input_file_name, '.');
|
||||
|
||||
if(!file_name_dot)
|
||||
{
|
||||
|
|
@ -20,7 +21,7 @@ int parse_file_for_header(const char *file_name, asset_def_t *asset_def)
|
|||
|
||||
if(!strcmp(file_name_ext, "png"))
|
||||
{
|
||||
return parse_png_for_header(file_name, asset_def);
|
||||
return parse_png_for_header(input_file_name, asset_header_def);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -30,22 +31,56 @@ int parse_file_for_header(const char *file_name, asset_def_t *asset_def)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int parse_png_for_header(const char *file_name, asset_def_t *asset_def)
|
||||
int write_header_to_file(asset_file_header_t *asset_file_header, FILE *output_file)
|
||||
{
|
||||
FILE *input_png_file = fopen(file_name, "rb");
|
||||
size_t ret = fwrite(&asset_file_header->assets_number, sizeof(size_t), 1, output_file);
|
||||
if(ret != 1)
|
||||
return -8; // failed writing output file
|
||||
|
||||
if(!input_png_file)
|
||||
for(size_t i = 0; i < asset_file_header->assets_number; i++)
|
||||
{
|
||||
size_t name_length = strlen(asset_file_header->assets_header_def[i].name) + 1;
|
||||
ret = fwrite(asset_file_header->assets_header_def[i].name, sizeof(unsigned char), name_length, output_file);
|
||||
if(ret != name_length)
|
||||
return -8; // failed writing output file
|
||||
|
||||
ret = fwrite(&asset_file_header->assets_header_def[i].start, sizeof(size_t), 1, output_file);
|
||||
if(ret != 1)
|
||||
return -8; // failed writing output file
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_file_for_content_and_write(const char *input_file_name, FILE *output_file)
|
||||
{
|
||||
const char *file_name_dot = strrchr(input_file_name, '.');
|
||||
const char *file_name_ext = file_name_dot + 1;
|
||||
|
||||
if(!strcmp(file_name_ext, "png"))
|
||||
{
|
||||
return parse_png_for_content(input_file_name, output_file);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// File specific functions.
|
||||
int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header_def)
|
||||
{
|
||||
FILE *input_file = fopen(file_name, "rb");
|
||||
|
||||
if(!input_file)
|
||||
{
|
||||
fclose(input_png_file);
|
||||
return -3; // failed to open file
|
||||
}
|
||||
|
||||
// Check png autenticity.
|
||||
unsigned char sig[8];
|
||||
fread(sig, 1, 8, input_png_file);
|
||||
fread(sig, 1, 8, input_file);
|
||||
if(!png_check_sig(sig, 8))
|
||||
{
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -4; // bad signature
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +88,7 @@ int parse_png_for_header(const char *file_name, asset_def_t *asset_def)
|
|||
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if(!png_ptr)
|
||||
{
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -5; // out of memory
|
||||
}
|
||||
|
||||
|
|
@ -61,73 +96,66 @@ int parse_png_for_header(const char *file_name, asset_def_t *asset_def)
|
|||
if(!info_ptr)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -5; // out of memory
|
||||
}
|
||||
|
||||
// Fill structs and load data.
|
||||
png_init_io(png_ptr, input_png_file);
|
||||
png_init_io(png_ptr, input_file);
|
||||
png_set_sig_bytes(png_ptr, 8);
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
|
||||
size_t width = png_get_image_width(png_ptr, info_ptr);
|
||||
size_t height = png_get_image_height(png_ptr, info_ptr);
|
||||
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
|
||||
|
||||
// Print infos.
|
||||
printf("%s : %lux%lu -%d ", file_name, width, height, bit_depth);
|
||||
uint8_t bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||
uint8_t color_type = png_get_color_type(png_ptr, info_ptr);
|
||||
|
||||
// Verify png attributes.
|
||||
if(bit_depth != 8)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_file);
|
||||
return -6; // png bit depth not supported
|
||||
}
|
||||
|
||||
switch(color_type)
|
||||
{
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
puts("RGB"); break;
|
||||
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
puts("RGBA"); break;
|
||||
break;
|
||||
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
default:
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_file);
|
||||
return -7; // png color type not supported
|
||||
}
|
||||
|
||||
// Fill asset def.
|
||||
const char *last_slash = strrchr(file_name, '/');
|
||||
const char *base_name = last_slash ? last_slash + 1 : file_name;
|
||||
|
||||
asset_def->name = malloc(strlen(base_name) - 4);
|
||||
strncpy(asset_def->name, base_name, strlen(base_name) - 4);
|
||||
|
||||
asset_def->start = 0;
|
||||
asset_def->end = height * width * 3;
|
||||
asset_header_def->start = 0;
|
||||
asset_header_def->end = height * width * 3;
|
||||
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_png_for_content(const char *file_name, void *buffer)
|
||||
int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||
{
|
||||
FILE *input_png_file = fopen(file_name, "rb");
|
||||
FILE *input_file = fopen(input_file_name, "rb");
|
||||
|
||||
if(!input_png_file)
|
||||
if(!input_file)
|
||||
{
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -3; // failed to open file
|
||||
}
|
||||
|
||||
// Check png autenticity.
|
||||
unsigned char sig[8];
|
||||
fread(sig, 1, 8, input_png_file);
|
||||
fread(sig, 1, 8, input_file);
|
||||
if(!png_check_sig(sig, 8))
|
||||
{
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -4; // bad signature
|
||||
}
|
||||
|
||||
|
|
@ -135,7 +163,7 @@ int parse_png_for_content(const char *file_name, void *buffer)
|
|||
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if(!png_ptr)
|
||||
{
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -5; // out of memory
|
||||
}
|
||||
|
||||
|
|
@ -143,40 +171,34 @@ int parse_png_for_content(const char *file_name, void *buffer)
|
|||
if(!info_ptr)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
return -5; // out of memory
|
||||
}
|
||||
|
||||
// Fill structs and load data.
|
||||
png_init_io(png_ptr, input_png_file);
|
||||
png_init_io(png_ptr, input_file);
|
||||
png_set_sig_bytes(png_ptr, 8);
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
|
||||
size_t width = png_get_image_width(png_ptr, info_ptr);
|
||||
size_t height = png_get_image_height(png_ptr, info_ptr);
|
||||
size_t row_length = png_get_rowbytes(png_ptr, info_ptr);
|
||||
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
|
||||
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
|
||||
|
||||
// Show infos.
|
||||
printf("%s : %lux%lu -%d ", file_name, width, height, bit_depth);
|
||||
|
||||
switch(color_type)
|
||||
// Write image size.
|
||||
size_t ret = fwrite(&width, sizeof(size_t), 1, output_file);
|
||||
if(!ret)
|
||||
{
|
||||
case PNG_COLOR_TYPE_GRAY :
|
||||
puts("grayscale"); break;
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_file);
|
||||
return -8; // failed writing to output file
|
||||
}
|
||||
|
||||
case PNG_COLOR_TYPE_GRAY_ALPHA :
|
||||
puts("gray with alpha"); break;
|
||||
|
||||
case PNG_COLOR_TYPE_PALETTE :
|
||||
puts("palette"); break;
|
||||
|
||||
case PNG_COLOR_TYPE_RGB :
|
||||
puts("RGB"); break;
|
||||
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA :
|
||||
puts("RGBA");
|
||||
ret = fwrite(&height, sizeof(size_t), 1, output_file);
|
||||
if(!ret)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_file);
|
||||
return -8; // failed writing to output file
|
||||
}
|
||||
|
||||
// Prepare storage.
|
||||
|
|
@ -188,30 +210,29 @@ int parse_png_for_content(const char *file_name, void *buffer)
|
|||
// Read pixels.
|
||||
png_read_image(png_ptr, row_ptrs);
|
||||
|
||||
for(size_t i = 0; i < height; i += 2)
|
||||
for(size_t i = 0; i < height; i++)
|
||||
{
|
||||
for(size_t j = 0; j < row_length; j += 4)
|
||||
{
|
||||
if(pixels[i * row_length + j + 3])
|
||||
printf("\033[48;2;%d;%d;%dm", pixels[i * row_length + j], pixels[i * row_length + j + 1], pixels[i * row_length + j + 2]);
|
||||
else
|
||||
printf("\033[48;2;255;0;255m");
|
||||
if(!pixels[i * row_length + j + 3])
|
||||
{
|
||||
pixels[i * row_length + j] = 0xFF;
|
||||
pixels[i * row_length + j + 1] = 0x00;
|
||||
pixels[i * row_length + j + 2] = 0xFF;
|
||||
}
|
||||
|
||||
if(pixels[(i + 1) * row_length + j + 3])
|
||||
printf("\033[38;2;%d;%d;%dm", pixels[(i + 1) * row_length + j], pixels[(i + 1) * row_length + j + 1], pixels[(i + 1) * row_length + j + 2]);
|
||||
else
|
||||
printf("\033[38;2;255;0;255m");
|
||||
|
||||
printf("▄");
|
||||
ret = fwrite(&pixels[i * row_length + j], sizeof(png_byte), 3, output_file);
|
||||
if(ret != 3)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_file);
|
||||
return -8; // failed writing to output file
|
||||
}
|
||||
}
|
||||
|
||||
puts("\033[m");
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||
fclose(input_png_file);
|
||||
fclose(input_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,24 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct asset_def_t {
|
||||
typedef struct asset_header_def_t {
|
||||
char *name;
|
||||
uint32_t start, end;
|
||||
} asset_def_t;
|
||||
size_t start, end;
|
||||
} asset_header_def_t;
|
||||
|
||||
typedef struct asset_file_header_t {
|
||||
uint32_t assets_number;
|
||||
asset_def_t *assets_def;
|
||||
size_t assets_number;
|
||||
asset_header_def_t *assets_header_def;
|
||||
|
||||
size_t header_size;
|
||||
} asset_file_header_t;
|
||||
|
||||
int parse_file_for_header(const char *file_name, asset_def_t *asset_def);
|
||||
int parse_file_for_header(const char *input_file_name, asset_header_def_t *asset_header_def);
|
||||
|
||||
int write_header_to_file(asset_file_header_t *asset_file_header, FILE *output_file);
|
||||
|
||||
int parse_file_for_content_and_write(const char *input_file_name, FILE *output_file);
|
||||
|
||||
#endif // ASSET_FILE_H
|
||||
|
|
@ -10,36 +10,56 @@ void print_error(const char *content)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exit_status = EXIT_SUCCESS;
|
||||
|
||||
// Verify arguments minimum number.
|
||||
if(argc < 3)
|
||||
{
|
||||
printf("\033[33musage\033[m: asset_builder output_file input_file [input_file_2 ...]");
|
||||
return -1;
|
||||
puts("\033[33musage\033[m: asset_builder output_file input_file [input_file_2 ...]");
|
||||
exit_status = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < (size_t)argc; i++)
|
||||
puts(argv[i]);
|
||||
// Header size and content.
|
||||
asset_file_header_t asset_file_header;
|
||||
asset_file_header.assets_number = argc - 2;
|
||||
asset_file_header.header_size = sizeof(uint32_t) + sizeof(size_t) * 2 * asset_file_header.assets_number;
|
||||
asset_file_header.assets_header_def = malloc(asset_file_header.assets_number * sizeof(asset_header_def_t));
|
||||
|
||||
// Create and open output file.
|
||||
FILE *output_asset = fopen(argv[1], "wb");
|
||||
// Set assets name.
|
||||
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
||||
{
|
||||
const char *file_name = argv[i + 2];
|
||||
|
||||
if(!output_asset)
|
||||
const char *file_name_last_slash = strrchr(file_name, '/');
|
||||
const char *file_base_name = file_name_last_slash ? file_name_last_slash + 1 : file_name;
|
||||
|
||||
const char *file_name_dot = strrchr(file_name, '.');
|
||||
const char *file_name_ext = file_name_dot ? file_name_dot + 1 : file_name;
|
||||
|
||||
size_t asset_name_length = strlen(file_base_name) - strlen(file_name_ext) - 1;
|
||||
|
||||
asset_file_header.assets_header_def[i].name = malloc(asset_name_length);
|
||||
strncpy(asset_file_header.assets_header_def[i].name, file_base_name, asset_name_length);
|
||||
|
||||
// Update header size.
|
||||
asset_file_header.header_size += asset_name_length;
|
||||
}
|
||||
|
||||
// Parse input files to create header structure and fill output file.
|
||||
FILE *output_asset_file = fopen(argv[1], "wb");
|
||||
|
||||
if(!output_asset_file)
|
||||
{
|
||||
print_error("could not create output file.");
|
||||
return -1;
|
||||
exit_status = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
// Parse input files to create header structure.
|
||||
asset_file_header_t asset_file_header;
|
||||
|
||||
asset_file_header.assets_number = argc - 2;
|
||||
asset_file_header.assets_def = malloc(asset_file_header.assets_number * sizeof(asset_def_t));
|
||||
asset_file_header.header_size = sizeof(uint32_t) + sizeof(uint32_t) * 2 * asset_file_header.assets_number;
|
||||
|
||||
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
||||
{
|
||||
// Fill asset_def.
|
||||
int ret = parse_file_for_header(argv[i + 2], &asset_file_header.assets_def[i]);
|
||||
int ret = parse_file_for_header(argv[i + 2], &asset_file_header.assets_header_def[i]);
|
||||
|
||||
if(ret)
|
||||
{
|
||||
switch(ret)
|
||||
|
|
@ -76,24 +96,68 @@ int main(int argc, char *argv[])
|
|||
print_error("unknown error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
exit_status = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
asset_file_header.header_size += strlen(asset_file_header.assets_def[i].name);
|
||||
|
||||
// Calculate absolute position of assets in the file.
|
||||
if(i) asset_file_header.assets_header_def[i].start = asset_file_header.assets_header_def[i - 1].end + 1;
|
||||
else asset_file_header.assets_header_def[0].start = asset_file_header.header_size + 1;
|
||||
asset_file_header.assets_header_def[i].end += asset_file_header.assets_header_def[i].start;
|
||||
}
|
||||
|
||||
asset_file_header.assets_def[0].start = asset_file_header.header_size + 1;
|
||||
asset_file_header.assets_def[0].end += asset_file_header.assets_def[0].start;
|
||||
|
||||
for(size_t i = 1; i < asset_file_header.assets_number; i++)
|
||||
if(write_header_to_file(&asset_file_header, output_asset_file))
|
||||
{
|
||||
// Adjust start and end.
|
||||
asset_file_header.assets_def[i].start = asset_file_header.assets_def[i - 1].end + 1;
|
||||
asset_file_header.assets_def[i].end += asset_file_header.assets_def[i].start;
|
||||
print_error("can't write to output file.");
|
||||
exit_status = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
||||
{
|
||||
printf("%s : %d - %d\n", asset_file_header.assets_def[i].name, asset_file_header.assets_def[i].start, asset_file_header.assets_def[i].end);
|
||||
int ret = parse_file_for_content_and_write(argv[i + 2], output_asset_file);
|
||||
|
||||
if(ret)
|
||||
{
|
||||
switch(ret)
|
||||
{
|
||||
case -3:
|
||||
print_error("failed to open file.");
|
||||
return -1;
|
||||
|
||||
case -4:
|
||||
print_error("file not valid.");
|
||||
return -1;
|
||||
|
||||
case -5:
|
||||
print_error("memory allocation failed.");
|
||||
return -1;
|
||||
|
||||
case -6:
|
||||
print_error("png bit depth not supported.");
|
||||
return -1;
|
||||
|
||||
case -7:
|
||||
print_error("png color type not supported.");
|
||||
return -1;
|
||||
|
||||
case -8:
|
||||
print_error("failed writing to output file.");
|
||||
return -1;
|
||||
|
||||
default:
|
||||
print_error("unknown error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
exit_status = EXIT_FAILURE;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
end:
|
||||
if(output_asset_file) fclose(output_asset_file);
|
||||
|
||||
return exit_status;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue