| 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 "PixelPaintParser.hxx" |
| 49 |
|
| 50 |
/** Internal: the structures of a PixelPaintParser */ |
| 51 |
namespace PixelPaintParserInternal |
| 52 |
{ |
| 53 |
//////////////////////////////////////// |
| 54 |
//! Internal: the state of a PixelPaintParser |
| 55 |
struct State { |
| 56 |
//! constructor |
| 57 |
State() |
| 58 |
: m_bitmapSize(0,0) |
| 59 |
, m_colorList() |
| 60 |
, m_bitmap() |
| 61 |
{ |
| 62 |
} |
| 63 |
//! the bitmap size(v1) |
| 64 |
MWAWVec2i m_bitmapSize; |
| 65 |
//! the color map |
| 66 |
std::vector<MWAWColor> m_colorList; |
| 67 |
/// the bitmap |
| 68 |
std::shared_ptr<MWAWPict> m_bitmap; |
| 69 |
}; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
//////////////////////////////////////////////////////////// |
| 74 |
// constructor/destructor, ... |
| 75 |
//////////////////////////////////////////////////////////// |
| 76 |
PixelPaintParser::PixelPaintParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 77 |
: MWAWGraphicParser(input, rsrcParser, header) |
| 78 |
, m_state() |
| 79 |
{ |
| 80 |
init(); |
| 81 |
} |
| 82 |
|
| 83 |
PixelPaintParser::~PixelPaintParser() |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
void PixelPaintParser::init() |
| 88 |
{ |
| 89 |
resetGraphicListener(); |
| 90 |
setAsciiName("main-1"); |
| 91 |
|
| 92 |
m_state.reset(new PixelPaintParserInternal::State); |
| 93 |
|
| 94 |
getPageSpan().setMargins(0.1); |
| 95 |
} |
| 96 |
|
| 97 |
//////////////////////////////////////////////////////////// |
| 98 |
// the parser |
| 99 |
//////////////////////////////////////////////////////////// |
| 100 |
void PixelPaintParser::parse(librevenge::RVNGDrawingInterface *docInterface) |
| 101 |
{ |
| 102 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 103 |
bool ok = false; |
| 104 |
try { |
| 105 |
// create the asciiFile |
| 106 |
ascii().setStream(getInput()); |
| 107 |
ascii().open(asciiName()); |
| 108 |
checkHeader(nullptr); |
| 109 |
ok = createZones(); |
| 110 |
if (ok) { |
| 111 |
createDocument(docInterface); |
| 112 |
sendBitmap(); |
| 113 |
} |
| 114 |
ascii().reset(); |
| 115 |
} |
| 116 |
catch (...) { |
| 117 |
MWAW_DEBUG_MSG(("PixelPaintParser::parse: exception catched when parsing\n")); |
| 118 |
ok = false; |
| 119 |
} |
| 120 |
|
| 121 |
resetGraphicListener(); |
| 122 |
if (!ok) throw(libmwaw::ParseException()); |
| 123 |
} |
| 124 |
|
| 125 |
//////////////////////////////////////////////////////////// |
| 126 |
// create the document |
| 127 |
//////////////////////////////////////////////////////////// |
| 128 |
void PixelPaintParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface) |
| 129 |
{ |
| 130 |
if (!documentInterface) return; |
| 131 |
if (getGraphicListener()) { |
| 132 |
MWAW_DEBUG_MSG(("PixelPaintParser::createDocument: listener already exist\n")); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// create the page list |
| 137 |
MWAWPageSpan ps(getPageSpan()); |
| 138 |
ps.setPageSpan(1); |
| 139 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 140 |
MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface)); |
| 141 |
setGraphicListener(listen); |
| 142 |
listen->startDocument(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
//////////////////////////////////////////////////////////// |
| 147 |
// |
| 148 |
// Intermediate level |
| 149 |
// |
| 150 |
//////////////////////////////////////////////////////////// |
| 151 |
bool PixelPaintParser::createZones() |
| 152 |
{ |
| 153 |
MWAWInputStreamPtr input = getInput(); |
| 154 |
int const vers=version(); |
| 155 |
if (input->size()<512) return false; |
| 156 |
libmwaw::DebugStream f; |
| 157 |
f << "FileHeader:"; |
| 158 |
input->seek(0, librevenge::RVNG_SEEK_SET); // seek file def |
| 159 |
ascii().addPos(0); |
| 160 |
ascii().addNote(f.str().c_str()); |
| 161 |
|
| 162 |
long pos; |
| 163 |
if ((vers<=1&&!readFileHeaderV1()) || (vers>=2&&!readFileHeaderV2())) |
| 164 |
return false; |
| 165 |
|
| 166 |
pos=input->tell(); |
| 167 |
if (!readColorMap()) { |
| 168 |
ascii().addPos(pos); |
| 169 |
ascii().addNote("Entries(ColorMap):###"); |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
pos=input->tell(); |
| 174 |
if (!readPatternMap()) { |
| 175 |
ascii().addPos(pos); |
| 176 |
ascii().addNote("Entries(PatternMap):###"); |
| 177 |
return false; |
| 178 |
} |
| 179 |
if ((vers==1 && !readBitmapV1()) || (vers==2 && !readBitmapV2())) |
| 180 |
return false; |
| 181 |
if (!input->isEnd()) { |
| 182 |
if (!input->checkPosition(input->tell()+8)) { |
| 183 |
ascii().addPos(input->tell()); |
| 184 |
ascii().addNote("Entries(Unused):"); |
| 185 |
} |
| 186 |
else { |
| 187 |
MWAW_DEBUG_MSG(("PixelPaintParser::createZones: find some extra data\n")); |
| 188 |
ascii().addPos(input->tell()); |
| 189 |
ascii().addNote("Entries(Unused):###"); |
| 190 |
} |
| 191 |
} |
| 192 |
return m_state->m_bitmap.get()!=nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
bool PixelPaintParser::readFileHeaderV1(bool onlyCheck) |
| 196 |
{ |
| 197 |
MWAWInputStreamPtr input = getInput(); |
| 198 |
if (!input->checkPosition(0x426)) { |
| 199 |
MWAW_DEBUG_MSG(("PixelPaintParser::readFileHeaderV1: file is too short\n")); |
| 200 |
return false; |
| 201 |
} |
| 202 |
libmwaw::DebugStream f; |
| 203 |
f << "Entries(Zone0):"; |
| 204 |
input->seek(4, librevenge::RVNG_SEEK_SET); // seek file def |
| 205 |
long pos=4; |
| 206 |
for (int i=0; i<144; ++i) { // always 0 |
| 207 |
auto val=static_cast<int>(input->readULong(2)); |
| 208 |
if (val) f << "f" << i << "=" << val << ","; |
| 209 |
} |
| 210 |
if (!onlyCheck) { |
| 211 |
ascii().addPos(pos); |
| 212 |
ascii().addNote(f.str().c_str()); |
| 213 |
} |
| 214 |
|
| 215 |
for (int z=0; z<8; ++z) { |
| 216 |
pos=input->tell(); |
| 217 |
f.str(""); |
| 218 |
f << "Zone0-A" << z << ":"; |
| 219 |
int val; |
| 220 |
val=static_cast<int>(input->readULong(2)); |
| 221 |
if (onlyCheck && z==0 && (val<1 || val>4)) return false; |
| 222 |
switch (val) { |
| 223 |
case 0: |
| 224 |
break; |
| 225 |
case 1: |
| 226 |
f << "image=512x512,"; |
| 227 |
if (z==0) |
| 228 |
m_state->m_bitmapSize=MWAWVec2i(512,512); |
| 229 |
break; |
| 230 |
case 2: |
| 231 |
f << "image=720x576,"; |
| 232 |
if (z==0) |
| 233 |
m_state->m_bitmapSize=MWAWVec2i(720,576); |
| 234 |
break; |
| 235 |
case 3: |
| 236 |
f << "image=1024x768,"; |
| 237 |
if (z==0) |
| 238 |
m_state->m_bitmapSize=MWAWVec2i(1024,768); |
| 239 |
break; |
| 240 |
case 4: |
| 241 |
f << "image=1024x1024,"; |
| 242 |
if (z==0) |
| 243 |
m_state->m_bitmapSize=MWAWVec2i(1024,1024); |
| 244 |
break; |
| 245 |
default: |
| 246 |
MWAW_DEBUG_MSG(("PixelPaintParser::readFileHeaderV1: unknown image size\n")); |
| 247 |
f << "###image=" << val << ","; |
| 248 |
break; |
| 249 |
} |
| 250 |
for (int i=0; i<4; ++i) { |
| 251 |
val=static_cast<int>(input->readULong(2)); |
| 252 |
if (val) |
| 253 |
f << "f" << i << "=" << val << ","; |
| 254 |
} |
| 255 |
val=static_cast<int>(input->readULong(2)); |
| 256 |
if (val!=5) // always 5 |
| 257 |
f << "f5=" << val << ","; |
| 258 |
int dim[4]; |
| 259 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 260 |
if (dim[0]||dim[1]||dim[2]||dim[3]) |
| 261 |
f << "windows[dim]=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ","; |
| 262 |
val=static_cast<int>(input->readULong(2)); |
| 263 |
if (val) // always 0 |
| 264 |
f << "f6=" << val << ","; |
| 265 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 266 |
if (dim[0]||dim[1]||dim[2]||dim[3]) |
| 267 |
f << "screen1[dim]=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ","; |
| 268 |
for (int i=0; i<8; ++i) { // always 0 |
| 269 |
val=static_cast<int>(input->readULong(2)); |
| 270 |
if (val) |
| 271 |
f << "f" << i+7 << "=" << val << ","; |
| 272 |
} |
| 273 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 274 |
if (dim[0]||dim[1]||dim[2]||dim[3]) |
| 275 |
f << "screen1[sz]=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ","; |
| 276 |
for (int i=0; i<2; ++i) { // f15=1|8, f16=0|69 |
| 277 |
val=static_cast<int>(input->readULong(2)); |
| 278 |
if (val) |
| 279 |
f << "f" << i+15 << "=" << val << ","; |
| 280 |
} |
| 281 |
for (int i=0; i<2; ++i) dim[i]=static_cast<int>(input->readULong(2)); |
| 282 |
if (dim[0]||dim[1]) f << "width=[" << dim[0] << "," << dim[1] << "],"; |
| 283 |
for (int i=0; i<4; ++i) { // g2=1|3 |
| 284 |
val=static_cast<int>(input->readULong(2)); |
| 285 |
if (val) |
| 286 |
f << "g" << i << "=" << val << ","; |
| 287 |
} |
| 288 |
for (int i=0; i<3; ++i) { |
| 289 |
val=static_cast<int>(input->readULong(4)); |
| 290 |
if (val) |
| 291 |
f << "ID" << i << "=" << std::hex << val << std::dec << ","; |
| 292 |
} |
| 293 |
val=static_cast<int>(input->readULong(2)); // always 0? |
| 294 |
if (val) f << "g2=" << val << ","; |
| 295 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 296 |
if (dim[0]||dim[1]||dim[2]||dim[3]) |
| 297 |
f << "screen2[dim]=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ","; |
| 298 |
if (onlyCheck) { |
| 299 |
input->seek(0x426, librevenge::RVNG_SEEK_SET); |
| 300 |
return true; |
| 301 |
} |
| 302 |
ascii().addPos(pos); |
| 303 |
ascii().addNote(f.str().c_str()); |
| 304 |
input->seek(pos+92, librevenge::RVNG_SEEK_SET); |
| 305 |
} |
| 306 |
pos=input->tell(); |
| 307 |
f.str(""); |
| 308 |
f << "Zone0-Prefs:"; |
| 309 |
int val; |
| 310 |
for (int i=0; i<5; ++i) { // always 0? |
| 311 |
val=static_cast<int>(input->readULong(2)); |
| 312 |
if (val) |
| 313 |
f << "f" << i << "=" << val << ","; |
| 314 |
} |
| 315 |
for (int i=0; i<6; ++i) { |
| 316 |
val=static_cast<int>(input->readULong(2)); |
| 317 |
static int const expected[]= {1, 0x28, 0xaa, 3, 0xff, 9}; |
| 318 |
if (val==expected[i]) continue; |
| 319 |
if (i==4) |
| 320 |
f << "font[sz]=" << val << ","; |
| 321 |
else |
| 322 |
f << "f" << i+5 << "=" << val << ","; |
| 323 |
|
| 324 |
} |
| 325 |
for (int i=0; i<6; ++i) { |
| 326 |
val=static_cast<int>(input->readULong(1)); |
| 327 |
if (!val) continue; |
| 328 |
static char const *wh[]= {"setForAllTools", "noFullScreen[zoom]","fl2","autoscroll[fatbits]","center[createObject]","remap[color]"}; |
| 329 |
f << wh[i]; |
| 330 |
if (val!=1) |
| 331 |
f << "=" << val; |
| 332 |
f << ","; |
| 333 |
} |
| 334 |
val=static_cast<int>(input->readLong(2)); |
| 335 |
if (val<0) { |
| 336 |
f << "option[first]=invertPattern,"; |
| 337 |
val*=-1; |
| 338 |
} |
| 339 |
switch (val) { |
| 340 |
case 1: |
| 341 |
break; |
| 342 |
case 2: |
| 343 |
f << "option[effect]=transp,"; |
| 344 |
break; |
| 345 |
case 3: |
| 346 |
f << "option[effect]=invert,"; |
| 347 |
break; |
| 348 |
case 4: |
| 349 |
f << "option[effect]=erase,"; |
| 350 |
break; |
| 351 |
default: |
| 352 |
f << "###option[effect]=" << val << ","; |
| 353 |
break; |
| 354 |
} |
| 355 |
val=static_cast<int>(input->readLong(2)); // numColors? |
| 356 |
if (val!=0x100) f << "g0=" << val << ","; |
| 357 |
val=static_cast<int>(input->readLong(2)); |
| 358 |
if (val!=0x80) f << "g1=" << val << ","; |
| 359 |
ascii().addPos(pos); |
| 360 |
ascii().addNote(f.str().c_str()); |
| 361 |
input->seek(0x426, librevenge::RVNG_SEEK_SET); |
| 362 |
return true; |
| 363 |
} |
| 364 |
|
| 365 |
bool PixelPaintParser::readFileHeaderV2(bool onlyCheck) |
| 366 |
{ |
| 367 |
MWAWInputStreamPtr input = getInput(); |
| 368 |
if (!input->checkPosition(58)) { |
| 369 |
MWAW_DEBUG_MSG(("PixelPaintParser::readFileHeaderV2: file is too short\n")); |
| 370 |
return false; |
| 371 |
} |
| 372 |
input->seek(4, librevenge::RVNG_SEEK_SET); // seek file def |
| 373 |
libmwaw::DebugStream f; |
| 374 |
f << "Entries(Zone0):"; |
| 375 |
long pos=4; |
| 376 |
int val; |
| 377 |
for (int i=0; i<2; ++i) { |
| 378 |
val=static_cast<int>(input->readULong(2)); |
| 379 |
if (val) |
| 380 |
f << "f" << i << "=" << val << ","; |
| 381 |
} |
| 382 |
int dim[2]; |
| 383 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 384 |
if (dim[0]<=0||dim[0]>1024 || dim[1]<=0 || dim[1]>1024) |
| 385 |
return false; |
| 386 |
if (onlyCheck) { |
| 387 |
input->seek(58, librevenge::RVNG_SEEK_SET); |
| 388 |
return true; |
| 389 |
} |
| 390 |
m_state->m_bitmapSize=MWAWVec2i(dim[1], dim[0]); |
| 391 |
f << "sz=" << m_state->m_bitmapSize << ","; |
| 392 |
for (int i=0; i<5; ++i) { |
| 393 |
val=static_cast<int>(input->readULong(2)); |
| 394 |
static int const expected[]= {8, 0, 0, 0, 0xff }; |
| 395 |
if (val==expected[i]) continue; |
| 396 |
if (i==0) // checkme |
| 397 |
f << "font[sz]=" << val << ","; |
| 398 |
else |
| 399 |
f << "f" << i << "=" << val << ","; |
| 400 |
} |
| 401 |
for (int i=0; i<18; ++i) { // g9=256 : numColor? |
| 402 |
val=static_cast<int>(input->readULong(2)); |
| 403 |
if (val) |
| 404 |
f << "g" << i << "=" << val << ","; |
| 405 |
} |
| 406 |
input->seek(58, librevenge::RVNG_SEEK_SET); |
| 407 |
ascii().addPos(pos); |
| 408 |
ascii().addNote(f.str().c_str()); |
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
bool PixelPaintParser::readColorMap(bool onlyCheck) |
| 413 |
{ |
| 414 |
MWAWInputStreamPtr input = getInput(); |
| 415 |
long pos=input->tell(); |
| 416 |
if (!input->checkPosition(pos+2048)) { |
| 417 |
MWAW_DEBUG_MSG(("PixelPaintParser::readColorMap: file is too short\n")); |
| 418 |
return false; |
| 419 |
} |
| 420 |
if (onlyCheck) { |
| 421 |
input->seek(pos+2048, librevenge::RVNG_SEEK_SET); |
| 422 |
return true; |
| 423 |
} |
| 424 |
libmwaw::DebugStream f; |
| 425 |
f << "Entries(ColorMap):"; |
| 426 |
m_state->m_colorList.resize(256); |
| 427 |
for (auto &color : m_state->m_colorList) { |
| 428 |
unsigned char c[4]; |
| 429 |
for (auto &col : c) col=static_cast<unsigned char>(input->readULong(2)>>8); |
| 430 |
color=MWAWColor(c[1],c[2],c[3],static_cast<unsigned char>(255-c[0])); |
| 431 |
f << color << ","; |
| 432 |
} |
| 433 |
ascii().addPos(pos); |
| 434 |
ascii().addNote(f.str().c_str()); |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
bool PixelPaintParser::readPatternMap(bool onlyCheck) |
| 439 |
{ |
| 440 |
MWAWInputStreamPtr input = getInput(); |
| 441 |
long pos=input->tell(); |
| 442 |
if (!input->checkPosition(pos+144*8)) { |
| 443 |
MWAW_DEBUG_MSG(("PixelPaintParser::readPatternMap: file is too short\n")); |
| 444 |
return false; |
| 445 |
} |
| 446 |
if (onlyCheck) { |
| 447 |
input->seek(pos+144*8, librevenge::RVNG_SEEK_SET); |
| 448 |
return true; |
| 449 |
} |
| 450 |
libmwaw::DebugStream f; |
| 451 |
f << "Entries(PatternMap):"; |
| 452 |
ascii().addPos(pos); |
| 453 |
ascii().addNote(f.str().c_str()); |
| 454 |
for (int i=0; i<144; ++i) { |
| 455 |
pos=input->tell(); |
| 456 |
f.str(""); |
| 457 |
f << "PatternMap-" << i << ":"; |
| 458 |
input->seek(pos+8, librevenge::RVNG_SEEK_SET); |
| 459 |
ascii().addPos(pos); |
| 460 |
ascii().addNote(f.str().c_str()); |
| 461 |
} |
| 462 |
return true; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
//////////////////////////////////////////////////////////// |
| 467 |
// send data |
| 468 |
//////////////////////////////////////////////////////////// |
| 469 |
bool PixelPaintParser::sendBitmap() |
| 470 |
{ |
| 471 |
MWAWGraphicListenerPtr listener=getGraphicListener(); |
| 472 |
if (!listener) { |
| 473 |
MWAW_DEBUG_MSG(("PixelPaintParser::sendBitmap: can not find the listener\n")); |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
MWAWEmbeddedObject picture; |
| 478 |
if (!m_state->m_bitmap || !m_state->m_bitmap->getBinary(picture)) return false; |
| 479 |
|
| 480 |
MWAWPageSpan const &page=getPageSpan(); |
| 481 |
MWAWPosition pos(MWAWVec2f(float(page.getMarginLeft()),float(page.getMarginRight())), |
| 482 |
MWAWVec2f(float(page.getPageWidth()),float(page.getPageLength())), librevenge::RVNG_INCH); |
| 483 |
pos.setRelativePosition(MWAWPosition::Page); |
| 484 |
pos.m_wrapping = MWAWPosition::WNone; |
| 485 |
listener->insertPicture(pos, picture); |
| 486 |
return true; |
| 487 |
} |
| 488 |
|
| 489 |
bool PixelPaintParser::readBitmapV1(bool onlyCheck) |
| 490 |
{ |
| 491 |
MWAWInputStreamPtr input = getInput(); |
| 492 |
long pos=input->tell(); |
| 493 |
|
| 494 |
libmwaw::DebugStream f; |
| 495 |
f << "Entries(Bitmap):"; |
| 496 |
std::shared_ptr<MWAWPictBitmapIndexed> pict; |
| 497 |
int numColors=256; |
| 498 |
if (!onlyCheck) { |
| 499 |
ascii().addPos(pos); |
| 500 |
ascii().addNote(f.str().c_str()); |
| 501 |
|
| 502 |
if (m_state->m_bitmapSize[0]<=0||m_state->m_bitmapSize[0]>1024 || |
| 503 |
m_state->m_bitmapSize[1]<=0||m_state->m_bitmapSize[1]>1024) { |
| 504 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV1: argh can not find the bitmap size\n")); |
| 505 |
return false; |
| 506 |
} |
| 507 |
if (m_state->m_colorList.empty()) { |
| 508 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV1: argh can not find the color list\n")); |
| 509 |
return false; |
| 510 |
} |
| 511 |
pict.reset(new MWAWPictBitmapIndexed(m_state->m_bitmapSize)); |
| 512 |
numColors=static_cast<int>(m_state->m_colorList.size()); |
| 513 |
pict->setColors(m_state->m_colorList); |
| 514 |
} |
| 515 |
for (int i=0; i<16*1024; ++i) { |
| 516 |
pos=input->tell(); |
| 517 |
auto sz=long(input->readULong(4)); |
| 518 |
long endPos=pos+4+sz; |
| 519 |
if (sz<2 || !input->checkPosition(endPos)) { |
| 520 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 521 |
return false; |
| 522 |
} |
| 523 |
int row=i/16; |
| 524 |
int col=(i%16)*64; |
| 525 |
f.str(""); |
| 526 |
f << "Bitmap[R" << row << "C" << col << "]:"; |
| 527 |
int nPixel=0; |
| 528 |
while (input->tell()+2<=endPos) { // UnpackBits |
| 529 |
auto n=static_cast<int>(input->readULong(1)); |
| 530 |
if (n>=0x81) { |
| 531 |
auto color=static_cast<int>(input->readULong(1)); |
| 532 |
if (color>=numColors) { |
| 533 |
static bool first=true; |
| 534 |
if (first) { |
| 535 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV1: find some bad index\n")); |
| 536 |
first=false; |
| 537 |
} |
| 538 |
f << "###id=" << color << ","; |
| 539 |
color=0; |
| 540 |
} |
| 541 |
for (int c=0; c<0x101-n; ++c) { |
| 542 |
if (!pict || row >= m_state->m_bitmapSize[1] || col >= m_state->m_bitmapSize[0]) |
| 543 |
break; |
| 544 |
pict->set(col++, row, color); |
| 545 |
} |
| 546 |
nPixel+=0x101-n; |
| 547 |
} |
| 548 |
else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression) |
| 549 |
if (input->tell()+n+1>endPos) { |
| 550 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 551 |
break; |
| 552 |
} |
| 553 |
nPixel+=n+1; |
| 554 |
for (int c=0; c<=n; ++c) { |
| 555 |
auto color=static_cast<int>(input->readULong(1)); |
| 556 |
if (color>=numColors) { |
| 557 |
static bool first=true; |
| 558 |
if (first) { |
| 559 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV1: find some bad index\n")); |
| 560 |
first=false; |
| 561 |
} |
| 562 |
f << "###id=" << color << ","; |
| 563 |
color=0; |
| 564 |
} |
| 565 |
if (!pict || row >= m_state->m_bitmapSize[1] || col >= m_state->m_bitmapSize[0]) |
| 566 |
continue; |
| 567 |
pict->set(col++, row, color); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
f << nPixel; |
| 572 |
if (onlyCheck) { |
| 573 |
if (nPixel!=64) return false; |
| 574 |
} |
| 575 |
else { |
| 576 |
if (input->tell()!=endPos) { |
| 577 |
ascii().addDelimiter(input->tell(),'|'); |
| 578 |
f << "###"; |
| 579 |
} |
| 580 |
ascii().addPos(pos); |
| 581 |
ascii().addNote(f.str().c_str()); |
| 582 |
} |
| 583 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 584 |
} |
| 585 |
m_state->m_bitmap=pict; |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
bool PixelPaintParser::readBitmapV2(bool onlyCheck) |
| 590 |
{ |
| 591 |
MWAWInputStreamPtr input = getInput(); |
| 592 |
long pos=input->tell(); |
| 593 |
auto dataSz=static_cast<int>(input->readULong(2)); |
| 594 |
if (dataSz<136+2048 || !input->checkPosition(pos+dataSz)) |
| 595 |
return false; |
| 596 |
libmwaw::DebugStream f; |
| 597 |
f << "Entries(Bitmap)[header]:"; |
| 598 |
int val; |
| 599 |
for (int i=0; i<2; ++i) { // always 0 |
| 600 |
val=static_cast<int>(input->readULong(2)); |
| 601 |
if (val) |
| 602 |
f << "f" << i << "=" << val << ","; |
| 603 |
} |
| 604 |
int dim[4]; |
| 605 |
for (int i=0; i<2; ++i) dim[i]=static_cast<int>(input->readULong(2)); |
| 606 |
if (dim[0]<=0||dim[0]>1024 || dim[1]<=0 || dim[1]>1024) |
| 607 |
return false; |
| 608 |
m_state->m_bitmapSize=MWAWVec2i(dim[1], dim[0]); |
| 609 |
f << "sz=" << m_state->m_bitmapSize << ","; |
| 610 |
for (int i=0; i<5; ++i) { // always 0 |
| 611 |
val=static_cast<int>(input->readULong(2)); |
| 612 |
if (val) |
| 613 |
f << "f" << i+2 << "=" << val << ","; |
| 614 |
} |
| 615 |
for (int i=0; i<9; ++i) { // always 0 |
| 616 |
val=static_cast<int>(input->readULong(2)); |
| 617 |
if (val) |
| 618 |
f << "f" << i+7 << "=" << val << ","; |
| 619 |
} |
| 620 |
for (int i=0; i<5; ++i) { |
| 621 |
val=static_cast<int>(input->readLong(2)); |
| 622 |
static int const expected[]= {0x11, 0x2ff, 0xc00, -1, -1}; |
| 623 |
if (val!=expected[i]) |
| 624 |
f << "g" << i << "=" << val << ","; |
| 625 |
} |
| 626 |
for (int i=0; i<4; ++i) { // always 0 |
| 627 |
val=static_cast<int>(input->readULong(2)); |
| 628 |
if (val) |
| 629 |
f << "g" << i+4 << "=" << val << ","; |
| 630 |
} |
| 631 |
if (!onlyCheck) { |
| 632 |
ascii().addPos(pos); |
| 633 |
ascii().addNote(f.str().c_str()); |
| 634 |
} |
| 635 |
pos=input->tell(); |
| 636 |
f.str(""); |
| 637 |
f << "Bitmap[headerA]:"; |
| 638 |
for (int i=0; i<2; ++i) { |
| 639 |
val=static_cast<int>(input->readLong(2)); |
| 640 |
if (val!=m_state->m_bitmapSize[i]) |
| 641 |
f << "##dim" << i << "=" << val << ","; |
| 642 |
val=static_cast<int>(input->readLong(2)); |
| 643 |
if (val) |
| 644 |
f << "dim" << i << "[low]=" << val << ","; |
| 645 |
} |
| 646 |
for (int i=0; i<9; ++i) { |
| 647 |
val=static_cast<int>(input->readLong(2)); |
| 648 |
static int const expected[]= {0, 0, 1, 0xa, 0, 0, 0x400, 0x400, 0x98 }; |
| 649 |
if (val!=expected[i]) |
| 650 |
f << "f" << i << "=" << val << ","; |
| 651 |
} |
| 652 |
val=static_cast<int>(input->readULong(2)); // 8248|8400 |
| 653 |
if (val) f << "fl?=" << std::hex << val << std::dec << ","; |
| 654 |
for (int i=0; i<2; ++i) { |
| 655 |
val=static_cast<int>(input->readLong(2)); |
| 656 |
if (val) |
| 657 |
f << "g" << i << "=" << val << ","; |
| 658 |
} |
| 659 |
for (int i=0; i<2; ++i) dim[i]=static_cast<int>(input->readULong(2)); |
| 660 |
if (MWAWVec2i(dim[1], dim[0])!=m_state->m_bitmapSize) |
| 661 |
f << "sz2=" << MWAWVec2i(dim[1], dim[0]) << ","; |
| 662 |
|
| 663 |
for (int i=0; i<4; ++i) { |
| 664 |
val=static_cast<int>(input->readLong(2)); |
| 665 |
if (val) |
| 666 |
f << "g" << i+2 << "=" << val << ","; |
| 667 |
} |
| 668 |
for (int i=0; i<15; ++i) { |
| 669 |
val=static_cast<int>(input->readLong(2)); |
| 670 |
static int const expected[]= {0x48, 0, 0x48, 0, 0, 8, 1, 8, 0, 0, 0, 0x1f10, 0, 0, 0 }; |
| 671 |
if (val!=expected[i]) |
| 672 |
f << "g" << i+6 << "=" << val << ","; |
| 673 |
} |
| 674 |
for (int i=0; i<2; ++i) { // fl2=bc5|7bb9|bccc, fl3=8000 |
| 675 |
val=static_cast<int>(input->readULong(2)); |
| 676 |
if (val) f << "fl" << i+1 << "=" << std::hex << val << std::dec << ","; |
| 677 |
} |
| 678 |
val=static_cast<int>(input->readULong(2)); |
| 679 |
if (val!=255) f << "h0=" << val << ","; |
| 680 |
if (!onlyCheck) { |
| 681 |
ascii().addPos(pos); |
| 682 |
ascii().addNote(f.str().c_str()); |
| 683 |
} |
| 684 |
|
| 685 |
pos=input->tell(); |
| 686 |
if (onlyCheck) |
| 687 |
input->seek(pos+2048, librevenge::RVNG_SEEK_SET); |
| 688 |
else { |
| 689 |
f.str(""); |
| 690 |
f << "Bitmap[color]:"; |
| 691 |
// CHECKME: does we want to use this color list or the main color list ? |
| 692 |
m_state->m_colorList.resize(256); |
| 693 |
for (auto &color : m_state->m_colorList) { |
| 694 |
val=static_cast<int>(input->readULong(2)); |
| 695 |
unsigned char c[3]; |
| 696 |
for (auto &col : c) col=static_cast<unsigned char>(input->readULong(2)>>8); |
| 697 |
color=MWAWColor(c[0],c[1],c[2]); |
| 698 |
f << color; |
| 699 |
if (val!=0x800) |
| 700 |
f << "[" << std::hex << val << std::dec << "],"; |
| 701 |
else |
| 702 |
f << ","; |
| 703 |
} |
| 704 |
|
| 705 |
ascii().addPos(pos); |
| 706 |
ascii().addNote(f.str().c_str()); |
| 707 |
|
| 708 |
input->seek(pos+2048, librevenge::RVNG_SEEK_SET); |
| 709 |
} |
| 710 |
|
| 711 |
pos=input->tell(); |
| 712 |
if (!input->checkPosition(pos+18)) |
| 713 |
return false; |
| 714 |
if (!onlyCheck) { |
| 715 |
f.str(""); |
| 716 |
f << "Bitmap[headerB]:"; |
| 717 |
for (int i=0; i<2; ++i) { |
| 718 |
for (auto &d : dim) d=static_cast<int>(input->readULong(2)); |
| 719 |
if (dim[0]||dim[1]||m_state->m_bitmapSize!=MWAWVec2i(dim[3],dim[2])) |
| 720 |
f << "dim" << i << "=" << MWAWBox2i(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])) << ","; |
| 721 |
} |
| 722 |
val=static_cast<int>(input->readLong(2)); // always 0 ? |
| 723 |
if (val) f << "f0=" << val << ","; |
| 724 |
ascii().addPos(pos); |
| 725 |
ascii().addNote(f.str().c_str()); |
| 726 |
} |
| 727 |
|
| 728 |
input->seek(pos+18, librevenge::RVNG_SEEK_SET); |
| 729 |
int numColors=0; |
| 730 |
std::shared_ptr<MWAWPictBitmapIndexed> pict; |
| 731 |
if (!onlyCheck) { |
| 732 |
ascii().addPos(input->tell()); |
| 733 |
ascii().addNote(f.str().c_str()); |
| 734 |
|
| 735 |
if (m_state->m_bitmapSize[0]<=0||m_state->m_bitmapSize[0]>1024 || |
| 736 |
m_state->m_bitmapSize[1]<=0||m_state->m_bitmapSize[1]>1024) { |
| 737 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV2: argh can not find the bitmap size\n")); |
| 738 |
return false; |
| 739 |
} |
| 740 |
if (m_state->m_colorList.empty()) { |
| 741 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV2: argh can not find the color list\n")); |
| 742 |
return false; |
| 743 |
} |
| 744 |
pict.reset(new MWAWPictBitmapIndexed(m_state->m_bitmapSize)); |
| 745 |
numColors=static_cast<int>(m_state->m_colorList.size()); |
| 746 |
pict->setColors(m_state->m_colorList); |
| 747 |
} |
| 748 |
|
| 749 |
for (int row=0; row<m_state->m_bitmapSize[1]; ++row) { |
| 750 |
pos=input->tell(); |
| 751 |
dataSz=static_cast<int>(input->readULong(2)); |
| 752 |
long endPos=pos+2+dataSz; |
| 753 |
if (dataSz<2 || !input->checkPosition(endPos)) { |
| 754 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 755 |
return false; |
| 756 |
} |
| 757 |
if (onlyCheck) { |
| 758 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 759 |
continue; |
| 760 |
} |
| 761 |
f.str(""); |
| 762 |
f << "Bitmap[R" << row << "]:"; |
| 763 |
int col=0, nPixel=0; |
| 764 |
while (input->tell()+2<=endPos) { // UnpackBits |
| 765 |
auto n=static_cast<int>(input->readULong(1)); |
| 766 |
if (n>=0x81) { |
| 767 |
auto color=static_cast<int>(input->readULong(1)); |
| 768 |
if (color>=numColors) { |
| 769 |
static bool first=true; |
| 770 |
if (first) { |
| 771 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV2: find some bad index\n")); |
| 772 |
first=false; |
| 773 |
} |
| 774 |
f << "###id=" << color << ","; |
| 775 |
color=0; |
| 776 |
} |
| 777 |
for (int c=0; c<0x101-n; ++c) { |
| 778 |
if (!pict || col >= m_state->m_bitmapSize[0]) |
| 779 |
break; |
| 780 |
pict->set(col++, row, color); |
| 781 |
} |
| 782 |
nPixel+=0x101-n; |
| 783 |
} |
| 784 |
else { // checkme normally 0x80 is reserved and almost nobody used it (for ending the compression) |
| 785 |
if (input->tell()+n+1>endPos) { |
| 786 |
input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 787 |
break; |
| 788 |
} |
| 789 |
nPixel+=n+1; |
| 790 |
for (int c=0; c<=n; ++c) { |
| 791 |
auto color=static_cast<int>(input->readULong(1)); |
| 792 |
if (color>=numColors) { |
| 793 |
static bool first=true; |
| 794 |
if (first) { |
| 795 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV2: find some bad index\n")); |
| 796 |
first=false; |
| 797 |
} |
| 798 |
f << "###id=" << color << ","; |
| 799 |
color=0; |
| 800 |
} |
| 801 |
if (!pict || col >= m_state->m_bitmapSize[0]) |
| 802 |
continue; |
| 803 |
pict->set(col++, row, color); |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
if (nPixel<m_state->m_bitmapSize[0] || nPixel>m_state->m_bitmapSize[0]+32) { |
| 808 |
static bool first=true; |
| 809 |
if (first) { |
| 810 |
MWAW_DEBUG_MSG(("PixelPaintParser::readBitmapV2: find row with odd number of pixel\n")); |
| 811 |
first=false; |
| 812 |
} |
| 813 |
f << "###numPixel=" << nPixel << ","; |
| 814 |
} |
| 815 |
if (input->tell()!=endPos) { |
| 816 |
ascii().addDelimiter(input->tell(),'|'); |
| 817 |
f << "###"; |
| 818 |
} |
| 819 |
ascii().addPos(pos); |
| 820 |
ascii().addNote(f.str().c_str()); |
| 821 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 822 |
} |
| 823 |
m_state->m_bitmap=pict; |
| 824 |
return true; |
| 825 |
} |
| 826 |
|
| 827 |
//////////////////////////////////////////////////////////// |
| 828 |
// read the header |
| 829 |
//////////////////////////////////////////////////////////// |
| 830 |
bool PixelPaintParser::checkHeader(MWAWHeader *header, bool strict) |
| 831 |
{ |
| 832 |
*m_state = PixelPaintParserInternal::State(); |
| 833 |
MWAWInputStreamPtr input = getInput(); |
| 834 |
if (!input || !input->hasDataFork() || !input->checkPosition(512)) |
| 835 |
return false; |
| 836 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 837 |
if (input->readULong(2)) |
| 838 |
return false; |
| 839 |
int vers=1; |
| 840 |
auto val=static_cast<int>(input->readULong(2)); |
| 841 |
if (val==0x7fff) |
| 842 |
; |
| 843 |
else if (val==0x8000) |
| 844 |
vers=2; |
| 845 |
else |
| 846 |
return false; |
| 847 |
if ((vers==1&&!readFileHeaderV1(true)) || (vers==2&&!readFileHeaderV2(true))) |
| 848 |
return false; |
| 849 |
if (!readColorMap(true) || !readPatternMap(true)) |
| 850 |
return false; |
| 851 |
if (strict) { |
| 852 |
if ((vers==1 && !readBitmapV1(true)) || (vers==2 && !readBitmapV2(true))) |
| 853 |
return false; |
| 854 |
} |
| 855 |
setVersion(vers); |
| 856 |
if (header) |
| 857 |
header->reset(MWAWDocument::MWAW_T_PIXELPAINT, vers, MWAWDocument::MWAW_K_PAINT); |
| 858 |
|
| 859 |
return true; |
| 860 |
} |
| 861 |
|
| 862 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |