1
0
Fork 0

Add debug prints

This commit is contained in:
Hans-Joerg Schurr 2020-03-22 17:14:23 +01:00
parent fd0148ec21
commit 4ed81b2aa1
1 changed files with 44 additions and 1 deletions

View File

@ -3,6 +3,20 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef DEBUG
void
debug_print_bytes(FILE* file)
{
long p = ftell(file);
uint8_t buffer[5];
unsigned l = fread(buffer, 1, 5, file);
printf("...");
for (unsigned i = 0; i < l; ++i) printf(" %02x", buffer[i]);
printf("\n");
fseek(file, p, SEEK_SET);
}
#endif
enum
{
STYLE_TYPE_SOLID_COLOR = 1,
@ -209,6 +223,9 @@ decode_color(char* buffer, bool gray, bool alpha)
hvif_status
read_color_style(hvif_style* style, char* buffer, bool gray, bool alpha)
{
#ifdef DEBUG
printf("%s: start.\n", __func__);
#endif
hvif_color color = decode_color(buffer, gray, alpha);
style->num_stops = 1;
style->colors = calloc(1, sizeof(hvif_color));
@ -223,6 +240,9 @@ read_color_style(hvif_style* style, char* buffer, bool gray, bool alpha)
hvif_status
read_gradient_style(hvif_style* style, FILE* file, char* style_buffer)
{
#ifdef DEBUG
printf("%s: start.\n", __func__);
#endif
uint8_t gradient_type = style_buffer[0];
uint8_t gradient_flags = style_buffer[1];
uint8_t gradient_stops = style_buffer[2];
@ -239,6 +259,9 @@ read_gradient_style(hvif_style* style, FILE* file, char* style_buffer)
hvif_matrix transformation;
if (gradient_flags & GRADIENT_FLAG_TRANSFORM) {
#ifdef DEBUG
printf("%s: read transfomation matrix.\n", __func__);
#endif
/* The transformation matrix is 6 hvif-floats: 18 bytes */
char buffer[18];
if (fread(buffer, 1, 18, file) != 18) { return ERROR_EOF; }
@ -254,6 +277,9 @@ read_gradient_style(hvif_style* style, FILE* file, char* style_buffer)
if (!style->offsets || !style->colors) return ERROR_NOMEM;
#ifdef DEBUG
printf("%s: read %u stops.\n", __func__, gradient_stops);
#endif
char buffer[4];
for (unsigned i = 0; i < gradient_stops; ++i) {
if (fread(buffer, 1, 1, file) != 1) { return ERROR_EOF; }
@ -310,7 +336,11 @@ read_style(FILE* file, hvif_style* style)
if (fread(buffer, 1, 1, file) != 1) { return ERROR_EOF; }
read_color_style(style, buffer, true, false);
break;
default: return ERROR_STYLE;
default:
#ifdef DEBUG
printf("%s: error unkown style type %u.\n", __func__, type);
#endif
return ERROR_STYLE;
}
return SUCCESS;
}
@ -438,6 +468,9 @@ hvif_from_file(FILE* file)
{
uint32_t const magic = 0x6669636e; /* 'finc' (on disk little endian 'cnif') */
#ifdef DEBUG
printf("%s: checking magic number.\n", __func__);
#endif
char magic_buffer[4];
if (fread(magic_buffer, 1, 4, file) != 4) { return ERROR_RESULT(ERROR_EOF); }
uint32_t read_magic;
@ -451,6 +484,9 @@ hvif_from_file(FILE* file)
hvif_free(image);
return ERROR_RESULT(ERROR_EOF);
}
#ifdef DEBUG
printf("%s: reading %u styles.\n", __func__, image->num_styles);
#endif
image->styles = calloc(image->num_styles, sizeof(hvif_style));
if (!image->styles) {
hvif_free(image);
@ -468,6 +504,9 @@ hvif_from_file(FILE* file)
hvif_free(image);
return ERROR_RESULT(ERROR_EOF);
}
#ifdef DEBUG
printf("%s: reading %u paths.\n", __func__, image->num_styles);
#endif
image->paths = calloc(image->num_paths, sizeof(hvif_path));
if (!image->paths) {
hvif_free(image);
@ -480,6 +519,10 @@ hvif_from_file(FILE* file)
return ERROR_RESULT(s);
}
}
#ifdef DEBUG
printf("%s: incomplete parsing stopped.\n", __func__);
debug_print_bytes(file);
#endif
return SUCCESS_RESULT(image);
}