David PHAM-VAN

Improve image decoding error messages

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 3.5.0 3 ## 3.5.0
4 4
5 - Add annotations [John Harris] 5 - Add annotations [John Harris]
  6 +- Improve image decoding error messages
6 7
7 ## 3.4.2 8 ## 3.4.2
8 9
@@ -98,7 +98,11 @@ class MemoryImage extends ImageProvider { @@ -98,7 +98,11 @@ class MemoryImage extends ImageProvider {
98 PdfImageOrientation? orientation, 98 PdfImageOrientation? orientation,
99 double? dpi, 99 double? dpi,
100 }) { 100 }) {
101 - final decoder = im.findDecoderForData(bytes)!; 101 + final decoder = im.findDecoderForData(bytes);
  102 + if (decoder == null) {
  103 + throw Exception('Unable to guess the image type ${bytes.length} bytes');
  104 + }
  105 +
102 if (decoder is im.JpegDecoder) { 106 if (decoder is im.JpegDecoder) {
103 final info = PdfJpegInfo(bytes); 107 final info = PdfJpegInfo(bytes);
104 108
@@ -111,7 +115,12 @@ class MemoryImage extends ImageProvider { @@ -111,7 +115,12 @@ class MemoryImage extends ImageProvider {
111 ); 115 );
112 } 116 }
113 117
114 - final info = decoder.startDecode(bytes)!; 118 + final info = decoder.startDecode(bytes);
  119 +
  120 + if (info == null) {
  121 + throw Exception('Unable decode the image');
  122 + }
  123 +
115 return MemoryImage._( 124 return MemoryImage._(
116 bytes, 125 bytes,
117 info.width, 126 info.width,
@@ -138,7 +147,12 @@ class MemoryImage extends ImageProvider { @@ -138,7 +147,12 @@ class MemoryImage extends ImageProvider {
138 return PdfImage.file(context.document, bytes: bytes); 147 return PdfImage.file(context.document, bytes: bytes);
139 } 148 }
140 149
141 - final image = im.decodeImage(bytes)!; 150 + final image = im.decodeImage(bytes);
  151 +
  152 + if (image == null) {
  153 + throw Exception('Unable decode the image');
  154 + }
  155 +
142 final resized = im.copyResize(image, width: width); 156 final resized = im.copyResize(image, width: width);
143 return PdfImage.fromImage(context.document, image: resized); 157 return PdfImage.fromImage(context.document, image: resized);
144 } 158 }