c++ - 将图像从 C++ 传递给 Haskell 并取回一个字符串

标签 c++ arrays pointers haskell ffi

我想使用图像作为参数从 C++ 调用 Haskell 函数。它只是一个无符号字符数组,包含有关宽度和高度的信息(以像素为单位)。

到目前为止,我已经有了这个工作代码。

-- Stuff.hs

module Stuff where

import Data.List
import Data.Word
import qualified Data.Vector.Unboxed as V

import Foreign.Ptr
import Foreign.Storable
import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Array
import Foreign.Marshal.Alloc

foreign export ccall freeResult :: CString -> IO ()
foreign export ccall doWithImageStruct :: ImageStruct -> IO CString

data Image = Image Word32 Word32 (V.Vector Double)

type ImageStruct = Ptr ImageStructType

-- CUInt is Word32.
-- https://hackage.haskell.org/package/base-4.6.0.0/docs/Foreign-C-Types.html#t:CInt
data ImageStructType = ImageStructType CUInt CUInt (Ptr CUChar)

instance Storable ImageStructType where
  sizeOf _ = 12
  alignment = sizeOf
  peek ptr = do
    w <- peekByteOff ptr 0
    h <- peekByteOff ptr 4
    p <- peekByteOff ptr 8
    return (ImageStructType w h p)

imageStructTypeToImage :: ImageStructType -> IO Image
imageStructTypeToImage (ImageStructType (CUInt width) (CUInt height) p) = do
  pixelsCUChar <- peekArray (fromIntegral $ width * height) p
  let pixels = map (\(CUChar c) -> fromIntegral c) pixelsCUChar
  return $ Image width height (V.fromList pixels)

doWithImage :: Image -> String
doWithImage (Image w h p) =
  intercalate " " [show w, show h, show $ V.sum p]

doWithImageStruct :: ImageStruct -> IO CString
doWithImageStruct is = do
  imageStruct <- peek is
  image <- imageStructTypeToImage imageStruct
  newCString $ doWithImage image

freeResult :: CString -> IO ()
freeResult s = free s

// StartEnd.c
#include <Rts.h>

void HsStart()
{
   int argc = 1;
   char* argv[] = {"ghcDll", NULL}; // argv must end with NULL

   // Initialize Haskell runtime
   char** args = argv;
   hs_init(&argc, &args);
}

void HsEnd()
{
   hs_exit();
}

编译

ghc -Wall -O2 -outputdir build -shared -o build\Stuff.dll Stuff.hs StartEnd.c

cpp 部分 (MSVC 2010) 如下所示:

// main.cpp
// link with /OPT:NOREF

#pragma comment(lib,"Stuff.dll.a")
#include "HsFFI.h"
#include "Stuff_stub.h"
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

extern "C" {
    void HsStart();
    void HsEnd();
}

struct Image {
    Image( std::uint32_t w, std::uint32_t h, std::uint8_t* p )
        : width_( w ), height_( h ), pixels_( p )
    {}
    std::uint32_t width_;
    std::uint32_t height_;
    std::uint8_t* pixels_;
};

int main()
{
    using namespace std;

    HsStart();

    // create image
    const uint32_t width = 320;
    const uint32_t height = 240;
    vector<uint8_t> mem( width * height, 10 );
    mem[1] = 13;
    Image image( width, height, &mem[0] );

    // Send Image to Haskell and receive a String.
    auto resultPtr = doWithImageStruct( &image );
    string result( reinterpret_cast<char*>( resultPtr ) );
    freeResult( resultPtr );

    cout << result << "\n";

    HsEnd();
}

输出符合预期:

320 240 768003.0

我的问题是:这是正确的做法吗?或者它现在没有崩溃只是纯粹的运气,实际上我有未定义的行为?

编辑:我修复了上面的代码,以便为该线程的 future 读者展示固定位宽整数的正确用法。

最佳答案

我建议您使用 C->Hs生成 Storable ImageStructType 实例。其他一切看起来都不错。

关于c++ - 将图像从 C++ 传递给 Haskell 并取回一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095561/

相关文章:

c - 结构中的数组在初始化后充满了垃圾

c++ - clang (MacOS 32/64) : disable stack frames in some functions

arrays - 返回数组中元素的索引 Excel VBA

c++ - 我可以将 nullptr 存储到 bool 中吗?

php - 有没有办法从单独的行创建数组?

python - .txt 文件中的 NumPy 数组太大,无法加载到内存中

c++ - 链表插入错误

c++ - 构造函数返回值中的异常

c++ - 指针的地址是什么意思?

c++ - 我可以强制可变参数模板采用特定类型的参数吗