Refactor image and mask utility functions

Moved convertToImage, createMaskFromImageSrc, and canvasToMaskImage from MaskProcessingUtils and mask_utils to ImageUtils for better modularity and reuse. Updated imports in dependent modules to use the new locations. Removed duplicate implementations from mask_utils and MaskProcessingUtils.
This commit is contained in:
Dariusz L
2025-07-27 18:47:41 +02:00
parent 9d0c946e22
commit 207bacc1f8
10 changed files with 113 additions and 95 deletions

View File

@@ -1,5 +1,6 @@
import { createModuleLogger } from "./LoggerUtils.js";
import { createCanvas } from "./CommonUtils.js";
import { convertToImage } from "./ImageUtils.js";
const log = createModuleLogger('MaskProcessingUtils');
@@ -119,27 +120,6 @@ export async function processImageWithTransform(
return tempCanvas;
}
/**
* Converts a canvas or image to an Image element
* @param source - Source canvas or image
* @returns Promise with Image element
*/
export async function convertToImage(source: HTMLCanvasElement | HTMLImageElement): Promise<HTMLImageElement> {
if (source instanceof HTMLImageElement) {
return source; // Already an image
}
const image = new Image();
image.src = source.toDataURL();
await new Promise<void>((resolve, reject) => {
image.onload = () => resolve();
image.onerror = reject;
});
return image;
}
/**
* Crops an image to a specific region
* @param sourceImage - Source image or canvas