| 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 some bitmap |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <sstream> |
| 38 |
#include <string> |
| 39 |
#ifdef USE_ZLIB |
| 40 |
# include <zlib.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <librevenge/librevenge.h> |
| 44 |
|
| 45 |
#include "libmwaw_internal.hxx" |
| 46 |
|
| 47 |
#include "MWAWPictBitmap.hxx" |
| 48 |
|
| 49 |
//! Internal: namespace used to define some internal function |
| 50 |
namespace MWAWPictBitmapInternal |
| 51 |
{ |
| 52 |
#ifdef USE_ZLIB |
| 53 |
// |
| 54 |
// functions used by getPNGData |
| 55 |
// |
| 56 |
|
| 57 |
//! Internal: small function to store an unsigned value in big endian |
| 58 |
static void writeBEU32(unsigned char *buffer, const unsigned value) |
| 59 |
{ |
| 60 |
*(buffer++) = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 61 |
*(buffer++) = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 62 |
*(buffer++) = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 63 |
*(buffer++) = static_cast<unsigned char>(value & 0xFF); |
| 64 |
} |
| 65 |
|
| 66 |
//! Internal: add a chunk zone in a PNG file |
| 67 |
static void addChunkInPNG(unsigned chunkType, unsigned char const *buffer, unsigned length, librevenge::RVNGBinaryData &data) |
| 68 |
{ |
| 69 |
unsigned char buf4[4]; |
| 70 |
writeBEU32(buf4, length); |
| 71 |
data.append(buf4, 4); // add length |
| 72 |
writeBEU32(buf4, chunkType); |
| 73 |
data.append(buf4, 4); // add type |
| 74 |
unsigned crc=unsigned(crc32(0, buf4, 4)); |
| 75 |
if (length) { |
| 76 |
data.append(buffer, length); // add data |
| 77 |
writeBEU32(buf4, unsigned(crc32(crc,buffer, length))); // add crc |
| 78 |
} |
| 79 |
else |
| 80 |
writeBEU32(buf4, crc); // add crc |
| 81 |
data.append(buf4, 4); |
| 82 |
} |
| 83 |
|
| 84 |
/** Internal: helper function to create a PNG knowing the ihdr, image zone |
| 85 |
and the palette zone(indexed bitmap) |
| 86 |
*/ |
| 87 |
static bool createPNGFile(unsigned char const *ihdr, unsigned ihdrSize, |
| 88 |
unsigned char const *image, unsigned imageSize, |
| 89 |
unsigned char const *palette, unsigned paletteSize, |
| 90 |
librevenge::RVNGBinaryData &data) |
| 91 |
{ |
| 92 |
unsigned char const signature[] = { |
| 93 |
/* PNG signature */ |
| 94 |
0x89, 0x50, 0x4e, 0x47, |
| 95 |
0x0d, 0x0a, 0x1a, 0x0a |
| 96 |
}; |
| 97 |
data.append(signature, MWAW_N_ELEMENTS(signature)); |
| 98 |
|
| 99 |
if (ihdr && ihdrSize) |
| 100 |
addChunkInPNG(0x49484452 /* IHDR*/, ihdr, ihdrSize, data); |
| 101 |
if (palette && paletteSize) |
| 102 |
addChunkInPNG(0x504C5445 /*PLTE*/, palette, paletteSize, data); |
| 103 |
// now compress the picture |
| 104 |
const unsigned tmpBufSize = 128 * 1024; |
| 105 |
std::unique_ptr<unsigned char[]> tmpBuffer{new unsigned char[tmpBufSize]}; |
| 106 |
std::vector<unsigned char> idatBuffer; |
| 107 |
|
| 108 |
z_stream strm; |
| 109 |
strm.zalloc = nullptr; |
| 110 |
strm.zfree = nullptr; |
| 111 |
strm.next_in = const_cast<unsigned char *>(image); |
| 112 |
strm.avail_in = imageSize; |
| 113 |
strm.next_out = tmpBuffer.get(); |
| 114 |
strm.avail_out = tmpBufSize; |
| 115 |
strm.total_in = strm.total_out=0; |
| 116 |
|
| 117 |
if (deflateInit(&strm, Z_RLE)!=Z_OK) return false; |
| 118 |
while (strm.avail_in != 0) { |
| 119 |
if (deflate(&strm, Z_NO_FLUSH)!=Z_OK) return false; |
| 120 |
if (strm.avail_out == 0) { |
| 121 |
idatBuffer.insert(idatBuffer.end(), tmpBuffer.get(), tmpBuffer.get() + tmpBufSize); |
| 122 |
strm.next_out = tmpBuffer.get(); |
| 123 |
strm.avail_out = tmpBufSize; |
| 124 |
} |
| 125 |
} |
| 126 |
while (deflate(&strm, Z_FINISH)==Z_OK) { |
| 127 |
if (strm.avail_out == 0) { |
| 128 |
idatBuffer.insert(idatBuffer.end(), tmpBuffer.get(), tmpBuffer.get() + tmpBufSize); |
| 129 |
strm.next_out = tmpBuffer.get(); |
| 130 |
strm.avail_out = tmpBufSize; |
| 131 |
} |
| 132 |
} |
| 133 |
idatBuffer.insert(idatBuffer.end(), tmpBuffer.get(), tmpBuffer.get() + tmpBufSize - strm.avail_out); |
| 134 |
deflateEnd(&strm); |
| 135 |
|
| 136 |
addChunkInPNG(0x49444154/*IDAT*/, idatBuffer.data(), unsigned(idatBuffer.size()), data); |
| 137 |
addChunkInPNG(0x49454e44 /*IEND*/, nullptr, 0, data); |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
//! Internal: fonction to create a PNG image from a true color bitmap (need zlib) |
| 142 |
static bool getPNGData(MWAWPictBitmapContainer<MWAWColor> const &orig, librevenge::RVNGBinaryData &data, bool useAlpha) |
| 143 |
{ |
| 144 |
MWAWVec2i sz = orig.size(); |
| 145 |
if (sz[0] <= 0 || sz[0] > 10000 || sz[1] <= 0) { |
| 146 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal:getPNGData: the bitmap size seems bad\n")); |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
unsigned char ihdr[] = { |
| 151 |
/* IHDR -- Image header */ |
| 152 |
0, 0, 0, 0, // width |
| 153 |
0, 0, 0, 0, // height |
| 154 |
8, // bit depth |
| 155 |
static_cast<unsigned char>(useAlpha ? 6 : 2), // 2: rgb, 6: rgba |
| 156 |
0, // compression method: 0=deflate |
| 157 |
0, // filter method: 0=adaptative |
| 158 |
0 // interlace method: 0=none |
| 159 |
}; |
| 160 |
writeBEU32(ihdr, unsigned(sz[0])); |
| 161 |
writeBEU32(ihdr+4, unsigned(sz[1])); |
| 162 |
|
| 163 |
unsigned const numBytes=useAlpha ? 4 : 3; |
| 164 |
unsigned const lineWidth=1+unsigned(sz[0])*numBytes; |
| 165 |
unsigned const imageSize=lineWidth*unsigned(sz[1]); |
| 166 |
if (!lineWidth || imageSize/lineWidth<unsigned(sz[1])) { |
| 167 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal:getPNGData: the idat chunk is too big\n")); |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
// create the image data |
| 172 |
std::unique_ptr<unsigned char[]> imageBuffer{new unsigned char[imageSize]}; |
| 173 |
unsigned char *imagePtr=imageBuffer.get(); |
| 174 |
for (int i = 0; i<sz[1]; ++i) { |
| 175 |
MWAWColor const *row = orig.getRow(i); |
| 176 |
*(imagePtr++) = 0; // 0: means none ; sometimes better with 1 but not always... |
| 177 |
for (int j = 0; j < sz[0]; j++) { |
| 178 |
uint32_t col = row[j].value(); |
| 179 |
|
| 180 |
*(imagePtr++)=static_cast<unsigned char>((col>>16)&0xFF); |
| 181 |
*(imagePtr++)=static_cast<unsigned char>((col>>8)&0xFF); |
| 182 |
*(imagePtr++)=static_cast<unsigned char>(col&0xFF); |
| 183 |
if (useAlpha) |
| 184 |
*(imagePtr++)=static_cast<unsigned char>((col>>24)&0xFF); |
| 185 |
} |
| 186 |
} |
| 187 |
return createPNGFile(ihdr, unsigned(MWAW_N_ELEMENTS(ihdr)), imageBuffer.get(), imageSize, nullptr, 0, data); |
| 188 |
} |
| 189 |
|
| 190 |
//! Internal: fonction to create a PNG image from a indexed bitmap (need zlib) |
| 191 |
template <class T> |
| 192 |
bool getPNGData(MWAWPictBitmapContainer<T> const &orig, librevenge::RVNGBinaryData &data, std::vector<MWAWColor> const &indexedColor) |
| 193 |
{ |
| 194 |
MWAWVec2i sz = orig.size(); |
| 195 |
auto nColors = int(indexedColor.size()); |
| 196 |
if (sz[0] <= 0 || sz[1] <= 0 || nColors==0) return false; |
| 197 |
bool useIndex=nColors<=256; |
| 198 |
unsigned char ihdr[] = { |
| 199 |
/* IHDR -- Image header */ |
| 200 |
0, 0, 0, 0, // width |
| 201 |
0, 0, 0, 0, // height |
| 202 |
8, // bit depth |
| 203 |
static_cast<unsigned char>(useIndex ? 3 : 2), // 2: rgb, 3: indexed |
| 204 |
0, // compression method: 0=deflate |
| 205 |
0, // filter method: 0=adaptative |
| 206 |
0 // interlace method: 0=none |
| 207 |
}; |
| 208 |
writeBEU32(ihdr, unsigned(sz[0])); |
| 209 |
writeBEU32(ihdr+4, unsigned(sz[1])); |
| 210 |
|
| 211 |
unsigned const numBytes=(useIndex ? 1 : 3); |
| 212 |
unsigned const lineWidth=1+unsigned(sz[0])*numBytes; |
| 213 |
unsigned const imageSize=lineWidth*unsigned(sz[1]); |
| 214 |
if (!lineWidth || imageSize/lineWidth<unsigned(sz[1])) { |
| 215 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal:getPNGData: the idat chunk is too big\n")); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
// create the image data |
| 220 |
std::unique_ptr<unsigned char[]> imageBuffer{new unsigned char[imageSize]}; |
| 221 |
unsigned char *imagePtr=imageBuffer.get(); |
| 222 |
for (int i = 0; i<sz[1]; ++i) { |
| 223 |
T const *row = orig.getRow(i); |
| 224 |
*(imagePtr++) = 0; // 0: means none, using sub filtering seems counterproductif... |
| 225 |
for (int j = 0; j < sz[0]; j++) { |
| 226 |
int ind = row[j]; |
| 227 |
if (ind < 0 || ind >= nColors) { |
| 228 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal::getPNGData invalid index %d\n", ind)); |
| 229 |
return false; |
| 230 |
} |
| 231 |
if (useIndex) |
| 232 |
*(imagePtr++)=static_cast<unsigned char>(ind); |
| 233 |
else { |
| 234 |
uint32_t col = indexedColor[size_t(ind)].value(); |
| 235 |
|
| 236 |
*(imagePtr++)=static_cast<unsigned char>((col>>16)&0xFF); |
| 237 |
*(imagePtr++)=static_cast<unsigned char>((col>>8)&0xFF); |
| 238 |
*(imagePtr++)=static_cast<unsigned char>(col&0xFF); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
if (!useIndex) |
| 243 |
return createPNGFile(ihdr, unsigned(MWAW_N_ELEMENTS(ihdr)), imageBuffer.get(), imageSize, nullptr, 0, data); |
| 244 |
std::unique_ptr<unsigned char[]> paletteBuffer{new unsigned char[3*unsigned(nColors)]}; |
| 245 |
unsigned char *palettePtr=paletteBuffer.get(); |
| 246 |
for (auto const &color : indexedColor) { |
| 247 |
uint32_t col = color.value(); |
| 248 |
*(palettePtr++)=static_cast<unsigned char>((col>>16)&0xFF); |
| 249 |
*(palettePtr++)=static_cast<unsigned char>((col>>8)&0xFF); |
| 250 |
*(palettePtr++)=static_cast<unsigned char>(col&0xFF); |
| 251 |
} |
| 252 |
return createPNGFile(ihdr, unsigned(MWAW_N_ELEMENTS(ihdr)), imageBuffer.get(), imageSize, paletteBuffer.get(), 3*unsigned(nColors), data); |
| 253 |
} |
| 254 |
|
| 255 |
//! Internal: helper function to create a 2 color PNG file |
| 256 |
template <class T> |
| 257 |
bool getPNG1Data(MWAWPictBitmapContainer<T> const &orig, librevenge::RVNGBinaryData &data, T white) |
| 258 |
{ |
| 259 |
MWAWVec2i sz = orig.size(); |
| 260 |
if (sz[0] <= 0 || sz[1] <= 0) return false; |
| 261 |
|
| 262 |
unsigned char ihdr[] = { |
| 263 |
/* IHDR -- Image header */ |
| 264 |
0, 0, 0, 0, // width |
| 265 |
0, 0, 0, 0, // height |
| 266 |
1, // bit depth |
| 267 |
3, // 3: indexed |
| 268 |
0, // compression method: 0=deflate |
| 269 |
0, // filter method: 0=adaptative |
| 270 |
0 // interlace method: 0=none |
| 271 |
}; |
| 272 |
writeBEU32(ihdr, unsigned(sz[0])); |
| 273 |
writeBEU32(ihdr+4, unsigned(sz[1])); |
| 274 |
|
| 275 |
unsigned const lineWidth=1+unsigned((sz[0]+7)/8); |
| 276 |
unsigned const imageSize=lineWidth*unsigned(sz[1]); |
| 277 |
if (!lineWidth || imageSize/lineWidth<unsigned(sz[1])) { |
| 278 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal:getPNG1Data: the idat chunk is too big\n")); |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
// create the image data |
| 283 |
std::unique_ptr<unsigned char[]> imageBuffer{new unsigned char[imageSize]}; |
| 284 |
unsigned char *imagePtr=imageBuffer.get(); |
| 285 |
|
| 286 |
for (int j = 0; j < sz[1]; j++) { |
| 287 |
T const *row = orig.getRow(j); |
| 288 |
*(imagePtr++) = 0; // 0: means none |
| 289 |
|
| 290 |
unsigned char mask = 0x80, value = 0; |
| 291 |
for (int i = 0; i < sz[0]; i++) { |
| 292 |
if (row[i] != white) value |= mask; |
| 293 |
mask = static_cast<unsigned char>(mask >> 1); |
| 294 |
if (mask != 0) continue; |
| 295 |
*(imagePtr++) = value; |
| 296 |
value = 0; |
| 297 |
mask = 0x80; |
| 298 |
} |
| 299 |
if (mask!= 0x80) *(imagePtr++) = value; |
| 300 |
} |
| 301 |
// create a black and white palette |
| 302 |
unsigned const nColors=2; |
| 303 |
std::unique_ptr<unsigned char[]> paletteBuffer{new unsigned char[3*unsigned(nColors)]}; |
| 304 |
unsigned char *palettePtr=paletteBuffer.get(); |
| 305 |
for (int i=0; i<2; ++i) { |
| 306 |
uint32_t col = i==1 ? 0 : 0xffffff; |
| 307 |
*(palettePtr++)=static_cast<unsigned char>((col>>16)&0xFF); |
| 308 |
*(palettePtr++)=static_cast<unsigned char>((col>>8)&0xFF); |
| 309 |
*(palettePtr++)=static_cast<unsigned char>(col&0xFF); |
| 310 |
} |
| 311 |
return createPNGFile(ihdr, unsigned(MWAW_N_ELEMENTS(ihdr)), imageBuffer.get(), imageSize, paletteBuffer.get(), 3*unsigned(nColors), data); |
| 312 |
} |
| 313 |
#else |
| 314 |
//! Internal: helper function to create a PBM |
| 315 |
template <class T> |
| 316 |
bool getPBMData(MWAWPictBitmapContainer<T> const &orig, librevenge::RVNGBinaryData &data, T white) |
| 317 |
{ |
| 318 |
MWAWVec2i sz = orig.size(); |
| 319 |
if (sz[0] <= 0 || sz[1] <= 0) return false; |
| 320 |
|
| 321 |
data.clear(); |
| 322 |
std::stringstream f; |
| 323 |
f << "P4\n" << sz[0] << " " << sz[1] << "\n"; |
| 324 |
std::string const &header = f.str(); |
| 325 |
data.append(reinterpret_cast<const unsigned char *>(header.c_str()), header.size()); |
| 326 |
|
| 327 |
for (int j = 0; j < sz[1]; j++) { |
| 328 |
T const *row = orig.getRow(j); |
| 329 |
|
| 330 |
unsigned char mask = 0x80, value = 0; |
| 331 |
for (int i = 0; i < sz[0]; i++) { |
| 332 |
if (row[i] != white) value |= mask; |
| 333 |
mask = static_cast<unsigned char>(mask >> 1); |
| 334 |
if (mask != 0) continue; |
| 335 |
data.append(value); |
| 336 |
value = 0; |
| 337 |
mask = 0x80; |
| 338 |
} |
| 339 |
if (mask!= 0x80) data.append(value); |
| 340 |
} |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
//! Internal: helper function to create a PPM |
| 345 |
template <class T> |
| 346 |
bool getPPMData(MWAWPictBitmapContainer<T> const &orig, librevenge::RVNGBinaryData &data, std::vector<MWAWColor> const &indexedColor) |
| 347 |
{ |
| 348 |
MWAWVec2i sz = orig.size(); |
| 349 |
if (sz[0] <= 0 || sz[1] <= 0) return false; |
| 350 |
|
| 351 |
auto nColors = int(indexedColor.size()); |
| 352 |
|
| 353 |
data.clear(); |
| 354 |
std::stringstream f; |
| 355 |
f << "P6\n" << sz[0] << " " << sz[1] << " 255\n"; |
| 356 |
std::string const &header = f.str(); |
| 357 |
data.append(reinterpret_cast<const unsigned char *>(header.c_str()), header.size()); |
| 358 |
std::vector<unsigned char> buf; |
| 359 |
buf.reserve(size_t(long(sz[1]) * sz[0] * 3)); |
| 360 |
for (int j = 0; j < sz[1]; j++) { |
| 361 |
T const *row = orig.getRow(j); |
| 362 |
|
| 363 |
for (int i = 0; i < sz[0]; i++) { |
| 364 |
int ind = row[i]; |
| 365 |
if (ind < 0 || ind >= nColors) { |
| 366 |
MWAW_DEBUG_MSG(("MWAWPictBitmapInternal::getPPMData invalid index %d\n", ind)); |
| 367 |
if (!buf.empty()) |
| 368 |
data.append(&buf[0], buf.size()); |
| 369 |
return false; |
| 370 |
} |
| 371 |
uint32_t col = indexedColor[size_t(ind)].value(); |
| 372 |
for (int c = 0, depl=16; c < 3; c++, depl-=8) |
| 373 |
buf.push_back(static_cast<unsigned char>((col>>depl)&0xFF)); |
| 374 |
} |
| 375 |
} |
| 376 |
if (!buf.empty()) |
| 377 |
data.append(&buf[0], buf.size()); |
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
//! Internal: helper function to create a PPM for a color bitmap |
| 382 |
static bool getPPMData(MWAWPictBitmapContainer<MWAWColor> const &orig, librevenge::RVNGBinaryData &data) |
| 383 |
{ |
| 384 |
MWAWVec2i sz = orig.size(); |
| 385 |
if (sz[0] <= 0 || sz[1] <= 0) return false; |
| 386 |
|
| 387 |
data.clear(); |
| 388 |
std::stringstream f; |
| 389 |
f << "P6\n" << sz[0] << " " << sz[1] << " 255\n"; |
| 390 |
std::string const &header = f.str(); |
| 391 |
data.append(reinterpret_cast<const unsigned char *>(header.c_str()), header.size()); |
| 392 |
for (int j = 0; j < sz[1]; j++) { |
| 393 |
MWAWColor const *row = orig.getRow(j); |
| 394 |
|
| 395 |
for (int i = 0; i < sz[0]; i++) { |
| 396 |
uint32_t col = row[i].value(); |
| 397 |
for (int c = 0, depl=16; c < 3; c++, depl-=8) |
| 398 |
data.append(static_cast<unsigned char>((col>>depl)&0xFF)); |
| 399 |
} |
| 400 |
} |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
// |
| 405 |
// functions used by getPBMData |
| 406 |
// |
| 407 |
static void writeU16(unsigned char *buffer, unsigned &position, const unsigned value) |
| 408 |
{ |
| 409 |
buffer[position++] = static_cast<unsigned char>(value & 0xFF); |
| 410 |
buffer[position++] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 411 |
} |
| 412 |
|
| 413 |
static void writeU32(unsigned char *buffer, unsigned &position, const unsigned value) |
| 414 |
{ |
| 415 |
buffer[position++] = static_cast<unsigned char>(value & 0xFF); |
| 416 |
buffer[position++] = static_cast<unsigned char>((value >> 8) & 0xFF); |
| 417 |
buffer[position++] = static_cast<unsigned char>((value >> 16) & 0xFF); |
| 418 |
buffer[position++] = static_cast<unsigned char>((value >> 24) & 0xFF); |
| 419 |
} |
| 420 |
|
| 421 |
//! Internal: helper function to create a BMP for a color bitmap (freely inspired from libpwg::WPGBitmap.cpp) |
| 422 |
static bool getBMPData(MWAWPictBitmapContainer<MWAWColor> const &orig, librevenge::RVNGBinaryData &data) |
| 423 |
{ |
| 424 |
MWAWVec2i sz = orig.size(); |
| 425 |
if (sz[0] <= 0 || sz[1] <= 0) return false; |
| 426 |
|
| 427 |
auto tmpPixelSize = static_cast<unsigned>(sz[0]*sz[1]); |
| 428 |
unsigned tmpBufferPosition = 0; |
| 429 |
|
| 430 |
unsigned tmpDIBImageSize = tmpPixelSize * 4; |
| 431 |
if (tmpPixelSize > tmpDIBImageSize) // overflow !!! |
| 432 |
return false; |
| 433 |
|
| 434 |
unsigned const headerSize=56; |
| 435 |
unsigned tmpDIBOffsetBits = 14 + headerSize; |
| 436 |
unsigned tmpDIBFileSize = tmpDIBOffsetBits + tmpDIBImageSize; |
| 437 |
if (tmpDIBImageSize > tmpDIBFileSize) // overflow !!! |
| 438 |
return false; |
| 439 |
|
| 440 |
std::unique_ptr<unsigned char[]> tmpDIBBuffer{new unsigned char[tmpDIBFileSize]}; |
| 441 |
if (!tmpDIBBuffer) { |
| 442 |
MWAW_DEBUG_MSG(("getBMPData: fail to allocated the data buffer\n")); |
| 443 |
return false; |
| 444 |
} |
| 445 |
// Create DIB file header |
| 446 |
writeU16(tmpDIBBuffer.get(), tmpBufferPosition, 0x4D42); // Type |
| 447 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, static_cast<unsigned>(tmpDIBFileSize)); // Size |
| 448 |
writeU16(tmpDIBBuffer.get(), tmpBufferPosition, 0); // Reserved1 |
| 449 |
writeU16(tmpDIBBuffer.get(), tmpBufferPosition, 0); // Reserved2 |
| 450 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, static_cast<unsigned>(tmpDIBOffsetBits)); // OffsetBits |
| 451 |
|
| 452 |
// Create DIB Info header |
| 453 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, headerSize); // Size |
| 454 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, static_cast<unsigned>(sz[0])); // Width |
| 455 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, static_cast<unsigned>(sz[1])); // Height |
| 456 |
writeU16(tmpDIBBuffer.get(), tmpBufferPosition, 1); // Planes |
| 457 |
writeU16(tmpDIBBuffer.get(), tmpBufferPosition, 32); // BitCount |
| 458 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0); // Compression |
| 459 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, static_cast<unsigned>(tmpDIBImageSize)); // SizeImage |
| 460 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 5904); // XPelsPerMeter: 300ppi |
| 461 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 5904); // YPelsPerMeter: 300ppi |
| 462 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0); // ColorsUsed |
| 463 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0); // ColorsImportant |
| 464 |
|
| 465 |
// Create DIB V3 Info header |
| 466 |
|
| 467 |
/* this is needed to create alpha picture ; but as both LibreOffice/OpenOffice ignore the alpha channel... */ |
| 468 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0x00FF0000); /* biRedMask */ |
| 469 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0x0000FF00); /* biGreenMask */ |
| 470 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0x000000FF); /* biBlueMask */ |
| 471 |
writeU32(tmpDIBBuffer.get(), tmpBufferPosition, 0xFF000000); /* biAlphaMask */ |
| 472 |
|
| 473 |
// Write DIB Image data |
| 474 |
for (int i = sz[1] - 1; i >= 0 && tmpBufferPosition < tmpDIBFileSize; i--) { |
| 475 |
MWAWColor const *row = orig.getRow(i); |
| 476 |
|
| 477 |
for (int j = 0; j < sz[0] && tmpBufferPosition < tmpDIBFileSize; j++) { |
| 478 |
uint32_t col = row[j].value(); |
| 479 |
|
| 480 |
tmpDIBBuffer.get()[tmpBufferPosition++]=static_cast<unsigned char>(col&0xFF); |
| 481 |
tmpDIBBuffer.get()[tmpBufferPosition++]=static_cast<unsigned char>((col>>8)&0xFF); |
| 482 |
tmpDIBBuffer.get()[tmpBufferPosition++]=static_cast<unsigned char>((col>>16)&0xFF); |
| 483 |
tmpDIBBuffer.get()[tmpBufferPosition++]=static_cast<unsigned char>((col>>24)&0xFF); |
| 484 |
} |
| 485 |
} |
| 486 |
data.clear(); |
| 487 |
data.append(tmpDIBBuffer.get(), tmpDIBFileSize); |
| 488 |
|
| 489 |
return true; |
| 490 |
} |
| 491 |
#endif |
| 492 |
} |
| 493 |
|
| 494 |
MWAWPictBitmapContainerBool::~MWAWPictBitmapContainerBool() |
| 495 |
{ |
| 496 |
} |
| 497 |
|
| 498 |
MWAWPictBitmap::~MWAWPictBitmap() |
| 499 |
{ |
| 500 |
} |
| 501 |
//////////////////////////////////////////////////////////// |
| 502 |
// BW bitmap |
| 503 |
//////////////////////////////////////////////////////////// |
| 504 |
|
| 505 |
bool MWAWPictBitmapBW::createFileData(librevenge::RVNGBinaryData &result) const |
| 506 |
{ |
| 507 |
#ifdef USE_ZLIB |
| 508 |
return MWAWPictBitmapInternal::getPNG1Data<bool>(m_data,result,false); |
| 509 |
#else |
| 510 |
return MWAWPictBitmapInternal::getPBMData<bool>(m_data,result,false); |
| 511 |
#endif |
| 512 |
} |
| 513 |
|
| 514 |
MWAWColor MWAWPictBitmapBW::getAverageColor() const |
| 515 |
{ |
| 516 |
auto const &sz=m_data.size(); |
| 517 |
if (sz[0] <= 0 || sz[1] <= 0) { |
| 518 |
MWAW_DEBUG_MSG(("MWAWPictBitmapBW::getAverageColor: called on empty picture\n")); |
| 519 |
return MWAWColor::black(); |
| 520 |
} |
| 521 |
unsigned long n=0; |
| 522 |
for (int j = 0; j < sz[1]; j++) { |
| 523 |
bool const *row = getRow(j); |
| 524 |
for (int i = 0; i < sz[0]; i++) |
| 525 |
if (!row[i]) ++n; |
| 526 |
} |
| 527 |
unsigned char c=(unsigned char)((255*n)/(unsigned long)(sz[0]*sz[1])); |
| 528 |
return MWAWColor(c,c,c); |
| 529 |
} |
| 530 |
|
| 531 |
//////////////////////////////////////////////////////////// |
| 532 |
// Color bitmap |
| 533 |
//////////////////////////////////////////////////////////// |
| 534 |
|
| 535 |
bool MWAWPictBitmapColor::createFileData(librevenge::RVNGBinaryData &result) const |
| 536 |
{ |
| 537 |
#ifdef USE_ZLIB |
| 538 |
return MWAWPictBitmapInternal::getPNGData(m_data,result,m_hasAlpha); |
| 539 |
#else |
| 540 |
if (m_hasAlpha) return MWAWPictBitmapInternal::getBMPData(m_data,result); |
| 541 |
return MWAWPictBitmapInternal::getPPMData(m_data,result); |
| 542 |
#endif |
| 543 |
} |
| 544 |
|
| 545 |
MWAWColor MWAWPictBitmapColor::getAverageColor() const |
| 546 |
{ |
| 547 |
auto const &sz=m_data.size(); |
| 548 |
if (sz[0] <= 0 || sz[1] <= 0) { |
| 549 |
MWAW_DEBUG_MSG(("MWAWPictBitmapColor::getAverageColor: called on empty picture\n")); |
| 550 |
return MWAWColor::black(); |
| 551 |
} |
| 552 |
unsigned long n[]= {0,0,0,0}; |
| 553 |
for (int j = 0; j < sz[1]; j++) { |
| 554 |
MWAWColor const *row = getRow(j); |
| 555 |
for (int i = 0; i < sz[0]; i++) { |
| 556 |
n[0]+=row[i].getRed(); |
| 557 |
n[1]+=row[i].getGreen(); |
| 558 |
n[2]+=row[i].getBlue(); |
| 559 |
n[3]+=row[i].getAlpha(); |
| 560 |
} |
| 561 |
} |
| 562 |
return MWAWColor((unsigned char)(n[0]/(unsigned long)(sz[0]*sz[1])), |
| 563 |
(unsigned char)(n[1]/(unsigned long)(sz[0]*sz[1])), |
| 564 |
(unsigned char)(n[2]/(unsigned long)(sz[0]*sz[1])), |
| 565 |
(unsigned char)(n[3]/(unsigned long)(sz[0]*sz[1]))); |
| 566 |
} |
| 567 |
|
| 568 |
//////////////////////////////////////////////////////////// |
| 569 |
// Indexed bitmap |
| 570 |
//////////////////////////////////////////////////////////// |
| 571 |
|
| 572 |
bool MWAWPictBitmapIndexed::createFileData(librevenge::RVNGBinaryData &result) const |
| 573 |
{ |
| 574 |
#ifdef USE_ZLIB |
| 575 |
if (m_colors.size() && MWAWPictBitmapInternal::getPNGData<int>(m_data,result,m_colors)) return true; |
| 576 |
return MWAWPictBitmapInternal::getPNG1Data<int>(m_data,result,0); |
| 577 |
#else |
| 578 |
if (m_colors.size() && MWAWPictBitmapInternal::getPPMData<int>(m_data,result,m_colors)) return true; |
| 579 |
return MWAWPictBitmapInternal::getPBMData<int>(m_data,result,0); |
| 580 |
#endif |
| 581 |
} |
| 582 |
|
| 583 |
MWAWColor MWAWPictBitmapIndexed::getAverageColor() const |
| 584 |
{ |
| 585 |
auto const &sz=m_data.size(); |
| 586 |
if (sz[0] <= 0 || sz[1] <= 0) { |
| 587 |
MWAW_DEBUG_MSG(("MWAWPictBitmapIndexed::getAverageColor: called on empty picture\n")); |
| 588 |
return MWAWColor::black(); |
| 589 |
} |
| 590 |
unsigned long n[]= {0,0,0,0}; |
| 591 |
size_t nCol=m_colors.size(); |
| 592 |
for (int j = 0; j < sz[1]; j++) { |
| 593 |
int const *row = getRow(j); |
| 594 |
for (int i = 0; i < sz[0]; i++) { |
| 595 |
size_t const id=size_t(row[i]); |
| 596 |
if (id >= nCol) continue; |
| 597 |
n[0]+=m_colors[id].getRed(); |
| 598 |
n[1]+=m_colors[id].getGreen(); |
| 599 |
n[2]+=m_colors[id].getBlue(); |
| 600 |
n[3]+=m_colors[id].getAlpha(); |
| 601 |
} |
| 602 |
} |
| 603 |
return MWAWColor((unsigned char)(n[0]/(unsigned long)(sz[0]*sz[1])), |
| 604 |
(unsigned char)(n[1]/(unsigned long)(sz[0]*sz[1])), |
| 605 |
(unsigned char)(n[2]/(unsigned long)(sz[0]*sz[1])), |
| 606 |
(unsigned char)(n[3]/(unsigned long)(sz[0]*sz[1]))); |
| 607 |
} |
| 608 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |