1
0
Fork 0

Start implementation of cairo rasterizer (for testing purposes)

This commit is contained in:
Hans-Joerg Schurr 2020-03-31 22:16:17 +02:00
parent 9dd3cb9f3b
commit 6818012f69
7 changed files with 92 additions and 45 deletions

View File

@ -42,6 +42,9 @@ AC_ARG_ENABLE([lto],
[AS_HELP_STRING([--disable-lto], [disable link time optimization])],
[lto=${enableval}], [lto=yes])
# Libraries
PKG_CHECK_MODULES([CAIRO], [cairo])
AC_MSG_NOTICE([Setting up compiler flags])
CFLAGS+=" -Wall -Wpedantic -Wextra"
@ -69,7 +72,6 @@ fi
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
AS_ECHO("")
AS_ECHO("")
AS_ECHO("Configuration summary")
AS_ECHO("CC = $CC")

View File

@ -15,6 +15,6 @@ pkgs.stdenv.mkDerivation {
name = "hvif-light";
hardeningDisable = [ "all" ];
buildInputs = with pkgs; [ autoconf automake gcc clang-tools gdb valgrind-light ];
buildInputs = with pkgs; [ autoconf automake gcc clang-tools gdb valgrind-light pkg-config cairo ];
}

View File

@ -2,7 +2,11 @@ NULL =
bin_PROGRAMS = hvif
# veriT configuration
# hvif configuration
hvif_CFLAGS = \
$(AM_CFLAGS) \
$(CAIRO_CFLAGS) \
$(NULL)
hvif_CPPFLAGS = \
$(AM_CPPFLAGS) \
-I$(top_builddir)/src \
@ -10,6 +14,7 @@ hvif_CPPFLAGS = \
hvif_LDADD = \
$(AM_LDADD) \
-lm \
$(CAIRO_LIBS) \
$(NULL)
hvif_SOURCES = \
@ -17,5 +22,7 @@ hvif_SOURCES = \
src/main.c \
src/hvif-light.c \
src/hvif-light.h \
src/hvif-cairo.c \
src/hvif-cairo.h \
$(NULL)

24
src/hvif-cairo.c Normal file
View File

@ -0,0 +1,24 @@
#include "hvif-light.h"
#include <stdint.h>
#include <cairo.h>
#define INTERNAL_DATASTRUCTURES
#include "hvif-light.c"
#undef INTERNAL_DATASTRUCTURES
bool
hvif_render_image(const char* filename, hvif_image* image)
{
cairo_surface_t* surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 240, 80);
cairo_t* cr = cairo_create(surface);
cairo_destroy(cr);
bool result = true;
if (cairo_surface_write_to_png(surface, filename) != CAIRO_STATUS_SUCCESS)
result = false;
cairo_surface_destroy(surface);
return result;
}

8
src/hvif-cairo.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef HVIF_CAIRO_H
#define HVIF_CAIRO_H
#include "hvif-ligh.h"
bool hvif_render_image(const char* filename, hvif_image* image);
#endif /* HVIF_CAIRO_H */

View File

@ -1,3 +1,4 @@
#ifndef INTERNAL_DATASTRUCTURES
#include "hvif-light.h"
#include <stdint.h>
@ -17,6 +18,8 @@ debug_print_bytes(FILE* file)
}
#endif
#endif /* INTERNAL_DATASTRUCTURES */
enum
{
STYLE_TYPE_SOLID_COLOR = 1,
@ -94,11 +97,6 @@ typedef enum hvif_line_cap
HVIF_LINE_CAP_TYPE_MAX
} hvif_line_cap;
#define ERROR_RESULT(E) \
(hvif_result) { .status = E }
#define SUCCESS_RESULT(I) \
(hvif_result) { .status = SUCCESS, .image = I }
/* An affine transformation. Ugly until I understand better how they are used */
typedef struct hvif_matrix hvif_matrix;
struct hvif_matrix
@ -128,42 +126,6 @@ struct hvif_point
#define POINT_ORIGIN \
(hvif_point) { 0.0, 0.0 }
#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])
{
return (buffer[0] << 0) | (buffer[1] << 8) | (buffer[2] << 16) |
(buffer[3] << 24);
}
static inline float
hvif_decode_float(char buffer[static 1])
{
int shortValue = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
int sign = (shortValue & 0x800000) >> 23;
int exponent = ((shortValue & 0x7e0000) >> 17) - 32;
int mantissa = (shortValue & 0x01ffff) << 6;
if (shortValue == 0)
return 0.0;
else {
uint32_t value = (sign << 31) | ((exponent + 127) << 23) | mantissa;
return (float)value;
}
}
static inline hvif_matrix
hvif_decode_hvif_matrix(char buffer[static 1])
{
return (hvif_matrix){
hvif_decode_float(buffer), hvif_decode_float(&buffer[3]),
hvif_decode_float(&buffer[6]), hvif_decode_float(&buffer[9]),
hvif_decode_float(&buffer[12]), hvif_decode_float(&buffer[15])
};
}
typedef uint32_t hvif_color; /* 8bit each: alpha, blue, green, red */
#define COLOR_GET_RED(C) (C & 0xff)
#define COLOR_GET_GREEN(C) ((C >> 8) & 0xff)
@ -249,6 +211,43 @@ struct hvif_image
hvif_shape* shapes;
};
#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])
{
return (buffer[0] << 0) | (buffer[1] << 8) | (buffer[2] << 16) |
(buffer[3] << 24);
}
static inline float
hvif_decode_float(char buffer[static 1])
{
int shortValue = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
int sign = (shortValue & 0x800000) >> 23;
int exponent = ((shortValue & 0x7e0000) >> 17) - 32;
int mantissa = (shortValue & 0x01ffff) << 6;
if (shortValue == 0)
return 0.0;
else {
uint32_t value = (sign << 31) | ((exponent + 127) << 23) | mantissa;
return (float)value;
}
}
static inline hvif_matrix
hvif_decode_hvif_matrix(char buffer[static 1])
{
return (hvif_matrix){
hvif_decode_float(buffer), hvif_decode_float(&buffer[3]),
hvif_decode_float(&buffer[6]), hvif_decode_float(&buffer[9]),
hvif_decode_float(&buffer[12]), hvif_decode_float(&buffer[15])
};
}
uint8_t
path_command_at(hvif_path* path, uint8_t index)
{
@ -691,6 +690,11 @@ read_shape(FILE* file, hvif_image const* const image, hvif_shape* shape)
return SUCCESS;
}
#define ERROR_RESULT(E) \
(hvif_result) { .status = E }
#define SUCCESS_RESULT(I) \
(hvif_result) { .status = SUCCESS, .image = I }
hvif_result
hvif_from_file(FILE* file)
{
@ -798,3 +802,5 @@ hvif_free(hvif_image* image)
free(image->shapes);
free(image);
}
#endif /* INTERNAL_DATASTRUCTURES */

View File

@ -27,4 +27,4 @@ struct hvif_result
hvif_result hvif_from_file(FILE* file);
void hvif_free(hvif_image* image);
#endif // HVIF_LIGHT_H
#endif /* HVIF_LIGHT_H */