Файл .BMP: анализ и преобразование - PullRequest
1 голос
/ 25 марта 2019

Моя цель - иметь возможность анализировать и преобразовывать графические файлы, ориентируясь на цветовую матрицу.

  1. Лучше ли работать с матрицами Word8 или Integer?
  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
...