Debian Codegraph @

src/lib/MWAWPictBitmap.hxx

raw
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 ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
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
47 ////////////////////////////////////////////////////////////
48 //
49 // Some container
50 //
51 ////////////////////////////////////////////////////////////
52
53 /** \brief a template class to store a 2D array of m_data */
54 template <class T> class MWAWPictBitmapContainer
55 {
56 public:
57 //! constructor given size
58 explicit MWAWPictBitmapContainer(MWAWVec2i const &sz)
59 : m_size(sz)
60 , m_data(nullptr)
61 {
62 if (m_size[0]*m_size[1] == 0) return;
63 m_data = new T[size_t(m_size[0]*m_size[1])];
64 std::uninitialized_fill_n(m_data, m_size[0] * m_size[1], T());
65 }
66 //! destructor
67 virtual ~MWAWPictBitmapContainer()
68 {
69 if (m_data) delete [] m_data;
70 }
71
72 //! returns ok, if the m_data is allocated
73 bool ok() const
74 {
75 return (m_data != nullptr);
76 }
77
78 //! a comparison operator
79 int cmp(MWAWPictBitmapContainer<T> const &orig) const
80 {
81 int diff = m_size.cmpY(orig.m_size);
82 if (diff) return diff;
83 if (!m_data) return orig.m_data ? 1 : 0;
84 if (!orig.m_data) return -1;
85 for (int i=0; i < m_size[0]*m_size[1]; i++) {
86 if (m_data[i] < orig.m_data[i]) return -1;
87 if (m_data[i] > orig.m_data[i]) return 1;
88 }
89 return 0;
90 }
91 //! return the array size
92 MWAWVec2i const &size() const
93 {
94 return m_size;
95 }
96 //! gets the number of row
97 int numRows() const
98 {
99 return m_size[0];
100 }
101 //! gets the number of column
102 int numColumns() const
103 {
104 return m_size[1];
105 }
106
107 //! accessor of a cell m_data
108 T const &get(int i, int j) const
109 {
110 if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1])
111 throw libmwaw::GenericException();
112 return m_data[i+m_size[0]*j];
113 }
114 //! accessor of a row m_data
115 T const *getRow(int j) const
116 {
117 if (m_data == nullptr || j<0 || j >= m_size[1])
118 throw libmwaw::GenericException();
119 return m_data+m_size[0]*j;
120 }
121
122 //! sets a cell m_data
123 void set(int i, int j, T const &v)
124 {
125 if (m_data == nullptr || i<0 || i >= m_size[0] || j<0 || j >= m_size[1]) {
126 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::set: call with bad coordinate %d %d\n", i, j));
127 return;
128 }
129 m_data[i+j*m_size[0]] = v;
130 }
131
132 //! sets a line of m_data
133 template <class U>
134 void setRow(int j, U const *val)
135 {
136 if (m_data == nullptr || j<0 || j >= m_size[1]) {
137 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setRow: call with bad coordinate %d\n", j));
138 return;
139 }
140 for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
141 }
142
143 //! sets a column of m_data
144 template <class U>
145 void setColumn(int i, U const *val)
146 {
147 if (m_data == nullptr || i<0 || i >= m_size[0]) {
148 MWAW_DEBUG_MSG(("MWAWPictBitmapContainer::setColumn: call with bad coordinate %d\n", i));
149 return;
150 }
151 for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
152 }
153
154 private:
155 MWAWPictBitmapContainer(MWAWPictBitmapContainer const &orig) = delete;
156 MWAWPictBitmapContainer &operator=(MWAWPictBitmapContainer const &orig) = delete;
157 protected:
158 //! the size
159 MWAWVec2i m_size;
160 //! the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
161 T *m_data;
162 };
163
164 //! a bool container with a function to put packed row
165 class MWAWPictBitmapContainerBool final : public MWAWPictBitmapContainer<bool>
166 {
167 public:
168 //! constructor
169 explicit MWAWPictBitmapContainerBool(MWAWVec2i const &sz)
170 : MWAWPictBitmapContainer<bool>(sz)
171 {
172 }
173 //! destructor
174 ~MWAWPictBitmapContainerBool() final;
175 //! a comparison operator
176 int cmp(MWAWPictBitmapContainerBool const &orig) const
177 {
178 int diff = m_size.cmpY(orig.m_size);
179 if (diff) return diff;
180 if (!m_data) return orig.m_data ? 1 : 0;
181 if (!orig.m_data) return -1;
182 for (int i=0; i < m_size[0]*m_size[1]; i++) {
183 if (m_data[i] == orig.m_data[i]) continue;
184 return m_data[i] ? 1 : -1;
185 }
186 return 0;
187 }
188
189 //! allows to use packed m_data
190 void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
191 {
192 if (m_data == nullptr || j<0 || j >= m_size[1] || val >= end) {
193 MWAW_DEBUG_MSG(("MWAWPictBitmapContainerBool::setRowPacked: call with bad coordinate %d\n", j));
194 return;
195 }
196 for (int i = 0, ind = j*m_size[0]; i < m_size[0];) {
197 unsigned char v = (val < end) ? *(val++) : 0;
198 unsigned char mask = 0x80;
199 for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
200 m_data[ind] = ((v&mask) != 0);
201 mask = static_cast<unsigned char>(mask >> 1);
202 }
203 }
204 }
205 };
206
207 //! Generic class used to construct bitmap
208 class MWAWPictBitmap : public MWAWPict
209 {
210 public:
211 //! destructor
212 ~MWAWPictBitmap() override;
213
214 //! the picture subtype: blackwhite, indexed, color
215 enum SubType { BW, Indexed, Color };
216 //! returns the picture type
217 Type getType() const override
218 {
219 return MWAWPict::Bitmap;
220 }
221 //! returns the picture subtype
222 virtual SubType getSubType() const = 0;
223
224 //! returns the final picture
225 bool getBinary(MWAWEmbeddedObject &picture) const override
226 {
227 if (!valid()) return false;
228
229 librevenge::RVNGBinaryData data;
230 createFileData(data);
231 picture=MWAWEmbeddedObject(data, "image/pict");
232 return true;
233 }
234
235 //! returns true if the picture is valid
236 virtual bool valid() const
237 {
238 return false;
239 }
240
241 //! returns the average color
242 virtual MWAWColor getAverageColor() const = 0;
243 /** a virtual function used to obtain a strict order,
244 must be redefined in the subs class */
245 int cmp(MWAWPict const &a) const override
246 {
247 int diff = MWAWPict::cmp(a);
248 if (diff) return diff;
249 auto const &aPict = static_cast<MWAWPictBitmap const &>(a);
250
251 // the type
252 diff = getSubType() - aPict.getSubType();
253 if (diff) return (diff < 0) ? -1 : 1;
254
255 return 0;
256 }
257
258 protected:
259 //! abstract function which creates the result file
260 virtual bool createFileData(librevenge::RVNGBinaryData &result) const = 0;
261
262 //! protected constructor: use check to construct a picture
263 explicit MWAWPictBitmap(MWAWVec2i const &sz)
264 {
265 setBdBox(MWAWBox2f(MWAWVec2f(0,0), MWAWVec2f(sz)));
266 }
267 };
268
269 /** a bitmap of bool to store black-white bitmap */
270 class MWAWPictBitmapBW final : public MWAWPictBitmap
271 {
272 public:
273 //! returns the picture subtype
274 SubType getSubType() const final
275 {
276 return BW;
277 }
278
279 /** a virtual function used to obtain a strict order,
280 must be redefined in the subs class */
281 int cmp(MWAWPict const &a) const final
282 {
283 int diff = MWAWPictBitmap::cmp(a);
284 if (diff) return diff;
285 auto const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
286
287 return m_data.cmp(aPict.m_data);
288 }
289
290 //! returns true if the picture is valid
291 bool valid() const final
292 {
293 return m_data.ok();
294 }
295
296 //! the constructor
297 explicit MWAWPictBitmapBW(MWAWVec2i const &sz)
298 : MWAWPictBitmap(sz)
299 , m_data(sz)
300 {
301 }
302
303 //! the picture size
304 MWAWVec2i const &size() const
305 {
306 return m_data.size();
307 }
308 //! the number of rows
309 int numRows() const
310 {
311 return m_data.numRows();
312 }
313 //! the number of columns
314 int numColumns() const
315 {
316 return m_data.numColumns();
317 }
318 //! returns a cell content
319 bool get(int i, int j) const
320 {
321 return m_data.get(i,j);
322 }
323 //! returns the cells content of a row
324 bool const *getRow(int j) const
325 {
326 return m_data.getRow(j);
327 }
328 //! sets a cell contents
329 void set(int i, int j, bool v)
330 {
331 m_data.set(i,j, v);
332 }
333 //! sets all cell contents of a row
334 void setRow(int j, bool const *val)
335 {
336 m_data.setRow(j, val);
337 }
338 //! sets all cell contents of a row given packed m_data
339 void setRowPacked(int j, unsigned char const *val, unsigned char const *end)
340 {
341 m_data.setRowPacked(j, val, end);
342 }
343 //! sets all cell contents of a column
344 void setColumn(int i, bool const *val)
345 {
346 m_data.setColumn(i, val);
347 }
348 //! returns the average color
349 MWAWColor getAverageColor() const final;
350
351 protected:
352 //! function which creates the result file
353 bool createFileData(librevenge::RVNGBinaryData &result) const final;
354
355 //! the data
356 MWAWPictBitmapContainerBool m_data;
357 };
358
359 /** a bitmap of int to store indexed bitmap */
360 class MWAWPictBitmapIndexed final : public MWAWPictBitmap
361 {
362 public:
363 //! return the picture subtype
364 SubType getSubType() const final
365 {
366 return Indexed;
367 }
368
369 /** a virtual function used to obtain a strict order,
370 must be redefined in the subs class */
371 int cmp(MWAWPict const &a) const final
372 {
373 int diff = MWAWPictBitmap::cmp(a);
374 if (diff) return diff;
375 auto const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
376
377 diff=int(m_colors.size())-int(aPict.m_colors.size());
378 if (diff) return (diff < 0) ? -1 : 1;
379 for (size_t c=0; c < m_colors.size(); c++) {
380 if (m_colors[c] < aPict.m_colors[c])
381 return 1;
382 if (m_colors[c] > aPict.m_colors[c])
383 return -1;
384 }
385 return m_data.cmp(aPict.m_data);
386 }
387
388 //! returns true if the picture is valid
389 bool valid() const final
390 {
391 return m_data.ok();
392 }
393 //! returns the average color
394 MWAWColor getAverageColor() const final;
395
396 //! the constructor
397 explicit MWAWPictBitmapIndexed(MWAWVec2i const &sz)
398 : MWAWPictBitmap(sz)
399 , m_data(sz)
400 , m_colors()
401 {
402 }
403
404 //! the picture size
405 MWAWVec2i const &size() const
406 {
407 return m_data.size();
408 }
409 //! the number of rows
410 int numRows() const
411 {
412 return m_data.numRows();
413 }
414 //! the number of columns
415 int numColumns() const
416 {
417 return m_data.numColumns();
418 }
419 //! returns a cell content
420 int get(int i, int j) const
421 {
422 return m_data.get(i,j);
423 }
424 //! returns the cells content of a row
425 int const *getRow(int j) const
426 {
427 return m_data.getRow(j);
428 }
429
430 //! sets a cell contents
431 void set(int i, int j, int v)
432 {
433 m_data.set(i,j, v);
434 }
435 //! sets all cell contents of a row
436 template <class U> void setRow(int j, U const *val)
437 {
438 m_data.setRow(j, val);
439 }
440 //! sets all cell contents of a column
441 template <class U> void setColumn(int i, U const *val)
442 {
443 m_data.setColumn(i, val);
444 }
445
446 //! returns the array of indexed colors
447 std::vector<MWAWColor> const &getColors() const
448 {
449 return m_colors;
450 }
451 //! sets the array of indexed colors
452 void setColors(std::vector<MWAWColor> const &cols)
453 {
454 m_colors = cols;
455 }
456
457 protected:
458 //! the function which creates the result file
459 bool createFileData(librevenge::RVNGBinaryData &result) const final;
460
461 //! the m_data
462 MWAWPictBitmapContainer<int> m_data;
463 //! the colors
464 std::vector<MWAWColor> m_colors;
465 };
466
467 /** a bitmap of MWAWColor to store true color bitmap
468
469 \note: this class is actually the only class which can create
470 bitmap with transparency (by creating a BMP), but as
471 LibreOffice/OpenOffice seem to ignore the alpha channel when
472 importing BMP pictures...
473 */
474 class MWAWPictBitmapColor final : public MWAWPictBitmap
475 {
476 public:
477 //! return the picture subtype
478 SubType getSubType() const final
479 {
480 return Color;
481 }
482
483 /** a virtual function used to obtain a strict order,
484 must be redefined in the subs class */
485 int cmp(MWAWPict const &a) const final
486 {
487 int diff = MWAWPictBitmap::cmp(a);
488 if (diff) return diff;
489 auto const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
490
491 return m_data.cmp(aPict.m_data);
492 }
493
494 //! returns true if the picture is valid
495 bool valid() const final
496 {
497 return m_data.ok();
498 }
499 //! returns the average color
500 MWAWColor getAverageColor() const final;
501
502 //! the constructor
503 explicit MWAWPictBitmapColor(MWAWVec2i const &sz, bool useAlphaChannel=false)
504 : MWAWPictBitmap(sz)
505 , m_data(sz)
506 , m_hasAlpha(useAlphaChannel)
507 {
508 }
509
510 //! the picture size
511 MWAWVec2i const &size() const
512 {
513 return m_data.size();
514 }
515 //! the number of rows
516 int numRows() const
517 {
518 return m_data.numRows();
519 }
520 //! the number of columns
521 int numColumns() const
522 {
523 return m_data.numColumns();
524 }
525 //! returns a cell content
526 MWAWColor get(int i, int j) const
527 {
528 return m_data.get(i,j);
529 }
530 //! returns the cells content of a row
531 MWAWColor const *getRow(int j) const
532 {
533 return m_data.getRow(j);
534 }
535
536 //! sets a cell contents
537 void set(int i, int j, MWAWColor const &v)
538 {
539 m_data.set(i,j, v);
540 }
541 //! sets all cell contents of a row
542 void setRow(int j, MWAWColor const *val)
543 {
544 m_data.setRow(j, val);
545 }
546 //! sets all cell contents of a column
547 void setColumn(int i, MWAWColor const *val)
548 {
549 m_data.setColumn(i, val);
550 }
551
552 protected:
553 //! the function which creates the result file
554 bool createFileData(librevenge::RVNGBinaryData &result) const final;
555
556 //! the data
557 MWAWPictBitmapContainer<MWAWColor> m_data;
558
559 //! true if the bitmap has alpha color
560 bool m_hasAlpha;
561 };
562 #endif
563 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: