@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function trimImagePadding

    • trim the padding of an image based on sum of pixel conditioning of each border's rows and columns.

      Note

      the output will always consist of at least 1-pixel width or 1-pixel height.

      Type Parameters

      • Channels extends 1 | 2 | 3 | 4

      Parameters

      Returns SimpleImageData

      for example, to trim the whitespace border pixels of an "RGBA" image, irrespective of the alpha, and a minimum requirement of at least three non-near-white pixels in each border row and column, you would design your padding_condition as such:

      import { assertEquals } from "jsr:@std/assert"

      // we want the distance between a pixel's rgb color and the white color (i.e. `(255, 255, 255,)`)
      // to be less than `10 * Math.sqrt(3)`, in order for it to be considered near-white.
      const my_white_padding_condition = (r: number, g: number, b: number, a: number) => {
      const distance_from_white = (3 * 255**2) - (r**2 + g**2 + b**2)
      return distance_from_white < (3 * 5**2)
      ? 0.0
      : 1.0
      }

      const
      width = 60,
      img_data = new ImageData(new Uint8ClampedArray(4 * width * 30).fill(255), width), // fully white rgba image
      trimmed_img_data = trimImagePadding(img_data, my_white_padding_condition, 3.0) // only a 1x1 white pixel remains

      assertEquals(trimmed_img_data.width, 1)
      assertEquals(trimmed_img_data.height, 1)
      assertEquals(trimmed_img_data.data, new Uint8ClampedArray(4).fill(255))