MWAWPictBitmap.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
00002 
00003 /* libmwaw
00004 * Version: MPL 2.0 / LGPLv2+
00005 *
00006 * The contents of this file are subject to the Mozilla Public License Version
00007 * 2.0 (the "License"); you may not use this file except in compliance with
00008 * the License or as specified alternatively below. You may obtain a copy of
00009 * the License at http://www.mozilla.org/MPL/
00010 *
00011 * Software distributed under the License is distributed on an "AS IS" basis,
00012 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013 * for the specific language governing rights and limitations under the
00014 * License.
00015 *
00016 * Major Contributor(s):
00017 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
00018 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00019 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00020 * Copyright (C) 2006, 2007 Andrew Ziem
00021 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
00022 *
00023 *
00024 * All Rights Reserved.
00025 *
00026 * For minor contributions see the git repository.
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00030 * in which case the provisions of the LGPLv2+ are applicable
00031 * instead of those above.
00032 */
00033 
00034 /* This header contains code specific to some bitmap
00035  */
00036 
00037 #ifndef MWAW_PICT_BITMAP
00038 #  define MWAW_PICT_BITMAP
00039 
00040 #include <assert.h>
00041 
00042 #include <vector>
00043 
00044 #include "libmwaw_internal.hxx"
00045 #include "MWAWPict.hxx"
00046 
00047 class WPXBinaryData;
00048 
00050 //
00051 //   Some container
00052 //
00054 
00056 template <class T> class MWAWPictBitmapContainer
00057 {
00058 public:
00060   MWAWPictBitmapContainer(Vec2i const &sz) : m_size(sz), m_data(0L) {
00061     if (m_size[0]*m_size[1] != 0) m_data = new T[size_t(m_size[0]*m_size[1])];
00062   }
00064   virtual ~MWAWPictBitmapContainer() {
00065     if (m_data) delete [] m_data;
00066   }
00067 
00069   bool ok() const {
00070     return (m_data != 0L);
00071   }
00072 
00074   int cmp(MWAWPictBitmapContainer<T> const &orig) const {
00075     int diff = m_size.cmpY(orig.m_size);
00076     if (diff) return diff;
00077     if (!m_data) return orig.m_data ? 1 : 0;
00078     if (!orig.m_data) return -1;
00079     for (int i=0; i < m_size[0]*m_size[1]; i++) {
00080       if (m_data[i] < orig.m_data[i]) return -1;
00081       if (m_data[i] > orig.m_data[i]) return 1;
00082     }
00083     return 0;
00084   }
00086   Vec2i const &size() const {
00087     return m_size;
00088   }
00090   int numRows() const {
00091     return m_size[0];
00092   }
00094   int numColumns() const {
00095     return m_size[1];
00096   }
00097 
00099   T const &get(int i, int j) const {
00100     assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
00101     return m_data[i+m_size[0]*j];
00102   }
00104   T const *getRow(int j) const {
00105     assert(m_data != 0L && j>=0 && j < m_size[1]);
00106     return m_data+m_size[0]*j;
00107   }
00108 
00110   void set(int i, int j, T const &v) {
00111     assert(m_data != 0L && i>=0 && i < m_size[0] && j>=0 && j < m_size[1]);
00112     m_data[i+j*m_size[0]] = v;
00113   }
00114 
00116   template <class U>
00117   void setRow(int j, U const *val) {
00118     assert(m_data != 0L && j>=0 && j < m_size[1]);
00119     for (int i = 0, ind=j*m_size[0]; i < m_size[0]; i++, ind++) m_data[ind] = T(val[i]);
00120   }
00121 
00123   template <class U>
00124   void setColumn(int i, U const *val) {
00125     assert(m_data != 0L && i>=0 && i < m_size[0]);
00126     for (int j = 0, ind=i; j < m_size[1]; j++, ind+=m_size[0]) m_data[ind] = T(val[i]);
00127   }
00128 
00129 private:
00130   MWAWPictBitmapContainer(MWAWPictBitmapContainer const &orig);
00131   MWAWPictBitmapContainer &operator=(MWAWPictBitmapContainer const &orig);
00132 protected:
00134   Vec2i m_size;
00136   T *m_data;
00137 };
00138 
00140 class MWAWPictBitmapContainerBool : public MWAWPictBitmapContainer<bool>
00141 {
00142 public:
00144   MWAWPictBitmapContainerBool(Vec2i const &sz) : MWAWPictBitmapContainer<bool>(sz) {}
00145 
00147   int cmp(MWAWPictBitmapContainerBool const &orig) const {
00148     int diff = m_size.cmpY(orig.m_size);
00149     if (diff) return diff;
00150     if (!m_data) return orig.m_data ? 1 : 0;
00151     if (!orig.m_data) return -1;
00152     for (int i=0; i < m_size[0]*m_size[1]; i++) {
00153       if (m_data[i] == orig.m_data[i]) continue;
00154       return m_data[i] ? 1 : -1;
00155     }
00156     return 0;
00157   }
00158 
00160   void setRowPacked(int j, unsigned char const *val) {
00161     assert(m_data != 0L && j>=0 && j < m_size[1]);
00162     for (int i = 0, ind = j*m_size[0]; i < m_size[0]; ) {
00163       unsigned char v = *(val++);
00164       unsigned char mask = 0x80;
00165       for (int p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
00166         m_data[ind] = ((v&mask) != 0);
00167         mask = (unsigned char) (mask >> 1);
00168       }
00169     }
00170   }
00171 };
00172 
00174 class MWAWPictBitmap : public MWAWPict
00175 {
00176 public:
00178   enum SubType { BW, Indexed, Color };
00180   virtual Type getType() const {
00181     return MWAWPict::Bitmap;
00182   }
00184   virtual SubType getSubType() const = 0;
00185 
00187   virtual bool getBinary(WPXBinaryData &res, std::string &s) const {
00188     if (!valid()) return false;
00189 
00190     s = "image/pict";
00191     createFileData(res);
00192     return true;
00193   }
00194 
00196   virtual bool valid() const {
00197     return false;
00198   }
00199 
00202   virtual int cmp(MWAWPict const &a) const {
00203     int diff = MWAWPict::cmp(a);
00204     if (diff) return diff;
00205     MWAWPictBitmap const &aPict = static_cast<MWAWPictBitmap const &>(a);
00206 
00207     // the type
00208     diff = getSubType() - aPict.getSubType();
00209     if (diff) return (diff < 0) ? -1 : 1;
00210 
00211     return 0;
00212   }
00213 
00214 protected:
00216   virtual bool createFileData(WPXBinaryData &result) const = 0;
00217 
00219   MWAWPictBitmap(Vec2i const &sz) {
00220     setBdBox(Box2f(Vec2f(0,0), sz));
00221   }
00222 };
00223 
00225 class MWAWPictBitmapBW : public MWAWPictBitmap
00226 {
00227 public:
00229   virtual SubType getSubType() const {
00230     return BW;
00231   }
00232 
00235   virtual int cmp(MWAWPict const &a) const {
00236     int diff = MWAWPictBitmap::cmp(a);
00237     if (diff) return diff;
00238     MWAWPictBitmapBW const &aPict = static_cast<MWAWPictBitmapBW const &>(a);
00239 
00240     return m_data.cmp(aPict.m_data);
00241   }
00242 
00244   virtual bool valid() const {
00245     return m_data.ok();
00246   }
00247 
00249   MWAWPictBitmapBW(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
00250 
00252   Vec2i const &size() const {
00253     return m_data.size();
00254   }
00256   int numRows() const {
00257     return m_data.numRows();
00258   }
00260   int numColumns() const {
00261     return m_data.numColumns();
00262   }
00264   bool get(int i, int j) const {
00265     return m_data.get(i,j);
00266   }
00268   bool const *getRow(int j) const {
00269     return m_data.getRow(j);
00270   }
00272   void set(int i, int j, bool v) {
00273     m_data.set(i,j, v);
00274   }
00276   void setRow(int j, bool const *val) {
00277     m_data.setRow(j, val);
00278   }
00280   void setRowPacked(int j, unsigned char const *val) {
00281     m_data.setRowPacked(j, val);
00282   }
00284   void setColumn(int i, bool const *val) {
00285     m_data.setColumn(i, val);
00286   }
00287 
00288 protected:
00290   virtual bool createFileData(WPXBinaryData &result) const;
00291 
00293   MWAWPictBitmapContainerBool m_data;
00294 };
00295 
00297 class MWAWPictBitmapIndexed : public MWAWPictBitmap
00298 {
00299 public:
00301   virtual SubType getSubType() const {
00302     return Indexed;
00303   }
00304 
00307   virtual int cmp(MWAWPict const &a) const {
00308     int diff = MWAWPictBitmap::cmp(a);
00309     if (diff) return diff;
00310     MWAWPictBitmapIndexed const &aPict = static_cast<MWAWPictBitmapIndexed const &>(a);
00311 
00312     diff=int(m_colors.size())-int(aPict.m_colors.size());
00313     if (diff) return (diff < 0) ? -1 : 1;
00314     for (size_t c=0; c < m_colors.size(); c++) {
00315       if (m_colors[c] < aPict.m_colors[c])
00316         return 1;
00317       if (m_colors[c] > aPict.m_colors[c])
00318         return -1;
00319     }
00320     return m_data.cmp(aPict.m_data);
00321   }
00322 
00324   virtual bool valid() const {
00325     return m_data.ok();
00326   }
00327 
00329   MWAWPictBitmapIndexed(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz), m_colors() { }
00330 
00332   Vec2i const &size() const {
00333     return m_data.size();
00334   }
00336   int numRows() const {
00337     return m_data.numRows();
00338   }
00340   int numColumns() const {
00341     return m_data.numColumns();
00342   }
00344   int get(int i, int j) const {
00345     return m_data.get(i,j);
00346   }
00348   int const *getRow(int j) const {
00349     return m_data.getRow(j);
00350   }
00351 
00353   void set(int i, int j, int v) {
00354     m_data.set(i,j, v);
00355   }
00357   template <class U> void setRow(int j, U const *val) {
00358     m_data.setRow(j, val);
00359   }
00361   template <class U> void setColumn(int i, U const *val) {
00362     m_data.setColumn(i, val);
00363   }
00364 
00366   std::vector<MWAWColor> const &getColors() const {
00367     return m_colors;
00368   }
00370   void setColors(std::vector<MWAWColor> const &cols) {
00371     m_colors = cols;
00372   }
00373 
00374 protected:
00376   virtual bool createFileData(WPXBinaryData &result) const;
00377 
00379   MWAWPictBitmapContainer<int> m_data;
00381   std::vector<MWAWColor> m_colors;
00382 };
00383 
00385 class MWAWPictBitmapColor : public MWAWPictBitmap
00386 {
00387 public:
00389   virtual SubType getSubType() const {
00390     return Indexed;
00391   }
00392 
00395   virtual int cmp(MWAWPict const &a) const {
00396     int diff = MWAWPictBitmap::cmp(a);
00397     if (diff) return diff;
00398     MWAWPictBitmapColor const &aPict = static_cast<MWAWPictBitmapColor const &>(a);
00399 
00400     return m_data.cmp(aPict.m_data);
00401   }
00402 
00404   virtual bool valid() const {
00405     return m_data.ok();
00406   }
00407 
00409   MWAWPictBitmapColor(Vec2i const &sz) : MWAWPictBitmap(sz), m_data(sz) { }
00410 
00412   Vec2i const &size() const {
00413     return m_data.size();
00414   }
00416   int numRows() const {
00417     return m_data.numRows();
00418   }
00420   int numColumns() const {
00421     return m_data.numColumns();
00422   }
00424   MWAWColor get(int i, int j) const {
00425     return m_data.get(i,j);
00426   }
00428   MWAWColor const *getRow(int j) const {
00429     return m_data.getRow(j);
00430   }
00431 
00433   void set(int i, int j, MWAWColor const &v) {
00434     m_data.set(i,j, v);
00435   }
00437   void setRow(int j, MWAWColor const *val) {
00438     m_data.setRow(j, val);
00439   }
00441   void setColumn(int i, MWAWColor const *val) {
00442     m_data.setColumn(i, val);
00443   }
00444 
00445 protected:
00447   virtual bool createFileData(WPXBinaryData &result) const;
00448 
00450   MWAWPictBitmapContainer<MWAWColor> m_data;
00451 };
00452 #endif
00453 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: