我的目标是能够分析和转换专注于颜色矩阵的图形文件。
?
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