1 // ***************************************************************** -*- C++ -*-
2 // mrwthumb.cpp
3 // Sample program to extract a Minolta thumbnail from the makernote
4 /*
5  * Copyright (C) 2004-2021 Exiv2 authors
6  * This program is part of the Exiv2 distribution.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <exiv2/exiv2.hpp>
24 #include <cassert>
25 #include <iostream>
26 
main(int argc,char * const argv[])27 int main(int argc, char* const argv[])
28 {
29     Exiv2::XmpParser::initialize();
30     ::atexit(Exiv2::XmpParser::terminate);
31 #ifdef EXV_ENABLE_BMFF
32     Exiv2::enableBMFF();
33 #endif
34 
35     try {
36         if (argc != 2) {
37             std::cout << "Usage: " << argv[0] << " file\n";
38             return 1;
39         }
40 
41         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
42         assert(image.get() != 0);
43         image->readMetadata();
44 
45         Exiv2::ExifData& exifData = image->exifData();
46         if (exifData.empty()) {
47             std::string error(argv[1]);
48             error += ": No Exif data found in the file";
49             throw Exiv2::Error(Exiv2::kerErrorMessage, error);
50         }
51 
52         Exiv2::ExifKey key("Exif.Minolta.ThumbnailOffset");
53         Exiv2::ExifData::const_iterator format = exifData.findKey(key);
54 
55         if (format != exifData.end()) {
56             Exiv2::DataBuf buf = format->dataArea();
57 
58             // The first byte of the buffer needs to be patched
59             buf.pData_[0] = 0xff;
60 
61             Exiv2::FileIo file("img_thumb.jpg");
62 
63             file.open("wb");
64             file.write(buf.pData_, buf.size_);
65             file.close();
66         }
67 
68         return 0;
69     } catch (Exiv2::AnyError& e) {
70         std::cout << "Caught Exiv2 exception '" << e << "'\n";
71         return -1;
72     }
73 }
74