1 // Aseprite
2 // Copyright (C) 2017  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "app/load_matrix.h"
12 
13 #include "app/context.h"
14 #include "app/doc.h"
15 #include "app/file/file.h"
16 #include "doc/layer.h"
17 #include "doc/sprite.h"
18 #include "render/dithering_matrix.h"
19 
20 namespace app {
21 
load_dithering_matrix_from_sprite(const std::string & filename,render::DitheringMatrix & matrix)22 bool load_dithering_matrix_from_sprite(
23   const std::string& filename,
24   render::DitheringMatrix& matrix)
25 {
26   base::UniquePtr<Doc> doc(load_document(nullptr, filename));
27   if (!doc)
28     return false;
29 
30   doc::Sprite* spr = doc->sprite();
31   const doc::Layer* lay = (spr && spr->root() ? spr->root()->firstLayer():
32                                                 nullptr);
33   const doc::Image* img = (lay && lay->cel(0) ? lay->cel(0)->image():
34                                                 nullptr);
35   if (img) {
36     const int w = spr->width();
37     const int h = spr->height();
38     matrix = render::DitheringMatrix(h, w);
39     for (int i=0; i<h; ++i)
40       for (int j=0; j<w; ++j)
41         matrix(i, j) = img->getPixel(j, i);
42 
43     matrix.calcMaxValue();
44   }
45   else {
46     matrix = render::DitheringMatrix();
47   }
48 
49   return true;
50 }
51 
52 } // namespace app
53