Blame src/pngchunk_int.hpp

Packit 01d647
// ***************************************************************** -*- C++ -*-
Packit 01d647
/*
Packit 01d647
 * Copyright (C) 2004-2018 Exiv2 authors
Packit 01d647
 * This program is part of the Exiv2 distribution.
Packit 01d647
 *
Packit 01d647
 * This program is free software; you can redistribute it and/or
Packit 01d647
 * modify it under the terms of the GNU General Public License
Packit 01d647
 * as published by the Free Software Foundation; either version 2
Packit 01d647
 * of the License, or (at your option) any later version.
Packit 01d647
 *
Packit 01d647
 * This program is distributed in the hope that it will be useful,
Packit 01d647
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 01d647
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 01d647
 * GNU General Public License for more details.
Packit 01d647
 *
Packit 01d647
 * You should have received a copy of the GNU General Public License
Packit 01d647
 * along with this program; if not, write to the Free Software
Packit 01d647
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
Packit 01d647
 */
Packit 01d647
/*!
Packit 01d647
  @file    pngchunk_int.hpp
Packit 01d647
  @brief   Class PngChunk to parse PNG chunk data implemented using the following references:
Packit 01d647
           PNG iTXt chunk structure from PNG definitive guide,
Packit 01d647
           PNG tTXt and zTXt chunks structures from PNG definitive guide,
Packit 01d647
           PNG tags list by Phil Harvey
Packit 01d647
           Email communication with caulier dot gilles at gmail dot com
Packit 01d647
 */
Packit 01d647
Packit 01d647
 /*
Packit 01d647
  File:    pngchunk.cpp
Packit 01d647
 */
