Blame src/gifimage.cpp

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:      gifimage.cpp
Packit Service 21b5d1
  Author(s): Marco Piovanelli, Ovolab (marco)
Packit Service 21b5d1
  History:   26-Feb-2007, marco: created
Packit Service 21b5d1
 */
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// included header files
Packit Service 21b5d1
#include "config.h"
Packit Service 21b5d1
Packit Service 21b5d1
#include "gifimage.hpp"
Packit Service 21b5d1
#include "image.hpp"
Packit Service 21b5d1
#include "basicio.hpp"
Packit Service 21b5d1
#include "error.hpp"
Packit Service 21b5d1
#include "futils.hpp"
Packit Service 21b5d1
Packit Service 21b5d1
// + standard includes
Packit Service 21b5d1
#include <string>
Packit Service 21b5d1
#include <cstring>
Packit Service 21b5d1
#include <iostream>
Packit Service 21b5d1
Packit Service 21b5d1
// *****************************************************************************
Packit Service 21b5d1
// class member definitions
Packit Service 21b5d1
namespace Exiv2 {
Packit Service 21b5d1
Packit Service 21b5d1
    GifImage::GifImage(BasicIo::AutoPtr io)
Packit Service 21b5d1
        : Image(ImageType::gif, mdNone, io)
Packit Service 21b5d1
    {
Packit Service 21b5d1
    } // GifImage::GifImage
Packit Service 21b5d1
Packit Service 21b5d1
    std::string GifImage::mimeType() const
Packit Service 21b5d1
    {
Packit Service 21b5d1
        return "image/gif";
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void GifImage::setExifData(const ExifData& /*exifData*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "Exif metadata", "GIF"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void GifImage::setIptcData(const IptcData& /*iptcData*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "IPTC metadata", "GIF"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void GifImage::setComment(const std::string& /*comment*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // not supported
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "Image comment", "GIF"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void GifImage::readMetadata()
Packit Service 21b5d1
    {
Packit Service 21b5d1
#ifdef EXIV2_DEBUG_MESSAGES
Packit Service 21b5d1
        std::cerr << "Exiv2::GifImage::readMetadata: Reading GIF file " << io_->path() << "\n";
Packit Service 21b5d1
#endif
Packit Service 21b5d1
        if (io_->open() != 0)
Packit Service 21b5d1
        {
Packit Service 21b5d1
            throw Error(kerDataSourceOpenFailed, io_->path(), strError());
Packit Service 21b5d1
        }
Packit Service 21b5d1
        IoCloser closer(*io_);
Packit Service 21b5d1
        // Ensure that this is the correct image type
Packit Service 21b5d1
        if (!isGifType(*io_, true))
Packit Service 21b5d1
        {
Packit Service 21b5d1
            if (io_->error() || io_->eof()) throw Error(kerFailedToReadImageData);
Packit Service 21b5d1
            throw Error(kerNotAnImage, "GIF");
Packit Service 21b5d1
        }
Packit Service 21b5d1
        clearMetadata();
Packit Service 21b5d1
Packit Service 21b5d1
        byte buf[4];
Packit Service 21b5d1
        if (io_->read(buf, sizeof(buf)) == sizeof(buf))
Packit Service 21b5d1
        {
Packit Service 21b5d1
            pixelWidth_ = getShort(buf, littleEndian);
Packit Service 21b5d1
            pixelHeight_ = getShort(buf + 2, littleEndian);
Packit Service 21b5d1
        }
Packit Service 21b5d1
    } // GifImage::readMetadata
Packit Service 21b5d1
Packit Service 21b5d1
    void GifImage::writeMetadata()
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerWritingImageFormatUnsupported, "GIF"));
Packit Service 21b5d1
    } // GifImage::writeMetadata
Packit Service 21b5d1
Packit Service 21b5d1
    // *************************************************************************
Packit Service 21b5d1
    // free functions
Packit Service 21b5d1
    Image::AutoPtr newGifInstance(BasicIo::AutoPtr io, bool /*create*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        Image::AutoPtr image(new GifImage(io));
Packit Service 21b5d1
        if (!image->good())
Packit Service 21b5d1
        {
Packit Service 21b5d1
            image.reset();
Packit Service 21b5d1
        }
Packit Service 21b5d1
        return image;
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    bool isGifType(BasicIo& iIo, bool advance)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        const int32_t len = 6;
Packit Service 21b5d1
        const unsigned char Gif87aId[8] = { 'G', 'I', 'F', '8', '7', 'a' };
Packit Service 21b5d1
        const unsigned char Gif89aId[8] = { 'G', 'I', 'F', '8', '9', 'a' };
Packit Service 21b5d1
        byte buf[len];
Packit Service 21b5d1
        iIo.read(buf, len);
Packit Service 21b5d1
        if (iIo.error() || iIo.eof())
Packit Service 21b5d1
        {
Packit Service 21b5d1
            return false;
Packit Service 21b5d1
        }
Packit Service 21b5d1
        bool matched =    (memcmp(buf, Gif87aId, len) == 0)
Packit Service 21b5d1
                       || (memcmp(buf, Gif89aId, len) == 0);
Packit Service 21b5d1
        if (!advance || !matched)
Packit Service 21b5d1
        {
Packit Service 21b5d1
            iIo.seek(-len, BasicIo::cur);
Packit Service 21b5d1
        }
Packit Service 21b5d1
        return matched;
Packit Service 21b5d1
    }
Packit Service 21b5d1
}                                       // namespace Exiv2