-
Notifications
You must be signed in to change notification settings - Fork 2
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 #577 from AFP-Medialab/571-synthetic-images-resizi…
…ng-to-2mpx 571-synthetic-images-resizing-to-2mpx
- Loading branch information
Showing
3 changed files
with
196 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Converts an ImageData object to File | ||
* @param imageData {ImageData} | ||
* @param fileName {string} | ||
* @param imageType {string} | ||
* @returns {Promise<File | Error>} | ||
*/ | ||
const imageDataToFile = async (imageData, fileName, imageType) => { | ||
const offscreenCanvas = new OffscreenCanvas( | ||
imageData.width, | ||
imageData.height, | ||
); | ||
|
||
//1. Create an OffscreenCanvas | ||
const ctx = offscreenCanvas.getContext("2d"); | ||
ctx.putImageData(imageData, 0, 0); | ||
|
||
//2. Convert the OffscreenCanvas to a Blob | ||
const blob = await offscreenCanvas.convertToBlob({ type: imageType }); | ||
if (!blob) { | ||
return new Error("OffscreenCanvas to Blob conversion failed"); | ||
} | ||
|
||
//3. Convert the Blob to a File | ||
return new File([blob], fileName, { type: blob.type }, imageType); | ||
}; | ||
|
||
/** | ||
* | ||
* @param imageData {File | Blob} | ||
* @returns {Promise<File | Blob | Error>} | ||
*/ | ||
export const resizeImage = async (imageData) => { | ||
const MAX_PIXEL_SIZE = 2073599; // < 2Mpx | ||
|
||
// Create ImageBitmap from image data | ||
const imageBitmap = await createImageBitmap(imageData); | ||
|
||
// Check if the image exceeds the max pixel size | ||
const originalPixelSize = imageBitmap.width * imageBitmap.height; | ||
|
||
if (originalPixelSize <= MAX_PIXEL_SIZE) { | ||
// If the image is within the max pixel size, return as is | ||
return imageData; | ||
} | ||
|
||
// Calculate new dimensions maintaining the aspect ratio | ||
const aspectRatio = imageBitmap.width / imageBitmap.height; | ||
let newWidth, newHeight; | ||
|
||
if (aspectRatio > 1) { | ||
newWidth = Math.sqrt(MAX_PIXEL_SIZE * aspectRatio); | ||
newHeight = newWidth / aspectRatio; | ||
} else { | ||
newHeight = Math.sqrt(MAX_PIXEL_SIZE / aspectRatio); | ||
newWidth = newHeight * aspectRatio; | ||
} | ||
|
||
// Resize the image using an OffscreenCanvas | ||
const resizedImageData = await resizeUsingCanvas( | ||
imageBitmap, | ||
newWidth, | ||
newHeight, | ||
); | ||
|
||
return imageDataToFile( | ||
resizedImageData, | ||
imageData.name ?? "image", | ||
imageData.type, | ||
); | ||
}; | ||
|
||
// Helper function to resize the image using a canvas | ||
const resizeUsingCanvas = async (imageBitmap, newWidth, newHeight) => { | ||
const offscreenCanvas = new OffscreenCanvas(newWidth, newHeight); | ||
const context = offscreenCanvas.getContext("2d"); | ||
context.drawImage(imageBitmap, 0, 0, newWidth, newHeight); | ||
|
||
// Get ImageData from the OffscreenCanvas | ||
return context.getImageData(0, 0, newWidth, newHeight); | ||
}; |
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 { resizeImage } from "../components/Shared/Utils/imageUtils"; | ||
|
||
self.onmessage = async function (e) { | ||
const data = e.data; | ||
|
||
// console.log(e); | ||
const result = await resizeImage(data); | ||
// console.log(result); | ||
|
||
// Send the result back to the main thread | ||
self.postMessage(result); | ||
}; |