File .BMP: analysis and transformation

77 Views Asked by At

My goal is to be able to analyze and transform graphic files focusing on the color matrix.

  1. Is it better to operate a Word8 or Integer matrix?
  2. How else can we switch from ByteString to a two-dimensional matrix?
  3. Are the procedures used the most efficient?

?

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

There are 0 best solutions below