1
0

Fix parsing of unsigned values, transformation matrix, export commands

- Parse unsigned values using uint8_t instead of signed char
- Correctly parse floats and give transformation matrix entries good names
- Fix default matrix (for translation, identity)
- Export `path_command_at` in INTERNAL_DATSTRUCTURES mode
This commit is contained in:
Hans-Joerg Schurr 2020-04-23 00:40:33 +02:00
parent a5c305d98c
commit 69229b956f

View File

@ -101,20 +101,18 @@ typedef enum hvif_line_cap
typedef struct hvif_matrix hvif_matrix;
struct hvif_matrix
{
double v1;
double v2;
double v3;
double v4;
double v5;
double v6;
double xx;
double yx;
double xy;
double yy;
double x0;
double y0;
};
/* TODO: This is most likely wrong */
#define MATRIX_ID \
(hvif_matrix) { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 }
/* TODO: This is most likely wrong */
(hvif_matrix) { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }
#define MATRIX_TRANSLATION(P) \
(hvif_matrix) { P.x, 0.0, 0.0, 0.0, P.y, 0.0 }
(hvif_matrix) { 1.0, 0.0, 0.0, 1.0, P.x, P.y }
typedef struct hvif_point hvif_point;
struct hvif_point
@ -211,20 +209,29 @@ struct hvif_image
hvif_shape* shapes;
};
inline uint8_t
path_command_at(hvif_path* path, uint8_t index)
{
uint8_t byte = path->commands[index / 4];
uint8_t byte_idx = index % 4;
return (byte >> (2 * byte_idx)) & 0x03;
}
#ifndef INTERNAL_DATASTRUCTURES
#define hvif_decode(V, BUFFER) \
V = _Generic((V), uint32_t \
: hvif_decode_uint32, float \
: hvif_decode_float, hvif_matrix \
: hvif_decode_hvif_matrix)(BUFFER)
static inline uint32_t
hvif_decode_uint32(char buffer[static 1])
hvif_decode_uint32(uint8_t buffer[static 1])
{
return (buffer[0] << 0) | (buffer[1] << 8) | (buffer[2] << 16) |
(buffer[3] << 24);
}
static inline float
hvif_decode_float(char buffer[static 1])
hvif_decode_float(uint8_t buffer[static 1])
{
int shortValue = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
int sign = (shortValue & 0x800000) >> 23;
@ -234,12 +241,13 @@ hvif_decode_float(char buffer[static 1])
if (shortValue == 0)
return 0.0;
else {
/* TODO: float might not be IEEE 754 compatible */
uint32_t value = (sign << 31) | ((exponent + 127) << 23) | mantissa;
return (float)value;
return *((float*)&value);
}
}
static inline hvif_matrix
hvif_decode_hvif_matrix(char buffer[static 1])
hvif_decode_hvif_matrix(uint8_t buffer[static 1])
{
return (hvif_matrix){
hvif_decode_float(buffer), hvif_decode_float(&buffer[3]),
@ -248,16 +256,8 @@ hvif_decode_hvif_matrix(char buffer[static 1])
};
}
uint8_t
path_command_at(hvif_path* path, uint8_t index)
{
uint8_t byte = path->commands[index / 4];
uint8_t byte_idx = index % 4;
return (byte >> (2 * byte_idx)) & 0x03;
}
hvif_color
decode_color(char* buffer, bool gray, bool alpha)
decode_color(uint8_t* buffer, bool gray, bool alpha)
{
/* On disk a color is:
red, green, blue, alpha
@ -286,7 +286,7 @@ decode_color(char* buffer, bool gray, bool alpha)
}
hvif_status
read_color_style(hvif_style* style, char* buffer, bool gray, bool alpha)
read_color_style(hvif_style* style, uint8_t* buffer, bool gray, bool alpha)
{
#ifdef DEBUG
printf("%s: start.\n", __func__);
@ -303,7 +303,7 @@ 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)
read_gradient_style(hvif_style* style, FILE* file, uint8_t* style_buffer)
{
#ifdef DEBUG
printf("%s: start.\n", __func__);
@ -328,7 +328,7 @@ read_gradient_style(hvif_style* style, FILE* file, char* style_buffer)
printf("%s: read transfomation matrix.\n", __func__);
#endif
/* The transformation matrix is 6 hvif-floats: 18 bytes */
char buffer[18];
uint8_t buffer[18];
if (fread(buffer, 1, 18, file) != 18) { return ERROR_EOF; }
hvif_decode(transformation, buffer);
} else {
@ -345,7 +345,7 @@ read_gradient_style(hvif_style* style, FILE* file, char* style_buffer)
#ifdef DEBUG
printf("%s: read %u stops.\n", __func__, gradient_stops);
#endif
char buffer[4];
uint8_t buffer[4];
for (unsigned i = 0; i < gradient_stops; ++i) {
if (fread(buffer, 1, 1, file) != 1) { return ERROR_EOF; }
float offset = buffer[0] / 255.0;
@ -379,7 +379,7 @@ read_style(FILE* file, hvif_style* style)
{
uint8_t type;
if (fread(&type, 1, 1, file) != 1) { return ERROR_EOF; }
char buffer[4];
uint8_t buffer[4];
switch (type) {
case STYLE_TYPE_SOLID_COLOR:
if (fread(buffer, 1, 4, file) != 4) { return ERROR_EOF; }
@ -565,7 +565,7 @@ read_transformer(FILE* file, hvif_transformer* transformer)
switch (byte) {
case TRANSFORMER_TYPE_AFFINE:
transformer->transformer_type = byte;
char buffer[18];
uint8_t buffer[18];
hvif_matrix matrix;
if (fread(buffer, 1, 18, file) != 18) return ERROR_EOF;
hvif_decode(matrix, buffer);
@ -645,7 +645,7 @@ read_shape(FILE* file, hvif_image const* const image, hvif_shape* shape)
hvif_matrix transformation;
if (shape_flags & SHAPE_FLAG_TRANSFORM) {
char buffer[18];
uint8_t buffer[18];
if (fread(buffer, 1, 18, file) != 18) { return ERROR_EOF; }
hvif_decode(transformation, buffer);
} else if (shape_flags & SHAPE_FLAG_TRANSLATION) {
@ -700,7 +700,7 @@ hvif_from_file(FILE* file)
#ifdef DEBUG
printf("%s: checking magic number.\n", __func__);
#endif
char magic_buffer[4];
uint8_t magic_buffer[4];
if (fread(magic_buffer, 1, 4, file) != 4) return ERROR_RESULT(ERROR_EOF);
uint32_t read_magic;
hvif_decode(read_magic, magic_buffer);