MWAWPictData.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 a pict which can be stored in a
00035  *      WPXBinaryData, this includes :
00036  *         - the mac Pict format (in MWAWPictMac)
00037  *         - some old data names db3
00038  *         - some potential short data file
00039  */
00040 
00041 #ifndef MWAW_PICT_DATA
00042 #  define MWAW_PICT_DATA
00043 
00044 #  include <assert.h>
00045 #  include <ostream>
00046 
00047 #  include <libwpd/libwpd.h>
00048 
00049 #  include "libmwaw_internal.hxx"
00050 #  include "MWAWPict.hxx"
00051 
00052 class WPXBinaryData;
00053 
00055 class MWAWPictData : public MWAWPict
00056 {
00057 public:
00059   enum SubType { PictMac, DB3, Unknown };
00061   virtual Type getType() const {
00062     return MWAWPict::PictData;
00063   }
00065   virtual SubType getSubType() const = 0;
00066 
00068   virtual bool getBinary(WPXBinaryData &res, std::string &s) const {
00069     if (!valid() || isEmpty()) return false;
00070 
00071     s = "image/pict";
00072     createFileData(m_data, res);
00073     return true;
00074   }
00075 
00077   virtual bool sure() const {
00078     return getSubType() != Unknown;
00079   }
00080 
00082   virtual bool valid() const {
00083     return false;
00084   }
00085 
00087   bool isEmpty() const {
00088     return m_empty;
00089   }
00090 
00095   static ReadResult check(MWAWInputStreamPtr input, int size, Box2f &box) {
00096     return checkOrGet(input, size, box, 0L);
00097   }
00098 
00102   static MWAWPictData *get(MWAWInputStreamPtr input, int size) {
00103     MWAWPictData *res = 0L;
00104     Box2f box;
00105     if (checkOrGet(input, size, box, &res) == MWAW_R_BAD) return 0L;
00106     if (res) { // if the bdbox is good, we set it
00107       Vec2f sz = box.size();
00108       if (sz.x()>0 && sz.y()>0) res->setBdBox(box);
00109     }
00110     return res;
00111   }
00112 
00115   virtual int cmp(MWAWPict const &a) const {
00116     int diff = MWAWPict::cmp(a);
00117     if (diff) return diff;
00118     MWAWPictData const &aPict = static_cast<MWAWPictData const &>(a);
00119 
00120     diff = (int) m_empty - (int) aPict.m_empty;
00121     if (diff) return (diff < 0) ? -1 : 1;
00122     // the type
00123     diff = getSubType() - aPict.getSubType();
00124     if (diff) return (diff < 0) ? -1 : 1;
00125 
00126     if (m_data.size() < aPict.m_data.size())
00127       return 1;
00128     if (m_data.size() > aPict.m_data.size())
00129       return -1;
00130     unsigned char const *data=m_data.getDataBuffer();
00131     unsigned char const *aData=m_data.getDataBuffer();
00132     for (unsigned long c=0; c < m_data.size(); c++, data++, aData++) {
00133       if (*data < *aData) return -1;
00134       if (*data > *aData) return 1;
00135     }
00136     return 0;
00137   }
00138 
00139 protected:
00142   static bool createFileData(WPXBinaryData const &orig, WPXBinaryData &result);
00143 
00145   MWAWPictData(): m_data(), m_empty(false) { }
00146   MWAWPictData(Box2f &): m_data(), m_empty(false) { }
00147 
00153   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size,
00154                                Box2f &box, MWAWPictData **result = 0L);
00155 
00157   WPXBinaryData m_data;
00158 
00160   bool m_empty;
00161 };
00162 
00164 class MWAWPictDB3 : public MWAWPictData
00165 {
00166 public:
00168   virtual SubType getSubType() const {
00169     return DB3;
00170   }
00171 
00173   virtual bool valid() const {
00174     return m_data.size() != 0;
00175   }
00176 
00179   virtual int cmp(MWAWPict const &a) const {
00180     return MWAWPictData::cmp(a);
00181   }
00182 
00183 protected:
00184 
00186   MWAWPictDB3() {
00187     m_empty = false;
00188   }
00189 
00190   friend class MWAWPictData;
00196   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size, MWAWPictData **result = 0L);
00197 };
00198 
00200 class MWAWPictDUnknown : public MWAWPictData
00201 {
00202 public:
00204   virtual SubType getSubType() const {
00205     return Unknown;
00206   }
00207 
00209   virtual bool valid() const {
00210     return m_data.size() != 0;
00211   }
00212 
00215   virtual int cmp(MWAWPict const &a) const {
00216     return MWAWPictData::cmp(a);
00217   }
00218 
00219 protected:
00220 
00222   MWAWPictDUnknown() {
00223     m_empty = false;
00224   }
00225 
00226   friend class MWAWPictData;
00227 
00233   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size, MWAWPictData **result = 0L);
00234 };
00235 
00236 #endif
00237 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: