Added canvas portion writing to disk

This commit is contained in:
Michaël Lemaire 2014-08-21 09:58:11 +02:00
parent 13904be001
commit 0bb1cf5bed
7 changed files with 79 additions and 11 deletions

View file

@ -11,7 +11,7 @@ Canvas::Canvas()
vertical_portion_count = 1; vertical_portion_count = 1;
width = 1; width = 1;
height = 1; height = 1;
portions.push_back(new CanvasPortion()); portions.push_back(new CanvasPortion);
preview = new CanvasPreview; preview = new CanvasPreview;
} }
@ -41,13 +41,14 @@ void Canvas::setSize(int width, int height)
int done_width = 0; int done_width = 0;
int done_height = 0; int done_height = 0;
int index = 0;
for (int y = 0; y < vertical_portion_count; y++) for (int y = 0; y < vertical_portion_count; y++)
{ {
done_width = 0; done_width = 0;
for (int x = 0; x < horizontal_portion_count; x++) 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, portion->setSize((x == horizontal_portion_count - 1) ? width - done_width : portion_width,
(y == vertical_portion_count - 1) ? height - done_height : portion_height, (y == vertical_portion_count - 1) ? height - done_height : portion_height,

View file

@ -4,6 +4,8 @@
#include "CanvasPixel.h" #include "CanvasPixel.h"
#include "CanvasPreview.h" #include "CanvasPreview.h"
#include "PackStream.h"
#include "FileSystem.h"
#define CHECK_COORDINATES() assert(x >= 0); \ #define CHECK_COORDINATES() assert(x >= 0); \
assert(x < width); \ assert(x < width); \
@ -11,8 +13,8 @@
assert(y < height); \ assert(y < height); \
assert(pixels != NULL) assert(pixels != NULL)
CanvasPortion::CanvasPortion(CanvasPreview* preview): CanvasPortion::CanvasPortion(int index, CanvasPreview* preview):
preview(preview) index(index), preview(preview)
{ {
width = 1; width = 1;
height = 1; height = 1;
@ -70,15 +72,38 @@ void CanvasPortion::preparePixels()
clear(); clear();
} }
void CanvasPortion::discardPixels() void CanvasPortion::discardPixels(bool save)
{ {
if (pixels) if (pixels)
{ {
if (save)
{
saveToDisk();
}
delete[] pixels; delete[] pixels;
pixels = NULL; 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) void CanvasPortion::pushFragment(int x, int y, const CanvasFragment &fragment)
{ {
CHECK_COORDINATES(); CHECK_COORDINATES();

View file

@ -16,8 +16,8 @@ namespace software {
class SOFTWARESHARED_EXPORT CanvasPortion class SOFTWARESHARED_EXPORT CanvasPortion
{ {
public: public:
CanvasPortion(CanvasPreview* preview=NULL); CanvasPortion(int index=0, CanvasPreview *preview=NULL);
~CanvasPortion(); virtual ~CanvasPortion();
inline int getWidth() const {return width;} inline int getWidth() const {return width;}
inline int getHeight() const {return height;} inline int getHeight() const {return height;}
@ -36,8 +36,15 @@ public:
/** /**
* Discard the memory used by pixels. * 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). * Add a fragment to the pixel located at (x, y).
@ -61,12 +68,13 @@ public:
void setColor(int x, int y, const Color &color); void setColor(int x, int y, const Color &color);
private: private:
int index;
int width; int width;
int height; int height;
int xoffset; int xoffset;
int yoffset; int yoffset;
CanvasPixel *pixels; CanvasPixel *pixels;
CanvasPreview* preview; CanvasPreview *preview;
}; };
} }

View file

@ -0,0 +1,8 @@
#include "FileSystem.h"
#include <QDir>
std::string FileSystem::getTempFile(const std::string &filename)
{
return QDir::temp().filePath(QString::fromStdString(filename)).toStdString();
}

23
src/system/FileSystem.h Normal file
View file

@ -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

View file

@ -26,7 +26,8 @@ SOURCES += \
Logs.cpp \ Logs.cpp \
ParallelPool.cpp \ ParallelPool.cpp \
ParallelWorker.cpp \ ParallelWorker.cpp \
Semaphore.cpp Semaphore.cpp \
FileSystem.cpp
HEADERS += \ HEADERS += \
system_global.h \ system_global.h \
@ -42,7 +43,8 @@ HEADERS += \
Logs.h \ Logs.h \
ParallelPool.h \ ParallelPool.h \
ParallelWorker.h \ ParallelWorker.h \
Semaphore.h Semaphore.h \
FileSystem.h
unix:!symbian { unix:!symbian {
maemo5 { maemo5 {

View file

@ -21,6 +21,7 @@ namespace system {
class Thread; class Thread;
class Mutex; class Mutex;
class Semaphore; class Semaphore;
class PictureWriter;
} }
} }
using namespace paysages::system; using namespace paysages::system;