Change Mask member functions to use gfx::Rect instead of x,y,w,h args

This commit is contained in:
David Capello 2014-12-13 19:10:54 -03:00
parent 9f0d460624
commit f66b48c698
12 changed files with 77 additions and 106 deletions

View File

@ -82,13 +82,11 @@ void InvertMaskCommand::onExecute(Context* context)
if (undo.isEnabled()) if (undo.isEnabled())
undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document)); undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document));
/* create a new mask */ // Select all the sprite area
base::UniquePtr<Mask> mask(new Mask()); base::UniquePtr<Mask> mask(new Mask());
mask->replace(sprite->bounds());
/* select all the sprite area */ // Remove in the new mask the current sprite marked region
mask->replace(0, 0, sprite->width(), sprite->height());
/* remove in the new mask the current sprite marked region */
const gfx::Rect& maskBounds = document->mask()->bounds(); const gfx::Rect& maskBounds = document->mask()->bounds();
doc::fill_rect(mask->bitmap(), doc::fill_rect(mask->bitmap(),
maskBounds.x, maskBounds.y, maskBounds.x, maskBounds.y,

View File

@ -65,7 +65,7 @@ void MaskAllCommand::onExecute(Context* context)
undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document)); undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document));
// Change the selection // Change the selection
document->mask()->replace(0, 0, sprite->width(), sprite->height()); document->mask()->replace(sprite->bounds());
document->setMaskVisible(true); document->setMaskVisible(true);
document->resetTransformation(); document->resetTransformation();

View File

@ -155,9 +155,10 @@ protected:
} }
// create the new rotated mask // create the new rotated mask
new_mask->replace(x, y, new_mask->replace(
gfx::Rect(x, y,
m_angle == 180 ? origBounds.w: origBounds.h, m_angle == 180 ? origBounds.w: origBounds.h,
m_angle == 180 ? origBounds.h: origBounds.w); m_angle == 180 ? origBounds.h: origBounds.w));
doc::rotate_image(origMask->bitmap(), new_mask->bitmap(), m_angle); doc::rotate_image(origMask->bitmap(), new_mask->bitmap(), m_angle);
// Copy new mask // Copy new mask

View File

