@vibrant/image
Advanced tools
Comparing version 3.2.1-alpha.1 to 4.0.0-alpha.1
{ | ||
"name": "@vibrant/image", | ||
"version": "3.2.1-alpha.1", | ||
"version": "4.0.0-alpha.1", | ||
"description": "Helpers and typings for writing a vibrant ImageClass", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "vite build" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"type": "module", | ||
"types": "dist/esm/index.d.ts", | ||
"main": "dist/cjs/index.cjs", | ||
"module": "dist/esm/index.js", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"author": { | ||
@@ -20,8 +40,10 @@ "name": "akfish", | ||
"dependencies": { | ||
"@vibrant/color": "^3.2.1-alpha.1", | ||
"@vibrant/types": "^3.2.1-alpha.1" | ||
"@vibrant/color": "^4.0.0-alpha.1", | ||
"@vibrant/types": "^4.0.0-alpha.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.12.18", | ||
"typescript": "^3.7.2" | ||
"@tanstack/config": "^0.14.2", | ||
"@types/node": "^14.14.31", | ||
"typescript": "^4.5.2", | ||
"vite": "^6.0.3" | ||
}, | ||
@@ -31,3 +53,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "e667d4c12e1de66e251d451710b093e9cb4630c2" | ||
"gitHead": "968ad565c625279b5d3e2baf3e5da7f764551ade" | ||
} |
@@ -1,78 +0,82 @@ | ||
import { Pixels } from './index' | ||
import { Pixels } from "./index"; | ||
export interface HistogramOptions { | ||
sigBits: number | ||
sigBits: number; | ||
} | ||
export default class Histogram { | ||
bmin: number | ||
bmax: number | ||
gmin: number | ||
gmax: number | ||
rmin: number | ||
rmax: number | ||
hist: Uint32Array | ||
private _colorCount: number | ||
get colorCount () { return this._colorCount } | ||
getColorIndex: (r: number, g: number, b: number) => number | ||
constructor (public pixels: Pixels, public opts: HistogramOptions) { | ||
const { sigBits } = opts | ||
export class Histogram { | ||
bmin: number; | ||
bmax: number; | ||
gmin: number; | ||
gmax: number; | ||
rmin: number; | ||
rmax: number; | ||
hist: Uint32Array; | ||
private _colorCount: number; | ||
get colorCount() { | ||
return this._colorCount; | ||
} | ||
getColorIndex: (r: number, g: number, b: number) => number; | ||
constructor(public pixels: Pixels, public opts: HistogramOptions) { | ||
const { sigBits } = opts; | ||
const getColorIndex = (r: number, g: number, b: number) => | ||
(r << (2 * sigBits)) + (g << sigBits) + b | ||
(r << (2 * sigBits)) + (g << sigBits) + b; | ||
this.getColorIndex = getColorIndex | ||
this.getColorIndex = getColorIndex; | ||
const rshift = 8 - sigBits | ||
const hn = 1 << (3 * sigBits) | ||
let hist = new Uint32Array(hn) | ||
let rmax: number | ||
let rmin: number | ||
let gmax: number | ||
let gmin: number | ||
let bmax: number | ||
let bmin: number | ||
let r: number | ||
let g: number | ||
let b: number | ||
let a: number | ||
rmax = gmax = bmax = 0 | ||
rmin = gmin = bmin = Number.MAX_VALUE | ||
let n = pixels.length / 4 | ||
let i = 0 | ||
const rshift = 8 - sigBits; | ||
const hn = 1 << (3 * sigBits); | ||
const hist = new Uint32Array(hn); | ||
let rmax: number; | ||
let rmin: number; | ||
let gmax: number; | ||
let gmin: number; | ||
let bmax: number; | ||
let bmin: number; | ||
let r: number; | ||
let g: number; | ||
let b: number; | ||
let a: number; | ||
rmax = gmax = bmax = 0; | ||
rmin = gmin = bmin = Number.MAX_VALUE; | ||
const n = pixels.length / 4; | ||
let i = 0; | ||
while (i < n) { | ||
const offset = i * 4; | ||
i++; | ||
r = pixels[offset + 0]; | ||
g = pixels[offset + 1]; | ||
b = pixels[offset + 2]; | ||
a = pixels[offset + 3]; | ||
let offset = i * 4 | ||
i++ | ||
r = pixels[offset + 0] | ||
g = pixels[offset + 1] | ||
b = pixels[offset + 2] | ||
a = pixels[offset + 3] | ||
// Ignored pixels' alpha is marked as 0 in filtering stage | ||
if (a === 0) continue | ||
if (a === 0) continue; | ||
r = r >> rshift | ||
g = g >> rshift | ||
b = b >> rshift | ||
r = r >> rshift; | ||
g = g >> rshift; | ||
b = b >> rshift; | ||
let index = getColorIndex(r, g, b) | ||
hist[index] += 1 | ||
const index = getColorIndex(r, g, b); | ||
hist[index] += 1; | ||
if (r > rmax) rmax = r | ||
if (r < rmin) rmin = r | ||
if (g > gmax) gmax = g | ||
if (g < gmin) gmin = g | ||
if (b > bmax) bmax = b | ||
if (b < bmin) bmin = b | ||
if (r > rmax) rmax = r; | ||
if (r < rmin) rmin = r; | ||
if (g > gmax) gmax = g; | ||
if (g < gmin) gmin = g; | ||
if (b > bmax) bmax = b; | ||
if (b < bmin) bmin = b; | ||
} | ||
this._colorCount = hist.reduce((total, c) => c > 0 ? total + 1 : total, 0) | ||
this.hist = hist | ||
this.rmax = rmax | ||
this.rmin = rmin | ||
this.gmax = gmax | ||
this.gmin = gmin | ||
this.bmax = bmax | ||
this.bmin = bmin | ||
this._colorCount = hist.reduce( | ||
(total, c) => (c > 0 ? total + 1 : total), | ||
0 | ||
); | ||
this.hist = hist; | ||
this.rmax = rmax; | ||
this.rmin = rmin; | ||
this.gmax = gmax; | ||
this.gmin = gmin; | ||
this.bmax = bmax; | ||
this.bmin = bmin; | ||
} | ||
} |
114
src/index.ts
@@ -1,85 +0,91 @@ | ||
import { Callback } from '@vibrant/types' | ||
import { Filter } from '@vibrant/color' | ||
export { Histogram } from "./histogram"; | ||
export type { HistogramOptions } from "./histogram"; | ||
import { Callback } from "@vibrant/types"; | ||
import { Filter } from "@vibrant/color"; | ||
export type ImageCallback = Callback<Image> | ||
export type ImageCallback = Callback<Image>; | ||
export type ImageSource = string | HTMLImageElement | Buffer | ||
export type ImageSource = string | HTMLImageElement | Buffer; | ||
export type Pixels = Uint8ClampedArray | Buffer | ||
export type Pixels = Uint8ClampedArray | Buffer; | ||
export interface ImageData { | ||
data: Pixels, | ||
width: number, | ||
height: number | ||
data: Pixels; | ||
width: number; | ||
height: number; | ||
} | ||
export interface ImageOptions { | ||
quality: number | ||
maxDimension: number | ||
quality: number; | ||
maxDimension: number; | ||
} | ||
export interface Image { | ||
load (image: ImageSource): Promise<Image> | ||
clear (): void | ||
update (imageData: ImageData): void | ||
getWidth (): number | ||
getHeight (): number | ||
resize (targetWidth: number, targetHeight: number, ratio: number): void | ||
getPixelCount (): number | ||
getImageData (): ImageData | ||
remove (): void | ||
scaleDown (opts: ImageOptions): void | ||
load(image: ImageSource): Promise<Image>; | ||
clear(): void; | ||
update(imageData: ImageData): void; | ||
getWidth(): number; | ||
getHeight(): number; | ||
resize(targetWidth: number, targetHeight: number, ratio: number): void; | ||
getPixelCount(): number; | ||
getImageData(): ImageData; | ||
remove(): void; | ||
scaleDown(opts: ImageOptions): void; | ||
} | ||
export interface ImageClass { | ||
new(): Image | ||
new (): Image; | ||
} | ||
export abstract class ImageBase implements Image { | ||
abstract load (image: ImageSource): Promise<ImageBase> | ||
abstract clear (): void | ||
abstract update (imageData: ImageData): void | ||
abstract getWidth (): number | ||
abstract getHeight (): number | ||
abstract resize (targetWidth: number, targetHeight: number, ratio: number): void | ||
abstract getPixelCount (): number | ||
abstract getImageData (): ImageData | ||
abstract remove (): void | ||
abstract load(image: ImageSource): Promise<ImageBase>; | ||
abstract clear(): void; | ||
abstract update(imageData: ImageData): void; | ||
abstract getWidth(): number; | ||
abstract getHeight(): number; | ||
abstract resize( | ||
targetWidth: number, | ||
targetHeight: number, | ||
ratio: number | ||
): void; | ||
abstract getPixelCount(): number; | ||
abstract getImageData(): ImageData; | ||
abstract remove(): void; | ||
scaleDown (opts: ImageOptions): void { | ||
let width: number = this.getWidth() | ||
let height: number = this.getHeight() | ||
scaleDown(opts: ImageOptions): void { | ||
const width: number = this.getWidth(); | ||
const height: number = this.getHeight(); | ||
let ratio: number = 1 | ||
let ratio = 1; | ||
if (opts.maxDimension > 0) { | ||
let maxSide: number = Math.max(width, height) | ||
if (maxSide > opts.maxDimension) ratio = opts.maxDimension / maxSide | ||
const maxSide: number = Math.max(width, height); | ||
if (maxSide > opts.maxDimension) ratio = opts.maxDimension / maxSide; | ||
} else { | ||
ratio = 1 / opts.quality | ||
ratio = 1 / opts.quality; | ||
} | ||
if (ratio < 1) this.resize(width * ratio, height * ratio, ratio) | ||
if (ratio < 1) this.resize(width * ratio, height * ratio, ratio); | ||
} | ||
} | ||
export function applyFilters (imageData: ImageData, filters: Filter[]) { | ||
export function applyFilters(imageData: ImageData, filters: Filter[]) { | ||
if (filters.length > 0) { | ||
let pixels = imageData.data | ||
let n = pixels.length / 4 | ||
let offset | ||
let r | ||
let g | ||
let b | ||
let a | ||
const pixels = imageData.data; | ||
const n = pixels.length / 4; | ||
let offset; | ||
let r; | ||
let g; | ||
let b; | ||
let a; | ||
for (let i = 0; i < n; i++) { | ||
offset = i * 4 | ||
r = pixels[offset + 0] | ||
g = pixels[offset + 1] | ||
b = pixels[offset + 2] | ||
a = pixels[offset + 3] | ||
offset = i * 4; | ||
r = pixels[offset + 0]; | ||
g = pixels[offset + 1]; | ||
b = pixels[offset + 2]; | ||
a = pixels[offset + 3]; | ||
// Mark ignored color | ||
for (let j = 0; j < filters.length; j++) { | ||
if (!filters[j](r, g, b, a)) { | ||
pixels[offset + 3] = 0 | ||
break | ||
pixels[offset + 3] = 0; | ||
break; | ||
} | ||
@@ -90,3 +96,3 @@ } | ||
return imageData | ||
return imageData; | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
28824
16
441
1
Yes
4
+ Added@vibrant/color@4.0.0(transitive)
+ Added@vibrant/types@4.0.0(transitive)
- Removed@vibrant/color@3.2.1-alpha.1(transitive)
- Removed@vibrant/types@3.2.1-alpha.1(transitive)