start on libncsixel #2383

This commit is contained in:
nick black 2021-12-13 00:53:31 -05:00 committed by nick black
parent 2c956e60d2
commit 3190419774
3 changed files with 119 additions and 2 deletions

View File

@ -213,7 +213,7 @@ file(GLOB COMPATSRC CONFIGURE_DEPENDS src/compat/*.c)
############################################################################ ############################################################################
# libnotcurses-core (core shared library, core static library) # libnotcurses-core (core shared library, core static library)
file(GLOB NCCORESRCS CONFIGURE_DEPENDS src/lib/*.c src/lib/*.cpp) file(GLOB NCCORESRCS CONFIGURE_DEPENDS src/lib/*.c)
add_library(notcurses-core SHARED ${NCCORESRCS} ${COMPATSRC}) add_library(notcurses-core SHARED ${NCCORESRCS} ${COMPATSRC})
if(${USE_STATIC}) if(${USE_STATIC})
add_library(notcurses-core-static STATIC ${NCCORESRCS} ${COMPATSRC}) add_library(notcurses-core-static STATIC ${NCCORESRCS} ${COMPATSRC})
@ -408,6 +408,57 @@ target_link_directories(notcurses PRIVATE ${OIIO_LIBRARY_DIRS})
target_link_directories(notcurses-static PRIVATE ${OIIO_STATIC_LIBRARY_DIRS}) target_link_directories(notcurses-static PRIVATE ${OIIO_STATIC_LIBRARY_DIRS})
endif() endif()
############################################################################
# libncsixel (sixel library built atop notcurses)
file(GLOB NCSIXSRCS CONFIGURE_DEPENDS src/sixel/*.c)
add_library(ncsixel SHARED ${NCSIXSRCS} ${COMPATSRC})
if(${USE_STATIC})
add_library(ncsixel-static STATIC ${NCSIXSRCS})
else()
add_library(ncsixel-static STATIC EXCLUDE_FROM_ALL ${NCSIXSRCS})
endif()
set_target_properties(ncsixel PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
set_target_properties(ncsixel-static PROPERTIES
VERSION ${PROJECT_VERSION}
OUTPUT_NAME ncsixel
)
target_include_directories(ncsixel
BEFORE
PRIVATE
include
src
"${CMAKE_REQUIRED_INCLUDES}"
"${PROJECT_BINARY_DIR}/include"
"${TERMINFO_INCLUDE_DIRS}"
)
target_include_directories(ncsixel-static
BEFORE
PRIVATE
include
src
"${CMAKE_REQUIRED_INCLUDES}"
"${PROJECT_BINARY_DIR}/include"
"${TERMINFO_INCLUDE_DIRS}"
)
target_compile_definitions(ncsixel
PRIVATE
_GNU_SOURCE _DEFAULT_SOURCE
)
target_compile_definitions(ncsixel-static
PRIVATE
_GNU_SOURCE _DEFAULT_SOURCE
)
target_link_libraries(ncsixel
PUBLIC
notcurses
)
target_link_libraries(ncsixel-static
PUBLIC
notcurses-static
)
############################################################################ ############################################################################
if(${USE_CXX}) if(${USE_CXX})
# libnotcurses++ (C++ wrappers) # libnotcurses++ (C++ wrappers)

View File

@ -1,6 +1,9 @@
#ifndef NOTCURSES_SIXEL_SIXEL #ifndef NOTCURSES_SIXEL_SIXEL
#define NOTCURSES_SIXEL_SIXEL #define NOTCURSES_SIXEL_SIXEL
// a library built atop Notcurses suitable for working with sixels outside
// the Notcurses framework--a replacement for libsixel.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#define RESTRICT #define RESTRICT
@ -15,7 +18,19 @@ extern "C" {
#endif #endif
#define ALLOC __attribute__((malloc)) __attribute__((warn_unused_result)) #define ALLOC __attribute__((malloc)) __attribute__((warn_unused_result))
// FIXME libsixel-like API struct sixel;
struct sixelctx;
API ALLOC struct sixelctx* libncsixel_init(void);
// load the file and encode the first frame as a sixel. if |colorregs| is 0,
// use the number supported by the terminal, or 256 if the terminal does not
// support sixel.
API ALLOC __attribute__ ((nonnull (1, 2)))
struct sixel* libncsixel_encode(struct sixelctx* sctx, const char* file,
unsigned colorregs);
API void libncsixel_stop(struct sixelctx* sctx);
#undef API #undef API
#undef ALLOC #undef ALLOC

51
src/sixel/sixel.c Normal file
View File

@ -0,0 +1,51 @@
#include "sixel/sixel.h"
#include <notcurses/notcurses.h>
// represents a sixel generated from some image
typedef struct sixel {
char* escape; // nul-terminated escape suitable for writing to the terminal
// there is both the true pixel geometry, and the sprixel-padded pixel
// geometry--the latter always has a height which is a multiple of six.
unsigned pixy, pixx; // original pixel geometry
unsigned sprixpixy, sprixpixx; // sprixel-padded pixel geometry
// we might only occupy a portion of the final column and row of cells.
unsigned celly, cellx; // cell geometry
unsigned colorregs_avail; // color registers available
unsigned colorregs_used; // color registers used
} sixel;
typedef struct sixelctx {
struct notcurses* nc;
} sixelctx;
sixelctx* libncsixel_init(void){
sixelctx* sctx = malloc(sizeof(*sctx));
if(sctx == NULL){
return NULL;
}
struct notcurses_options nopts = {
.flags = NCOPTION_NO_ALTERNATE_SCREEN |
NCOPTION_PRESERVE_CURSOR |
NCOPTION_SUPPRESS_BANNERS |
NCOPTION_DRAIN_INPUT,
};
if((sctx->nc = notcurses_init(&nopts, NULL)) == NULL){
free(sctx);
return NULL;
}
return sctx;
}
sixel* libncsixel_encode(sixelctx* sctx, const char* file, unsigned colorregs){
(void)sctx;
(void)file;
(void)colorregs;
return NULL;
}
void libncsixel_stop(sixelctx* sctx){
if(sctx){
notcurses_stop(sctx->nc);
free(sctx);
}
}