implement frames save and open from ppi

This commit is contained in:
2019-10-17 23:41:37 +02:00
parent 880eb7a406
commit 4e0520da80
9 changed files with 165 additions and 128 deletions

View File

@@ -54,7 +54,7 @@ void App::create()
void App::open_document(std::string path) void App::open_document(std::string path)
{ {
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)"); std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m; std::smatch m;
if (!std::regex_search(path, m, r)) if (!std::regex_search(path, m, r))
return; return;

View File

@@ -5,6 +5,8 @@
#include "texture.h" #include "texture.h"
#include "node_progress_bar.h" #include "node_progress_bar.h"
#include <thread> #include <thread>
#include <algorithm>
#include <numeric>
#ifdef __APPLE__ #ifdef __APPLE__
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
@@ -2117,8 +2119,13 @@ bool Canvas::project_save_thread(std::string file_path, bool show_progress)
int n_layers = (int)m_layers.size(); int n_layers = (int)m_layers.size();
fwrite(&n_layers, sizeof(int), 1, fp); fwrite(&n_layers, sizeof(int), 1, fp);
int n_frames = std::accumulate(m_layers.begin(), m_layers.end(), 0,
[](int tot, auto& l) { return tot + l->m_frames.size(); });
if (ppi_header.doc_version.minor >= 3)
fwrite(&n_frames, sizeof(int), 1, fp);
int progress = 0; int progress = 0;
int total = (int)m_layers.size() * 6; int total = n_frames * 6;
for (int i = 0; i < (int)m_layers.size(); i++) for (int i = 0; i < (int)m_layers.size(); i++)
{ {
@@ -2132,15 +2139,27 @@ bool Canvas::project_save_thread(std::string file_path, bool show_progress)
fwrite(&name_len, sizeof(int), 1, fp); fwrite(&name_len, sizeof(int), 1, fp);
fwrite(m_layers[i]->m_name.data(), name_len, 1, fp); fwrite(m_layers[i]->m_name.data(), name_len, 1, fp);
if (ppi_header.doc_version.minor > 1) if (ppi_header.doc_version.minor >= 2)
{ {
fwrite(&m_layers[i]->m_blend_mode, sizeof(int), 1, fp); fwrite(&m_layers[i]->m_blend_mode, sizeof(int), 1, fp);
fwrite(&m_layers[i]->m_alpha_locked, sizeof(bool), 1, fp); fwrite(&m_layers[i]->m_alpha_locked, sizeof(bool), 1, fp);
fwrite(&m_layers[i]->m_visible, sizeof(bool), 1, fp); fwrite(&m_layers[i]->m_visible, sizeof(bool), 1, fp);
} }
m_layers[i]->optimize(); int frames = 1;
auto snap = m_layers[i]->snapshot(); if (ppi_header.doc_version.minor >= 3)
{
frames = (int)m_layers[i]->m_frames.size();
fwrite(&frames, sizeof(int), 1, fp);
}
for (int fi = 0; fi < frames; fi++)
{
if (ppi_header.doc_version.minor >= 3)
fwrite(&m_layers[i]->m_frames[fi].m_duration, sizeof(int), 1, fp);
m_layers[i]->optimize(fi);
auto snap = m_layers[i]->snapshot(fi);
for (int plane_index = 0; plane_index < 6; plane_index++) for (int plane_index = 0; plane_index < 6; plane_index++)
{ {
int has_data = snap.m_dirty_face[plane_index] ? 1 : 0; int has_data = snap.m_dirty_face[plane_index] ? 1 : 0;
@@ -2172,6 +2191,7 @@ bool Canvas::project_save_thread(std::string file_path, bool show_progress)
LOG("progress: %f", p); LOG("progress: %f", p);
} }
} }
}
fclose(fp); fclose(fp);
bool success = false; bool success = false;
if (use_tmp) if (use_tmp)
@@ -2273,13 +2293,16 @@ bool Canvas::project_open_thread(std::string file_path)
int n_layers = 0; int n_layers = 0;
fread(&n_layers, sizeof(int), 1, fp); fread(&n_layers, sizeof(int), 1, fp);
int n_frames = 1;
if (ppi_header.doc_version.minor >= 3)
fread(&n_frames, sizeof(int), 1, fp);
const int bytes = m_width * m_height * 4; const int bytes = m_width * m_height * 4;
Layer::Snapshot snap; Layer::Snapshot snap;
snap.create(m_width, m_height); // allocate single data, no box should be bigger snap.create(m_width, m_height); // allocate single data, no box should be bigger
int progress = 0; int progress = 0;
int total = n_layers * 6; int total = n_frames * 6;
for (auto& l : m_layers) for (auto& l : m_layers)
l->destroy(); l->destroy();
@@ -2307,13 +2330,25 @@ bool Canvas::project_open_thread(std::string file_path)
std::string name(name_len, '\0'); std::string name(name_len, '\0');
fread((char*)name.data(), name_len, 1, fp); fread((char*)name.data(), name_len, 1, fp);
if (ppi_header.doc_version.minor > 1) if (ppi_header.doc_version.minor >= 2)
{ {
fread(&layer->m_blend_mode, sizeof(int), 1, fp); fread(&layer->m_blend_mode, sizeof(int), 1, fp);
fread(&layer->m_alpha_locked, sizeof(bool), 1, fp); fread(&layer->m_alpha_locked, sizeof(bool), 1, fp);
fread(&layer->m_visible, sizeof(bool), 1, fp); fread(&layer->m_visible, sizeof(bool), 1, fp);
} }
int frames = 1;
if (ppi_header.doc_version.minor >= 3)
fread(&frames, sizeof(int), 1, fp);
layer->create(m_width, m_height, name.c_str());
for (int fi = 0; fi < frames; fi++)
{
if (fi > 0)
layer->add_frame();
if (ppi_header.doc_version.minor >= 3)
fread(&layer->m_frames[fi].m_duration, sizeof(int), 1, fp);
snap.clear(); snap.clear();
for (int plane_index = 0; plane_index < 6; plane_index++) for (int plane_index = 0; plane_index < 6; plane_index++)
{ {
@@ -2350,9 +2385,8 @@ bool Canvas::project_open_thread(std::string file_path)
pb->m_progress->SetWidthP(p); pb->m_progress->SetWidthP(p);
} }
} }
layer->restore(snap, fi);
layer->create(m_width, m_height, name.c_str()); }
layer->restore(snap);
} }
std::swap(tmp_layers, m_layers); std::swap(tmp_layers, m_layers);
@@ -2377,6 +2411,7 @@ bool Canvas::project_open_thread(std::string file_path)
{ {
pb->destroy(); pb->destroy();
App::I->title_update(); App::I->title_update();
App::I->animation->load_layers();
} }
return true; return true;
} }

