MWAWPictBitmap.hxx
Go to the documentation of this file.
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 (wrlach@gmail.com)
18* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20* Copyright (C) 2006, 2007 Andrew Ziem
21* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
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#ifndef MWAW_PICT_BITMAP
38# define MWAW_PICT_BITMAP
39
40
41#include <limits>
42#include <vector>
43
44#include "libmwaw_internal.hxx"
45#include "MWAWDebug.hxx"
46#include "MWAWPict.hxx"
47
49//
50// Some container
51//
53
55template <class T> class MWAWPictBitmapContainer
56{
57public:
60 : m_size(sz)
61 , m_data(nullptr)
62 {
63 if (m_size[0]<=0 || m_size[1]<=0 || m_size[0]>(std::numeric_limits<int>::max)()/m_size[1]) return;
64 size_t const numElements=size_t(m_size[0])*size_t(m_size[1]);
65 m_data = new T[numElements];
66 std::uninitialized_fill_n(m_data, numElements, T());
67 }
68
70 {
71 if (m_data) delete [] m_data;
72 }
73
75 bool ok() const
76 {
77 return (m_data != nullptr);
78 }
79
81 int cmp(MWAWPictBitmapContainer<T> const &orig) const
82 {
83 int diff = m_size.cmpY(orig.m_size);
84 if (diff) return diff;
85 if (!m_data) return orig.m_data ? 1 : 0;
86 if (!orig.m_data) return -1;
87 for (int i=0; i < m_size[0]*m_size[1]; i++) {
88 if (m_data[i] < orig.m_data[i]) return -1;
89 if (m_data[i] > orig.m_data[i]) return 1;
90 }
91 return 0;
92 }
93
94 MWAWVec2i const &size() const
95 {
96 return m_size;
97 }
98
99 int numRows() const
100 {
101 return m_size[0];
102 }
103
104 int numColumns() const
105 {
106 return m_size[1];
107 }
108
110 T const &get(int i, int j) const
111 {
112 if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1])
114 return m_data[i+m_size[0]*j];
115 }
116
117 T const *getRow(int j) const
118 {
119 if (m_data == nullptr || j<0 || j >= m_size[1])
121 return m_data+m_size[0]*j;
122 }
123
125 void set(int i, int j, T const &v)
126 {
127 if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1]) {
128 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::set: call with bad coordinate %d %d\n", i, j));
129 return;
130 }
131 m_data[i+j*m_size[0]] = v;
132 }
133
135 template <class U>
136 void setRow(int j, U const *val)
137 {
138 if (m_data == nullptr || j<0 || j >= m_size[1]) {
139 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setRow: call with bad coordinate %d\n", j));
140 return;
141 }
142 for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
143 }
144
146 template <class U>
147 void setColumn(int i, U const *val)
148 {
149 if (m_data == nullptr || i<0 || i >= m_size[0]) {
150 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setColumn: call with bad coordinate %d\n", i));
151 return;
152 }
153 for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
154 }
155
156private:
159protected:
164};
165
168{
169public:
172 : MWAWPictBitmapContainer<bool>(sz)
173 {
174 }
175
178 int cmp(MWAWPictBitmapContainerBool const &orig) const
179 {
180 int diff = m_size.cmpY(orig.m_size);
181 if (diff) return diff;
182 if (!m_data) return orig.m_data ? 1 : 0;
183 if (!orig.m_data) return -1;
184 for (int i=0; i < m_size[0]*m_size[1]; i++) {
185 if (m_data[i] == orig.m_data[i]) continue;
186 return m_data[i] ? 1 : -1;
187 }
188 return 0;
189 }
190
192 void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
193 {
194 if (m_data == nullptr || j<0 || j >= m_size[1] || val >= end) {
195 MWAW_DEBUG_MSG(("MWAWPictBitmapContainerBool::setRowPacked: call with bad coordinate %d\n", j));
196 return;
197 }
198 for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
199 unsigned char v = (val < end) ? *(val++) : 0;
200 unsigned char mask = 0x80;
201 for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
202 m_data[ind] = ((v&mask) != 0);
203 mask = static_cast<unsigned char>(mask >> 1);
204 }
205 }
206 }
207};
208
211{
212public:
214 ~MWAWPictBitmap() override;
215
219 Type getType() const override
220 {
221 return MWAWPict::Bitmap;
222 }
223
224 virtual SubType getSubType() const = 0;
225
227 bool getBinary(MWAWEmbeddedObject &picture) const override
228 {
229 if (!valid()) return false;
230
231 librevenge::RVNGBinaryData data;
232 createFileData(data);
233 picture=MWAWEmbeddedObject(data, "image/pict");
234 return true;
235 }
236
238 virtual bool valid() const
239 {
240 return false;
241 }
242
244 virtual MWAWColor getAverageColor() const = 0;
247 int cmp(MWAWPict const &a) const override
248 {
249 int diff = MWAWPict::cmp(a);
250 if (diff) return diff;
251 auto const &aPict = static_cast<MWAWPictBitmap const &>(a);
252
253 // the type
254 diff = getSubType() - aPict.getSubType();
255 if (diff) return (diff < 0) ? -1 : 1;
256
257 return 0;
258 }
259
260protected:
262 virtual bool createFileData(librevenge::RVNGBinaryData &result) const = 0;
263
265 explicit MWAWPictBitmap(MWAWVec2i const &sz)
266 {
268 }
269};
270
273{
274public:
276 SubType getSubType() const final
277 {
278 return BW;
279 }
280
283 int cmp(MWAWPict const &a) const final
284 {
285 int diff = MWAWPictBitmap::cmp(a);
286 if (diff) return diff;
287 auto const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
288
289 return m_data.cmp(aPict.m_data);
290 }
291
293 bool valid() const final
294 {
295 return m_data.ok();
296 }
297
299 explicit MWAWPictBitmapBW(MWAWVec2i const &sz)
300 : MWAWPictBitmap(sz)
301 , m_data(sz)
302 {
303 }
304
306 MWAWVec2i const &size() const
307 {
308 return m_data.size();
309 }
310
311 int numRows() const
312 {
313 return m_data.numRows();
314 }
315
316 int numColumns() const
317 {
318 return m_data.numColumns();
319 }
320
321 bool get(int i, int j) const
322 {
323 return m_data.get(i,j);
324 }
325
326 bool const *getRow(int j) const
327 {
328 return m_data.getRow(j);
329 }
330
331 void set(int i, int j, bool v)
332 {
333 m_data.set(i,j, v);
334 }
335
336 void setRow(int j, bool const *val)
337 {
338 m_data.setRow(j, val);
339 }
340
341 void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
342 {
343 m_data.setRowPacked(j, val, end);
344 }
345
346 void setColumn(int i, bool const *val)
347 {
348 m_data.setColumn(i, val);
349 }
350
351 MWAWColor getAverageColor() const final;
352
353protected:
355 bool createFileData(librevenge::RVNGBinaryData &result) const final;
356
359};
360
363{
364public:
366 SubType getSubType() const final
367 {
368 return Indexed;
369 }
370
373 int cmp(MWAWPict const &a) const final
374 {
375 int diff = MWAWPictBitmap::cmp(a);
376 if (diff) return diff;
377 auto const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
378
379 diff=int(m_colors.size())-int(aPict.m_colors.size());
380 if (diff) return (diff < 0) ? -1 : 1;
381 for (size_t c=0; c < m_colors.size(); c++) {
382 if (m_colors[c] < aPict.m_colors[c])
383 return 1;
384 if (m_colors[c] > aPict.m_colors[c])
385 return -1;
386 }
387 return m_data.cmp(aPict.m_data);
388 }
389
391 bool valid() const final
392 {
393 return m_data.ok();
394 }
395
396 MWAWColor getAverageColor() const final;
397
400 : MWAWPictBitmap(sz)
401 , m_data(sz)
402 , m_colors()
403 {
404 }
405
407 MWAWVec2i const &size() const
408 {
409 return m_data.size();
410 }
411
412 int numRows() const
413 {
414 return m_data.numRows();
415 }
416
417 int numColumns() const
418 {
419 return m_data.numColumns();
420 }
421
422 int get(int i, int j) const
423 {
424 return m_data.get(i,j);
425 }
426
427 int const *getRow(int j) const
428 {
429 return m_data.getRow(j);
430 }
431
433 void set(int i, int j, int v)
434 {
435 m_data.set(i,j, v);
436 }
437
438 template <class U> void setRow(int j, U const *val)
439 {
440 m_data.setRow(j, val);
441 }
442
443 template <class U> void setColumn(int i, U const *val)
444 {
445 m_data.setColumn(i, val);
446 }
447
449 std::vector<MWAWColor> const &getColors() const
450 {
451 return m_colors;
452 }
453
454 void setColors(std::vector<MWAWColor> const &cols)
455 {
456 m_colors = cols;
457 }
458
459protected:
461 bool createFileData(librevenge::RVNGBinaryData &result) const final;
462
466 std::vector<MWAWColor> m_colors;
467};
468
477{
478public:
480 SubType getSubType() const final
481 {
482 return Color;
483 }
484
487 int cmp(MWAWPict const &a) const final
488 {
489 int diff = MWAWPictBitmap::cmp(a);
490 if (diff) return diff;
491 auto const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
492
493 return m_data.cmp(aPict.m_data);
494 }
495
497 bool valid() const final
498 {
499 return m_data.ok();
500 }
501
502 MWAWColor getAverageColor() const final;
503
505 explicit MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false)
506 : MWAWPictBitmap(sz)
507 , m_data(sz)
508 , m_hasAlpha(useAlphaChannel)
509 {
510 }
511
513 MWAWVec2i const &size() const
514 {
515 return m_data.size();
516 }
517
518 int numRows() const
519 {
520 return m_data.numRows();
521 }
522
523 int numColumns() const
524 {
525 return m_data.numColumns();
526 }
527
528 MWAWColor get(int i, int j) const
529 {
530 return m_data.get(i,j);
531 }
532
533 MWAWColor const *getRow(int j) const
534 {
535 return m_data.getRow(j);
536 }
537
539 void set(int i, int j, MWAWColor const &v)
540 {
541 m_data.set(i,j, v);
542 }
543
544 void setRow(int j, MWAWColor const *val)
545 {
546 m_data.setRow(j, val);
547 }
548
549 void setColumn(int i, MWAWColor const *val)
550 {
551 m_data.setColumn(i, val);
552 }
553
554protected:
556 bool createFileData(librevenge::RVNGBinaryData &result) const final;
557
560
563};
564#endif
565// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
void setColumn(int i, bool const *val)
sets all cell contents of a column
Definition MWAWPictBitmap.hxx:346
void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
sets all cell contents of a row given packed m_data
Definition MWAWPictBitmap.hxx:341
void set(int i, int j, bool v)
sets a cell contents
Definition MWAWPictBitmap.hxx:331
MWAWVec2i const & size() const
the picture size
Definition MWAWPictBitmap.hxx:306
int numRows() const
the number of rows
Definition MWAWPictBitmap.hxx:311
bool get(int i, int j) const
returns a cell content
Definition MWAWPictBitmap.hxx:321
bool const * getRow(int j) const
returns the cells content of a row
Definition MWAWPictBitmap.hxx:326
SubType getSubType() const final
returns the picture subtype
Definition MWAWPictBitmap.hxx:276
MWAWPictBitmapBW(MWAWVec2i const &sz)
the constructor
Definition MWAWPictBitmap.hxx:299
int cmp(MWAWPict const &a) const final
a virtual function used to obtain a strict order, must be redefined in the subs class
Definition MWAWPictBitmap.hxx:283
int numColumns() const
the number of columns
Definition MWAWPictBitmap.hxx:316
MWAWColor getAverageColor() const final
returns the average color
Definition MWAWPictBitmap.cxx:514
bool createFileData(librevenge::RVNGBinaryData &result) const final
function which creates the result file
Definition MWAWPictBitmap.cxx:505
MWAWPictBitmapContainerBool m_data
the data
Definition MWAWPictBitmap.hxx:358
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition MWAWPictBitmap.hxx:336
bool valid() const final
returns true if the picture is valid
Definition MWAWPictBitmap.hxx:293
SubType getSubType() const final
return the picture subtype
Definition MWAWPictBitmap.hxx:480
void set(int i, int j, MWAWColor const &v)
sets a cell contents
Definition MWAWPictBitmap.hxx:539
bool m_hasAlpha
true if the bitmap has alpha color
Definition MWAWPictBitmap.hxx:562
int cmp(MWAWPict const &a) const final
a virtual function used to obtain a strict order, must be redefined in the subs class
Definition MWAWPictBitmap.hxx:487
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition MWAWPictBitmap.hxx:549
MWAWVec2i const & size() const
the picture size
Definition MWAWPictBitmap.hxx:513
bool valid() const final
returns true if the picture is valid
Definition MWAWPictBitmap.hxx:497
MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false)
the constructor
Definition MWAWPictBitmap.hxx:505
int numColumns() const
the number of columns
Definition MWAWPictBitmap.hxx:523
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition MWAWPictBitmap.hxx:559
int numRows() const
the number of rows
Definition MWAWPictBitmap.hxx:518
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition MWAWPictBitmap.hxx:544
MWAWColor get(int i, int j) const
returns a cell content
Definition MWAWPictBitmap.hxx:528
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition MWAWPictBitmap.hxx:533
a bool container with a function to put packed row
Definition MWAWPictBitmap.hxx:168
~MWAWPictBitmapContainerBool() final
destructor
Definition MWAWPictBitmap.cxx:494
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition MWAWPictBitmap.hxx:178
MWAWPictBitmapContainerBool(MWAWVec2i const &sz)
constructor
Definition MWAWPictBitmap.hxx:171
void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
allows to use packed m_data
Definition MWAWPictBitmap.hxx:192
a template class to store a 2D array of m_data
Definition MWAWPictBitmap.hxx:56
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition MWAWPictBitmap.hxx:163
MWAWPictBitmapContainer(MWAWPictBitmapContainer const &orig)=delete
void set(int i, int j, T const &v)
sets a cell m_data
Definition MWAWPictBitmap.hxx:125
int numRows() const
gets the number of row
Definition MWAWPictBitmap.hxx:99
MWAWPictBitmapContainer(MWAWVec2i const &sz)
constructor given size
Definition MWAWPictBitmap.hxx:59
void setColumn(int i, U const *val)
sets a column of m_data
Definition MWAWPictBitmap.hxx:147
MWAWVec2i m_size
the size
Definition MWAWPictBitmap.hxx:161
void setRow(int j, U const *val)
sets a line of m_data
Definition MWAWPictBitmap.hxx:136
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)=delete
bool ok() const
returns ok, if the m_data is allocated
Definition MWAWPictBitmap.hxx:75
virtual ~MWAWPictBitmapContainer()
destructor
Definition MWAWPictBitmap.hxx:69
T const * getRow(int j) const
accessor of a row m_data
Definition MWAWPictBitmap.hxx:117
int cmp(MWAWPictBitmapContainer< T > const &orig) const
a comparison operator
Definition MWAWPictBitmap.hxx:81
MWAWVec2i const & size() const
return the array size
Definition MWAWPictBitmap.hxx:94
T const & get(int i, int j) const
accessor of a cell m_data
Definition MWAWPictBitmap.hxx:110
int numColumns() const
gets the number of column
Definition MWAWPictBitmap.hxx:104
MWAWPictBitmapIndexed(MWAWVec2i const &sz)
the constructor
Definition MWAWPictBitmap.hxx:399
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition MWAWPictBitmap.hxx:443
MWAWVec2i const & size() const
the picture size
Definition MWAWPictBitmap.hxx:407
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition MWAWPictBitmap.hxx:454
void setRow(int j, U const *val)
sets all cell contents of a row
Definition MWAWPictBitmap.hxx:438
int get(int i, int j) const
returns a cell content
Definition MWAWPictBitmap.hxx:422
std::vector< MWAWColor > m_colors
the colors
Definition MWAWPictBitmap.hxx:466
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition MWAWPictBitmap.hxx:449
int const * getRow(int j) const
returns the cells content of a row
Definition MWAWPictBitmap.hxx:427
SubType getSubType() const final
return the picture subtype
Definition MWAWPictBitmap.hxx:366
MWAWPictBitmapContainer< int > m_data
the m_data
Definition MWAWPictBitmap.hxx:464
int numRows() const
the number of rows
Definition MWAWPictBitmap.hxx:412
int cmp(MWAWPict const &a) const final
a virtual function used to obtain a strict order, must be redefined in the subs class
Definition MWAWPictBitmap.hxx:373
bool valid() const final
returns true if the picture is valid
Definition MWAWPictBitmap.hxx:391
int numColumns() const
the number of columns
Definition MWAWPictBitmap.hxx:417
void set(int i, int j, int v)
sets a cell contents
Definition MWAWPictBitmap.hxx:433
virtual MWAWColor getAverageColor() const =0
returns the average color
SubType
the picture subtype: blackwhite, indexed, color
Definition MWAWPictBitmap.hxx:217
@ BW
Definition MWAWPictBitmap.hxx:217
@ Color
Definition MWAWPictBitmap.hxx:217
@ Indexed
Definition MWAWPictBitmap.hxx:217
MWAWPictBitmap(MWAWVec2i const &sz)
protected constructor: use check to construct a picture
Definition MWAWPictBitmap.hxx:265
virtual SubType getSubType() const =0
returns the picture subtype
Type getType() const override
returns the picture type
Definition MWAWPictBitmap.hxx:219
virtual bool createFileData(librevenge::RVNGBinaryData &result) const =0
abstract function which creates the result file
~MWAWPictBitmap() override
destructor
Definition MWAWPictBitmap.cxx:498
int cmp(MWAWPict const &a) const override
a virtual function used to obtain a strict order, must be redefined in the subs class
Definition MWAWPictBitmap.hxx:247
bool getBinary(MWAWEmbeddedObject &picture) const override
returns the final picture
Definition MWAWPictBitmap.hxx:227
virtual bool valid() const
returns true if the picture is valid
Definition MWAWPictBitmap.hxx:238
MWAWPict()
protected constructor must not be called directly
Definition MWAWPict.hxx:143
Type
the different picture types:
Definition MWAWPict.hxx:63
@ Bitmap
Definition MWAWPict.hxx:63
virtual int cmp(MWAWPict const &a) const
a virtual function used to obtain a strict order, must be redefined in the subs class
Definition MWAWPict.hxx:101
void setBdBox(MWAWBox2f const &box)
sets the bdbox of the picture
Definition MWAWPict.hxx:84
Definition libmwaw_internal.hxx:148
MWAWVec2< int > MWAWVec2i
MWAWVec2 of int.
Definition libmwaw_internal.hxx:838
MWAWBox2< float > MWAWBox2f
MWAWBox2 of float.
Definition libmwaw_internal.hxx:1193
MWAWVec2< float > MWAWVec2f
MWAWVec2 of float.
Definition libmwaw_internal.hxx:842
#define MWAW_DEBUG_MSG(M)
Definition libmwaw_internal.hxx:129
Definition MWAWDocument.hxx:57
the class to store a color
Definition libmwaw_internal.hxx:192
small class use to define a embedded object
Definition libmwaw_internal.hxx:467

Generated on for libmwaw by doxygen 1.16.1