fromEquirectImageBytes static method

Future<EnvironmentMap> fromEquirectImageBytes({
  1. required Uint8List bytes,
  2. int maxWidth = 4096,
  3. List<Vector3>? diffuseSphericalHarmonics,
})

Builds an EnvironmentMap from the raw bytes of an equirectangular image, detecting Radiance HDR, OpenEXR, or a standard sRGB image (see fromEquirectImageAsset). Useful for an image fetched over the network or picked by the user.

On the web there are no isolates, so an HDR/EXR decode runs on the main thread and a large panorama can jank; prefer a modest maxWidth there.

Implementation

static Future<EnvironmentMap> fromEquirectImageBytes({
  required Uint8List bytes,
  int maxWidth = 4096,
  List<Vector3>? diffuseSphericalHarmonics,
}) async {
  // LDR decodes through the platform image codec on the main isolate; only
  // the CPU-heavy HDR/EXR decode (and its SH projection) is worth an isolate.
  if (detectEquirectImageFormat(bytes) == EquirectImageFormat.ldr) {
    return fromUIImages(
      radianceImage: await imageFromBytes(bytes, maxWidth: maxWidth),
      diffuseSphericalHarmonics: diffuseSphericalHarmonics,
    );
  }
  final (pixels, width, height, shFlat) = await compute(
    _decodeEquirectHdrOnIsolate,
    (bytes, maxWidth, diffuseSphericalHarmonics == null),
  );
  return fromEquirectHdr(
    linearPixels: pixels,
    width: width,
    height: height,
    diffuseSphericalHarmonics:
        diffuseSphericalHarmonics ?? _shFromFlat(shFlat),
  );
}