1 // Aseprite Document Library
2 // Copyright (c) 2001-2018 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef DOC_SPRITE_H_INCLUDED
8 #define DOC_SPRITE_H_INCLUDED
9 #pragma once
10 
11 #include "base/disable_copying.h"
12 #include "doc/cel_data.h"
13 #include "doc/cel_list.h"
14 #include "doc/color.h"
15 #include "doc/frame.h"
16 #include "doc/frame_tags.h"
17 #include "doc/image_ref.h"
18 #include "doc/image_spec.h"
19 #include "doc/layer_list.h"
20 #include "doc/object.h"
21 #include "doc/pixel_format.h"
22 #include "doc/pixel_ratio.h"
23 #include "doc/slices.h"
24 #include "gfx/rect.h"
25 
26 #include <vector>
27 
28 #define DOC_SPRITE_MAX_WIDTH  65535
29 #define DOC_SPRITE_MAX_HEIGHT 65535
30 
31 namespace doc {
32 
33   class CelsRange;
34   class Document;
35   class Image;
36   class Layer;
37   class LayerGroup;
38   class LayerImage;
39   class Mask;
40   class Palette;
41   class Remap;
42   class RgbMap;
43   class SelectedFrames;
44 
45   typedef std::vector<Palette*> PalettesList;
46 
47   // The main structure used in the whole program to handle a sprite.
48   class Sprite : public Object {
49   public:
50     enum class RgbMapFor {
51       OpaqueLayer,
52       TransparentLayer
53     };
54 
55     ////////////////////////////////////////
56     // Constructors/Destructor
57 
58     Sprite(PixelFormat format, int width, int height, int ncolors);
59     Sprite(const ImageSpec& spec, int ncolors);
60     virtual ~Sprite();
61 
62     static Sprite* createBasicSprite(PixelFormat format, int width, int height, int ncolors);
63 
64     ////////////////////////////////////////
65     // Main properties
66 
spec()67     const ImageSpec& spec() const { return m_spec; }
68 
document()69     Document* document() const { return m_document; }
setDocument(Document * doc)70     void setDocument(Document* doc) { m_document = doc; }
71 
pixelFormat()72     PixelFormat pixelFormat() const { return (PixelFormat)m_spec.colorMode(); }
pixelRatio()73     const PixelRatio& pixelRatio() const { return m_pixelRatio; }
size()74     gfx::Size size() const { return m_spec.size(); }
bounds()75     gfx::Rect bounds() const { return m_spec.bounds(); }
width()76     int width() const { return m_spec.width(); }
height()77     int height() const { return m_spec.height(); }
78 
79     void setPixelFormat(PixelFormat format);
80     void setPixelRatio(const PixelRatio& pixelRatio);
81     void setSize(int width, int height);
82 
83     // Returns true if the rendered images will contain alpha values less
84     // than 255. Only RGBA and Grayscale images without background needs
85     // alpha channel in the render.
86     bool needAlpha() const;
87     bool supportAlpha() const;
88 
transparentColor()89     color_t transparentColor() const { return m_spec.maskColor(); }
90     void setTransparentColor(color_t color);
91 
92     virtual int getMemSize() const override;
93 
94     ////////////////////////////////////////
95     // Layers
96 
root()97     LayerGroup* root() const { return m_root; }
98     LayerImage* backgroundLayer() const;
99     Layer* firstBrowsableLayer() const;
100     layer_t allLayersCount() const;
101     bool hasVisibleReferenceLayers() const;
102 
103     ////////////////////////////////////////
104     // Palettes
105 
106     Palette* palette(frame_t frame) const;
107     const PalettesList& getPalettes() const;
108 
109     void setPalette(const Palette* pal, bool truncate);
110 
111     // Removes all palettes from the sprites except the first one.
112     void resetPalettes();
113 
114     void deletePalette(frame_t frame);
115 
116     RgbMap* rgbMap(frame_t frame) const;
117     RgbMap* rgbMap(frame_t frame, RgbMapFor forLayer) const;
118 
119     ////////////////////////////////////////
120     // Frames
121 
totalFrames()122     frame_t totalFrames() const { return m_frames; }
lastFrame()123     frame_t lastFrame() const { return m_frames-1; }
124 
125     void addFrame(frame_t newFrame);
126     void removeFrame(frame_t frame);
127     void setTotalFrames(frame_t frames);
128 
129     int frameDuration(frame_t frame) const;
130     int totalAnimationDuration() const;
131     void setFrameDuration(frame_t frame, int msecs);
132     void setFrameRangeDuration(frame_t from, frame_t to, int msecs);
133     void setDurationForAllFrames(int msecs);
134 
frameTags()135     const FrameTags& frameTags() const { return m_frameTags; }
frameTags()136     FrameTags& frameTags() { return m_frameTags; }
137 
slices()138     const Slices& slices() const { return m_slices; }
slices()139     Slices& slices() { return m_slices; }
140 
141     ////////////////////////////////////////
142     // Shared Images and CelData (for linked Cels)
143 
144     ImageRef getImageRef(ObjectId imageId);
145     CelDataRef getCelDataRef(ObjectId celDataId);
146 
147     ////////////////////////////////////////
148     // Images
149 
150     void replaceImage(ObjectId curImageId, const ImageRef& newImage);
151     void getImages(std::vector<Image*>& images) const;
152     void remapImages(frame_t frameFrom, frame_t frameTo, const Remap& remap);
153     void pickCels(const double x,
154                   const double y,
155                   const frame_t frame,
156                   const int opacityThreshold,
157                   const LayerList& layers,
158                   CelList& cels) const;
159 
160     ////////////////////////////////////////
161     // Iterators
162 
163     LayerList allLayers() const;
164     LayerList allVisibleLayers() const;
165     LayerList allVisibleReferenceLayers() const;
166     LayerList allBrowsableLayers() const;
167 
168     CelsRange cels() const;
169     CelsRange cels(frame_t frame) const;
170     CelsRange uniqueCels() const;
171     CelsRange uniqueCels(const SelectedFrames& selFrames) const;
172 
173   private:
174     Document* m_document;
175     ImageSpec m_spec;
176     PixelRatio m_pixelRatio;
177     frame_t m_frames;                      // how many frames has this sprite
178     std::vector<int> m_frlens;             // duration per frame
179     PalettesList m_palettes;               // list of palettes
180     LayerGroup* m_root;                    // main group of layers
181 
182     // Current rgb map
183     mutable RgbMap* m_rgbMap;
184 
185     FrameTags m_frameTags;
186     Slices m_slices;
187 
188     // Disable default constructor and copying
189     Sprite();
190     DISABLE_COPYING(Sprite);
191   };
192 
193 } // namespace doc
194 
195 #endif
196