Project

General

Profile

Bug #780 ยป thumb.cpp

Reproducer based on the code in the bug report - Andreas Huggel, 27 Jul 2011 19:35

 
1
#include <exiv2/exiv2.hpp>
2

    
3
#include <iostream>
4
#include <iomanip>
5
#include <cassert>
6

    
7
int main(int argc, char* const argv[])
8
{
9
    if (argc != 2) {
10
        std::cout << "Usage: " << argv[0] << " file\n";
11
        return 1;
12
    }
13

    
14
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
15
    assert(image.get() != 0);
16
    image->readMetadata();
17

    
18
    Exiv2::ExifData exifData = image->exifData();
19

    
20
    if (exifData.empty()) {
21
        std::cerr << "exif data is empty\n";
22
        exifData = Exiv2::ExifData();
23
    }
24

    
25
    bool isTiff = (image->mimeType() == "image/tiff");
26

    
27
    // ok, let's try to save the thumbnail...
28
    try {
29

    
30
        Exiv2::ExifThumb eThumb(exifData);
31

    
32
        if (isTiff) {
33
            eThumb.erase();
34

    
35
            Exiv2::ExifData::const_iterator pos = exifData.findKey(Exiv2::ExifKey("Exif.Image.NewSubfileType"));
36
            if (pos == exifData.end() || pos->count() != 1 || pos->toLong() != 0) {
37
                throw Exiv2::Error(1, "Exif.Image.NewSubfileType missing or not set as main image");
38
             }
39
             std::string subImage1("SubImage1");
40
             for (Exiv2::ExifData::iterator md = exifData.begin(); md != exifData.end();)
41
             {
42
                 if (md->groupName() == subImage1)
43
                     md = exifData.erase(md);
44
                 else
45
                     ++md;
46
             }
47
        }
48

    
49
        Exiv2::DataBuf buf(256);
50
        memset(buf.pData_, 0x88, buf.size_);
51

    
52
        if (isTiff) {
53
            Exiv2::ULongValue val;
54
            val.read("0");
55
            val.setDataArea(buf.pData_, buf.size_);
56
            exifData["Exif.SubImage1.JPEGInterchangeFormat"] = val;
57
            exifData["Exif.SubImage1.JPEGInterchangeFormatLength"] = uint32_t(buf.size_);
58
            exifData["Exif.SubImage1.Compression"] = uint16_t(6); // JPEG (old-style)
59
            exifData["Exif.SubImage1.NewSubfileType"] = uint32_t(1); // Thumbnail image
60
            std::cerr << "As you told me to, I am writing the TIFF thumbs...\n";
61

    
62
        } else {
63
            eThumb.setJpegThumbnail(buf.pData_, buf.size_);
64
            std::cerr << "As you told me to, I am writing the JPEG thumbs...\n";
65
        }
66

    
67
        image->setExifData(exifData);
68
        image->writeMetadata();
69
    }
70
    catch (Exiv2::Error& e) {
71
        std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
72
        return -1;
73
    }
74

    
75
    return 0;
76
}
    (1-1/1)