.BMP文件:分析和转换

时间:2019-03-25 08:19:37

标签: haskell bmp bytestring

我的目标是能够分析和转换专注于颜色矩阵的图形文件。

  1. 操作Word8或整数矩阵更好吗?
  2. 我们还能如何从ByteString转换为二维矩阵?
  3. 使用的程序最有效吗?

bmpToMatrix :: FilePath -> IO (Matrix [Integer])
bmpToMatrix input = do
    Right bmp  <- readBMP input
    let rgbas   =  unpackBMPToRGBA32 bmp
        (width, height) = bmpDimensions bmp
        integers =  BS.foldr ((:) . toInteger) [] rgbas
    return $ MT.fromList height width $ SP.chunksOf 4 integers

bmpEdit :: (Matrix [Integer] -> Matrix [Integer]) -> FilePath -> FilePath -> IO ()
bmpEdit f input output = do
    matrix <- bmpToMatrix input
    let matrix' = f matrix
    matrixToBMP output matrix'

matrixToByteString :: Matrix [Integer] -> ByteString 
matrixToByteString = BS.pack . L.concatMap (L.map fromIntegral) . MT.toList

matrixToBMP :: FilePath -> Matrix [Integer] -> IO ()
matrixToBMP output mt =
    writeBMP output $ packRGBA32ToBMP (ncols mt) (nrows mt) $ matrixToByteString mt

0 个答案:

没有答案