1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/cel.h"
13#include "doc/frame.h"
14#include "doc/image.h"
15#include "doc/layer.h"
16#include "doc/sprite.h"
17#include "gfx/rect.h"
18#include "render/render.h"
19
20#include <memory>
21
22namespace app {
23
24using namespace doc;
25
26static bool has_cels(const Layer* layer, frame_t frame);
27
28LayerImage* create_flatten_layer_copy(Sprite* dstSprite, const Layer* srcLayer,
29 const gfx::Rect& bounds,
30 frame_t frmin, frame_t frmax,
31 const bool newBlend)
32{
33 std::unique_ptr<LayerImage> flatLayer(new LayerImage(dstSprite));
34 render::Render render;
35 render.setNewBlend(newBlend);
36
37 for (frame_t frame=frmin; frame<=frmax; ++frame) {
38 // Does this frame have cels to render?
39 if (has_cels(srcLayer, frame)) {
40 // Create a new image to render each frame.
41 ImageRef image(Image::create(flatLayer->sprite()->pixelFormat(), bounds.w, bounds.h));
42
43 // Create the new cel for the output layer.
44 std::unique_ptr<Cel> cel(new Cel(frame, image));
45 cel->setPosition(bounds.x, bounds.y);
46
47 // Render this frame.
48 render.renderLayer(image.get(), srcLayer, frame,
49 gfx::Clip(0, 0, bounds));
50
51 // Add the cel (and release the std::unique_ptr).
52 flatLayer->addCel(cel.get());
53 cel.release();
54 }
55 }
56
57 return flatLayer.release();
58}
59
60// Returns true if the "layer" or its children have any cel to render
61// in the given "frame".
62static bool has_cels(const Layer* layer, frame_t frame)
63{
64 if (!layer->isVisible())
65 return false;
66
67 switch (layer->type()) {
68
69 case ObjectType::LayerImage:
70 return (layer->cel(frame) ? true: false);
71
72 case ObjectType::LayerGroup: {
73 for (const Layer* child : static_cast<const LayerGroup*>(layer)->layers()) {
74 if (has_cels(child, frame))
75 return true;
76 }
77 break;
78 }
79
80 }
81
82 return false;
83}
84
85} // namespace app
86