decodeImageInfos function

Future<ImageInfos> decodeImageInfos({
  1. required Uint8List bytes,
  2. required Size screenSize,
  3. TransformConfigs? configs,
})

Decode the image being edited.

Implementation

Future<ImageInfos> decodeImageInfos({
  required Uint8List bytes,
  required Size screenSize,
  TransformConfigs? configs,
}) async {
  var decodedImage = await decodeImageFromList(bytes);
  Size rawSize = Size(
    decodedImage.width.toDouble(),
    decodedImage.height.toDouble(),
  );

  bool rotated = configs?.is90DegRotated == true;
  int width = decodedImage.width;
  int height = decodedImage.height;

  double calculatePixelRatio(num width, num height) {
    bool fitToHeight = screenSize.aspectRatio > (width / height);
    double widthRatio = width / screenSize.width;
    double heightRatio = height / screenSize.height;
    return fitToHeight ? heightRatio : widthRatio;
  }

  // Calculate the pixel ratio before scaling and rotation
  double originalPixelRatio = calculatePixelRatio(width, height);

  /// If the image is rotated we also flip the width/ height
  if (rotated) {
    int hX = height;
    height = width;
    width = hX;
  }

  if (configs != null && configs.isNotEmpty) {
    width ~/= configs.scaleUser;
    height ~/= configs.scaleUser;
  }

  // Calculate final pixel ratio after scaling
  double pixelRatio = calculatePixelRatio(width, height);

  Size renderedSize = rawSize / pixelRatio;
  Size originalRenderedSize = rawSize / originalPixelRatio;

  return ImageInfos(
    rawSize: rawSize,
    renderedSize: renderedSize,
    originalRenderedSize: originalRenderedSize,
    cropRectSize: configs != null && configs.isNotEmpty
        ? configs.cropRect.size
        : renderedSize,
    pixelRatio: pixelRatio,
    isRotated: configs?.is90DegRotated ?? false,
  );
}