Debian Codegraph @

src/lib/MacPaintParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <set>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWGraphicListener.hxx"
43 #include "MWAWHeader.hxx"
44 #include "MWAWPictBitmap.hxx"
45 #include "MWAWPictData.hxx"
46 #include "MWAWPosition.hxx"
47
48 #include "MacPaintParser.hxx"
49
50 /** Internal: the structures of a MacPaintParser */
51 namespace MacPaintParserInternal
52 {
53 ////////////////////////////////////////
54 //! Internal: the state of a MacPaintParser
55 struct State {
56 //! constructor
57 State()
58 : m_bitmap()
59 {
60 }
61 /// the bitmap (v1)
62 std::shared_ptr<MWAWPict> m_bitmap;
63 };
64
65 }
66
67 ////////////////////////////////////////////////////////////
68 // constructor/destructor, ...
69 ////////////////////////////////////////////////////////////
70 MacPaintParser::MacPaintParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
71 : MWAWGraphicParser(input, rsrcParser, header)
72 , m_state()
73 {
74 init();
75 }
76
77 MacPaintParser::~MacPaintParser()
78 {
79 }
80
81 void MacPaintParser::init()
82 {
83 resetGraphicListener();
84 setAsciiName("main-1");
85
86 m_state.reset(new MacPaintParserInternal::State);
87
88 getPageSpan().setMargins(0.1);
89 }
90
91 ////////////////////////////////////////////////////////////
92 // the parser
93 ////////////////////////////////////////////////////////////
94 void MacPaintParser::parse(librevenge::RVNGDrawingInterface *docInterface)
95 {
96 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
97 bool ok = false;
98 try {
99 // create the asciiFile
100 ascii().setStream(getInput());
101 ascii().open(asciiName());
102 checkHeader(nullptr);
103 ok = createZones();
104 if (ok) {
105 createDocument(docInterface);
106 sendBitmap();
107 }
108 ascii().reset();
109 }
110 catch (...) {
111 MWAW_DEBUG_MSG(("MacPaintParser::parse: exception catched when parsing\n"));
112 ok = false;
113 }
114
115 resetGraphicListener();
116 if (!ok) throw(libmwaw::ParseException());
117 }
118
119 ////////////////////////////////////////////////////////////
120 // create the document
121 ////////////////////////////////////////////////////////////
122 void MacPaintParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
123 {
124 if (!documentInterface) return;
125 if (getGraphicListener()) {
126 MWAW_DEBUG_MSG(("MacPaintParser::createDocument: listener already exist\n"));
127 return;
128 }
129
130 // create the page list
131 MWAWPageSpan ps(getPageSpan());
132 ps.setPageSpan(1);
133 std::vector<MWAWPageSpan> pageList(1,ps);
134 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
135 setGraphicListener(listen);
136 listen->startDocument();
137 }
138
139
140 ////////////////////////////////////////////////////////////
141 //
142 // Intermediate level
143 //
144 ////////////////////////////////////////////////////////////
145 bool MacPaintParser::createZones()
146 {
147 MWAWInputStreamPtr input = getInput();
148 if (input->size()<512) return false;
149 #ifdef DEBUG
150 libmwaw::DebugStream f;
151 f << "FileHeader:";
152 input->seek(0, librevenge::RVNG_SEEK_SET);
153 for (int i=0; i<256; i++) { // normally 0, but can be a list of patter
154 auto val=static_cast<int>(input->readLong(2));
155 if (val)
156 f << "f" << i << "=" << val << ",";
157 }
158 ascii().addPos(0);
159 ascii().addNote(f.str().c_str());
160 #endif
161 if (!readBitmap()) return false;
162 if (!input->isEnd()) {
163 MWAW_DEBUG_MSG(("MacPaintParser::createZones: find some extra data\n"));
164 ascii().addPos(input->tell());
165 ascii().addNote("Entries(End):###");
166 }
167 return true;
168 }
169
170 ////////////////////////////////////////////////////////////
171 // send data
172 ////////////////////////////////////////////////////////////
173 bool MacPaintParser::sendBitmap()
174 {
175 MWAWGraphicListenerPtr listener=getGraphicListener();
176 if (!listener) {
177 MWAW_DEBUG_MSG(("MacPaintParser::sendBitmap: can not find the listener\n"));
178 return false;
179 }
180
181 MWAWEmbeddedObject picture;
182 if (!m_state->m_bitmap || !m_state->m_bitmap->getBinary(picture)) return false;
183
184 MWAWPageSpan const &page=getPageSpan();
185 MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())),
186 MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH);
187 pos.setRelativePosition(MWAWPosition::Page);
188 pos.m_wrapping = MWAWPosition::WNone;
189 listener->insertPicture(pos, picture);
190 return true;
191 }
192
193 bool MacPaintParser::readBitmap(bool onlyCheck)
194 {
195 MWAWInputStreamPtr input = getInput();
196 long endPos=input->size();
197 input->seek(512, librevenge::RVNG_SEEK_SET);
198
199 libmwaw::DebugStream f;
200 // a bitmap is composed of 720 rows of (72x8bytes)
201 std::shared_ptr<MWAWPictBitmapIndexed> pict;
202 if (!onlyCheck) {
203 pict.reset(new MWAWPictBitmapIndexed(MWAWVec2i(576,720)));
204 std::vector<MWAWColor> colors(2);
205 colors[0]=MWAWColor::white();
206 colors[1]=MWAWColor::black();
207 pict->setColors(colors);
208 }
209
210 for (int r=0; r<720; ++r) {
211 long rowPos=input->tell();
212 f.str("");
213 f << "Entries(Bitmap)-" << r << ":";
214 int col=0;
215 while (col<72*8) { // UnpackBits
216 if (input->tell()+2>endPos) {
217 MWAW_DEBUG_MSG(("MacPaintParser::readBitmap: can not read row %d\n", r));
218 f << "###";
219 ascii().addPos(rowPos);
220 ascii().addNote(f.str().c_str());
221 return false;
222 }
223 auto wh=static_cast<int>(input->readULong(1));
224 if (wh>=0x81) {
225 auto color=static_cast<int>(input->readULong(1));
226 if (onlyCheck) {
227 col+=8*(0x101-wh);
228 if (col>72*8)
229 return false;
230 continue;
231 }
232 for (int j=0; j < 0x101-wh; ++j) {
233 if (col>=72*8) {
234 MWAW_DEBUG_MSG(("MacPaintParser::readBitmap: can not read row %d\n", r));
235 f << "###";
236 ascii().addPos(rowPos);
237 ascii().addNote(f.str().c_str());
238 return false;
239 }
240 for (int b=7; b>=0; --b)
241 pict->set(col++, r, (color>>b)&1);
242 }
243 }
244 else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression)
245 if (input->tell()+wh+1>endPos) {
246 MWAW_DEBUG_MSG(("MacPaintParser::readBitmap: can not read row %d\n", r));
247 f << "###";
248 ascii().addPos(rowPos);
249 ascii().addNote(f.str().c_str());
250 return false;
251 }
252 for (int j=0; j < wh+1; ++j) {
253 auto color=static_cast<int>(input->readULong(1));
254 if (col>=72*8) {
255 MWAW_DEBUG_MSG(("MacPaintParser::readBitmap: can not read row %d\n", r));
256 f << "###";
257 ascii().addPos(rowPos);
258 ascii().addNote(f.str().c_str());
259 return false;
260 }
261 if (onlyCheck) {
262 col+=8;
263 continue;
264 }
265 for (int b=7; b>=0; --b)
266 pict->set(col++, r, (color>>b)&1);
267 }
268 }
269 }
270 ascii().addPos(rowPos);
271 ascii().addNote(f.str().c_str());
272 }
273 if (!onlyCheck)
274 m_state->m_bitmap=pict;
275 return true;
276 }
277
278 ////////////////////////////////////////////////////////////
279 // read the header
280 ////////////////////////////////////////////////////////////
281 bool MacPaintParser::checkHeader(MWAWHeader *header, bool strict)
282 {
283 *m_state = MacPaintParserInternal::State();
284 MWAWInputStreamPtr input = getInput();
285 if (!input || !input->hasDataFork() || !input->checkPosition(512+720*2))
286 return false;
287
288 MWAWDocument::Type type=MWAWDocument::MWAW_T_MACPAINT;
289 std::string fType, fCreator;
290 if (input->getFinderInfo(fType, fCreator) && fCreator=="PANT")
291 type=MWAWDocument::MWAW_T_FULLPAINT;
292
293 int const vers=1;
294 if (strict) {
295 /* check :
296 - if we can read the bitmap,
297 - if the data have been packed: ie. if the bitmap size is 720x144
298 the bitmap's creator clearly creates the worst possible data,
299 - and if after reading the bitmap we are at the end of the file
300 (up to 512 char) */
301 input->seek(512, librevenge::RVNG_SEEK_SET);
302 if (!readBitmap(true) || input->tell()==512+720*144 || input->checkPosition(input->tell()+512))
303 return false;
304 }
305 setVersion(vers);
306 if (header)
307 header->reset(type, vers, MWAWDocument::MWAW_K_PAINT);
308
309 return true;
310 }
311
312 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: