Blame src/pngchunk_int.hpp

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