diff --git a/cucul/cucul.c b/cucul/cucul.c index 9aa9deb..5285e8d 100644 --- a/cucul/cucul.c +++ b/cucul/cucul.c @@ -77,7 +77,47 @@ cucul_t * cucul_create(unsigned int width, unsigned int height) return qq; } -/** \brief Set the canvas size. +cucul_t *cucul_load(void *data, unsigned int size) +{ + cucul_t *qq; + uint8_t *buf = (uint8_t *)data; + unsigned int width, height, n; + + if(size < 12) + return NULL; + + if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A') + return NULL; + + width = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16) + | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7]; + height = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) + | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; + + if(!width || !height) + return NULL; + + if(size != 12 + width * height * 5 + 4) + return NULL; + + qq = cucul_create(width, height); + + if(!qq) + return NULL; + + for(n = height * width; n--; ) + { + qq->chars[n] = ((uint32_t)buf[12 + 5 * n] << 24) + | ((uint32_t)buf[13 + 5 * n] << 16) + | ((uint32_t)buf[14 + 5 * n] << 8) + | (uint32_t)buf[15 + 5 * n]; + qq->attr[n] = buf[16 + 5 * n]; + } + + return qq; +} + +/** \brief Resize a canvas. * * This function sets the canvas width and height, in character cells. * diff --git a/cucul/cucul.h b/cucul/cucul.h index 6f93dce..943b371 100644 --- a/cucul/cucul.h +++ b/cucul/cucul.h @@ -119,6 +119,7 @@ typedef struct cucul_context cucul_t; * * @{ */ cucul_t * cucul_create(unsigned int, unsigned int); +cucul_t * cucul_load(void *, unsigned int); void cucul_set_size(cucul_t *, unsigned int, unsigned int); unsigned int cucul_get_width(cucul_t *); unsigned int cucul_get_height(cucul_t *);