summaryrefslogtreecommitdiffstats
path: root/lisiblepng
diff options
context:
space:
mode:
authorClement Sibille <clements+git@lisible.xyz>2024-05-20 02:15:11 +0900
committerClement Sibille <clements+git@lisible.xyz>2024-05-20 02:15:11 +0900
commitc0d9501d787d9a61884233ecb63182a02e413f5f (patch)
tree4348356e2e3392f2895e63befdecfdddbf341d10 /lisiblepng
parent378206a6b3d2859ee1d1dd79f4d31d7f5663d924 (diff)
Use lisiblestd-0.1.0
Diffstat (limited to 'lisiblepng')
-rw-r--r--lisiblepng/meson.build5
-rw-r--r--lisiblepng/src/lisiblepng.c133
-rw-r--r--lisiblepng/src/lisiblepng/assert.h15
-rw-r--r--lisiblepng/src/lisiblepng/bitstream.c19
-rw-r--r--lisiblepng/src/lisiblepng/deflate.c102
-rw-r--r--lisiblepng/src/lisiblepng/log.h23
6 files changed, 130 insertions, 167 deletions
diff --git a/lisiblepng/meson.build b/lisiblepng/meson.build
index 20b242c..644f974 100644
--- a/lisiblepng/meson.build
+++ b/lisiblepng/meson.build
@@ -6,6 +6,7 @@ if get_option('buildtype').startswith('debug')
add_project_arguments('-DLPNG_COMPUTE_CRC', language: ['c'])
endif
+lisiblestd_dep = dependency('lisiblestd')
lisiblepng_incdir = include_directories('src/')
-lisiblepng_lib = library('lisiblepng', 'src/lisiblepng.c', 'src/lisiblepng/deflate.c', 'src/lisiblepng/bitstream.c', dependencies: [m_dep])
-lisiblepng_dep = declare_dependency(include_directories: lisiblepng_incdir, link_with: [lisiblepng_lib], dependencies: [m_dep])
+lisiblepng_lib = library('lisiblepng', 'src/lisiblepng.c', 'src/lisiblepng/deflate.c', 'src/lisiblepng/bitstream.c', dependencies: [m_dep, lisiblestd_dep])
+lisiblepng_dep = declare_dependency(include_directories: lisiblepng_incdir, link_with: [lisiblepng_lib], dependencies: [m_dep, lisiblestd_dep])
diff --git a/lisiblepng/src/lisiblepng.c b/lisiblepng/src/lisiblepng.c
index 34040b8..fdcf053 100644
--- a/lisiblepng/src/lisiblepng.c
+++ b/lisiblepng/src/lisiblepng.c
@@ -1,8 +1,8 @@
#include "lisiblepng.h"
-#include "lisiblepng/assert.h"
#include "lisiblepng/deflate.h"
-#include "lisiblepng/log.h"
#include <errno.h>
+#include <lisiblestd/assert.h>
+#include <lisiblestd/log.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -82,23 +82,23 @@ struct LisPng {
};
LisPngColourType LisPng_colour_type(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
return png->colour_type;
}
uint8_t LisPng_bits_per_sample(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
return png->bits_per_sample;
}
uint8_t *LisPng_data_ptr(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
return png->data;
}
uint32_t LisPng_width(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
return png->width;
}
uint32_t LisPng_height(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
return png->height;
}
@@ -115,7 +115,7 @@ uint8_t LisPngColourType_sample_count(const LisPngColourType colour_type) {
case LisPngColourType_TruecolourWithAlpha:
return 4;
default:
- LPNG_LOG_ERR0("Unknown colour type");
+ LOG0_ERROR("Unknown colour type");
abort();
}
}
@@ -127,8 +127,8 @@ typedef struct {
} DeflateDecompressor;
void DeflateDecompressor_init(DeflateDecompressor *ctx, FILE *stream) {
- ASSERT(ctx != NULL);
- ASSERT(stream != NULL);
+ LSTD_ASSERT(ctx != NULL);
+ LSTD_ASSERT(stream != NULL);
ctx->stream = stream;
#ifdef LPNG_COMPUTE_CRC
ctx->computed_crc = 0xFFFFFFFFu;
@@ -136,7 +136,7 @@ void DeflateDecompressor_init(DeflateDecompressor *ctx, FILE *stream) {
}
void ParsingContext_crc_reset(DeflateDecompressor *ctx) {
- ASSERT(ctx != NULL);
+ LSTD_ASSERT(ctx != NULL);
#ifdef LPNG_COMPUTE_CRC
ctx->computed_crc = 0xFFFFFFFFu;
#endif // LPNG_COMPUTE_CRC
@@ -144,20 +144,20 @@ void ParsingContext_crc_reset(DeflateDecompressor *ctx) {
#ifdef LPNG_COMPUTE_CRC
uint32_t ParsingContext_computed_crc(DeflateDecompressor *ctx) {
- ASSERT(ctx != NULL);
+ LSTD_ASSERT(ctx != NULL);
return ctx->computed_crc ^ 0xFFFFFFFFu;
}
#endif // LPNG_COMPUTE_CRC
long ParsingContext_cursor_position(DeflateDecompressor *ctx) {
- ASSERT(ctx != NULL);
+ LSTD_ASSERT(ctx != NULL);
return ftell(ctx->stream);
}
bool ParsingContext_skip_bytes(DeflateDecompressor *ctx, size_t byte_count) {
- ASSERT(ctx != NULL);
+ LSTD_ASSERT(ctx != NULL);
if (fseek(ctx->stream, byte_count, SEEK_CUR) != 0) {
- LPNG_LOG_ERR("Couldn't skip bytes: %s", strerror(errno));
+ LOG_ERROR("Couldn't skip bytes: %s", strerror(errno));
return false;
}
@@ -166,10 +166,10 @@ bool ParsingContext_skip_bytes(DeflateDecompressor *ctx, size_t byte_count) {
bool ParsingContext_parse_bytes(DeflateDecompressor *ctx, size_t byte_count,
uint8_t *output_buffer) {
- ASSERT(ctx != NULL);
- ASSERT(output_buffer != NULL);
+ LSTD_ASSERT(ctx != NULL);
+ LSTD_ASSERT(output_buffer != NULL);
if (fread(output_buffer, 1, byte_count, ctx->stream) < byte_count) {
- LPNG_LOG_ERR0("Couldn't parse bytes, EOF reached");
+ LOG0_ERROR("Couldn't parse bytes, EOF reached");
return false;
}
@@ -185,8 +185,8 @@ bool ParsingContext_parse_bytes(DeflateDecompressor *ctx, size_t byte_count,
bool ParsingContext_parse_uint32_t(DeflateDecompressor *ctx,
uint32_t *output_u32) {
- ASSERT(ctx != NULL);
- ASSERT(output_u32 != NULL);
+ LSTD_ASSERT(ctx != NULL);
+ LSTD_ASSERT(output_u32 != NULL);
uint8_t bytes[4];
if (!ParsingContext_parse_bytes(ctx, 4, bytes)) {
return false;
@@ -198,8 +198,8 @@ bool ParsingContext_parse_uint32_t(DeflateDecompressor *ctx,
bool ParsingContext_parse_uint8_t(DeflateDecompressor *ctx,
uint8_t *output_u8) {
- ASSERT(ctx != NULL);
- ASSERT(output_u8 != NULL);
+ LSTD_ASSERT(ctx != NULL);
+ LSTD_ASSERT(output_u8 != NULL);
if (!ParsingContext_parse_bytes(ctx, 1, output_u8)) {
return false;
}
@@ -233,18 +233,17 @@ typedef struct {
} ImageData;
void ImageHeader_print_image_header(const ImageHeader *image_header) {
- ASSERT(image_header != NULL);
- LPNG_LOG_DBG("Image header:\n"
- "- dimensions: %dx%d\n"
- "- bit depth: %d\n"
- "- colour type: %d\n"
- "- compression method: %d\n"
- "- filter method: %d\n"
- "- interlace method: %d",
- image_header->width, image_header->height,
- image_header->bit_depth, image_header->colour_type,
- image_header->compression_method, image_header->filter_method,
- image_header->interlace_method);
+ LSTD_ASSERT(image_header != NULL);
+ LOG_DEBUG("Image header:\n"
+ "- dimensions: %dx%d\n"
+ "- bit depth: %d\n"
+ "- colour type: %d\n"
+ "- compression method: %d\n"
+ "- filter method: %d\n"
+ "- interlace method: %d",
+ image_header->width, image_header->height, image_header->bit_depth,
+ image_header->colour_type, image_header->compression_method,
+ image_header->filter_method, image_header->interlace_method);
}
bool ParsingContext_validate_crc_if_required(DeflateDecompressor *ctx) {
@@ -253,7 +252,7 @@ bool ParsingContext_validate_crc_if_required(DeflateDecompressor *ctx) {
uint32_t crc;
PARSE_FIELD(uint32_t, crc);
if (computed_crc != crc) {
- LPNG_LOG_ERR0("Invalid CRC checksum");
+ LOG0_ERROR("Invalid CRC checksum");
return false;
}
#else
@@ -269,8 +268,8 @@ bool is_valid_bit_depth(uint8_t bit_depth) {
}
bool parse_IHDR_chunk(DeflateDecompressor *ctx, ImageHeader *image_header) {
- ASSERT(ctx != NULL);
- ASSERT(image_header != NULL);
+ LSTD_ASSERT(ctx != NULL);
+ LSTD_ASSERT(image_header != NULL);
uint32_t length;
PARSE_FIELD(uint32_t, length);
@@ -280,7 +279,7 @@ bool parse_IHDR_chunk(DeflateDecompressor *ctx, ImageHeader *image_header) {
uint32_t type;
PARSE_FIELD(uint32_t, type);
if (type != IHDR_CHUNK_TYPE) {
- LPNG_LOG_ERR0("Expected IHDR chunk");
+ LOG0_ERROR("Expected IHDR chunk");
return false;
}
@@ -289,7 +288,7 @@ bool parse_IHDR_chunk(DeflateDecompressor *ctx, ImageHeader *image_header) {
PARSE_FIELD(uint32_t, image_header->height);
PARSE_FIELD(uint8_t, image_header->bit_depth);
if (!is_valid_bit_depth(image_header->bit_depth)) {
- LPNG_LOG_ERR("Invalid bitdepth: %d", image_header->bit_depth);
+ LOG_ERROR("Invalid bitdepth: %d", image_header->bit_depth);
return false;
}
PARSE_FIELD(uint8_t, image_header->colour_type);
@@ -297,7 +296,7 @@ bool parse_IHDR_chunk(DeflateDecompressor *ctx, ImageHeader *image_header) {
PARSE_FIELD(uint8_t, image_header->filter_method);
PARSE_FIELD(uint8_t, image_header->interlace_method);
long read_data_length = ParsingContext_cursor_position(ctx) - data_start;
- ASSERT(read_data_length == length);
+ LSTD_ASSERT(read_data_length == length);
if (!ParsingContext_validate_crc_if_required(ctx)) {
return false;
@@ -308,8 +307,8 @@ bool parse_IHDR_chunk(DeflateDecompressor *ctx, ImageHeader *image_header) {
bool parse_IDAT_chunk(DeflateDecompressor *decompressor, uint32_t data_length,
ImageData *image_data) {
- ASSERT(decompressor != NULL);
- ASSERT(image_data != NULL);
+ LSTD_ASSERT(decompressor != NULL);
+ LSTD_ASSERT(image_data != NULL);
image_data->data =
realloc(image_data->data, image_data->length + data_length);
@@ -328,7 +327,7 @@ bool parse_IDAT_chunk(DeflateDecompressor *decompressor, uint32_t data_length,
Palette *parse_PLTE_chunk(DeflateDecompressor *decompressor,
uint32_t data_length) {
- ASSERT(decompressor != NULL);
+ LSTD_ASSERT(decompressor != NULL);
if (data_length % 3 != 0) {
return NULL;
}
@@ -420,10 +419,10 @@ void apply_reconstruction_functions_to_scanline(
uint8_t *output_scanline, const uint8_t *input_scanline,
const uint8_t *previous_output_scanline, size_t filter_type,
size_t scanline_size, size_t bytes_per_pixel) {
- ASSERT(output_scanline != NULL);
- ASSERT(input_scanline != NULL);
- ASSERT(filter_type <
- sizeof(reconstruction_functions) / sizeof(ReconstructionFn));
+ LSTD_ASSERT(output_scanline != NULL);
+ LSTD_ASSERT(input_scanline != NULL);
+ LSTD_ASSERT(filter_type <
+ sizeof(reconstruction_functions) / sizeof(ReconstructionFn));
for (size_t i = 0; i < scanline_size / bytes_per_pixel; i++) {
for (size_t byte = 0; byte < bytes_per_pixel; byte++) {
@@ -451,8 +450,8 @@ void apply_reconstruction_functions_to_scanline(
void apply_reconstruction_functions(LisPng *image,
const uint8_t *decompressed_data_buffer) {
- ASSERT(image != NULL);
- ASSERT(decompressed_data_buffer != NULL);
+ LSTD_ASSERT(image != NULL);
+ LSTD_ASSERT(decompressed_data_buffer != NULL);
size_t sample_count = LisPngColourType_sample_count(image->colour_type);
size_t bits_per_pixel = (image->bits_per_sample * sample_count);
size_t bytes_per_pixel = bits_per_pixel / 8;
@@ -486,12 +485,12 @@ LisPng *LisPng_decode(FILE *stream) {
uint8_t parsed_png_signature[PNG_SIGNATURE_LENGTH];
if (!ParsingContext_parse_bytes(&ctx, PNG_SIGNATURE_LENGTH,
parsed_png_signature)) {
- LPNG_LOG_ERR0("Couldn't parse signature");
+ LOG0_ERROR("Couldn't parse signature");
goto err;
}
if (!matches_png_signature(parsed_png_signature)) {
- LPNG_LOG_ERR0("Invalid signature");
+ LOG0_ERROR("Invalid signature");
goto err;
}
@@ -509,27 +508,27 @@ LisPng *LisPng_decode(FILE *stream) {
while (!end_reached) {
uint32_t length;
if (!ParsingContext_parse_uint32_t(&ctx, &length)) {
- LPNG_LOG_ERR0("Couldn't parse chunk length");
+ LOG0_ERROR("Couldn't parse chunk length");
goto cleanup_data;
}
ParsingContext_crc_reset(&ctx);
uint32_t type;
if (!ParsingContext_parse_uint32_t(&ctx, &type)) {
- LPNG_LOG_ERR0("Couldn't parse chunk type");
+ LOG0_ERROR("Couldn't parse chunk type");
goto cleanup_data;
}
#ifdef LPNG_DEBUG_LOG
uint32_t type_le = uint32_t_to_le(type);
- LPNG_LOG_DBG("Parsing %.4s chunk", (char *)&type_le);
- LPNG_LOG_DBG("Chunk length: %u", length);
+ LOG_DEBUG("Parsing %.4s chunk", (char *)&type_le);
+ LOG_DEBUG("Chunk length: %u", length);
#endif
switch (type) {
case IDAT_CHUNK_TYPE:
if (!parse_IDAT_chunk(&ctx, length, &image_data)) {
- LPNG_LOG_ERR0("Couldn't parse IDAT chunk");
+ LOG0_ERROR("Couldn't parse IDAT chunk");
goto cleanup_data;
}
parsed_data_chunk_count++;
@@ -541,23 +540,23 @@ LisPng *LisPng_decode(FILE *stream) {
case PLTE_CHUNK_TYPE:
palette = parse_PLTE_chunk(&ctx, length);
if (!palette) {
- LPNG_LOG_ERR0("Couldn't parse PLTE chunk");
+ LOG0_ERROR("Couldn't parse PLTE chunk");
goto cleanup_data;
}
break;
default:
- LPNG_LOG_DBG0("Unknown chunk type, skipping chunk...");
+ LOG0_DEBUG("Unknown chunk type, skipping chunk...");
ParsingContext_skip_bytes(&ctx, length + sizeof(uint32_t));
break;
}
}
if (parsed_data_chunk_count == 0) {
- LPNG_LOG_ERR0("No IDAT chunk found, at least one is required");
+ LOG0_ERROR("No IDAT chunk found, at least one is required");
goto cleanup_data;
}
- LPNG_LOG_DBG("Data length: %zu", image_data.length);
+ LOG_DEBUG("Data length: %zu", image_data.length);
png->width = header.width;
png->height = header.height;
png->colour_type = header.colour_type;
@@ -583,8 +582,8 @@ err:
#undef PARSE_FIELD
void LisPng_write_RGBA8_data(const LisPng *png, uint8_t *output_data) {
- ASSERT(png != NULL);
- ASSERT(output_data != NULL);
+ LSTD_ASSERT(png != NULL);
+ LSTD_ASSERT(output_data != NULL);
static const int TARGET_BYTES_PER_PIXEL = 4;
size_t sample_count = LisPngColourType_sample_count(png->colour_type);
size_t bits_per_pixel = png->bits_per_sample * sample_count;
@@ -599,7 +598,7 @@ void LisPng_write_RGBA8_data(const LisPng *png, uint8_t *output_data) {
size_t target_pixel_base = pixel_index * TARGET_BYTES_PER_PIXEL;
if (png->colour_type == LisPngColourType_IndexedColour) {
- ASSERT(png->palette != NULL);
+ LSTD_ASSERT(png->palette != NULL);
size_t absolute_bit_offset = pixel_index * bits_per_pixel;
size_t byte_offset = absolute_bit_offset / 8;
size_t relative_bit_offset = absolute_bit_offset % 8;
@@ -653,7 +652,7 @@ void LisPng_write_RGBA8_data(const LisPng *png, uint8_t *output_data) {
output_data[target_pixel_base + 3] = 0xFF;
}
} else {
- LPNG_LOG_ERR0("Unsupported colour type");
+ LOG0_ERROR("Unsupported colour type");
exit(1);
}
}
@@ -668,7 +667,7 @@ void LisPng_destroy(LisPng *png) {
free(png);
}
void LisPng_dump_ppm(const LisPng *png) {
- ASSERT(png != NULL);
+ LSTD_ASSERT(png != NULL);
printf("P3\n");
printf("%zu %zu\n", png->width, png->height);
if (png->colour_type == LisPngColourType_IndexedColour) {
@@ -686,7 +685,7 @@ void LisPng_dump_ppm(const LisPng *png) {
pixel_index++) {
size_t pixel_base = pixel_index * bytes_per_pixel;
if (png->colour_type == LisPngColourType_IndexedColour) {
- ASSERT(png->palette != NULL);
+ LSTD_ASSERT(png->palette != NULL);
size_t absolute_bit_offset = pixel_index * bits_per_pixel;
size_t byte_offset = absolute_bit_offset / 8;
size_t relative_bit_offset = absolute_bit_offset % 8;
@@ -724,7 +723,7 @@ void LisPng_dump_ppm(const LisPng *png) {
printf("%u %u %u\n", r, g, b);
}
} else {
- LPNG_LOG_ERR0("Unsupported colour type");
+ LOG0_ERROR("Unsupported colour type");
exit(1);
}
}
diff --git a/lisiblepng/src/lisiblepng/assert.h b/lisiblepng/src/lisiblepng/assert.h
deleted file mode 100644
index 1de9c36..0000000
--- a/lisiblepng/src/lisiblepng/assert.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef LISIBLE_PNG_ASSET_H
-#define LISIBLE_PNG_ASSET_H
-
-#include "log.h"
-
-#define ASSERT(predicate) \
- do { \
- if (!(predicate)) { \
- LPNG_LOG_ERR("Assertion failed in %s:%d:\n\t%s", __FILE__, __LINE__, \
- #predicate); \
- exit(1); \
- } \
- } while (0)
-
-#endif // LISIBLE_PNG_ASSET_H
diff --git a/lisiblepng/src/lisiblepng/bitstream.c b/lisiblepng/src/lisiblepng/bitstream.c
index 83e70c6..defac7e 100644
--- a/lisiblepng/src/lisiblepng/bitstream.c
+++ b/lisiblepng/src/lisiblepng/bitstream.c
@@ -1,13 +1,13 @@
#include "bitstream.h"
-#include "assert.h"
+#include <lisiblestd/assert.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
void Bitstream_init(Bitstream *bitstream, const uint8_t *data,
size_t data_size) {
- ASSERT(bitstream != NULL);
- ASSERT(data != NULL);
+ LSTD_ASSERT(bitstream != NULL);
+ LSTD_ASSERT(data != NULL);
bitstream->data = data;
bitstream->data_size = data_size;
bitstream->byte_offset = 0;
@@ -15,7 +15,7 @@ void Bitstream_init(Bitstream *bitstream, const uint8_t *data,
}
void bitstream_advance(Bitstream *bitstream, size_t bit_count) {
- ASSERT(bitstream != NULL);
+ LSTD_ASSERT(bitstream != NULL);
uint8_t current_bit = bitstream->bit_index;
bitstream->byte_offset =
bitstream->byte_offset + (current_bit + bit_count) / 8;
@@ -23,14 +23,15 @@ void bitstream_advance(Bitstream *bitstream, size_t bit_count) {
}
void Bitstream_skip(Bitstream *bitstream, size_t bit_count) {
- ASSERT(bitstream != NULL);
+ LSTD_ASSERT(bitstream != NULL);
bitstream_advance(bitstream, bit_count);
}
uint16_t Bitstream_next_bits(Bitstream *bitstream, int bit_count) {
- ASSERT(bitstream != NULL);
- ASSERT(bit_count <= 16);
- ASSERT((bitstream->bit_index + bit_count - 1) / 8 + bitstream->byte_offset <
- bitstream->data_size);
+ LSTD_ASSERT(bitstream != NULL);
+ LSTD_ASSERT(bit_count <= 16);
+ LSTD_ASSERT((bitstream->bit_index + bit_count - 1) / 8 +
+ bitstream->byte_offset <
+ bitstream->data_size);
uint16_t val = 0;
int bits_collected = 0;
diff --git a/lisiblepng/src/lisiblepng/deflate.c b/lisiblepng/src/lisiblepng/deflate.c
index 36073c6..41933c1 100644
--- a/lisiblepng/src/lisiblepng/deflate.c
+++ b/lisiblepng/src/lisiblepng/deflate.c
@@ -1,6 +1,6 @@
#include "deflate.h"
-#include "assert.h"
#include "bitstream.h"
+#include <lisiblestd/assert.h>
#include <string.h>
#define CM_LENGTH_BITS 4
@@ -49,10 +49,10 @@ typedef struct {
} OutputBuffer;
void OutputBuffer_init(OutputBuffer *output_buffer) {
- ASSERT(output_buffer != NULL);
+ LSTD_ASSERT(output_buffer != NULL);
output_buffer->buffer = calloc(DEFLATE_OUTPUT_BUFFER_INITIAL_CAPACITY, 1);
if (!output_buffer->buffer) {
- LPNG_LOG_ERR0("Couldn't allocate deflate output buffer (out of memory?)");
+ LOG0_ERROR("Couldn't allocate deflate output buffer (out of memory?)");
abort();
}
output_buffer->cap = DEFLATE_OUTPUT_BUFFER_INITIAL_CAPACITY;
@@ -60,11 +60,11 @@ void OutputBuffer_init(OutputBuffer *output_buffer) {
}
void OutputBuffer_expand(OutputBuffer *output_buffer) {
- ASSERT(output_buffer != NULL);
+ LSTD_ASSERT(output_buffer != NULL);
size_t new_cap = output_buffer->cap * 2;
output_buffer->buffer = realloc(output_buffer->buffer, new_cap);
if (!output_buffer->buffer) {
- LPNG_LOG_ERR0("Couldn't reallocate deflate output buffer (out of memory?)");
+ LOG0_ERROR("Couldn't reallocate deflate output buffer (out of memory?)");
abort();
}
@@ -72,7 +72,7 @@ void OutputBuffer_expand(OutputBuffer *output_buffer) {
}
void OutputBuffer_push(OutputBuffer *output_buffer, uint8_t byte) {
- ASSERT(output_buffer != NULL);
+ LSTD_ASSERT(output_buffer != NULL);
if (output_buffer->len == output_buffer->cap) {
OutputBuffer_expand(output_buffer);
}
@@ -81,15 +81,15 @@ void OutputBuffer_push(OutputBuffer *output_buffer, uint8_t byte) {
}
size_t OutputBuffer_length(const OutputBuffer *output_buffer) {
- ASSERT(output_buffer != NULL);
+ LSTD_ASSERT(output_buffer != NULL);
return output_buffer->len;
}
int huffman_table_decode(const uint16_t *symbols, const uint16_t *counts,
Bitstream *bitstream) {
- ASSERT(symbols != NULL);
- ASSERT(counts != NULL);
- ASSERT(bitstream != NULL);
+ LSTD_ASSERT(symbols != NULL);
+ LSTD_ASSERT(counts != NULL);
+ LSTD_ASSERT(bitstream != NULL);
int code = 0;
int index = 0;
int first = 0;
@@ -112,9 +112,9 @@ int huffman_table_decode(const uint16_t *symbols, const uint16_t *counts,
void build_huffman_table_from_codelengths(uint16_t *symbols, uint16_t *counts,
const uint16_t *codelengths,
uint16_t codelength_count) {
- ASSERT(symbols != NULL);
- ASSERT(counts != NULL);
- ASSERT(codelengths != NULL);
+ LSTD_ASSERT(symbols != NULL);
+ LSTD_ASSERT(counts != NULL);
+ LSTD_ASSERT(codelengths != NULL);
for (int i = 0; i < codelength_count; i++) {
counts[codelengths[i]]++;
@@ -137,8 +137,8 @@ void build_huffman_table_from_codelengths(uint16_t *symbols, uint16_t *counts,
void CodeLengthTable_build_from_codelengths(
CodeLengthTable *table,
uint16_t codelength_codelengths[CODE_LENGTH_ALPHABET_MAX_SYMBOL_COUNT]) {
- ASSERT(table != NULL);
- ASSERT(codelength_codelengths != NULL);
+ LSTD_ASSERT(table != NULL);
+ LSTD_ASSERT(codelength_codelengths != NULL);
static const uint8_t CODELENGTH_MAPPING[] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
uint16_t mapped_codelengths[CODE_LENGTH_ALPHABET_MAX_SYMBOL_COUNT] = {0};
@@ -153,8 +153,8 @@ void CodeLengthTable_build_from_codelengths(
void build_codelength_table(CodeLengthTable *codelength_table,
Bitstream *bitstream, const uint8_t hclen) {
- ASSERT(codelength_table != NULL);
- ASSERT(bitstream != NULL);
+ LSTD_ASSERT(codelength_table != NULL);
+ LSTD_ASSERT(bitstream != NULL);
uint16_t codelengths_codelengths[CODE_LENGTH_ALPHABET_MAX_SYMBOL_COUNT] = {0};
for (int i = 0; i < hclen; i++) {
@@ -169,10 +169,10 @@ bool deflate_decompress_(Bitstream *bitstream,
const LengthLiteralTable *length_literal_table,
const DistanceTable *distance_table,
OutputBuffer *output) {
- ASSERT(bitstream != NULL);
- ASSERT(output != NULL);
- ASSERT(length_literal_table != NULL);
- ASSERT(distance_table != NULL);
+ LSTD_ASSERT(bitstream != NULL);
+ LSTD_ASSERT(output != NULL);
+ LSTD_ASSERT(length_literal_table != NULL);
+ LSTD_ASSERT(distance_table != NULL);
static const uint16_t length_size_base[29] = {
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27,
31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
@@ -192,7 +192,7 @@ bool deflate_decompress_(Bitstream *bitstream,
symbol = huffman_table_decode(length_literal_table->symbols,
length_literal_table->counts, bitstream);
if (symbol < 0) {
- LPNG_LOG_ERR0("Unknown symbol decoded");
+ LOG0_ERROR("Unknown symbol decoded");
return false;
}
if (symbol < 256) {
@@ -226,21 +226,21 @@ bool deflate_decompress_(Bitstream *bitstream,
}
bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
- ASSERT(bitstream != NULL);
- ASSERT(output != NULL);
+ LSTD_ASSERT(bitstream != NULL);
+ LSTD_ASSERT(output != NULL);
uint8_t b_final = 0;
while (!b_final) {
- LPNG_LOG_DBG0("Parse deflate block");
+ LOG0_DEBUG("Parse deflate block");
b_final = Bitstream_next_bits(bitstream, BFINAL_LENGTH_BITS);
- LPNG_LOG_DBG("Final block: %d", b_final);
+ LOG_DEBUG("Final block: %d", b_final);
const uint8_t b_type = Bitstream_next_bits(bitstream, BTYPE_LENGTH_BITS);
if (b_type == DeflateBlockType_NoCompression) {
- LPNG_LOG_ERR0("Uncompressed deflate blocks aren't supported");
+ LOG0_ERROR("Uncompressed deflate blocks aren't supported");
abort();
} else {
if (b_type == DeflateBlockType_FixedHuffman) {
- LPNG_LOG_DBG0("Static huffman table");
+ LOG0_DEBUG("Static huffman table");
static bool are_tables_blank = true;
static LengthLiteralTable length_literal_table = {0};
static DistanceTable distance_table = {0};
@@ -248,7 +248,7 @@ bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
uint16_t lenlit_codelengths[FIXED_LENGTH_LITERAL_CODE_COUNT];
if (are_tables_blank) {
- LPNG_LOG_DBG0("Computing static huffman table");
+ LOG0_DEBUG("Computing static huffman table");
for (int symbol = 0; symbol < 144; symbol++) {
lenlit_codelengths[symbol] = 8;
}
@@ -288,11 +288,11 @@ bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
const uint16_t hlit = Bitstream_next_bits(bitstream, 5) + 257;
const uint8_t hdist = Bitstream_next_bits(bitstream, 5) + 1;
const uint8_t hclen = Bitstream_next_bits(bitstream, 4) + 4;
- LPNG_LOG_DBG("Dynamic table:\n"
- "hlit: %d\n"
- "hdist: %d\n"
- "hclen: %d",
- hlit, hdist, hclen);
+ LOG_DEBUG("Dynamic table:\n"
+ "hlit: %d\n"
+ "hdist: %d\n"
+ "hclen: %d",
+ hlit, hdist, hclen);
CodeLengthTable codelength_table = {0};
build_codelength_table(&codelength_table, bitstream, hclen);
@@ -303,7 +303,7 @@ bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
int symbol = huffman_table_decode(codelength_table.symbols,
codelength_table.counts, bitstream);
if (symbol < 0) {
- LPNG_LOG_ERR0("Unknown symbol decoded using huffman table");
+ LOG0_ERROR("Unknown symbol decoded using huffman table");
return false;
} else if (symbol < 16) {
lenlit_dist_codelengths[index++] = symbol;
@@ -326,7 +326,7 @@ bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
}
// The block must have an end
- ASSERT(lenlit_dist_codelengths[256] > 0);
+ LSTD_ASSERT(lenlit_dist_codelengths[256] > 0);
build_huffman_table_from_codelengths(length_literal_table.symbols,
length_literal_table.counts,
@@ -346,7 +346,7 @@ bool deflate_decompress(Bitstream *bitstream, OutputBuffer *output) {
uint8_t *zlib_decompress(const uint8_t *compressed_data_buffer,
const size_t compressed_data_length,
size_t *output_length) {
- ASSERT(compressed_data_buffer != NULL);
+ LSTD_ASSERT(compressed_data_buffer != NULL);
Bitstream bitstream;
Bitstream_init(&bitstream, compressed_data_buffer, compressed_data_length);
@@ -358,13 +358,13 @@ uint8_t *zlib_decompress(const uint8_t *compressed_data_buffer,
uint16_t flevel = Bitstream_next_bits(&bitstream, FLEVEL_LENGTH_BITS);
#ifdef LPNG_DEBUG_LOG
- LPNG_LOG_DBG("zlib informations:\n"
- "- CM (compression method): %d\n"
- "- CINFO (compression info): %d\n"
- "- FCHECK (check bits for CMF and FLG): %d\n"
- "- FDICT (preset dictionary): %d\n"
- "- FLEVEL (compression level): %d",
- cm, cinfo, fcheck, fdict, flevel);
+ LOG_DEBUG("zlib informations:\n"
+ "- CM (compression method): %d\n"
+ "- CINFO (compression info): %d\n"
+ "- FCHECK (check bits for CMF and FLG): %d\n"
+ "- FDICT (preset dictionary): %d\n"
+ "- FLEVEL (compression level): %d",
+ cm, cinfo, fcheck, fdict, flevel);
#else
(void)cm;
(void)cinfo;
@@ -374,24 +374,24 @@ uint8_t *zlib_decompress(const uint8_t *compressed_data_buffer,
uint16_t cmf = bitstream.data[0];
uint16_t flg = bitstream.data[1];
if ((cmf * 256 + flg) % 31 != 0) {
- LPNG_LOG_ERR0("fcheck validation failed");
+ LOG0_ERROR("fcheck validation failed");
return false;
}
if (flevel > 9) {
- LPNG_LOG_ERR0("Invalid compression level");
+ LOG0_ERROR("Invalid compression level");
return NULL;
}
if (fdict != 0) {
- LPNG_LOG_ERR0("preset dictionnaries are unsupported");
+ LOG0_ERROR("preset dictionnaries are unsupported");
return NULL;
}
OutputBuffer output;
OutputBuffer_init(&output);
if (!deflate_decompress(&bitstream, &output)) {
- LPNG_LOG_ERR0("deflate decompression failed");
+ LOG0_ERROR("deflate decompression failed");
return NULL;
}
@@ -404,7 +404,7 @@ uint8_t *zlib_decompress(const uint8_t *compressed_data_buffer,
(bitstream.data[adler32_offset + 2] << 8) +
(bitstream.data[adler32_offset + 1] << 16) +
(bitstream.data[adler32_offset] << 24);
- LPNG_LOG_DBG("Adler32 checksum: %u", adler32);
+ LOG_DEBUG("Adler32 checksum: %u", adler32);
uint32_t a = 1;
uint32_t b = 0;
for (size_t i = 0; i < output.len; i++) {
@@ -412,9 +412,9 @@ uint8_t *zlib_decompress(const uint8_t *compressed_data_buffer,
b = (b + a) % 65521;
}
uint32_t computed_adler32 = (b << 16) | a;
- LPNG_LOG_DBG("Computed Adler32 checksum: %u", computed_adler32);
+ LOG_DEBUG("Computed Adler32 checksum: %u", computed_adler32);
if (adler32 != computed_adler32) {
- LPNG_LOG_ERR0("Invalid checksum");
+ LOG0_ERROR("Invalid checksum");
exit(1);
}
diff --git a/lisiblepng/src/lisiblepng/log.h b/lisiblepng/src/lisiblepng/log.h
deleted file mode 100644
index 720acd1..0000000
--- a/lisiblepng/src/lisiblepng/log.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef LISIBLE_PNG_LOG_H
-#define LISIBLE_PNG_LOG_H
-
-#include <stdio.h>
-
-#define LPNG_LOG_PREFIX "LPNG: "
-#define LPNG_DEBUG_LOG_PREFIX "%s:%d " LPNG_LOG_PREFIX
-#define LPNG_LOG_ERR0(fmt) fprintf(stderr, LPNG_LOG_PREFIX fmt "\n")
-#define LPNG_LOG_ERR(fmt, ...) \
- fprintf(stderr, LPNG_LOG_PREFIX fmt "\n", __VA_ARGS__)
-
-#ifdef LPNG_DEBUG_LOG
-#define LPNG_LOG_DBG0(fmt) \
- fprintf(stderr, LPNG_DEBUG_LOG_PREFIX fmt "\n", __FILE__, __LINE__)
-#define LPNG_LOG_DBG(fmt, ...) \
- fprintf(stderr, LPNG_DEBUG_LOG_PREFIX fmt "\n", __FILE__, __LINE__, \
- __VA_ARGS__)
-#else
-#define LPNG_LOG_DBG0(fmt)
-#define LPNG_LOG_DBG(fmt, ...)
-#endif // LPNG_DEBUG_LOG
-
-#endif // LISIBLE_PNG_LOG_H
Go back to lisible.xyz