2014-06-05 15:12:49 +00:00
|
|
|
#include "CanvasPortion.h"
|
|
|
|
|
2014-06-10 13:13:16 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "CanvasPixel.h"
|
2014-06-12 15:45:59 +00:00
|
|
|
#include "CanvasPreview.h"
|
2014-08-21 07:58:11 +00:00
|
|
|
#include "PackStream.h"
|
|
|
|
#include "FileSystem.h"
|
2014-06-10 13:13:16 +00:00
|
|
|
|
|
|
|
#define CHECK_COORDINATES() assert(x >= 0); \
|
|
|
|
assert(x < width); \
|
|
|
|
assert(y >= 0); \
|
2014-08-18 15:16:17 +00:00
|
|
|
assert(y < height); \
|
|
|
|
assert(pixels != NULL)
|
2014-06-10 13:13:16 +00:00
|
|
|
|
2014-08-21 07:58:11 +00:00
|
|
|
CanvasPortion::CanvasPortion(int index, CanvasPreview* preview):
|
|
|
|
index(index), preview(preview)
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-06-10 13:13:16 +00:00
|
|
|
width = 1;
|
|
|
|
height = 1;
|
2014-08-18 15:33:15 +00:00
|
|
|
xoffset = 0;
|
|
|
|
yoffset = 0;
|
2014-08-18 15:16:17 +00:00
|
|
|
pixels = NULL;
|
2014-06-10 13:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CanvasPortion::~CanvasPortion()
|
|
|
|
{
|
2014-08-18 15:16:17 +00:00
|
|
|
if (pixels)
|
|
|
|
{
|
|
|
|
delete[] pixels;
|
|
|
|
}
|
2014-06-10 13:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CanvasPortion::getFragmentCount(int x, int y) const
|
|
|
|
{
|
|
|
|
CHECK_COORDINATES();
|
|
|
|
|
|
|
|
return pixels[y * width + x].getFragmentCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CanvasFragment *CanvasPortion::getFrontFragment(int x, int y) const
|
|
|
|
{
|
|
|
|
CHECK_COORDINATES();
|
|
|
|
|
|
|
|
return pixels[y * width + x].getFrontFragment();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasPortion::clear()
|
|
|
|
{
|
|
|
|
int n = width * height;
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
pixels[i].reset();
|
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:33:15 +00:00
|
|
|
void CanvasPortion::setSize(int width, int height, int xoffset, int yoffset)
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
2014-08-18 15:33:15 +00:00
|
|
|
this->xoffset = xoffset;
|
|
|
|
this->yoffset = yoffset;
|
2014-08-18 15:16:17 +00:00
|
|
|
}
|
2014-06-10 13:13:16 +00:00
|
|
|
|
2014-08-18 15:16:17 +00:00
|
|
|
void CanvasPortion::preparePixels()
|
|
|
|
{
|
|
|
|
if (pixels)
|
|
|
|
{
|
|
|
|
delete[] pixels;
|
|
|
|
}
|
2014-06-10 13:13:16 +00:00
|
|
|
pixels = new CanvasPixel[width * height];
|
|
|
|
}
|
|
|
|
|
2014-08-21 07:58:11 +00:00
|
|
|
void CanvasPortion::discardPixels(bool save)
|
2014-08-18 15:33:15 +00:00
|
|
|
{
|
|
|
|
if (pixels)
|
|
|
|
{
|
2014-08-21 07:58:11 +00:00
|
|
|
if (save)
|
|
|
|
{
|
|
|
|
saveToDisk();
|
|
|
|
}
|
2014-08-18 15:33:15 +00:00
|
|
|
delete[] pixels;
|
|
|
|
pixels = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 07:58:11 +00:00
|
|
|
void CanvasPortion::saveToDisk()
|
|
|
|
{
|
|
|
|
if (pixels)
|
|
|
|
{
|
2014-08-21 10:36:28 +00:00
|
|
|
filepath = FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + ".dat");
|
2014-08-21 07:58:11 +00:00
|
|
|
PackStream stream;
|
|
|
|
stream.bindToFile(filepath, true);
|
|
|
|
stream.write(&width);
|
|
|
|
stream.write(&height);
|
2014-08-21 10:36:28 +00:00
|
|
|
for (int y = 0; y < height; y++)
|
2014-08-21 07:58:11 +00:00
|
|
|
{
|
2014-08-21 10:36:28 +00:00
|
|
|
for (int x = 0; x < width; x++)
|
2014-08-21 07:58:11 +00:00
|
|
|
{
|
|
|
|
pixels[y * width + x].getComposite().save(&stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 10:36:28 +00:00
|
|
|
bool CanvasPortion::getReadStream(PackStream &stream, int x, int y)
|
|
|
|
{
|
|
|
|
if (FileSystem::isFile(filepath))
|
|
|
|
{
|
2014-08-21 13:30:09 +00:00
|
|
|
if (not stream.bindToFile(filepath))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-21 10:36:28 +00:00
|
|
|
int unused_i;
|
|
|
|
stream.skip(unused_i, 2);
|
2014-08-21 13:30:09 +00:00
|
|
|
|
|
|
|
if (x > 0 or y > 0)
|
|
|
|
{
|
|
|
|
double unused_d;
|
|
|
|
stream.skip(unused_d, (y * width + x) * 4);
|
|
|
|
}
|
|
|
|
|
2014-08-21 10:36:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:13:16 +00:00
|
|
|
void CanvasPortion::pushFragment(int x, int y, const CanvasFragment &fragment)
|
|
|
|
{
|
|
|
|
CHECK_COORDINATES();
|
|
|
|
|
|
|
|
CanvasPixel &pixel = pixels[y * width + x];
|
2014-06-12 15:45:59 +00:00
|
|
|
Color old_color = pixel.getComposite();
|
|
|
|
|
2014-06-10 13:13:16 +00:00
|
|
|
pixel.pushFragment(fragment);
|
2014-06-12 15:45:59 +00:00
|
|
|
|
|
|
|
if (preview)
|
|
|
|
{
|
2014-08-18 15:33:15 +00:00
|
|
|
preview->pushPixel(xoffset + x, yoffset + y, old_color, pixel.getComposite());
|
2014-06-12 15:45:59 +00:00
|
|
|
}
|
2014-06-05 15:12:49 +00:00
|
|
|
}
|
2014-08-18 10:17:16 +00:00
|
|
|
|
|
|
|
const CanvasPixel &CanvasPortion::at(int x, int y)
|
|
|
|
{
|
|
|
|
CHECK_COORDINATES();
|
|
|
|
|
|
|
|
return pixels[y * width + x];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasPortion::setColor(int x, int y, const Color &color)
|
|
|
|
{
|
|
|
|
CHECK_COORDINATES();
|
|
|
|
|
|
|
|
CanvasPixel &pixel = pixels[y * width + x];
|
|
|
|
Color old_color = pixel.getComposite();
|
|
|
|
|
|
|
|
pixel.setComposite(color);
|
|
|
|
|
|
|
|
if (preview)
|
|
|
|
{
|
2014-08-18 15:33:15 +00:00
|
|
|
preview->pushPixel(xoffset + x, yoffset + y, old_color, pixel.getComposite());
|
2014-08-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
}
|