Debian Codegraph @

src/lib/CommonParser.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "CommonParser.h"
11
12 #include <string.h>
13
14 #include "CDRCollector.h"
15 #include "CDRPath.h"
16 #include "libcdr_utils.h"
17
18 libcdr::CommonParser::CommonParser(libcdr::CDRCollector *collector)
19 : m_collector(collector), m_precision(libcdr::PRECISION_UNKNOWN) {}
20
21 libcdr::CommonParser::~CommonParser()
22 {
23 }
24
25 double libcdr::CommonParser::readCoordinate(librevenge::RVNGInputStream *input, bool bigEndian)
26 {
27 if (m_precision == PRECISION_UNKNOWN)
28 throw UnknownPrecisionException();
29 else if (m_precision == PRECISION_16BIT)
30 return (double)readS16(input, bigEndian) / 1000.0;
31 return (double)readS32(input, bigEndian) / 254000.0;
32 }
33
34 unsigned libcdr::CommonParser::readUnsigned(librevenge::RVNGInputStream *input, bool bigEndian)
35 {
36 if (m_precision == PRECISION_UNKNOWN)
37 throw UnknownPrecisionException();
38 else if (m_precision == PRECISION_16BIT)
39 return (unsigned)readU16(input, bigEndian);
40 return readU32(input, bigEndian);
41 }
42
43 unsigned short libcdr::CommonParser::readUnsignedShort(librevenge::RVNGInputStream *input, bool bigEndian)
44 {
45 if (m_precision == PRECISION_UNKNOWN)
46 throw UnknownPrecisionException();
47 else if (m_precision == PRECISION_16BIT)
48 return (unsigned short)readU8(input, bigEndian);
49 return readU16(input, bigEndian);
50 }
51
52 int libcdr::CommonParser::readInteger(librevenge::RVNGInputStream *input, bool bigEndian)
53 {
54 if (m_precision == PRECISION_UNKNOWN)
55 throw UnknownPrecisionException();
56 else if (m_precision == PRECISION_16BIT)
57 return (int)readS16(input, bigEndian);
58 return readS32(input, bigEndian);
59 }
60
61 double libcdr::CommonParser::readAngle(librevenge::RVNGInputStream *input, bool bigEndian)
62 {
63 if (m_precision == PRECISION_UNKNOWN)
64 throw UnknownPrecisionException();
65 else if (m_precision == PRECISION_16BIT)
66 return M_PI * (double)readS16(input, bigEndian) / 1800.0;
67 return M_PI * (double)readS32(input, bigEndian) / 180000000.0;
68 }
69
70 void libcdr::CommonParser::outputPath(const std::vector<std::pair<double, double> > &points,
71 const std::vector<unsigned char> &types)
72 {
73 CDRPath path;
74 processPath(points, types, path);
75 m_collector->collectPath(path);
76 }
77
78 void libcdr::CommonParser::processPath(const std::vector<std::pair<double, double> > &points,
79 const std::vector<unsigned char> &types, CDRPath &path)
80 {
81 bool isClosedPath = false;
82 std::vector<std::pair<double, double> >tmpPoints;
83 for (size_t k=0; k<points.size(); k++)
84 {
85 const unsigned char &type = types[k];
86 if (type & 0x08)
87 isClosedPath = true;
88 else
89 isClosedPath = false;
90 if (!(type & 0x10) && !(type & 0x20))
91 {
92 // cont angle
93 }
94 else if (type & 0x10)
95 {
96 // cont smooth
97 }
98 else if (type & 0x20)
99 {
100 // cont symmetrical
101 }
102 if (!(type & 0x40) && !(type & 0x80))
103 {
104 if (isClosedPath)
105 path.appendClosePath();
106 tmpPoints.clear();
107 path.appendMoveTo(points[k].first, points[k].second);
108 }
109 else if ((type & 0x40) && !(type & 0x80))
110 {
111 tmpPoints.clear();
112 path.appendLineTo(points[k].first, points[k].second);
113 if (isClosedPath)
114 path.appendClosePath();
115 }
116 else if (!(type & 0x40) && (type & 0x80))
117 {
118 if (tmpPoints.size() >= 2)
119 path.appendCubicBezierTo(tmpPoints[0].first, tmpPoints[0].second,
120 tmpPoints[1].first, tmpPoints[1].second,
121 points[k].first, points[k].second);
122 else
123 path.appendLineTo(points[k].first, points[k].second);
124 if (isClosedPath)
125 path.appendClosePath();
126 tmpPoints.clear();
127 }
128 else if ((type & 0x40) && (type & 0x80))
129 {
130 tmpPoints.push_back(points[k]);
131 }
132 }
133 }
134
135 void libcdr::CommonParser::readRImage(unsigned &colorModel, unsigned &width, unsigned &height, unsigned &bpp,
136 std::vector<unsigned> &palette, std::vector<unsigned char> &bitmap,
137 librevenge::RVNGInputStream *input, bool bigEndian)
138 {
139 colorModel = readU32(input, bigEndian);
140 input->seek(4, librevenge::RVNG_SEEK_CUR);
141 width = readU32(input, bigEndian);
142 height = readU32(input, bigEndian);
143 input->seek(4, librevenge::RVNG_SEEK_CUR);
144 bpp = readU32(input, bigEndian);
145 input->seek(4, librevenge::RVNG_SEEK_CUR);
146 unsigned bmpsize = readU32(input, bigEndian);
147 input->seek(32, librevenge::RVNG_SEEK_CUR);
148 if (bpp < 24 && colorModel != 5 && colorModel != 6)
149 {
150 palette.clear();
151 input->seek(2, librevenge::RVNG_SEEK_CUR);
152 unsigned long palettesize = readU16(input);
153 if (palettesize > getRemainingLength(input) / 3)
154 palettesize = getRemainingLength(input) / 3;
155 palette.reserve(palettesize);
156 for (unsigned long i = 0; i <palettesize; ++i)
157 {
158 unsigned b = readU8(input);
159 unsigned g = readU8(input);
160 unsigned r = readU8(input);
161 palette.push_back(b | (g << 8) | (r << 16));
162 }
163 }
164 if (bmpsize == 0)
165 return;
166 unsigned long tmpNumBytesRead = 0;
167 const unsigned char *tmpBuffer = input->read(bmpsize, tmpNumBytesRead);
168 if (bmpsize != tmpNumBytesRead)
169 return;
170 bitmap.clear();
171 bitmap.resize(bmpsize);
172 memcpy(&bitmap[0], tmpBuffer, bmpsize);
173 }
174
175 void libcdr::CommonParser::readBmpPattern(unsigned &width, unsigned &height, std::vector<unsigned char> &pattern,
176 unsigned length, librevenge::RVNGInputStream *input, bool bigEndian)
177 {
178 unsigned headerLength = readU32(input);
179 if (headerLength != 40)
180 return;
181 width = readU32(input, bigEndian);
182 height = readU32(input, bigEndian);
183 input->seek(2, librevenge::RVNG_SEEK_CUR);
184 unsigned bpp = readU16(input);
185 if (bpp != 1)
186 return;
187 input->seek(4, librevenge::RVNG_SEEK_CUR); // compression
188 unsigned dataSize = readU32(input, bigEndian);
189 if (dataSize == 0)
190 return;
191 CDR_DEBUG_MSG(("CommonParser::readBmpPattern - offset of pixel data %i\n", length - dataSize - 24));
192 input->seek(length - dataSize - 24, librevenge::RVNG_SEEK_CUR);
193 unsigned long tmpNumBytesRead = 0;
194 const unsigned char *tmpBuffer = input->read(dataSize, tmpNumBytesRead);
195 if (dataSize != tmpNumBytesRead)
196 return;
197 pattern.clear();
198 pattern.resize(dataSize);
199 memcpy(&pattern[0], tmpBuffer, dataSize);
200 }
201
202
203 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */