-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #520 from uploadcare/chore/image-shrink-tests
chore: testing setup for image shrink
- Loading branch information
Showing
24 changed files
with
2,935 additions
and
11,727 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
LICENSE | ||
src/test/snapshots | ||
coverage |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/image-shrink/src/test/helpers/getImageAttributes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { readMagickImage } from './readMagickImage' | ||
|
||
export const getImageAttributes = async (inputBlob: Blob) => { | ||
return readMagickImage(inputBlob, (image) => { | ||
return image.attributeNames.reduce((acc, name) => { | ||
return { | ||
...acc, | ||
[name]: image.getAttribute(name) | ||
} | ||
}, {}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const loadImageAsBlob = async ( | ||
moduleResolver: () => Promise<{ default: string }> | ||
) => { | ||
const imageUrl = await moduleResolver().then((module) => module.default) | ||
const response = await fetch(imageUrl) | ||
const buffer = await response.arrayBuffer() | ||
return new Blob([buffer], { | ||
type: response.headers.get('content-type') ?? 'application/octet-stream' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference types="vite/client" /> | ||
import { | ||
ImageMagick, | ||
Magick, | ||
MagickFormat, | ||
Quantum, | ||
initializeImageMagick | ||
} from '@imagemagick/magick-wasm' | ||
// eslint-disable-next-line import/no-unresolved | ||
import wasmUrl from '@imagemagick/magick-wasm/magick.wasm?url' | ||
|
||
export const loadImageMagick = async () => { | ||
const wasmBytes = await fetch(wasmUrl).then((res) => res.arrayBuffer()) | ||
await initializeImageMagick(wasmBytes) | ||
|
||
return { Magick, MagickFormat, Quantum, ImageMagick } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { loadImageMagick } from './loadImageMagick' | ||
import { type IMagickImage } from '@imagemagick/magick-wasm' | ||
|
||
export const readMagickImage = async <T>( | ||
inputBlob: Blob, | ||
func: (image: IMagickImage) => T | ||
): Promise<T> => { | ||
const { ImageMagick } = await loadImageMagick() | ||
const blobArray = new Uint8Array(await inputBlob.arrayBuffer()) | ||
return new Promise<T>((resolve) => { | ||
ImageMagick.read(blobArray, (image) => { | ||
resolve(func(image)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TaskContext } from 'vitest' | ||
|
||
export const uploadImage = (blob: Blob, variant: string, ctx?: TaskContext) => { | ||
const ext = blob.type.replace('image/', '') | ||
let filename = `${variant}.${ext}` | ||
if (ctx) { | ||
filename = `${ctx.task.suite.name}__${ctx.task.name}__${variant}.${ext}` | ||
} | ||
return fetch(`/upload-image?filename=${filename}`, { | ||
method: 'POST', | ||
body: blob | ||
}) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 35 additions & 40 deletions
75
packages/image-shrink/src/utils/image/JPEG/replaceJpegChunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,47 @@ | ||
import { readJpegChunks } from './readJpegChunks' | ||
|
||
export const replaceJpegChunk = ( | ||
export const replaceJpegChunk = async ( | ||
blob: Blob, | ||
marker: number, | ||
chunks: ArrayBuffer[] | ||
) => { | ||
return new Promise<Blob>((resolve, reject) => { | ||
{ | ||
const oldChunkPos: number[] = [] | ||
const oldChunkLength: number[] = [] | ||
|
||
const { promiseReadJpegChunks, stack } = readJpegChunks() | ||
|
||
return promiseReadJpegChunks(blob) | ||
.then(() => { | ||
stack.forEach((chunk) => { | ||
if (chunk.marker === marker) { | ||
oldChunkPos.push(chunk.startPos) | ||
return oldChunkLength.push(chunk.length) | ||
} | ||
}) | ||
}) | ||
.then(() => { | ||
const newChunks: (ArrayBuffer | Blob)[] = [blob.slice(0, 2)] | ||
|
||
for (const chunk of chunks) { | ||
const intro = new DataView(new ArrayBuffer(4)) | ||
intro.setUint16(0, 0xff00 + marker) | ||
intro.setUint16(2, chunk.byteLength + 2) | ||
newChunks.push(intro.buffer) | ||
newChunks.push(chunk) | ||
} | ||
|
||
let pos = 2 | ||
for (let i = 0; i < oldChunkPos.length; i++) { | ||
if (oldChunkPos[i] > pos) { | ||
newChunks.push(blob.slice(pos, oldChunkPos[i])) | ||
} | ||
pos = oldChunkPos[i] + oldChunkLength[i] + 4 | ||
} | ||
|
||
newChunks.push(blob.slice(pos, blob.size)) | ||
|
||
resolve( | ||
new Blob(newChunks, { | ||
type: blob.type | ||
}) | ||
) | ||
}) | ||
.catch(() => reject(blob)) | ||
}).catch(() => blob) | ||
await promiseReadJpegChunks(blob) | ||
|
||
stack.forEach((chunk) => { | ||
if (chunk.marker === marker) { | ||
oldChunkPos.push(chunk.startPos) | ||
return oldChunkLength.push(chunk.length) | ||
} | ||
}) | ||
|
||
const newChunks: (ArrayBuffer | Blob)[] = [blob.slice(0, 2)] | ||
|
||
for (const chunk of chunks) { | ||
const intro = new DataView(new ArrayBuffer(4)) | ||
intro.setUint16(0, 0xff00 + marker) | ||
intro.setUint16(2, chunk.byteLength + 2) | ||
newChunks.push(intro.buffer) | ||
newChunks.push(chunk) | ||
} | ||
|
||
let pos = 2 | ||
for (let i = 0; i < oldChunkPos.length; i++) { | ||
if (oldChunkPos[i] > pos) { | ||
newChunks.push(blob.slice(pos, oldChunkPos[i])) | ||
} | ||
pos = oldChunkPos[i] + oldChunkLength[i] + 4 | ||
} | ||
|
||
newChunks.push(blob.slice(pos, blob.size)) | ||
|
||
return new Blob(newChunks, { | ||
type: blob.type | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.