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  *      librevenge::RVNGBinaryData, 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 <librevenge/librevenge.h>
00048 
00049 #  include "libmwaw_internal.hxx"
00050 #  include "MWAWPict.hxx"
00051 
00053 class MWAWPictData : public MWAWPict
00054 {
00055 public:
00057   enum SubType { PictMac, DB3, Unknown };
00059   virtual Type getType() const
00060   {
00061     return MWAWPict::PictData;
00062   }
00064   virtual SubType getSubType() const = 0;
00065 
00067   virtual bool getBinary(librevenge::RVNGBinaryData &res, std::string &s) const
00068   {
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   {
00079     return getSubType() != Unknown;
00080   }
00081 
00083   virtual bool valid() const
00084   {
00085     return false;
00086   }
00087 
00089   bool isEmpty() const
00090   {
00091     return m_empty;
00092   }
00093 
00098   static ReadResult check(MWAWInputStreamPtr input, int size, MWAWBox2f &box)
00099   {
00100     return checkOrGet(input, size, box, 0L);
00101   }
00102 
00106   static MWAWPictData *get(MWAWInputStreamPtr input, int size)
00107   {
00108     MWAWPictData *res = 0L;
00109     MWAWBox2f box;
00110     if (checkOrGet(input, size, box, &res) == MWAW_R_BAD) return 0L;
00111     if (res) { // if the bdbox is good, we set it
00112       MWAWVec2f sz = box.size();
00113       if (sz.x()>0 && sz.y()>0) res->setBdBox(box);
00114     }
00115     return res;
00116   }
00117 
00120   virtual int cmp(MWAWPict const &a) const
00121   {
00122     int diff = MWAWPict::cmp(a);
00123     if (diff) return diff;
00124     MWAWPictData const &aPict = static_cast<MWAWPictData const &>(a);
00125 
00126     diff = (int) m_empty - (int) aPict.m_empty;
00127     if (diff) return (diff < 0) ? -1 : 1;
00128     else if (m_empty) // both empty
00129       return 0;
00130     // the type
00131     diff = getSubType() - aPict.getSubType();
00132     if (diff) return (diff < 0) ? -1 : 1;
00133 
00134     if (m_data.size() < aPict.m_data.size())
00135       return 1;
00136     if (m_data.size() > aPict.m_data.size())
00137       return -1;
00138     unsigned char const *data=m_data.getDataBuffer();
00139     unsigned char const *aData=m_data.getDataBuffer();
00140     if (!data || !aData) return 0; // must only appear if the two buffers are empty
00141     for (unsigned long c=0; c < m_data.size(); c++, data++, aData++) {
00142       if (*data < *aData) return -1;
00143       if (*data > *aData) return 1;
00144     }
00145     return 0;
00146   }
00147 
00148 protected:
00151   static bool createFileData(librevenge::RVNGBinaryData const &orig, librevenge::RVNGBinaryData &result);
00152 
00154   MWAWPictData(): m_data(), m_empty(false) { }
00155   MWAWPictData(MWAWBox2f &): m_data(), m_empty(false) { }
00156 
00162   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size,
00163                                MWAWBox2f &box, MWAWPictData **result = 0L);
00164 
00166   librevenge::RVNGBinaryData m_data;
00167 
00169   bool m_empty;
00170 };
00171 
00173 class MWAWPictDB3 : public MWAWPictData
00174 {
00175 public:
00177   virtual SubType getSubType() const
00178   {
00179     return DB3;
00180   }
00181 
00183   virtual bool valid() const
00184   {
00185     return m_data.size() != 0;
00186   }
00187 
00190   virtual int cmp(MWAWPict const &a) const
00191   {
00192     return MWAWPictData::cmp(a);
00193   }
00194 
00195 protected:
00196 
00198   MWAWPictDB3()
00199   {
00200     m_empty = false;
00201   }
00202 
00203   friend class MWAWPictData;
00209   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size, MWAWPictData **result = 0L);
00210 };
00211 
00213 class MWAWPictDUnknown : public MWAWPictData
00214 {
00215 public:
00217   virtual SubType getSubType() const
00218   {
00219     return Unknown;
00220   }
00221 
00223   virtual bool valid() const
00224   {
00225     return m_data.size() != 0;
00226   }
00227 
00230   virtual int cmp(MWAWPict const &a) const
00231   {
00232     return MWAWPictData::cmp(a);
00233   }
00234 
00235 protected:
00236 
00238   MWAWPictDUnknown()
00239   {
00240     m_empty = false;
00241   }
00242 
00243   friend class MWAWPictData;
00244 
00250   static ReadResult checkOrGet(MWAWInputStreamPtr input, int size, MWAWPictData **result = 0L);
00251 };
00252 
00253 #endif
00254 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: