Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmstafffile.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #ifndef MMSTAFFFILE_H_
00034 #define MMSTAFFFILE_H_
00035 
00036 #include "mmstools/mmsfile.h"
00037 
00038 //! TAFF Eyecatcher
00039 #define TAFF_IDENT  "TAFF"
00040 
00041 //! Types of TAFF attributes
00042 typedef enum {
00043     //! not set
00044     TAFF_ATTRTYPE_NONE,
00045     //! any characters
00046     TAFF_ATTRTYPE_STRING,
00047     //! any characters, but not empty
00048     TAFF_ATTRTYPE_NE_STRING,
00049     //! valid values: "true", "false"
00050     TAFF_ATTRTYPE_BOOL,
00051     //! valid values: "0".."255"
00052     TAFF_ATTRTYPE_UCHAR,
00053     //! valid values: "0".."100"
00054     TAFF_ATTRTYPE_UCHAR100,
00055     //! valid values: "-2147483648".."2147483647"
00056     TAFF_ATTRTYPE_INT,
00057     //! any binary data
00058     TAFF_ATTRTYPE_BINDATA,
00059     //! valid values: "true", "false", "auto"
00060     TAFF_ATTRTYPE_STATE,
00061     //! valid values: "true", "false", "linear", "log", "log_soft_start", "log_soft_end"
00062     TAFF_ATTRTYPE_SEQUENCE_MODE,
00063     //! argb values in hex format, syntax: "#rrggbbaa"
00064     TAFF_ATTRTYPE_COLOR
00065 } TAFF_ATTRTYPE;
00066 
00067 //! Describe a TAFF attribute
00068 typedef struct {
00069     //! name of attribute
00070     const char      *name;
00071     //! type of attribute
00072     TAFF_ATTRTYPE   type;
00073 } TAFF_ATTRDESC;
00074 
00075 //! Describe a TAFF tag
00076 typedef struct {
00077     //! name of tag
00078     const char      *name;
00079     //! name of special type attribute
00080     const char      *typeattr;
00081     //! value of special type attribute
00082     const char      *type;
00083     //! attributes
00084     TAFF_ATTRDESC   *attr;
00085 } TAFF_TAGTABLE;
00086 
00087 //! Describe a TAFF file format
00088 typedef struct {
00089     //! type of TAFF file
00090     char            type[32];
00091     //! type-based version
00092     unsigned int    version;
00093     //! tags
00094     TAFF_TAGTABLE   *tagtable;
00095 } TAFF_DESCRIPTION;
00096 
00097 //! Internal tag/attribute identifiers
00098 typedef enum {
00099     //! internally identifies a close tag
00100     MMSTAFF_TAGTABLE_TYPE_CLOSETAG,
00101     //! internally identifies a tag
00102     MMSTAFF_TAGTABLE_TYPE_TAG,
00103     //! internally identifies a attribute
00104     MMSTAFF_TAGTABLE_TYPE_ATTR
00105 } MMSTAFF_TAGTABLE_TYPE;
00106 
00107 //! internally identifies attributes without id (attribute names will be stored)
00108 #define MMSTAFF_ATTR_WITHOUT_ID     0xff
00109 
00110 //! Supported types of external files
00111 typedef enum {
00112     //! the external file is written in XML
00113     MMSTAFF_EXTERNAL_TYPE_XML,
00114     //! the external file is an image (currently we only support PNG images) */
00115     MMSTAFF_EXTERNAL_TYPE_IMAGE
00116 } MMSTAFF_EXTERNAL_TYPE;
00117 
00118 //! Supported pixelformats of taff images
00119 typedef enum {
00120     //! 32 bit ARGB (4 byte, alpha 8\@24, red 8\@16, green 8\@8, blue 8\@0)
00121     MMSTAFF_PF_ARGB,
00122     //! 32 bit ARGB (4 byte, inv. alpha 8\@24, red 8\@16, green 8\@8, blue 8\@0)
00123     MMSTAFF_PF_AiRGB,
00124     //! 32 bit AYUV (4 byte, alpha 8\@24, Y 8\@16, Cb 8\@8, Cr 8\@0)
00125     MMSTAFF_PF_AYUV,
00126     //! 16 bit ARGB (2 byte, alpha 4\@12, red 4\@8, green 4\@4, blue 4\@0)
00127     MMSTAFF_PF_ARGB4444,
00128     //! 16 bit RGB (2 byte, red 5\@11, green 6\@5, blue 5\@0)
00129     MMSTAFF_PF_RGB16,
00130     //! 32 bit ABGR (4 byte, alpha 8\@24, blue 8\@16, green 8\@8, red 8\@0)
00131     MMSTAFF_PF_ABGR
00132 } MMSTAFF_PF;
00133 
00134 //! convert 4 bytes from byte stream to an 32 bit integer (needed especially by ARM)
00135 #define MMSTAFF_INT32_FROM_UCHAR_STREAM(stream) \
00136      ((int)( (stream)[0] | ((stream)[1] << 8) | ((stream)[2] << 16) | ((stream)[3] << 24) ))
00137 
00138 //! A data access class for Tagged Attributes File Format (TAFF).
00139 /*!
00140 This class is written to generate an simple to parse binary presentation of
00141 high level markup languages such as XML. For now the conversion XML to TAFF
00142 or vice versa and PNG/JPEG/TIFF to TAFF is supported.
00143 The user of this class must specify a description of which tags and attributes
00144 are allowed. Further he specifies the type of an attribute. With this informations
00145 this class also checks the types and ranges of attributes during the conversion.
00146 For example the MMSGUI works completely with TAFF.
00147 \author Jens Schneider
00148 */
00149 class MMSTaffFile {
00150     private:
00151         //! taff filename
00152         string              taff_filename;
00153 
00154         //! describe the tags and attributes
00155         TAFF_DESCRIPTION    *taff_desc;
00156 
00157         //! binary presentation data
00158         unsigned char       *taff_buf;
00159 
00160         //! size of the buffer
00161         int                 taff_buf_size;
00162 
00163         //! current read position
00164         int                 taff_buf_pos;
00165 
00166         //! name of the external file for conversion
00167         string                  external_filename;
00168 
00169         //! type of the external file
00170         MMSTAFF_EXTERNAL_TYPE   external_type;
00171 
00172         //! ignore blank values during the conversion from external file
00173         bool    ignore_blank_values;
00174 
00175         //! print trace messages?
00176         bool    trace;
00177 
00178         //! print warnings?
00179         bool    print_warnings;
00180 
00181         //! convert image to this pixelformat
00182         MMSTAFF_PF  destination_pixelformat;
00183 
00184         //! should the destination pixels premultiplied?
00185         bool        destination_premultiplied;
00186 
00187         //! size of the mirror in pixel
00188         int mirror_size;
00189 
00190         //! rotate by 180 degree?
00191         bool rotate_180;
00192 
00193         //! is the TAFF buffer loaded?
00194         bool    loaded;
00195 
00196         //! has the TAFF file the correct version?
00197         bool    correct_version;
00198 
00199         //! id of the current tag
00200         int     current_tag;
00201 
00202         //! buffer postion of the current tag
00203         int     current_tag_pos;
00204 
00205         //! Internal method: Writes a buffer to a file.
00206         bool writeBuffer(MMSFile *file, void *ptr, size_t *ritems, size_t size, size_t nitems, bool *write_status = NULL);
00207 
00208         //! Internal method: Create mirror effect, rotate and convert to target pixelformat.
00209         bool postprocessImage(void **buf, int *width, int *height, int *pitch,
00210                                  int *size, bool *alphachannel);
00211 
00212         //! Internal method: Read a PNG Image.
00213         bool readPNG(const char *filename, void **buf, int *width, int *height, int *pitch,
00214                         int *size, bool *alphachannel);
00215 
00216         //! Internal method: Read a JPEG Image.
00217         bool readJPEG(const char *filename, void **buf, int *width, int *height, int *pitch,
00218                         int *size, bool *alphachannel);
00219 
00220         //! Internal method: Read a TIFF Image.
00221         bool readTIFF(const char *filename, void **buf, int *width, int *height, int *pitch,
00222                         int *size, bool *alphachannel);
00223 
00224         //! Internal method: Recursive called method for XML to TAFF conversion.
00225         bool convertXML2TAFF_throughDoc(int depth, void *void_node, MMSFile *taff_file);
00226 
00227         //! Internal method: XML to TAFF conversion.
00228         bool convertXML2TAFF();
00229 
00230         //! Internal method: IMAGE to TAFF conversion.
00231         bool convertIMAGE2TAFF();
00232 
00233         //! Internal method: Recursive called method for TAFF to XML conversion.
00234         bool convertTAFF2XML_throughDoc(int depth, int tagid, MMSFile *external_file);
00235 
00236         //! Internal method: TAFF to XML conversion.
00237         bool convertTAFF2XML();
00238 
00239     public:
00240         //! Constructor of class MMSTaffFile.
00241         /*!
00242         \param taff_filename        under this name the converted TAFF buffer is/will be stored
00243         \param taff_desc            the user of this class have to support this tag/attribute description, use NULL
00244                                     here if you use the external type MMSTAFF_EXTERNAL_TYPE_IMAGE
00245         \param external_filename    name of the external file for conversion
00246                                     set to blank if no conversion is to be done in the constructor
00247         \param external_type        type of the external file
00248         \param ignore_blank_values  ignore blank values during the conversion from external file
00249         \param trace                print trace messages?
00250         \param print_warnings       print warnings?
00251         \param force_rewrite_taff   (re-)convert from external file before loading TAFF
00252         \param auto_rewrite_taff    (re-)convert from external file if the TAFF file is older than the external file
00253         */
00254         MMSTaffFile(string taff_filename, TAFF_DESCRIPTION *taff_desc,
00255                     string external_filename = "", MMSTAFF_EXTERNAL_TYPE external_type = MMSTAFF_EXTERNAL_TYPE_XML,
00256                     bool ignore_blank_values = false, bool trace = false, bool print_warnings = false,
00257                     bool force_rewrite_taff = false, bool auto_rewrite_taff = true);
00258 
00259         //! Destructor of class MMSTaffFile.
00260         ~MMSTaffFile();
00261 
00262         //! Convert external file to TAFF.
00263         bool convertExternal2TAFF();
00264 
00265         //! Convert TAFF to external.
00266         bool convertTAFF2External();
00267 
00268         //! Read the TAFF file. This will normally done in the constructor.
00269         bool readFile();
00270 
00271         //! Is TAFF buffer filled?
00272         /*!
00273         \return true if successfully loaded
00274         */
00275         bool isLoaded();
00276 
00277         //! Has the TAFF file the correct version described in TAFF description?
00278         /*!
00279         \return true if correct version
00280         */
00281         bool checkVersion();
00282 
00283         //! Set or reset the external file and type.
00284         /*!
00285         \param external_filename    name of the external file for conversion
00286         \param external_type        type of the external file
00287         */
00288         void setExternal(string external_filename = "", MMSTAFF_EXTERNAL_TYPE external_type = MMSTAFF_EXTERNAL_TYPE_XML);
00289 
00290         //! Switch trace on/off.
00291         /*!
00292         \param trace    print trace messages?
00293         */
00294         void setTrace(bool trace);
00295 
00296         //! Switch print warnings on/off.
00297         /*!
00298         \param print_warnings   print warnings?
00299         */
00300         void setPrintWarnings(bool print_warnings);
00301 
00302         //! Set the final pixelformat for the convertion (type MMSTAFF_EXTERNAL_TYPE_IMAGE).
00303         /*!
00304         \param pixelformat      final pixelformat
00305         \param premultiplied    the image will be premultiplied during the conversion
00306         */
00307         void setDestinationPixelFormat(MMSTAFF_PF pixelformat = MMSTAFF_PF_ARGB, bool premultiplied = true);
00308 
00309         //! Set the mirror effect (type MMSTAFF_EXTERNAL_TYPE_IMAGE).
00310         /*!
00311         \param size size of the mirror effect in pixel
00312         */
00313         void setMirrorEffect(int size);
00314 
00315         //! Rotate the image by 180 degree (type MMSTAFF_EXTERNAL_TYPE_IMAGE).
00316         void rotate180(bool rotate_180);
00317 
00318         //! Get the first tag id.
00319         /*!
00320         \return id of the tag or -1 if an error has occurred
00321         */
00322         int  getFirstTag();
00323 
00324         //! Get the next tag id.
00325         /*!
00326         \param eof  if eof set to true after calling this method, the end of file is reached
00327         \return id of the tag or -1 in case of close tag or eof
00328         */
00329         int  getNextTag(bool &eof);
00330 
00331         //! Get the id of the current tag.
00332         /*!
00333         \param name     optional, with this parameter you can get the name of the current tag
00334         \return id of the current tag
00335         */
00336         int  getCurrentTag(const char **name = NULL);
00337 
00338         //! Get the name of the current tag.
00339         /*!
00340         \return name of the current tag
00341         */
00342         const char *getCurrentTagName();
00343 
00344         //! Copy the complete current tag into a new MMSTaffFile.
00345         /*!
00346         \return pointer to the new MMSTaffFile or NULL in case of errors
00347         */
00348         MMSTaffFile *copyCurrentTag();
00349 
00350         //! Determine if the current tag has attributes.
00351         /*!
00352         \return true if the current tag has at least one attribute
00353         */
00354         bool  hasAttributes();
00355 
00356         //! Get the first attribute of the current tag.
00357         /*!
00358         \param value_str    return pointer to null terminated value string or NULL if value is returned by value_int parameter
00359         \param value_int    a few types of attributes will be directly stored in binary format and will be returned by this parameter
00360         \param name         optional, with this parameter you can get the name of the attribute
00361         \return id of the attribute or -1 in case of close tag has reached
00362         */
00363         int  getFirstAttribute(char **value_str, int *value_int, char **name = NULL);
00364 
00365         //! Get the next attributes of the current tag.
00366         /*!
00367         \param value_str    return pointer to null terminated value string or NULL if value is returned by value_int parameter
00368         \param value_int    a few types of attributes will be directly stored in binary format and will be returned by this parameter
00369         \param name         optional, with this parameter you can get the name of the attribute
00370         \return id of the attribute or -1 in case of close tag has reached
00371         */
00372         int  getNextAttribute(char **value_str, int *value_int, char **name = NULL);
00373 
00374         //! Searching for an attribute id within the current tag.
00375         /*!
00376         \param id           attribute id to search for
00377         \param value_str    return pointer to null terminated value string or NULL if value is returned by value_int parameter
00378         \param value_int    a few types of attributes will be directly stored in binary format and will be returned by this parameter
00379         \return true if attribute was found
00380         */
00381         bool getAttribute(int id, char **value_str, int *value_int);
00382 
00383         //! Searching for an attribute id within the current tag.
00384         /*!
00385         \param id   attribute id to search for
00386         \return pointer to null terminated value string or NULL
00387         */
00388         char *getAttributeString(int id);
00389 
00390         //! Convert a value given as string into binary format and check ranges.
00391         /*!
00392         \param attrType             type of the attribute value string
00393         \param attrValStr           attribute value string
00394         \param attrValStr_valid     returns if attribute value string is valid
00395         \param int_val_set          returns true if the value is an integer (int)
00396         \param byte_val_set         returns true if the value is an byte (unsigned char)
00397         \param int_val              binary presentation of the value if int_val_set or byte_val_set set to true
00398         \param attrname             optional attribute name needed for error messages
00399         \param attrid               optional attribute id needed for error messages
00400         \param nodename             optional tag name needed for error messages
00401         \param nodeline             optional line needed for error messages
00402         \return true if conversion was successful
00403         \note If method returns true the attrValStr can be invalid anyway (see attrValStr_valid)
00404         */
00405         bool convertString2TaffAttributeType(TAFF_ATTRTYPE attrType, char *attrValStr, bool *attrValStr_valid,
00406                                              bool *int_val_set, bool *byte_val_set, int *int_val,
00407                                              const char *attrname = NULL, int attrid = -1, const char *nodename = 0, int nodeline = -1);
00408 
00409 };
00410 
00411 
00412 namespace MMSTAFF_IMAGE_RAWIMAGE_ATTR {
00413 
00414     #define MMSTAFF_IMAGE_RAWIMAGE_ATTR_ATTRDESC \
00415         { "width", TAFF_ATTRTYPE_INT }, \
00416         { "height", TAFF_ATTRTYPE_INT }, \
00417         { "pitch", TAFF_ATTRTYPE_INT }, \
00418         { "size", TAFF_ATTRTYPE_INT }, \
00419         { "data", TAFF_ATTRTYPE_BINDATA }, \
00420         { "pixelformat", TAFF_ATTRTYPE_INT }, \
00421         { "premultiplied", TAFF_ATTRTYPE_BOOL }, \
00422         { "mirror_size", TAFF_ATTRTYPE_INT }, \
00423         { "alphachannel", TAFF_ATTRTYPE_BOOL }, \
00424         { "rotate_180", TAFF_ATTRTYPE_BOOL }
00425 
00426     #define MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS \
00427         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_width, \
00428         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_height, \
00429         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_pitch, \
00430         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_size, \
00431         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_data, \
00432         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_pixelformat, \
00433         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_premultiplied, \
00434         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_mirror_size, \
00435         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_alphachannel, \
00436         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS_rotate_180
00437 
00438     #define MMSTAFF_IMAGE_RAWIMAGE_ATTR_INIT { \
00439         MMSTAFF_IMAGE_RAWIMAGE_ATTR_ATTRDESC, \
00440         {NULL, TAFF_ATTRTYPE_NONE} \
00441     }
00442 
00443     typedef enum {
00444         MMSTAFF_IMAGE_RAWIMAGE_ATTR_IDS
00445     } ids;
00446 }
00447 
00448 extern TAFF_DESCRIPTION mmstaff_image_taff_description;
00449 
00450 typedef enum {
00451     MMSTAFF_IMAGE_TAGTABLE_TAG_RAWIMAGE
00452 } MMSTAFF_IMAGE_TAGTABLE_TAG;
00453 
00454 
00455 #endif /*MMSTAFFFILE_H_*/

Generated by doxygen