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 
47 class WPXBinaryData;
48 
50 //
51 // Some container
52 //
54 
56 template <class T> class MWAWPictBitmapContainer
57 {
58 public:
60  MWAWPictBitmapContainer(Vec2i const &sz) : m_size(sz), m_data(0L) {
61  if (m_size[0]*m_size[1] != 0) m_data = new T[size_t(m_size[0]*m_size[1])];
62  }
65  if (m_data) delete [] m_data;
66  }
67 
69  bool ok() const {
70  return (m_data != 0L);
71  }
72 
74  int cmp(MWAWPictBitmapContainer<T> const &orig) const {
75  int diff = m_size.cmpY(orig.m_size);
76  if (diff) return diff;
77  if (!m_data) return orig.m_data ? 1 : 0;
78  if (!orig.m_data) return -1;
79  for (int i=0; i < m_size[0]*m_size[1]; i++) {
80  if (m_data[i] < orig.m_data[i]) return -1;
81  if (m_data[i] > orig.m_data[i]) return 1;
82  }
83  return 0;
84  }
86  Vec2i const &size() const {
87  return m_size;
88  }
90  int numRows() const {
91  return m_size[0];
92  }
94  int numColumns() const {
95  return m_size[1];
96  }
97 
99  T const &get(int i, int j) const {
100  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
101  return m_data[i+m_size[0]*j];
102  }
104  T const *getRow(int j) const {
105  assert(m_data != 0L && j>=0 && j < m_size[1]);
106  return m_data+m_size[0]*j;
107  }
108 
110  void set(int i, int j, T const &v) {
111  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
112  m_data[i+j*m_size[0]] = v;
113  }
114 
116  template <class U>
117  void setRow(int j, U const *val) {
118  assert(m_data != 0L && j>=0 && j < m_size[1]);
119  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
120  }
121 
123  template <class U>
124  void setColumn(int i, U const *val) {
125  assert(m_data != 0L && i>=0 && i < m_size[0]);
126  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
127  }
128 
129 private:
132 protected:
136  T *m_data;
137 };
138 
141 {
142 public:
145 
147  int cmp(MWAWPictBitmapContainerBool const &orig) const {
148  int diff = m_size.cmpY(orig.m_size);
149  if (diff) return diff;
150  if (!m_data) return orig.m_data ? 1 : 0;
151  if (!orig.m_data) return -1;
152  for (int i=0; i < m_size[0]*m_size[1]; i++) {
153  if (m_data[i] == orig.m_data[i]) continue;
154  return m_data[i] ? 1 : -1;
155  }
156  return 0;
157  }
158 
160  void setRowPacked(int j, unsigned char const *val) {
161  assert(m_data != 0L && j>=0 && j < m_size[1]);
162  for (int i = 0, ind = j*m_size[0]; i < m_size[0]; ) {
163  unsigned char v = *(val++);
164  unsigned char mask = 0x80;
165  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
166  m_data[ind] = ((v&mask) != 0);
167  mask = (unsigned char) (mask >> 1);
168  }
169  }
170  }
171 };
172 
174 class MWAWPictBitmap : public MWAWPict
175 {
176 public:
178  enum SubType { BW, Indexed, Color };
180  virtual Type getType() const {
181  return MWAWPict::Bitmap;
182  }
184  virtual SubType getSubType() const = 0;
185 
187  virtual bool getBinary(WPXBinaryData &res, std::string &s) const {
188  if (!valid()) return false;
189 
190  s = "image/pict";
191  createFileData(res);
192  return true;
193  }
194 
196  virtual bool valid() const {
197  return false;
198  }
199 
202  virtual int cmp(MWAWPict const &a) const {
203  int diff = MWAWPict::cmp(a);
204  if (diff) return diff;
205  MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
206 
207  // the type
208  diff = getSubType() - aPict.getSubType();
209  if (diff) return (diff < 0) ? -1 : 1;
210 
211  return 0;
212  }
213 
214 protected:
216  virtual bool createFileData(WPXBinaryData &result) const = 0;
217 
219  MWAWPictBitmap(Vec2i const &sz) {
220  setBdBox(Box2f(Vec2f(0,0), sz));
221  }
222 };
223 
226 {
227 public:
229  virtual SubType getSubType() const {
230  return BW;
231  }
232 
235  virtual int cmp(MWAWPict const &a) const {
236  int diff = MWAWPictBitmap::cmp(a);
237  if (diff) return diff;
238  MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
239 
240  return m_data.cmp(aPict.m_data);
241  }
242 
244  virtual bool valid() const {
245  return m_data.ok();
246  }
247 
249  MWAWPictBitmapBW(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
250 
252  Vec2i const &size() const {
253  return m_data.size();
254  }
256  int numRows() const {
257  return m_data.numRows();
258  }
260  int numColumns() const {
261  return m_data.numColumns();
262  }
264  bool get(int i, int j) const {
265  return m_data.get(i,j);
266  }
268  bool const *getRow(int j) const {
269  return m_data.getRow(j);
270  }
272  void set(int i, int j, bool v) {
273  m_data.set(i,j, v);
274  }
276  void setRow(int j, bool const *val) {
277  m_data.setRow(j, val);
278  }
280  void setRowPacked(int j, unsigned char const *val) {
281  m_data.setRowPacked(j, val);
282  }
284  void setColumn(int i, bool const *val) {
285  m_data.setColumn(i, val);
286  }
287 
288 protected:
290  virtual bool createFileData(WPXBinaryData &result) const;
291 
294 };
295 
298 {
299 public:
301  virtual SubType getSubType() const {
302  return Indexed;
303  }
304 
307  virtual int cmp(MWAWPict const &a) const {
308  int diff = MWAWPictBitmap::cmp(a);
309  if (diff) return diff;
310  MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
311 
312  diff=int(m_colors.size())-int(aPict.m_colors.size());
313  if (diff) return (diff < 0) ? -1 : 1;
314  for (size_t c=0; c < m_colors.size(); c++) {
315  if (m_colors[c] < aPict.m_colors[c])
316  return 1;
317  if (m_colors[c] > aPict.m_colors[c])
318  return -1;
319  }
320  return m_data.cmp(aPict.m_data);
321  }
322 
324  virtual bool valid() const {
325  return m_data.ok();
326  }
327 
330 
332  Vec2i const &size() const {
333  return m_data.size();
334  }
336  int numRows() const {
337  return m_data.numRows();
338  }
340  int numColumns() const {
341  return m_data.numColumns();
342  }
344  int get(int i, int j) const {
345  return m_data.get(i,j);
346  }
348  int const *getRow(int j) const {
349  return m_data.getRow(j);
350  }
351 
353  void set(int i, int j, int v) {
354  m_data.set(i,j, v);
355  }
357  template <class U> void setRow(int j, U const *val) {
358  m_data.setRow(j, val);
359  }
361  template <class U> void setColumn(int i, U const *val) {
362  m_data.setColumn(i, val);
363  }
364 
366  std::vector<MWAWColor> const &getColors() const {
367  return m_colors;
368  }
370  void setColors(std::vector<MWAWColor> const &cols) {
371  m_colors = cols;
372  }
373 
374 protected:
376  virtual bool createFileData(WPXBinaryData &result) const;
377 
381  std::vector<MWAWColor> m_colors;
382 };
383 
386 {
387 public:
389  virtual SubType getSubType() const {
390  return Indexed;
391  }
392 
395  virtual int cmp(MWAWPict const &a) const {
396  int diff = MWAWPictBitmap::cmp(a);
397  if (diff) return diff;
398  MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
399 
400  return m_data.cmp(aPict.m_data);
401  }
402 
404  virtual bool valid() const {
405  return m_data.ok();
406  }
407 
409  MWAWPictBitmapColor(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
410 
412  Vec2i const &size() const {
413  return m_data.size();
414  }
416  int numRows() const {
417  return m_data.numRows();
418  }
420  int numColumns() const {
421  return m_data.numColumns();
422  }
424  MWAWColor get(int i, int j) const {
425  return m_data.get(i,j);
426  }
428  MWAWColor const *getRow(int j) const {
429  return m_data.getRow(j);
430  }
431 
433  void set(int i, int j, MWAWColor const &v) {
434  m_data.set(i,j, v);
435  }
437  void setRow(int j, MWAWColor const *val) {
438  m_data.setRow(j, val);
439  }
441  void setColumn(int i, MWAWColor const *val) {
442  m_data.setColumn(i, val);
443  }
444 
445 protected:
447  virtual bool createFileData(WPXBinaryData &result) const;
448 
451 };
452 #endif
453 // 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:284
Definition: MWAWPictBitmap.hxx:178
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:94
void set(int i, int j, MWAWColor const &v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:433
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition: MWAWPictBitmap.hxx:136
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:202
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:404
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:395
Definition: MWAWPictBitmap.hxx:178
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:252
Vec2< float > Vec2f
Vec2 of float.
Definition: libmwaw_internal.hxx:596
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:244
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:124
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:416
virtual bool createFileData(WPXBinaryData &result) const
function which creates the result file
Definition: MWAWPictBitmap.cxx:134
void setRow(int j, U const *val)
sets a line of m_data
Definition: MWAWPictBitmap.hxx:117
virtual bool createFileData(WPXBinaryData &result) const =0
abstract function which creates the result file
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:110
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:361
MWAWPictBitmap(Vec2i const &sz)
protected constructor: use check to construct a picture
Definition: MWAWPictBitmap.hxx:219
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:437
virtual SubType getSubType() const =0
returns the picture subtype
Definition: MWAWPictBitmap.hxx:178
Vec2i m_size
the size
Definition: MWAWPictBitmap.hxx:134
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:412
Box2< float > Box2f
Box2 of float.
Definition: libmwaw_internal.hxx:909
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:99
T const & get(int i, int j) const
accessor of a cell m_data
Definition: MWAWPictBitmap.hxx:99
a bitmap of Vec3u to store true color bitmap
Definition: MWAWPictBitmap.hxx:385
int const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:348
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:260
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:256
a template class to store a 2D array of m_data
Definition: MWAWPictBitmap.hxx:56
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:324
virtual bool createFileData(WPXBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:143
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:276
int numRows() const
gets the number of row
Definition: MWAWPictBitmap.hxx:90
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition: MWAWPictBitmap.hxx:366
virtual bool getBinary(WPXBinaryData &res, std::string &s) const
returns the final WPXBinary data
Definition: MWAWPictBitmap.hxx:187
a bitmap of bool to store black-white bitmap
Definition: MWAWPictBitmap.hxx:225
bool const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:268
void set(int i, int j, int v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:353
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:178
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:140
void set(int i, int j, bool v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:272
MWAWPictBitmapIndexed(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:329
a bitmap of int to store indexed bitmap
Definition: MWAWPictBitmap.hxx:297
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition: MWAWPictBitmap.hxx:370
MWAWPictBitmapContainer< int > m_data
the m_data
Definition: MWAWPictBitmap.hxx:379
void setBdBox(Box2f const &box)
sets the bdbox of the picture
Definition: MWAWPict.hxx:84
Type
the different picture types:
Definition: MWAWPict.hxx:64
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:147
virtual bool createFileData(WPXBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:152
int cmpY(Vec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libmwaw_internal.hxx:542
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)
void setRowPacked(int j, unsigned char const *val)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:160
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:428
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:357
MWAWPictBitmapContainerBool(Vec2i const &sz)
constructor
Definition: MWAWPictBitmap.hxx:144
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:64
Generic class used to construct bitmap.
Definition: MWAWPictBitmap.hxx:174
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:420
int cmp(MWAWPictBitmapContainer< T > const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:74
bool ok() const
returns ok, if the m_data is allocated
Definition: MWAWPictBitmap.hxx:69
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:336
MWAWPictBitmapColor(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:409
T const * getRow(int j) const
accessor of a row m_data
Definition: MWAWPictBitmap.hxx:104
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:441
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:196
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:235
virtual SubType getSubType() const
returns the picture subtype
Definition: MWAWPictBitmap.hxx:229
MWAWPictBitmapContainer(Vec2i const &sz)
constructor given size
Definition: MWAWPictBitmap.hxx:60
Definition: MWAWPict.hxx:64
std::vector< MWAWColor > m_colors
the colors
Definition: MWAWPictBitmap.hxx:381
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:301
virtual Type getType() const
returns the picture type
Definition: MWAWPictBitmap.hxx:180
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:389
void setRowPacked(int j, unsigned char const *val)
sets all cell contents of a row given packed m_data
Definition: MWAWPictBitmap.hxx:280
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:340
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition: MWAWPictBitmap.hxx:450
Vec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:86
MWAWPictBitmapBW(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:249
Generic function used to define/store a picture.
Definition: MWAWPict.hxx:52
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:332
MWAWPictBitmapContainerBool m_data
the data
Definition: MWAWPictBitmap.hxx:293
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:307

Generated on Tue Mar 10 2015 17:32:09 for libmwaw by doxygen 1.8.5