1 //=============================================================================
2 //  MusE Reader
3 //  Music Score Reader
4 //
5 //  Copyright (C) 2010 Werner Schweer
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 __OMRPAGE_H__
21 #define __OMRPAGE_H__
22 
23 #include "libmscore/mscore.h"
24 #include "libmscore/durationtype.h"
25 #include "libmscore/fraction.h"
26 #include "libmscore/clef.h"
27 #include "libmscore/xml.h"
28 #include "libmscore/sym.h"
29 
30 namespace Ms {
31 
32 class Omr;
33 class Score;
34 class XmlWriter;
35 class XmlReader;
36 class Pattern;
37 class OmrPage;
38 
39 
40 //---------------------------------------------------------
41 //   HLine
42 //---------------------------------------------------------
43 
44 struct HLine {
45       int x1, x2, y;
HLineHLine46       HLine() {}
HLineHLine47       HLine(int a, int b, int c) : x1(a), x2(b), y(c) {}
48       };
49 
50 //---------------------------------------------------------
51 //   OmrPattern
52 //---------------------------------------------------------
53 
54 class OmrPattern : public QRect {
55    public:
OmrPattern()56     OmrPattern() : QRect(), sym(SymId::noSym), prob(0.0) {}
57       SymId sym;
58       double prob;
59       };
60 
61 //---------------------------------------------------------
62 //   OmrClef
63 //---------------------------------------------------------
64 
65 class OmrClef : public OmrPattern {
66    public:
OmrClef()67       OmrClef() : OmrPattern() {}
OmrClef(const OmrPattern & p)68       OmrClef(const OmrPattern& p) : OmrPattern(p) {}
69       ClefType type = ClefType::G;//CLEF_G;
70       };
71 
72 //---------------------------------------------------------
73 //   OmrNote
74 //    object on staff line
75 //---------------------------------------------------------
76 
77 class OmrNote : public OmrPattern {
78    public:
79       int line;
80       };
81 
82 //---------------------------------------------------------
83 //   OmrChord
84 //---------------------------------------------------------
85 
86 class OmrChord {
87    public:
88       TDuration duration;
89       QList<OmrNote*> notes;
90       };
91 
92 //---------------------------------------------------------
93 //   OmrTimesig
94 //---------------------------------------------------------
95 
96 class OmrTimesig : public QRect {
97    public:
OmrTimesig()98       OmrTimesig() {}
OmrTimesig(const QRect & r)99       OmrTimesig(const QRect& r) : QRect(r) {}
100       Fraction timesig;
101       };
102 
103 //---------------------------------------------------------
104 //   OmrKeySig
105 //---------------------------------------------------------
106 
107 class OmrKeySig : public QRect {
108    public:
OmrKeySig()109       OmrKeySig() {}
OmrKeySig(const QRect & r)110       OmrKeySig(const QRect& r) : QRect(r) {}
111       int type = 0;          // -7 -> +7
112       };
113 
114 //---------------------------------------------------------
115 //   OmrMeasure
116 //---------------------------------------------------------
117 
118 class OmrMeasure {
119       int _x1, _x2;
120       QList<QList<OmrChord>> _chords;      // list of notes for every staff
121       OmrTimesig* _timesig = 0;
122 
123    public:
OmrMeasure()124       OmrMeasure() {}
OmrMeasure(int x1,int x2)125       OmrMeasure(int x1, int x2) : _x1(x1), _x2(x2) {}
chords()126       QList<QList<OmrChord>>& chords()             { return _chords; }
chords()127       const QList<QList<OmrChord>>& chords() const { return _chords; }
x1()128       int x1() const { return _x1; }
x2()129       int x2() const { return _x2; }
timesig()130       OmrTimesig* timesig() const     { return _timesig; }
setTimesig(OmrTimesig * ts)131       void setTimesig(OmrTimesig* ts) { _timesig = ts;}
132       };
133 
134 //---------------------------------------------------------
135 //   OmrStaff
136 //    rectangle of staff lines
137 //---------------------------------------------------------
138 
139 class OmrStaff : public QRect {
140       QList<OmrNote*> _notes;
141       OmrClef _clef;
142       OmrKeySig _keySig;
143 
144    public:
OmrStaff()145       OmrStaff() : QRect() {}
OmrStaff(const QRect & r)146       OmrStaff(const QRect& r) : QRect(r) {}
OmrStaff(int x,int y,int w,int h)147       OmrStaff(int x, int y, int w, int h) : QRect(x, y, w, h) {}
notes()148       QList<OmrNote*>& notes()             { return _notes; }
notes()149       const QList<OmrNote*>& notes() const { return _notes; }
clef()150       OmrClef clef() const                 { return _clef; }
setClef(const OmrClef & c)151       void setClef(const OmrClef& c)       { _clef = c; }
keySig()152       OmrKeySig keySig() const             { return _keySig; }
setKeySig(const OmrKeySig & s)153       void setKeySig(const OmrKeySig& s)   { _keySig = s; }
154       };
155 
156 //---------------------------------------------------------
157 //   OmrSystem
158 //---------------------------------------------------------
159 
160 class OmrSystem {
161       OmrPage* _page;
162       QList<OmrStaff>  _staves;
163       QList<OmrMeasure>_measures;
164 
165       void searchNotes(QList<OmrNote*>*, int x1, int x2, int y, int line);
166 
167    public:
OmrSystem(OmrPage * p)168       OmrSystem(OmrPage* p) { _page = p;  }
169 
staves()170       const QList<OmrStaff>& staves() const { return _staves; }
staves()171       QList<OmrStaff>& staves()             { return _staves; }
measures()172       QList<OmrMeasure>& measures()         { return _measures; }
measures()173       const QList<OmrMeasure>& measures() const { return _measures; }
174 
175       QList<QLine> barLines;
176 
177       void searchSysBarLines();
178       float searchBarLinesvar(int n_staff, int **note_labels);
179       void searchNotes();
180       void searchNotes(int *note_labels, int ran);
181       };
182 
183 //---------------------------------------------------------
184 //   OmrPage
185 //---------------------------------------------------------
186 
187 class OmrPage {
188       Omr* _omr;
189       QImage _image;
190       double _spatium;
191       double _ratio;
192 
193       int cropL, cropR;       // crop values in words (32 bit) units
194       int cropT, cropB;       // crop values in pixel units
195 
196       QList<QRect> _slices;
197       QList<OmrStaff> staves;
198       QList<HLine> slines;
199 
200       QList<QLine>  lines;
201       QList<OmrSystem> _systems;
202 
203       void removeBorder();
204       void crop();
205       void slice();
206       double skew(const QRect&);
207       void deSkew();
208       void getStaffLines();
209       void getRatio();
210       double xproject2(int y);
211       int xproject(const uint* p, int wl);
212       void radonTransform(ulong* projection, int w, int n, const QRect&);
213       OmrTimesig* searchTimeSig(OmrSystem* system);
214       OmrClef searchClef(OmrSystem* system, OmrStaff* staff);
215       void searchKeySig(OmrSystem* system, OmrStaff* staff);
216       OmrPattern searchPattern(const std::vector<Pattern*>& pl, int y, int x1, int x2);
217 
218    public:
219       OmrPage(Omr* _parent);
setImage(const QImage & i)220       void setImage(const QImage& i)     { _image = i; }
image()221       const QImage& image() const        { return _image; }
image()222       QImage& image()                    { return _image; }
223       void read();
width()224       int width() const                  { return _image.width(); }
height()225       int height() const                 { return _image.height(); }
scanLine(int y)226       const uint* scanLine(int y) const  { return (const uint*)_image.scanLine(y); }
bits()227       const uint* bits() const           { return (const uint*)_image.bits(); }
wordsPerLine()228       int wordsPerLine() const           { return (_image.bytesPerLine() + 3)/4; }
229 
sl()230       const QList<QLine>& sl()           { return lines;    }
l()231       const QList<HLine>& l()            { return slines;   }
232 
slices()233       const QList<QRect>& slices() const { return _slices;  }
spatium()234       double spatium() const             { return _spatium; }
ratio()235       double ratio() const   {return _ratio;}
236       double staffDistance() const;
237       double systemDistance() const;
238       void readHeader(Score* score);
239       void readBarLines();
240       float searchBarLines(int start_staff, int end_staff);
241       void identifySystems();
242 
systems()243       const QList<OmrSystem>& systems() const { return _systems; }
244       //QList<OmrSystem>& systems() { return _systems; }
system(int idx)245       OmrSystem* system(int idx)  { return &_systems[idx]; }
246 
247 
248       void write(XmlWriter&) const;
249       void read(XmlReader&);
250       bool dot(int x, int y) const;
251       bool isBlack(int x, int y) const;
252       };
253 
254 }
255 
256 #endif
257 
258 
259