@@ -129,6 +129,16 @@ impl HarrisResponse {
129129 Self { k, ..self }
130130 }
131131
132+ #[ inline]
133+ fn row_bounds_err ( cols : usize , rows : usize ) -> ImageError {
134+ ImageError :: PixelIndexOutOfBounds ( 0 , rows. saturating_sub ( 1 ) , cols, rows)
135+ }
136+
137+ #[ inline]
138+ fn col_bounds_err ( cols : usize , rows : usize , row_idx : usize ) -> ImageError {
139+ ImageError :: PixelIndexOutOfBounds ( cols. saturating_sub ( 1 ) , row_idx + 1 , cols, rows)
140+ }
141+
132142 /// Computes the harris response of an image.
133143 ///
134144 /// The Harris response is computed by the determinant minus the trace squared.
@@ -158,49 +168,58 @@ impl HarrisResponse {
158168 ) ) ;
159169 }
160170
171+ if src. cols ( ) < 2 || src. rows ( ) < 2 {
172+ return Err ( ImageError :: InvalidImageSize (
173+ src. size ( ) . width ,
174+ src. size ( ) . height ,
175+ 2 ,
176+ 2 ,
177+ ) ) ;
178+ }
179+
161180 let src_data = src. as_slice ( ) ;
162181 let col_slice = src. cols ( ) ..src_data. len ( ) - src. cols ( ) ;
163182 let row_slice = 1 ..src. cols ( ) - 1 ;
164183
165184 self . dx2_data
166185 . as_mut_slice ( )
167186 . get_mut ( col_slice. clone ( ) )
168- // SAFETY: we ranges is valid
169- . unwrap ( )
187+ . ok_or_else ( || Self :: row_bounds_err ( src. cols ( ) , src. rows ( ) ) ) ?
170188 . par_chunks_exact_mut ( src. cols ( ) )
171189 . zip (
172190 self . dy2_data
173191 . as_mut_slice ( )
174192 . get_mut ( col_slice. clone ( ) )
175- // SAFETY: we ranges is valid
176- . unwrap ( )
193+ . ok_or_else ( || Self :: row_bounds_err ( src. cols ( ) , src. rows ( ) ) ) ?
177194 . par_chunks_exact_mut ( src. cols ( ) ) ,
178195 )
179196 . zip (
180197 self . dxy_data
181198 . as_mut_slice ( )
182199 . get_mut ( col_slice. clone ( ) )
183- // SAFETY: we ranges is valid
184- . unwrap ( )
200+ . ok_or_else ( || Self :: row_bounds_err ( src. cols ( ) , src. rows ( ) ) ) ?
185201 . par_chunks_exact_mut ( src. cols ( ) ) ,
186202 )
187203 . enumerate ( )
188- . for_each ( |( row_idx, ( ( dx2_chunk, dy2_chunk) , dxy_chunk) ) | {
204+ . try_for_each ( |( row_idx, ( ( dx2_chunk, dy2_chunk) , dxy_chunk) ) | {
189205 let row_offset = ( row_idx + 1 ) * src. cols ( ) ;
190206
191207 dx2_chunk
192208 . get_mut ( row_slice. clone ( ) )
193- // SAFETY: we ranges is valid
194- . unwrap ( )
209+ . ok_or_else ( || Self :: col_bounds_err ( src. cols ( ) , src. rows ( ) , row_idx) ) ?
195210 . iter_mut ( )
196211 . zip (
197212 dy2_chunk
198213 . get_mut ( row_slice. clone ( ) )
199- // SAFETY: we ranges is valid
200- . unwrap ( )
214+ . ok_or_else ( || Self :: col_bounds_err ( src. cols ( ) , src. rows ( ) , row_idx) ) ?
215+ . iter_mut ( ) ,
216+ )
217+ . zip (
218+ dxy_chunk
219+ . get_mut ( row_slice. clone ( ) )
220+ . ok_or_else ( || Self :: col_bounds_err ( src. cols ( ) , src. rows ( ) , row_idx) ) ?
201221 . iter_mut ( ) ,
202222 )
203- . zip ( dxy_chunk. get_mut ( row_slice. clone ( ) ) . unwrap ( ) . iter_mut ( ) )
204223 . enumerate ( )
205224 . for_each ( |( col_idx, ( ( dx2_pixel, dy2_pixel) , dxy_pixel) ) | {
206225 let current_idx = row_offset + col_idx + 1 ;
@@ -230,21 +249,20 @@ impl HarrisResponse {
230249 * dy2_pixel = dy * dy;
231250 * dxy_pixel = dx * dy;
232251 } ) ;
233- } ) ;
252+ Ok :: < ( ) , ImageError > ( ( ) )
253+ } ) ?;
234254
235255 dst. as_slice_mut ( )
236256 . get_mut ( col_slice. clone ( ) )
237- // SAFETY: we ranges is valid
238- . unwrap ( )
257+ . ok_or_else ( || Self :: row_bounds_err ( src. cols ( ) , src. rows ( ) ) ) ?
239258 . par_chunks_exact_mut ( src. cols ( ) )
240259 . enumerate ( )
241- . for_each ( |( row_idx, dst_chunk) | {
260+ . try_for_each ( |( row_idx, dst_chunk) | {
242261 let row_offset = ( row_idx + 1 ) * src. cols ( ) ;
243262
244263 dst_chunk
245264 . get_mut ( row_slice. clone ( ) )
246- // SAFETY: we ranges is valid
247- . unwrap ( )
265+ . ok_or_else ( || Self :: col_bounds_err ( src. cols ( ) , src. rows ( ) , row_idx) ) ?
248266 . iter_mut ( )
249267 . enumerate ( )
250268 . for_each ( |( col_idx, dst_pixel) | {
@@ -282,7 +300,8 @@ impl HarrisResponse {
282300
283301 * dst_pixel = f32:: max ( 0.0 , response) ;
284302 } ) ;
285- } ) ;
303+ Ok :: < ( ) , ImageError > ( ( ) )
304+ } ) ?;
286305
287306 Ok ( ( ) )
288307 }
0 commit comments