@@ -29,9 +29,11 @@ module OpenCV.Internal.Core.Types.Mat
29
29
, newEmptyMat
30
30
, newMat
31
31
, withMatData
32
+ , withMatAsVec
32
33
, matElemAddress
33
34
, mkMat
34
35
, cloneMat
36
+ , isContinuous
35
37
36
38
-- * Mutable matrix
37
39
, typeCheckMatM
@@ -80,10 +82,12 @@ import "base" Data.Monoid ( (<>) )
80
82
import "base" Data.Proxy
81
83
import "base" Data.Word
82
84
import "base" Foreign.C.Types
83
- import "base" Foreign.ForeignPtr ( ForeignPtr , withForeignPtr , touchForeignPtr )
85
+ import "base" Foreign.ForeignPtr
86
+ ( ForeignPtr , withForeignPtr , touchForeignPtr , newForeignPtr_ )
84
87
import "base" Foreign.Marshal.Alloc ( alloca )
85
88
import "base" Foreign.Marshal.Array ( allocaArray , peekArray )
86
- import "base" Foreign.Ptr ( Ptr , plusPtr )
89
+ import "base" Foreign.Marshal.Utils ( toBool )
90
+ import "base" Foreign.Ptr ( Ptr , plusPtr , castPtr )
87
91
import "base" Foreign.Storable ( Storable (.. ), peek )
88
92
import "base" GHC.TypeLits
89
93
import "base" System.IO.Unsafe ( unsafePerformIO )
@@ -106,6 +110,7 @@ import "this" OpenCV.Internal.Mutable
106
110
import "this" OpenCV.TypeLevel
107
111
import "transformers" Control.Monad.Trans.Except
108
112
import qualified "vector" Data.Vector as V
113
+ import qualified "vector" Data.Vector.Storable as VS
109
114
import qualified "vector" Data.Vector.Generic as VG
110
115
111
116
--------------------------------------------------------------------------------
@@ -351,6 +356,29 @@ withMatData mat f = withPtr mat $ \matPtr ->
351
356
step <- peekArray (fromIntegral dims) stepPtr
352
357
f step dataPtr
353
358
359
+ -- | Access a Mat's data via a temporary Storable Vector.
360
+ --
361
+ -- The storable vector may no longer be used after the supplied
362
+ -- computation terminates.
363
+ withMatAsVec
364
+ :: forall a shape channels depth . (Storable depth )
365
+ => Mat shape channels ('S depth )
366
+ -> (VS. Vector depth -> IO a )
367
+ -- ^ A computation to perform on the vector.
368
+ -> IO a
369
+ withMatAsVec mat f =
370
+ withMatData continuousMat $ \ _step dataPtr -> do
371
+ foreignDataPtr :: ForeignPtr depth <- newForeignPtr_ $ castPtr dataPtr
372
+ f $ VS. unsafeFromForeignPtr0 foreignDataPtr numElems
373
+ where
374
+ numElems = fromIntegral $ product $ miChannels i : miShape i
375
+ where
376
+ i = matInfo continuousMat
377
+
378
+ continuousMat
379
+ | isContinuous mat = mat
380
+ | otherwise = cloneMat mat
381
+
354
382
matElemAddress :: Ptr Word8 -> [Int ] -> [Int ] -> Ptr a
355
383
matElemAddress dataPtr step pos = dataPtr `plusPtr` offset
356
384
where
@@ -383,6 +411,11 @@ cloneMatIO mat =
383
411
fmap unsafeCoerceMat $ fromPtr $ withPtr mat $ \ matPtr ->
384
412
[C. exp |Mat * { new Mat($(Mat * matPtr)->clone()) }|]
385
413
414
+ isContinuous :: Mat shape channels depth -> Bool
415
+ isContinuous mat = toBool $ unsafePerformIO $
416
+ withPtr mat $ \ matPtr ->
417
+ [CU. exp | bool { $(Mat * matPtr)->isContinuous() } |]
418
+
386
419
--------------------------------------------------------------------------------
387
420
-- Mutable matrix
388
421
--------------------------------------------------------------------------------
0 commit comments