1// Aseprite Document Library
2// Copyright (c) 2020 Igara Studio S.A.
3// Copyright (c) 2018 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/algorithm/fill_selection.h"
13
14#include "doc/grid.h"
15#include "doc/image_impl.h"
16#include "doc/mask.h"
17#include "doc/primitives.h"
18
19namespace doc {
20namespace algorithm {
21
22void fill_selection(Image* image,
23 const gfx::Rect& imageBounds,
24 const Mask* mask,
25 const color_t color,
26 const Grid* grid)
27{
28 ASSERT(mask);
29 ASSERT(mask->bitmap());
30 if (!mask || !mask->bitmap())
31 return;
32
33 const auto rc = (imageBounds & mask->bounds());
34 if (rc.isEmpty())
35 return; // <- There is no intersection between image bounds and mask bounds
36
37 const LockImageBits<BitmapTraits> maskBits(mask->bitmap(),
38 gfx::Rect(rc).offset(-mask->origin()));
39 auto it = maskBits.begin();
40
41 for (int v=0; v<rc.h; ++v) {
42 for (int u=0; u<rc.w; ++u, ++it) {
43 ASSERT(it != maskBits.end());
44 if (*it) {
45 gfx::Point pt(u + rc.x,
46 v + rc.y);
47
48 if (grid) {
49 pt = grid->canvasToTile(pt);
50 }
51 else {
52 pt -= imageBounds.origin();
53 }
54
55 // TODO use iterator
56 put_pixel(image, pt.x, pt.y, color);
57 }
58 }
59 }
60
61 ASSERT(it == maskBits.end());
62}
63
64} // namespace algorithm
65} // namespace doc
66