Blame src/tgaimage.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:      tgaimage.cpp
Packit Service 21b5d1
  Author(s): Marco Piovanelli, Ovolab (marco)
Packit Service 21b5d1
  History:   05-Mar-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 "tgaimage.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
    TgaImage::TgaImage(BasicIo::AutoPtr io)
Packit Service 21b5d1
        : Image(ImageType::tga, mdNone, io)
Packit Service 21b5d1
    {
Packit Service 21b5d1
    } // TgaImage::TgaImage
Packit Service 21b5d1
Packit Service 21b5d1
    std::string TgaImage::mimeType() const
Packit Service 21b5d1
    {
Packit Service 21b5d1
        return "image/targa";
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void TgaImage::setExifData(const ExifData& /*exifData*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "Exif metadata", "TGA"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void TgaImage::setIptcData(const IptcData& /*iptcData*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "IPTC metadata", "TGA"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void TgaImage::setComment(const std::string& /*comment*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // not supported
Packit Service 21b5d1
        throw(Error(kerInvalidSettingForImage, "Image comment", "TGA"));
Packit Service 21b5d1
    }
Packit Service 21b5d1
Packit Service 21b5d1
    void TgaImage::readMetadata()
Packit Service 21b5d1
    {
Packit Service 21b5d1
#ifdef EXIV2_DEBUG_MESSAGES
Packit Service 21b5d1
        std::cerr << "Exiv2::TgaImage::readMetadata: Reading TARGA 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 (!isTgaType(*io_, false))
Packit Service 21b5d1
        {
Packit Service 21b5d1
            if (io_->error() || io_->eof()) throw Error(kerFailedToReadImageData);
Packit Service 21b5d1
            throw Error(kerNotAnImage, "TGA");
Packit Service 21b5d1
        }
Packit Service 21b5d1
        clearMetadata();
Packit Service 21b5d1
Packit Service 21b5d1
        /*
Packit Service 21b5d1
          The TARGA header goes as follows -- all numbers are in little-endian byte order:
Packit Service 21b5d1
Packit Service 21b5d1
          offset  length   name                     description
Packit Service 21b5d1
          ======  =======  =======================  ===========
Packit Service 21b5d1
           0      1 byte   ID length                length of image ID (0 to 255)
Packit Service 21b5d1
           1      1 byte   color map type           0 = no color map; 1 = color map included
Packit Service 21b5d1
           2      1 byte   image type                0 = no image;
Packit Service 21b5d1
                                                     1 = uncompressed color-mapped;
Packit Service 21b5d1
                                                     2 = uncompressed true-color;
Packit Service 21b5d1
                                                     3 = uncompressed black-and-white;
Packit Service 21b5d1
                                                     9 = RLE-encoded color mapped;
Packit Service 21b5d1
                                                    10 = RLE-encoded true-color;
Packit Service 21b5d1
                                                    11 = RLE-encoded black-and-white
Packit Service 21b5d1
           3      5 bytes  color map specification
Packit Service 21b5d1
           8      2 bytes  x-origin of image
Packit Service 21b5d1
          10      2 bytes  y-origin of image
Packit Service 21b5d1
          12      2 bytes  image width
Packit Service 21b5d1
          14      2 bytes  image height
Packit Service 21b5d1
          16      1 byte   pixel depth
Packit Service 21b5d1
          17      1 byte   image descriptor
Packit Service 21b5d1
        */
Packit Service 21b5d1
        byte buf[18];
Packit Service 21b5d1
        if (io_->read(buf, sizeof(buf)) == sizeof(buf))
Packit Service 21b5d1
        {
Packit Service 21b5d1
            pixelWidth_ = getShort(buf + 12, littleEndian);
Packit Service 21b5d1
            pixelHeight_ = getShort(buf + 14, littleEndian);
Packit Service 21b5d1
        }
Packit Service 21b5d1
    } // TgaImage::readMetadata
Packit Service 21b5d1
Packit Service 21b5d1
    void TgaImage::writeMetadata()
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // Todo: implement me!
Packit Service 21b5d1
        throw(Error(kerWritingImageFormatUnsupported, "TGA"));
Packit Service 21b5d1
    } // TgaImage::writeMetadata
Packit Service 21b5d1
Packit Service 21b5d1
    // *************************************************************************
Packit Service 21b5d1
    // free functions
Packit Service 21b5d1
    Image::AutoPtr newTgaInstance(BasicIo::AutoPtr io, bool /*create*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        Image::AutoPtr image(new TgaImage(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 isTgaType(BasicIo& iIo, bool /*advance*/)
Packit Service 21b5d1
    {
Packit Service 21b5d1
        // not all TARGA files have a signature string, so first just try to match the file name extension
Packit Service 21b5d1
#ifdef EXV_UNICODE_PATH
Packit Service 21b5d1
        std::wstring wpath = iIo.wpath();
Packit Service 21b5d1
        if(   wpath.rfind(EXV_WIDEN(".tga")) != std::wstring::npos
Packit Service 21b5d1
           || wpath.rfind(EXV_WIDEN(".TGA")) != std::wstring::npos) {
Packit Service 21b5d1
            return true;
Packit Service 21b5d1
        }
Packit Service 21b5d1
#else
Packit Service 21b5d1
        std::string path = iIo.path();
Packit Service 21b5d1
        if(   path.rfind(".tga") != std::string::npos
Packit Service 21b5d1
           || path.rfind(".TGA") != std::string::npos) {
Packit Service 21b5d1
            return true;
Packit Service 21b5d1
        }
Packit Service 21b5d1
#endif
Packit Service 21b5d1
        byte buf[26];
Packit Service 21b5d1
        long curPos = iIo.tell();
Packit Service 21b5d1
        iIo.seek(-26, BasicIo::end);
Packit Service 21b5d1
        if (iIo.error() || iIo.eof())
Packit Service 21b5d1
        {
Packit Service 21b5d1
            return false;
Packit Service 21b5d1
        }
Packit Service 21b5d1
        iIo.read(buf, sizeof(buf));
Packit Service 21b5d1
        if (iIo.error())
Packit Service 21b5d1
        {
Packit Service 21b5d1
            return false;
Packit Service 21b5d1
        }
Packit Service 21b5d1
        // some TARGA files, but not all, have a signature string at the end
Packit Service 21b5d1
        bool matched = (memcmp(buf + 8, "TRUEVISION-XFILE", 16) == 0);
Packit Service 21b5d1
        iIo.seek(curPos, BasicIo::beg);
Packit Service 21b5d1
        return matched;
Packit Service 21b5d1
    }
Packit Service 21b5d1
}                                       // namespace Exiv2