resizePng static method

Uint8List resizePng({
  1. required Uint8List pngBytes,
  2. required int outputWidth,
  3. required int outputHeight,
  4. BicubicFilter filter = BicubicFilter.catmullRom,
  5. EdgeMode edgeMode = EdgeMode.clamp,
  6. double crop = 1.0,
  7. CropAnchor cropAnchor = CropAnchor.center,
  8. CropAspectRatio cropAspectRatio = CropAspectRatio.square,
  9. double aspectRatioWidth = 1.0,
  10. double aspectRatioHeight = 1.0,
  11. int compressionLevel = 6,
})

Resize PNG image bytes using bicubic interpolation

Entire pipeline (decode -> resize -> encode) runs in native C code. Preserves alpha channel if present. This is synchronous but very fast due to native performance.

pngBytes - PNG encoded image data outputWidth - Desired output width outputHeight - Desired output height filter - Bicubic filter type (default: Catmull-Rom) edgeMode - How to handle pixels outside image bounds (default: clamp) crop - Crop factor (0.0-1.0), 1.0 = no crop, 0.5 = 50% cropAnchor - Position to anchor the crop (default: center) cropAspectRatio - Aspect ratio mode for crop (default: square) aspectRatioWidth - Custom aspect ratio width (only used with CropAspectRatio.custom) aspectRatioHeight - Custom aspect ratio height (only used with CropAspectRatio.custom) compressionLevel - PNG compression level (0-9, default 6, 0=none, 9=max)

Returns resized PNG encoded data

Implementation

static Uint8List resizePng({
  required Uint8List pngBytes,
  required int outputWidth,
  required int outputHeight,
  BicubicFilter filter = BicubicFilter.catmullRom,
  EdgeMode edgeMode = EdgeMode.clamp,
  double crop = 1.0,
  CropAnchor cropAnchor = CropAnchor.center,
  CropAspectRatio cropAspectRatio = CropAspectRatio.square,
  double aspectRatioWidth = 1.0,
  double aspectRatioHeight = 1.0,
  int compressionLevel = 6,
}) {
  final inputPtr = calloc<Uint8>(pngBytes.length);
  final outputDataPtr = calloc<Pointer<Uint8>>();
  final outputSizePtr = calloc<Int32>();

  try {
    inputPtr.asTypedList(pngBytes.length).setAll(0, pngBytes);

    final result = NativeBindings.instance.bicubicResizePng(
      inputPtr,
      pngBytes.length,
      outputWidth,
      outputHeight,
      filter.value,
      edgeMode.value,
      crop,
      cropAnchor.value,
      cropAspectRatio.value,
      aspectRatioWidth,
      aspectRatioHeight,
      compressionLevel,
      outputDataPtr,
      outputSizePtr,
    );

    _throwIfError(result);

    final outputData = outputDataPtr.value;
    final outputSize = outputSizePtr.value;

    // Copy data before freeing native buffer
    final resultBytes = Uint8List.fromList(
      outputData.asTypedList(outputSize),
    );

    // Free the native-allocated buffer
    NativeBindings.instance.freeBuffer(outputData);

    return resultBytes;
  } finally {
    calloc.free(inputPtr);
    calloc.free(outputDataPtr);
    calloc.free(outputSizePtr);
  }
}