Pixel Data JS - v0.28.0
    Preparing search index...

    Function fileToImageData

    • Converts a browser File object into ImageData. This utility handles the full pipeline of image decoding using hardware-accelerated APIs createImageBitmap and OffscreenCanvas. It ensures that underlying resources like ImageBitmap are properly closed even if the conversion fails.

      Parameters

      • file: File | null | undefined

        The image file to convert. Can be null or undefined.

      Returns Promise<ImageData | null>

      A Promise resolving to the pixel data as ImageData, or null if no file was provided.

      UnsupportedFormatError Thrown if the provided file's MIME type does not start with image/.

      try {
      const imageData = await fileToImageData(file);
      if (imageData) {
      console.log('Pixels:', imageData.data);
      }
      } catch (err) {
      if (err instanceof UnsupportedFormatError) {
      // Handle bad file type
      }
      }