summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FileHandlers/WEBP_Handler.cpp
blob: 24b0147424b4d2ed524ccfb56faeb7cddb430f71 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "public/include/XMP_Const.h"
#include "public/include/XMP_Environment.h" // ! XMP_Environment.h must be the first included header.

#include "XMPFiles/source/FileHandlers/WEBP_Handler.hpp"

#include "XMPFiles/source/FormatSupport/IPTC_Support.hpp"
#include "XMPFiles/source/FormatSupport/PSIR_Support.hpp"
#include "XMPFiles/source/FormatSupport/ReconcileLegacy.hpp"
#include "XMPFiles/source/FormatSupport/Reconcile_Impl.hpp"
#include "XMPFiles/source/FormatSupport/TIFF_Support.hpp"
#include "XMPFiles/source/FormatSupport/WEBP_Support.hpp"
#include "source/XIO.hpp"

using namespace std;

/// File format handler for WEBP.

XMPFileHandler* WEBP_MetaHandlerCTor(XMPFiles* parent)
{
    return new WEBP_MetaHandler(parent);
}

// Check that the file begins with "RIFF", a 4 byte length, then the chunkType
// (WEBP).
bool WEBP_CheckFormat(XMP_FileFormat format, XMP_StringPtr filePath,
                      XMP_IO* file, XMPFiles* parent)
{
    IgnoreParam(filePath);
    IgnoreParam(parent);
    XMP_Assert(format == kXMP_WEBPFile);

    if (file->Length() < 12)
        return false;
    file->Rewind();

    XMP_Uns8 chunkID[12];
    file->ReadAll(chunkID, 12);
    if (!CheckBytes(&chunkID[0], "RIFF", 4)) {
        return false;
    }
    if (CheckBytes(&chunkID[8], "WEBP", 4) && format == kXMP_WEBPFile) {
        return true;
    }
    return false;
}

WEBP_MetaHandler::WEBP_MetaHandler(XMPFiles* parent)
  : exifMgr(0)
{
    this->parent = parent;
    this->handlerFlags = kWEBP_HandlerFlags;
    this->stdCharForm = kXMP_Char8Bit;

    this->initialFileSize = 0;
    this->mainChunk = 0;
}

WEBP_MetaHandler::~WEBP_MetaHandler()
{
    if (this->mainChunk) {
        delete this->mainChunk;
    }
    if (this->exifMgr) {
        delete this->exifMgr;
    }
    if (this->iptcMgr) {
        delete this->iptcMgr;
    }
    if (this->psirMgr) {
        delete this->psirMgr;
    }
}

void WEBP_MetaHandler::CacheFileData()
{
    this->containsXMP = false; // assume for now

    XMP_IO* file = this->parent->ioRef;
    this->initialFileSize = file->Length();

    file->Rewind();

    XMP_Int64 filePos = 0;
    while (filePos < this->initialFileSize) {
        this->mainChunk = new WEBP::Container(this);
        filePos = file->Offset();
    }

    // covered before => internal error if it occurs
    XMP_Validate(file->Offset() == this->initialFileSize,
                 "WEBP_MetaHandler::CacheFileData: unknown data at end of file",
                 kXMPErr_InternalFailure);
}

void WEBP_MetaHandler::ProcessXMP()
{
    SXMPUtils::RemoveProperties(&this->xmpObj, 0, 0, kXMPUtil_DoAllProperties);

    bool readOnly = false;
    bool xmpOnly = false;
    bool haveExif = false;
    if (this->parent) {
        readOnly =
            !XMP_OptionIsSet(this->parent->openFlags, kXMPFiles_OpenForUpdate);
        xmpOnly =
            XMP_OptionIsSet(this->parent->openFlags, kXMPFiles_OpenOnlyXMP);
    }
    if (!xmpOnly) {
        if (readOnly) {
            this->exifMgr = new TIFF_MemoryReader();
        }
        else {
            this->exifMgr = new TIFF_FileWriter();
        }
        this->psirMgr = new PSIR_MemoryReader();
        this->iptcMgr = new IPTC_Reader();
        if (this->parent) {
            exifMgr->SetErrorCallback(&this->parent->errorCallback);
        }
        if (this->mainChunk) {
            WEBP::Chunk* exifChunk = this->mainChunk->getExifChunk();
            if (exifChunk != NULL) {
                haveExif = true;
                this->exifMgr->ParseMemoryStream(exifChunk->data.data() + 6,
                                                 exifChunk->data.size() - 6);
            }
        }
    }

    if (this->containsXMP) {
        this->xmpObj.ParseFromBuffer(this->xmpPacket.c_str(),
                                     (XMP_StringLen) this->xmpPacket.size());
    }
    if (haveExif) {
        XMP_OptionBits options = k2XMP_FileHadExif;
        if (this->containsXMP) {
            options |= k2XMP_FileHadXMP;
        }
        TIFF_Manager& exif = *this->exifMgr;
        PSIR_Manager& psir = *this->psirMgr;
        IPTC_Manager& iptc = *this->iptcMgr;
        ImportPhotoData(exif, iptc, psir, kDigestMatches, &this->xmpObj,
                        options);
        // Assume that, since the file had EXIF data, some of it was mapped to
        // XMP
        this->containsXMP = true;
    }
    this->processedXMP = true;
}

void WEBP_MetaHandler::UpdateFile(bool /*doSafeUpdate*/)
{
    XMP_Validate(this->needsUpdate, "nothing to update",
                 kXMPErr_InternalFailure);

    bool xmpOnly = false;
    if (this->parent) {
        xmpOnly =
            XMP_OptionIsSet(this->parent->openFlags, kXMPFiles_OpenOnlyXMP);
    }

    if (!xmpOnly && this->exifMgr) {
        WEBP::Chunk* exifChunk = this->mainChunk->getExifChunk();
        if (exifChunk != NULL) {
            ExportPhotoData(kXMP_TIFFFile, &this->xmpObj, this->exifMgr, 0, 0);
            if (this->exifMgr->IsLegacyChanged()) {
                XMP_Uns8* exifPtr;
                XMP_Uns32 exifLen =
                    this->exifMgr->UpdateMemoryStream((void**)&exifPtr);
                RawDataBlock exifData(&exifChunk->data[0], &exifChunk->data[6]);
                exifData.insert(exifData.begin() + 6, &exifPtr[0],
                                &exifPtr[exifLen]);
                exifChunk->data = exifData;
                exifChunk->size = exifLen + 6;
                exifChunk->needsRewrite = true;
            }
        }
    }

    this->packetInfo.charForm = stdCharForm;
    this->packetInfo.writeable = true;
    this->packetInfo.offset = kXMPFiles_UnknownOffset;
    this->packetInfo.length = kXMPFiles_UnknownLength;

    this->xmpObj.SerializeToBuffer(&this->xmpPacket, kXMP_OmitPacketWrapper);

    this->mainChunk->write(this);
    this->needsUpdate = false; // do last for safety
}

void WEBP_MetaHandler::WriteTempFile(XMP_IO* tempRef)
{
    IgnoreParam(tempRef);
    XMP_Throw("WEBP_MetaHandler::WriteTempFile: Not supported (must go through "
              "UpdateFile)",
              kXMPErr_Unavailable);
}