@danielbiegler/vendure-plugin-blurry-image-lazy-loading
Version:
Generates image hashes for displaying blurry previews when loading images on the frontend.
88 lines (87 loc) • 4.34 kB
TypeScript
/**
* DISCLAIMER:
*
* All the functions inside this file were copied (and slightly modified) on the 2024-12-18 from: https://github.com/evanw/thumbhash
*
* The specific commit including the copied code: https://github.com/evanw/thumbhash/commit/d2e50a74ac26d1e2539c8b6160ea8828b5307de3
*
* Permalinks to the specific file version:
* - https://github.com/evanw/thumbhash/blob/a652ce6ed691242f459f468f0a8756cda3b90a82/js/thumbhash.js
* - https://github.com/evanw/thumbhash/blob/a652ce6ed691242f459f468f0a8756cda3b90a82/js/thumbhash.d.ts
*
* Modifications to the original code include:
* - Typescript type annotations and coercion
* - Code comments
* - Whitespace auto formatting
* - Removed throwing an error for images larger than 100x100px
*
* Now follows the original license and copyright notice at time of copying
*
* ========================================================================================================================================
*
* Copyright (c) 2023 Evan Wallace
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Encodes an RGBA image to a ThumbHash. RGB should not be premultiplied by A.
*
* @param w The width of the input image.
* @param h The height of the input image.
* @param rgba The pixels in the input image, row-by-row. Must have w*h*4 elements.
* @returns The ThumbHash as a Uint8Array.
*/
export declare function rgbaToThumbHash(w: number, h: number, rgba: ArrayLike<number>): Uint8Array;
/**
* Decodes a ThumbHash to an RGBA image. RGB is not be premultiplied by A.
*
* @param hash The bytes of the ThumbHash.
* @returns The width, height, and pixels of the rendered placeholder image.
*/
export declare function thumbHashToRGBA(hash: ArrayLike<number>): {
w: number;
h: number;
rgba: Uint8Array;
};
/**
* Extracts the average color from a ThumbHash. RGB is not be premultiplied by A.
*
* @param hash The bytes of the ThumbHash.
* @returns The RGBA values for the average color. Each value ranges from 0 to 1.
*/
export declare function thumbHashToAverageRGBA(hash: ArrayLike<number>): {
r: number;
g: number;
b: number;
a: number;
};
/**
* Extracts the approximate aspect ratio of the original image.
*
* @param hash The bytes of the ThumbHash.
* @returns The approximate aspect ratio (i.e. width / height).
*/
export declare function thumbHashToApproximateAspectRatio(hash: ArrayLike<number>): number;
/**
* Encodes an RGBA image to a PNG data URL. RGB should not be premultiplied by
* A. This is optimized for speed and simplicity and does not optimize for size
* at all. This doesn't do any compression (all values are stored uncompressed).
*
* @param w The width of the input image.
* @param h The height of the input image.
* @param rgba The pixels in the input image, row-by-row. Must have w*h*4 elements.
* @returns A data URL containing a PNG for the input image.
*/
export declare function rgbaToDataURL(w: number, h: number, rgba: ArrayLike<number>): string;
/**
* Decodes a ThumbHash to a PNG data URL. This is a convenience function that
* just calls "thumbHashToRGBA" followed by "rgbaToDataURL".
*
* @param hash The bytes of the ThumbHash.
* @returns A data URL containing a PNG for the rendered ThumbHash.
*/
export declare function thumbHashToDataURL(hash: ArrayLike<number>): string;