aseprite/src/doc/mask.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.4 KiB
C
Raw Normal View History

// Aseprite Document Library
2024-08-14 03:33:13 +08:00
// Copyright (c) 2020-2024 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// 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
#include "doc/image.h"
#include "doc/image_buffer.h"
#include "doc/image_ref.h"
#include "doc/object.h"
#include "doc/primitives.h"
#include "gfx/rect.h"
2007-09-19 07:57:02 +08:00
#include <string>
namespace doc {
// Represents the selection (selected pixels, 0/1, 0=non-selected, 1=selected)
//
// TODO rename Mask -> Selection
class Mask : public Object {
public:
Mask();
Mask(const Mask& mask);
virtual ~Mask();
2024-12-17 01:52:19 +08:00
virtual int getMemSize() const override;
2024-12-17 01:52:19 +08:00
void setName(const char* name);
const std::string& name() const { return m_name; }
2024-12-17 01:52:19 +08:00
const Image* bitmap() const { return m_bitmap.get(); }
Image* bitmap() { return m_bitmap.get(); }
2024-12-17 01:52:19 +08:00
// Returns true if the mask is completely empty (i.e. nothing
// selected)
bool isEmpty() const { return (!m_bitmap ? true : false); }
2024-12-17 01:52:19 +08:00
// Returns true if the point is inside the mask
bool containsPoint(int u, int v) const
{
return (m_bitmap.get() && u >= m_bounds.x && u < m_bounds.x + m_bounds.w && v >= m_bounds.y &&
v < m_bounds.y + m_bounds.h &&
get_pixel(m_bitmap.get(), u - m_bounds.x, v - m_bounds.y));
}
2024-12-17 01:52:19 +08:00
gfx::Point origin() const { return m_bounds.origin(); }
const gfx::Rect& bounds() const { return m_bounds; }
2024-12-17 01:52:19 +08:00
void setOrigin(int x, int y)
{
m_bounds.x = x;
m_bounds.y = y;
}
2024-12-17 01:52:19 +08:00
// 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();
2024-12-17 01:52:19 +08:00
// Returns true if the mask is frozen (See freeze/unfreeze functions).
bool isFrozen() const { return m_freeze_count > 0; }
2024-12-17 01:52:19 +08:00
// Returns true if the mask is a rectangular region.
bool isRectangular() const;
2024-12-17 01:52:19 +08:00
// Clears the mask.
void clear();
2024-12-17 01:52:19 +08:00
// Copies the data from the given mask.
void copyFrom(const Mask* sourceMask);
void fromImage(const Image* image, const gfx::Point& maskOrigin, uint8_t alphaThreshold = 0);
2024-12-17 01:52:19 +08:00
// Replace the whole mask with the given region.
void replace(const gfx::Rect& bounds);
void replace(const doc::Mask& sourceMask) { copyFrom(&sourceMask); }
2024-12-17 01:52:19 +08:00
// Inverts the mask.
void invert();
2024-12-17 01:52:19 +08:00
void add(const doc::Mask& mask);
void subtract(const doc::Mask& mask);
void intersect(const doc::Mask& mask);
2024-12-17 01:52:19 +08:00
void add(const gfx::Rect& bounds);
void subtract(const gfx::Rect& bounds);
void intersect(const gfx::Rect& bounds);
2024-12-17 01:52:19 +08:00
void byColor(const Image* image, int color, int fuzziness);
void crop(const Image* image);
2024-12-17 01:52:19 +08:00
// Reserves a rectangle to draw onto the bitmap (you should call
// shrink after you draw in the bitmap)
void reserve(const gfx::Rect& bounds);
2024-12-17 01:52:19 +08:00
// Shrinks all sides of the mask to the minimum possible looking at
// empty pixels in the bitmap
void shrink();
2024-12-17 01:52:19 +08:00
// Displaces the mask bounds origin point.
void offsetOrigin(int dx, int dy);
2024-12-17 01:52:19 +08:00
private:
void initialize();
2024-12-17 01:52:19 +08:00
int m_freeze_count;
std::string m_name; // Mask name
gfx::Rect m_bounds; // Region bounds
ImageRef m_bitmap; // Bitmapped image mask
ImageBufferPtr m_buffer; // Buffer used in m_bitmap
2024-12-17 01:52:19 +08:00
Mask& operator=(const Mask& mask);
};
2024-12-17 01:52:19 +08:00
typedef std::shared_ptr<Mask> MaskRef;
} // namespace doc
2007-09-19 07:57:02 +08:00
2009-08-18 05:38:00 +08:00
#endif