1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2020 MuseScore BVBA and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef AVS_AVSOMR_H
21 #define AVS_AVSOMR_H
22 
23 #include <memory>
24 #include <QList>
25 #include <QHash>
26 #include <QSet>
27 #include <QRect>
28 #include <QImage>
29 #include <QByteArray>
30 
31 namespace Ms {
32 namespace Avs {
33 
34 class MsmrFile;
35 class AvsOmr
36       {
37    public:
38       AvsOmr();
39       using ID = uint32_t;
40       using Num = uint16_t;
41       using Idx = uint16_t;
42 
43       // Model
44       struct Barline {
45             ID id{0};
46             ID glyphID{0};
47             };
48 
49       struct Staff {
50             ID id{0};
51             QList<ID> barlines;
52 
53             struct {
54                   int32_t start{0};
55                   int32_t stop{0};
56                   ID clefID{0};
57                   ID keyID{0};
58                   ID timeID{0};
59                   } header;
60 
isValidStaff61             bool isValid() const { return id > 0; }
62             };
63 
64       struct MStack {
65             ID id{0};
66             Idx idx{0}; // resolved
67             int32_t left{0};
68             int32_t right{0};
isValidMStack69             bool isValid() const { return right > 0; }
70             };
71 
72       struct Part {
73             QList<Staff> staffs;
74             };
75 
76       struct Inters {
77             QHash<ID, Barline> barlines;
78             QSet<ID> usedglyphs;
79             };
80 
81       struct System {
82             QList<MStack> mstacks;
83             Part part;
84             Inters inters;
85             QSet<ID> freeglyphs;
86 
87             // resolved
88             int32_t top{0};
89             int32_t bottom{0};
90 
91             const MStack& stackByIdx(Idx idx, Idx* idxInSys = nullptr) const;
92             };
93 
94       struct Page {
95             QList<System> systems;
96             };
97 
98       enum class GlyphUsed {
99             Undefined = 0,
100             Used,
101             Free,
102             Free_Covered,   // Avs marked as free, but fully covered used
103             Trash           // Detected as trash (too small)
104             };
105 
106       struct Glyph {
107             ID id{0};
108             QRect bbox;
109             GlyphUsed used{GlyphUsed::Undefined};
110             QImage img;
111             };
112 
113       struct Sheet {
114             Num num{0};
115             Page page;
116             Idx mbeginIdx{0};
117             Idx mendIdx{0};
118             QHash<ID, Glyph*> glyphs;
119 
120             bool isGlyphUsed(const ID& glypthID) const;
121             bool isGlyphFree(const ID& glypthID) const;
122             };
123 
124       struct Book {
125             uint16_t sheets{0};
126             };
127 
128       void resolve();
129 
130       // Configure
131       struct Config {
isHiddenAllConfig132             bool isHiddenAll() const { return !_isShowRecognized && !_isShowNotRecognized; }
setIsShowRecognizedConfig133             void setIsShowRecognized(bool arg) { _isShowRecognized = arg; }
isShowRecognizedConfig134             bool isShowRecognized() const { return _isShowRecognized; }
setIsShowNotRecognizedConfig135             void setIsShowNotRecognized(bool arg) { _isShowNotRecognized = arg; }
isShowNotRecognizedConfig136             bool isShowNotRecognized() const { return _isShowNotRecognized; }
137 
138          private:
139             bool _isShowRecognized{true};
140             bool _isShowNotRecognized{true};
141             };
142 
143       Config& config();
144       const Config& config() const;
145 
146       // Access
147       struct MMetrics {
148             QRect bbox;  //! NOTE bbox of measure
149             QRect ebbox; //! NOTE bbox in which there may be elements belonging to measure
150             QRect hbbox; //! NOTE bbox of measure header (clef, key, time)
151 
headerBBoxMMetrics152             QRect headerBBox() const {
153                   return hbbox;
154                   }
155 
chordBBoxMMetrics156             QRect chordBBox() const {
157                   QRect c = bbox;
158                   c.setLeft(c.left() + hbbox.width());
159                   return c;
160                   }
161             };
162 
163       Num sheetNumByMeausereIdx(const Idx& meausureIdx) const;
164       MMetrics mmetrics(const Num& sheetNum, const Idx &meausureIdx) const;
165       QList<const Glyph*> glyphsByBBox(const Num& sheetNum, const QRect& bbox, QList<AvsOmr::GlyphUsed>& accepted) const;
166 
167       // Data
168       void setMsmrFile(std::shared_ptr<MsmrFile> file); // keep data for saving
169       std::shared_ptr<MsmrFile> msmrFile() const;
170 
171       // Info
172       struct Info {
173             QColor usedColor;
174             uint32_t usedCount{0};
175             QColor freeColor;
176             uint32_t freeCount{0};
177             };
178 
179       const Info& info() const;
180 
181    private:
182       friend class AvsOmrReader;
183 
184       const Sheet* sheet(const Num& sheetNum) const;
185 
186       Book _book;
187       QList<Sheet*> _sheets;
188       Config _config;
189       std::shared_ptr<MsmrFile> _msmrFile;
190       Info _info;
191       };
192 
193 } // Avs
194 } // Ms
195 
196 #endif // AVS_AVSOMR_H
197