View File

@@ -29,7 +29,11 @@ struct PPIThumb
struct PPIDocVersion struct PPIDocVersion
{ {
int major = 0; int major = 0;
int minor = 2;
// version 1: initial
// version 2: added blend mode, alpha and visibility
// version 3: added animation frames
int minor = 3;
}; };
struct PPISoftVersion struct PPISoftVersion

View File

@@ -101,15 +101,17 @@ void Layer::destroy()
rtt(i).destroy(); rtt(i).destroy();
} }
void Layer::optimize() void Layer::optimize(int frame /*= -1*/)
{ {
if (frame == -1)
frame = m_frame_index;
int saved_bytes = 0; int saved_bytes = 0;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
if (!face(i)) if (!face(i, frame))
continue; continue;
auto data = std::unique_ptr<glm::u8vec4[]>(reinterpret_cast<glm::u8vec4*>(rtt(i).readTextureData())); auto data = std::unique_ptr<glm::u8vec4[]>(reinterpret_cast<glm::u8vec4*>(rtt(i, frame).readTextureData()));
glm::ivec2 bbmin(w, h); glm::ivec2 bbmin(w, h);
glm::ivec2 bbmax(0); glm::ivec2 bbmax(0);
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
@@ -124,17 +126,17 @@ void Layer::optimize()
} }
} }
glm::vec2 bbsz = bbmax - bbmin; glm::vec2 bbsz = bbmax - bbmin;
glm::vec2 old_size = zw(box(i)) - xy(box(i)); glm::vec2 old_size = zw(box(i, frame)) - xy(box(i, frame));
glm::vec2 diff; glm::vec2 diff;
if (bbsz.x <= 0 || bbmax.y <= 0) if (bbsz.x <= 0 || bbmax.y <= 0)
{ {
face(i) = false; face(i, frame) = false;
box(i) = glm::vec4(0); box(i, frame) = glm::vec4(0);
diff = old_size; diff = old_size;
} }
else else
{ {
box(i) = { bbmin, bbmax }; box(i, frame) = { bbmin, bbmax };
diff = old_size - bbsz; diff = old_size - bbsz;
} }
@@ -143,73 +145,79 @@ void Layer::optimize()
LOG("optimized %d bytes", saved_bytes); LOG("optimized %d bytes", saved_bytes);
} }
void Layer::restore(const Snapshot& snap) void Layer::restore(const Snapshot& snap, int frame /*= -1*/)
{ {
clear({ 0, 0, 0, 0 }); if (frame == -1)
frame = m_frame_index;
clear({ 0, 0, 0, 0 }, frame);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
if (snap.image[i] == nullptr || snap.m_dirty_face[i] == false || box_area(snap.m_dirty_box[i]) <= 0) if (snap.image[i] == nullptr || snap.m_dirty_face[i] == false || box_area(snap.m_dirty_box[i]) <= 0)
{ {
box(i) = glm::vec4(snap.width, snap.height, 0, 0); box(i, frame) = glm::vec4(snap.width, snap.height, 0, 0);
face(i) = false; face(i, frame) = false;
continue; continue;
} }
box(i) = snap.m_dirty_box[i]; box(i, frame) = snap.m_dirty_box[i];
face(i) = snap.m_dirty_face[i]; face(i, frame) = snap.m_dirty_face[i];
// TODO: this should not be recreated here! // TODO: this should not be recreated here!
// Sorry I messed up with this, // Sorry I messed up with this,
// it's just a quick fix DON'T SHIP!! // it's just a quick fix DON'T SHIP!!
//m_rtt[i].recreate(); //m_rtt[i].recreate();
App::I->render_task_async([this, i, &snap] App::I->render_task_async([this, i, &snap, frame]
{ {
rtt(i).bindTexture(); rtt(i, frame).bindTexture();
glm::vec2 box_sz = zw(box(i)) - xy(box(i)); glm::vec2 box_sz = zw(box(i, frame)) - xy(box(i, frame));
glTexSubImage2D(GL_TEXTURE_2D, 0, glTexSubImage2D(GL_TEXTURE_2D, 0,
box(i).x, box(i).y, box(i, frame).x, box(i, frame).y,
box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE,
snap.image[i].get()); snap.image[i].get());
rtt(i).unbindTexture(); rtt(i, frame).unbindTexture();
LOG("restore face %d - %d bytes (%dx%d)", i, LOG("restore frame %d face %d - %d bytes (%dx%d)", frame, i,
(int)box_sz.x * (int)box_sz.y * 4, (int)box_sz.x, (int)box_sz.y); (int)box_sz.x * (int)box_sz.y * 4, (int)box_sz.x, (int)box_sz.y);
}); });
} }
App::I->render_sync(); App::I->render_sync();
} }
Layer::Snapshot Layer::snapshot(std::array<glm::vec4, 6>* dirty_box /*= nullptr*/, std::array<bool, 6>* dirty_face /*= nullptr*/) Layer::Snapshot Layer::snapshot(int frame /*= -1*/, std::array<glm::vec4, 6>* dirty_box /*= nullptr*/, std::array<bool, 6>* dirty_face /*= nullptr*/)
{ {
if (frame == -1)
frame = m_frame_index;
Snapshot snap; Snapshot snap;
snap.width = w; snap.width = w;
snap.height = h; snap.height = h;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
snap.m_dirty_box[i] = dirty_box ? dirty_box->at(i) : box(i); snap.m_dirty_box[i] = dirty_box ? dirty_box->at(i) : box(i, frame);
snap.m_dirty_face[i] = dirty_face ? dirty_face->at(i) : face(i); snap.m_dirty_face[i] = dirty_face ? dirty_face->at(i) : face(i, frame);
if (!snap.m_dirty_face[i]) if (!snap.m_dirty_face[i])
continue; continue;
snap.image[i] = std::make_unique<uint8_t[]>(rtt(i).bytes()); snap.image[i] = std::make_unique<uint8_t[]>(rtt(i, frame).bytes());
App::I->render_task_async([this, i, &snap] App::I->render_task_async([this, i, &snap, frame]
{ {
rtt(i).bindFramebuffer(); rtt(i, frame).bindFramebuffer();
glm::vec2 box_sz = zw(snap.m_dirty_box[i]) - xy(snap.m_dirty_box[i]); glm::vec2 box_sz = zw(snap.m_dirty_box[i]) - xy(snap.m_dirty_box[i]);
glReadPixels(snap.m_dirty_box[i].x, snap.m_dirty_box[i].y, glReadPixels(snap.m_dirty_box[i].x, snap.m_dirty_box[i].y,
box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, snap.image[i].get()); box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, snap.image[i].get());
rtt(i).unbindFramebuffer(); rtt(i, frame).unbindFramebuffer();
}); });
} }
App::I->render_sync(); App::I->render_sync();
return snap; return snap;
} }
void Layer::clear(const glm::vec4& c) void Layer::clear(const glm::vec4& c, int frame /*= -1*/)
{ {
frame().clear(c); if (frame == -1)
frame = m_frame_index;
m_frames[frame].clear(c);
} }
bool Layer::create(int width, int height, std::string name) bool Layer::create(int width, int height, std::string name)
@@ -218,9 +226,9 @@ bool Layer::create(int width, int height, std::string name)
w = width; w = width;
h = height; h = height;
m_frame_index = 0; m_frame_index = 0;
if (m_frames.empty()) m_frames.clear();
m_frames.emplace_back(); m_frames.emplace_back();
frame().create(width, height); m_frames.back().create(width, height);
return true; return true;
} }

View File

@@ -63,11 +63,11 @@ public:
bool add_frame(); bool add_frame();
int total_duration() const noexcept; int total_duration() const noexcept;
void goto_frame(int frame) noexcept; void goto_frame(int frame) noexcept;
void clear(const glm::vec4& c); void clear(const glm::vec4& c, int frame = -1);
Snapshot snapshot(std::array<glm::vec4, 6>* dirty_box = nullptr, std::array<bool, 6>* dirty_face = nullptr); Snapshot snapshot(int frame = -1, std::array<glm::vec4, 6>* dirty_box = nullptr, std::array<bool, 6>* dirty_face = nullptr);
TextureCube gen_cube(); TextureCube gen_cube();
Texture2D gen_equirect(); Texture2D gen_equirect();
void restore(const Snapshot& snap); void restore(const Snapshot& snap, int frame = -1);
void destroy(); void destroy();
void optimize(); void optimize(int frame = -1);
}; };

View File

@@ -37,21 +37,16 @@ void NodePanelAnimation::init_controls()
Canvas::I->layer().add_frame(); Canvas::I->layer().add_frame();
load_layers(); load_layers();
}; };
btn_up->on_click = [this](Node*) {
auto& layers = Canvas::I->m_layers;
auto layer = std::find_if(layers.begin(), layers.end(),
[id = m_selected_frame_layer_id](const auto& l) { return l->id == id; });
if (layer != layers.end())
(*layer)->m_frames[m_selected_frame_index].m_duration++;
};
btn_up->on_click = [this](Node*) { btn_up->on_click = [this](Node*) {
if (auto layer = Canvas::I->layer_with_id(m_selected_frame_layer_id)) if (auto layer = Canvas::I->layer_with_id(m_selected_frame_layer_id))
layer->m_frames[m_selected_frame_index].m_duration++; layer->m_frames[m_selected_frame_index].m_duration =
glm::max(layer->m_frames[m_selected_frame_index].m_duration + 1, 1);
load_layers(); load_layers();
}; };
btn_down->on_click = [this](Node*) { btn_down->on_click = [this](Node*) {
if (auto layer = Canvas::I->layer_with_id(m_selected_frame_layer_id)) if (auto layer = Canvas::I->layer_with_id(m_selected_frame_layer_id))
layer->m_frames[m_selected_frame_index].m_duration--; layer->m_frames[m_selected_frame_index].m_duration =
glm::max(layer->m_frames[m_selected_frame_index].m_duration - 1, 1);
load_layers(); load_layers();
}; };
@@ -85,9 +80,9 @@ void NodePanelAnimation::load_layers()
} }
b->on_click = [this, fi, lid=layers[i]->id] (Node* target) { b->on_click = [this, fi, lid=layers[i]->id] (Node* target) {
auto frame = static_cast<NodeAnimationFrame*>(target); auto frame = static_cast<NodeAnimationFrame*>(target);
frame->set_active(true);
if (m_selected_frame) if (m_selected_frame)
m_selected_frame->set_active(false); m_selected_frame->set_active(false);
frame->set_active(true);
m_selected_frame = frame; m_selected_frame = frame;
m_selected_frame_layer_id = lid; m_selected_frame_layer_id = lid;
m_selected_frame_index = fi; m_selected_frame_index = fi;

View File

@@ -83,7 +83,7 @@ void NodePanelBrush::init()
m_btn_add->on_click = [this](Node*) { m_btn_add->on_click = [this](Node*) {
App::I->pick_file({ "JPG", "PNG" }, [this](std::string path) { App::I->pick_file({ "JPG", "PNG" }, [this](std::string path) {
std::string name, base, ext; std::string name, base, ext;
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)"); std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m; std::smatch m;
if (!std::regex_search(path, m, r)) if (!std::regex_search(path, m, r))
return; return;
@@ -708,7 +708,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path_in, const PPBRInf
path += ".ppbr"; path += ".ppbr";
LOG("export ppbr to: %s", path.c_str()); LOG("export ppbr to: %s", path.c_str());
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)?$)"); std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)?$)");
std::smatch m; std::smatch m;
if (!std::regex_search(path, m, r)) if (!std::regex_search(path, m, r))
return false; return false;
@@ -972,7 +972,7 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
LOG("ABR detected"); LOG("ABR detected");
std::string name, base, ext; std::string name, base, ext;
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)"); std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m; std::smatch m;
if (!std::regex_search(path, m, r)) if (!std::regex_search(path, m, r))
return false; return false;
@@ -1042,7 +1042,7 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
bool NodePanelBrushPreset::import_brush(const std::string& path) bool NodePanelBrushPreset::import_brush(const std::string& path)
{ {
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)"); std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m; std::smatch m;
if (!std::regex_search(path, m, r)) if (!std::regex_search(path, m, r))
return false; return false;

View File

@@ -409,7 +409,7 @@ void NodePanelLayer::merge(int src_index, int dst_index, bool create_history)
a->m_dirty_face[i] = Canvas::I->m_layers[dst_index]->face(i); a->m_dirty_face[i] = Canvas::I->m_layers[dst_index]->face(i);
} }
a->m_snap = std::make_shared<Layer::Snapshot>(); a->m_snap = std::make_shared<Layer::Snapshot>();
*a->m_snap = Canvas::I->m_layers[dst_index]->snapshot( *a->m_snap = Canvas::I->m_layers[dst_index]->snapshot(-1,
&Canvas::I->m_layers[src_index]->frame().m_dirty_box, &Canvas::I->m_layers[src_index]->frame().m_dirty_face); &Canvas::I->m_layers[src_index]->frame().m_dirty_box, &Canvas::I->m_layers[src_index]->frame().m_dirty_face);
a->m_layer = Canvas::I->m_layers[src_index]; a->m_layer = Canvas::I->m_layers[src_index];
a->m_layer_node = std::static_pointer_cast<NodeLayer>(m_layers_container->m_children[src_index]); a->m_layer_node = std::static_pointer_cast<NodeLayer>(m_layers_container->m_children[src_index]);

View File

@@ -93,12 +93,7 @@ bool RTT::resize(int width, int height)
oldRFboID = 0; oldRFboID = 0;
oldDFboID = 0; oldDFboID = 0;
fboID = new_rtt.fboID; *this = std::move(new_rtt);
rboID = new_rtt.rboID;
texID = new_rtt.texID;
int_fmt = new_rtt.int_fmt;
w = new_rtt.w;
h = new_rtt.h;
return true; return true;
} }