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); \
|
2014-08-18 15:16:17 +00:00
|
|
|
assert(y < height); \
|
|
|
|
assert(pixels != NULL)
|
2014-06-10 13:13:16 +00:00
|
|
|
|
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;
|
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-18 15:16:17 +00:00
|
|
|
clear();
|
2014-06-10 13:13:16 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:33:15 +00:00
|
|
|
void CanvasPortion::discardPixels()
|
|
|
|
{
|
|
|
|
if (pixels)
|
|
|
|
{
|
|
|
|
delete[] pixels;
|
|
|
|
pixels = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|