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:
59  {
60  if (m_size[0]*m_size[1] == 0) return;
61  m_data = new T[size_t(m_size[0]*m_size[1])];
62  std::uninitialized_fill_n(m_data, m_size[0] * m_size[1], T());
63  }
66  {
67  if (m_data) delete [] m_data;
68  }
69 
71  bool ok() const
72  {
73  return (m_data != 0L);
74  }
75 
77  int cmp(MWAWPictBitmapContainer<T> const &orig) const
78  {
79  int diff = m_size.cmpY(orig.m_size);
80  if (diff) return diff;
81  if (!m_data) return orig.m_data ? 1 : 0;
82  if (!orig.m_data) return -1;
83  for (int i=0; i < m_size[0]*m_size[1]; i++) {
84  if (m_data[i] < orig.m_data[i]) return -1;
85  if (m_data[i] > orig.m_data[i]) return 1;
86  }
87  return 0;
88  }
90  MWAWVec2i const &size() const
91  {
92  return m_size;
93  }
95  int numRows() const
96  {
97  return m_size[0];
98  }
100  int numColumns() const
101  {
102  return m_size[1];
103  }
104 
106  T const &get(int i, int j) const
107  {
108  if (m_data == 0L || i<0 || i >= m_size[0] || j<0 || j >= m_size[1])
110  return m_data[i+m_size[0]*j];
111  }
113  T const *getRow(int j) const
114  {
115  if (m_data == 0L || j<0 || j >= m_size[1])
117  return m_data+m_size[0]*j;
118  }
119 
121  void set(int i, int j, T const &v)
122  {
123  if (m_data == 0L || i<0 || i >= m_size[0] || j<0 || j >= m_size[1]) {
124  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::set: call with bad coordinate %d %d\n", i, j));
125  return;
126  }
127  m_data[i+j*m_size[0]] = v;
128  }
129 
131  template <class U>
132  void setRow(int j, U const *val)
133  {
134  if (m_data == 0L || j<0 || j >= m_size[1]) {
135  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setRow: call with bad coordinate %d\n", j));
136  return;
137  }
138  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
139  }
140 
142  template <class U>
143  void setColumn(int i, U const *val)
144  {
145  if (m_data == 0L || i<0 || i >= m_size[0]) {
146  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setColumn: call with bad coordinate %d\n", i));
147  return;
148  }
149  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
150  }
151 
152 private:
155 protected:
159  T *m_data;
160 };
161 
164 {
165 public:
168 
170  int cmp(MWAWPictBitmapContainerBool const &orig) const
171  {
172  int diff = m_size.cmpY(orig.m_size);
173  if (diff) return diff;
174  if (!m_data) return orig.m_data ? 1 : 0;
175  if (!orig.m_data) return -1;
176  for (int i=0; i < m_size[0]*m_size[1]; i++) {
177  if (m_data[i] == orig.m_data[i]) continue;
178  return m_data[i] ? 1 : -1;
179  }
180  return 0;
181  }
182 
184  void setRowPacked(int j, unsigned char const *val)
185  {
186  if (m_data == 0L || j<0 || j >= m_size[1]) {
187  MWAW_DEBUG_MSG(("MWAWPictBitmapContainerBool::setRowPacked: call with bad coordinate %d\n", j));
188  return;
189  }
190  for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
191  unsigned char v = *(val++);
192  unsigned char mask = 0x80;
193  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
194  m_data[ind] = ((v&mask) != 0);
195  mask = (unsigned char)(mask >> 1);
196  }
197  }
198  }
199 };
200 
202 class MWAWPictBitmap : public MWAWPict
203 {
204 public:
206  enum SubType { BW, Indexed, Color };
208  virtual Type getType() const
209  {
210  return MWAWPict::Bitmap;
211  }
213  virtual SubType getSubType() const = 0;
214 
216  virtual bool getBinary(MWAWEmbeddedObject &picture) const
217  {
218  if (!valid()) return false;
219 
220  librevenge::RVNGBinaryData data;
221  createFileData(data);
222  picture=MWAWEmbeddedObject(data, "image/pict");
223  return true;
224  }
225 
227  virtual bool valid() const
228  {
229  return false;
230  }
231 
234  virtual int cmp(MWAWPict const &a) const
235  {
236  int diff = MWAWPict::cmp(a);
237  if (diff) return diff;
238  MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
239 
240  // the type
241  diff = getSubType() - aPict.getSubType();
242  if (diff) return (diff < 0) ? -1 : 1;
243 
244  return 0;
245  }
246 
247 protected:
249  virtual bool createFileData(librevenge::RVNGBinaryData &result) const = 0;
250 
253  {
254  setBdBox(MWAWBox2f(MWAWVec2f(0,0), sz));
255  }
256 };
257 
260 {
261 public:
263  virtual SubType getSubType() const
264  {
265  return BW;
266  }
267 
270  virtual int cmp(MWAWPict const &a) const
271  {
272  int diff = MWAWPictBitmap::cmp(a);
273  if (diff) return diff;
274  MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
275 
276  return m_data.cmp(aPict.m_data);
277  }
278 
280  virtual bool valid() const
281  {
282  return m_data.ok();
283  }
284 
287 
289  MWAWVec2i const &size() const
290  {
291  return m_data.size();
292  }
294  int numRows() const
295  {
296  return m_data.numRows();
297  }
299  int numColumns() const
300  {
301  return m_data.numColumns();
302  }
304  bool get(int i, int j) const
305  {
306  return m_data.get(i,j);
307  }
309  bool const *getRow(int j) const
310  {
311  return m_data.getRow(j);
312  }
314  void set(int i, int j, bool v)
315  {
316  m_data.set(i,j, v);
317  }
319  void setRow(int j, bool const *val)
320  {
321  m_data.setRow(j, val);
322  }
324  void setRowPacked(int j, unsigned char const *val)
325  {
326  m_data.setRowPacked(j, val);
327  }
329  void setColumn(int i, bool const *val)
330  {
331  m_data.setColumn(i, val);
332  }
333 
334 protected:
336  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
337 
340 };
341 
344 {
345 public:
347  virtual SubType getSubType() const
348  {
349  return Indexed;
350  }
351 
354  virtual int cmp(MWAWPict const &a) const
355  {
356  int diff = MWAWPictBitmap::cmp(a);
357  if (diff) return diff;
358  MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
359 
360  diff=int(m_colors.size())-int(aPict.m_colors.size());
361  if (diff) return (diff < 0) ? -1 : 1;
362  for (size_t c=0; c < m_colors.size(); c++) {
363  if (m_colors[c] < aPict.m_colors[c])
364  return 1;
365  if (m_colors[c] > aPict.m_colors[c])
366  return -1;
367  }
368  return m_data.cmp(aPict.m_data);
369  }
370 
372  virtual bool valid() const
373  {
374  return m_data.ok();
375  }
376 
379 
381  MWAWVec2i const &size() const
382  {
383  return m_data.size();
384  }
386  int numRows() const
387  {
388  return m_data.numRows();
389  }
391  int numColumns() const
392  {
393  return m_data.numColumns();
394  }
396  int get(int i, int j) const
397  {
398  return m_data.get(i,j);
399  }
401  int const *getRow(int j) const
402  {
403  return m_data.getRow(j);
404  }
405 
407  void set(int i, int j, int v)
408  {
409  m_data.set(i,j, v);
410  }
412  template <class U> void setRow(int j, U const *val)
413  {
414  m_data.setRow(j, val);
415  }
417  template <class U> void setColumn(int i, U const *val)
418  {
419  m_data.setColumn(i, val);
420  }
421 
423  std::vector<MWAWColor> const &getColors() const
424  {
425  return m_colors;
426  }
428  void setColors(std::vector<MWAWColor> const &cols)
429  {
430  m_colors = cols;
431  }
432 
433 protected:
435  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
436 
440  std::vector<MWAWColor> m_colors;
441 };
442 
451 {
452 public:
454  virtual SubType getSubType() const
455  {
456  return Indexed;
457  }
458 
461  virtual int cmp(MWAWPict const &a) const
462  {
463  int diff = MWAWPictBitmap::cmp(a);
464  if (diff) return diff;
465  MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
466 
467  return m_data.cmp(aPict.m_data);
468  }
469 
471  virtual bool valid() const
472  {
473  return m_data.ok();
474  }
475 
477  MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false) : MWAWPictBitmap(sz), m_data(sz), m_hasAlpha(useAlphaChannel) { }
478 
480  MWAWVec2i const &size() const
481  {
482  return m_data.size();
483  }
485  int numRows() const
486  {
487  return m_data.numRows();
488  }
490  int numColumns() const
491  {
492  return m_data.numColumns();
493  }
495  MWAWColor get(int i, int j) const
496  {
497  return m_data.get(i,j);
498  }
500  MWAWColor const *getRow(int j) const
501  {
502  return m_data.getRow(j);
503  }
504 
506  void set(int i, int j, MWAWColor const &v)
507  {
508  m_data.set(i,j, v);
509  }
511  void setRow(int j, MWAWColor const *val)
512  {
513  m_data.setRow(j, val);
514  }
516  void setColumn(int i, MWAWColor const *val)
517  {
518  m_data.setColumn(i, val);
519  }
520 
521 protected:
523  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
524 
527 
530 };
531 #endif
532 // 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:329
MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false)
the constructor
Definition: MWAWPictBitmap.hxx:477
Definition: MWAWPictBitmap.hxx:206
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:100
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:381
void set(int i, int j, MWAWColor const &v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:506
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition: MWAWPictBitmap.hxx:159
virtual int cmp(MWAWPict const &a) const
a virtual function used to obtain a strict order, must be redefined in the subs class ...
Definition: MWAWPictBitmap.hxx:234
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:471
virtual int cmp(MWAWPict const &a) const
a virtual function used to obtain a strict order, must be redefined in the subs class ...
Definition: MWAWPictBitmap.hxx:461
Definition: MWAWPictBitmap.hxx:206
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:280
MWAWVec2i m_size
the size
Definition: MWAWPictBitmap.hxx:157
MWAWVec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:90
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:143
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:485
void setRow(int j, U const *val)
sets a line of m_data
Definition: MWAWPictBitmap.hxx:132
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:121
virtual bool createFileData(librevenge::RVNGBinaryData &result) const =0
abstract function which creates the result file
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:417
virtual SubType getSubType() const =0
returns the picture subtype
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:511
Definition: MWAWPictBitmap.hxx:206
the class to store a color
Definition: libmwaw_internal.hxx:176
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
T const & get(int i, int j) const
accessor of a cell m_data
Definition: MWAWPictBitmap.hxx:106
a bitmap of MWAWColor to store true color bitmap
Definition: MWAWPictBitmap.hxx:450
int const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:401
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:299
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:294
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
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:372
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:319
int numRows() const
gets the number of row
Definition: MWAWPictBitmap.hxx:95
#define MWAW_DEBUG_MSG(M)
Definition: libmwaw_internal.hxx:126
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition: MWAWPictBitmap.hxx:423
int cmpY(MWAWVec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libmwaw_internal.hxx:715
a bitmap of bool to store black-white bitmap
Definition: MWAWPictBitmap.hxx:259
bool const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:309
void set(int i, int j, int v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:407
Definition: libmwaw_internal.hxx:144
MWAWPictBitmapIndexed(MWAWVec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:378
MWAWVec2< float > MWAWVec2f
MWAWVec2 of float.
Definition: libmwaw_internal.hxx:771
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:206
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:163
void set(int i, int j, bool v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:314
bool m_hasAlpha
true if the bitmap has alpha color
Definition: MWAWPictBitmap.hxx:529
a bitmap of int to store indexed bitmap
Definition: MWAWPictBitmap.hxx:343
MWAWPictBitmap(MWAWVec2i const &sz)
protected constructor: use check to construct a picture
Definition: MWAWPictBitmap.hxx:252
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition: MWAWPictBitmap.hxx:428
MWAWPictBitmapContainer(MWAWVec2i const &sz)
constructor given size
Definition: MWAWPictBitmap.hxx:58
MWAWPictBitmapContainer< int > m_data
the m_data
Definition: MWAWPictBitmap.hxx:438
virtual bool createFileData(librevenge::RVNGBinaryData &result) const
function which creates the result file
Definition: MWAWPictBitmap.cxx:227
Type
the different picture types:
Definition: MWAWPict.hxx:63
small class use to define a embedded object
Definition: libmwaw_internal.hxx:412
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:170
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)
void setRowPacked(int j, unsigned char const *val)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:184
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:500
virtual bool getBinary(MWAWEmbeddedObject &picture) const
returns the final picture
Definition: MWAWPictBitmap.hxx:216
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:412
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:65
MWAWBox2< float > MWAWBox2f
MWAWBox2 of float.
Definition: libmwaw_internal.hxx:1132
virtual bool createFileData(librevenge::RVNGBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:246
Generic class used to construct bitmap.
Definition: MWAWPictBitmap.hxx:202
MWAWPictBitmapBW(MWAWVec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:286
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:490
int cmp(MWAWPictBitmapContainer< T > const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:77
bool ok() const
returns ok, if the m_data is allocated
Definition: MWAWPictBitmap.hxx:71
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:386
T const * getRow(int j) const
accessor of a row m_data
Definition: MWAWPictBitmap.hxx:113
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:516
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:227
virtual int cmp(MWAWPict const &a) const
a virtual function used to obtain a strict order, must be redefined in the subs class ...
Definition: MWAWPictBitmap.hxx:270
virtual SubType getSubType() const
returns the picture subtype
Definition: MWAWPictBitmap.hxx:263
Definition: MWAWPict.hxx:63
virtual bool createFileData(librevenge::RVNGBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:236
std::vector< MWAWColor > m_colors
the colors
Definition: MWAWPictBitmap.hxx:440
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:289
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:347
MWAWPictBitmapContainerBool(MWAWVec2i const &sz)
constructor
Definition: MWAWPictBitmap.hxx:167
virtual Type getType() const
returns the picture type
Definition: MWAWPictBitmap.hxx:208
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:454
void setRowPacked(int j, unsigned char const *val)
sets all cell contents of a row given packed m_data
Definition: MWAWPictBitmap.hxx:324
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:391
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition: MWAWPictBitmap.hxx:526
MWAWVec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:480
Generic function used to define/store a picture.
Definition: MWAWPict.hxx:51
MWAWPictBitmapContainerBool m_data
the data
Definition: MWAWPictBitmap.hxx:339
virtual int cmp(MWAWPict const &a) const
a virtual function used to obtain a strict order, must be redefined in the subs class ...
Definition: MWAWPictBitmap.hxx:354

Generated on Thu Nov 5 2015 16:32:45 for libmwaw by doxygen 1.8.10