diff --git a/src/lib/png.c b/src/lib/png.c index 7521d0965..3d167cd40 100644 --- a/src/lib/png.c +++ b/src/lib/png.c @@ -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)){ diff --git a/src/lib/png.h b/src/lib/png.h new file mode 100644 index 000000000..e7217d67d --- /dev/null +++ b/src/lib/png.h @@ -0,0 +1,18 @@ +#ifndef NOTCURSES_PNG +#define NOTCURSES_PNG + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct ncvisual; + +void* create_png_mmap(const struct ncvisual* ncv, size_t* bsize, int fd); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/tests/png.cpp b/src/tests/png.cpp deleted file mode 100644 index a1c313d4d..000000000 --- a/src/tests/png.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "main.h" -#include "png.h" -#include "visual-details.h" -#include -#include - -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 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_)); -}