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-06-10 13:13:16 +00:00
|
|
|
|
|
|
|
#define CHECK_COORDINATES() assert(x >= 0); \
|
|
|
|
assert(x < width); \
|
|
|
|
assert(y >= 0); \
|
|
|
|
assert(y < height)
|
|
|
|
|
2014-06-12 15:45:59 +00:00
|
|
|
CanvasPortion::CanvasPortion(CanvasPreview* preview):
|
|
|
|
preview(preview)
|
2014-06-05 15:12:49 +00:00
|
|
|
{
|
2014-06-10 13:13:16 +00:00
|
|
|
width = 1;
|
|
|
|
height = 1;
|
|
|
|
pixels = new CanvasPixel[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasPortion::~CanvasPortion()
|
|
|
|
{
|
|
|
|
delete[] pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasPortion::setSize(int width, int height)
|
|
|
|
{
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
2014-06-10 13:13:16 +00:00
|
|
|
|
|
|
|
delete[] pixels;
|
|
|
|
pixels = new CanvasPixel[width * height];
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
preview->pushPixel(x, y, old_color, pixel.getComposite());
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
preview->pushPixel(x, y, old_color, pixel.getComposite());
|
|
|
|
}
|
|
|
|
}
|