Skip to content

Commit

Permalink
chore: enhance funding and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Feb 14, 2025
1 parent dd5587b commit 50d058b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github: [stacksjs, chrisbbreuer]
open_collective: [stacksjs]
open_collective: stacksjs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ imgx serve ./public -p 3000
### Library Usage

```ts
import { process, generateSprite, analyzeImage } from '@stacksjs/imgx'
import { analyzeImage, generateSprite, process } from '@stacksjs/imgx'

// Basic optimization
await process({
Expand Down
4 changes: 2 additions & 2 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ async function processFile(input: string, output: string, options: ProcessOption
if (options.thumbhash) {
try {
const { hash, dataUrl } = await generateThumbHash(input)
const [width, height] = options.thumbhashSize.split('x').map(Number)
// const [width, height] = options.thumbhashSize.split('x').map(Number)

const thumbPath = `${output}.thumb.png`
const hashPath = `${output}.thumb.hash`
Expand All @@ -342,7 +342,7 @@ async function processFile(input: string, output: string, options: ProcessOption

console.log(`Generated ThumbHash: ${thumbPath}`)
}
catch (error) {
catch (error: any) {
console.error(`Error generating ThumbHash: ${error.message}`)
}
}
Expand Down
126 changes: 63 additions & 63 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Plugin } from 'vite'
import type { Compiler } from 'webpack'
import type { ProcessOptions } from './types'
import { Buffer } from 'node:buffer'
import { process } from './core'
import { debugLog } from './utils'

Expand Down
1 change: 1 addition & 0 deletions src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OptimizeResult, ProcessOptions } from './types'
import { Buffer } from 'node:buffer'
import { mkdir, readFile, stat, writeFile } from 'node:fs/promises'
import { dirname } from 'node:path'
import sharp from 'sharp'
Expand Down
6 changes: 2 additions & 4 deletions src/responsive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export async function generateResponsiveImages(
const outputPath = join(outputDir, filename)

await sharp(inputBuffer)
.resize(width)
[format]({ quality })
.resize(width)[format]({ quality })
.toFile(outputPath)

const { size } = await sharp(outputPath).metadata()
Expand Down Expand Up @@ -122,8 +121,7 @@ export async function generateImageSet(options: ImageSetOptions) {
const outputPath = join(outputDir, `${name}-${suffix}.${format}`)

await sharp(input)
.resize(size.width, size.height)
[format]({ quality })
.resize(size.width, size.height)[format]({ quality })
.toFile(outputPath)

results.push({
Expand Down
24 changes: 18 additions & 6 deletions src/thumbhash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export function rgbaToThumbHash(w: number, h: number, rgba: ArrayLike<number>):
const { PI, round, max, cos, abs } = Math

// Determine the average color
let avg_r = 0; let avg_g = 0; let avg_b = 0; let avg_a = 0
let avg_r = 0
let avg_g = 0
let avg_b = 0
let avg_a = 0
for (let i = 0, j = 0; i < w * h; i++, j += 4) {
const alpha = rgba[j + 3] / 255
avg_r += alpha / 255 * rgba[j]
Expand Down Expand Up @@ -54,8 +57,11 @@ export function rgbaToThumbHash(w: number, h: number, rgba: ArrayLike<number>):
}

// Encode using the DCT into DC (constant) and normalized AC (varying) terms
const encodeChannel = (channel, nx, ny) => {
let dc = 0; const ac = []; let scale = 0; const fx = []
const encodeChannel = (channel: any, nx: any, ny: any) => {
let dc = 0
const ac = []
let scale = 0
const fx = []
for (let cy = 0; cy < ny; cy++) {
for (let cx = 0; cx * ny < nx * (ny - cy); cx++) {
let f = 0
Expand Down Expand Up @@ -149,10 +155,15 @@ export function thumbHashToRGBA(hash: ArrayLike<number>): { w: number, h: number
const ratio = thumbHashToApproximateAspectRatio(hash)
const w = round(ratio > 1 ? 32 : 32 * ratio)
const h = round(ratio > 1 ? 32 / ratio : 32)
const rgba = new Uint8Array(w * h * 4); const fx = []; const fy = []
const rgba = new Uint8Array(w * h * 4)
const fx = []
const fy = []
for (let y = 0, i = 0; y < h; y++) {
for (let x = 0; x < w; x++, i += 4) {
let l = l_dc; let p = p_dc; let q = q_dc; let a = a_dc
let l = l_dc
let p = p_dc
let q = q_dc
let a = a_dc

// Precompute the coefficients
for (let cx = 0, n = max(lx, hasAlpha ? 5 : 3); cx < n; cx++)
Expand Down Expand Up @@ -312,7 +323,8 @@ export function rgbaToDataURL(w: number, h: number, rgba: ArrayLike<number>): st
-1609899400,
-1111625188,
]
let a = 1; let b = 0
let a = 1
let b = 0
for (let y = 0, i = 0, end = row - 1; y < h; y++, end += row - 1) {
bytes.push(y + 1 < h ? 0 : 1, row & 255, row >> 8, ~row & 255, (row >> 8) ^ 255, 0)
for (b = (b + a) % 65521; i < end; i++) {
Expand Down

0 comments on commit 50d058b

Please sign in to comment.