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 #include <assert.h>
41 
42 #include <vector>
43 
44 #include "libmwaw_internal.hxx"
45 #include "MWAWDebug.hxx"
46 #include "MWAWPict.hxx"
47 
49 //
50 // Some container
51 //
53 
55 template <class T> class MWAWPictBitmapContainer
56 {
57 public:
60  {
61  if (m_size[0]*m_size[1] == 0) return;
62  m_data = new T[size_t(m_size[0]*m_size[1])];
63  std::uninitialized_fill_n(m_data, m_size[0] * m_size[1], T());
64  }
67  {
68  if (m_data) delete [] m_data;
69  }
70 
72  bool ok() const
73  {
74  return (m_data != 0L);
75  }
76 
78  int cmp(MWAWPictBitmapContainer<T> const &orig) const
79  {
80  int diff = m_size.cmpY(orig.m_size);
81  if (diff) return diff;
82  if (!m_data) return orig.m_data ? 1 : 0;
83  if (!orig.m_data) return -1;
84  for (int i=0; i < m_size[0]*m_size[1]; i++) {
85  if (m_data[i] < orig.m_data[i]) return -1;
86  if (m_data[i] > orig.m_data[i]) return 1;
87  }
88  return 0;
89  }
91  MWAWVec2i const &size() const
92  {
93  return m_size;
94  }
96  int numRows() const
97  {
98  return m_size[0];
99  }
101  int numColumns() const
102  {
103  return m_size[1];
104  }
105 
107  T const &get(int i, int j) const
108  {
109  if (m_data == 0L || i<0 || i >= m_size[0] || j<0 || j >= m_size[1])
111  return m_data[i+m_size[0]*j];
112  }
114  T const *getRow(int j) const
115  {
116  if (m_data == 0L || j<0 || j >= m_size[1])
118  return m_data+m_size[0]*j;
119  }
120 
122  void set(int i, int j, T const &v)
123  {
124  if (m_data == 0L || i<0 || i >= m_size[0] || j<0 || j >= m_size[1]) {
125  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::set: call with bad coordinate %d %d\n", i, j));
126  return;
127  }
128  m_data[i+j*m_size[0]] = v;
129  }
130 
132  template <class U>
133  void setRow(int j, U const *val)
134  {
135  if (m_data == 0L || j<0 || j >= m_size[1]) {
136  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setRow: call with bad coordinate %d\n", j));
137  return;
138  }
139  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
140  }
141 
143  template <class U>
144  void setColumn(int i, U const *val)
145  {
146  if (m_data == 0L || i<0 || i >= m_size[0]) {
147  MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setColumn: call with bad coordinate %d\n", i));
148  return;
149  }
150  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
151  }
152 
153 private:
156 protected:
160  T *m_data;
161 };
162 
165 {
166 public:
169 
171  int cmp(MWAWPictBitmapContainerBool const &orig) const
172  {
173  int diff = m_size.cmpY(orig.m_size);
174  if (diff) return diff;
175  if (!m_data) return orig.m_data ? 1 : 0;
176  if (!orig.m_data) return -1;
177  for (int i=0; i < m_size[0]*m_size[1]; i++) {
178  if (m_data[i] == orig.m_data[i]) continue;
179  return m_data[i] ? 1 : -1;
180  }
181  return 0;
182  }
183 
185  void setRowPacked(int j, unsigned char const *val)
186  {
187  if (m_data == 0L || j<0 || j >= m_size[1]) {
188  MWAW_DEBUG_MSG(("MWAWPictBitmapContainerBool::setRowPacked: call with bad coordinate %d\n", j));
189  return;
190  }
191  for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
192  unsigned char v = *(val++);
193  unsigned char mask = 0x80;
194  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
195  m_data[ind] = ((v&mask) != 0);
196  mask = (unsigned char)(mask >> 1);
197  }
198  }
199  }
200 };
201 
203 class MWAWPictBitmap : public MWAWPict
204 {
205 public:
207  enum SubType { BW, Indexed, Color };
209  virtual Type getType() const
210  {
211  return MWAWPict::Bitmap;
212  }
214  virtual SubType getSubType() const = 0;
215 
217  virtual bool getBinary(librevenge::RVNGBinaryData &res, std::string &s) const
218  {
219  if (!valid()) return false;
220 
221  s = "image/pict";
222  createFileData(res);
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:207
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:101
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:160
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:207
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:280
MWAWVec2i m_size
the size
Definition: MWAWPictBitmap.hxx:158
MWAWVec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:91
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:144
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:133
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:122
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:417
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:511
virtual SubType getSubType() const =0
returns the picture subtype
Definition: MWAWPictBitmap.hxx:207
the class to store a color
Definition: libmwaw_internal.hxx:177
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:102
T const & get(int i, int j) const
accessor of a cell m_data
Definition: MWAWPictBitmap.hxx:107
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:85
a template class to store a 2D array of m_data
Definition: MWAWPictBitmap.hxx:55
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:96
#define MWAW_DEBUG_MSG(M)
Definition: libmwaw_internal.hxx:127
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:665
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:145
MWAWPictBitmapIndexed(MWAWVec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:378
MWAWVec2< float > MWAWVec2f
MWAWVec2 of float.
Definition: libmwaw_internal.hxx:721
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:207
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:164
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:59
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:64
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:171
virtual bool getBinary(librevenge::RVNGBinaryData &res, std::string &s) const
returns the final librevenge::RVNGBinary data
Definition: MWAWPictBitmap.hxx:217
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)
void setRowPacked(int j, unsigned char const *val)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:185
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:500
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:412
virtual bool createFileData(librevenge::RVNGBinaryData &result) const =0
abstract function which creates the result file
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:66
MWAWBox2< float > MWAWBox2f
MWAWBox2 of float.
Definition: libmwaw_internal.hxx:1082
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:203
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:78
bool ok() const
returns ok, if the m_data is allocated
Definition: MWAWPictBitmap.hxx:72
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:114
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:64
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:168
virtual Type getType() const
returns the picture type
Definition: MWAWPictBitmap.hxx:209
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:52
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 Jul 9 2015 20:26:36 for libmwaw by doxygen 1.8.8