resizeImageWidth = ({ img, toWidth }) => {
  /**
   * For performance reasons we want to avoid changing the img data array size.
   * Instead we'll just keep the record of the resized image width and height separately.
   * @type {ImageSize}
   */
  const size = { w: img.width, h: img.height };

  // Calculating the number of pixels to remove.
  const pxToRemove = img.width - toWidth;

  let energyMap = null;
  let seam = null;

  // Removing the lowest energy seams one by one.
  for (let i = 0; i < pxToRemove; i += 1) {
    // 1. Calculate the energy map for the current version of the image.
    energyMap = calculateEnergyMap(img, size);

    // 2. Find the seam with the lowest energy based on the energy map.
    seam = findLowEnergySeam(energyMap, size);

    // 3. Delete the seam with the lowest energy seam from the image.
    deleteSeam(img, seam, size);

    // Reduce the image width, and continue iterations.
    size.w -= 1;
  }

  // Returning the resized image and its final size.
  // The img is actually a reference to the ImageData, so technically
  // the caller of the function already has this pointer. But let's
  // still return it for better code readability.
  return { img, size };
}
Performs the content-aware image width resizing using the seam carving method.
@param {ResizeImageWidthArgs} args
@returns {ResizeImageWidthResult}