Coverage Report

Created: 2026-08-13 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/exiv2/src/tgaimage.cpp
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
// included header files
4
#include "tgaimage.hpp"
5
6
#include "basicio.hpp"
7
#include "config.h"
8
#include "error.hpp"
9
#include "futils.hpp"
10
#include "image.hpp"
11
12
#include <cstring>
13
14
#ifdef EXIV2_DEBUG_MESSAGES
15
#include <iostream>
16
#endif
17
18
// *****************************************************************************
19
// class member definitions
20
namespace Exiv2 {
21
TgaImage::TgaImage(BasicIo::UniquePtr io, const ImageCtorParams& params) :
22
12
    Image(ImageType::tga, mdNone, std::move(io), params) {
23
12
}
24
25
0
std::string TgaImage::mimeType() const {
26
0
  return "image/targa";
27
0
}
28
29
0
void TgaImage::setExifData(const ExifData& /*exifData*/) {
30
  // Todo: implement me!
31
0
  throw(Error(ErrorCode::kerInvalidSettingForImage, "Exif metadata", "TGA"));
32
0
}
33
34
0
void TgaImage::setIptcData(const IptcData& /*iptcData*/) {
35
  // Todo: implement me!
36
0
  throw(Error(ErrorCode::kerInvalidSettingForImage, "IPTC metadata", "TGA"));
37
0
}
38
39
0
void TgaImage::setComment(const std::string&) {
40
  // not supported
41
0
  throw(Error(ErrorCode::kerInvalidSettingForImage, "Image comment", "TGA"));
42
0
}
43
44
0
void TgaImage::readMetadata() {
45
#ifdef EXIV2_DEBUG_MESSAGES
46
  std::cerr << "Exiv2::TgaImage::readMetadata: Reading TARGA file " << io_->path() << "\n";
47
#endif
48
0
  if (io_->open() != 0) {
49
0
    throw Error(ErrorCode::kerDataSourceOpenFailed, io_->path(), strError());
50
0
  }
51
0
  IoCloser closer(*io_);
52
  // Ensure that this is the correct image type
53
0
  if (!isTgaType(*io_, false)) {
54
0
    if (io_->error() || io_->eof())
55
0
      throw Error(ErrorCode::kerFailedToReadImageData);
56
0
    throw Error(ErrorCode::kerNotAnImage, "TGA");
57
0
  }
58
0
  clearMetadata();
59
60
  /*
61
    The TARGA header goes as follows -- all numbers are in little-endian byte order:
62
63
    offset  length   name                     description
64
    ======  =======  =======================  ===========
65
     0      1 byte   ID length                length of image ID (0 to 255)
66
     1      1 byte   color map type           0 = no color map; 1 = color map included
67
     2      1 byte   image type                0 = no image;
68
                                               1 = uncompressed color-mapped;
69
                                               2 = uncompressed true-color;
70
                                               3 = uncompressed black-and-white;
71
                                               9 = RLE-encoded color mapped;
72
                                              10 = RLE-encoded true-color;
73
                                              11 = RLE-encoded black-and-white
74
     3      5 bytes  color map specification
75
     8      2 bytes  x-origin of image
76
    10      2 bytes  y-origin of image
77
    12      2 bytes  image width
78
    14      2 bytes  image height
79
    16      1 byte   pixel depth
80
    17      1 byte   image descriptor
81
  */
82
0
  byte buf[18];
83
0
  if (io_->read(buf, sizeof(buf)) == sizeof(buf)) {
84
0
    pixelWidth_ = getShort(buf + 12, littleEndian);
85
0
    pixelHeight_ = getShort(buf + 14, littleEndian);
86
0
  }
87
0
}  // TgaImage::readMetadata
88
89
0
void TgaImage::writeMetadata() {
90
  // Todo: implement me!
91
0
  throw(Error(ErrorCode::kerWritingImageFormatUnsupported, "TGA"));
92
0
}  // TgaImage::writeMetadata
93
94
// *************************************************************************
95
// free functions
96
12
Image::UniquePtr newTgaInstance(BasicIo::UniquePtr io, const ImageCtorParams& params) {
97
12
  auto image = std::make_unique<TgaImage>(std::move(io), params);
98
12
  if (!image->good()) {
99
12
    return nullptr;
100
12
  }
101
0
  return image;
102
12
}
103
104
15.4k
bool isTgaType(BasicIo& iIo, bool /*advance*/) {
105
  // not all TARGA files have a signature string, so first just try to match the file name extension
106
15.4k
  const std::string& path = iIo.path();
107
15.4k
  if (path.ends_with(".tga") || path.ends_with(".TGA")) {
108
0
    return true;
109
0
  }
110
15.4k
  byte buf[26];
111
15.4k
  const size_t curPos = iIo.tell();
112
15.4k
  if (curPos < 26)
113
15.2k
    return false;
114
115
295
  iIo.seek(-26, BasicIo::end);
116
295
  if (iIo.error() || iIo.eof()) {
117
0
    return false;
118
0
  }
119
295
  iIo.read(buf, sizeof(buf));
120
295
  if (iIo.error()) {
121
0
    return false;
122
0
  }
123
  // some TARGA files, but not all, have a signature string at the end
124
295
  bool matched = (memcmp(buf + 8, "TRUEVISION-XFILE", 16) == 0);
125
295
  iIo.seek(curPos, BasicIo::beg);
126
295
  return matched;
127
295
}
128
}  // namespace Exiv2