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 w = decodedImage.width;
  int h = decodedImage.height;

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

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

  double widthRatio = w.toDouble() / screenSize.width;
  double heightRatio = h.toDouble() / screenSize.height;

  bool imageFitToHeight =
      screenSize.aspectRatio > Size(w.toDouble(), h.toDouble()).aspectRatio;

  double pixelRatio = imageFitToHeight ? heightRatio : widthRatio;

  Size renderedSize = rawSize / pixelRatio;

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