| 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 |
/* This header contains code specific to a pict mac file |
| 35 |
*/ |
| 36 |
#include <cstring> |
| 37 |
#include <map> |
| 38 |
#include <sstream> |
| 39 |
#include <string> |
| 40 |
#include <vector> |
| 41 |
|
| 42 |
#include <librevenge/librevenge.h> |
| 43 |
|
| 44 |
#include "libmwaw_internal.hxx" |
| 45 |
#include "MWAWDebug.hxx" |
| 46 |
#include "MWAWInputStream.hxx" |
| 47 |
|
| 48 |
#include "MWAWPictMac.hxx" |
| 49 |
#include "MWAWPictBitmap.hxx" |
| 50 |
|
| 51 |
// and then to save the read bitmap/pixmap |
| 52 |
#define DEBUG_BITMAP 0 |
| 53 |
|
| 54 |
MWAWPictMac::~MWAWPictMac() |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
MWAWPictMac::ReadResult MWAWPictMac::checkOrGet |
| 59 |
(MWAWInputStreamPtr input, int size, MWAWBox2f &box, MWAWPictData **result) |
| 60 |
{ |
| 61 |
if (result) *result=nullptr; |
| 62 |
|
| 63 |
int version, subvers; |
| 64 |
// we can not read the data, ... |
| 65 |
long actualPos = input->tell(); |
| 66 |
if (size < 0xd) |
| 67 |
return MWAW_R_BAD; |
| 68 |
|
| 69 |
auto readSize = int(input->readULong(2)); |
| 70 |
long dim[4]; |
| 71 |
for (auto &d : dim) d = input->readLong(2); |
| 72 |
long lastFlag = input->readLong(2); |
| 73 |
bool empty = false; |
| 74 |
switch (lastFlag) { |
| 75 |
case 0x1101: { |
| 76 |
// some MacDraw Pro pict stores a bigger size 512(file header?)+10? |
| 77 |
if (readSize != size && readSize+1 != size && (actualPos!=512 || readSize != size+512+10)) |
| 78 |
return MWAW_R_BAD; |
| 79 |
version = subvers = 1; |
| 80 |
empty = (size == 0xd); |
| 81 |
break; |
| 82 |
} |
| 83 |
case 0x0011: { |
| 84 |
if (size < 40) return MWAW_R_BAD; |
| 85 |
if (input->readULong(2) != 0x2ff) return MWAW_R_BAD; |
| 86 |
if (input->readULong(2) != 0xC00) return MWAW_R_BAD; |
| 87 |
subvers = -int(input->readLong(2)); |
| 88 |
if (subvers == 1) empty = (size == 42); |
| 89 |
else if (subvers == 2) empty = (size == 40); |
| 90 |
else if (subvers >= -6 && subvers < 6) { |
| 91 |
// find also 0 and -1 and -4 here... |
| 92 |
MWAW_DEBUG_MSG(("MWAWPictMac::checkOrGet: unknown subversion: %d\n", subvers)); |
| 93 |
empty = (size == 0xd); |
| 94 |
} |
| 95 |
else return MWAW_R_BAD; |
| 96 |
version = 2; |
| 97 |
break; |
| 98 |
} |
| 99 |
default: |
| 100 |
return MWAW_R_BAD; |
| 101 |
} |
| 102 |
|
| 103 |
if (empty) { |
| 104 |
input->seek(actualPos+size-1,librevenge::RVNG_SEEK_SET); |
| 105 |
if (input->readULong(1) != 0xff) return MWAW_R_BAD; |
| 106 |
} |
| 107 |
|
| 108 |
box.set(MWAWVec2f(float(dim[1]),float(dim[0])), MWAWVec2f(float(dim[3]),float(dim[2]))); |
| 109 |
if (!empty && (box.size().x() < 0 || box.size().y() < 0)) return MWAW_R_BAD; |
| 110 |
if (box.size().x() <= 0 || box.size().y() <= 0) empty = true; |
| 111 |
|
| 112 |
if (empty) return MWAW_R_OK_EMPTY; |
| 113 |
if (!result) return MWAW_R_OK; |
| 114 |
|
| 115 |
auto *res = new MWAWPictMac(box); |
| 116 |
res->m_version = version; |
| 117 |
res->m_subVersion = subvers; |
| 118 |
*result = res; |
| 119 |
|
| 120 |
// let caller read the data |
| 121 |
return MWAW_R_OK; |
| 122 |
} |
| 123 |
/** Internal and low level: generic tools about Mac Pict1.0 picture |
| 124 |
* |
| 125 |
* This regroups some functions to parse them and to convert them in Pict2.0 picture |
| 126 |
*/ |
| 127 |
namespace libmwaw_applepict1 |
| 128 |
{ |
| 129 |
/** Internal and low level: the different types of arguments. |
| 130 |
* |
| 131 |
* By default, data are signed, excepted if we add U to indicate that they are unsigned, |
| 132 |
* - WP_PATTERN: 8x8bits which defined a 8x8 picture (black or white) |
| 133 |
* - WP_COLOR: 3 bits which defined r,g,b (checkme) |
| 134 |
* - for BITMAP, R indicates Region bitmap while P indicates Packed bitmap |
| 135 |
*/ |
| 136 |
enum DataType { |
| 137 |
WP_NONE, WP_BYTE, WP_UBYTE, WP_INT, WP_UINT, WP_UFIXED, |
| 138 |
WP_COLOR, WP_PATTERN, WP_POINT, WP_POINTBYTE, WP_POINTUBYTE, WP_POLY, WP_RECT, WP_REGION, WP_TEXT, WP_LTEXT, |
| 139 |
WP_BITMAP, WP_RBITMAP, WP_PBITMAP, WP_RPBITMAP, WP_UNKNOWN |
| 140 |
}; |
| 141 |
|
| 142 |
/** Internal and low level: class used to read/store a picture region |
| 143 |
* |
| 144 |
* A region is formed by bounding box followed by an array of bits |
| 145 |
* which indicate which defines a mask */ |
| 146 |
class Region |
| 147 |
{ |
| 148 |
public: |
| 149 |
Region() |
| 150 |
: m_box() |
| 151 |
, m_points() |
| 152 |
{ |
| 153 |
} |
| 154 |
//! operator << for a Region |
| 155 |
friend std::ostream &operator<< (std::ostream &o, Region const &f) |
| 156 |
{ |
| 157 |
o << "reg=" << f.m_box; |
| 158 |
if (f.m_points.size()==0) return o; |
| 159 |
o << ", ["; |
| 160 |
for (size_t c = 0; c < f.m_points.size(); c++) { |
| 161 |
if (c) o << ","; |
| 162 |
o << f.m_points[c]; |
| 163 |
} |
| 164 |
o << "]"; |
| 165 |
return o; |
| 166 |
} |
| 167 |
//! tries to read the data |
| 168 |
bool read(MWAWInputStream &input) |
| 169 |
{ |
| 170 |
long actualPos = input.tell(); |
| 171 |
|
| 172 |
// the region size |
| 173 |
auto sz = static_cast<int>(input.readULong(2)); |
| 174 |
if ((sz%2) != 0 || sz<10 || !input.checkPosition(actualPos+sz)) { |
| 175 |
MWAW_DEBUG_MSG(("Pict1:Region: read odd size: %d\n", sz)); |
| 176 |
return false; |
| 177 |
} |
| 178 |
sz /= 2; |
| 179 |
int dim[4]; |
| 180 |
for (auto &d : dim) d = static_cast<int>(input.readLong(2)); |
| 181 |
m_box.set(MWAWVec2i(dim[1], dim[0]), MWAWVec2i(dim[3], dim[2])); |
| 182 |
sz -= 5; |
| 183 |
m_points.resize(0); |
| 184 |
if (sz == 0) return true; |
| 185 |
// une liste de point dans la box: x1, y1, .. yn 0x7fff, x2, ... 0x7fff |
| 186 |
while (sz > 0) { |
| 187 |
auto y = static_cast<int>(input.readLong(2)); |
| 188 |
sz--; |
| 189 |
if (y == 0x7fff) break; |
| 190 |
if (y < m_box[0].y() || y > m_box[1].y()) { |
| 191 |
MWAW_DEBUG_MSG(("Pict1:Region: found eroneous y value: %d\n", y)); |
| 192 |
return false; |
| 193 |
} |
| 194 |
bool endF = false; |
| 195 |
while (sz > 0) { |
| 196 |
auto x = static_cast<int>(input.readLong(2)); |
| 197 |
sz--; |
| 198 |
if (x == 0x7fff) { |
| 199 |
endF = true; |
| 200 |
break; |
| 201 |
} |
| 202 |
if (x < m_box[0].x() || x > m_box[1].x()) { |
| 203 |
MWAW_DEBUG_MSG(("Pict1:Region: found eroneous x value %d\n", x)); |
| 204 |
return false; |
| 205 |
} |
| 206 |
m_points.push_back(MWAWVec2i(x,y)); |
| 207 |
} |
| 208 |
if (!endF) { |
| 209 |
MWAW_DEBUG_MSG(("Pict1:Region: does not find end of file...\n")); |
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
if (sz) { |
| 214 |
MWAW_DEBUG_MSG(("Pict1:Region: find some remaining data ...\n")); |
| 215 |
return false; |
| 216 |
} |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
protected: |
| 222 |
//! the bounding box |
| 223 |
MWAWBox2i m_box; |
| 224 |
//! the set of points which defines the mask |
| 225 |
std::vector<MWAWVec2i> m_points; |
| 226 |
}; |
| 227 |
|
| 228 |
//! Internal and low level: a class used to read pack/unpack black-white bitmap |
| 229 |
struct Bitmap { |
| 230 |
Bitmap() |
| 231 |
: m_rowBytes() |
| 232 |
, m_rect() |
| 233 |
, m_src() |
| 234 |
, m_dst() |
| 235 |
, m_region() |
| 236 |
, m_bitmap() |
| 237 |
, m_mode(0) |
| 238 |
{ |
| 239 |
} |
| 240 |
//! tries to read a bitmap |
| 241 |
bool read(MWAWInputStream &input, bool packed, bool hasRegion) |
| 242 |
{ |
| 243 |
m_rowBytes = static_cast<int>(input.readULong(2)); |
| 244 |
m_rowBytes &= 0x3FFF; |
| 245 |
if (m_rowBytes < 0 || (!packed && m_rowBytes > 8)) { |
| 246 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: find odd rowBytes %d... \n", m_rowBytes)); |
| 247 |
return false; |
| 248 |
} |
| 249 |
// read the rectangle: bound |
| 250 |
// ------ end of bitmap ---------- |
| 251 |
// and the two general rectangle src, dst |
| 252 |
for (int c = 0; c < 3; c++) { |
| 253 |
int dim[4]; |
| 254 |
for (auto &d : dim) d = static_cast<int>(input.readLong(2)); |
| 255 |
MWAWBox2i box(MWAWVec2i(dim[1],dim[0]), MWAWVec2i(dim[3],dim[2])); |
| 256 |
if (box.size().x() <= 0 || box.size().y() <= 0) { |
| 257 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: find odd rectangle %d... \n", c)); |
| 258 |
return false; |
| 259 |
} |
| 260 |
if (c == 0) m_rect=box; |
| 261 |
else if (c==1) m_src = box; |
| 262 |
else m_dst = box; |
| 263 |
} |
| 264 |
|
| 265 |
if (!packed && m_rowBytes*8 < m_rect.size().x()) { |
| 266 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: row bytes seems to short: %d/%d... \n", m_rowBytes*8, m_rect.size().y())); |
| 267 |
return false; |
| 268 |
} |
| 269 |
m_mode = static_cast<int>(input.readLong(2)); // mode: I find 0,1 and 3 |
| 270 |
if (m_mode < 0 || m_mode > 64) { |
| 271 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: unknown mode: %d \n", m_mode)); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
if (hasRegion) { // CHECKME... |
| 276 |
std::shared_ptr<Region>rgn(new Region); |
| 277 |
if (!rgn->read(input)) return false; |
| 278 |
m_region = rgn; |
| 279 |
} |
| 280 |
if (!readBitmapData(input, packed)) return false; |
| 281 |
|
| 282 |
if (input.isEnd()) { |
| 283 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: EOF \n")); |
| 284 |
return false; |
| 285 |
} |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
//! operator<< for Bitmap |
| 290 |
friend std::ostream &operator<< (std::ostream &o, Bitmap const &f) |
| 291 |
{ |
| 292 |
o << "rDim=" << f.m_rowBytes << ", " << f.m_rect << ", " << f.m_src << ", " << f.m_dst; |
| 293 |
if (f.m_region.get()) o << ", " << *f.m_region; |
| 294 |
|
| 295 |
if (f.m_mode >= 0 && f.m_mode < 16) { |
| 296 |
static char const *mode0[] = { // 0-15 |
| 297 |
"srcCopy", "srcOr", "srcXOr", "srcBic", |
| 298 |
"notSrcCopy", "notSrcOr", "notSrcXOr", "notSrcBic", |
| 299 |
"patCopy", "patOr", "patXOr", "patBic", |
| 300 |
"notPatCopy", "notPatOr", "notPatXOr", "notPatBic" |
| 301 |
}; |
| 302 |
o << ", " << mode0[f.m_mode] << ", [...]"; |
| 303 |
} |
| 304 |
else if (f.m_mode >= 32 && f.m_mode < 40) { |
| 305 |
static char const *mode1[] = { // 32-39 |
| 306 |
"blend", "addPin", "addOver", "subPin", |
| 307 |
"transparent", "addMax", "subOver", "addMin" |
| 308 |
}; |
| 309 |
o << ", " << mode1[f.m_mode-32] << ", [...]"; |
| 310 |
} |
| 311 |
else if (f.m_mode == 49) o << ", grayishTextOr, [...]"; |
| 312 |
else if (f.m_mode == 50) o << ", hilitetransfermode, [...]"; |
| 313 |
else if (f.m_mode == 64) o << ", ditherCopy, [...]"; |
| 314 |
else |
| 315 |
o << ", ###mod=" << f.m_mode << ", [...]"; |
| 316 |
#if DEBUG_BITMAP |
| 317 |
f.saveBitmap(); |
| 318 |
#endif |
| 319 |
return o; |
| 320 |
} |
| 321 |
|
| 322 |
//! saves the bitmap in file (debugging function) |
| 323 |
bool saveBitmap() const |
| 324 |
{ |
| 325 |
if (m_rowBytes <= 0) return false; |
| 326 |
int nRows = int(m_bitmap.size())/m_rowBytes; |
| 327 |
MWAWPictBitmapBW bitmap(MWAWVec2i(m_rect.size().x(),nRows)); |
| 328 |
if (!bitmap.valid()) return false; |
| 329 |
|
| 330 |
for (int i = 0; i < nRows; i++) |
| 331 |
bitmap.setRowPacked(i, &m_bitmap[size_t(i*m_rowBytes)], &m_bitmap[0] + m_bitmap.size()); |
| 332 |
MWAWEmbeddedObject picture; |
| 333 |
if (!bitmap.getBinary(picture) || picture.m_dataList.empty()) return false; |
| 334 |
|
| 335 |
static int ppmNumber = 0; |
| 336 |
std::stringstream f; |
| 337 |
f << "PictBitmap" << ppmNumber++ << ".pbm"; |
| 338 |
return libmwaw::Debug::dumpFile(picture.m_dataList[0], f.str().c_str()); |
| 339 |
} |
| 340 |
|
| 341 |
//! creates the bitmap from the packdata |
| 342 |
bool unpackedData(unsigned char const *pData, int sz) |
| 343 |
{ |
| 344 |
int rPos = 0; |
| 345 |
size_t wPos = m_bitmap.size(), wNPos = wPos+size_t(m_rowBytes); |
| 346 |
m_bitmap.resize(size_t(wNPos)); |
| 347 |
|
| 348 |
while (rPos < sz) { |
| 349 |
if (rPos+2 > sz) return false; |
| 350 |
auto n = static_cast<signed char>(pData[rPos++]); |
| 351 |
if (n < 0) { |
| 352 |
int nCount = 1-n; |
| 353 |
if (wPos+size_t(nCount) > wNPos) return false; |
| 354 |
|
| 355 |
unsigned char val = pData[rPos++]; |
| 356 |
for (int i = 0; i < nCount; i++) |
| 357 |
m_bitmap[wPos++] = val; |
| 358 |
continue; |
| 359 |
} |
| 360 |
int nCount = 1+n; |
| 361 |
if (rPos+nCount > sz || wPos+size_t(nCount) > wNPos) return false; |
| 362 |
for (int i = 0; i < nCount; i++) |
| 363 |
m_bitmap[wPos++] = pData[rPos++]; |
| 364 |
} |
| 365 |
return (wPos == wNPos); |
| 366 |
} |
| 367 |
|
| 368 |
//! parses the bitmap data zone |
| 369 |
bool readBitmapData(MWAWInputStream &input, bool packed) |
| 370 |
{ |
| 371 |
int numRows = m_rect.size().y(), szRowSize=1; |
| 372 |
|
| 373 |
if (packed) { |
| 374 |
// CHECKME: the limit(1/2 bytes) is probably 251: the value for a Pict2.0 |
| 375 |
// from collected data files, we have 246 < limit < 254 |
| 376 |
if (m_rowBytes > 250) szRowSize = 2; |
| 377 |
} |
| 378 |
else |
| 379 |
m_bitmap.resize(size_t(numRows*m_rowBytes)); |
| 380 |
|
| 381 |
size_t pos=0; |
| 382 |
for (int i = 0; i < numRows; i++) { |
| 383 |
if (input.isEnd()) break; |
| 384 |
|
| 385 |
if (!packed) { |
| 386 |
unsigned long numR = 0; |
| 387 |
unsigned char const *data = input.read(size_t(m_rowBytes), numR); |
| 388 |
if (!data || int(numR) != m_rowBytes) { |
| 389 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: can not read line %d/%d (%d chars)\n", i, numRows, m_rowBytes)); |
| 390 |
return false; |
| 391 |
} |
| 392 |
for (int j = 0; j < m_rowBytes; j++) |
| 393 |
m_bitmap[pos++]=data[j]; |
| 394 |
} |
| 395 |
else { |
| 396 |
auto numB = static_cast<int>(input.readULong(szRowSize)); |
| 397 |
if (numB < 0 || numB > 2*m_rowBytes) { |
| 398 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: odd numB:%d in row: %d/%d\n", numB, i, numRows)); |
| 399 |
return false; |
| 400 |
} |
| 401 |
unsigned long numR = 0; |
| 402 |
unsigned char const *data = input.read(size_t(numB), numR); |
| 403 |
if (!data || int(numR) != numB) { |
| 404 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: can not read line %d/%d (%d chars)\n", i, numRows, numB)); |
| 405 |
return false; |
| 406 |
} |
| 407 |
if (!unpackedData(data,numB)) { |
| 408 |
MWAW_DEBUG_MSG(("Pict1:Bitmap: can not unpacked line:%d\n", i)); |
| 409 |
return false; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
return true; |
| 414 |
} |
| 415 |
//! the num of bytes used to store a row |
| 416 |
int m_rowBytes; |
| 417 |
MWAWBox2i m_rect /** the bitmap rectangle */, m_src/** the initial dimension */, /** another final dimension */ m_dst ; |
| 418 |
//! the region |
| 419 |
std::shared_ptr<Region> m_region; |
| 420 |
//! the bitmap |
| 421 |
std::vector<unsigned char> m_bitmap; |
| 422 |
//! the encoding mode ? |
| 423 |
int m_mode; |
| 424 |
}; |
| 425 |
|
| 426 |
//! Internal and low level: a class used to read and store all possible value |
| 427 |
struct Value { |
| 428 |
Value() |
| 429 |
: m_type() |
| 430 |
, m_int(0) |
| 431 |
, m_rgb(MWAWColor::white()) |
| 432 |
, m_text("") |
| 433 |
, m_point() |
| 434 |
, m_box() |
| 435 |
, m_listPoint() |
| 436 |
, m_region() |
| 437 |
, m_bitmap() |
| 438 |
{ |
| 439 |
for (auto &p : m_pat) p=0; |
| 440 |
} |
| 441 |
Value(Value const &)=default; |
| 442 |
Value &operator=(Value const &)=default; |
| 443 |
virtual ~Value(); |
| 444 |
|
| 445 |
/** the stored type of the data |
| 446 |
* |
| 447 |
* This can only be WP_INT, WP_COLOR, WP_PATTERN, WP_POINT, WP_POLY, WP_RECT, WP_REGION, WP_TEXT, WP_BITMAP */ |
| 448 |
DataType m_type; |
| 449 |
|
| 450 |
//! operator<< for Value |
| 451 |
friend std::ostream &operator<< (std::ostream &o, Value const &f) |
| 452 |
{ |
| 453 |
switch (f.m_type) { |
| 454 |
case WP_INT: |
| 455 |
o << f.m_int; |
| 456 |
break; |
| 457 |
case WP_COLOR: |
| 458 |
o << "col=(" << f.m_rgb << ")"; |
| 459 |
break; |
| 460 |
case WP_PATTERN: |
| 461 |
o << "pat=(" << std::hex; |
| 462 |
for (int c= 0; c < 8; c++) { |
| 463 |
if (c) o << ","; |
| 464 |
o << f.m_pat[c]; |
| 465 |
} |
| 466 |
o << ")" << std::dec; |
| 467 |
break; |
| 468 |
case WP_POINT: |
| 469 |
o << f.m_point; |
| 470 |
break; |
| 471 |
case WP_RECT: |
| 472 |
o << f.m_box; |
| 473 |
break; |
| 474 |
case WP_REGION: |
| 475 |
if (f.m_region.get()) { |
| 476 |
o << *f.m_region; |
| 477 |
break; |
| 478 |
} |
| 479 |
MWAW_DEBUG_MSG(("Pict1:Value: I do not find my region... \n")); |
| 480 |
break; |
| 481 |
case WP_POLY: |
| 482 |
o << "[reg=" << f.m_box << ":"; |
| 483 |
for (size_t c = 0; c < f.m_listPoint.size(); c++) { |
| 484 |
if (c) o << ","; |
| 485 |
o << f.m_listPoint[c]; |
| 486 |
} |
| 487 |
o << "]"; |
| 488 |
break; |
| 489 |
case WP_TEXT: |
| 490 |
o << "\"" << f.m_text << "\""; |
| 491 |
break; |
| 492 |
case WP_BITMAP: |
| 493 |
if (f.m_bitmap.get()) { |
| 494 |
o << *f.m_bitmap; |
| 495 |
break; |
| 496 |
} |
| 497 |
MWAW_DEBUG_MSG(("Pict1:Value: I do not find my bitmap... \n")); |
| 498 |
break; |
| 499 |
case WP_NONE: |
| 500 |
case WP_BYTE: |
| 501 |
case WP_UBYTE: |
| 502 |
case WP_UINT: |
| 503 |
case WP_UFIXED: |
| 504 |
case WP_POINTBYTE: |
| 505 |
case WP_POINTUBYTE: |
| 506 |
case WP_LTEXT: |
| 507 |
case WP_RBITMAP: |
| 508 |
case WP_PBITMAP: |
| 509 |
case WP_RPBITMAP: |
| 510 |
case WP_UNKNOWN: |
| 511 |
#if !defined(__clang__) |
| 512 |
default: |
| 513 |
#endif |
| 514 |
MWAW_DEBUG_MSG(("Pict1:Value: does not know how to print my values... \n")); |
| 515 |
} |
| 516 |
return o; |
| 517 |
} |
| 518 |
|
| 519 |
//! the int value when type=WP_INT |
| 520 |
int m_int; |
| 521 |
//! the color when type=WP_COLOR |
| 522 |
MWAWColor m_rgb; |
| 523 |
//! the pattern when type=WP_PATTERN |
| 524 |
int m_pat[8]; |
| 525 |
//! the text when type=WP_TEXT |
| 526 |
std::string m_text; |
| 527 |
//! the point when type=WP_POINT |
| 528 |
MWAWVec2i m_point; |
| 529 |
//! the rectangle when type=WP_RECT |
| 530 |
MWAWBox2i m_box; |
| 531 |
//! the list of points which defined the polygon when type=WP_POLY |
| 532 |
std::vector<MWAWVec2i> m_listPoint; |
| 533 |
//! the region when type=WP_REGION |
| 534 |
std::shared_ptr<Region> m_region; |
| 535 |
//! the bitmap when type=WP_BITMAP |
| 536 |
std::shared_ptr<Bitmap> m_bitmap; |
| 537 |
}; |
| 538 |
|
| 539 |
Value::~Value() |
| 540 |
{ |
| 541 |
} |
| 542 |
|
| 543 |
//! Internal and low level: a class to define each opcode and their arguments and read their data |
| 544 |
struct OpCode { |
| 545 |
/** constructor |
| 546 |
* |
| 547 |
* \param id is the code of the opcode in the file |
| 548 |
* \param nm is the short name of the opcode |
| 549 |
* \param type1 type of the first component |
| 550 |
* \param type2 type of the second component (if it exists) |
| 551 |
* \param type3 type of the third component (if it exists) |
| 552 |
* \param type4 type of the fourst component (if it exists) |
| 553 |
* \param type5 type of the fifth component (if it exists) |
| 554 |
*/ |
| 555 |
OpCode(int id, char const *nm, DataType type1=WP_NONE, DataType type2=WP_NONE, DataType type3=WP_NONE, DataType type4=WP_NONE, DataType type5=WP_NONE) |
| 556 |
: m_id(id) |
| 557 |
, m_name(nm) |
| 558 |
, m_types() |
| 559 |
{ |
| 560 |
if (type1==WP_NONE) return; |
| 561 |
else m_types.push_back(type1); |
| 562 |
if (type2==WP_NONE) return; |
| 563 |
else m_types.push_back(type2); |
| 564 |
if (type3==WP_NONE) return; |
| 565 |
else m_types.push_back(type3); |
| 566 |
if (type4==WP_NONE) return; |
| 567 |
else m_types.push_back(type4); |
| 568 |
if (type5==WP_NONE) return; |
| 569 |
else m_types.push_back(type5); |
| 570 |
} |
| 571 |
OpCode(OpCode const &)=default; |
| 572 |
virtual ~OpCode(); |
| 573 |
|
| 574 |
/** tries to read the data in the file |
| 575 |
* |
| 576 |
* If the read is succefull, fills listValue with the read argument */ |
| 577 |
bool readData(MWAWInputStream &input, std::vector<Value> &listValue) const |
| 578 |
{ |
| 579 |
size_t numTypes = m_types.size(); |
| 580 |
listValue.resize(numTypes); |
| 581 |
Value newVal; |
| 582 |
for (size_t i = 0; i < numTypes; i++) { |
| 583 |
long actualPos = input.tell(); |
| 584 |
if (readValue(input, m_types[i], newVal)) { |
| 585 |
listValue[i] = newVal; |
| 586 |
continue; |
| 587 |
} |
| 588 |
input.seek(actualPos, librevenge::RVNG_SEEK_SET); |
| 589 |
return false; |
| 590 |
} |
| 591 |
return true; |
| 592 |
} |
| 593 |
|
| 594 |
//! computes the size of the data |
| 595 |
bool computeSize(MWAWInputStream &input, int &sz) const |
| 596 |
{ |
| 597 |
long actPos = input.tell(); |
| 598 |
sz = 0; |
| 599 |
|
| 600 |
for (auto type : m_types) { |
| 601 |
input.seek(actPos+sz, librevenge::RVNG_SEEK_SET); |
| 602 |
int newSz = getSize(input, type); |
| 603 |
if (newSz < 0) return false; |
| 604 |
sz += newSz; |
| 605 |
} |
| 606 |
input.seek(actPos, librevenge::RVNG_SEEK_SET); |
| 607 |
return true; |
| 608 |
} |
| 609 |
|
| 610 |
/** read a rectangles field |
| 611 |
|
| 612 |
\note can be used to read the first dimensions of a picture */ |
| 613 |
static bool readRect(MWAWInputStream &input, DataType type, MWAWBox2i &res) |
| 614 |
{ |
| 615 |
DataType valType; |
| 616 |
switch (type) { |
| 617 |
case WP_RECT: |
| 618 |
valType = WP_POINT; |
| 619 |
break; |
| 620 |
case WP_NONE: |
| 621 |
case WP_BYTE: |
| 622 |
case WP_UBYTE: |
| 623 |
case WP_INT: |
| 624 |
case WP_UINT: |
| 625 |
case WP_UFIXED: |
| 626 |
case WP_COLOR: |
| 627 |
case WP_PATTERN: |
| 628 |
case WP_POINT: |
| 629 |
case WP_POINTBYTE: |
| 630 |
case WP_POINTUBYTE: |
| 631 |
case WP_POLY: |
| 632 |
case WP_REGION: |
| 633 |
case WP_TEXT: |
| 634 |
case WP_LTEXT: |
| 635 |
case WP_BITMAP: |
| 636 |
case WP_RBITMAP: |
| 637 |
case WP_PBITMAP: |
| 638 |
case WP_RPBITMAP: |
| 639 |
case WP_UNKNOWN: |
| 640 |
#if !defined(__clang__) |
| 641 |
default: |
| 642 |
#endif |
| 643 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readRect is called with %d\n", type)); |
| 644 |
return false; |
| 645 |
}; |
| 646 |
MWAWVec2i v[2]; |
| 647 |
for (auto &val : v) { |
| 648 |
if (!readPoint(input, valType, val)) return false; |
| 649 |
} |
| 650 |
res.set(v[0], v[1]); |
| 651 |
return true; |
| 652 |
} |
| 653 |
//! the opCode |
| 654 |
int m_id; |
| 655 |
//! the opCode name |
| 656 |
std::string m_name; |
| 657 |
//! the different argument types |
| 658 |
std::vector<DataType> m_types; |
| 659 |
|
| 660 |
protected: |
| 661 |
/** returns the size of the next argument of type \a type. |
| 662 |
* |
| 663 |
* \note This function can update the next reading position in the input, if it uses the input to compute the size of this argument */ |
| 664 |
static int getSize(MWAWInputStream &input, DataType type) |
| 665 |
{ |
| 666 |
switch (type) { |
| 667 |
case WP_BYTE: |
| 668 |
case WP_UBYTE: |
| 669 |
return 1; |
| 670 |
case WP_INT: |
| 671 |
case WP_UINT: |
| 672 |
case WP_POINTBYTE: |
| 673 |
case WP_POINTUBYTE: |
| 674 |
return 2; |
| 675 |
case WP_UFIXED: |
| 676 |
case WP_COLOR: |
| 677 |
case WP_POINT: |
| 678 |
return 4; |
| 679 |
case WP_PATTERN: |
| 680 |
case WP_RECT: |
| 681 |
return 8; |
| 682 |
case WP_POLY: |
| 683 |
case WP_REGION: |
| 684 |
return static_cast<int>(input.readULong(2)); |
| 685 |
case WP_TEXT: |
| 686 |
return 1+static_cast<int>(input.readULong(1)); |
| 687 |
case WP_LTEXT: |
| 688 |
return 2+static_cast<int>(input.readULong(2)); |
| 689 |
case WP_BITMAP: |
| 690 |
case WP_RBITMAP: |
| 691 |
case WP_PBITMAP: |
| 692 |
case WP_RPBITMAP: { |
| 693 |
// can not guess, so we read the bitmap... |
| 694 |
long actPos = input.tell(); |
| 695 |
bool packed = type==WP_PBITMAP || type == WP_RPBITMAP; |
| 696 |
bool hasRgn = type ==WP_RBITMAP || type == WP_RPBITMAP; |
| 697 |
std::shared_ptr<Bitmap> btmap(new Bitmap); |
| 698 |
if (!btmap->read(input, packed, hasRgn)) return -1; |
| 699 |
return int(input.tell()-actPos); |
| 700 |
} |
| 701 |
case WP_UNKNOWN: |
| 702 |
case WP_NONE: |
| 703 |
#if !defined(__clang__) |
| 704 |
default: |
| 705 |
#endif |
| 706 |
break; |
| 707 |
} |
| 708 |
return -1; |
| 709 |
} |
| 710 |
//! reads a argument of type \a type, if successfull updates \a val. |
| 711 |
static bool readValue(MWAWInputStream &input, DataType type, Value &val) |
| 712 |
{ |
| 713 |
switch (type) { |
| 714 |
case WP_BYTE: |
| 715 |
case WP_UBYTE: |
| 716 |
case WP_INT: |
| 717 |
case WP_UINT: |
| 718 |
case WP_UFIXED: |
| 719 |
val.m_type = WP_INT; |
| 720 |
return readInt(input, type, val.m_int); |
| 721 |
case WP_COLOR: |
| 722 |
val.m_type = WP_COLOR; |
| 723 |
return readColor(input, type, val.m_rgb); |
| 724 |
case WP_PATTERN: |
| 725 |
val.m_type = WP_PATTERN; |
| 726 |
return readPattern(input, type, val.m_pat); |
| 727 |
case WP_POINT: |
| 728 |
case WP_POINTBYTE: |
| 729 |
case WP_POINTUBYTE: |
| 730 |
val.m_type = WP_POINT; |
| 731 |
return readPoint(input, type, val.m_point); |
| 732 |
case WP_POLY: |
| 733 |
val.m_type = WP_POLY; |
| 734 |
return readPoly(input, type, val.m_box, val.m_listPoint); |
| 735 |
case WP_RECT: |
| 736 |
val.m_type = WP_RECT; |
| 737 |
return readRect(input, type, val.m_box); |
| 738 |
case WP_REGION: { |
| 739 |
std::shared_ptr<Region> rgn(new Region); |
| 740 |
if (!rgn->read(input)) return false; |
| 741 |
val.m_type = WP_REGION; |
| 742 |
val.m_region = rgn; |
| 743 |
return true; |
| 744 |
} |
| 745 |
case WP_TEXT: |
| 746 |
case WP_LTEXT: |
| 747 |
val.m_type = WP_TEXT; |
| 748 |
return readText(input, type, val.m_text); |
| 749 |
case WP_BITMAP: |
| 750 |
case WP_RBITMAP: |
| 751 |
case WP_PBITMAP: |
| 752 |
case WP_RPBITMAP: { |
| 753 |
bool packed = type==WP_PBITMAP || type == WP_RPBITMAP; |
| 754 |
bool hasRgn = type ==WP_RBITMAP || type == WP_RPBITMAP; |
| 755 |
std::shared_ptr<Bitmap> btmap(new Bitmap); |
| 756 |
if (!btmap->read(input, packed, hasRgn)) return false; |
| 757 |
val.m_type = WP_BITMAP; |
| 758 |
val.m_bitmap = btmap; |
| 759 |
return true; |
| 760 |
} |
| 761 |
case WP_UNKNOWN: |
| 762 |
MWAW_DEBUG_MSG(("Pict1:readValue: find unknown type... \n")); |
| 763 |
break; |
| 764 |
case WP_NONE: |
| 765 |
#if !defined(__clang__) |
| 766 |
default: |
| 767 |
#endif |
| 768 |
MWAW_DEBUG_MSG(("Pict1:readValue: does not know how to read type %d... \n", type)); |
| 769 |
} |
| 770 |
return false; |
| 771 |
} |
| 772 |
//! low level: reads a integer ( bytes or 2 bytes, signed or unsigned) |
| 773 |
static bool readInt(MWAWInputStream &input, DataType type, int &res) |
| 774 |
{ |
| 775 |
int sz = 0; |
| 776 |
long actualPos = input.tell(); |
| 777 |
res = 0; |
| 778 |
switch (type) { |
| 779 |
case WP_BYTE: |
| 780 |
res = static_cast<int>(input.readLong((sz=1))); |
| 781 |
break; |
| 782 |
case WP_UBYTE: |
| 783 |
res = static_cast<int>(input.readULong((sz=1))); |
| 784 |
break; |
| 785 |
case WP_INT: |
| 786 |
res = static_cast<int>(input.readLong((sz=2))); |
| 787 |
break; |
| 788 |
case WP_UINT: |
| 789 |
res = static_cast<int>(input.readULong((sz=2))); |
| 790 |
break; |
| 791 |
case WP_UFIXED: |
| 792 |
res = static_cast<int>(input.readULong((sz=4))); |
| 793 |
break; |
| 794 |
case WP_NONE: |
| 795 |
case WP_COLOR: |
| 796 |
case WP_PATTERN: |
| 797 |
case WP_POINT: |
| 798 |
case WP_POINTBYTE: |
| 799 |
case WP_POINTUBYTE: |
| 800 |
case WP_POLY: |
| 801 |
case WP_RECT: |
| 802 |
case WP_REGION: |
| 803 |
case WP_TEXT: |
| 804 |
case WP_LTEXT: |
| 805 |
case WP_BITMAP: |
| 806 |
case WP_RBITMAP: |
| 807 |
case WP_PBITMAP: |
| 808 |
case WP_RPBITMAP: |
| 809 |
case WP_UNKNOWN: |
| 810 |
#if !defined(__clang__) |
| 811 |
default: |
| 812 |
#endif |
| 813 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readInt is called with %d\n", type)); |
| 814 |
return false; |
| 815 |
}; |
| 816 |
if (actualPos+sz != input.tell()) { |
| 817 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readInt find end of file...\n")); |
| 818 |
return false; |
| 819 |
} |
| 820 |
return true; |
| 821 |
} |
| 822 |
/** low level: reads a color argument |
| 823 |
* |
| 824 |
* \note check if this is not an indexed color */ |
| 825 |
static bool readColor(MWAWInputStream &input, DataType type, MWAWColor &col) |
| 826 |
{ |
| 827 |
if (type != WP_COLOR) { |
| 828 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readColor is called with %d\n", type)); |
| 829 |
return false; |
| 830 |
} |
| 831 |
long actualPos = input.tell(); |
| 832 |
auto val = long(input.readULong(4)); |
| 833 |
switch (val) { |
| 834 |
case 30: |
| 835 |
col = MWAWColor::white(); |
| 836 |
break; // white |
| 837 |
case 33: |
| 838 |
col = MWAWColor::black(); |
| 839 |
break; // black |
| 840 |
case 69: |
| 841 |
col = MWAWColor(255,255,0); |
| 842 |
break; // yellow |
| 843 |
case 137: |
| 844 |
col = MWAWColor(255,0,255); |
| 845 |
break; // magenta |
| 846 |
case 205: |
| 847 |
col = MWAWColor(255,0,0); |
| 848 |
break; // red |
| 849 |
case 273: |
| 850 |
col = MWAWColor(0,255,255); |
| 851 |
break; // cyan |
| 852 |
case 341: |
| 853 |
col = MWAWColor(0,255,0); |
| 854 |
break; // green |
| 855 |
case 409: |
| 856 |
col = MWAWColor(0,0,255); |
| 857 |
break; // blue |
| 858 |
default: |
| 859 |
MWAW_DEBUG_MSG(("Pict1:OpCode: unknown color %ld\n", val)); |
| 860 |
col = MWAWColor(128,128,128); |
| 861 |
break; // gray |
| 862 |
} |
| 863 |
|
| 864 |
if (actualPos+4 != input.tell()) { |
| 865 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readColor find end of file...\n")); |
| 866 |
return false; |
| 867 |
} |
| 868 |
return true; |
| 869 |
} |
| 870 |
|
| 871 |
//! low level: reads a pattern argument |
| 872 |
static bool readPattern(MWAWInputStream &input, DataType type, int (&pat)[8]) |
| 873 |
{ |
| 874 |
if (type != WP_PATTERN) { |
| 875 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPattern is called with %d\n", type)); |
| 876 |
return false; |
| 877 |
} |
| 878 |
long actualPos = input.tell(); |
| 879 |
|
| 880 |
for (auto &p : pat) p=static_cast<int>(input.readULong(1)); |
| 881 |
|
| 882 |
if (actualPos+8 != input.tell()) { |
| 883 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPattern find end of file...\n")); |
| 884 |
return false; |
| 885 |
} |
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
//! low level: reads a point argument |
| 890 |
static bool readPoint(MWAWInputStream &input, DataType type, MWAWVec2i &res) |
| 891 |
{ |
| 892 |
DataType valType; |
| 893 |
switch (type) { |
| 894 |
case WP_POINT: |
| 895 |
valType = WP_INT; |
| 896 |
break; |
| 897 |
case WP_POINTBYTE: |
| 898 |
valType = WP_BYTE; |
| 899 |
break; |
| 900 |
case WP_POINTUBYTE: |
| 901 |
valType = WP_UBYTE; |
| 902 |
break; |
| 903 |
case WP_NONE: |
| 904 |
case WP_BYTE: |
| 905 |
case WP_UBYTE: |
| 906 |
case WP_INT: |
| 907 |
case WP_UINT: |
| 908 |
case WP_UFIXED: |
| 909 |
case WP_COLOR: |
| 910 |
case WP_PATTERN: |
| 911 |
case WP_POLY: |
| 912 |
case WP_RECT: |
| 913 |
case WP_REGION: |
| 914 |
case WP_TEXT: |
| 915 |
case WP_LTEXT: |
| 916 |
case WP_BITMAP: |
| 917 |
case WP_RBITMAP: |
| 918 |
case WP_PBITMAP: |
| 919 |
case WP_RPBITMAP: |
| 920 |
case WP_UNKNOWN: |
| 921 |
#if !defined(__clang__) |
| 922 |
default: |
| 923 |
#endif |
| 924 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPoint is called with %d\n", type)); |
| 925 |
return false; |
| 926 |
}; |
| 927 |
int v[2]; |
| 928 |
for (auto &val : v) { |
| 929 |
if (!readInt(input, valType, val)) return false; |
| 930 |
} |
| 931 |
res.set(v[0], v[1]); |
| 932 |
return true; |
| 933 |
} |
| 934 |
|
| 935 |
//! low level: reads a polygon argument |
| 936 |
static bool readPoly(MWAWInputStream &input, DataType type, MWAWBox2i &box, std::vector<MWAWVec2i> &res) |
| 937 |
{ |
| 938 |
DataType boxType, valType; |
| 939 |
switch (type) { |
| 940 |
case WP_POLY: |
| 941 |
valType = WP_POINT; |
| 942 |
boxType = WP_RECT; |
| 943 |
break; |
| 944 |
case WP_NONE: |
| 945 |
case WP_BYTE: |
| 946 |
case WP_UBYTE: |
| 947 |
case WP_INT: |
| 948 |
case WP_UINT: |
| 949 |
case WP_UFIXED: |
| 950 |
case WP_COLOR: |
| 951 |
case WP_PATTERN: |
| 952 |
case WP_POINT: |
| 953 |
case WP_POINTBYTE: |
| 954 |
case WP_POINTUBYTE: |
| 955 |
case WP_RECT: |
| 956 |
case WP_REGION: |
| 957 |
case WP_TEXT: |
| 958 |
case WP_LTEXT: |
| 959 |
case WP_BITMAP: |
| 960 |
case WP_RBITMAP: |
| 961 |
case WP_PBITMAP: |
| 962 |
case WP_RPBITMAP: |
| 963 |
case WP_UNKNOWN: |
| 964 |
#if !defined(__clang__) |
| 965 |
default: |
| 966 |
#endif |
| 967 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPoly is called with %d\n", type)); |
| 968 |
return false; |
| 969 |
}; |
| 970 |
int sz; |
| 971 |
if (!readInt(input, WP_UINT, sz)) return false; |
| 972 |
if ((sz%2) != 0) { |
| 973 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPoly odd size: %d\n", sz)); |
| 974 |
return false; |
| 975 |
} |
| 976 |
sz /= 2; |
| 977 |
if (sz < 5) { |
| 978 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPoly size is too short: %d\n", sz*2)); |
| 979 |
return false; |
| 980 |
} |
| 981 |
if (!readRect(input, boxType, box)) return false; |
| 982 |
int numPt = sz-5; |
| 983 |
if ((numPt%2) != 0) { |
| 984 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readPoly odd point number: %d\n", numPt)); |
| 985 |
return false; |
| 986 |
} |
| 987 |
numPt /= 2; |
| 988 |
res.resize(size_t(numPt)); |
| 989 |
|
| 990 |
MWAWVec2i pt; |
| 991 |
for (auto &p : res) { |
| 992 |
if (!readPoint(input, valType, pt)) return false; |
| 993 |
p = pt; |
| 994 |
} |
| 995 |
return true; |
| 996 |
} |
| 997 |
//! low level: reads a string argument |
| 998 |
static bool readText(MWAWInputStream &input, DataType type, std::string &res) |
| 999 |
{ |
| 1000 |
int sz = 0; |
| 1001 |
switch (type) { |
| 1002 |
case WP_TEXT: |
| 1003 |
// CHECKME:in at least one file: DHText, sz=5 but the text contains only |
| 1004 |
// one char |
| 1005 |
if (!readInt(input, WP_UBYTE, sz)) return false; |
| 1006 |
break; |
| 1007 |
case WP_LTEXT: |
| 1008 |
if (!readInt(input, WP_INT, sz) || sz < 0) return false; |
| 1009 |
break; |
| 1010 |
case WP_NONE: |
| 1011 |
case WP_BYTE: |
| 1012 |
case WP_UBYTE: |
| 1013 |
case WP_INT: |
| 1014 |
case WP_UINT: |
| 1015 |
case WP_UFIXED: |
| 1016 |
case WP_COLOR: |
| 1017 |
case WP_PATTERN: |
| 1018 |
case WP_POINT: |
| 1019 |
case WP_POINTBYTE: |
| 1020 |
case WP_POINTUBYTE: |
| 1021 |
case WP_POLY: |
| 1022 |
case WP_RECT: |
| 1023 |
case WP_REGION: |
| 1024 |
case WP_BITMAP: |
| 1025 |
case WP_RBITMAP: |
| 1026 |
case WP_PBITMAP: |
| 1027 |
case WP_RPBITMAP: |
| 1028 |
case WP_UNKNOWN: |
| 1029 |
#if !defined(__clang__) |
| 1030 |
default: |
| 1031 |
#endif |
| 1032 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readText is called with %d\n", type)); |
| 1033 |
return false; |
| 1034 |
} |
| 1035 |
|
| 1036 |
long actualPos = input.tell(); |
| 1037 |
res = ""; |
| 1038 |
if (!input.checkPosition(actualPos+sz)) { |
| 1039 |
MWAW_DEBUG_MSG(("Pict1:OpCode: readText: find EOF\n")); |
| 1040 |
return false; |
| 1041 |
} |
| 1042 |
|
| 1043 |
for (int i = 0; i < sz; i++) { |
| 1044 |
auto c = char(input.readULong(1)); |
| 1045 |
res += c; |
| 1046 |
} |
| 1047 |
return true; |
| 1048 |
} |
| 1049 |
|
| 1050 |
}; |
| 1051 |
|
| 1052 |
OpCode::~OpCode() |
| 1053 |
{ |
| 1054 |
} |
| 1055 |
|
| 1056 |
/** internal and low level: map opcode id -> OpCode */ |
| 1057 |
class PictParser |
| 1058 |
{ |
| 1059 |
public: |
| 1060 |
//! the constructor |
| 1061 |
PictParser() |
| 1062 |
: m_mapIdOp() |
| 1063 |
{ |
| 1064 |
/* list of known opcodes |
| 1065 |
* |
| 1066 |
* \note codes 0x2c, 0x2e, 0xa5 are not standard opcodes, but I find them in some pictures */ |
| 1067 |
OpCode const listCodes[] = { |
| 1068 |
OpCode(0,"NOP"), OpCode(1,"ClipRgn",WP_REGION), OpCode(2,"BkPat",WP_PATTERN), |
| 1069 |
OpCode(3,"TxFont",WP_INT), OpCode(4,"TxFace",WP_UBYTE), OpCode(5,"TxMode",WP_INT), OpCode(6,"SpExtra",WP_UFIXED), |
| 1070 |
OpCode(7,"PnSize",WP_POINT), OpCode(8,"PnMode",WP_INT), OpCode(9,"PnPat",WP_PATTERN), |
| 1071 |
OpCode(0xa,"FillPat",WP_PATTERN), OpCode(0xb,"OvSize",WP_POINT), OpCode(0xc,"Origin",WP_POINT), |
| 1072 |
OpCode(0xd,"TxSize",WP_INT), OpCode(0xe,"FgColor",WP_COLOR), OpCode(0xf,"BkColor",WP_COLOR), |
| 1073 |
OpCode(0x10,"TxRatio",WP_POINT,WP_POINT), OpCode(0x11,"picVersion",WP_UBYTE), |
| 1074 |
|
| 1075 |
OpCode(0x20,"Line",WP_POINT,WP_POINT),OpCode(0x21,"LineFrom",WP_POINT), |
| 1076 |
OpCode(0x22,"ShortLine",WP_POINT, WP_POINTBYTE), OpCode(0x23,"ShortLineFrom", WP_POINTBYTE), |
| 1077 |
|
| 1078 |
OpCode(0x28,"LongText",WP_POINT,WP_TEXT), OpCode(0x29,"DHText",WP_UBYTE,WP_TEXT), OpCode(0x2a,"DVText",WP_UBYTE,WP_TEXT), |
| 1079 |
OpCode(0x2b,"DHDVText",WP_POINTUBYTE,WP_TEXT), |
| 1080 |
// not really a Pict1. code, but it can appear in some pict |
| 1081 |
OpCode(0x2c,"FontName",WP_INT,WP_INT,WP_TEXT), |
| 1082 |
// can we find 0x2d ? |
| 1083 |
// Fixme: add 0x2e: not really a Pict1. code, but it can appear in some pict |
| 1084 |
OpCode(0x2e,"GlyphState?", WP_INT, WP_INT, WP_INT), |
| 1085 |
|
| 1086 |
OpCode(0x30,"frameRect",WP_RECT), OpCode(0x31,"paintRect",WP_RECT), OpCode(0x32,"eraseRect",WP_RECT), |
| 1087 |
OpCode(0x33,"invertRect",WP_RECT), OpCode(0x34,"fillRect",WP_RECT), |
| 1088 |
OpCode(0x38,"frameSameRect"), OpCode(0x39,"paintSameRect"), OpCode(0x3a,"eraseSameRect"), |
| 1089 |
OpCode(0x3b,"invertSameRect"), OpCode(0x3c,"fillSameRect"), |
| 1090 |
|
| 1091 |
OpCode(0x40,"frameRRect",WP_RECT), OpCode(0x41,"paintRRect",WP_RECT), OpCode(0x42,"eraseRRect",WP_RECT), |
| 1092 |
OpCode(0x43,"invertRRect",WP_RECT), OpCode(0x44,"fillRRect",WP_RECT), |
| 1093 |
OpCode(0x48,"frameSameRRect"), OpCode(0x49,"paintSameRRect"), OpCode(0x4a,"eraseSameRRect"), |
| 1094 |
OpCode(0x4b,"invertSameRRect"), OpCode(0x4c,"fillSameRRect"), |
| 1095 |
|
| 1096 |
OpCode(0x50,"frameOval",WP_RECT), OpCode(0x51,"paintOval",WP_RECT), OpCode(0x52,"eraseOval",WP_RECT), |
| 1097 |
OpCode(0x53,"invertOval",WP_RECT), OpCode(0x54,"fillOval",WP_RECT), |
| 1098 |
OpCode(0x58,"frameSameOval"), OpCode(0x59,"paintSameOval"), OpCode(0x5a,"eraseSameOval"), |
| 1099 |
OpCode(0x5b,"invertSameOval"), OpCode(0x5c,"fillSameOval"), |
| 1100 |
|
| 1101 |
OpCode(0x60,"frameArc",WP_RECT,WP_INT,WP_INT), OpCode(0x61,"paintArc",WP_RECT,WP_INT,WP_INT), OpCode(0x62,"eraseArc",WP_RECT,WP_INT,WP_INT), |
| 1102 |
OpCode(0x63,"invertArc",WP_RECT,WP_INT,WP_INT), OpCode(0x64,"fillArc",WP_RECT,WP_INT,WP_INT), |
| 1103 |
OpCode(0x68,"frameSameArc",WP_INT,WP_INT), OpCode(0x69,"paintSameArc",WP_INT,WP_INT), OpCode(0x6a,"eraseSameArc",WP_INT,WP_INT), |
| 1104 |
OpCode(0x6b,"invertSameArc",WP_INT,WP_INT), OpCode(0x6c,"fillSameArc",WP_INT,WP_INT), |
| 1105 |
|
| 1106 |
OpCode(0x70,"framePoly",WP_POLY), OpCode(0x71,"paintPoly",WP_POLY), OpCode(0x72,"erasePoly",WP_POLY), |
| 1107 |
OpCode(0x73,"invertPoly",WP_POLY), OpCode(0x74,"fillPoly",WP_POLY), |
| 1108 |
// reserved: but not implemented |
| 1109 |
OpCode(0x78,"frameSamePoly"), OpCode(0x79,"paintSamePoly"), OpCode(0x7a,"eraseSamePoly"), |
| 1110 |
OpCode(0x7b,"invertSamePoly"), OpCode(0x7c,"fillSamePoly"), |
| 1111 |
|
| 1112 |
OpCode(0x80,"frameRgn",WP_REGION), OpCode(0x81,"paintRgn",WP_REGION), OpCode(0x82,"eraseRgn",WP_REGION), |
| 1113 |
OpCode(0x83,"invertRgn",WP_REGION), OpCode(0x84,"fillRgn",WP_REGION), |
| 1114 |
// reserved: but not implemented |
| 1115 |
OpCode(0x88,"frameSameRgn"), OpCode(0x89,"paintSameRgn"), OpCode(0x8a,"eraseSameRgn"), |
| 1116 |
OpCode(0x8b,"invertSameRgn"), OpCode(0x8c,"fillSameRgn"), |
| 1117 |
|
| 1118 |
// fixme: bitmap to implement |
| 1119 |
OpCode(0x90,"BitsRect", WP_BITMAP), |
| 1120 |
OpCode(0x91,"BitsRgn", WP_RBITMAP), |
| 1121 |
|
| 1122 |
OpCode(0x98,"PackBitsRect", WP_PBITMAP), |
| 1123 |
OpCode(0x99,"PackBitsRgn", WP_RPBITMAP), |
| 1124 |
|
| 1125 |
OpCode(0xa0,"ShortComment", WP_INT), |
| 1126 |
OpCode(0xa1,"LongComment", WP_INT, WP_LTEXT), |
| 1127 |
// not really a Pict1. code, but it can appear in some pict |
| 1128 |
OpCode(0xa5,"LongComment????", WP_INT, WP_LTEXT), |
| 1129 |
OpCode(0xff,"EndOfPicture") |
| 1130 |
}; |
| 1131 |
|
| 1132 |
for (auto const &opCode : listCodes) |
| 1133 |
m_mapIdOp.insert(std::map<int,OpCode>::value_type(opCode.m_id,opCode)); |
| 1134 |
} |
| 1135 |
|
| 1136 |
/** internal and low level: tries to convert a Pict1.0 picture stored in \a orig in a Pict2.0 picture */ |
| 1137 |
bool convertToPict2(librevenge::RVNGBinaryData const &orig, librevenge::RVNGBinaryData &result); |
| 1138 |
protected: |
| 1139 |
|
| 1140 |
//! the map |
| 1141 |
std::map<int,OpCode> m_mapIdOp; |
| 1142 |
}; |
| 1143 |
|
| 1144 |
|
| 1145 |
/** internal and low level: tries to convert a Pict1.0 picture stored in \a orig in a Pict2.0 picture */ |
| 1146 |
bool PictParser::convertToPict2(librevenge::RVNGBinaryData const &orig, librevenge::RVNGBinaryData &result) |
| 1147 |
{ |
| 1148 |
# ifdef ADD_DATA_SHORT |
| 1149 |
# undef ADD_DATA_SHORT |
| 1150 |
# endif |
| 1151 |
# define ADD_DATA_SHORT(resPtr,val) do { \ |
| 1152 |
*(resPtr++) = static_cast<unsigned char>((val & 0xFF00) >> 8); \ |
| 1153 |
*(resPtr++) = static_cast<unsigned char> (val & 0xFF); } while(0) |
| 1154 |
|
| 1155 |
auto pictSize = long(orig.size()); |
| 1156 |
if (pictSize < 10) return false; |
| 1157 |
|
| 1158 |
std::unique_ptr<unsigned char[]> res{new unsigned char [size_t(2*pictSize+50)]}; |
| 1159 |
unsigned char *resPtr = res.get(); |
| 1160 |
const unsigned char *resEnd = res.get() + 2*pictSize+50; |
| 1161 |
if (!res) return false; |
| 1162 |
|
| 1163 |
MWAWInputStreamPtr input=MWAWInputStream::get(orig, false); |
| 1164 |
if (!input) { |
| 1165 |
return false; |
| 1166 |
} |
| 1167 |
|
| 1168 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 1169 |
auto sz = static_cast<int>(input->readULong(2)); |
| 1170 |
if (pictSize != sz && pictSize != sz+1) { |
| 1171 |
return false; |
| 1172 |
} |
| 1173 |
|
| 1174 |
ADD_DATA_SHORT(resPtr,0); // size, we must fill it latter |
| 1175 |
long dim[4]; |
| 1176 |
for (auto &d : dim) { // read the rectangle |
| 1177 |
d = input->readLong(2); |
| 1178 |
ADD_DATA_SHORT(resPtr,d); |
| 1179 |
} |
| 1180 |
if (input->readLong(2) != 0x1101) { |
| 1181 |
return false; |
| 1182 |
} |
| 1183 |
ADD_DATA_SHORT(resPtr,0x11); |
| 1184 |
ADD_DATA_SHORT(resPtr,0x2FF); |
| 1185 |
ADD_DATA_SHORT(resPtr,0xC00); // HeaderOp |
| 1186 |
ADD_DATA_SHORT(resPtr, -1); |
| 1187 |
ADD_DATA_SHORT(resPtr, -1); |
| 1188 |
for (int i = 0; i < 4; i++) { |
| 1189 |
int depl = (i%2) ? -1 : 1; |
| 1190 |
ADD_DATA_SHORT(resPtr,dim[i+depl]); |
| 1191 |
ADD_DATA_SHORT(resPtr,0); |
| 1192 |
} |
| 1193 |
ADD_DATA_SHORT(resPtr, 0); |
| 1194 |
ADD_DATA_SHORT(resPtr, 0); |
| 1195 |
# undef ADD_DATA_SHORT |
| 1196 |
|
| 1197 |
bool findEnd = false; |
| 1198 |
while (!findEnd && !input->isEnd()) { |
| 1199 |
long actPos = input->tell(); |
| 1200 |
auto code = static_cast<int>(input->readULong(1)); |
| 1201 |
auto it = m_mapIdOp.find(code); |
| 1202 |
if (it == m_mapIdOp.end()) { |
| 1203 |
MWAW_DEBUG_MSG(("Pict1:convertToPict2 can not find opCode 0x%x\n", static_cast<unsigned int>(code))); |
| 1204 |
return false; |
| 1205 |
} |
| 1206 |
|
| 1207 |
OpCode const &opCode = it->second; |
| 1208 |
sz = 0; |
| 1209 |
if (!opCode.computeSize(*input, sz) || !input->checkPosition(input->tell()+sz)) { |
| 1210 |
MWAW_DEBUG_MSG(("Pict1:convertToPict2 can not compute size for opCode 0x%x\n", static_cast<unsigned int>(code))); |
| 1211 |
return false; |
| 1212 |
} |
| 1213 |
bool skip = (code == 0x2e) || (code == 0xa5); // normally unemplemented, so.. |
| 1214 |
findEnd = code == 0xff; |
| 1215 |
|
| 1216 |
if (!skip) { |
| 1217 |
if (resEnd-resPtr < 2+sz+sz%2) { |
| 1218 |
MWAW_DEBUG_MSG(("Pict1:convertToPict2 output buffer overflow\n")); |
| 1219 |
return false; |
| 1220 |
} |
| 1221 |
*(resPtr++) = 0; |
| 1222 |
*(resPtr++) = static_cast<unsigned char>(code); |
| 1223 |
input->seek(actPos+1, librevenge::RVNG_SEEK_SET); |
| 1224 |
for (int i = 0; i < sz; i++) |
| 1225 |
*(resPtr++) = static_cast<unsigned char>(input->readULong(1)); |
| 1226 |
if ((sz%2)==1) *(resPtr++) = 0; |
| 1227 |
} |
| 1228 |
input->seek(actPos+1+sz, librevenge::RVNG_SEEK_SET); |
| 1229 |
} |
| 1230 |
|
| 1231 |
bool endOk = false; |
| 1232 |
if (findEnd) { |
| 1233 |
if (input->isEnd()) endOk = true; |
| 1234 |
else { // allows a final caracter for alignment |
| 1235 |
input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1236 |
endOk = input->isEnd(); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
if (!endOk) { |
| 1240 |
MWAW_DEBUG_MSG(("Pict1:convertToPict2 find EOF or EndOfPict prematurely \n")); |
| 1241 |
return false; |
| 1242 |
} |
| 1243 |
|
| 1244 |
long newSize = resPtr - res.get(); |
| 1245 |
res[0] = static_cast<unsigned char>((newSize & 0xFF00) >> 8); |
| 1246 |
res[1] = static_cast<unsigned char>(newSize & 0xFF); |
| 1247 |
result.clear(); |
| 1248 |
result.append(res.get(), static_cast<unsigned long>(newSize)); |
| 1249 |
|
| 1250 |
return true; |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
bool MWAWPictMac::convertPict1To2(librevenge::RVNGBinaryData const &orig, librevenge::RVNGBinaryData &result) |
| 1255 |
{ |
| 1256 |
libmwaw_applepict1::PictParser parser; |
| 1257 |
return parser.convertToPict2(orig, result); |
| 1258 |
} |
| 1259 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |