From 1afcb907c475e52b48e5a96a87d06d8c1849d9e4 Mon Sep 17 00:00:00 2001 From: Michael Lemaire Date: Thu, 21 Aug 2014 15:30:09 +0200 Subject: [PATCH] Optimized and fixed canvas portion merging in final picture --- src/render/software/Canvas.cpp | 6 ++-- src/render/software/CanvasPictureWriter.cpp | 34 +++++++++++++++++---- src/render/software/CanvasPictureWriter.h | 4 +++ src/render/software/CanvasPortion.cpp | 15 +++++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/render/software/Canvas.cpp b/src/render/software/Canvas.cpp index 6c796f7..8c9d0df 100644 --- a/src/render/software/Canvas.cpp +++ b/src/render/software/Canvas.cpp @@ -6,6 +6,8 @@ #include "CanvasPreview.h" #include "CanvasPictureWriter.h" +#define CUTTER_SIZE 800 + Canvas::Canvas() { horizontal_portion_count = 1; @@ -28,8 +30,8 @@ Canvas::~Canvas() void Canvas::setSize(int width, int height) { - horizontal_portion_count = 1 + (width - 1) / 800; - vertical_portion_count = 1 + (height - 1) / 800; + horizontal_portion_count = 1 + (width - 1) / CUTTER_SIZE; + vertical_portion_count = 1 + (height - 1) / CUTTER_SIZE; int portion_width = width / horizontal_portion_count; int portion_height = height / vertical_portion_count; diff --git a/src/render/software/CanvasPictureWriter.cpp b/src/render/software/CanvasPictureWriter.cpp index b71ace5..22d1775 100644 --- a/src/render/software/CanvasPictureWriter.cpp +++ b/src/render/software/CanvasPictureWriter.cpp @@ -14,11 +14,19 @@ CanvasPictureWriter::CanvasPictureWriter(const Canvas *canvas): antialias = 1; width = canvas->getWidth(); height = canvas->getHeight(); + + last_portion = NULL; + last_stream = NULL; + last_y = 0; } CanvasPictureWriter::~CanvasPictureWriter() { delete profile; + if (last_stream) + { + delete last_stream; + } } void CanvasPictureWriter::setAntialias(int antialias) @@ -80,15 +88,29 @@ Color CanvasPictureWriter::getRawPixel(int x, int y) // Get the portion this pixel is in CanvasPortion *portion = canvas->atPixel(x, y); - // Get the pack stream positioned at the pixel - PackStream stream; - if (not portion->getReadStream(stream, x - portion->getXOffset(), y - portion->getYOffset())) + // While we stay in the same portion line, read is sequential in the stream + if (portion != last_portion or last_y != y) { - return COLOR_BLACK; + // Get the pack stream positioned at the pixel + if (last_stream) + { + delete last_stream; + } + last_stream = new PackStream; + if (portion->getReadStream(*last_stream, x - portion->getXOffset(), y - portion->getYOffset())) + { + last_portion = portion; + last_y = y; + } + else + { + // Portion has no stream + return COLOR_BLACK; + } } - // Load the pixel and apply tone mapping + // Load the pixel Color col; - col.load(&stream); + col.load(last_stream); return col; } diff --git a/src/render/software/CanvasPictureWriter.h b/src/render/software/CanvasPictureWriter.h index 6f7895f..a07d3df 100644 --- a/src/render/software/CanvasPictureWriter.h +++ b/src/render/software/CanvasPictureWriter.h @@ -46,6 +46,10 @@ private: int width; int height; ColorProfile *profile; + + CanvasPortion *last_portion; + int last_y; + PackStream *last_stream; }; } diff --git a/src/render/software/CanvasPortion.cpp b/src/render/software/CanvasPortion.cpp index 4aee76f..805cb02 100644 --- a/src/render/software/CanvasPortion.cpp +++ b/src/render/software/CanvasPortion.cpp @@ -108,11 +108,20 @@ bool CanvasPortion::getReadStream(PackStream &stream, int x, int y) { if (FileSystem::isFile(filepath)) { + if (not stream.bindToFile(filepath)) + { + return false; + } + int unused_i; - double unused_d; - stream.bindToFile(filepath); stream.skip(unused_i, 2); - stream.skip(unused_d, (y * width + x - 1) * 4); + + if (x > 0 or y > 0) + { + double unused_d; + stream.skip(unused_d, (y * width + x) * 4); + } + return true; } else