scantools  1.0.4
Graphics manipulation with a view towards scanned documents
JBIG2Segment.h
1 /*
2  * Copyright © 2016 Stefan Kebekus <stefan.kebekus@math.uni-freiburg.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation, either version 3 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef JBIG2SEGMENT
20 #define JBIG2SEGMENT 1
21 
22 #include <QIODevice>
23 #include <QVector>
24 
25 
26 /* Reads and stores a JBIG2 segment, and interprets the segment header.
27 
28  This class can be used to read a JBIG2 segment. It is used internally by
29  the JBIG2Document class; users will hardly ever access this class directly.
30  The class has minimal support for interpretation of the segment header, and
31  for manipulating a few flags there. In order to support JBIG2 with random
32  access organisation, instances of this class are constructed in a three-step
33  procedure.
34 
35  - First, an instance is constructed using the default JBIG2Segment().
36 
37  - Second, the segment header is read from a QIODevice (usually a QFile),
38  using the method readHeader(). If an error occurs at this stage, the
39  JBIG2Segment should not be used anymore.
40 
41  - Third, the segment data is read from a QIODevice, using the method
42  readData(). Again, if an error occurs at this stage, the JBIG2Segment
43  should not be used anymore.
44 
45  The instance can then be converted to a QByteArray which contains the full
46  segment, or it can be manipulated first.
47 
48  The methods of this class are reentrant, but not thread-safe.
49 */
50 
51 class JBIG2Segment
52 {
53  public:
55  JBIG2Segment();
56 
58  void clear();
59 
74  QString readHeader(QIODevice *device);
75 
76 
78  inline bool hasHeader() const
79  {
80  return !_header.isEmpty();
81  };
82 
83 
89  QByteArray header() const;
90 
91 
105  QString readData(QIODevice *device);
106 
107 
113  inline bool hasData() const
114  {
115  return !_data.isNull();
116  };
117 
118 
124  QByteArray data() const;
125 
126 
136  inline operator QByteArray() const {return header()+data();};
137 
138 
146  QString info() const;
147 
148 
157  quint8 type() const;
158 
159 
168  bool retainbitForThisSegment() const;
169 
170 
179  void setRetainbitForThisSegment(bool bit=true);
180 
181 
190  quint32 pageAssociation() const;
191 
192 
201  void setPageAssociation();
202 
203 
209  quint32 number() const;
210 
211 
220  QVector<quint32> refersTo() const;
221 
222  private:
223  // QByteArray containing the segment header
224  QByteArray _header;
225 
226  // QByteArray containing the segment data
227  QByteArray _data;
228 
229  // Once the header has been read successfully, this member contains the
230  // position of the 'page association' field in the segment header data. This
231  // information is used internally to modify the header data.
232  qint64 pageAssociationPos{};
233 
234  // Once the header has been read successfully, this member contains the
235  // position of the byte in the segment header data whose least significant bit
236  // is the retain bit for this segment. This information is used internally to
237  // modify the header data.
238  qint64 retainbitForThisSegmentPos{};
239 
240 
241  /* Warning! The following members duplicate data that is also present in the
242  member _header, which represents the segment header data. When modifying
243  these members, the segment header data must be modified accordingly,
244  otherwise data corruption will like follow.
245  */
246 
247  // Length of segment data, as read from the header data
248  quint32 _dataLength{};
249 
250  // Header flags,
251  quint8 _headerFlags{};
252 
253  // Number of this segment
254  quint32 _number{};
255 
256  // Page association
257  quint32 _pageAssociation{};
258 
259  // Numbers of segments that this segment refers to, in order of appearance in
260  // the segment header data.
261  QVector<quint32> _referredToSegmentNumbers;
262 
263  // Convenience method, tells if the page association field in the header data
264  // is one byte or four bytes long
265  inline bool segmentPageAssociationIs8Bit() const
266  {
267  return ((_headerFlags & (1 << 6)) == 0);
268  };
269 
270 };
271 
272 #endif