create_png_mmap: accept a file descriptor

pull/1940/head
nick black 3 years ago committed by nick black
parent a55a37a838
commit 3dbc5645f7

@ -120,21 +120,28 @@ mmap_round_size(size_t s){
// write a PNG, creating the buffer ourselves. it must be munmapped. the
// resulting length is written to *bsize on success. returns MMAP_FAILED
// on a failure. if |fname| is NULL, an anonymous map will be made.
void* create_png_mmap(const ncvisual* ncv, size_t* bsize, const char* fname){
// on a failure. if |fd| is negative, an anonymous map will be made.
void* create_png_mmap(const ncvisual* ncv, size_t* bsize, int fd){
const unsigned alphap = 1; // FIXME 0 if no alpha used, for smaller output
*bsize = compute_png_size(ncv, alphap);
*bsize = mmap_round_size(*bsize);
if(*bsize == 0){
return MAP_FAILED;
}
// FIXME open and ftruncate file if fname is set
if(fd >= 0){
if(ftruncate(fd, *bsize) < 0){
logerror("Couldn't set size of %d to %zuB (%s)\n",
fd, *bsize, strerror(errno));
return MAP_FAILED;
}
loginfo("Set size of %d to %zuB\n", fd, *bsize);
}
// FIXME hugetlb?
void* map = mmap(NULL, *bsize, PROT_WRITE | PROT_READ,
MAP_SHARED_VALIDATE |
(fname ? 0 : MAP_ANONYMOUS), -1, 0);
(fd >= 0 ? 0 : MAP_ANONYMOUS), fd, 0);
if(map == MAP_FAILED){
logerror("Couldn't get %zuB map\n", *bsize);
logerror("Couldn't get %zuB map for %d\n", *bsize, fd);
return MAP_FAILED;
}
if(create_png(ncv, map, bsize, alphap)){

@ -0,0 +1,18 @@
#ifndef NOTCURSES_PNG
#define NOTCURSES_PNG
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/mman.h>
struct ncvisual;
void* create_png_mmap(const struct ncvisual* ncv, size_t* bsize, int fd);
#ifdef __cplusplus
}
#endif
#endif

@ -1,30 +0,0 @@
#include "main.h"
#include "png.h"
#include "visual-details.h"
#include <vector>
#include <cmath>
TEST_CASE("PNG") {
auto nc_ = testing_notcurses();
REQUIRE(nullptr != nc_);
ncplane* ncp_ = notcurses_stdplane(nc_);
REQUIRE(ncp_);
auto n_ = notcurses_stdplane(nc_);
REQUIRE(n_);
#ifndef NOTCURSES_USE_MULTIMEDIA
#else
// write a 10x10 opaque PNG out, and ensure we can read it back
SUBCASE("ReadWrittenOpaquePNG") {
std::array<uint32_t, 100> pixels;
pixels.fill(htole(0x00ff0000ull)); // green, opaque set later
auto ncv = ncvisual_from_rgb_loose(pixels.data(), 10, 40, 10, 0xff);
REQUIRE(nullptr != ncv);
// FIXME write out ncvisual to PNG, read it back, render it
CHECK(0 == notcurses_render(nc_));
ncvisual_destroy(ncv);
}
#endif
CHECK(!notcurses_stop(nc_));
}
Loading…
Cancel
Save