Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad58a34e authored by Fyodor Kyslov's avatar Fyodor Kyslov
Browse files

librecoverymap: Update XMP parser

Updating XMP parser to support latest JPEGR XMP spec

Bug: b/252835416
Test: manual
Change-Id: I882cd3bebb7db8d3c7f7afa51cf70c0895c68b3d
parent f5847b80
Loading
Loading
Loading
Loading
+54 −15
Original line number Original line Diff line number Diff line
@@ -36,7 +36,7 @@ class XMPXmlHandler : public XmlHandler {
public:
public:


    XMPXmlHandler() : XmlHandler() {
    XMPXmlHandler() : XmlHandler() {
        rangeScalingFactorState = NotStrarted;
        gContainerItemState = NotStrarted;
    }
    }


    enum ParseState {
    enum ParseState {
@@ -48,11 +48,11 @@ public:
    virtual DataMatchResult StartElement(const XmlTokenContext& context) {
    virtual DataMatchResult StartElement(const XmlTokenContext& context) {
        string val;
        string val;
        if (context.BuildTokenValue(&val)) {
        if (context.BuildTokenValue(&val)) {
            if (!val.compare(rangeScalingFactorName)) {
            if (!val.compare(gContainerItemName)) {
                rangeScalingFactorState = Started;
                gContainerItemState = Started;
            } else {
            } else {
                if (rangeScalingFactorState != Done) {
                if (gContainerItemState != Done) {
                    rangeScalingFactorState = NotStrarted;
                    gContainerItemState = NotStrarted;
                }
                }
            }
            }
        }
        }
@@ -60,24 +60,45 @@ public:
    }
    }


    virtual DataMatchResult FinishElement(const XmlTokenContext& context) {
    virtual DataMatchResult FinishElement(const XmlTokenContext& context) {
        if (rangeScalingFactorState == Started) {
        if (gContainerItemState == Started) {
            rangeScalingFactorState = Done;
            gContainerItemState = Done;
            lastAttributeName = "";
        }
        }
        return context.GetResult();
        return context.GetResult();
    }
    }


    virtual DataMatchResult ElementContent(const XmlTokenContext& context) {
    virtual DataMatchResult AttributeName(const XmlTokenContext& context) {
        string val;
        string val;
        if (rangeScalingFactorState == Started) {
        if (gContainerItemState == Started) {
            if (context.BuildTokenValue(&val)) {
            if (context.BuildTokenValue(&val)) {
                rangeScalingFactorStr.assign(val);
                if (!val.compare(rangeScalingFactorAttrName)) {
                    lastAttributeName = rangeScalingFactorAttrName;
                } else if (!val.compare(transferFunctionAttrName)) {
                    lastAttributeName = transferFunctionAttrName;
                } else {
                    lastAttributeName = "";
                }
            }
        }
        return context.GetResult();
    }

    virtual DataMatchResult AttributeValue(const XmlTokenContext& context) {
        string val;
        if (gContainerItemState == Started) {
            if (context.BuildTokenValue(&val, true)) {
                if (!lastAttributeName.compare(rangeScalingFactorAttrName)) {
                    rangeScalingFactorStr = val;
                } else if (!lastAttributeName.compare(transferFunctionAttrName)) {
                    transferFunctionStr = val;
                }
            }
            }
        }
        }
        return context.GetResult();
        return context.GetResult();
    }
    }


    bool getRangeScalingFactor(float* scaling_factor) {
    bool getRangeScalingFactor(float* scaling_factor) {
        if (rangeScalingFactorState == Done) {
        if (gContainerItemState == Done) {
            stringstream ss(rangeScalingFactorStr);
            stringstream ss(rangeScalingFactorStr);
            float val;
            float val;
            if (ss >> val) {
            if (ss >> val) {
@@ -92,17 +113,35 @@ public:
    }
    }


    bool getTransferFunction(jpegr_transfer_function* transfer_function) {
    bool getTransferFunction(jpegr_transfer_function* transfer_function) {
        *transfer_function = JPEGR_TF_HLG;
        if (gContainerItemState == Done) {
            stringstream ss(transferFunctionStr);
            int val;
            if (ss >> val) {
                *transfer_function = static_cast<jpegr_transfer_function>(val);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
        return true;
    }
    }


private:
private:
    static const string rangeScalingFactorName;
    static const string gContainerItemName;
    static const string rangeScalingFactorAttrName;
    static const string transferFunctionAttrName;
    string              rangeScalingFactorStr;
    string              rangeScalingFactorStr;
    ParseState          rangeScalingFactorState;
    string              transferFunctionStr;
    string              lastAttributeName;
    ParseState          gContainerItemState;
};
};


const string XMPXmlHandler::rangeScalingFactorName = "GContainer:rangeScalingFactor";
const string XMPXmlHandler::gContainerItemName = "GContainer:Item";
const string XMPXmlHandler::rangeScalingFactorAttrName = "RecoveryMap:RangeScalingFactor";
const string XMPXmlHandler::transferFunctionAttrName = "RecoveryMap:TransferFunction";





bool getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, jpegr_metadata* metadata) {
bool getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, jpegr_metadata* metadata) {