Class PixelConverter

java.lang.Object
dev.matrixlab.webp4j.internal.PixelConverter

public final class PixelConverter extends Object
Internal utility class for converting between BufferedImage and raw pixel byte arrays.

This class serves as a bridge between Java's BufferedImage objects and the raw byte arrays required by the native WebP encoding/decoding functions.

Author:
MrNanko
  • Method Details

    • toBufferedImage

      public static BufferedImage toBufferedImage(int width, int height, byte[] data)
      Creates a BufferedImage from a byte array containing pixel data.

      This method supports both RGB (3 bytes per pixel) and RGBA (4 bytes per pixel) formats. It determines the format based on the length of the input byte array and the image dimensions.

      Parameters:
      width - The width of the image.
      height - The height of the image.
      data - A byte array containing the pixel data. - For RGB format: Each pixel is represented by 3 consecutive bytes (R, G, B). - For RGBA format: Each pixel is represented by 4 consecutive bytes (R, G, B, A).
      Returns:
      A BufferedImage object representing the image with the specified width, height, and pixel data. - If the input buffer is RGB, the image will be of type BufferedImage.TYPE_INT_RGB. - If the input buffer is RGBA, the image will be of type BufferedImage.TYPE_INT_ARGB.
    • toBytes

      public static byte[] toBytes(BufferedImage image)
      Extracts pixel data from a BufferedImage into a byte array.

      This method automatically detects the image's color type, channel order, and alpha presence, then extracts pixel data accordingly.

      Parameters:
      image - The BufferedImage to extract pixel data from.
      Returns:
      A byte array containing the pixel data in RGB or RGBA format.