@ -132,8 +132,10 @@ protected:
int w = scale_x(old_bitmap->width()); int w = scale_x(old_bitmap->width());
int h = scale_y(old_bitmap->height()); int h = scale_y(old_bitmap->height());
base::UniquePtr<Mask> new_mask(new Mask); base::UniquePtr<Mask> new_mask(new Mask);
new_mask->replace(scale_x(m_document->mask()->bounds().x-1), new_mask->replace(
scale_y(m_document->mask()->bounds().y-1), MAX(1, w), MAX(1, h)); gfx::Rect(
scale_x(m_document->mask()->bounds().x-1),
scale_y(m_document->mask()->bounds().y-1), MAX(1, w), MAX(1, h)));
algorithm::resize_image(old_bitmap, new_mask->bitmap(), algorithm::resize_image(old_bitmap, new_mask->bitmap(),
m_resize_method, m_resize_method,
m_sprite->getPalette(FrameNumber(0)), // Ignored m_sprite->getPalette(FrameNumber(0)), // Ignored

View File

@ -116,9 +116,10 @@ void FilterManagerImpl::beginForPreview()
m_preview_mask.reset(new Mask(*document->mask())); m_preview_mask.reset(new Mask(*document->mask()));
else { else {
m_preview_mask.reset(new Mask()); m_preview_mask.reset(new Mask());
m_preview_mask->replace(m_offset_x, m_offset_y, m_preview_mask->replace(
gfx::Rect(m_offset_x, m_offset_y,
m_src->width(), m_src->width(),
m_src->height()); m_src->height()));
} }
m_row = 0; m_row = 0;

View File

@ -1218,7 +1218,7 @@ static Mask* ase_file_read_mask_chunk(FILE* f)
mask = new Mask(); mask = new Mask();
mask->setName(name.c_str()); mask->setName(name.c_str());
mask->replace(x, y, w, h); mask->replace(gfx::Rect(x, y, w, h));
// Read image data // Read image data
for (v=0; v<h; v++) for (v=0; v<h; v++)

View File

@ -288,10 +288,12 @@ public:
switch (loop->getSelectionMode()) { switch (loop->getSelectionMode()) {
case kDefaultSelectionMode: case kDefaultSelectionMode:
case kAddSelectionMode: case kAddSelectionMode:
loop->getMask()->add(x1-offset.x, y-offset.y, x2-x1+1, 1); loop->getMask()->add(
gfx::Rect(x1-offset.x, y-offset.y, x2-x1+1, 1));
break; break;
case kSubtractSelectionMode: case kSubtractSelectionMode:
loop->getMask()->subtract(x1-offset.x, y-offset.y, x2-x1+1, 1); loop->getMask()->subtract(
gfx::Rect(x1-offset.x, y-offset.y, x2-x1+1, 1));
break; break;
} }
} }
@ -312,7 +314,7 @@ public:
undo->pushUndoer(new undoers::SetMask(undo->getObjects(), loop->getDocument())); undo->pushUndoer(new undoers::SetMask(undo->getObjects(), loop->getDocument()));
loop->getMask()->freeze(); loop->getMask()->freeze();
loop->getMask()->reserve(0, 0, loop->sprite()->width(), loop->sprite()->height()); loop->getMask()->reserve(loop->sprite()->bounds());
} }
else { else {
loop->getMask()->unfreeze(); loop->getMask()->unfreeze();

View File

@ -588,7 +588,7 @@ void PixelsMovement::redrawCurrentMask()
// Transform mask // Transform mask
m_currentMask->replace(0, 0, m_sprite->width(), m_sprite->height()); m_currentMask->replace(m_sprite->bounds());
m_currentMask->freeze(); m_currentMask->freeze();
clear_image(m_currentMask->bitmap(), 0); clear_image(m_currentMask->bitmap(), 0);
drawParallelogram(m_currentMask->bitmap(), m_initialMask->bitmap(), drawParallelogram(m_currentMask->bitmap(), m_initialMask->bitmap(),

View File

@ -60,7 +60,7 @@ Mask* load_msk_file(const char* filename)
// Animator MSK format // Animator MSK format
else if (orig_size == 8000) { else if (orig_size == 8000) {
mask = new Mask(); mask = new Mask();
mask->replace(0, 0, 320, 200); mask->replace(gfx::Rect(0, 0, 320, 200));
u = v = 0; u = v = 0;
for (i=0; i<8000; i++) { for (i=0; i<8000; i++) {

View File

@ -136,69 +136,36 @@ void Mask::invert()
} }
} }
void Mask::replace(int x, int y, int w, int h) void Mask::replace(const gfx::Rect& bounds)
{ {
m_bounds = gfx::Rect(x, y, w, h); m_bounds = bounds;
delete m_bitmap; delete m_bitmap;
m_bitmap = Image::create(IMAGE_BITMAP, w, h); m_bitmap = Image::create(IMAGE_BITMAP, bounds.w, bounds.h);
clear_image(m_bitmap, 1); clear_image(m_bitmap, 1);
} }
void Mask::replace(const gfx::Rect& bounds)
{
replace(bounds.x, bounds.y, bounds.w, bounds.h);
}
void Mask::add(int x, int y, int w, int h)
{
if (m_freeze_count == 0)
reserve(x, y, w, h);
fill_rect(m_bitmap,
x-m_bounds.x, y-m_bounds.y,
x-m_bounds.x+w-1, y-m_bounds.y+h-1, 1);
}
void Mask::add(const gfx::Rect& bounds) void Mask::add(const gfx::Rect& bounds)
{ {
add(bounds.x, bounds.y, bounds.w, bounds.h); if (m_freeze_count == 0)
} reserve(bounds);
void Mask::subtract(int x, int y, int w, int h)
{
if (m_bitmap) {
fill_rect(m_bitmap, fill_rect(m_bitmap,
x-m_bounds.x, bounds.x-m_bounds.x,
y-m_bounds.y, bounds.y-m_bounds.y,
x-m_bounds.x+w-1, bounds.x-m_bounds.x+bounds.w-1,
y-m_bounds.y+h-1, 0); bounds.y-m_bounds.y+bounds.h-1, 1);
shrink();
}
} }
void Mask::subtract(const gfx::Rect& bounds) void Mask::subtract(const gfx::Rect& bounds)
{
subtract(bounds.x, bounds.y, bounds.w, bounds.h);
}
void Mask::intersect(int x, int y, int w, int h)
{ {
if (m_bitmap) { if (m_bitmap) {
int x1 = m_bounds.x; fill_rect(m_bitmap,
int y1 = m_bounds.y; bounds.x-m_bounds.x,
int x2 = MIN(m_bounds.x+m_bounds.w-1, x+w-1); bounds.y-m_bounds.y,
int y2 = MIN(m_bounds.y+m_bounds.h-1, y+h-1); bounds.x-m_bounds.x+bounds.w-1,
bounds.y-m_bounds.y+bounds.h-1, 0);
m_bounds.x = MAX(x, x1);
m_bounds.y = MAX(y, y1);
m_bounds.w = x2 - m_bounds.x + 1;
m_bounds.h = y2 - m_bounds.y + 1;
Image* image = crop_image(m_bitmap, m_bounds.x-x1, m_bounds.y-y1, m_bounds.w, m_bounds.h, 0);
delete m_bitmap;
m_bitmap = image;
shrink(); shrink();
} }
@ -206,12 +173,30 @@ void Mask::intersect(int x, int y, int w, int h)
void Mask::intersect(const gfx::Rect& bounds) void Mask::intersect(const gfx::Rect& bounds)
{ {
intersect(bounds.x, bounds.y, bounds.w, bounds.h); if (m_bitmap) {
gfx::Rect newBounds = m_bounds.createIntersect(bounds);
Image* image = NULL;
if (!newBounds.isEmpty()) {
image = crop_image(m_bitmap,
newBounds.x-m_bounds.x,
newBounds.y-m_bounds.y,
newBounds.w,
newBounds.h, 0);
}
delete m_bitmap;
m_bitmap = image;
m_bounds = newBounds;
shrink();
}
} }
void Mask::byColor(const Image *src, int color, int fuzziness) void Mask::byColor(const Image *src, int color, int fuzziness)
{ {
replace(0, 0, src->width(), src->height()); replace(src->bounds());
Image* dst = m_bitmap; Image* dst = m_bitmap;
@ -371,47 +356,34 @@ void Mask::crop(const Image *image)
get_pixel(image, c, y2)); get_pixel(image, c, y2));
if (done_count < 4) if (done_count < 4)
intersect(x1, y1, x2-x1+1, y2-y1+1); intersect(gfx::Rect(x1, y1, x2-x1+1, y2-y1+1));
else else
clear(); clear();
#undef ADVANCE #undef ADVANCE
} }
void Mask::reserve(int x, int y, int w, int h) void Mask::reserve(const gfx::Rect& bounds)
{ {
ASSERT(w > 0 && h > 0); ASSERT(!bounds.isEmpty());
if (!m_bitmap) { if (!m_bitmap) {
m_bounds.x = x; m_bounds = bounds;
m_bounds.y = y; m_bitmap = Image::create(IMAGE_BITMAP, bounds.w, bounds.h);
m_bounds.w = w;
m_bounds.h = h;
m_bitmap = Image::create(IMAGE_BITMAP, w, h);
clear_image(m_bitmap, 0); clear_image(m_bitmap, 0);
} }
else { else {
int x1 = m_bounds.x; gfx::Rect newBounds = m_bounds.createUnion(bounds);
int y1 = m_bounds.y;
int x2 = MAX(m_bounds.x+m_bounds.w-1, x+w-1);
int y2 = MAX(m_bounds.y+m_bounds.h-1, y+h-1);
int new_mask_x = MIN(x, x1);
int new_mask_y = MIN(y, y1);
int new_mask_w = x2 - new_mask_x + 1;
int new_mask_h = y2 - new_mask_y + 1;
if (m_bounds.x != new_mask_x || if (m_bounds != newBounds) {
m_bounds.y != new_mask_y || Image* image = crop_image(m_bitmap,
m_bounds.w != new_mask_w || newBounds.x-m_bounds.x,
m_bounds.h != new_mask_h) { newBounds.y-m_bounds.y,
m_bounds.x = new_mask_x; newBounds.w,
m_bounds.y = new_mask_y; newBounds.h, 0);
m_bounds.w = new_mask_w;
m_bounds.h = new_mask_h;
Image* image = crop_image(m_bitmap, m_bounds.x-x1, m_bounds.y-y1, m_bounds.w, m_bounds.h, 0);
delete m_bitmap; // image delete m_bitmap; // image
m_bitmap = image; m_bitmap = image;
m_bounds = newBounds;
} }
} }
} }

View File

@ -73,26 +73,21 @@ namespace doc {
void copyFrom(const Mask* sourceMask); void copyFrom(const Mask* sourceMask);
// Replace the whole mask with the given region. // Replace the whole mask with the given region.
void replace(int x, int y, int w, int h);
void replace(const gfx::Rect& bounds); void replace(const gfx::Rect& bounds);
// Inverts the mask. // Inverts the mask.
void invert(); void invert();
// Adds the specified rectangle in the mask/selection // Adds the specified rectangle in the mask/selection
void add(int x, int y, int w, int h);
void add(const gfx::Rect& bounds); void add(const gfx::Rect& bounds);
void subtract(int x, int y, int w, int h);
void subtract(const gfx::Rect& bounds); void subtract(const gfx::Rect& bounds);
void intersect(int x, int y, int w, int h);
void intersect(const gfx::Rect& bounds); void intersect(const gfx::Rect& bounds);
void byColor(const Image* image, int color, int fuzziness); void byColor(const Image* image, int color, int fuzziness);
void crop(const Image* image); void crop(const Image* image);
// Reserves a rectangle to draw onto the bitmap (you should call // Reserves a rectangle to draw onto the bitmap (you should call
// shrink after you draw in the bitmap) // shrink after you draw in the bitmap)
void reserve(int x, int y, int w, int h); void reserve(const gfx::Rect& bounds);
// Shrinks all sides of the mask to the minimum possible looking at // Shrinks all sides of the mask to the minimum possible looking at
// empty pixels in the bitmap // empty pixels in the bitmap

View File

@ -58,7 +58,7 @@ Mask* read_mask(std::istream& is)
if (w > 0 && h > 0) { if (w > 0 && h > 0) {
int size = BitmapTraits::getRowStrideBytes(w); int size = BitmapTraits::getRowStrideBytes(w);
mask->add(x, y, w, h); mask->add(gfx::Rect(x, y, w, h));
for (int c=0; c<mask->bounds().h; c++) for (int c=0; c<mask->bounds().h; c++)
is.read((char*)mask->bitmap()->getPixelAddress(0, c), size); is.read((char*)mask->bitmap()->getPixelAddress(0, c), size);
} }