2014-10-21 09:21:31 +08:00
|
|
|
// Aseprite Document Library
|
2018-03-16 03:05:56 +08:00
|
|
|
// Copyright (c) 2001-2018 David Capello
|
2014-10-21 09:21:31 +08:00
|
|
|
//
|
|
|
|
// This file is released under the terms of the MIT license.
|
|
|
|
// Read LICENSE.txt for more information.
|
|
|
|
|
|
|
|
#ifndef DOC_MASK_H_INCLUDED
|
|
|
|
#define DOC_MASK_H_INCLUDED
|
2014-03-30 06:40:17 +08:00
|
|
|
#pragma once
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
2014-12-14 06:43:14 +08:00
|
|
|
#include "doc/image_buffer.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "doc/image_ref.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/object.h"
|
|
|
|
#include "doc/primitives.h"
|
2014-12-14 06:43:14 +08:00
|
|
|
#include "gfx/rect.h"
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-01-09 09:34:36 +08:00
|
|
|
#include <string>
|
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
namespace doc {
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
// Represents the selection (selected pixels, 0/1, 0=non-selected, 1=selected)
|
2018-05-31 05:05:18 +08:00
|
|
|
//
|
|
|
|
// TODO rename Mask -> Selection
|
2013-11-10 06:59:05 +08:00
|
|
|
class Mask : public Object {
|
2013-08-06 08:20:19 +08:00
|
|
|
public:
|
|
|
|
Mask();
|
|
|
|
Mask(const Mask& mask);
|
|
|
|
virtual ~Mask();
|
|
|
|
|
2014-11-17 08:32:18 +08:00
|
|
|
virtual int getMemSize() const override;
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
void setName(const char *name);
|
2014-07-30 12:28:15 +08:00
|
|
|
const std::string& name() const { return m_name; }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-04-03 07:42:43 +08:00
|
|
|
const Image* bitmap() const { return m_bitmap.get(); }
|
|
|
|
Image* bitmap() { return m_bitmap.get(); }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
// Returns true if the mask is completely empty (i.e. nothing
|
|
|
|
// selected)
|
|
|
|
bool isEmpty() const {
|
|
|
|
return (!m_bitmap ? true: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the point is inside the mask
|
|
|
|
bool containsPoint(int u, int v) const {
|
2015-04-03 07:42:43 +08:00
|
|
|
return (m_bitmap.get() &&
|
2013-08-06 08:20:19 +08:00
|
|
|
u >= m_bounds.x && u < m_bounds.x+m_bounds.w &&
|
|
|
|
v >= m_bounds.y && v < m_bounds.y+m_bounds.h &&
|
2015-04-03 07:42:43 +08:00
|
|
|
get_pixel(m_bitmap.get(), u-m_bounds.x, v-m_bounds.y));
|
2013-08-06 08:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 12:28:15 +08:00
|
|
|
const gfx::Rect& bounds() const { return m_bounds; }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
void setOrigin(int x, int y) {
|
|
|
|
m_bounds.x = x;
|
|
|
|
m_bounds.y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These functions can be used to disable the automatic call to
|
|
|
|
// "shrink" method (so you can do a lot of modifications without
|
|
|
|
// lossing time shrinking the mask in each little operation).
|
|
|
|
void freeze();
|
|
|
|
void unfreeze();
|
|
|
|
|
|
|
|
// Returns true if the mask is frozen (See freeze/unfreeze functions).
|
|
|
|
bool isFrozen() const { return m_freeze_count > 0; }
|
|
|
|
|
|
|
|
// Returns true if the mask is a rectangular region.
|
|
|
|
bool isRectangular() const;
|
|
|
|
|
|
|
|
// Clears the mask.
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
// Copies the data from the given mask.
|
|
|
|
void copyFrom(const Mask* sourceMask);
|
|
|
|
|
|
|
|
// Replace the whole mask with the given region.
|
|
|
|
void replace(const gfx::Rect& bounds);
|
2018-09-08 01:44:54 +08:00
|
|
|
void replace(const doc::Mask& sourceMask) { copyFrom(&sourceMask); }
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
// Inverts the mask.
|
|
|
|
void invert();
|
|
|
|
|
2018-03-16 03:05:56 +08:00
|
|
|
void add(const doc::Mask& mask);
|
|
|
|
void subtract(const doc::Mask& mask);
|
|
|
|
void intersect(const doc::Mask& mask);
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
void add(const gfx::Rect& bounds);
|
2013-11-10 06:59:05 +08:00
|
|
|
void subtract(const gfx::Rect& bounds);
|
|
|
|
void intersect(const gfx::Rect& bounds);
|
2018-03-16 03:05:56 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
void byColor(const Image* image, int color, int fuzziness);
|
|
|
|
void crop(const Image* image);
|
|
|
|
|
|
|
|
// Reserves a rectangle to draw onto the bitmap (you should call
|
|
|
|
// shrink after you draw in the bitmap)
|
2014-12-14 06:10:54 +08:00
|
|
|
void reserve(const gfx::Rect& bounds);
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
// Shrinks all sides of the mask to the minimum possible looking at
|
|
|
|
// empty pixels in the bitmap
|
|
|
|
void shrink();
|
|
|
|
|
|
|
|
// Displaces the mask bounds origin point.
|
|
|
|
void offsetOrigin(int dx, int dy);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void initialize();
|
|
|
|
|
|
|
|
int m_freeze_count;
|
|
|
|
std::string m_name; // Mask name
|
|
|
|
gfx::Rect m_bounds; // Region bounds
|
2015-01-19 09:05:33 +08:00
|
|
|
ImageRef m_bitmap; // Bitmapped image mask
|
2014-12-14 06:43:14 +08:00
|
|
|
ImageBufferPtr m_buffer; // Buffer used in m_bitmap
|
2015-01-19 09:05:33 +08:00
|
|
|
|
|
|
|
Mask& operator=(const Mask& mask);
|
2013-08-06 08:20:19 +08:00
|
|
|
};
|
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
} // namespace doc
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2009-08-18 05:38:00 +08:00
|
|
|
#endif
|