File: tiff-test.cpp

package info (click to toggle)
exiv2 0.28.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 109,216 kB
  • sloc: cpp: 77,667; python: 9,619; javascript: 237; makefile: 193; sh: 172; ansic: 51; sed: 16
file content (97 lines) | stat: -rw-r--r-- 3,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: GPL-2.0-or-later
// First and very simple TIFF write test.

#include <enforce.hpp>
#include <exiv2/exiv2.hpp>

#include <iomanip>
#include <iostream>
#include <string>

using namespace Exiv2;

void print(const ExifData& exifData);

void mini1(const char* path);
void mini9(const char* path);

int main(int argc, char* const argv[]) {
  try {
    Exiv2::XmpParser::initialize();
    ::atexit(Exiv2::XmpParser::terminate);

    if (argc != 2) {
      std::cout << "Usage: " << argv[0] << " file\n";
      return EXIT_FAILURE;
    }

    const char* path = argv[1];
    mini1(path);
    mini9(path);

    return EXIT_SUCCESS;
  } catch (const Error& e) {
    std::cout << e << "\n";
    return EXIT_FAILURE;
  }
}

void mini1(const char* path) {
  ExifData exifData;
  Blob blob;
  WriteMethod wm;

  // Write nothing to a new structure, without a previous binary image
  wm = ExifParser::encode(blob, nullptr, 0, bigEndian, exifData);
  Internal::enforce(wm == wmIntrusive, Exiv2::ErrorCode::kerErrorMessage, "encode returned an unexpected value");
  std::cout << "Test 1: Writing empty Exif data without original binary data: ok.\n";

  // Write nothing, this time with a previous binary image
  DataBuf buf = readFile(path);
  wm = ExifParser::encode(blob, buf.c_data(), buf.size(), bigEndian, exifData);
  Internal::enforce(wm == wmIntrusive, Exiv2::ErrorCode::kerErrorMessage, "encode returned an unexpected value");
  std::cout << "Test 2: Writing empty Exif data with original binary data: ok.\n";

  // Write something to a new structure, without a previous binary image
  exifData["Exif.Photo.DateTimeOriginal"] = "Yesterday at noon";
  wm = ExifParser::encode(blob, nullptr, 0, bigEndian, exifData);
  Internal::enforce(wm == wmIntrusive, Exiv2::ErrorCode::kerErrorMessage, "encode returned an unexpected value");
  std::cout << "Test 3: Wrote non-empty Exif data without original binary data:\n";
  exifData.clear();
  ByteOrder bo = ExifParser::decode(exifData, blob.data(), blob.size());
  Internal::enforce(bo == bigEndian, Exiv2::ErrorCode::kerErrorMessage, "decode returned an unexpected value");
  print(exifData);
}

void mini9(const char* path) {
  TiffImage tiffImage(std::make_unique<FileIo>(path), false);
  tiffImage.readMetadata();

  std::cout << "MIME type:  " << tiffImage.mimeType() << "\n";
  std::cout << "Image size: " << tiffImage.pixelWidth() << " x " << tiffImage.pixelHeight() << "\n";

  ExifData& exifData = tiffImage.exifData();
  std::cout << "Before\n";
  print(exifData);
  std::cout << "======\n";

  exifData["Exif.Photo.DateTimeOriginal"] = "Yesterday at noon";

  std::cout << "After\n";
  print(exifData);
  tiffImage.writeMetadata();
}

void print(const ExifData& exifData) {
  if (exifData.empty()) {
    std::string error("No Exif data found in the file");
    throw Exiv2::Error(ErrorCode::kerErrorMessage, error);
  }
  auto end = exifData.end();
  for (auto i = exifData.begin(); i != end; ++i) {
    std::cout << std::setw(44) << std::setfill(' ') << std::left << i->key() << " "
              << "0x" << std::setw(4) << std::setfill('0') << std::right << std::hex << i->tag() << " " << std::setw(9)
              << std::setfill(' ') << std::left << i->typeName() << " " << std::dec << std::setw(3) << std::setfill(' ')
              << std::right << i->count() << "  " << std::dec << i->value() << "\n";
  }
}