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 "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  Vec2i 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  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
109  return m_data[i+m_size[0]*j];
110  }
112  T const *getRow(int j) const
113  {
114  assert(m_data != 0L && j>=0 && j < m_size[1]);
115  return m_data+m_size[0]*j;
116  }
117 
119  void set(int i, int j, T const &v)
120  {
121  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
122  m_data[i+j*m_size[0]] = v;
123  }
124 
126  template <class U>
127  void setRow(int j, U const *val)
128  {
129  assert(m_data != 0L && j>=0 && j < m_size[1]);
130  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
131  }
132 
134  template <class U>
135  void setColumn(int i, U const *val)
136  {
137  assert(m_data != 0L && i>=0 && i < m_size[0]);
138  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
139  }
140 
141 private:
144 protected:
148  T *m_data;
149 };
150 
153 {
154 public:
157 
159  int cmp(MWAWPictBitmapContainerBool const &orig) const
160  {
161  int diff = m_size.cmpY(orig.m_size);
162  if (diff) return diff;
163  if (!m_data) return orig.m_data ? 1 : 0;
164  if (!orig.m_data) return -1;
165  for (int i=0; i < m_size[0]*m_size[1]; i++) {
166  if (m_data[i] == orig.m_data[i]) continue;
167  return m_data[i] ? 1 : -1;
168  }
169  return 0;
170  }
171 
173  void setRowPacked(int j, unsigned char const *val)
174  {
175  assert(m_data != 0L && j>=0 && j < m_size[1]);
176  for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
177  unsigned char v = *(val++);
178  unsigned char mask = 0x80;
179  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
180  m_data[ind] = ((v&mask) != 0);
181  mask = (unsigned char)(mask >> 1);
182  }
183  }
184  }
185 };
186 
188 class MWAWPictBitmap : public MWAWPict
189 {
190 public:
192  enum SubType { BW, Indexed, Color };
194  virtual Type getType() const
195  {
196  return MWAWPict::Bitmap;
197  }
199  virtual SubType getSubType() const = 0;
200 
202  virtual bool getBinary(librevenge::RVNGBinaryData &res, std::string &s) const
203  {
204  if (!valid()) return false;
205 
206  s = "image/pict";
207  createFileData(res);
208  return true;
209  }
210 
212  virtual bool valid() const
213  {
214  return false;
215  }
216 
219  virtual int cmp(MWAWPict const &a) const
220  {
221  int diff = MWAWPict::cmp(a);
222  if (diff) return diff;
223  MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
224 
225  // the type
226  diff = getSubType() - aPict.getSubType();
227  if (diff) return (diff < 0) ? -1 : 1;
228 
229  return 0;
230  }
231 
232 protected:
234  virtual bool createFileData(librevenge::RVNGBinaryData &result) const = 0;
235 
238  {
239  setBdBox(Box2f(Vec2f(0,0), sz));
240  }
241 };
242 
245 {
246 public:
248  virtual SubType getSubType() const
249  {
250  return BW;
251  }
252 
255  virtual int cmp(MWAWPict const &a) const
256  {
257  int diff = MWAWPictBitmap::cmp(a);
258  if (diff) return diff;
259  MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
260 
261  return m_data.cmp(aPict.m_data);
262  }
263 
265  virtual bool valid() const
266  {
267  return m_data.ok();
268  }
269 
271  MWAWPictBitmapBW(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
272 
274  Vec2i const &size() const
275  {
276  return m_data.size();
277  }
279  int numRows() const
280  {
281  return m_data.numRows();
282  }
284  int numColumns() const
285  {
286  return m_data.numColumns();
287  }
289  bool get(int i, int j) const
290  {
291  return m_data.get(i,j);
292  }
294  bool const *getRow(int j) const
295  {
296  return m_data.getRow(j);
297  }
299  void set(int i, int j, bool v)
300  {
301  m_data.set(i,j, v);
302  }
304  void setRow(int j, bool const *val)
305  {
306  m_data.setRow(j, val);
307  }
309  void setRowPacked(int j, unsigned char const *val)
310  {
311  m_data.setRowPacked(j, val);
312  }
314  void setColumn(int i, bool const *val)
315  {
316  m_data.setColumn(i, val);
317  }
318 
319 protected:
321  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
322 
325 };
326 
329 {
330 public:
332  virtual SubType getSubType() const
333  {
334  return Indexed;
335  }
336 
339  virtual int cmp(MWAWPict const &a) const
340  {
341  int diff = MWAWPictBitmap::cmp(a);
342  if (diff) return diff;
343  MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
344 
345  diff=int(m_colors.size())-int(aPict.m_colors.size());
346  if (diff) return (diff < 0) ? -1 : 1;
347  for (size_t c=0; c < m_colors.size(); c++) {
348  if (m_colors[c] < aPict.m_colors[c])
349  return 1;
350  if (m_colors[c] > aPict.m_colors[c])
351  return -1;
352  }
353  return m_data.cmp(aPict.m_data);
354  }
355 
357  virtual bool valid() const
358  {
359  return m_data.ok();
360  }
361 
364 
366  Vec2i const &size() const
367  {
368  return m_data.size();
369  }
371  int numRows() const
372  {
373  return m_data.numRows();
374  }
376  int numColumns() const
377  {
378  return m_data.numColumns();
379  }
381  int get(int i, int j) const
382  {
383  return m_data.get(i,j);
384  }
386  int const *getRow(int j) const
387  {
388  return m_data.getRow(j);
389  }
390 
392  void set(int i, int j, int v)
393  {
394  m_data.set(i,j, v);
395  }
397  template <class U> void setRow(int j, U const *val)
398  {
399  m_data.setRow(j, val);
400  }
402  template <class U> void setColumn(int i, U const *val)
403  {
404  m_data.setColumn(i, val);
405  }
406 
408  std::vector<MWAWColor> const &getColors() const
409  {
410  return m_colors;
411  }
413  void setColors(std::vector<MWAWColor> const &cols)
414  {
415  m_colors = cols;
416  }
417 
418 protected:
420  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
421 
425  std::vector<MWAWColor> m_colors;
426 };
427 
436 {
437 public:
439  virtual SubType getSubType() const
440  {
441  return Indexed;
442  }
443 
446  virtual int cmp(MWAWPict const &a) const
447  {
448  int diff = MWAWPictBitmap::cmp(a);
449  if (diff) return diff;
450  MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
451 
452  return m_data.cmp(aPict.m_data);
453  }
454 
456  virtual bool valid() const
457  {
458  return m_data.ok();
459  }
460 
462  MWAWPictBitmapColor(Vec2i const &sz, bool useAlphaChannel=false) : MWAWPictBitmap(sz), m_data(sz), m_hasAlpha(useAlphaChannel) { }
463 
465  Vec2i const &size() const
466  {
467  return m_data.size();
468  }
470  int numRows() const
471  {
472  return m_data.numRows();
473  }
475  int numColumns() const
476  {
477  return m_data.numColumns();
478  }
480  MWAWColor get(int i, int j) const
481  {
482  return m_data.get(i,j);
483  }
485  MWAWColor const *getRow(int j) const
486  {
487  return m_data.getRow(j);
488  }
489 
491  void set(int i, int j, MWAWColor const &v)
492  {
493  m_data.set(i,j, v);
494  }
496  void setRow(int j, MWAWColor const *val)
497  {
498  m_data.setRow(j, val);
499  }
501  void setColumn(int i, MWAWColor const *val)
502  {
503  m_data.setColumn(i, val);
504  }
505 
506 protected:
508  virtual bool createFileData(librevenge::RVNGBinaryData &result) const;
509 
512 
515 };
516 #endif
517 // 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:314
Definition: MWAWPictBitmap.hxx:192
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:100
void set(int i, int j, MWAWColor const &v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:491
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition: MWAWPictBitmap.hxx:148
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:219
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:456
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:446
Definition: MWAWPictBitmap.hxx:192
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:274
Vec2< float > Vec2f
Vec2 of float.
Definition: libmwaw_internal.hxx:683
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:265
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:135
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:470
void setRow(int j, U const *val)
sets a line of m_data
Definition: MWAWPictBitmap.hxx:127
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:119
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:402
MWAWPictBitmap(Vec2i const &sz)
protected constructor: use check to construct a picture
Definition: MWAWPictBitmap.hxx:237
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:496
virtual SubType getSubType() const =0
returns the picture subtype
Definition: MWAWPictBitmap.hxx:192
Vec2i m_size
the size
Definition: MWAWPictBitmap.hxx:146
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:465
Box2< float > Box2f
Box2 of float.
Definition: libmwaw_internal.hxx:1044
the class to store a color
Definition: libmwaw_internal.hxx:166
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:106
a bitmap of MWAWColor to store true color bitmap
Definition: MWAWPictBitmap.hxx:435
int const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:386
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:284
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:279
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:357
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:304
int numRows() const
gets the number of row
Definition: MWAWPictBitmap.hxx:95
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition: MWAWPictBitmap.hxx:408
a bitmap of bool to store black-white bitmap
Definition: MWAWPictBitmap.hxx:244
bool const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:294
void set(int i, int j, int v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:392
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:192
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:152
void set(int i, int j, bool v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:299
MWAWPictBitmapIndexed(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:363
bool m_hasAlpha
true if the bitmap has alpha color
Definition: MWAWPictBitmap.hxx:514
a bitmap of int to store indexed bitmap
Definition: MWAWPictBitmap.hxx:328
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition: MWAWPictBitmap.hxx:413
MWAWPictBitmapColor(Vec2i const &sz, bool useAlphaChannel=false)
the constructor
Definition: MWAWPictBitmap.hxx:462
MWAWPictBitmapContainer< int > m_data
the m_data
Definition: MWAWPictBitmap.hxx:423
void setBdBox(Box2f const &box)
sets the bdbox of the picture
Definition: MWAWPict.hxx:85
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:159
virtual bool getBinary(librevenge::RVNGBinaryData &res, std::string &s) const
returns the final librevenge::RVNGBinary data
Definition: MWAWPictBitmap.hxx:202
int cmpY(Vec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libmwaw_internal.hxx:627
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)
void setRowPacked(int j, unsigned char const *val)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:173
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:485
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:397
virtual bool createFileData(librevenge::RVNGBinaryData &result) const =0
abstract function which creates the result file
MWAWPictBitmapContainerBool(Vec2i const &sz)
constructor
Definition: MWAWPictBitmap.hxx:156
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:65
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:188
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:475
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:371
T const * getRow(int j) const
accessor of a row m_data
Definition: MWAWPictBitmap.hxx:112
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:501
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:212
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:255
virtual SubType getSubType() const
returns the picture subtype
Definition: MWAWPictBitmap.hxx:248
MWAWPictBitmapContainer(Vec2i const &sz)
constructor given size
Definition: MWAWPictBitmap.hxx:58
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:425
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:332
virtual Type getType() const
returns the picture type
Definition: MWAWPictBitmap.hxx:194
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:439
void setRowPacked(int j, unsigned char const *val)
sets all cell contents of a row given packed m_data
Definition: MWAWPictBitmap.hxx:309
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:376
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition: MWAWPictBitmap.hxx:511
Vec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:90
MWAWPictBitmapBW(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:271
Generic function used to define/store a picture.
Definition: MWAWPict.hxx:52
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:366
MWAWPictBitmapContainerBool m_data
the data
Definition: MWAWPictBitmap.hxx:324
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:339

Generated on Mon Apr 10 2017 17:36:53 for libmwaw by doxygen 1.8.8