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  Vec2i const &size() const {
75  return m_size;
76  }
78  int numRows() const {
79  return m_size[0];
80  }
82  int numColumns() const {
83  return m_size[1];
84  }
85 
87  T const &get(int i, int j) const {
88  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
89  return m_data[i+m_size[0]*j];
90  }
92  T const *getRow(int j) const {
93  assert(m_data != 0L && j>=0 && j < m_size[1]);
94  return m_data+m_size[0]*j;
95  }
96 
98  void set(int i, int j, T const &v) {
99  assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
100  m_data[i+j*m_size[0]] = v;
101  }
102 
104  template <class U>
105  void setRow(int j, U const *val) {
106  assert(m_data != 0L && j>=0 && j < m_size[1]);
107  for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
108  }
109 
111  template <class U>
112  void setColumn(int i, U const *val) {
113  assert(m_data != 0L && i>=0 && i < m_size[0]);
114  for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
115  }
116 
117 private:
120 protected:
124  T *m_data;
125 };
126 
129 {
130 public:
133 
135  void setRowPacked(int j, unsigned char const *val) {
136  assert(m_data != 0L && j>=0 && j < m_size[1]);
137  for (int i = 0, ind = j*m_size[0]; i < m_size[0]; ) {
138  unsigned char v = *(val++);
139  unsigned char mask = 0x80;
140  for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
141  m_data[ind] = ((v&mask) != 0);
142  mask = (unsigned char) (mask >> 1);
143  }
144  }
145  }
146 };
147 
149 class MWAWPictBitmap : public MWAWPict
150 {
151 public:
153  enum SubType { BW, Indexed, Color };
155  virtual Type getType() const {
156  return MWAWPict::Bitmap;
157  }
159  virtual SubType getSubType() const = 0;
160 
162  virtual bool getBinary(WPXBinaryData &res, std::string &s) const {
163  if (!valid()) return false;
164 
165  s = "image/pict";
166  createFileData(res);
167  return true;
168  }
169 
171  virtual bool valid() const {
172  return false;
173  }
174 
177  virtual int cmp(MWAWPict const &a) const {
178  int diff = MWAWPict::cmp(a);
179  if (diff) return diff;
180  MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
181 
182  // the type
183  diff = getSubType() - aPict.getSubType();
184  if (diff) return (diff < 0) ? -1 : 1;
185 
186  return 0;
187  }
188 
189 protected:
191  virtual bool createFileData(WPXBinaryData &result) const = 0;
192 
194  MWAWPictBitmap(Vec2i const &sz) {
195  setBdBox(Box2f(Vec2f(0,0), sz));
196  }
197 };
198 
201 {
202 public:
204  virtual SubType getSubType() const {
205  return BW;
206  }
207 
210  virtual int cmp(MWAWPict const &a) const {
211  int diff = MWAWPictBitmap::cmp(a);
212  if (diff) return diff;
213  MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
214 
215  diff=m_data.size().cmpY(aPict.m_data.size());
216  if (diff) return diff;
217 
218  long diffL = (long) this -(long) &aPict;
219  if (diffL) return (diffL<0) ? -1 : 1;
220 
221  return 0;
222  }
223 
225  virtual bool valid() const {
226  return m_data.ok();
227  }
228 
230  MWAWPictBitmapBW(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
231 
233  Vec2i const &size() const {
234  return m_data.size();
235  }
237  int numRows() const {
238  return m_data.numRows();
239  }
241  int numColumns() const {
242  return m_data.numColumns();
243  }
245  bool get(int i, int j) const {
246  return m_data.get(i,j);
247  }
249  bool const *getRow(int j) const {
250  return m_data.getRow(j);
251  }
253  void set(int i, int j, bool v) {
254  m_data.set(i,j, v);
255  }
257  void setRow(int j, bool const *val) {
258  m_data.setRow(j, val);
259  }
261  void setRowPacked(int j, unsigned char const *val) {
262  m_data.setRowPacked(j, val);
263  }
265  void setColumn(int i, bool const *val) {
266  m_data.setColumn(i, val);
267  }
268 
269 protected:
271  virtual bool createFileData(WPXBinaryData &result) const;
272 
275 };
276 
279 {
280 public:
282  virtual SubType getSubType() const {
283  return Indexed;
284  }
285 
288  virtual int cmp(MWAWPict const &a) const {
289  int diff = MWAWPictBitmap::cmp(a);
290  if (diff) return diff;
291  MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
292 
293  diff=m_data.size().cmpY(aPict.m_data.size());
294  if (diff) return diff;
295 
296  diff=int(m_colors.size())-int(aPict.m_colors.size());
297  if (diff) return (diff < 0) ? -1 : 1;
298 
299  long diffL = (long) this -(long) &aPict;
300  if (diffL) return (diffL<0) ? -1 : 1;
301 
302  return 0;
303  }
304 
306  virtual bool valid() const {
307  return m_data.ok();
308  }
309 
312 
314  Vec2i const &size() const {
315  return m_data.size();
316  }
318  int numRows() const {
319  return m_data.numRows();
320  }
322  int numColumns() const {
323  return m_data.numColumns();
324  }
326  int get(int i, int j) const {
327  return m_data.get(i,j);
328  }
330  int const *getRow(int j) const {
331  return m_data.getRow(j);
332  }
333 
335  void set(int i, int j, int v) {
336  m_data.set(i,j, v);
337  }
339  template <class U> void setRow(int j, U const *val) {
340  m_data.setRow(j, val);
341  }
343  template <class U> void setColumn(int i, U const *val) {
344  m_data.setColumn(i, val);
345  }
346 
348  std::vector<MWAWColor> const &getColors() const {
349  return m_colors;
350  }
352  void setColors(std::vector<MWAWColor> const &cols) {
353  m_colors = cols;
354  }
355 
356 protected:
358  virtual bool createFileData(WPXBinaryData &result) const;
359 
363  std::vector<MWAWColor> m_colors;
364 };
365 
368 {
369 public:
371  virtual SubType getSubType() const {
372  return Indexed;
373  }
374 
377  virtual int cmp(MWAWPict const &a) const {
378  int diff = MWAWPictBitmap::cmp(a);
379  if (diff) return diff;
380  MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
381 
382  diff=m_data.size().cmpY(aPict.m_data.size());
383  if (diff) return diff;
384 
385  long diffL = (long) this -(long) &aPict;
386  if (diffL) return (diffL<0) ? -1 : 1;
387 
388  return 0;
389  }
390 
392  virtual bool valid() const {
393  return m_data.ok();
394  }
395 
397  MWAWPictBitmapColor(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
398 
400  Vec2i const &size() const {
401  return m_data.size();
402  }
404  int numRows() const {
405  return m_data.numRows();
406  }
408  int numColumns() const {
409  return m_data.numColumns();
410  }
412  MWAWColor get(int i, int j) const {
413  return m_data.get(i,j);
414  }
416  MWAWColor const *getRow(int j) const {
417  return m_data.getRow(j);
418  }
419 
421  void set(int i, int j, MWAWColor const &v) {
422  m_data.set(i,j, v);
423  }
425  void setRow(int j, MWAWColor const *val) {
426  m_data.setRow(j, val);
427  }
429  void setColumn(int i, MWAWColor const *val) {
430  m_data.setColumn(i, val);
431  }
432 
433 protected:
435  virtual bool createFileData(WPXBinaryData &result) const;
436 
439 };
440 #endif
441 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:

Generated on Sat May 4 2013 20:11:08 for libmwaw by doxygen 1.8.3.1