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;
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,

View file

@ -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();

View file

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

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 \
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 {

View file

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