@ohos-ports/applitools-image 1.3.2-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/dist/formats/bmp.js +45 -0
- package/dist/formats/gif.js +51 -0
- package/dist/formats/jpeg.js +54 -0
- package/dist/formats/png-sharp.js +44 -0
- package/dist/formats/png.js +113 -0
- package/dist/image.js +593 -0
- package/dist/index.js +21 -0
- package/dist/types.js +2 -0
- package/package.json +65 -0
- package/types/formats/bmp.d.ts +5 -0
- package/types/formats/gif.d.ts +6 -0
- package/types/formats/jpeg.d.ts +5 -0
- package/types/formats/png-sharp.d.ts +3 -0
- package/types/formats/png.d.ts +17 -0
- package/types/image.d.ts +46 -0
- package/types/index.d.ts +5 -0
- package/types/types.d.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
- -
|
|
2
|
+
SDK LICENSE AGREEMENT
|
|
3
|
+
IMPORTANT - PLEASE READ CAREFULLY THE TERMS OF THIS LICENSE AGREEMENT (“AGREEMENT”). BY INSTALLING, ACCESSING AND/OR USING THE SOFTWARE (AS DEFINED BELOW), YOU EXPRESSLY ACKNOWLEDGE AND AGREE THAT YOU, OR THE COMPANY YOU REPRESENT, (“YOU” OR “LICENSEE”) ARE ENTERING INTO A LEGAL AGREEMENT WITH APPLITOOLS LTD. AND ITS AFFILIATES (“APPLITOOLS”), AND HAVE UNDERSTOOD AND AGREE TO COMPLY WITH, AND BE LEGALLY BOUND BY, THE TERMS AND CONDITIONS OF THIS AGREEMENT. YOU HEREBY WAIVE ANY RIGHTS OR REQUIREMENTS UNDER ANY LAWS OR REGULATIONS IN ANY JURISDICTION WHICH REQUIRE AN ORIGINAL (NON-ELECTRONIC) SIGNATURE OR DELIVERY OR RETENTION OF NON-ELECTRONIC RECORDS, TO THE EXTENT PERMITTED UNDER APPLICABLE LAW.
|
|
4
|
+
THE SOFTWARE MAY BE USED SOLELY FOR YOUR PERSONAL, NON-COMMERCIAL PURPOSES. FOR COMMERCIAL PURPOSES PLEASE CONTACT THE REGIONAL APPLITOOLS BUSINESS REPRESENTATIVE.
|
|
5
|
+
1. Definitions. For purposes of this Agreement, the following capitalized terms shall have the following meaning:
|
|
6
|
+
1. “Documentation” means the user’s guides and technical manuals delivered by Applitools to Licensee.
|
|
7
|
+
2. “Feedback” means suggestions, comments or feedback (whether orally or in writing) with respect to the Software.
|
|
8
|
+
3. “Intellectual Property Rights” means all intangible legal rights, titles and interests evidenced by or embodied in all: (i) inventions (regardless of patentability and whether or not reduced to practice), improvements thereto, patents, patent applications, patent disclosures, together with all reissuances, continuations, continuations in part, revisions, extensions and reexaminations thereof; (ii) trademarks, service marks, trade dress, logos, trade names, corporate names, together with translations, adaptations, derivations and combinations thereof, including goodwill associated therewith, and applications, registrations, and renewals in connection therewith; (iii) any work of authorship, regardless of copyrightable, copyrightable works, copyrights (including moral rights), and applications, registrations and renewals in connection therewith; (iv) mask works and applications, registrations and renewals in connection therewith; (v) trade secrets and Confidential Information; and (vi) other proprietary rights and any other similar rights, in each case on a worldwide basis, and copies and tangible embodiments thereof, in whatever form or medium.
|
|
9
|
+
4. “License” means the right to use the Software pursuant to Section 2.1 to this Agreement.
|
|
10
|
+
5. “Software” means Applitools’ software development kit software in object or source code version, Documentation and any updates and upgrade thereto (to the extent delivered).
|
|
11
|
+
1. License
|
|
12
|
+
1. Grant of License. Subject to the terms and conditions of this Agreement, Applitools grants You, during the Term, a personal, non-exclusive, non-sublicensable, non-transferable, revocable license to: (i) use the Software solely for Your own personal (non-commercial) use and (ii) use and display Applitools’ Marks solely for the purpose of publicizing or advertising that You are using the Software.
|
|
13
|
+
2. Documentation. Applitools may make available Documentation to Licensee for Licensee to use solely in connection with Licensee’s use of the Software during the term of this Agreement. Licensee may print or copy the Documentation as needed for its own purposes provided that all copyright notices are included therein. The Documentation shall be considered the Confidential Information of Applitools.
|
|
14
|
+
1. Reservation of Rights; Use Restrictions. Other than the rights explicitly granted in this Agreement, Licensee shall have no other rights, express or implied, in the Software. Without limiting the generality of the foregoing, Licensee agrees and undertakes not to: (i) allow any third party to use the Software in any manner, including but not limited to, sell, lease, sublicense or distribute the Software, or any part thereof; (ii) modify, revise, or alter the Software or reverse engineer, decompile, disassemble or otherwise reduce to human-perceivable form the Software’s source code; (iii) copy or allow copies of the Software to be made; (iv) remove, alter or obscure any proprietary notice or identification, including copyright, trademark, patent or other notices, contained in or displayed on or via the Software; (v) use the Software to violate any applicable laws, rules or regulations, or for any unlawful, harmful, irresponsible, or inappropriate purpose, or in any manner that breaches this Agreement, and/or (vi) represent that it possesses any proprietary interest in the Software.
|
|
15
|
+
1. Third Party Software. Licensee acknowledges and agrees that any third party software (“Third Party Software”) that provided with the Software is provided under the terms of the license attached/linked thereto or, if no such license is attached, such Third Party Software is provided for free and on “AS IS” basis. Applitools is not liable for any losses or damages which may occur resulting from the use of any Third Party Software. Applitools does not possess any proprietary interest in such Third Party Software.
|
|
16
|
+
1. Open Source Licenses. The Software includes certain open source code software and materials (as shall be listed in the documentation of the Software) (“Open Source Software”) that are subject to their respective open source licenses (“Open Source Licenses”). Such Open Source Licenses contain a list of conditions with respect to warranty, copyright policy and other provisions. By executing this Agreement, Licensee undertakes to strictly comply with the terms and condition of the Open Source Licenses, as may be amended from time to time. In order to comply with the Open Source Licenses, Licensee shall read the respective licenses or notices, such list of Open Source Licenses may be amended from time to time by Applitools, at its sole discretion. In the event of any inconsistencies or conflicting provisions between the provisions of the Open Source Licenses and the provisions of this Agreement, the provisions of the Open Source Licenses shall prevail. Without derogating from the generality of the foregoing, it is clarified that any Open Source Software is provided on an “AS IS” basis, without indemnity or warranty of any kind, whether express or implied. For clarity, the representations and warranties set forth in Section 4 hereunder shall not apply to any Open Source Software.
|
|
17
|
+
1. Title & Ownership. APPLITOOLS DOES NOT SELL OR TRANSFER TITLE IN THE SOFTWARE, OR ANY PART THEREOF, TO LICENSEE. The Documentation, Software (excluding any Open Source Software and Third Party Software therein which are owned by their respective licensors) and/or any copies thereof, including without limitation any derivative works made (regardless of whether such derivative works were made and/or developed pursuant to the request and/or specifications of Licensee, and irrespective of any support and/or assistance Applitools may, will or had received from Licensee, or any third party on its behalf, with respect thereto), as well as any updates or upgrades thereto, if provided to Applitools pursuant to this Agreement, shall remain Applitools’ sole and exclusive property. All Intellectual Property Rights evidenced by or embodied in and/or attached/connected/related to the Software, or part thereof, are and shall be owned solely and exclusively by Applitools. Nothing in this Agreement shall constitute a waiver of Applitools’ Intellectual Property Rights under any law, or be in any way construed or interpreted as such. It is further agreed that to the extent Licensee provides Applitools with Feedback, Licensee acknowledges that any and all rights, including Intellectual Property Rights in such Feedback shall belong exclusively to Applitools and Licensee hereby irrevocably and unconditionally transfers and assigns to Applitools all intellectual property rights in such Feedback and waives any and all moral rights that Licensee may have in respect thereto. It is further understood that use of Feedback, if any, may be made by Applitools at its sole discretion, and that Applitools in no way shall be obliged to make use of any kind of the Feedback or part thereof.
|
|
18
|
+
1. Warranty. Applitools warrants that to its knowledge it has the right to grant the license under this Agreement. Applitools’ sole liability for any breach of this warranty or any other warranty under this Agreement shall be, at Applitools’ sole discretion: (i) to replace or repair the Software or the applicable portion thereof; or (ii) to terminate this Agreement.
|
|
19
|
+
2. Warranty Exclusions. The warranties set forth in Section 4 are contingent upon Licensee’s proper use of the Software, and shall not apply to damage caused by abuse, misuse, alteration, neglect or unauthorized repair or installation, or by the use or attempted use of Software other than that supplied and supported by Applitools. Applitools will use reasonable commercial efforts to repair or replace the Software or the applicable portion thereof, pursuant to the foregoing warranty within thirty (30) days of being so notified.
|
|
20
|
+
1. Warranty Disclaimers. AS BETWEEN LICENSEE AND APPLITOOLS, EXCEPT AS SET IN SECTION 4, THE SOFTWARE IS PROVIDED ON AN “AS IS” AND “AS AVAILABLE” BASIS WITHOUT WARRANTIES OF ANY KIND INCLUDING, WITHOUT LIMITATION, REPRESENTATIONS, WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR INTENDED OR PARTICULAR PURPOSE, TITLE, NON-INFRINGEMENT, THAT THE SOFTWARE WILL MEET LICENSEE’S REQUIREMENTS OR EXPECTATIONS OR WILL ACHIEVE ANY SPECIFIC RESULTS AND THOSE ARISING BY STATUTE OR FROM A COURSE OF DEALING OR USAGE OF TRADE. Applicable law may not allow the exclusion of certain warranties, so to that extent such exclusions may not apply.
|
|
21
|
+
1. High Risk Activities. You acknowledge that the Software is not fault tolerant and is not designed, manufactured, or intended for use or resale as on-line control equipment in hazardous or high risk environments and activities requiring fail-safe performance (such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic control, direct life support machines and/or devices, or weapons systems) in which the failure of the Software could lead directly to death, personal injury, or severe physical or environmental damage, and You agree not to use or allow the use of the Software or any portion thereof for, or in connection with, any such environment or activity.
|
|
22
|
+
1. Indemnify. You agree that Applitools shall have no liability whatsoever for any use made of the Software by You or any third party. You hereby agree to defend, indemnify and hold harmless Applitools and its affiliates and their respective officers, directors, agents and employees from any and all claims, damages, liabilities, costs, and expenses (including attorney’s fees) arising from claims related to Your use of the Software as well as from Your failure to comply with this Agreement.
|
|
23
|
+
1. Limitation of Liability. UNDER NO CIRCUMSTANCES SHALL APPLITOOLS AND/OR ITS AFFILIATES BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL DAMAGES, OR FOR ANY LOSS OF DATA, REVENUE, BUSINESS OR REPUTATION, THAT ARISES UNDER OR IN CONNECTION WITH THIS AGREEMENT, OR THAT RESULTS FROM THE USE OF, OR THE INABILITY TO USE, THE SOFTWARE. APPLITOOLS’S TOTAL AGGREGATE LIABILITY FOR ANY AND ALL DIRECT DAMAGES AND LOSSES THAT ARISE UNDER OR IN CONNECTION WITH THIS AGREEMENT SHALL NOT IN ANY CIRCUMSTANCE EXCEED THE AMOUNT OF 100.00 (ONE HUNDRED) UNITED STATES DOLLARS. THE FOREGOING LIMITATIONS AND EXCLUSIONS IN THIS SECTION 7 SHALL APPLY: (I) EVEN IF APPLITOOLS HAS BEEN ADVISED OF THE POSSIBILITY OF ANY DAMAGES OR LOSSES; (II) EVEN IF ANY REMEDY SET FORTH HEREIN FAILS OF ITS ESSENTIAL PURPOSE; AND (III) REGARDLESS OF THE BASIS OR THEORY OF LIABILITY.
|
|
24
|
+
1. Export Laws. Licensee agrees to comply fully with all U.S., EU, Israeli, and all applicable export laws and regulations to ensure that neither the Software nor any technical data related thereto are exported or re-exported directly or indirectly in violation of, or used for any purposes prohibited by, such laws and regulations. For clarity, and without derogating from Section 11 below, in case of any change of any applicable law, policy or regulation, which might affect Applitools’ business, Applitools will have the right to terminate this Agreement and the license granted hereunder and the Licensee shall have no claims regarding such termination.
|
|
25
|
+
1. Term and Termination. This Agreement shall continue until terminated as set forth in this section (the “Term”). You may terminate this Agreement at any time by removing the Software from Your system and destroying all copies of the Software and Documentation relating to the Software. Unauthorized copying of the Software or otherwise failing to comply with this Agreement will result in automatic immediate termination of this Agreement and will make available to Applitools legal remedies. Applitools reserves the right to terminate this Agreement and the License at any time and without notice. Upon termination of this Agreement, the License will terminate and You: (i) will cease any and all rights to use the Software, and (ii) will remove the Software from all hard drives, networks and other storage media and destroy all copies of the Software in your possession or under your control. The provisions of Sections 2.3, 2.4, 2.5, 6, 7, 8, 9, 10, 11 and 12 shall survive the termination, expiration or other ending of this Agreement.
|
|
26
|
+
1. Miscellaneous. This Agreement represents the complete agreement concerning the Software between You and Applitools and supersedes all prior agreements and representations between You and Applitools. If any provision of this Agreement is held to be unenforceable for any reason, such provision shall be reformed only to the extent necessary to make it enforceable. Any waiver of any provision of this Agreement will be effective only if in writing and signed by Applitools. This Agreement is personal to You and may not be assigned or transferred for any reason whatsoever without the consent of Applitools and any action or conduct in violation of the foregoing shall be void and without effect. Applitools expressly reserves the right to assign this Agreement and to delegate any of its obligations hereunder. This Agreement are governed by and construed under the laws of the State of Israel, excluding its conflicts of law rules. You expressly agree that the exclusive jurisdiction for any claim or action arising out of or relating to this Agreement shall be the courts located in Tel Aviv, Israel, and You further agree and submit to the exercise of personal jurisdiction of such courts for the purpose of litigating any such claim or action. In any action or proceeding to enforce rights under this Agreement, the prevailing party shall be entitled to recover costs and attorneys’ fees.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.fromBmpBuffer = exports.extractBmpSize = exports.isBmpBuffer = void 0;
|
|
27
|
+
const bmp = __importStar(require("bmpimagejs"));
|
|
28
|
+
function isBmpBuffer(buffer) {
|
|
29
|
+
return new TextDecoder().decode(buffer.subarray(0, 2)) === 'BM';
|
|
30
|
+
}
|
|
31
|
+
exports.isBmpBuffer = isBmpBuffer;
|
|
32
|
+
function extractBmpSize(buffer) {
|
|
33
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
34
|
+
return { width: view.getUint32(18, true), height: view.getUint32(22, true) };
|
|
35
|
+
}
|
|
36
|
+
exports.extractBmpSize = extractBmpSize;
|
|
37
|
+
async function fromBmpBuffer(buffer) {
|
|
38
|
+
const image = bmp.decode(buffer);
|
|
39
|
+
return {
|
|
40
|
+
data: new Uint8Array(image.pixels.buffer, image.pixels.byteOffset, image.pixels.byteLength),
|
|
41
|
+
width: image.width,
|
|
42
|
+
height: image.height,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
exports.fromBmpBuffer = fromBmpBuffer;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.freezeGif = exports.fromGifBuffer = exports.extractGifSize = exports.isGifBuffer = void 0;
|
|
27
|
+
const gif = __importStar(require("omggif"));
|
|
28
|
+
function isGifBuffer(buffer) {
|
|
29
|
+
return ['GIF87a', 'GIF89a'].includes(new TextDecoder().decode(buffer.subarray(0, 6)));
|
|
30
|
+
}
|
|
31
|
+
exports.isGifBuffer = isGifBuffer;
|
|
32
|
+
function extractGifSize(buffer) {
|
|
33
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
34
|
+
return { width: view.getUint16(6, true), height: view.getUint16(8, true) };
|
|
35
|
+
}
|
|
36
|
+
exports.extractGifSize = extractGifSize;
|
|
37
|
+
async function fromGifBuffer(buffer) {
|
|
38
|
+
const reader = new gif.GifReader(buffer);
|
|
39
|
+
const data = new Uint8Array(reader.width * reader.height * 4).fill(0);
|
|
40
|
+
reader.decodeAndBlitFrameRGBA(0, data);
|
|
41
|
+
return { width: reader.width, height: reader.height, data };
|
|
42
|
+
}
|
|
43
|
+
exports.fromGifBuffer = fromGifBuffer;
|
|
44
|
+
async function freezeGif(buffer) {
|
|
45
|
+
const reader = new gif.GifReader(buffer);
|
|
46
|
+
if (reader.numFrames() === 0)
|
|
47
|
+
return buffer;
|
|
48
|
+
const frame = reader.frameInfo(0);
|
|
49
|
+
return buffer.subarray(0, frame.data_offset + frame.data_length);
|
|
50
|
+
}
|
|
51
|
+
exports.freezeGif = freezeGif;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.fromJpegBuffer = exports.extractJpegSize = exports.isJpegBuffer = void 0;
|
|
27
|
+
const jpeg = __importStar(require("jpeg-js"));
|
|
28
|
+
function isJpegBuffer(buffer) {
|
|
29
|
+
return ['JFIF', 'Exif'].includes(new TextDecoder().decode(buffer.subarray(6, 10)));
|
|
30
|
+
}
|
|
31
|
+
exports.isJpegBuffer = isJpegBuffer;
|
|
32
|
+
function extractJpegSize(buffer) {
|
|
33
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
34
|
+
// skip file signature
|
|
35
|
+
let offset = 4;
|
|
36
|
+
while (view.byteLength > offset) {
|
|
37
|
+
// extract length of the block
|
|
38
|
+
offset += view.getUint16(offset);
|
|
39
|
+
// if next segment is SOF0 or SOF2 extract size
|
|
40
|
+
if (view.getUint8(offset + 1) === 0xc0 || view.getUint8(offset + 1) === 0xc2) {
|
|
41
|
+
return { width: view.getUint16(offset + 7), height: view.getUint16(offset + 5) };
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// skip block signature
|
|
45
|
+
offset += 2;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { width: 0, height: 0 };
|
|
49
|
+
}
|
|
50
|
+
exports.extractJpegSize = extractJpegSize;
|
|
51
|
+
async function fromJpegBuffer(buffer) {
|
|
52
|
+
return jpeg.decode(buffer, { tolerantDecoding: true, formatAsRGBA: true });
|
|
53
|
+
}
|
|
54
|
+
exports.fromJpegBuffer = fromJpegBuffer;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toPngSharp = exports.fromPngBufferSharp = void 0;
|
|
7
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
8
|
+
const buffer_1 = require("buffer");
|
|
9
|
+
async function fromPngBufferSharp(buffer) {
|
|
10
|
+
// Ensure we always get RGBA (4 channels) output for consistency
|
|
11
|
+
const { data, info } = await (0, sharp_1.default)(buffer_1.Buffer.from(buffer))
|
|
12
|
+
.ensureAlpha() // Force RGBA output
|
|
13
|
+
.raw()
|
|
14
|
+
.toBuffer({ resolveWithObject: true });
|
|
15
|
+
return {
|
|
16
|
+
width: info.width,
|
|
17
|
+
height: info.height,
|
|
18
|
+
data: data,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.fromPngBufferSharp = fromPngBufferSharp;
|
|
22
|
+
async function toPngSharp(image) {
|
|
23
|
+
// Calculate channels from data size (only support RGB=3 and RGBA=4)
|
|
24
|
+
const calculatedChannels = image.data.length / (image.width * image.height);
|
|
25
|
+
const channels = Math.round(calculatedChannels);
|
|
26
|
+
// Validate channels and data size
|
|
27
|
+
if (![3, 4].includes(channels) || Math.abs(channels - calculatedChannels) > 0.01) {
|
|
28
|
+
throw new Error(`Invalid image data: expected ${image.width}x${image.height} with RGB (3) or RGBA (4) channels, ` +
|
|
29
|
+
`got ${image.data.length} bytes (${calculatedChannels.toFixed(2)} channels)`);
|
|
30
|
+
}
|
|
31
|
+
return await (0, sharp_1.default)(buffer_1.Buffer.from(image.data), {
|
|
32
|
+
raw: {
|
|
33
|
+
width: image.width,
|
|
34
|
+
height: image.height,
|
|
35
|
+
channels,
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
.png({
|
|
39
|
+
compressionLevel: 9,
|
|
40
|
+
adaptiveFiltering: false,
|
|
41
|
+
})
|
|
42
|
+
.toBuffer();
|
|
43
|
+
}
|
|
44
|
+
exports.toPngSharp = toPngSharp;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.generateWhitePng = exports.toPng = exports.fromPngBuffer = exports.extractPngSize = exports.isPngBuffer = void 0;
|
|
27
|
+
const buffer_1 = require("buffer");
|
|
28
|
+
const png = __importStar(require("png-async"));
|
|
29
|
+
const utils = __importStar(require("@applitools/utils"));
|
|
30
|
+
function isPngBuffer(buffer) {
|
|
31
|
+
return new TextDecoder().decode(buffer.subarray(12, 16)) === 'IHDR';
|
|
32
|
+
}
|
|
33
|
+
exports.isPngBuffer = isPngBuffer;
|
|
34
|
+
function extractPngSize(buffer) {
|
|
35
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
36
|
+
return { width: view.getUint32(16), height: view.getUint32(20) };
|
|
37
|
+
}
|
|
38
|
+
exports.extractPngSize = extractPngSize;
|
|
39
|
+
/**
|
|
40
|
+
* Dynamically import png-sharp module
|
|
41
|
+
* Tries .js first (production), then .ts (ts-node/dev)
|
|
42
|
+
*/
|
|
43
|
+
async function importPngSharp() {
|
|
44
|
+
try {
|
|
45
|
+
return await import('./png-sharp.js');
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
// In ts-node, .js imports fail, try .ts instead
|
|
49
|
+
// Use dynamic string to bypass TypeScript's static import checking
|
|
50
|
+
const tsPath = './png-sharp' + '.ts';
|
|
51
|
+
return await import(tsPath);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Decode PNG buffer to ImageRaw
|
|
56
|
+
* Tries sharp first (faster), falls back to png-async if sharp fails
|
|
57
|
+
*/
|
|
58
|
+
async function fromPngBuffer(buffer, defaultLogger) {
|
|
59
|
+
const logger = defaultLogger === null || defaultLogger === void 0 ? void 0 : defaultLogger.extend({ label: 'image/png' });
|
|
60
|
+
try {
|
|
61
|
+
const pngSharp = await importPngSharp();
|
|
62
|
+
const result = await pngSharp.fromPngBufferSharp(buffer);
|
|
63
|
+
logger === null || logger === void 0 ? void 0 : logger.log('Used Sharp for PNG decoding');
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
// Fallback to png-async if sharp fails (SEA, not installed, or runtime error)
|
|
68
|
+
logger === null || logger === void 0 ? void 0 : logger.log('Falling back to png-async for PNG decoding:', error instanceof Error ? error.message : String(error));
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const image = new png.Image();
|
|
71
|
+
image.parse(buffer_1.Buffer.from(buffer), (err, image) => {
|
|
72
|
+
if (err)
|
|
73
|
+
return reject(err);
|
|
74
|
+
resolve(image);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.fromPngBuffer = fromPngBuffer;
|
|
80
|
+
/**
|
|
81
|
+
* Encode ImageRaw to PNG buffer
|
|
82
|
+
* Tries sharp first (22× faster), falls back to png-async if sharp fails
|
|
83
|
+
*/
|
|
84
|
+
async function toPng(image, defaultLogger) {
|
|
85
|
+
const logger = defaultLogger === null || defaultLogger === void 0 ? void 0 : defaultLogger.extend({ label: 'image/png' });
|
|
86
|
+
try {
|
|
87
|
+
const pngSharp = await importPngSharp();
|
|
88
|
+
const result = await pngSharp.toPngSharp(image);
|
|
89
|
+
logger === null || logger === void 0 ? void 0 : logger.log('Used Sharp for PNG encoding');
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
// Fallback to png-async if sharp fails (SEA, not installed, or runtime error)
|
|
94
|
+
// Uses RLE strategy (default) for optimal performance (~35ms)
|
|
95
|
+
logger === null || logger === void 0 ? void 0 : logger.log('Falling back to png-async for PNG encoding:', error instanceof Error ? error.message : String(error));
|
|
96
|
+
const expectedSize = image.width * image.height * 4;
|
|
97
|
+
const actualSize = image.data.length;
|
|
98
|
+
if (actualSize !== expectedSize) {
|
|
99
|
+
throw new Error(`Image data size mismatch: expected ${expectedSize} bytes for ${image.width}x${image.height}, got ${actualSize} bytes`);
|
|
100
|
+
}
|
|
101
|
+
const wrapper = new png.Image({ width: image.width, height: image.height });
|
|
102
|
+
wrapper.data = buffer_1.Buffer.from(image.data);
|
|
103
|
+
return utils.streams.toBuffer(wrapper.pack());
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.toPng = toPng;
|
|
107
|
+
// A blank placeholder image sized to a viewport (e.g. a design-comparison step that has only a size, not a design).
|
|
108
|
+
async function generateWhitePng(width, height) {
|
|
109
|
+
const image = new png.Image({ width, height });
|
|
110
|
+
image.data = buffer_1.Buffer.alloc(width * height * 4, 0xff); // opaque white RGBA
|
|
111
|
+
return utils.streams.toBuffer(image.pack());
|
|
112
|
+
}
|
|
113
|
+
exports.generateWhitePng = generateWhitePng;
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.makeImage = void 0;
|
|
30
|
+
const png_1 = require("./formats/png");
|
|
31
|
+
const jpeg_1 = require("./formats/jpeg");
|
|
32
|
+
const bmp_1 = require("./formats/bmp");
|
|
33
|
+
const gif_1 = require("./formats/gif");
|
|
34
|
+
const buffer_1 = require("buffer");
|
|
35
|
+
const fs_1 = __importDefault(require("fs"));
|
|
36
|
+
const path_1 = __importDefault(require("path"));
|
|
37
|
+
const utils = __importStar(require("@applitools/utils"));
|
|
38
|
+
function makeImage(data, defaultLogger) {
|
|
39
|
+
let image, size;
|
|
40
|
+
let transforms = { rotate: 0, scale: 1, crop: undefined, modifiers: [] };
|
|
41
|
+
const logger = defaultLogger === null || defaultLogger === void 0 ? void 0 : defaultLogger.extend({ label: 'image' });
|
|
42
|
+
if (utils.types.isUint8Array(data) || utils.types.isAnyArrayBuffer(data)) {
|
|
43
|
+
const buffer = utils.types.isAnyArrayBuffer(data) ? new Uint8Array(data) : data;
|
|
44
|
+
if ((0, png_1.isPngBuffer)(buffer)) {
|
|
45
|
+
image = (0, png_1.fromPngBuffer)(buffer, logger);
|
|
46
|
+
size = (0, png_1.extractPngSize)(buffer);
|
|
47
|
+
}
|
|
48
|
+
else if ((0, jpeg_1.isJpegBuffer)(buffer)) {
|
|
49
|
+
image = (0, jpeg_1.fromJpegBuffer)(buffer);
|
|
50
|
+
size = (0, jpeg_1.extractJpegSize)(buffer);
|
|
51
|
+
}
|
|
52
|
+
else if ((0, bmp_1.isBmpBuffer)(buffer)) {
|
|
53
|
+
image = (0, bmp_1.fromBmpBuffer)(buffer);
|
|
54
|
+
size = (0, bmp_1.extractBmpSize)(buffer);
|
|
55
|
+
}
|
|
56
|
+
else if ((0, gif_1.isGifBuffer)(buffer)) {
|
|
57
|
+
image = (0, gif_1.fromGifBuffer)(buffer);
|
|
58
|
+
size = (0, gif_1.extractGifSize)(buffer);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw new Error('Unable to create an image abstraction from buffer with unknown data');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else if (utils.types.isBase64(data)) {
|
|
65
|
+
return makeImage(buffer_1.Buffer.from(data, 'base64'));
|
|
66
|
+
}
|
|
67
|
+
else if (utils.types.isString(data)) {
|
|
68
|
+
return makeImage(fs_1.default.readFileSync(data));
|
|
69
|
+
}
|
|
70
|
+
else if (utils.types.has(data, 'isImage')) {
|
|
71
|
+
transforms = data.transforms;
|
|
72
|
+
image = data.toRaw();
|
|
73
|
+
size = data.rawSize;
|
|
74
|
+
}
|
|
75
|
+
else if (utils.types.has(data, ['width', 'height'])) {
|
|
76
|
+
image = fromSize(data);
|
|
77
|
+
if (utils.types.has(data, 'data') && data.data)
|
|
78
|
+
image.data = data.data;
|
|
79
|
+
size = { width: data.width, height: data.height };
|
|
80
|
+
}
|
|
81
|
+
else if (data.auto) {
|
|
82
|
+
size = { width: -1, height: -1 };
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
throw new Error('Unable to create an image abstraction from unknown data');
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
get isImage() {
|
|
89
|
+
return true;
|
|
90
|
+
},
|
|
91
|
+
get size() {
|
|
92
|
+
var _a;
|
|
93
|
+
const croppedSize = utils.geometry.size((_a = transforms.crop) !== null && _a !== void 0 ? _a : size);
|
|
94
|
+
const scaledSize = utils.geometry.scale(croppedSize, transforms.scale);
|
|
95
|
+
const rotatedSize = utils.geometry.rotate(scaledSize, transforms.rotate);
|
|
96
|
+
return utils.geometry.ceil(rotatedSize);
|
|
97
|
+
},
|
|
98
|
+
get unscaledSize() {
|
|
99
|
+
var _a;
|
|
100
|
+
const croppedSize = utils.geometry.size((_a = transforms.crop) !== null && _a !== void 0 ? _a : size);
|
|
101
|
+
const rotatedSize = utils.geometry.rotate(croppedSize, transforms.rotate);
|
|
102
|
+
return utils.geometry.ceil(rotatedSize);
|
|
103
|
+
},
|
|
104
|
+
get rawSize() {
|
|
105
|
+
return size;
|
|
106
|
+
},
|
|
107
|
+
get transforms() {
|
|
108
|
+
return { ...transforms };
|
|
109
|
+
},
|
|
110
|
+
get width() {
|
|
111
|
+
return this.size.width;
|
|
112
|
+
},
|
|
113
|
+
get height() {
|
|
114
|
+
return this.size.height;
|
|
115
|
+
},
|
|
116
|
+
scale(ratio) {
|
|
117
|
+
transforms.scale *= ratio;
|
|
118
|
+
return this;
|
|
119
|
+
},
|
|
120
|
+
rotate(degrees) {
|
|
121
|
+
transforms.rotate = (transforms.rotate + degrees) % 360;
|
|
122
|
+
return this;
|
|
123
|
+
},
|
|
124
|
+
crop(region) {
|
|
125
|
+
if (utils.types.has(region, ['left', 'right', 'top', 'bottom'])) {
|
|
126
|
+
region = {
|
|
127
|
+
x: region.left / transforms.scale,
|
|
128
|
+
y: region.top / transforms.scale,
|
|
129
|
+
width: size.width - (region.left + region.right) / transforms.scale,
|
|
130
|
+
height: size.height - (region.top + region.bottom) / transforms.scale,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
region = utils.geometry.scale(region, 1 / transforms.scale);
|
|
135
|
+
}
|
|
136
|
+
region = utils.geometry.rotate(region, -transforms.rotate, utils.geometry.rotate(size, transforms.rotate));
|
|
137
|
+
region = transforms.crop
|
|
138
|
+
? utils.geometry.intersect(transforms.crop, utils.geometry.offset(region, transforms.crop))
|
|
139
|
+
: utils.geometry.intersect({ x: 0, y: 0, ...size }, region);
|
|
140
|
+
transforms.crop = region;
|
|
141
|
+
size = utils.geometry.size(region);
|
|
142
|
+
return this;
|
|
143
|
+
},
|
|
144
|
+
copy(srcImage, offset) {
|
|
145
|
+
// if "auto" image and this is first chunk
|
|
146
|
+
if (!image && size.width === -1 && size.height === -1)
|
|
147
|
+
transforms.scale = srcImage.transforms.scale;
|
|
148
|
+
const unscaledOffset = utils.geometry.scale(offset, 1 / transforms.scale);
|
|
149
|
+
if (!image) {
|
|
150
|
+
size = {
|
|
151
|
+
width: Math.max(Math.floor(unscaledOffset.x) + srcImage.unscaledSize.width, size.width),
|
|
152
|
+
height: Math.max(Math.floor(unscaledOffset.y) + srcImage.unscaledSize.height, size.height),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const unscale = srcImage.transforms.scale === transforms.scale
|
|
156
|
+
? 1 / srcImage.transforms.scale
|
|
157
|
+
: srcImage.transforms.scale / transforms.scale;
|
|
158
|
+
transforms.modifiers.push({ type: 'copy', image: srcImage.scale(unscale).toObject(), offset: unscaledOffset });
|
|
159
|
+
return this;
|
|
160
|
+
},
|
|
161
|
+
frame(topImage, bottomImage, region) {
|
|
162
|
+
const prevSize = size;
|
|
163
|
+
const unscaledRegion = utils.geometry.scale(region, 1 / topImage.transforms.scale);
|
|
164
|
+
size = {
|
|
165
|
+
width: Math.floor(topImage.unscaledSize.width + Math.max(size.width - unscaledRegion.width, 0)),
|
|
166
|
+
height: Math.floor(topImage.unscaledSize.height + Math.max(size.height - unscaledRegion.height, 0)),
|
|
167
|
+
};
|
|
168
|
+
const unscale = topImage.transforms.scale === transforms.scale
|
|
169
|
+
? 1 / topImage.transforms.scale
|
|
170
|
+
: topImage.transforms.scale / transforms.scale;
|
|
171
|
+
transforms.modifiers.push({
|
|
172
|
+
type: 'frame',
|
|
173
|
+
top: topImage.scale(unscale).toObject(),
|
|
174
|
+
bottom: bottomImage.scale(unscale).toObject(),
|
|
175
|
+
region: unscaledRegion,
|
|
176
|
+
});
|
|
177
|
+
transforms.added = { width: size.width - prevSize.width, height: size.height - prevSize.height };
|
|
178
|
+
return this;
|
|
179
|
+
},
|
|
180
|
+
async toRaw() {
|
|
181
|
+
return image;
|
|
182
|
+
},
|
|
183
|
+
async toBuffer() {
|
|
184
|
+
const image = await this.toObject();
|
|
185
|
+
return image.data;
|
|
186
|
+
},
|
|
187
|
+
async toPng() {
|
|
188
|
+
return (0, png_1.toPng)(await this.toObject(), logger);
|
|
189
|
+
},
|
|
190
|
+
async toFile(path) {
|
|
191
|
+
return toFile(await image, path, logger);
|
|
192
|
+
},
|
|
193
|
+
async toObject() {
|
|
194
|
+
image = await transform(image ? await image : size, transforms);
|
|
195
|
+
size = { width: image.width, height: image.height };
|
|
196
|
+
transforms = { crop: undefined, scale: 1, rotate: 0, modifiers: [] };
|
|
197
|
+
return image;
|
|
198
|
+
},
|
|
199
|
+
async debug(debug) {
|
|
200
|
+
if (!debug || !debug.path)
|
|
201
|
+
return;
|
|
202
|
+
const timestamp = new Date().toISOString().replace(/[-T:.]/g, '_');
|
|
203
|
+
const filename = ['screenshot', timestamp, debug.name, debug.suffix].filter(part => part).join('_') + '.png';
|
|
204
|
+
const transformedImage = await transform(image ? await image : size, transforms);
|
|
205
|
+
return toFile(transformedImage, path_1.default.join(debug.path, filename), logger).catch(() => undefined);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
exports.makeImage = makeImage;
|
|
210
|
+
function fromSize(size) {
|
|
211
|
+
return { ...size, data: new Uint8Array(4 * size.width * size.height) };
|
|
212
|
+
}
|
|
213
|
+
async function toFile(image, filepath, logger) {
|
|
214
|
+
const buffer = await (0, png_1.toPng)(image, logger);
|
|
215
|
+
return new Promise((resolve, reject) => {
|
|
216
|
+
fs_1.default.mkdirSync(path_1.default.dirname(filepath), { recursive: true });
|
|
217
|
+
fs_1.default.writeFile(filepath, buffer, err => (err ? reject(err) : resolve()));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
async function transform(imageOrSize, transforms) {
|
|
221
|
+
let image;
|
|
222
|
+
if (utils.types.has(imageOrSize, 'data')) {
|
|
223
|
+
image = imageOrSize;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const size = transforms.added
|
|
227
|
+
? { width: imageOrSize.width - transforms.added.width, height: imageOrSize.height - transforms.added.height }
|
|
228
|
+
: imageOrSize;
|
|
229
|
+
image = fromSize(size);
|
|
230
|
+
}
|
|
231
|
+
image = await transforms.modifiers.reduce(async (image, modifier) => {
|
|
232
|
+
if (modifier.type === 'copy') {
|
|
233
|
+
return copy(await image, await modifier.image, modifier.offset);
|
|
234
|
+
}
|
|
235
|
+
else if (modifier.type === 'frame') {
|
|
236
|
+
return frame(await modifier.top, await modifier.bottom, await image, modifier.region);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
return image;
|
|
240
|
+
}
|
|
241
|
+
}, Promise.resolve(image));
|
|
242
|
+
image = transforms.crop ? await extract(image, transforms.crop) : image;
|
|
243
|
+
image = transforms.scale !== 1 ? await scale(image, transforms.scale) : image;
|
|
244
|
+
image = transforms.rotate !== 0 ? await rotate(image, transforms.rotate) : image;
|
|
245
|
+
return image;
|
|
246
|
+
}
|
|
247
|
+
async function scale(image, scaleRatio) {
|
|
248
|
+
if (scaleRatio === 1)
|
|
249
|
+
return image;
|
|
250
|
+
const ratio = image.height / image.width;
|
|
251
|
+
const scaledWidth = Math.ceil(image.width * scaleRatio);
|
|
252
|
+
const scaledHeight = Math.ceil(scaledWidth * ratio);
|
|
253
|
+
return resize(image, { width: scaledWidth, height: scaledHeight });
|
|
254
|
+
}
|
|
255
|
+
async function resize(image, size) {
|
|
256
|
+
const dst = fromSize(size);
|
|
257
|
+
if (dst.width > image.width || dst.height > image.height) {
|
|
258
|
+
_doBicubicInterpolation(image, dst);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
_scaleImageIncrementally(image, dst);
|
|
262
|
+
}
|
|
263
|
+
image.data = dst.data;
|
|
264
|
+
image.width = dst.width;
|
|
265
|
+
image.height = dst.height;
|
|
266
|
+
return image;
|
|
267
|
+
}
|
|
268
|
+
async function extract(image, region) {
|
|
269
|
+
const srcX = Math.max(0, Math.round(region.x));
|
|
270
|
+
const srcY = Math.max(0, Math.round(region.y));
|
|
271
|
+
const dstWidth = Math.round(Math.min(image.width - srcX, region.width));
|
|
272
|
+
const dstHeight = Math.round(Math.min(image.height - srcY, region.height));
|
|
273
|
+
const dstSize = { width: dstWidth, height: dstHeight };
|
|
274
|
+
if (utils.geometry.isEmpty(dstSize)) {
|
|
275
|
+
throw new Error(`Cannot extract empty region (${srcX};${srcY})${dstWidth}x${dstHeight} from image`);
|
|
276
|
+
}
|
|
277
|
+
const extracted = fromSize(dstSize);
|
|
278
|
+
if (srcX === 0 && dstWidth === image.width) {
|
|
279
|
+
const srcOffset = srcY * image.width * 4;
|
|
280
|
+
const dstLength = dstWidth * dstHeight * 4;
|
|
281
|
+
extracted.data.set(image.data.subarray(srcOffset, srcOffset + dstLength));
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const chunkLength = dstWidth * 4;
|
|
285
|
+
for (let chunk = 0; chunk < dstHeight; ++chunk) {
|
|
286
|
+
const srcOffset = ((srcY + chunk) * image.width + srcX) * 4;
|
|
287
|
+
extracted.data.set(image.data.subarray(srcOffset, srcOffset + chunkLength), chunk * chunkLength);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return extracted;
|
|
291
|
+
}
|
|
292
|
+
async function rotate(image, degrees) {
|
|
293
|
+
degrees = (360 + degrees) % 360;
|
|
294
|
+
const dstImage = fromSize({ width: image.width, height: image.height });
|
|
295
|
+
const srcView = new DataView(image.data.buffer, image.data.byteOffset, image.data.byteLength);
|
|
296
|
+
const dstView = new DataView(dstImage.data.buffer, dstImage.data.byteOffset, dstImage.data.byteLength);
|
|
297
|
+
if (degrees === 90) {
|
|
298
|
+
dstImage.width = image.height;
|
|
299
|
+
dstImage.height = image.width;
|
|
300
|
+
for (let srcY = 0, dstX = image.height - 1; srcY < image.height; ++srcY, --dstX) {
|
|
301
|
+
for (let srcX = 0, dstY = 0; srcX < image.width; ++srcX, ++dstY) {
|
|
302
|
+
const pixel = srcView.getUint32((srcY * image.width + srcX) * 4);
|
|
303
|
+
dstView.setUint32((dstY * dstImage.width + dstX) * 4, pixel);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else if (degrees === 180) {
|
|
308
|
+
dstImage.width = image.width;
|
|
309
|
+
dstImage.height = image.height;
|
|
310
|
+
for (let srcY = 0, dstY = image.height - 1; srcY < image.height; ++srcY, --dstY) {
|
|
311
|
+
for (let srcX = 0, dstX = image.width - 1; srcX < image.width; ++srcX, --dstX) {
|
|
312
|
+
const pixel = srcView.getUint32((srcY * image.width + srcX) * 4);
|
|
313
|
+
dstView.setUint32((dstY * dstImage.width + dstX) * 4, pixel);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else if (degrees === 270) {
|
|
318
|
+
dstImage.width = image.height;
|
|
319
|
+
dstImage.height = image.width;
|
|
320
|
+
for (let srcY = 0, dstX = 0; srcY < image.height; ++srcY, ++dstX) {
|
|
321
|
+
for (let srcX = 0, dstY = image.width - 1; srcX < image.width; ++srcX, --dstY) {
|
|
322
|
+
const pixel = srcView.getUint32((srcY * image.width + srcX) * 4);
|
|
323
|
+
dstView.setUint32((dstY * dstImage.width + dstX) * 4, pixel);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
dstImage.data.set(image.data);
|
|
329
|
+
}
|
|
330
|
+
return dstImage;
|
|
331
|
+
}
|
|
332
|
+
async function copy(dstImage, srcImage, offset) {
|
|
333
|
+
const dstX = Math.round(offset.x);
|
|
334
|
+
const dstY = Math.round(offset.y);
|
|
335
|
+
const srcWidth = Math.min(srcImage.width, dstImage.width - dstX);
|
|
336
|
+
const srcHeight = Math.min(srcImage.height, dstImage.height - dstY);
|
|
337
|
+
if (dstX === 0 && srcWidth === dstImage.width && srcWidth === srcImage.width) {
|
|
338
|
+
const dstOffset = dstY * dstImage.width * 4;
|
|
339
|
+
dstImage.data.set(srcImage.data.subarray(0, srcWidth * srcHeight * 4), dstOffset);
|
|
340
|
+
return dstImage;
|
|
341
|
+
}
|
|
342
|
+
const chunkLength = srcWidth * 4;
|
|
343
|
+
for (let chunk = 0; chunk < srcHeight; ++chunk) {
|
|
344
|
+
const srcOffset = chunk * srcImage.width * 4;
|
|
345
|
+
const dstOffset = ((dstY + chunk) * dstImage.width + dstX) * 4;
|
|
346
|
+
if (dstOffset >= 0) {
|
|
347
|
+
dstImage.data.set(srcImage.data.subarray(srcOffset, srcOffset + chunkLength), dstOffset);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return dstImage;
|
|
351
|
+
}
|
|
352
|
+
async function frame(topImage, bottomImage, srcImage, region) {
|
|
353
|
+
region = utils.geometry.intersect({ x: 0, y: 0, width: topImage.width, height: topImage.height }, utils.geometry.round(region));
|
|
354
|
+
if (region.x === 0 && region.y === 0 && region.width >= topImage.width && region.height >= topImage.height) {
|
|
355
|
+
return srcImage;
|
|
356
|
+
}
|
|
357
|
+
if (region.width >= srcImage.width && region.height >= srcImage.height) {
|
|
358
|
+
await copy(topImage, srcImage, { x: region.x, y: region.y });
|
|
359
|
+
return topImage;
|
|
360
|
+
}
|
|
361
|
+
const dstImage = fromSize({
|
|
362
|
+
width: topImage.width + Math.max(srcImage.width - region.width, 0),
|
|
363
|
+
height: topImage.height + Math.max(srcImage.height - region.height, 0),
|
|
364
|
+
});
|
|
365
|
+
if (region.width === srcImage.width) {
|
|
366
|
+
const topExtImage = await extract(topImage, {
|
|
367
|
+
x: 0,
|
|
368
|
+
y: 0,
|
|
369
|
+
width: topImage.width,
|
|
370
|
+
height: region.y + region.height,
|
|
371
|
+
});
|
|
372
|
+
await copy(dstImage, topExtImage, { x: 0, y: 0 });
|
|
373
|
+
}
|
|
374
|
+
else if (region.height === srcImage.height) {
|
|
375
|
+
const leftExtImage = await extract(topImage, {
|
|
376
|
+
x: 0,
|
|
377
|
+
y: 0,
|
|
378
|
+
width: region.x + region.width,
|
|
379
|
+
height: topImage.height,
|
|
380
|
+
});
|
|
381
|
+
await copy(dstImage, leftExtImage, { x: 0, y: 0 });
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
const topLeftExtImage = await extract(topImage, {
|
|
385
|
+
x: 0,
|
|
386
|
+
y: 0,
|
|
387
|
+
width: region.x + region.width,
|
|
388
|
+
height: region.y + region.height,
|
|
389
|
+
});
|
|
390
|
+
await copy(dstImage, topLeftExtImage, { x: 0, y: 0 });
|
|
391
|
+
const rightExtImage = await extract(topImage, {
|
|
392
|
+
x: region.x + region.width,
|
|
393
|
+
y: 0,
|
|
394
|
+
width: topImage.width - (region.x + region.width),
|
|
395
|
+
height: region.y,
|
|
396
|
+
});
|
|
397
|
+
await copy(dstImage, rightExtImage, { x: region.x + region.width, y: 0 });
|
|
398
|
+
const bottomExtImage = await extract(topImage, {
|
|
399
|
+
x: 0,
|
|
400
|
+
y: region.y + region.height,
|
|
401
|
+
width: region.x,
|
|
402
|
+
height: topImage.height - (region.y + region.height),
|
|
403
|
+
});
|
|
404
|
+
await copy(dstImage, bottomExtImage, { x: 0, y: region.y + region.height });
|
|
405
|
+
}
|
|
406
|
+
if (bottomImage.height > region.y + region.height || bottomImage.width > region.x + region.width) {
|
|
407
|
+
// first image might be higher
|
|
408
|
+
const yDiff = topImage.height - bottomImage.height;
|
|
409
|
+
if (region.width === srcImage.width) {
|
|
410
|
+
const bottomExtImage = await extract(bottomImage, {
|
|
411
|
+
x: 0,
|
|
412
|
+
y: region.y - yDiff + region.height,
|
|
413
|
+
width: bottomImage.width,
|
|
414
|
+
height: bottomImage.height - (region.y - yDiff + region.height),
|
|
415
|
+
});
|
|
416
|
+
await copy(dstImage, bottomExtImage, { x: 0, y: region.y + Math.max(srcImage.height, region.height) });
|
|
417
|
+
}
|
|
418
|
+
else if (region.height === srcImage.height) {
|
|
419
|
+
const rightExtImage = await extract(bottomImage, {
|
|
420
|
+
x: region.x + region.width,
|
|
421
|
+
y: 0,
|
|
422
|
+
width: bottomImage.width - (region.x + region.width),
|
|
423
|
+
height: bottomImage.height,
|
|
424
|
+
});
|
|
425
|
+
await copy(dstImage, rightExtImage, { x: region.x + Math.max(srcImage.width, region.width), y: 0 });
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
const bottomRightExtImage = await extract(bottomImage, {
|
|
429
|
+
x: region.x,
|
|
430
|
+
y: region.y - yDiff,
|
|
431
|
+
width: bottomImage.width - region.x,
|
|
432
|
+
height: bottomImage.height - (region.y - yDiff),
|
|
433
|
+
});
|
|
434
|
+
await copy(dstImage, bottomRightExtImage, {
|
|
435
|
+
x: region.x + Math.max(srcImage.width - region.width, 0),
|
|
436
|
+
y: region.y + Math.max(srcImage.height - region.height, 0),
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
await copy(dstImage, srcImage, { x: region.x, y: region.y });
|
|
441
|
+
return dstImage;
|
|
442
|
+
}
|
|
443
|
+
function _interpolateCubic(x0, x1, x2, x3, t) {
|
|
444
|
+
const a0 = x3 - x2 - x0 + x1;
|
|
445
|
+
const a1 = x0 - x1 - a0;
|
|
446
|
+
const a2 = x2 - x0;
|
|
447
|
+
return Math.ceil(Math.max(0, Math.min(255, a0 * (t * t * t) + a1 * (t * t) + (a2 * t + x1))));
|
|
448
|
+
}
|
|
449
|
+
function _interpolateRows(bufSrc, wSrc, hSrc, wDst) {
|
|
450
|
+
const buf = new Uint8Array(wDst * hSrc * 4);
|
|
451
|
+
for (let i = 0; i < hSrc; i += 1) {
|
|
452
|
+
for (let j = 0; j < wDst; j += 1) {
|
|
453
|
+
const x = (j * (wSrc - 1)) / wDst;
|
|
454
|
+
const xPos = Math.floor(x);
|
|
455
|
+
const t = x - xPos;
|
|
456
|
+
const srcPos = (i * wSrc + xPos) * 4;
|
|
457
|
+
const buf1Pos = (i * wDst + j) * 4;
|
|
458
|
+
for (let k = 0; k < 4; k += 1) {
|
|
459
|
+
const kPos = srcPos + k;
|
|
460
|
+
const x0 = xPos > 0 ? bufSrc[kPos - 4] : 2 * bufSrc[kPos] - bufSrc[kPos + 4];
|
|
461
|
+
const x1 = bufSrc[kPos];
|
|
462
|
+
const x2 = bufSrc[kPos + 4];
|
|
463
|
+
const x3 = xPos < wSrc - 2 ? bufSrc[kPos + 8] : 2 * bufSrc[kPos + 4] - bufSrc[kPos];
|
|
464
|
+
buf[buf1Pos + k] = _interpolateCubic(x0, x1, x2, x3, t);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return buf;
|
|
469
|
+
}
|
|
470
|
+
function _interpolateColumns(bufSrc, hSrc, wDst, hDst) {
|
|
471
|
+
const buf = new Uint8Array(wDst * hDst * 4);
|
|
472
|
+
for (let i = 0; i < hDst; i += 1) {
|
|
473
|
+
for (let j = 0; j < wDst; j += 1) {
|
|
474
|
+
const y = (i * (hSrc - 1)) / hDst;
|
|
475
|
+
const yPos = Math.floor(y);
|
|
476
|
+
const t = y - yPos;
|
|
477
|
+
const buf1Pos = (yPos * wDst + j) * 4;
|
|
478
|
+
const buf2Pos = (i * wDst + j) * 4;
|
|
479
|
+
for (let k = 0; k < 4; k += 1) {
|
|
480
|
+
const kPos = buf1Pos + k;
|
|
481
|
+
const y0 = yPos > 0 ? bufSrc[kPos - wDst * 4] : 2 * bufSrc[kPos] - bufSrc[kPos + wDst * 4];
|
|
482
|
+
const y1 = bufSrc[kPos];
|
|
483
|
+
const y2 = bufSrc[kPos + wDst * 4];
|
|
484
|
+
const y3 = yPos < hSrc - 2 ? bufSrc[kPos + wDst * 8] : 2 * bufSrc[kPos + wDst * 4] - bufSrc[kPos];
|
|
485
|
+
buf[buf2Pos + k] = _interpolateCubic(y0, y1, y2, y3, t);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return buf;
|
|
490
|
+
}
|
|
491
|
+
function _interpolateScale(bufColumns, wDst, hDst, wDst2, m, wM, hM) {
|
|
492
|
+
const buf = new Uint8Array(wDst * hDst * 4);
|
|
493
|
+
for (let i = 0; i < hDst; i += 1) {
|
|
494
|
+
for (let j = 0; j < wDst; j += 1) {
|
|
495
|
+
let r = 0;
|
|
496
|
+
let g = 0;
|
|
497
|
+
let b = 0;
|
|
498
|
+
let a = 0;
|
|
499
|
+
let realColors = 0;
|
|
500
|
+
for (let y = 0; y < hM; y += 1) {
|
|
501
|
+
const yPos = i * hM + y;
|
|
502
|
+
for (let x = 0; x < wM; x += 1) {
|
|
503
|
+
const xPos = j * wM + x;
|
|
504
|
+
const xyPos = (yPos * wDst2 + xPos) * 4;
|
|
505
|
+
const pixelAlpha = bufColumns[xyPos + 3];
|
|
506
|
+
if (pixelAlpha) {
|
|
507
|
+
r += bufColumns[xyPos];
|
|
508
|
+
g += bufColumns[xyPos + 1];
|
|
509
|
+
b += bufColumns[xyPos + 2];
|
|
510
|
+
realColors += 1;
|
|
511
|
+
}
|
|
512
|
+
a += pixelAlpha;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
const pos = (i * wDst + j) * 4;
|
|
516
|
+
buf[pos] = realColors ? Math.round(r / realColors) : 0;
|
|
517
|
+
buf[pos + 1] = realColors ? Math.round(g / realColors) : 0;
|
|
518
|
+
buf[pos + 2] = realColors ? Math.round(b / realColors) : 0;
|
|
519
|
+
buf[pos + 3] = Math.round(a / m);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return buf;
|
|
523
|
+
}
|
|
524
|
+
function _doBicubicInterpolation(src, dst) {
|
|
525
|
+
// The implementation was taken from
|
|
526
|
+
// https://github.com/oliver-moran/jimp/blob/master/resize2.js
|
|
527
|
+
// when dst smaller than src/2, interpolate first to a multiple between 0.5 and 1.0 src, then sum squares
|
|
528
|
+
const wM = Math.max(1, Math.floor(src.width / dst.width));
|
|
529
|
+
const wDst2 = dst.width * wM;
|
|
530
|
+
const hM = Math.max(1, Math.floor(src.height / dst.height));
|
|
531
|
+
const hDst2 = dst.height * hM;
|
|
532
|
+
// Pass 1 - interpolate rows
|
|
533
|
+
// bufRows has width of dst2 and height of src
|
|
534
|
+
const bufRows = _interpolateRows(src.data, src.width, src.height, wDst2);
|
|
535
|
+
// Pass 2 - interpolate columns
|
|
536
|
+
// bufColumns has width and height of dst2
|
|
537
|
+
const bufColumns = _interpolateColumns(bufRows, src.height, wDst2, hDst2);
|
|
538
|
+
// Pass 3 - scale to dst
|
|
539
|
+
const m = wM * hM;
|
|
540
|
+
if (m > 1) {
|
|
541
|
+
dst.data = _interpolateScale(bufColumns, dst.width, dst.height, wDst2, m, wM, hM);
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
dst.data = bufColumns;
|
|
545
|
+
}
|
|
546
|
+
return dst;
|
|
547
|
+
}
|
|
548
|
+
function _scaleImageIncrementally(src, dst) {
|
|
549
|
+
let currentWidth = src.width;
|
|
550
|
+
let currentHeight = src.height;
|
|
551
|
+
const targetWidth = dst.width;
|
|
552
|
+
const targetHeight = dst.height;
|
|
553
|
+
dst.data = src.data;
|
|
554
|
+
dst.width = src.width;
|
|
555
|
+
dst.height = src.height;
|
|
556
|
+
// For ultra quality should use 7
|
|
557
|
+
const fraction = 2;
|
|
558
|
+
do {
|
|
559
|
+
const prevCurrentWidth = currentWidth;
|
|
560
|
+
const prevCurrentHeight = currentHeight;
|
|
561
|
+
// If the current width is bigger than our target, cut it in half and sample again.
|
|
562
|
+
if (currentWidth > targetWidth) {
|
|
563
|
+
currentWidth -= Math.floor(currentWidth / fraction);
|
|
564
|
+
// If we cut the width too far it means we are on our last iteration. Just set it to the target width
|
|
565
|
+
// and finish up.
|
|
566
|
+
if (currentWidth < targetWidth) {
|
|
567
|
+
currentWidth = targetWidth;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
// If the current height is bigger than our target, cut it in half and sample again.
|
|
571
|
+
if (currentHeight > targetHeight) {
|
|
572
|
+
currentHeight -= Math.floor(currentHeight / fraction);
|
|
573
|
+
// If we cut the height too far it means we are on our last iteration. Just set it to the target height
|
|
574
|
+
// and finish up.
|
|
575
|
+
if (currentHeight < targetHeight) {
|
|
576
|
+
currentHeight = targetHeight;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
// Stop when we cannot incrementally step down anymore.
|
|
580
|
+
if (prevCurrentWidth === currentWidth && prevCurrentHeight === currentHeight) {
|
|
581
|
+
return dst;
|
|
582
|
+
}
|
|
583
|
+
// Render the incremental scaled image.
|
|
584
|
+
const incrementalImage = fromSize({ width: currentWidth, height: currentHeight });
|
|
585
|
+
_doBicubicInterpolation(dst, incrementalImage);
|
|
586
|
+
// Now treat our incremental partially scaled image as the src image
|
|
587
|
+
// and cycle through our loop again to do another incremental scaling of it (if necessary).
|
|
588
|
+
dst.data = incrementalImage.data;
|
|
589
|
+
dst.width = incrementalImage.width;
|
|
590
|
+
dst.height = incrementalImage.height;
|
|
591
|
+
} while (currentWidth !== targetWidth || currentHeight !== targetHeight);
|
|
592
|
+
return dst;
|
|
593
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./image"), exports);
|
|
18
|
+
__exportStar(require("./formats/png"), exports);
|
|
19
|
+
__exportStar(require("./formats/jpeg"), exports);
|
|
20
|
+
__exportStar(require("./formats/bmp"), exports);
|
|
21
|
+
__exportStar(require("./formats/gif"), exports);
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ohos-ports/applitools-image",
|
|
3
|
+
"version": "1.3.2-beta.1",
|
|
4
|
+
"description": "Image primitive used for capturing and manipulating screenshots",
|
|
5
|
+
"homepage": "https://applitools.com",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/ohos-ports/ohos-ports.git",
|
|
12
|
+
"directory": "ports/applitools-image/1.3.2"
|
|
13
|
+
},
|
|
14
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Applitools Team",
|
|
17
|
+
"email": "team@applitools.com"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"types"
|
|
22
|
+
],
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./types/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./types/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./dist/*": "./dist/*.js",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"lint": "run --top-level eslint '**/*.ts'",
|
|
35
|
+
"build": "run --top-level tsc --project ./tsconfig.build.json",
|
|
36
|
+
"test": "run --top-level mocha './test/**/*.spec.ts'"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@applitools/logger": "2.3.1",
|
|
40
|
+
"@applitools/utils": "1.15.2",
|
|
41
|
+
"bmpimagejs": "1.0.4",
|
|
42
|
+
"jpeg-js": "0.4.4",
|
|
43
|
+
"omggif": "1.0.10",
|
|
44
|
+
"png-async": "0.9.4",
|
|
45
|
+
"@ohos-ports/sharp": "^0.34.5-beta.11"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@applitools/test-utils": "^1.5.17",
|
|
49
|
+
"@types/node": "^12.20.55",
|
|
50
|
+
"@types/omggif": "^1.0.3",
|
|
51
|
+
"@types/pixelmatch": "^5.2.4",
|
|
52
|
+
"pixelmatch": "^5.3.0"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=12.13.0"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"lavamoat": {
|
|
61
|
+
"allowScripts": {
|
|
62
|
+
"sharp": true
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ImageBuffer, ImageRaw } from '../types';
|
|
2
|
+
import type { Size } from '@applitools/utils';
|
|
3
|
+
export declare function isBmpBuffer(buffer: ImageBuffer): boolean;
|
|
4
|
+
export declare function extractBmpSize(buffer: ImageBuffer): Size;
|
|
5
|
+
export declare function fromBmpBuffer(buffer: ImageBuffer): Promise<ImageRaw>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ImageBuffer, ImageRaw } from '../types';
|
|
2
|
+
import type { Size } from '@applitools/utils';
|
|
3
|
+
export declare function isGifBuffer(buffer: ImageBuffer): boolean;
|
|
4
|
+
export declare function extractGifSize(buffer: ImageBuffer): Size;
|
|
5
|
+
export declare function fromGifBuffer(buffer: ImageBuffer): Promise<ImageRaw>;
|
|
6
|
+
export declare function freezeGif(buffer: ImageBuffer): Promise<ImageBuffer>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ImageBuffer, ImageRaw } from '../types';
|
|
2
|
+
import type { Size } from '@applitools/utils';
|
|
3
|
+
export declare function isJpegBuffer(buffer: ImageBuffer): boolean;
|
|
4
|
+
export declare function extractJpegSize(buffer: ImageBuffer): Size;
|
|
5
|
+
export declare function fromJpegBuffer(buffer: ImageBuffer): Promise<ImageRaw>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { ImageBuffer, ImageRaw } from '../types';
|
|
3
|
+
import type { Size } from '@applitools/utils';
|
|
4
|
+
import type { Logger } from '@applitools/logger';
|
|
5
|
+
export declare function isPngBuffer(buffer: ImageBuffer): boolean;
|
|
6
|
+
export declare function extractPngSize(buffer: ImageBuffer): Size;
|
|
7
|
+
/**
|
|
8
|
+
* Decode PNG buffer to ImageRaw
|
|
9
|
+
* Tries sharp first (faster), falls back to png-async if sharp fails
|
|
10
|
+
*/
|
|
11
|
+
export declare function fromPngBuffer(buffer: ImageBuffer, defaultLogger?: Logger): Promise<ImageRaw>;
|
|
12
|
+
/**
|
|
13
|
+
* Encode ImageRaw to PNG buffer
|
|
14
|
+
* Tries sharp first (22× faster), falls back to png-async if sharp fails
|
|
15
|
+
*/
|
|
16
|
+
export declare function toPng(image: ImageRaw, defaultLogger?: Logger): Promise<ImageBuffer>;
|
|
17
|
+
export declare function generateWhitePng(width: number, height: number): Promise<Buffer>;
|
package/types/image.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ImageBuffer, ImageRaw } from './types';
|
|
2
|
+
import type { Location, Size, Region } from '@applitools/utils';
|
|
3
|
+
import type { Logger } from '@applitools/logger';
|
|
4
|
+
export interface Image {
|
|
5
|
+
isImage: true;
|
|
6
|
+
size: Size;
|
|
7
|
+
unscaledSize: Size;
|
|
8
|
+
rawSize: Size;
|
|
9
|
+
transforms: Transforms;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
scale(ratio: number): Image;
|
|
13
|
+
rotate(degrees: number): Image;
|
|
14
|
+
crop(region: any): Image;
|
|
15
|
+
copy(srcImage: Image, offset: any): Image;
|
|
16
|
+
frame(topImage: Image, bottomImage: Image, region: any): Image;
|
|
17
|
+
toRaw(): Promise<ImageRaw>;
|
|
18
|
+
toBuffer(): Promise<ImageBuffer>;
|
|
19
|
+
toPng(): Promise<ImageBuffer>;
|
|
20
|
+
toFile(path: string): Promise<void>;
|
|
21
|
+
toObject(): Promise<ImageRaw>;
|
|
22
|
+
debug(debug: any): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
type ImageSource = string | ArrayBuffer | ImageBuffer | Image | (Size & {
|
|
25
|
+
data?: ImageBuffer;
|
|
26
|
+
}) | {
|
|
27
|
+
auto: true;
|
|
28
|
+
};
|
|
29
|
+
type Transforms = {
|
|
30
|
+
rotate: number;
|
|
31
|
+
scale: number;
|
|
32
|
+
crop?: Region;
|
|
33
|
+
modifiers: ({
|
|
34
|
+
type: 'copy';
|
|
35
|
+
image: Promise<ImageRaw>;
|
|
36
|
+
offset: Location;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'frame';
|
|
39
|
+
top: Promise<ImageRaw>;
|
|
40
|
+
bottom: Promise<ImageRaw>;
|
|
41
|
+
region: Region;
|
|
42
|
+
})[];
|
|
43
|
+
added?: any;
|
|
44
|
+
};
|
|
45
|
+
export declare function makeImage(data: ImageSource, defaultLogger?: Logger): Image;
|
|
46
|
+
export {};
|
package/types/index.d.ts
ADDED