1// Aseprite
2// Copyright (C) 2019-2020 Igara Studio S.A.
3// Copyright (C) 2018 David Capello
4// Copyright (C) 2016 Carlo Caputo
5//
6// This program is distributed under the terms of
7// the End-User License Agreement for Aseprite.
8
9#ifdef HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#include "app/util/conversion_to_surface.h"
14#include "doc/blend_mode.h"
15#include "doc/cel.h"
16#include "doc/layer.h"
17#include "doc/sprite.h"
18#include "os/surface.h"
19#include "os/system.h"
20#include "render/render.h"
21
22namespace app {
23namespace thumb {
24
25os::SurfaceRef get_cel_thumbnail(const doc::Cel* cel,
26 const gfx::Size& fitInSize)
27{
28 gfx::Size newSize;
29
30 if (cel->bounds().w > fitInSize.w ||
31 cel->bounds().h > fitInSize.h)
32 newSize = gfx::Rect(cel->bounds()).fitIn(gfx::Rect(fitInSize)).size();
33 else
34 newSize = cel->bounds().size();
35
36 if (newSize.w < 1 ||
37 newSize.h < 1)
38 return nullptr;
39
40 doc::ImageRef thumbnailImage(
41 doc::Image::create(
42 doc::IMAGE_RGB, newSize.w, newSize.h));
43
44 render::Render render;
45 render::Projection proj(cel->sprite()->pixelRatio(),
46 render::Zoom(newSize.w, cel->bounds().w));
47 render.setProjection(proj);
48
49 const doc::Palette* palette = cel->sprite()->palette(cel->frame());
50 render.renderCel(
51 thumbnailImage.get(),
52 cel,
53 cel->sprite(),
54 cel->image(),
55 cel->layer(),
56 palette,
57 gfx::Rect(gfx::Point(0, 0), cel->bounds().size()),
58 gfx::Clip(gfx::Rect(gfx::Point(0, 0), newSize)),
59 255, doc::BlendMode::NORMAL);
60
61 if (os::SurfaceRef thumbnail = os::instance()->makeRgbaSurface(
62 thumbnailImage->width(),
63 thumbnailImage->height())) {
64 convert_image_to_surface(
65 thumbnailImage.get(), palette, thumbnail.get(),
66 0, 0, 0, 0, thumbnailImage->width(), thumbnailImage->height());
67 return thumbnail;
68 }
69 else
70 return nullptr;
71}
72
73} // thumb
74} // app
75