diff --git a/src/render/software/Canvas.cpp b/src/render/software/Canvas.cpp index 7cf1439..67cd6c0 100644 --- a/src/render/software/Canvas.cpp +++ b/src/render/software/Canvas.cpp @@ -11,7 +11,7 @@ Canvas::Canvas() vertical_portion_count = 1; width = 1; height = 1; - portions.push_back(new CanvasPortion()); + portions.push_back(new CanvasPortion); preview = new CanvasPreview; } @@ -41,13 +41,14 @@ void Canvas::setSize(int width, int height) int done_width = 0; int done_height = 0; + int index = 0; for (int y = 0; y < vertical_portion_count; y++) { done_width = 0; for (int x = 0; x < horizontal_portion_count; x++) { - CanvasPortion *portion = new CanvasPortion(preview); + CanvasPortion *portion = new CanvasPortion(index++, preview); portion->setSize((x == horizontal_portion_count - 1) ? width - done_width : portion_width, (y == vertical_portion_count - 1) ? height - done_height : portion_height, diff --git a/src/render/software/CanvasPortion.cpp b/src/render/software/CanvasPortion.cpp index 193404a..c29fec9 100644 --- a/src/render/software/CanvasPortion.cpp +++ b/src/render/software/CanvasPortion.cpp @@ -4,6 +4,8 @@ #include "CanvasPixel.h" #include "CanvasPreview.h" +#include "PackStream.h" +#include "FileSystem.h" #define CHECK_COORDINATES() assert(x >= 0); \ assert(x < width); \ @@ -11,8 +13,8 @@ assert(y < height); \ assert(pixels != NULL) -CanvasPortion::CanvasPortion(CanvasPreview* preview): - preview(preview) +CanvasPortion::CanvasPortion(int index, CanvasPreview* preview): + index(index), preview(preview) { width = 1; height = 1; @@ -70,15 +72,38 @@ void CanvasPortion::preparePixels() clear(); } -void CanvasPortion::discardPixels() +void CanvasPortion::discardPixels(bool save) { if (pixels) { + if (save) + { + saveToDisk(); + } delete[] pixels; pixels = NULL; } } +void CanvasPortion::saveToDisk() +{ + if (pixels) + { + std::string filepath = FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + ".dat"); + PackStream stream; + stream.bindToFile(filepath, true); + stream.write(&width); + stream.write(&height); + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + pixels[y * width + x].getComposite().save(&stream); + } + } + } +} + void CanvasPortion::pushFragment(int x, int y, const CanvasFragment &fragment) { CHECK_COORDINATES(); diff --git a/src/render/software/CanvasPortion.h b/src/render/software/CanvasPortion.h index 59d0153..326b826 100644 --- a/src/render/software/CanvasPortion.h +++ b/src/render/software/CanvasPortion.h @@ -16,8 +16,8 @@ namespace software { class SOFTWARESHARED_EXPORT CanvasPortion { public: - CanvasPortion(CanvasPreview* preview=NULL); - ~CanvasPortion(); + CanvasPortion(int index=0, CanvasPreview *preview=NULL); + virtual ~CanvasPortion(); inline int getWidth() const {return width;} inline int getHeight() const {return height;} @@ -36,8 +36,15 @@ public: /** * Discard the memory used by pixels. + * + * If save is true, the portion will be saved to disk before. */ - void discardPixels(); + void discardPixels(bool save=true); + + /** + * Save the portion to a picture file on disk. + */ + void saveToDisk(); /** * Add a fragment to the pixel located at (x, y). @@ -61,12 +68,13 @@ public: void setColor(int x, int y, const Color &color); private: + int index; int width; int height; int xoffset; int yoffset; CanvasPixel *pixels; - CanvasPreview* preview; + CanvasPreview *preview; }; } diff --git a/src/system/FileSystem.cpp b/src/system/FileSystem.cpp new file mode 100644 index 0000000..2ee3e45 --- /dev/null +++ b/src/system/FileSystem.cpp @@ -0,0 +1,8 @@ +#include "FileSystem.h" + +#include + +std::string FileSystem::getTempFile(const std::string &filename) +{ + return QDir::temp().filePath(QString::fromStdString(filename)).toStdString(); +} diff --git a/src/system/FileSystem.h b/src/system/FileSystem.h new file mode 100644 index 0000000..afd2b3f --- /dev/null +++ b/src/system/FileSystem.h @@ -0,0 +1,23 @@ +#ifndef FILESYSTEM_H +#define FILESYSTEM_H + +#include "system_global.h" + +namespace paysages { +namespace system { + +class SYSTEMSHARED_EXPORT FileSystem +{ +public: + /** + * Get an absolute path to a temporary file. + * + * filename must not contain directory separators. + */ + static std::string getTempFile(const std::string &filename); +}; + +} +} + +#endif // FILESYSTEM_H diff --git a/src/system/system.pro b/src/system/system.pro index ceac911..0c1de8c 100644 --- a/src/system/system.pro +++ b/src/system/system.pro @@ -26,7 +26,8 @@ SOURCES += \ Logs.cpp \ ParallelPool.cpp \ ParallelWorker.cpp \ - Semaphore.cpp + Semaphore.cpp \ + FileSystem.cpp HEADERS += \ system_global.h \ @@ -42,7 +43,8 @@ HEADERS += \ Logs.h \ ParallelPool.h \ ParallelWorker.h \ - Semaphore.h + Semaphore.h \ + FileSystem.h unix:!symbian { maemo5 { diff --git a/src/system/system_global.h b/src/system/system_global.h index 80b4d3c..769b128 100644 --- a/src/system/system_global.h +++ b/src/system/system_global.h @@ -21,6 +21,7 @@ namespace system { class Thread; class Mutex; class Semaphore; + class PictureWriter; } } using namespace paysages::system;