Port motivate, globe (assets need work), and reverse
This commit is contained in:
parent
05795313c4
commit
0162997792
6 changed files with 174 additions and 155 deletions
BIN
assets/images/globediffuse.png
Normal file
BIN
assets/images/globediffuse.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
assets/images/globespec.png
Normal file
BIN
assets/images/globespec.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
Binary file not shown.
Before Width: | Height: | Size: 283 KiB After Width: | Height: | Size: 29 KiB |
109
natives/globe.cc
109
natives/globe.cc
|
@ -1,11 +1,9 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value Globe(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -15,66 +13,65 @@ Napi::Value Globe(const Napi::CallbackInfo &info) {
|
|||
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
||||
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
||||
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
|
||||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options = VImage::option();
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
list<Image> mid;
|
||||
Image distort;
|
||||
Image overlay;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
VImage in =
|
||||
VImage::new_from_buffer(
|
||||
data.Data(), data.Length(), "",
|
||||
type == "gif" ? options->set("n", -1)->set("access", "sequential")
|
||||
: options)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||
|
||||
int width = in.width();
|
||||
int page_height = vips_image_get_page_height(in.get_image());
|
||||
int n_pages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 29;
|
||||
|
||||
double size = min(width, page_height);
|
||||
|
||||
string diffPath = basePath + "assets/images/globediffuse.png";
|
||||
VImage diffuse =
|
||||
VImage::new_from_file(diffPath.c_str()).resize(size / 500.0) / 255;
|
||||
|
||||
string specPath = basePath + "assets/images/globespec.png";
|
||||
VImage specular =
|
||||
VImage::new_from_file(specPath.c_str()).resize(size / 500.0);
|
||||
|
||||
string distortPath = basePath + "assets/images/spheremap.png";
|
||||
VImage distort =
|
||||
(VImage::new_from_file(distortPath.c_str()).resize(size / 500.0) /
|
||||
255) *
|
||||
size;
|
||||
|
||||
vector<VImage> img;
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
VImage img_frame =
|
||||
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||
VImage resized = img_frame.resize(
|
||||
size / (double)width,
|
||||
VImage::option()->set("vscale", size / (double)page_height));
|
||||
VImage rolled = resized.wrap(
|
||||
VImage::option()->set("x", page_height * i / n_pages)->set("y", 0));
|
||||
VImage extracted = rolled.extract_band(0, VImage::option()->set("n", 3));
|
||||
VImage mapped = extracted.mapim(distort);
|
||||
VImage composited = mapped * diffuse + specular;
|
||||
VImage frame = composited.bandjoin(diffuse > 0.0);
|
||||
img.push_back(frame);
|
||||
}
|
||||
distort.read(basePath + "assets/images/spheremap.png");
|
||||
overlay.read(basePath + "assets/images/sphere_overlay.png");
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
|
||||
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
||||
final.set(VIPS_META_PAGE_HEIGHT, page_height);
|
||||
if (type != "gif") {
|
||||
list<Image>::iterator it = coalesced.begin();
|
||||
for (int i = 0; i < 29; ++i) {
|
||||
coalesced.push_back(*it);
|
||||
}
|
||||
vector<int> delay(30, 50);
|
||||
final.set("delay", delay);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (Image &image : coalesced) {
|
||||
image.scale(Geometry("500x500!"));
|
||||
image.alphaChannel(Magick::SetAlphaChannel);
|
||||
size_t width = image.columns();
|
||||
image.roll(Geometry("+" + to_string(width * i / coalesced.size())));
|
||||
image.composite(overlay, Magick::CenterGravity,
|
||||
Magick::HardLightCompositeOp);
|
||||
image.composite(distort, Magick::CenterGravity,
|
||||
Magick::DistortCompositeOp);
|
||||
image.magick("GIF");
|
||||
mid.push_back(image);
|
||||
i++;
|
||||
}
|
||||
|
||||
optimizeTransparency(mid.begin(), mid.end());
|
||||
if (delay != 0) {
|
||||
for_each(mid.begin(), mid.end(), animationDelayImage(delay));
|
||||
} else if (type != "gif") {
|
||||
for_each(mid.begin(), mid.end(), animationDelayImage(5));
|
||||
}
|
||||
|
||||
for (Image &image : mid) {
|
||||
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
||||
image.quantize();
|
||||
}
|
||||
|
||||
writeImages(mid.begin(), mid.end(), &blob);
|
||||
void *buf;
|
||||
size_t length;
|
||||
final.write_to_buffer(".gif", &buf, &length);
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", "gif");
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value Motivate(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -20,85 +18,102 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) {
|
|||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options = VImage::option()->set("access", "sequential");
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
list<Image> mid;
|
||||
Image top;
|
||||
Image bottom;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
}
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
VImage in =
|
||||
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||
type == "gif" ? options->set("n", -1) : options)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||
|
||||
top.size(Geometry("600"));
|
||||
top.backgroundColor("black");
|
||||
top.font("Times");
|
||||
top.textGravity(Magick::CenterGravity);
|
||||
top.fontPointsize(56);
|
||||
top.read("pango:<span font_family=\"" +
|
||||
(font == "roboto" ? "Roboto Condensed" : font) +
|
||||
"\" foreground='white'>" + top_text + "</span>");
|
||||
top.extent(Geometry(bottom_text != "" ? to_string(top.columns()) + "x" +
|
||||
to_string(top.rows())
|
||||
: to_string(top.columns()) + "x" +
|
||||
to_string(top.rows() + 20)),
|
||||
"black", Magick::NorthGravity);
|
||||
int width = in.width();
|
||||
int size = width / 5;
|
||||
int page_height = vips_image_get_page_height(in.get_image());
|
||||
int n_pages = vips_image_get_n_pages(in.get_image());
|
||||
int textWidth = width - ((width / 25) * 2);
|
||||
|
||||
string font_string = font == "roboto" ? "Roboto Condensed" : font;
|
||||
|
||||
string topText = "<span foreground=\"white\" background=\"black\">" +
|
||||
top_text + "</span>";
|
||||
|
||||
VImage topImage = VImage::text(
|
||||
topText.c_str(),
|
||||
VImage::option()
|
||||
->set("rgba", true)
|
||||
->set("align", VIPS_ALIGN_CENTRE)
|
||||
->set("font", (font_string + " " + to_string(size)).c_str())
|
||||
->set("width", textWidth));
|
||||
topImage.write_to_file("/home/esm/motivatetext2.png");
|
||||
|
||||
VImage bottomImage;
|
||||
if (bottom_text != "") {
|
||||
bottom.size(Geometry("600"));
|
||||
bottom.backgroundColor("black");
|
||||
bottom.font("Times");
|
||||
bottom.textGravity(Magick::CenterGravity);
|
||||
bottom.fontPointsize(28);
|
||||
bottom.read("pango:<span font_family=\"" +
|
||||
(font == "roboto" ? "Roboto Condensed" : font) +
|
||||
"\" foreground='white'>" + bottom_text + "</span>");
|
||||
bottom.extent(Geometry(to_string(bottom.columns()) + "x" +
|
||||
to_string(bottom.rows() + 20)),
|
||||
"black", Magick::NorthGravity);
|
||||
string bottomText = "<span foreground=\"white\" background=\"black\">" +
|
||||
bottom_text + "</span>";
|
||||
|
||||
bottomImage = VImage::text(
|
||||
bottomText.c_str(),
|
||||
VImage::option()
|
||||
->set("rgba", true)
|
||||
->set("align", VIPS_ALIGN_CENTRE)
|
||||
->set("font", (font_string + " " + to_string(size * 0.4)).c_str())
|
||||
->set("width", textWidth));
|
||||
}
|
||||
|
||||
for (Image &image : coalesced) {
|
||||
Image final;
|
||||
image.scale(Geometry(500, 500));
|
||||
image.borderColor("black");
|
||||
image.border(Geometry(5, 5));
|
||||
image.borderColor("white");
|
||||
image.border(Geometry(3, 3));
|
||||
image.backgroundColor("black");
|
||||
image.extent(Geometry(600, image.rows() + 50), Magick::CenterGravity);
|
||||
vector<VImage> img;
|
||||
int height;
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
VImage img_frame =
|
||||
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||
|
||||
list<Image> to_append;
|
||||
to_append.push_back(image);
|
||||
to_append.push_back(top);
|
||||
if (bottom_text != "") to_append.push_back(bottom);
|
||||
appendImages(&final, to_append.begin(), to_append.end(), true);
|
||||
final.repage();
|
||||
final.magick(type);
|
||||
final.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
||||
mid.push_back(final);
|
||||
}
|
||||
int borderSize = max(2, width / 66);
|
||||
int borderSize2 = borderSize * 0.5;
|
||||
VImage bordered =
|
||||
img_frame.embed(borderSize, borderSize, width + (borderSize * 2),
|
||||
page_height + (borderSize * 2),
|
||||
VImage::option()->set("extend", "black"));
|
||||
VImage bordered2 = bordered.embed(
|
||||
borderSize2, borderSize2, bordered.width() + (borderSize2 * 2),
|
||||
bordered.height() + (borderSize2 * 2),
|
||||
VImage::option()->set("extend", "white"));
|
||||
|
||||
optimizeTransparency(mid.begin(), mid.end());
|
||||
int addition = width / 8;
|
||||
int sideAddition = page_height * 0.4;
|
||||
|
||||
if (type == "gif") {
|
||||
for (Image &image : mid) {
|
||||
image.quantizeDither(false);
|
||||
image.quantize();
|
||||
VImage bordered3 = bordered2.embed(
|
||||
sideAddition / 2, addition / 2, bordered2.width() + sideAddition,
|
||||
bordered2.height() + addition,
|
||||
VImage::option()->set("extend", "black"));
|
||||
VImage frame = bordered3.join(
|
||||
topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
|
||||
topImage.height() + (size / 4),
|
||||
VImage::option()->set("extend", "black")),
|
||||
VIPS_DIRECTION_VERTICAL,
|
||||
VImage::option()->set("background", 0x000000)->set("expand", true));
|
||||
if (bottom_text != "") {
|
||||
frame = frame.join(
|
||||
bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
|
||||
bottomImage.height() + (size / 4),
|
||||
VImage::option()->set("extend", "black")),
|
||||
VIPS_DIRECTION_VERTICAL,
|
||||
VImage::option()->set("background", 0x000000)->set("expand", true));
|
||||
}
|
||||
height = frame.height();
|
||||
img.push_back(frame);
|
||||
}
|
||||
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1))
|
||||
.extract_band(0, VImage::option()->set("n", 3));
|
||||
final.set(VIPS_META_PAGE_HEIGHT, height);
|
||||
if (delay) final.set("delay", delay);
|
||||
|
||||
writeImages(mid.begin(), mid.end(), &blob);
|
||||
void *buf;
|
||||
size_t length;
|
||||
final.write_to_buffer(
|
||||
("." + type).c_str(), &buf, &length,
|
||||
type == "gif" ? VImage::option()->set("dither", 1) : 0);
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", type);
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value Reverse(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -15,47 +15,54 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) {
|
|||
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
||||
bool soos =
|
||||
obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
|
||||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options =
|
||||
VImage::option()->set("access", "sequential")->set("n", -1);
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", options)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
|
||||
int width = in.width();
|
||||
int page_height = vips_image_get_page_height(in.get_image());
|
||||
int n_pages = vips_image_get_n_pages(in.get_image());
|
||||
|
||||
vector<VImage> split;
|
||||
// todo: find a better way of getting individual frames (or at least getting the frames in reverse order)
|
||||
for (int i = 0; i < n_pages; i++) {
|
||||
VImage img_frame = in.crop(0, i * page_height, width, page_height);
|
||||
split.push_back(img_frame);
|
||||
}
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
|
||||
vector<int> delays = in.get_array_int("delay");
|
||||
if (soos) {
|
||||
list<Image> copy = coalesced;
|
||||
copy.reverse();
|
||||
vector<VImage> copy = split;
|
||||
vector<int> copy2 = delays;
|
||||
reverse(copy.begin(), copy.end());
|
||||
reverse(copy2.begin(), copy2.end());
|
||||
copy.pop_back();
|
||||
copy.pop_front();
|
||||
coalesced.insert(coalesced.end(), copy.begin(), copy.end());
|
||||
copy2.pop_back();
|
||||
copy.erase(copy.begin());
|
||||
copy2.erase(copy2.begin());
|
||||
split.insert(split.end(), copy.begin(), copy.end());
|
||||
delays.insert(delays.end(), copy2.begin(), copy2.end());
|
||||
} else {
|
||||
coalesced.reverse();
|
||||
reverse(split.begin(), split.end());
|
||||
reverse(delays.begin(), delays.end());
|
||||
}
|
||||
|
||||
for_each(coalesced.begin(), coalesced.end(), magickImage("GIF"));
|
||||
VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
|
||||
final.set(VIPS_META_PAGE_HEIGHT, page_height);
|
||||
final.set("delay", delays);
|
||||
|
||||
optimizeTransparency(coalesced.begin(), coalesced.end());
|
||||
void *buf;
|
||||
size_t length;
|
||||
final.write_to_buffer(".gif", &buf, &length,
|
||||
VImage::option()->set("dither", 0));
|
||||
|
||||
for (Image &image : coalesced) {
|
||||
image.quantizeDither(false);
|
||||
image.quantize();
|
||||
if (delay != 0) image.animationDelay(delay);
|
||||
}
|
||||
|
||||
writeImages(coalesced.begin(), coalesced.end(), &blob);
|
||||
vips_thread_shutdown();
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", "gif");
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
Loading…
Reference in a new issue