MWAWPictBitmap.hxx
Go to the documentation of this file.
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
/* libmwaw
* Version: MPL 2.0 / LGPLv2+
*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
* Copyright (C) 2006, 2007 Andrew Ziem
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
*
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
* in which case the provisions of the LGPLv2+ are applicable
* instead of those above.
*/
/* This header contains code specific to some bitmap
*/
#ifndef MWAW_PICT_BITMAP
# define MWAW_PICT_BITMAP
#include <assert.h>
#include <vector>
#include "libmwaw_internal.hxx"
#include "MWAWPict.hxx"
class WPXBinaryData;
//
// Some container
//
template <class T> class MWAWPictBitmapContainer
{
public:
MWAWPictBitmapContainer(Vec2i const &sz) : m_size(sz), m_data(0L) {
if (m_size[0]*m_size[1] != 0) m_data = new T[size_t(m_size[0]*m_size[1])];
}
virtual ~MWAWPictBitmapContainer() {
if (m_data) delete [] m_data;
}
bool ok() const {
return (m_data != 0L);
}
int cmp(MWAWPictBitmapContainer<T> const &orig) const {
int diff = m_size.cmpY(orig.m_size);
if (diff) return diff;
if (!m_data) return orig.m_data ? 1 : 0;
if (!orig.m_data) return -1;
for (int i=0; i < m_size[0]*m_size[1]; i++) {
if (m_data[i] < orig.m_data[i]) return -1;
if (m_data[i] > orig.m_data[i]) return 1;
}
return 0;
}
Vec2i const &size() const {
return m_size;
}
int numRows() const {
return m_size[0];
}
int numColumns() const {
return m_size[1];
}
T const &get(int i, int j) const {
assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
return m_data[i+m_size[0]*j];
}
T const *getRow(int j) const {
assert(m_data != 0L && j>=0 && j < m_size[1]);
return m_data+m_size[0]*j;
}
void set(int i, int j, T const &v) {
assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
m_data[i+j*m_size[0]] = v;
}
template <class U>
void setRow(int j, U const *val) {
assert(m_data != 0L && j>=0 && j < m_size[1]);
for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
}
template <class U>
void setColumn(int i, U const *val) {
assert(m_data != 0L && i>=0 && i < m_size[0]);
for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
}
private:
MWAWPictBitmapContainer(MWAWPictBitmapContainer const &orig);
MWAWPictBitmapContainer &operator=(MWAWPictBitmapContainer const &orig);
protected:
Vec2i m_size;
T *m_data;
};
class MWAWPictBitmapContainerBool : public MWAWPictBitmapContainer<bool>
{
public:
MWAWPictBitmapContainerBool(Vec2i const &sz) : MWAWPictBitmapContainer<bool>(sz) {}
int cmp(MWAWPictBitmapContainerBool const &orig) const {
int diff = m_size.cmpY(orig.m_size);
if (diff) return diff;
if (!m_data) return orig.m_data ? 1 : 0;
if (!orig.m_data) return -1;
for (int i=0; i < m_size[0]*m_size[1]; i++) {
if (m_data[i] == orig.m_data[i]) continue;
return m_data[i] ? 1 : -1;
}
return 0;
}
void setRowPacked(int j, unsigned char const *val) {
assert(m_data != 0L && j>=0 && j < m_size[1]);
for (int i = 0, ind = j*m_size[0]; i < m_size[0]; ) {
unsigned char v = *(val++);
unsigned char mask = 0x80;
for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
m_data[ind] = ((v&mask) != 0);
mask = (unsigned char) (mask >> 1);
}
}
}
};
class MWAWPictBitmap : public MWAWPict
{
public:
enum SubType { BW, Indexed, Color };
virtual Type getType() const {
return MWAWPict::Bitmap;
}
virtual SubType getSubType() const = 0;
virtual bool getBinary(WPXBinaryData &res, std::string &s) const {
if (!valid()) return false;
s = "image/pict";
createFileData(res);
return true;
}
virtual bool valid() const {
return false;
}
virtual int cmp(MWAWPict const &a) const {
int diff = MWAWPict::cmp(a);
if (diff) return diff;
MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
// the type
diff = getSubType() - aPict.getSubType();
if (diff) return (diff < 0) ? -1 : 1;
return 0;
}
protected:
virtual bool createFileData(WPXBinaryData &result) const = 0;
MWAWPictBitmap(Vec2i const &sz) {
setBdBox(Box2f(Vec2f(0,0), sz));
}
};
class MWAWPictBitmapBW : public MWAWPictBitmap
{
public:
virtual SubType getSubType() const {
return BW;
}
virtual int cmp(MWAWPict const &a) const {
int diff = MWAWPictBitmap::cmp(a);
if (diff) return diff;
MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
return m_data.cmp(aPict.m_data);
}
virtual bool valid() const {
return m_data.ok();
}
MWAWPictBitmapBW(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
Vec2i const &size() const {
return m_data.size();
}
int numRows() const {
return m_data.numRows();
}
int numColumns() const {
return m_data.numColumns();
}
bool get(int i, int j) const {
return m_data.get(i,j);
}
bool const *getRow(int j) const {
return m_data.getRow(j);
}
void set(int i, int j, bool v) {
m_data.set(i,j, v);
}
void setRow(int j, bool const *val) {
m_data.setRow(j, val);
}
void setRowPacked(int j, unsigned char const *val) {
m_data.setRowPacked(j, val);
}
void setColumn(int i, bool const *val) {
m_data.setColumn(i, val);
}
protected:
virtual bool createFileData(WPXBinaryData &result) const;
MWAWPictBitmapContainerBool m_data;
};
class MWAWPictBitmapIndexed : public MWAWPictBitmap
{
public:
virtual SubType getSubType() const {
return Indexed;
}
virtual int cmp(MWAWPict const &a) const {
int diff = MWAWPictBitmap::cmp(a);
if (diff) return diff;
MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
diff=int(m_colors.size())-int(aPict.m_colors.size());
if (diff) return (diff < 0) ? -1 : 1;
for (size_t c=0; c < m_colors.size(); c++) {
if (m_colors[c] < aPict.m_colors[c])
return 1;
if (m_colors[c] > aPict.m_colors[c])
return -1;
}
return m_data.cmp(aPict.m_data);
}
virtual bool valid() const {
return m_data.ok();
}
MWAWPictBitmapIndexed(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz), m_colors() { }
Vec2i const &size() const {
return m_data.size();
}
int numRows() const {
return m_data.numRows();
}
int numColumns() const {
return m_data.numColumns();
}
int get(int i, int j) const {
return m_data.get(i,j);
}
int const *getRow(int j) const {
return m_data.getRow(j);
}
void set(int i, int j, int v) {
m_data.set(i,j, v);
}
template <class U> void setRow(int j, U const *val) {
m_data.setRow(j, val);
}
template <class U> void setColumn(int i, U const *val) {
m_data.setColumn(i, val);
}
std::vector<MWAWColor> const &getColors() const {
return m_colors;
}
void setColors(std::vector<MWAWColor> const &cols) {
m_colors = cols;
}
protected:
virtual bool createFileData(WPXBinaryData &result) const;
MWAWPictBitmapContainer<int> m_data;
std::vector<MWAWColor> m_colors;
};
class MWAWPictBitmapColor : public MWAWPictBitmap
{
public:
virtual SubType getSubType() const {
return Indexed;
}
virtual int cmp(MWAWPict const &a) const {
int diff = MWAWPictBitmap::cmp(a);
if (diff) return diff;
MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
return m_data.cmp(aPict.m_data);
}
virtual bool valid() const {
return m_data.ok();
}
MWAWPictBitmapColor(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
Vec2i const &size() const {
return m_data.size();
}
int numRows() const {
return m_data.numRows();
}
int numColumns() const {
return m_data.numColumns();
}
MWAWColor get(int i, int j) const {
return m_data.get(i,j);
}
MWAWColor const *getRow(int j) const {
return m_data.getRow(j);
}
void set(int i, int j, MWAWColor const &v) {
m_data.set(i,j, v);
}
void setRow(int j, MWAWColor const *val) {
m_data.setRow(j, val);
}
void setColumn(int i, MWAWColor const *val) {
m_data.setColumn(i, val);
}
protected:
virtual bool createFileData(WPXBinaryData &result) const;
MWAWPictBitmapContainer<MWAWColor> m_data;
};
#endif
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
MWAWPictBitmapBW::setColumn
void setColumn(int i, bool const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:284
MWAWPictBitmap::BW
Definition: MWAWPictBitmap.hxx:178
MWAWPictBitmapContainer::numColumns
int numColumns() const
gets the number of column
Definition: MWAWPictBitmap.hxx:94
MWAWPictBitmapColor::set
void set(int i, int j, MWAWColor const &v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:433
MWAWPictBitmapContainer::m_data
T * m_data
the m_data placed by row ie. d_00, d_10, ... , d_{X-1}0, ..
Definition: MWAWPictBitmap.hxx:136
MWAWPictBitmap::cmp
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
MWAWPictBitmapColor::valid
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:404
MWAWPictBitmapColor::cmp
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
MWAWPictBitmap::Indexed
Definition: MWAWPictBitmap.hxx:178
MWAWPictBitmapBW::size
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:252
libmwaw_internal.hxx
Vec2f
Vec2< float > Vec2f
Vec2 of float.
Definition: libmwaw_internal.hxx:582
MWAWPictBitmapBW::valid
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:244
MWAWPictBitmapContainer::setColumn
void setColumn(int i, U const *val)
sets a column of m_data
Definition: MWAWPictBitmap.hxx:124
MWAWPictBitmapColor::numRows
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:416
MWAWPictBitmapBW::createFileData
virtual bool createFileData(WPXBinaryData &result) const
function which creates the result file
Definition: MWAWPictBitmap.cxx:134
MWAWPictBitmapContainer::setRow
void setRow(int j, U const *val)
sets a line of m_data
Definition: MWAWPictBitmap.hxx:117
MWAWPictBitmap::createFileData
virtual bool createFileData(WPXBinaryData &result) const =0
abstract function which creates the result file
MWAWPictBitmapContainer::set
void set(int i, int j, T const &v)
sets a cell m_data
Definition: MWAWPictBitmap.hxx:110
MWAWPictBitmapIndexed::setColumn
void setColumn(int i, U const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:361
MWAWPictBitmap::MWAWPictBitmap
MWAWPictBitmap(Vec2i const &sz)
protected constructor: use check to construct a picture
Definition: MWAWPictBitmap.hxx:219
MWAWPictBitmapColor::setRow
void setRow(int j, MWAWColor const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:437
MWAWPictBitmap::getSubType
virtual SubType getSubType() const =0
returns the picture subtype
MWAWPictBitmap::Color
Definition: MWAWPictBitmap.hxx:178
MWAWPictBitmapContainer::m_size
Vec2i m_size
the size
Definition: MWAWPictBitmap.hxx:134
MWAWPictBitmapColor::size
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:412
Box2f
Box2< float > Box2f
Box2 of float.
Definition: libmwaw_internal.hxx:877
MWAWColor
the class to store a color
Definition: libmwaw_internal.hxx:161
MWAWPict::cmp
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
MWAWPictBitmapContainer::get
T const & get(int i, int j) const
accessor of a cell m_data
Definition: MWAWPictBitmap.hxx:99
MWAWPictBitmapColor
a bitmap of Vec3u to store true color bitmap
Definition: MWAWPictBitmap.hxx:385
MWAWPictBitmapIndexed::getRow
int const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:348
MWAWPictBitmapBW::numColumns
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:260
MWAWPictBitmapBW::numRows
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:256
MWAWPictBitmapContainer
a template class to store a 2D array of m_data
Definition: MWAWPictBitmap.hxx:56
MWAWPictBitmapIndexed::valid
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:324
MWAWPictBitmapColor::createFileData
virtual bool createFileData(WPXBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:143
MWAWPictBitmapBW::setRow
void setRow(int j, bool const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:276
MWAWPictBitmapContainer::numRows
int numRows() const
gets the number of row
Definition: MWAWPictBitmap.hxx:90
MWAWPictBitmapIndexed::getColors
std::vector< MWAWColor > const & getColors() const
returns the array of indexed colors
Definition: MWAWPictBitmap.hxx:366
MWAWPictBitmap::getBinary
virtual bool getBinary(WPXBinaryData &res, std::string &s) const
returns the final WPXBinary data
Definition: MWAWPictBitmap.hxx:187
MWAWPict.hxx
MWAWPictBitmapBW
a bitmap of bool to store black-white bitmap
Definition: MWAWPictBitmap.hxx:225
MWAWPictBitmapBW::getRow
bool const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:268
MWAWPictBitmapIndexed::set
void set(int i, int j, int v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:353
MWAWPictBitmap::SubType
SubType
the picture subtype: blackwhite, indexed, color
Definition: MWAWPictBitmap.hxx:178
MWAWPictBitmapContainerBool
a bool container with a function to put packed row
Definition: MWAWPictBitmap.hxx:140
MWAWPictBitmapBW::set
void set(int i, int j, bool v)
sets a cell contents
Definition: MWAWPictBitmap.hxx:272
MWAWPictBitmapIndexed::MWAWPictBitmapIndexed
MWAWPictBitmapIndexed(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:329
MWAWPictBitmapIndexed
a bitmap of int to store indexed bitmap
Definition: MWAWPictBitmap.hxx:297
MWAWPictBitmapIndexed::setColors
void setColors(std::vector< MWAWColor > const &cols)
sets the array of indexed colors
Definition: MWAWPictBitmap.hxx:370
MWAWPictBitmapIndexed::m_data
MWAWPictBitmapContainer< int > m_data
the m_data
Definition: MWAWPictBitmap.hxx:379
MWAWPict::setBdBox
void setBdBox(Box2f const &box)
sets the bdbox of the picture
Definition: MWAWPict.hxx:87
MWAWPict::Type
Type
the different picture types:
Definition: MWAWPict.hxx:67
MWAWPictBitmapContainerBool::cmp
int cmp(MWAWPictBitmapContainerBool const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:147
MWAWPictBitmapIndexed::createFileData
virtual bool createFileData(WPXBinaryData &result) const
the function which creates the result file
Definition: MWAWPictBitmap.cxx:152
Vec2::cmpY
int cmpY(Vec2< T > const &p) const
a comparison function: which first compares y then x
Definition: libmwaw_internal.hxx:528
MWAWPictBitmapContainer::operator=
MWAWPictBitmapContainer & operator=(MWAWPictBitmapContainer const &orig)
MWAWPictBitmapContainerBool::setRowPacked
void setRowPacked(int j, unsigned char const *val)
allows to use packed m_data
Definition: MWAWPictBitmap.hxx:160
MWAWPictBitmapColor::getRow
MWAWColor const * getRow(int j) const
returns the cells content of a row
Definition: MWAWPictBitmap.hxx:428
MWAWPictBitmapIndexed::setRow
void setRow(int j, U const *val)
sets all cell contents of a row
Definition: MWAWPictBitmap.hxx:357
MWAWPictBitmapContainerBool::MWAWPictBitmapContainerBool
MWAWPictBitmapContainerBool(Vec2i const &sz)
constructor
Definition: MWAWPictBitmap.hxx:144
MWAWPictBitmapContainer::~MWAWPictBitmapContainer
virtual ~MWAWPictBitmapContainer()
destructor
Definition: MWAWPictBitmap.hxx:64
MWAWPictBitmap
Generic class used to construct bitmap.
Definition: MWAWPictBitmap.hxx:174
MWAWPictBitmapColor::numColumns
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:420
MWAWPictBitmapContainer::cmp
int cmp(MWAWPictBitmapContainer< T > const &orig) const
a comparison operator
Definition: MWAWPictBitmap.hxx:74
MWAWPictBitmapContainer::ok
bool ok() const
returns ok, if the m_data is allocated
Definition: MWAWPictBitmap.hxx:69
MWAWPictBitmapIndexed::numRows
int numRows() const
the number of rows
Definition: MWAWPictBitmap.hxx:336
MWAWPictBitmapColor::MWAWPictBitmapColor
MWAWPictBitmapColor(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:409
MWAWPictBitmapContainer::getRow
T const * getRow(int j) const
accessor of a row m_data
Definition: MWAWPictBitmap.hxx:104
MWAWPictBitmapColor::setColumn
void setColumn(int i, MWAWColor const *val)
sets all cell contents of a column
Definition: MWAWPictBitmap.hxx:441
MWAWPictBitmap::valid
virtual bool valid() const
returns true if the picture is valid
Definition: MWAWPictBitmap.hxx:196
MWAWPictBitmapBW::cmp
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
MWAWPictBitmapBW::getSubType
virtual SubType getSubType() const
returns the picture subtype
Definition: MWAWPictBitmap.hxx:229
MWAWPictBitmapContainer::MWAWPictBitmapContainer
MWAWPictBitmapContainer(Vec2i const &sz)
constructor given size
Definition: MWAWPictBitmap.hxx:60
Vec2< int >
MWAWPict::Bitmap
Definition: MWAWPict.hxx:67
MWAWPictBitmapIndexed::m_colors
std::vector< MWAWColor > m_colors
the colors
Definition: MWAWPictBitmap.hxx:381
MWAWPictBitmapIndexed::getSubType
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:301
MWAWPictBitmap::getType
virtual Type getType() const
returns the picture type
Definition: MWAWPictBitmap.hxx:180
MWAWPictBitmapColor::getSubType
virtual SubType getSubType() const
return the picture subtype
Definition: MWAWPictBitmap.hxx:389
MWAWPictBitmapBW::setRowPacked
void setRowPacked(int j, unsigned char const *val)
sets all cell contents of a row given packed m_data
Definition: MWAWPictBitmap.hxx:280
MWAWPictBitmapIndexed::numColumns
int numColumns() const
the number of columns
Definition: MWAWPictBitmap.hxx:340
MWAWPictBitmapColor::m_data
MWAWPictBitmapContainer< MWAWColor > m_data
the data
Definition: MWAWPictBitmap.hxx:450
MWAWPictBitmapContainer::size
Vec2i const & size() const
return the array size
Definition: MWAWPictBitmap.hxx:86
MWAWPictBitmapBW::MWAWPictBitmapBW
MWAWPictBitmapBW(Vec2i const &sz)
the constructor
Definition: MWAWPictBitmap.hxx:249
MWAWPict
Generic function used to define/store a picture.
Definition: MWAWPict.hxx:53
MWAWPictBitmapIndexed::size
Vec2i const & size() const
the picture size
Definition: MWAWPictBitmap.hxx:332
MWAWPictBitmapBW::m_data
MWAWPictBitmapContainerBool m_data
the data
Definition: MWAWPictBitmap.hxx:293
MWAWPictBitmapIndexed::cmp
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