loadTiffImage function
Implementation
Future<MemoryImage> loadTiffImage(String path) async {
try {
// Load the TIFF file as a byte array
final ByteData data = await rootBundle.load(path);
final Uint8List bytes = data.buffer.asUint8List();
// Debugging output
print('Loaded TIFF image with length: ${bytes.length} bytes');
// Verify that the length of the bytes is reasonable
if (bytes.isEmpty) {
throw Exception('The TIFF image is empty or could not be loaded.');
}
// Decode the TIFF image using the image package
final img.Image? decodedImage = img.decodeTiff(bytes);
// Check if the image was decoded successfully
if (decodedImage != null) {
// Convert the decoded image to a PNG for display in Flutter
final List<int> pngBytes = img.encodePng(decodedImage);
return MemoryImage(Uint8List.fromList(pngBytes));
} else {
throw Exception('Failed to decode TIFF image');
}
} catch (e) {
print('Error loading TIFF image: $e');
// Fallback to a placeholder image or handle error as needed
return MemoryImage(
Uint8List.fromList([])); // Return an empty image as fallback
}
}