Permalink
Browse files

initial version

  • Loading branch information...
0 parents commit d89bfa30eb9a6d6de280d8e52480c09a38debd5e @aleator committed Jul 19, 2011
Showing with 302 additions and 0 deletions.
  1. +166 −0 Bindings/DC1394.hsc
  2. +81 −0 Examples/HelloWorld.hs
  3. +30 −0 LICENSE
  4. +2 −0 Setup.hs
  5. +23 −0 bindings-dc1394
@@ -0,0 +1,166 @@
+{-# LANGUAGE ForeignFunctionInterface #-}
+
+#include <bindings.dsl.h>
+#include <dc1394/dc1394.h>
+
+module Bindings.DC1394 where
+import Data.Word
+#strict_import
+
+-- camera_id_t
+#starttype dc1394camera_id_t
+#field unit , Word16
+#field guid , Word64
+#stoptype
+
+-- camera_list_t
+#starttype dc1394camera_list_t
+#field num , Word32
+#field ids , Ptr <dc1394camera_id_t>
+#stoptype
+
+-- dc1394switch_t
+#num DC1394_OFF
+#num DC1394_ON
+
+-- utils
+#ccall memcpy, Ptr () -> Ptr () -> Word32 -> IO ()
+
+-- video_frame_t
+#starttype dc1394video_frame_t
+#field image, Ptr Word8
+
+#field size, Ptr Word32
+#field position, Ptr Word32
+#field color_coding, CInt
+#field color_filter, CInt
+#field yuv_byte_order, Word32
+#field data_depth, Word32
+
+#field stride, Word32
+#field video_mode, CInt
+#field total_bytes, Word64
+
+#field image_bytes, Word32
+#field padding_bytes, Word32
+#field packet_size, Word32
+#field packets_per_frame, Word32
+#field timestamp, Word64
+
+#field frames_behind, Word32
+#field camera, Ptr <dc1394camera_t>
+#field id, Word32
+#field allocated_image_bytes, Word64
+#field little_endian, CInt
+
+#field data_in_padding, CInt
+#stoptype
+
+#opaque_t dc1394camera_t
+#opaque_t dc1394_t
+
+#ccall dc1394_video_set_transmission , Ptr <dc1394camera_t> -> CInt -> IO CInt
+#ccall dc1394_capture_stop , Ptr <dc1394camera_t> -> IO CInt
+#ccall dc1394_camera_free , Ptr <dc1394camera_t> -> IO ()
+#ccall dc1394_camera_enumerate, Ptr <dc1394_t> -> Ptr (Ptr <dc1394camera_list_t>) -> IO CInt
+#ccall dc1394_camera_new, Ptr <dc1394_t> -> Word64 -> IO (Ptr <dc1394camera_t>)
+#ccall dc1394_new , IO (Ptr <dc1394_t>)
+#ccall dc1394_free , Ptr <dc1394_t> -> IO ()
+#ccall dc1394_camera_free_list , (Ptr <dc1394camera_list_t>) -> IO ()
+#ccall dc1394_video_set_iso_speed , Ptr <dc1394camera_t> -> CInt -> IO CInt
+#ccall dc1394_capture_setup , Ptr <dc1394camera_t> -> CInt -> CInt -> IO CInt
+#ccall dc1394_capture_dequeue , Ptr <dc1394camera_t> -> CInt -> Ptr (Ptr <dc1394video_frame_t>) -> IO CInt
+#ccall dc1394_video_set_mode , Ptr <dc1394camera_t> -> CInt -> IO CInt
+#ccall dc1394_get_image_size_from_video_mode , Ptr <dc1394camera_t> -> CInt -> Ptr Word32 -> Ptr Word32 -> IO CInt
+#ccall dc1394_video_set_framerate , Ptr <dc1394camera_t> -> CInt -> IO CInt
+
+#num DC1394_ISO_SPEED_100
+#num DC1394_ISO_SPEED_200
+#num DC1394_ISO_SPEED_400
+#num DC1394_ISO_SPEED_800
+#num DC1394_ISO_SPEED_1600
+#num DC1394_ISO_SPEED_3200
+#num DC1394_ISO_SPEED_MIN
+#num DC1394_ISO_SPEED_MAX
+#num DC1394_ISO_SPEED_NUM
+
+#num DC1394_VIDEO_MODE_160x120_YUV444
+#num DC1394_VIDEO_MODE_320x240_YUV422
+#num DC1394_VIDEO_MODE_640x480_YUV411
+#num DC1394_VIDEO_MODE_640x480_YUV422
+#num DC1394_VIDEO_MODE_640x480_RGB8
+#num DC1394_VIDEO_MODE_640x480_MONO8
+#num DC1394_VIDEO_MODE_640x480_MONO16
+#num DC1394_VIDEO_MODE_800x600_YUV422
+#num DC1394_VIDEO_MODE_800x600_RGB8
+#num DC1394_VIDEO_MODE_800x600_MONO8
+#num DC1394_VIDEO_MODE_1024x768_YUV422
+#num DC1394_VIDEO_MODE_1024x768_RGB8
+#num DC1394_VIDEO_MODE_1024x768_MONO8
+#num DC1394_VIDEO_MODE_800x600_MONO16
+#num DC1394_VIDEO_MODE_1024x768_MONO16
+#num DC1394_VIDEO_MODE_1280x960_YUV422
+#num DC1394_VIDEO_MODE_1280x960_RGB8
+#num DC1394_VIDEO_MODE_1280x960_MONO8
+#num DC1394_VIDEO_MODE_1600x1200_YUV422
+#num DC1394_VIDEO_MODE_1600x1200_RGB8
+#num DC1394_VIDEO_MODE_1600x1200_MONO8
+#num DC1394_VIDEO_MODE_1280x960_MONO16
+#num DC1394_VIDEO_MODE_1600x1200_MONO16
+#num DC1394_VIDEO_MODE_EXIF
+#num DC1394_VIDEO_MODE_FORMAT7_0
+#num DC1394_VIDEO_MODE_FORMAT7_1
+#num DC1394_VIDEO_MODE_FORMAT7_2
+#num DC1394_VIDEO_MODE_FORMAT7_3
+#num DC1394_VIDEO_MODE_FORMAT7_4
+#num DC1394_VIDEO_MODE_FORMAT7_5
+#num DC1394_VIDEO_MODE_FORMAT7_6
+#num DC1394_VIDEO_MODE_FORMAT7_7
+
+
+#num DC1394_FRAMERATE_1_875
+#num DC1394_FRAMERATE_3_75
+#num DC1394_FRAMERATE_7_5
+#num DC1394_FRAMERATE_15
+#num DC1394_FRAMERATE_30
+#num DC1394_FRAMERATE_60
+#num DC1394_FRAMERATE_120
+#num DC1394_FRAMERATE_240
+#num DC1394_FRAMERATE_MIN
+#num DC1394_FRAMERATE_MAX
+
+#num DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC
+#num DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC
+#num DC1394_CAPTURE_FLAGS_DEFAULT
+#num DC1394_CAPTURE_FLAGS_AUTO_ISO
+
+#num DC1394_CAPTURE_POLICY_WAIT
+#num DC1394_CAPTURE_POLICY_POLL
+#num DC1394_CAPTURE_POLICY_MIN
+#num DC1394_CAPTURE_POLICY_MAX
+#num DC1394_CAPTURE_POLICY_NUM
+
+#num DC1394_COLOR_CODING_MONO8
+#num DC1394_COLOR_CODING_YUV411
+#num DC1394_COLOR_CODING_YUV422
+#num DC1394_COLOR_CODING_YUV444
+#num DC1394_COLOR_CODING_RGB8
+#num DC1394_COLOR_CODING_MONO16
+#num DC1394_COLOR_CODING_RGB16
+#num DC1394_COLOR_CODING_MONO16S
+#num DC1394_COLOR_CODING_RGB16S
+#num DC1394_COLOR_CODING_RAW8
+#num DC1394_COLOR_CODING_RAW16
+#num DC1394_COLOR_CODING_MIN
+#num DC1394_COLOR_CODING_MAX
+
+#num DC1394_COLOR_FILTER_RGGB
+#num DC1394_COLOR_FILTER_GBRG
+#num DC1394_COLOR_FILTER_GRBG
+#num DC1394_COLOR_FILTER_BGGR
+#num DC1394_COLOR_FILTER_MIN
+#num DC1394_COLOR_FILTER_MAX
+
+
+
+
@@ -0,0 +1,81 @@
+{-#LANGUAGE ScopedTypeVariables#-}
+module Main where
+
+import Bindings.DC1394
+import CV.ColourUtils
+import CV.Conversions
+import CV.Image
+import Control.Applicative
+import Control.Exception
+import Data.Array.CArray
+import Data.Array.IArray
+import Foreign.C.Types
+import Foreign.Marshal.Alloc
+import Foreign.Marshal.Array
+import Foreign.Ptr
+import Foreign.Storable
+import Foreign.Storable.Tuple
+import qualified Data.ByteString.Char8 as B
+
+
+
+getIds camList = peekArray (fromIntegral $ c'dc1394camera_list_t'num camList)
+ (c'dc1394camera_list_t'ids camList)
+
+getCameras dc = alloca $ \list -> bracket
+ (c'dc1394_camera_enumerate dc list)
+ (\_ -> peek list >>= c'dc1394_camera_free_list)
+ (\_ -> peek list >>= peek >>= getIds) -- TODO: Errors
+
+getFrame camera = alloca $ \framePtr -> do
+ c'dc1394_capture_dequeue camera c'DC1394_CAPTURE_POLICY_WAIT framePtr
+ dataPtr <- c'dc1394video_frame_t'image <$> (peek framePtr >>= peek)
+ return $ unsafe8UC3FromPtr (640,480) dataPtr
+
+main = do
+ dc <- c'dc1394_new
+ (e:_) <- getCameras dc
+ print e
+ let guid = c'dc1394camera_id_t'guid e
+ print ("Trying camera", guid)
+ camera <- c'dc1394_camera_new dc guid
+ print camera
+ c'dc1394_video_set_iso_speed camera c'DC1394_ISO_SPEED_400
+ c'dc1394_video_set_mode camera c'DC1394_VIDEO_MODE_640x480_RGB8
+ c'dc1394_video_set_framerate camera c'DC1394_FRAMERATE_7_5
+ c'dc1394_capture_setup camera 4 c'DC1394_CAPTURE_FLAGS_DEFAULT
+ c'dc1394_video_set_transmission camera c'DC1394_ON
+
+
+ getFrame camera >>= saveImage "testShot2-1.png"
+ getFrame camera >>= saveImage "testShot2-2.png"
+ getFrame camera >>= saveImage "testShot2-3.png"
+
+ --peekArray (3*640*480) dataPtr >>= print
+-- bs <- B.packCStringLen (dataPtr,640*480*3)
+-- B.writeFile "test.ppm" $ B.pack "P6\n640 480\n255\n"
+-- B.appendFile "test.ppm" bs
+-- ca :: CArray (Int,Int) (CChar,CChar, CChar)
+-- <- createCArray ((0,0),(639,479))
+-- (\ptr -> c'memcpy (castPtr dataPtr) (castPtr ptr)
+-- (fromIntegral $ 640*480*sizeOf (0::CChar,0::CChar, 0::CChar)))
+-- print $ toList ca
+-- saveImage "testShot.png" $ stretchHistogram $ copyFCArrayToImage $ amap (\(r,g,b) -> fromIntegral (r+g+b) / 3) ca
+--
+ w <- malloc
+ h <- malloc
+ c'dc1394_get_image_size_from_video_mode camera c'DC1394_VIDEO_MODE_640x480_RGB8 w h
+ print "Size"
+ peek w >>= print
+ peek h >>= print
+ print "eSize"
+
+
+ c'dc1394_video_set_transmission camera c'DC1394_OFF
+ c'dc1394_capture_stop camera
+ c'dc1394_camera_free camera
+ c'dc1394_free dc
+
+
+
+
@@ -0,0 +1,30 @@
+Copyright (c)2011, Ville Tirronen
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Ville Tirronen nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,23 @@
+Name: bindings-dc1394
+Version: 0.1
+Synopsis: Library for using firewire (iidc-1394) cameras
+Description: Low level bindings for libdc1394 (for driving firewire cameras)
+Homepage: http://github.com/aleator/hdc1394
+License: BSD3
+License-file: LICENSE
+Author: Ville Tirronen
+Maintainer: aleator@gmail.com
+Category: System
+Build-type: Simple
+Cabal-version: >=1.2
+
+Library
+ Exposed-modules:
+ Bindings.DC1394
+ Build-depends:
+ bindings-DSL >= 1.0.7 && < 1.1,
+ base > 4 && <5,
+ CV >= 0.3
+ Build-tools: c2hs >= 0.16.0
+ Includes: dc1394/dc1394.h
+ Extra-Libraries: dc1394

0 comments on commit d89bfa3

Please sign in to comment.