Packit 01d647
Packit 01d647
#ifndef PNGCHUNK_INT_HPP_
Packit 01d647
#define PNGCHUNK_INT_HPP_
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// included header files
Packit 01d647
#include "types.hpp"
Packit 01d647
#include "pngimage.hpp"
Packit 01d647
Packit 01d647
// + standard includes
Packit 01d647
#include <iosfwd>
Packit 01d647
#include <cassert>
Packit 01d647
#include <cstdarg>
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// namespace extensions
Packit 01d647
namespace Exiv2 {
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// class declarations
Packit 01d647
    class Image;
Packit 01d647
Packit 01d647
    namespace Internal {
Packit 01d647
Packit 01d647
// *****************************************************************************
Packit 01d647
// class definitions
Packit 01d647
Packit 01d647
    /*!
Packit 01d647
      @brief Stateless parser class for data in PNG chunk format. Images use this
Packit 01d647
             class to decode and encode PNG-based data.
Packit 01d647
     */
Packit 01d647
    class PngChunk {
Packit 01d647
    public:
Packit 01d647
        /*!
Packit 01d647
          @brief Text Chunk types.
Packit 01d647
        */
Packit 01d647
        enum TxtChunkType {
Packit 01d647
            tEXt_Chunk = 0,
Packit 01d647
            zTXt_Chunk = 1,
Packit 01d647
            iTXt_Chunk = 2
Packit 01d647
        };
Packit 01d647
Packit 01d647
    public:
Packit 01d647
        /*!
Packit 01d647
          @brief Decode PNG IHDR chunk data from a data buffer
Packit 01d647
                 \em data and return image size to \em outWidth and \em outHeight.
Packit 01d647
Packit 01d647
          @param data      PNG Chunk data buffer.
Packit 01d647
          @param outWidth  Integer pointer to be set to the width of the image.
Packit 01d647
          @param outHeight Integer pointer to be set to the height of the image.
Packit 01d647
        */
Packit 01d647
        static void decodeIHDRChunk(const DataBuf& data,
Packit 01d647
                                    int*           outWidth,
Packit 01d647
                                    int*           outHeight);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Decode PNG tEXt, zTXt, or iTXt chunk data from \em pImage passed by data buffer
Packit 01d647
                 \em data and extract Comment, Exif, Iptc, Xmp metadata accordingly.
Packit 01d647
Packit 01d647
          @param pImage    Pointer to the image to hold the metadata
Packit 01d647
          @param data      PNG Chunk data buffer.
Packit 01d647
          @param type      PNG Chunk TXT type.
Packit 01d647
        */
Packit 01d647
        static void decodeTXTChunk(Image*         pImage,
Packit 01d647
                                   const DataBuf& data,
Packit 01d647
                                   TxtChunkType   type);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
         @brief Decode PNG tEXt, zTXt, or iTXt chunk data from \em pImage passed by data buffer
Packit 01d647
         \em data and extract Comment, Exif, Iptc, Xmp to DataBuf
Packit 01d647
Packit 01d647
         @param data      PNG Chunk data buffer.
Packit 01d647
         @param type      PNG Chunk TXT type.
Packit 01d647
         */
Packit 01d647
        static DataBuf decodeTXTChunk(const DataBuf& data,
Packit 01d647
                                     TxtChunkType   type);
Packit 01d647
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Return PNG TXT chunk key as data buffer.
Packit 01d647
Packit 01d647
          @param data        PNG Chunk data buffer.
Packit 01d647
          @param stripHeader Set true if chunk data start with header bytes, else false (default).
Packit 01d647
        */
Packit 01d647
        static DataBuf keyTXTChunk(const DataBuf& data, bool stripHeader=false);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Return a complete PNG chunk data compressed or not as buffer.
Packit 01d647
                 Data returned is formated accordingly with metadata \em type
Packit 01d647
                 to host passed by \em metadata.
Packit 01d647
Packit 01d647
          @param metadata    metadata buffer.
Packit 01d647
          @param type        metadata type.
Packit 01d647
        */
Packit 01d647
        static std::string makeMetadataChunk(const std::string& metadata,
Packit 01d647
                                                   MetadataId   type);
Packit 01d647
Packit 01d647
    private:
Packit 01d647
        /*!
Packit 01d647
          @brief Parse PNG Text chunk to determine type and extract content.
Packit 01d647
                 Supported Chunk types are tTXt, zTXt, and iTXt.
Packit 01d647
         */
Packit 01d647
        static DataBuf parseTXTChunk(const DataBuf& data,
Packit 01d647
                                     int            keysize,
Packit 01d647
                                     TxtChunkType   type);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Parse PNG chunk contents to extract metadata container and assign it to image.
Packit 01d647
                 Supported contents are:
Packit 01d647
                    Exif raw text profile generated by ImageMagick ==> Image Exif metadata.
Packit 01d647
                    Iptc raw text profile generated by ImageMagick ==> Image Iptc metadata.
Packit 01d647
                    Xmp  raw text profile generated by ImageMagick ==> Image Xmp metadata.
Packit 01d647
                    Xmp  packet generated by Adobe                 ==> Image Xmp metadata.
Packit 01d647
                    Description string                             ==> Image Comments.
Packit 01d647
         */
Packit 01d647
        static void parseChunkContent(      Image*  pImage,
Packit 01d647
                                      const byte*   key,
Packit 01d647
                                            long    keySize,
Packit 01d647
                                      const DataBuf arr);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Return a compressed (zTXt) or uncompressed (tEXt) PNG ASCII text chunk
Packit 01d647
                 (length + chunk type + chunk data + CRC) as a string.
Packit 01d647
Packit 01d647
          @param keyword  Keyword for the PNG text chunk
Packit 01d647
          @param text     Text to be recorded in the PNG chunk.
Packit 01d647
          @param compress Flag indicating whether to compress the PNG chunk data.
Packit 01d647
Packit 01d647
          @return String containing the PNG chunk
Packit 01d647
        */
Packit 01d647
        static std::string makeAsciiTxtChunk(const std::string& keyword,
Packit 01d647
                                             const std::string& text,
Packit 01d647
                                             bool               compress);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Return a compressed or uncompressed (iTXt) PNG international text chunk
Packit 01d647
                 (length + chunk type + chunk data + CRC) as a string.
Packit 01d647
Packit 01d647
          @param keyword  Keyword for the PNG international text chunk
Packit 01d647
          @param text     Text to be recorded in the PNG chunk.
Packit 01d647
          @param compress Flag indicating whether to compress the PNG chunk data.
Packit 01d647
        */
Packit 01d647
        static std::string makeUtf8TxtChunk(const std::string& keyword,
Packit 01d647
                                            const std::string& text,
Packit 01d647
                                            bool               compress);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Wrapper around zlib to uncompress a PNG chunk content.
Packit 01d647
         */
Packit 01d647
        static void zlibUncompress(const byte*  compressedText,
Packit 01d647
                                   unsigned int compressedTextSize,
Packit 01d647
                                   DataBuf&     arr);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Wrapper around zlib to compress a PNG chunk content.
Packit 01d647
         */
Packit 01d647
        static std::string zlibCompress(const std::string& text);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Decode from ImageMagick raw text profile which host encoded Exif/Iptc/Xmp metadata byte array.
Packit 01d647
         */
Packit 01d647
        static DataBuf readRawProfile(const DataBuf& text,bool iTXt);
Packit 01d647
Packit 01d647
        /*!
Packit 01d647
          @brief Encode to ImageMagick raw text profile, which host encoded
Packit 01d647
                 Exif/IPTC/XMP metadata byte arrays.
Packit 01d647
         */
Packit 01d647
        static std::string writeRawProfile(const std::string& profileData,
Packit 01d647
                                           const char*        profileType);
Packit 01d647
Packit 01d647
        friend class Exiv2::PngImage;
Packit 01d647
Packit 01d647
    }; // class PngChunk
Packit 01d647
Packit 01d647
}}                                      // namespace Internal, Exiv2
Packit 01d647
Packit 01d647
#endif                                  // #ifndef PNGCHUNK_INT_HPP_