UNPKG

@aidin36/xmp

Version:

Read and write XMP metadata from/to various media formats

166 lines (165 loc) 7.08 kB
"use strict"; /* * This file is part of @aidin36/xmp Javascript package. * * @aidin36/xmp is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any * later version. * * @aidin36/xmp is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details * * You should have received a copy of the GNU Lesser General Public License * along with @aidin26/xmp. If not, see <https://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.writeXmpToHeicAsString = exports.writeXmpToHeic = exports.readXmpFromHeicAsJs = exports.readXmpFromHeic = exports.readXmpFromHeicAsBinary = exports.readXmpFromJpegAsJs = exports.readXmpFromJpeg = exports.readXmpFromJpegAsBinary = void 0; const xmp2js_1 = require("@aidin36/xmp2js"); const jpegUtils_1 = require("./jpegUtils"); const heicReader_1 = require("./heicReader"); const heicWriter_1 = require("./heicWriter"); /** * This is the most portable way of reading the data. Because not every * Javascript environment supports TextDecoder. In those cases, you can call * this function and decode the output using what is available. * * If your environment has TextDecoder, or if you have a polyfill of it in your * application, you can use 'readXmpFromJpegBinary' function to get a decoded * string. * * Note that the output is encoded as UTF-8. Javascript's strings are UTF-16. * So you need to decode the output from UTF-8. * * @param image - Data of the JPEG image in the form of Uint8Array * @returns XMP as a UTF-8 byte array, or undefined if no XMP data found in the * provided file. */ const readXmpFromJpegAsBinary = (image) => (0, jpegUtils_1.jpegExtractXMP)(image); exports.readXmpFromJpegAsBinary = readXmpFromJpegAsBinary; /** * Extracts the XMP string from a JPEG file. * It returns the string as-is without modifications. * * Note that your Javascript environment should provide TextDecoder or a * polyfill of it. * * @param image - Data of the JPEG image in the form of Uint8Array * @returns XMP as a string, or undefined if no XMP data found in the provided file. */ const readXmpFromJpeg = (image) => { if (TextDecoder === undefined) { throw Error('This method needs TextDecoder which is not available in your environment. You can try other methods.'); } const xmpBinary = (0, jpegUtils_1.jpegExtractXMP)(image); if (xmpBinary == null) { return undefined; } const decoder = new TextDecoder('UTF-8'); return decoder.decode(xmpBinary); }; exports.readXmpFromJpeg = readXmpFromJpeg; /** * Extracts the XMP from a JPEG file, and transforms it to a JS Object. * It uses the '@aidin36/xmp2js' for the transformation. You can read the documents * of '@aidin36/xmp2js' to learn more about the output format. * * @param image - Data of the JPEG image in the form of Uint8Array * @returns A JS Object, or undefined if no XMP data found in the provided file. */ const readXmpFromJpegAsJs = (image) => { const xmpString = (0, exports.readXmpFromJpeg)(image); if (xmpString == null || xmpString.length === 0) { return undefined; } return (0, xmp2js_1.xmp2js)(xmpString); }; exports.readXmpFromJpegAsJs = readXmpFromJpegAsJs; /** * This is the most portable way of reading the data. Because not every * Javascript environment supports TextDecoder. In those cases, you can call * this function and decode the output using what is available. * * If your environment has TextDecoder, or if you have a polyfill of it in your * application, you can use 'readXmpFromHeic' function to get a decoded string. * * Note that the output is encoded as UTF-8. Javascript's strings are UTF-16. * So you need to decode the output from UTF-8. * * @param image - Data of the HEIC image in the form of Uint8Array * @returns XMP as a UTF-8 byte array, or undefined if no XMP data found in the * provided file. */ const readXmpFromHeicAsBinary = (image) => (0, heicReader_1.heicExtractXmp)(image); exports.readXmpFromHeicAsBinary = readXmpFromHeicAsBinary; /** * Extracts the XMP string from a HEIC file. * It returns the string as-is without modifications. * * Note that your Javascript environment should provide TextDecoder. * * @param image - Data of the HEIC image in the form of Uint8Array * @returns XMP as a string, or undefined if no XMP data found in the provided file. */ const readXmpFromHeic = (image) => { if (TextDecoder === undefined) { throw Error('This method needs TextDecoder which is not available in your environment. You can try other methods.'); } const xmpBinary = (0, heicReader_1.heicExtractXmp)(image); if (xmpBinary == null) { return undefined; } const decoder = new TextDecoder('UTF-8'); return decoder.decode(xmpBinary); }; exports.readXmpFromHeic = readXmpFromHeic; /** * Extracts the XMP string from a HEIC file, then converts it to a Javascript * Object. * It uses the '@aidin36/xmp2js' for the transformation. You can read the documents * of '@aidin36/xmp2js' to learn more about the output format. * * Note that your Javascript environment should provide TextDecoder. * * @param image - Data of the HEIC image in the form of Uint8Array * @returns A JS Object, or undefined if no XMP data found in the provided file. */ const readXmpFromHeicAsJs = (image) => { const xmpStr = (0, exports.readXmpFromHeic)(image); if (xmpStr == null || xmpStr.length === 0) { return undefined; } return (0, xmp2js_1.xmp2js)(xmpStr); }; exports.readXmpFromHeicAsJs = readXmpFromHeicAsJs; /** * Writes the XMP data to an image file in HEIC format. * This is the most portable overload. Because not every Javascript * environment supports TextEncoder. In those cases, you can encode your * XMP with what is available and pass it. * * Note that XMP needs to be in UTF-8 encoding. * * @param image - Data of the HEIC image in the form of Uint8Array * @param xmp - Encoded XMP * @returns The modified image */ const writeXmpToHeic = (image, xmp) => (0, heicWriter_1.heicWriteOrUpdateXmp)(image, xmp); exports.writeXmpToHeic = writeXmpToHeic; /** * Writes the XMP data to an image file in HEIC format. * * @param image - Data of the HEIC image in the form of Uint8Array * @param xmp - Encoded XMP * @returns The modified image */ const writeXmpToHeicAsString = (image, xmp) => { if (TextEncoder === undefined) { throw Error('This method needs TextEncoder which is not available in your environment. You can try other methods.'); } const encoder = new TextEncoder(); const encodedXmp = encoder.encode(xmp); return (0, heicWriter_1.heicWriteOrUpdateXmp)(image, encodedXmp); }; exports.writeXmpToHeicAsString = writeXmpToHeicAsString;