cucul_import_canvas(). * Renamed cucul_create_export() into cucul_export_canvas() for consistency.tags/v0.99.beta14
@@ -55,7 +55,7 @@ static void raw_display(caca_display_t *dp) | |||||
{ | { | ||||
cucul_buffer_t *buffer; | cucul_buffer_t *buffer; | ||||
buffer = cucul_create_export(dp->cv, "caca"); | |||||
buffer = cucul_export_canvas(dp->cv, "caca"); | |||||
fwrite(cucul_get_buffer_data(buffer), | fwrite(cucul_get_buffer_data(buffer), | ||||
cucul_get_buffer_size(buffer), 1, stdout); | cucul_get_buffer_size(buffer), 1, stdout); | ||||
fflush(stdout); | fflush(stdout); | ||||
@@ -350,10 +350,10 @@ void Cucul::free_font ( Cucul::Font *f) | |||||
cucul_free_font(f->font); | cucul_free_font(f->font); | ||||
} | } | ||||
Cucul::Buffer * Cucul::create_export (char const *buf) | |||||
Cucul::Buffer * Cucul::export_canvas (char const *buf) | |||||
{ | { | ||||
Cucul::Buffer *b = new Cucul::Buffer(); | Cucul::Buffer *b = new Cucul::Buffer(); | ||||
b->buffer = cucul_create_export(cv, buf); | |||||
b->buffer = cucul_export_canvas(cv, buf); | |||||
return b; | return b; | ||||
} | } | ||||
@@ -118,7 +118,7 @@ class Cucul { | |||||
unsigned int get_font_height (Font *); | unsigned int get_font_height (Font *); | ||||
void render_canvas ( Font *, unsigned char *, unsigned int, unsigned int, unsigned int); | void render_canvas ( Font *, unsigned char *, unsigned int, unsigned int, unsigned int); | ||||
void free_font (Font *); | void free_font (Font *); | ||||
Buffer * create_export ( char const *); | |||||
Buffer * export_canvas ( char const *); | |||||
char const *const * get_export_list (void); | char const *const * get_export_list (void); | ||||
@@ -28,6 +28,7 @@ libcucul_la_SOURCES = \ | |||||
font.c \ | font.c \ | ||||
font_mono9.h \ | font_mono9.h \ | ||||
font_monobold12.h \ | font_monobold12.h \ | ||||
import.c \ | |||||
export.c \ | export.c \ | ||||
$(NULL) | $(NULL) | ||||
libcucul_la_LDFLAGS = -no-undefined | libcucul_la_LDFLAGS = -no-undefined | ||||
@@ -72,61 +72,6 @@ cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height) | |||||
return cv; | return cv; | ||||
} | } | ||||
/** \brief Load a memory area into a canvas. | |||||
* | |||||
* This function loads a memory area containing an exported canvas into | |||||
* a new \e libcucul canvas. | |||||
* | |||||
* \param data The memory area to be loaded into a canvas. | |||||
* \param size The length of the memory area. | |||||
* \return A libcucul canvas, or NULL in case of error. | |||||
*/ | |||||
cucul_canvas_t *cucul_load_canvas(void *data, unsigned int size) | |||||
{ | |||||
cucul_canvas_t *cv; | |||||
uint8_t *buf = (uint8_t *)data; | |||||
unsigned int width, height, n; | |||||
if(size < 16) | |||||
return NULL; | |||||
if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A') | |||||
return NULL; | |||||
if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V') | |||||
return NULL; | |||||
width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) | |||||
| ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; | |||||
height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16) | |||||
| ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; | |||||
if(!width || !height) | |||||
return NULL; | |||||
if(size != 16 + width * height * 8) | |||||
return NULL; | |||||
cv = cucul_create_canvas(width, height); | |||||
if(!cv) | |||||
return NULL; | |||||
for(n = height * width; n--; ) | |||||
{ | |||||
cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) | |||||
| ((uint32_t)buf[16 + 1 + 8 * n] << 16) | |||||
| ((uint32_t)buf[16 + 2 + 8 * n] << 8) | |||||
| (uint32_t)buf[16 + 3 + 8 * n]; | |||||
cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) | |||||
| ((uint32_t)buf[16 + 5 + 8 * n] << 16) | |||||
| ((uint32_t)buf[16 + 6 + 8 * n] << 8) | |||||
| (uint32_t)buf[16 + 7 + 8 * n]; | |||||
} | |||||
return cv; | |||||
} | |||||
/** \brief Resize a canvas. | /** \brief Resize a canvas. | ||||
* | * | ||||
* This function sets the canvas width and height, in character cells. | * This function sets the canvas width and height, in character cells. | ||||
@@ -74,7 +74,6 @@ typedef struct cucul_font cucul_font_t; | |||||
* | * | ||||
* @{ */ | * @{ */ | ||||
cucul_canvas_t * cucul_create_canvas(unsigned int, unsigned int); | cucul_canvas_t * cucul_create_canvas(unsigned int, unsigned int); | ||||
cucul_canvas_t * cucul_load_canvas(void *, unsigned int); | |||||
void cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int); | void cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int); | ||||
unsigned int cucul_get_canvas_width(cucul_canvas_t *); | unsigned int cucul_get_canvas_width(cucul_canvas_t *); | ||||
unsigned int cucul_get_canvas_height(cucul_canvas_t *); | unsigned int cucul_get_canvas_height(cucul_canvas_t *); | ||||
@@ -205,14 +204,16 @@ void cucul_render_canvas(cucul_canvas_t *, cucul_font_t *, void *, | |||||
void cucul_free_font(cucul_font_t *); | void cucul_free_font(cucul_font_t *); | ||||
/* @} */ | /* @} */ | ||||
/** \defgroup exporter libcucul exporters to various formats | |||||
/** \defgroup importexport libcucul importers/exporters from/to various formats | |||||
* | * | ||||
* These functions export the current canvas to various text formats. It | |||||
* is necessary to call cucul_free_buffer() to dispose of the data. | |||||
* These functions import various file formats into a new canvas, or export | |||||
* the current canvas to various text formats. | |||||
* | * | ||||
* @{ */ | * @{ */ | ||||
cucul_buffer_t * cucul_create_export(cucul_canvas_t *, char const *); | |||||
cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *); | |||||
char const * const * cucul_get_export_list(void); | char const * const * cucul_get_export_list(void); | ||||
cucul_canvas_t * cucul_import_canvas(void const *, unsigned int, char const *); | |||||
char const * const * cucul_get_import_list(void); | |||||
/* @} */ | /* @} */ | ||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
@@ -65,7 +65,7 @@ static void export_tga(cucul_canvas_t *, cucul_buffer_t *); | |||||
* \param cv A libcucul canvas | * \param cv A libcucul canvas | ||||
* \param format A string describing the requested output format. | * \param format A string describing the requested output format. | ||||
*/ | */ | ||||
cucul_buffer_t * cucul_create_export(cucul_canvas_t *cv, char const *format) | |||||
cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format) | |||||
{ | { | ||||
cucul_buffer_t *ex; | cucul_buffer_t *ex; | ||||
@@ -103,7 +103,7 @@ cucul_buffer_t * cucul_create_export(cucul_canvas_t *cv, char const *format) | |||||
* | * | ||||
* Return a list of available export formats. The list is a NULL-terminated | * Return a list of available export formats. The list is a NULL-terminated | ||||
* array of strings, interleaving a string containing the internal value for | * array of strings, interleaving a string containing the internal value for | ||||
* the export format, to be used with cucul_create_export(), and a string | |||||
* the export format, to be used with cucul_export_canvas(), and a string | |||||
* containing the natural language description for that export format. | * containing the natural language description for that export format. | ||||
* | * | ||||
* \return An array of strings. | * \return An array of strings. | ||||
@@ -0,0 +1,127 @@ | |||||
/* | |||||
* libcucul Canvas for ultrafast compositing of Unicode letters | |||||
* Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> | |||||
* All Rights Reserved | |||||
* | |||||
* $Id$ | |||||
* | |||||
* This library is free software; you can redistribute it and/or | |||||
* modify it under the terms of the Do What The Fuck You Want To | |||||
* Public License, Version 2, as published by Sam Hocevar. See | |||||
* http://sam.zoy.org/wtfpl/COPYING for more details. | |||||
*/ | |||||
/* | |||||
* This file contains various import functions. | |||||
*/ | |||||
#include "config.h" | |||||
#if !defined(__KERNEL__) | |||||
# include <stdio.h> | |||||
# include <stdlib.h> | |||||
# include <string.h> | |||||
#endif | |||||
#include "cucul.h" | |||||
#include "cucul_internals.h" | |||||
static cucul_canvas_t *import_caca(void const *, unsigned int); | |||||
/** \brief Import a buffer into a canvas | |||||
* | |||||
* This function imports a memory area into an internal libcucul canvas. | |||||
* | |||||
* Valid values for \c format are: | |||||
* | |||||
* \li \c "caca": import native libcaca files. | |||||
* | |||||
* \param data The memory area to be loaded into a canvas. | |||||
* \param size The length of the memory area. | |||||
* \param format A string describing the input format. | |||||
* \return A libcucul canvas, or NULL in case of error. | |||||
*/ | |||||
cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, | |||||
char const *format) | |||||
{ | |||||
if(!strcasecmp("caca", format)) | |||||
return import_caca(data, size); | |||||
/* FIXME: Try to autodetect */ | |||||
if(!strcasecmp("", format)) | |||||
return import_caca(data, size); | |||||
return NULL; | |||||
} | |||||
/** \brief Get available import formats | |||||
* | |||||
* Return a list of available import formats. The list is a NULL-terminated | |||||
* array of strings, interleaving a string containing the internal value for | |||||
* the import format, to be used with cucul_import_canvas(), and a string | |||||
* containing the natural language description for that import format. | |||||
* | |||||
* \return An array of strings. | |||||
*/ | |||||
char const * const * cucul_get_import_list(void) | |||||
{ | |||||
static char const * const list[] = | |||||
{ | |||||
"", "autodetect", | |||||
"caca", "native libcaca format", | |||||
NULL, NULL | |||||
}; | |||||
return list; | |||||
} | |||||
/* | |||||
* XXX: the following functions are local. | |||||
*/ | |||||
static cucul_canvas_t *import_caca(void const *data, unsigned int size) | |||||
{ | |||||
cucul_canvas_t *cv; | |||||
uint8_t *buf = (uint8_t *)data; | |||||
unsigned int width, height, n; | |||||
if(size < 16) | |||||
return NULL; | |||||
if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A') | |||||
return NULL; | |||||
if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V') | |||||
return NULL; | |||||
width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) | |||||
| ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; | |||||
height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16) | |||||
| ((uint32_t)buf[14] << 8) | (uint32_t)buf[15]; | |||||
if(!width || !height) | |||||
return NULL; | |||||
if(size != 16 + width * height * 8) | |||||
return NULL; | |||||
cv = cucul_create_canvas(width, height); | |||||
if(!cv) | |||||
return NULL; | |||||
for(n = height * width; n--; ) | |||||
{ | |||||
cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24) | |||||
| ((uint32_t)buf[16 + 1 + 8 * n] << 16) | |||||
| ((uint32_t)buf[16 + 2 + 8 * n] << 8) | |||||
| (uint32_t)buf[16 + 3 + 8 * n]; | |||||
cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24) | |||||
| ((uint32_t)buf[16 + 5 + 8 * n] << 16) | |||||
| ((uint32_t)buf[16 + 6 + 8 * n] << 8) | |||||
| (uint32_t)buf[16 + 7 + 8 * n]; | |||||
} | |||||
return cv; | |||||
} | |||||
@@ -53,7 +53,7 @@ int main(int argc, char **argv) | |||||
buffer = malloc(statbuf.st_size); | buffer = malloc(statbuf.st_size); | ||||
read(fd, buffer, statbuf.st_size); | read(fd, buffer, statbuf.st_size); | ||||
cv = cucul_load_canvas(buffer, statbuf.st_size); | |||||
cv = cucul_import_canvas(buffer, statbuf.st_size, "caca"); | |||||
free(buffer); | free(buffer); | ||||
if(!cv) | if(!cv) | ||||
@@ -226,7 +226,7 @@ int main(void) | |||||
if(server->canvas) | if(server->canvas) | ||||
cucul_free_canvas(server->canvas); | cucul_free_canvas(server->canvas); | ||||
server->canvas = cucul_load_canvas(buf, size); | |||||
server->canvas = cucul_import_canvas(buf, size, "caca"); | |||||
if(!server->canvas) | if(!server->canvas) | ||||
continue; /* Load error */ | continue; /* Load error */ | ||||
@@ -240,7 +240,7 @@ int main(void) | |||||
/* Get ANSI representation of the image and skip the end-of buffer | /* Get ANSI representation of the image and skip the end-of buffer | ||||
* linefeed ("\r\n", 2 bytes) */ | * linefeed ("\r\n", 2 bytes) */ | ||||
server->buffer = cucul_create_export(server->canvas, "ansi"); | |||||
server->buffer = cucul_export_canvas(server->canvas, "ansi"); | |||||
server->bufdata = cucul_get_buffer_data(server->buffer); | server->bufdata = cucul_get_buffer_data(server->buffer); | ||||
server->buflen = cucul_get_buffer_size(server->buffer); | server->buflen = cucul_get_buffer_size(server->buffer); | ||||
server->buflen -= 2; | server->buflen -= 2; | ||||
@@ -58,7 +58,7 @@ int main(int argc, char **argv) | |||||
unload_image(i); | unload_image(i); | ||||
export = cucul_create_export(cv, "irc"); | |||||
export = cucul_export_canvas(cv, "irc"); | |||||
fwrite(cucul_get_buffer_data(export), | fwrite(cucul_get_buffer_data(export), | ||||
cucul_get_buffer_size(export), 1, stdout); | cucul_get_buffer_size(export), 1, stdout); | ||||
cucul_free_buffer(export); | cucul_free_buffer(export); | ||||
@@ -101,7 +101,7 @@ int main(int argc, char *argv[]) | |||||
cucul_putstr(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 5, "#"); | cucul_putstr(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 5, "#"); | ||||
} | } | ||||
buffer = cucul_create_export(cv, argv[1]); | |||||
buffer = cucul_export_canvas(cv, argv[1]); | |||||
fwrite(cucul_get_buffer_data(buffer), | fwrite(cucul_get_buffer_data(buffer), | ||||
cucul_get_buffer_size(buffer), 1, stdout); | cucul_get_buffer_size(buffer), 1, stdout); | ||||
cucul_free_buffer(buffer); | cucul_free_buffer(buffer); | ||||