@@ -157,40 +157,52 @@ public TiffDecoderCore(Configuration configuration, ITiffDecoderOptions options)
157157 public Image < TPixel > Decode < TPixel > ( BufferedReadStream stream , CancellationToken cancellationToken )
158158 where TPixel : unmanaged, IPixel < TPixel >
159159 {
160- this . inputStream = stream ;
161- var reader = new DirectoryReader ( stream , this . Configuration . MemoryAllocator ) ;
162-
163- IEnumerable < ExifProfile > directories = reader . Read ( ) ;
164- this . byteOrder = reader . ByteOrder ;
165- this . isBigTiff = reader . IsBigTiff ;
166-
167160 var frames = new List < ImageFrame < TPixel > > ( ) ;
168- foreach ( ExifProfile ifd in directories )
161+ try
169162 {
170- cancellationToken . ThrowIfCancellationRequested ( ) ;
171- ImageFrame < TPixel > frame = this . DecodeFrame < TPixel > ( ifd , cancellationToken ) ;
172- frames . Add ( frame ) ;
163+ this . inputStream = stream ;
164+ var reader = new DirectoryReader ( stream , this . Configuration . MemoryAllocator ) ;
165+
166+ IEnumerable < ExifProfile > directories = reader . Read ( ) ;
167+ this . byteOrder = reader . ByteOrder ;
168+ this . isBigTiff = reader . IsBigTiff ;
173169
174- if ( this . decodingMode is FrameDecodingMode . First )
170+ foreach ( ExifProfile ifd in directories )
175171 {
176- break ;
172+ cancellationToken . ThrowIfCancellationRequested ( ) ;
173+ ImageFrame < TPixel > frame = this . DecodeFrame < TPixel > ( ifd , cancellationToken ) ;
174+ frames . Add ( frame ) ;
175+
176+ if ( this . decodingMode is FrameDecodingMode . First )
177+ {
178+ break ;
179+ }
177180 }
178- }
179181
180- ImageMetadata metadata = TiffDecoderMetadataCreator . Create ( frames , this . ignoreMetadata , reader . ByteOrder , reader . IsBigTiff ) ;
182+ ImageMetadata metadata = TiffDecoderMetadataCreator . Create ( frames , this . ignoreMetadata , reader . ByteOrder , reader . IsBigTiff ) ;
181183
182- // TODO: Tiff frames can have different sizes.
183- ImageFrame < TPixel > root = frames [ 0 ] ;
184- this . Dimensions = root . Size ( ) ;
185- foreach ( ImageFrame < TPixel > frame in frames )
186- {
187- if ( frame . Size ( ) != root . Size ( ) )
184+ // TODO: Tiff frames can have different sizes.
185+ ImageFrame < TPixel > root = frames [ 0 ] ;
186+ this . Dimensions = root . Size ( ) ;
187+ foreach ( ImageFrame < TPixel > frame in frames )
188188 {
189- TiffThrowHelper . ThrowNotSupported ( "Images with different sizes are not supported" ) ;
189+ if ( frame . Size ( ) != root . Size ( ) )
190+ {
191+ TiffThrowHelper . ThrowNotSupported ( "Images with different sizes are not supported" ) ;
192+ }
190193 }
194+
195+ return new Image < TPixel > ( this . Configuration , metadata , frames ) ;
191196 }
197+ catch
198+ {
199+ foreach ( ImageFrame < TPixel > f in frames )
200+ {
201+ f . Dispose ( ) ;
202+ }
192203
193- return new Image < TPixel > ( this . Configuration , metadata , frames ) ;
204+ throw ;
205+ }
194206 }
195207
196208 /// <inheritdoc/>
@@ -240,8 +252,8 @@ private ImageFrame<TPixel> DecodeFrame<TPixel>(ExifProfile tags, CancellationTok
240252 var stripOffsetsArray = ( Array ) tags . GetValueInternal ( ExifTag . StripOffsets ) . GetValue ( ) ;
241253 var stripByteCountsArray = ( Array ) tags . GetValueInternal ( ExifTag . StripByteCounts ) . GetValue ( ) ;
242254
243- IMemoryOwner < ulong > stripOffsetsMemory = this . ConvertNumbers ( stripOffsetsArray , out Span < ulong > stripOffsets ) ;
244- IMemoryOwner < ulong > stripByteCountsMemory = this . ConvertNumbers ( stripByteCountsArray , out Span < ulong > stripByteCounts ) ;
255+ using IMemoryOwner < ulong > stripOffsetsMemory = this . ConvertNumbers ( stripOffsetsArray , out Span < ulong > stripOffsets ) ;
256+ using IMemoryOwner < ulong > stripByteCountsMemory = this . ConvertNumbers ( stripByteCountsArray , out Span < ulong > stripByteCounts ) ;
245257
246258 if ( this . PlanarConfiguration == TiffPlanarConfiguration . Planar )
247259 {
@@ -262,8 +274,6 @@ private ImageFrame<TPixel> DecodeFrame<TPixel>(ExifProfile tags, CancellationTok
262274 cancellationToken ) ;
263275 }
264276
265- stripOffsetsMemory ? . Dispose ( ) ;
266- stripByteCountsMemory ? . Dispose ( ) ;
267277 return frame ;
268278 }
269279
0 commit comments