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 <vector>
42 
43 #include "libmwaw_internal.hxx"
44 #include "MWAWDebug.hxx"
45 #include "MWAWPict.hxx"
46 
48 //
49 // Some container
50 //
52 
54 template <class T> class MWAWPictBitmapContainer
55 {
56 public:
58  explicit MWAWPictBitmapContainer(MWAWVec2i const &sz)
59  : m_size(sz)
60  , m_data(nullptr)
61  {
62  if (m_size[0]*m_size[1] == 0) return;
63  m_data = new T[size_t(m_size[0]*m_size[1])];
64  std::uninitialized_fill_n(m_data, m_size[0] * m_size[1], T());
65  }
68  {
69  if (m_data) delete [] m_data;
70  }
71 
73  bool ok() const
74  {
75  return (m_data != nullptr);
76  }
77 
79  int cmp(MWAWPictBitmapContainer<T> const &orig) const
80  {
81  int diff = m_size.cmpY(orig.m_size);
82  if (diff) return diff;
83  if (!m_data) return orig.m_data ? 1 : 0;
84  if (!orig.m_data) return -1;
85  for (int i=0; i < m_size[0]*m_size[1]; i++) {
86  if (m_data[i] < orig.m_data[i]) return -1;
87  if (m_data[i] > orig.m_data[i]) return 1;
88  }
89  return 0;
90  }
92  MWAWVec2i const &size() const
93  {
94  return m_size;
95  }
97  int numRows() const
98  {
99  return m_size[0];
100  }
102  int numColumns() const
103  {
104  return m_size[1];
105  }
106 
108  T const &get(int i, int j) const
109  {
110  if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1])
112  return m_data[i+m_size[0]*j];
113  }
115  T const *getRow(int j) const
116  {
117  if (m_data == nullptr || j<0 || j >= m_size[1])
119  return m_data+m_size[0]*j;
120  }
121 
123  void set(int i, int j, T const &v)
124  {
125  if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1]) {
126  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::set: call with bad coordinate %d %d\n", i, j));
127  return;
128  }
129  m_data[i+j*m_size[0]] = v;
130  }
131 
133  template <class U>
134  void setRow(int j, U const *val)
135  {
136  if (m_data == nullptr || j<0 || j >= m_size[1]) {
137  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setRow: call with bad coordinate %d\n", j));
138  return;
139  }
140  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
141  }
142 
144  template <class U>
145  void setColumn(int i, U const *val)
146  {
147  if (m_data == nullptr || i<0 || i >= m_size[0]) {
148  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setColumn: call with bad coordinate %d\n", i));
149  return;
150  }
151  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
152  }
153 
154 private:
155  MWAWPictBitmapContainer(MWAWPictBitmapContainer const &orig) = delete;
157 protected:
161  T *m_data;
162 };
163 
166 {
167 public:
170  : MWAWPictBitmapContainer<bool>(sz)
171  {
172  }
176  int cmp(MWAWPictBitmapContainerBool const &orig) const
177  {
178  int diff = m_size.cmpY(orig.m_size);
179  if (diff) return diff;
180  if (!m_data) return orig.m_data ? 1 : 0;
181  if (!orig.m_data) return -1;
182  for (int i=0; i < m_size[0]*m_size[1]; i++) {
183  if (m_data[i] == orig.m_data[i]) continue;
184  return m_data[i] ? 1 : -1;
185  }
186  return 0;
187  }
188 
190  void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
191  {
192  if (m_data == nullptr || j<0 || j >= m_size[1] || val >= end) {
193  MWAW_DEBUG_MSG(("MWAWPictBitmapContainerBool::setRowPacked: call with bad coordinate %d\n", j));
194  return;
195  }
196  for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
197  unsigned char v = (val < end) ? *(val++) : 0;
198  unsigned char mask = 0x80;
199  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
200  m_data[ind] = ((v&mask) != 0);
201  mask = static_cast<unsigned char>(mask >> 1);
202  }
203  }
204  }
205 };
206 
208 class MWAWPictBitmap : public MWAWPict
209 {
210 public:
212  ~MWAWPictBitmap() override;
213 
215  enum SubType { BW, Indexed, Color };
217  Type getType() const override
218  {
219  return MWAWPict::Bitmap;
220  }
222  virtual SubType getSubType() const = 0;
223 
225  bool getBinary(MWAWEmbeddedObject &picture) const override
226  {
227  if (!valid()) return false;
228 
229  librevenge::RVNGBinaryData data;
230  createFileData(data);
231  picture=MWAWEmbeddedObject(data, "image/pict");
232  return true;
233  }
234 
236  virtual bool valid() const
237  {
238  return false;
239  }
240 
242  virtual MWAWColor getAverageColor() const = 0;
245  int cmp(MWAWPict const &a) const override
246  {
247  int diff = MWAWPict::cmp(a);
248  if (diff) return diff;
249  auto const &aPict = static_cast<MWAWPictBitmap const &>(a);
250 
251  // the type
252  diff = getSubType() - aPict.getSubType();
253  if (diff) return (diff < 0) ? -1 : 1;
254 
255  return 0;
256  }
257 
258 protected:
260  virtual bool createFileData(librevenge::RVNGBinaryData &result) const = 0;
261 
263  explicit MWAWPictBitmap(MWAWVec2i const &sz)
264  {
266  }
267 };
268 
270 class MWAWPictBitmapBW final : public MWAWPictBitmap
271 {
272 public:
274  SubType getSubType() const final
275  {
276  return BW;
277  }
278 
281  int cmp(MWAWPict const &a) const final
282  {
283  int diff = MWAWPictBitmap::cmp(a);
284  if (diff) return diff;
285  auto const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
286 
287  return m_data.cmp(aPict.m_data);
288  }
289 
291  bool valid() const final
292  {
293  return m_data.ok();
294  }
295 
297  explicit MWAWPictBitmapBW(MWAWVec2i const &sz)
298  : MWAWPictBitmap(sz)
299  , m_data(sz)
300  {
301  }
302 
304  MWAWVec2i const &size() const
305  {
306  return m_data.size();
307  }
309  int numRows() const
310  {
311  return m_data.numRows();
312  }
314  int numColumns() const
315  {
316  return m_data.numColumns();
317  }
319  bool get(int i, int j) const
320  {
321  return m_data.get(i,j);
322  }
324  bool const *getRow(int j) const
325  {
326  return m_data.getRow(j);
327  }
329  void set(int i, int j, bool v)
330  {
331  m_data.set(i,j, v);
332  }
334  void setRow(int j, bool const *val)
335  {
336  m_data.setRow(j, val);
337  }
339  void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
340  {
341  m_data.setRowPacked(j, val, end);
342  }
344  void setColumn(int i, bool const *val)
345  {
346  m_data.setColumn(i, val);
347  }
349  MWAWColor getAverageColor() const final;
350 
351 protected:
353  bool createFileData(librevenge::RVNGBinaryData &result) const final;
354 
357 };
358 
361 {
362 public:
364  SubType getSubType() const final
365  {
366  return Indexed;
367  }
368 
371  int cmp(MWAWPict const &a) const final
372  {
373  int diff = MWAWPictBitmap::cmp(a);
374  if (diff) return diff;
375  auto const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
376 
377  diff=int(m_colors.size())-int(aPict.m_colors.size());
378  if (diff) return (diff < 0) ? -1 : 1;
379  for (size_t c=0; c < m_colors.size(); c++) {
380  if (m_colors[c] < aPict.m_colors[c])
381  return 1;
382  if (m_colors[c] > aPict.m_colors[c])
383  return -1;
384  }
385  return m_data.cmp(aPict.m_data);
386  }
387 
389  bool valid() const final
390  {
391  return m_data.ok();
392  }
394  MWAWColor getAverageColor() const final;
395 
397  explicit MWAWPictBitmapIndexed(MWAWVec2i const &sz)
398  : MWAWPictBitmap(sz)
399  , m_data(sz)
400  , m_colors()
401  {
402  }
403 
405  MWAWVec2i const &size() const
406  {
407  return m_data.size();
408  }
410  int numRows() const
411  {
412  return m_data.numRows();
413  }
415  int numColumns() const
416  {
417  return m_data.numColumns();
418  }
420  int get(int i, int j) const
421  {
422  return m_data.get(i,j);
423  }
425  int const *getRow(int j) const
426  {
427  return m_data.getRow(j);
428  }
429 
431  void set(int i, int j, int v)
432  {
433  m_data.set(i,j, v);
434  }
436  template <class U> void setRow(int j, U const *val)
437  {
438  m_data.setRow(j, val);
439  }
441  template <class U> void setColumn(int i, U const *val)
442  {
443  m_data.setColumn(i, val);
444  }
445 
447  std::vector<MWAWColor> const &getColors() const
448  {
449  return m_colors;
450  }
452  void setColors(std::vector<MWAWColor> const &cols)
453  {
454  m_colors = cols;
455  }
456 
457 protected:
459  bool createFileData(librevenge::RVNGBinaryData &result) const final;
460 
464  std::vector<MWAWColor> m_colors;
465 };
466 
475 {
476 public:
478  SubType getSubType() const final
479  {
480  return Color;
481  }
482 
485  int cmp(MWAWPict const &a) const final
486  {
487  int diff = MWAWPictBitmap::cmp(a);
488  if (diff) return diff;
489  auto const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
490 
491  return m_data.cmp(aPict.m_data);
492  }
493 
495  bool valid() const final
496  {
497  return m_data.ok();
498  }
500  MWAWColor getAverageColor() const final;
501 
503  MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false)
504  : MWAWPictBitmap(sz)
505  , m_data(sz)
506  , m_hasAlpha(useAlphaChannel)
507  {
508  }
509 
511  MWAWVec2i const &size() const
512  {
513  return m_data.size();
514  }
516  int numRows() const
517  {
518  return m_data.numRows();
519  }
521  int numColumns() const
522  {
523  return m_data.numColumns();
524  }
526  MWAWColor get(int i, int j) const
527  {
528  return m_data.get(i,j);
529  }
531  MWAWColor const *getRow(int j) const
532  {
533  return m_data.getRow(j);
534  }
535 
537  void set(int i, int j, MWAWColor const &v)
538  {
539  m_data.set(i,j, v);
540  }
542  void setRow(int j, MWAWColor const *val)
543  {
544  m_data.setRow(j, val);
545  }
547  void setColumn(int i, MWAWColor const *val)
548  {
549  m_data.setColumn(i, val);
550  }
551 
552 protected:
554  bool createFileData(librevenge::RVNGBinaryData &result) const final;
555 
558 
561 };
562 #endif
563 // 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:344
Definition: MWAWPictBitmap.hxx:215
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition: MWAWPictBitmap.hxx:161
Type getType() const override
returns the picture type
Definition: MWAWPictBitmap.hxx:217
Definition: MWAWPictBitmap.hxx:215
Definition: MWAWDocument.hxx:56
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:410
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition: MWAWPictBitmap.hxx:447
MWAWVec2i m_size
the size
Definition: MWAWPictBitmap.hxx:159
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:102
T const * getRow(int j) const
accessor of a row m_data
Definition: MWAWPictBitmap.hxx:115
~MWAWPictBitmapContainerBool() final
destructor
Definition: MWAWPictBitmap.cxx:494
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:415
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:521
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:145
virtual MWAWColor getAverageColor() const =0
returns the average color
void setRow(int j, U const *val)
sets a line of m_data
Definition: MWAWPictBitmap.hxx:134
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:511
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:123
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:441
T const & get(int i, int j) const
accessor of a cell m_data
Definition: MWAWPictBitmap.hxx:108
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:542
virtual SubType getSubType() const =0
returns the picture subtype
Definition: MWAWPictBitmap.hxx:215
bool const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:324
int numRows() const
gets the number of row
Definition: MWAWPictBitmap.hxx:97
the class to store a color
Definition: libmwaw_internal.hxx:192
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:309
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:281
a bitmap of MWAWColor to store true color bitmap
Definition: MWAWPictBitmap.hxx:474
SubType getSubType() const final
return the picture subtype
Definition: MWAWPictBitmap.hxx:478
void setBdBox(MWAWBox2f const &box)
sets the bdbox of the picture
Definition: MWAWPict.hxx:84
a template class to store a 2D array of m_data
Definition: MWAWPictBitmap.hxx:54
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:334
bool valid() const final
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:389
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:314
SubType getSubType() const final
returns the picture subtype
Definition: MWAWPictBitmap.hxx:274
#define MWAW_DEBUG_MSG(M)
Definition: libmwaw_internal.hxx:129
a bitmap of bool to store black-white bitmap
Definition: MWAWPictBitmap.hxx:270
MWAWVec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:92
Definition: libmwaw_internal.hxx:147
MWAWVec2< float > MWAWVec2f
MWAWVec2 of float.
Definition: libmwaw_internal.hxx:842
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:215
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:165
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
bool m_hasAlpha
true if the bitmap has alpha color
Definition: MWAWPictBitmap.hxx:560
bool ok() const
returns ok, if the m_data is allocated
Definition: MWAWPictBitmap.hxx:73
a bitmap of int to store indexed bitmap
Definition: MWAWPictBitmap.hxx:360
int const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:425
MWAWPictBitmap(MWAWVec2i const &sz)
protected constructor: use check to construct a picture
Definition: MWAWPictBitmap.hxx:263
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition: MWAWPictBitmap.hxx:452
MWAWPictBitmapContainer(MWAWVec2i const &sz)
constructor given size
Definition: MWAWPictBitmap.hxx:58
MWAWPictBitmapContainer< int > m_data
the m_data
Definition: MWAWPictBitmap.hxx:462
bool valid() const final
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:495
Type
the different picture types:
Definition: MWAWPict.hxx:63
small class use to define a embedded object
Definition: libmwaw_internal.hxx:467
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:405
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:436
virtual bool createFileData(librevenge::RVNGBinaryData &result) const =0
abstract function which creates the result file
SubType getSubType() const final
return the picture subtype
Definition: MWAWPictBitmap.hxx:364
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:67
bool createFileData(librevenge::RVNGBinaryData &result) const final
function which creates the result file
Definition: MWAWPictBitmap.cxx:505
MWAWBox2< float > MWAWBox2f
MWAWBox2 of float.
Definition: libmwaw_internal.hxx:1193
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:176
Generic class used to construct bitmap.
Definition: MWAWPictBitmap.hxx:208
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:245
MWAWPictBitmapBW(MWAWVec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:297
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:531
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:339
int cmpY(MWAWVec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libmwaw_internal.hxx:786
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:547
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:516
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)=delete
Definition: MWAWPict.hxx:63
std::vector< MWAWColor > m_colors
the colors
Definition: MWAWPictBitmap.hxx:464
bool getBinary(MWAWEmbeddedObject &picture) const override
returns the final picture
Definition: MWAWPictBitmap.hxx:225
int cmp(MWAWPictBitmapContainer< T > const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:79
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:371
MWAWPictBitmapContainerBool(MWAWVec2i const &sz)
constructor
Definition: MWAWPictBitmap.hxx:169
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:485
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition: MWAWPictBitmap.hxx:557
MWAWColor getAverageColor() const final
returns the average color
Definition: MWAWPictBitmap.cxx:514
Generic function used to define/store a picture.
Definition: MWAWPict.hxx:51
~MWAWPictBitmap() override
destructor
Definition: MWAWPictBitmap.cxx:498
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:304
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:236
bool valid() const final
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:291
void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:190
MWAWPictBitmapContainerBool m_data
the data
Definition: MWAWPictBitmap.hxx:356

Generated for libmwaw by doxygen 1.8.14