From 9c5a90bd4eb69233d06394c628c183a06ed80f7c Mon Sep 17 00:00:00 2001 From: Paul Fitzpatrick Date: Sat, 23 Feb 2019 17:28:37 -0500 Subject: [PATCH] allow loading geometry from json; sample slightly more cleanly --- CMakeLists.txt | 3 +++ Dockerfile | 4 ++++ src/CMakeLists.txt | 5 ++++- src/Pixer.h | 50 +++++++++++++++++++++++++++++++++++++++++++++- src/Render.cpp | 22 +++++++++++++++----- src/reanimator.cpp | 37 +++++++++++++++++++++++++++++++--- 6 files changed, 111 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index de5d939..978b83b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,4 +11,7 @@ FIND_PACKAGE(YARP) INCLUDE_DIRECTORIES(${YARP_INCLUDE_DIRS}) LINK_LIBRARIES(${YARP_LIBRARIES}) +find_package(PkgConfig REQUIRED) +pkg_check_modules(JSONCPP jsoncpp) + ADD_SUBDIRECTORY(src) diff --git a/Dockerfile b/Dockerfile index ac2f22a..4bb8ded 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,6 +29,10 @@ RUN \ apt-get update; \ apt-get install -y libopencv-videoio-dev +RUN \ + apt-get update; \ + apt-get install -y libjsoncpp-dev + COPY . /makesweet/ RUN \ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 981751a..f33905d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,8 @@ ELSE() SET(PROTO_HDRS) ENDIF() -ADD_LIBRARY(msmap msmap.h msmap.c) +ADD_LIBRARY(msmap STATIC msmap.h msmap.c) +include_directories(${JSONCPP_INCLUDE_DIRS}) ADD_EXECUTABLE(reanimator reanimator.cpp Prop.h Prop.cpp Pixer.h Mapping.h Mapping.cpp Repository.h Repository.cpp Render.h Render.cpp @@ -42,3 +43,5 @@ endif() if (USE_DETAIL) TARGET_LINK_LIBRARIES(reanimator ${PROTOBUF_LIBRARIES}) endif() + +target_link_libraries(reanimator ${JSONCPP_LIBRARIES}) diff --git a/src/Pixer.h b/src/Pixer.h index 2021b13..ef56e94 100644 --- a/src/Pixer.h +++ b/src/Pixer.h @@ -43,9 +43,21 @@ public: p.a = a/v; return p; } + + void preblend() { + r *= a; + g *= a; + b *= a; + } + + void postblend(double sc) { + r /= sc; + g /= sc; + b /= sc; + } }; -inline Pixer sampleLinear(const yarp::sig::ImageOf& src, +inline Pixer sampleWeakly(const yarp::sig::ImageOf& src, double x, double y) { const yarp::sig::PixelBgra& p = src.safePixel(x,y); Pixer v; @@ -56,4 +68,40 @@ inline Pixer sampleLinear(const yarp::sig::ImageOf& src, return v; } +inline Pixer sampleLinear(const yarp::sig::ImageOf& src, + double x, double y) { + int xx = (int)x; + int yy = (int)y; + float fx = x - xx; + float fy = y - yy; + const yarp::sig::PixelBgra& poo = src.safePixel(x,y); + const yarp::sig::PixelBgra& poy = src.safePixel(x,y+1); + const yarp::sig::PixelBgra& pxo = src.safePixel(x+1,y); + const yarp::sig::PixelBgra& pxy = src.safePixel(x+1,y+1); + Pixer v; + double aa = (1-fx)*(1-fy)*poo.a + fx*(1-fy)*pxo.a + (1-fx)*fy*poy.a + fx*fy*pxy.a; + if (aa < 0.0001) aa = 0.0001; + v.r = + ((1-fx)*(1-fy)*poo.r*poo.a + + fx*(1-fy)*pxo.r*pxo.a + + (1-fx)*fy*poy.r*poy.a + + fx*fy*pxy.r*pxy.a)/aa; + v.g = + ((1-fx)*(1-fy)*poo.g*poo.a + + fx*(1-fy)*pxo.g*pxo.a + + (1-fx)*fy*poy.g*poy.a + + fx*fy*pxy.g*pxy.a)/aa; + v.b = + ((1-fx)*(1-fy)*poo.b*poo.a + + fx*(1-fy)*pxo.b*pxo.a + + (1-fx)*fy*poy.b*poy.a + + fx*fy*pxy.b*pxy.a)/aa; + v.a = + (1-fx)*(1-fy)*poo.a + + fx*(1-fy)*pxo.a + + (1-fx)*fy*poy.a + + fx*fy*pxy.a; + return v; +} + #endif diff --git a/src/Render.cpp b/src/Render.cpp index 0d03563..808dc1f 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -249,17 +249,29 @@ void Render::add(const Input& in) { Pixer m3b = sampleLinear(in.get(),xx+(xxa-xxb)/2.0,yy+(yya-yyb)/2.0); Pixer m4b = sampleLinear(in.get(),xx-(xxa+xxb)/2.0,yy-(yya+yyb)/2.0); Pixer m5b = sampleLinear(in.get(),xx-(xxa-xxb)/2.0,yy-(yya-yyb)/2.0); - + + mo.preblend(); + m2.preblend(); + m3.preblend(); + m4.preblend(); + m5.preblend(); + m2b.preblend(); + m3b.preblend(); + m4b.preblend(); + m5b.preblend(); + double sc = (mo.a*4.0 + (m2.a+m3.a+m4.a+m5.a)*2.0 + (m2b.a+m3b.a+m4b.a+m5b.a))/16.0; mo = (mo*4.0 + (m2+m3+m4+m5)*2.0 + (m2b+m3b+m4b+m5b))/16.0; + if (sc > 0.0001) { + mo.postblend(sc); + } else { + mo.r = mo.g = mo.b = mo.a = 0.0; + } + m.r = mo.r; m.g = mo.g; m.b = mo.b; m.a = mo.a; - //} else { - //m = in.get().safePixel((int)xx,(int)yy); - //} - //const PixelBgra& m = in.get().safePixel((int)xx,(int)yy); PixelBgra result; if (d && act>25) { result.r = darkPixel.r + ((lightPixel.r-darkPixel.r)*m.r)/255; diff --git a/src/reanimator.cpp b/src/reanimator.cpp index b9f9be4..d1fa164 100644 --- a/src/reanimator.cpp +++ b/src/reanimator.cpp @@ -16,6 +16,10 @@ #include "VidAnim.h" #include "Dbg.h" +#include +#include +#include + using namespace std; using namespace yarp::os; using namespace yarp::sig; @@ -33,9 +37,10 @@ int main(int argc, char *argv[]) { Repository repo; - if (!options.check("zip")) { + if (!options.check("zip") && !options.check("json")) { fprintf(stderr, "reanimator --zip foo.zip --in layer1.png layer2.png\n"); fprintf(stderr, "reanimator --zip foo.zip --in layer1.png layer2.png +layer2_more.png\n"); + fprintf(stderr, "reanimator --json setup.json\n"); fprintf(stderr, "reanimator ... --w 200 --h 150\n"); fprintf(stderr, "reanimator ... --first 2\n"); fprintf(stderr, "reanimator ... --last 4\n"); @@ -50,10 +55,36 @@ int main(int argc, char *argv[]) { exit(1); } - repo.load(options.find("zip").asString().c_str()); - Inputs ins; + if (options.check("json")) { + std::ifstream file(options.find("json").asString().c_str()); + Json::Value root; + file >> root; + std::cout << root; + Json::Value inputs = root["inputs"]; + for (int i=0; i