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) : m_size(sz), m_data(0L)
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 
252  explicit MWAWPictBitmap(MWAWVec2i const &sz)
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 
286  explicit MWAWPictBitmapBW(MWAWVec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
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 
378  explicit MWAWPictBitmapIndexed(MWAWVec2i const &sz) : MWAWPictBitmap(sz), m_data(sz), m_colors() { }
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:

Generated on Tue Mar 1 2016 23:42:50 for libmwaw by doxygen 1.8.4