1// Aseprite
2// Copyright (C) 2019-2020 Igara Studio S.A.
3// Copyright (C) 2016 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 "app/cmd/trim_cel.h"
13
14#include "app/cmd/crop_cel.h"
15#include "app/cmd/remove_cel.h"
16#include "doc/algorithm/shrink_bounds.h"
17#include "doc/cel.h"
18#include "doc/layer.h"
19#include "doc/sprite.h"
20
21namespace app {
22namespace cmd {
23
24using namespace doc;
25
26TrimCel::TrimCel(Cel* cel)
27{
28 gfx::Rect newBounds = cel->bounds();
29
30 if (algorithm::shrink_cel_bounds(cel,
31 cel->image()->maskColor(),
32 newBounds)) {
33 if (cel->bounds() != newBounds)
34 add(new cmd::CropCel(cel, newBounds));
35 }
36 else {
37 // Delete the given "cel" and all its links.
38 Sprite* sprite = cel->sprite();
39 Layer* layer = cel->layer();
40 CelData* celData = cel->dataRef().get();
41
42 for (frame_t fr=sprite->totalFrames()-1; fr>=0; --fr) {
43 Cel* c = layer->cel(fr);
44 if (c && c->dataRef().get() == celData)
45 add(new cmd::RemoveCel(c));
46 }
47 }
48}
49
50} // namespace cmd
51} // namespace app
52