1

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
Alberto Capitani
  • 1,039
  • 13
  • 30
  • 1
    I do not see any pure function. Do you have any reason for that? – Elmex80s Mar 25 '19 at 16:02
  • I think I don't understand your observation well. However, if you meant to say that there are no functions for manipulating an rgba matrix, this is due to the fact that for me the functions I published serve as a working "environment". If instead you mean that Matrix is bound to the Integer type (and not for example to the Int type) this is because the Matrix conversion function (matrix library) requires the Integer type. I think that working with Array I wouldn't have this constraint. – Alberto Capitani Mar 25 '19 at 19:48
  • Do you have any real problem? – Elmex80s Mar 25 '19 at 22:18
  • What is `Matrix`? I would expect `Matrix Integer` instead of `Matrix [Integer]`. – Elmex80s Mar 25 '19 at 22:20
  • 1
    I voted to close as too broad, because it is. But briefly: `Is it better to operate a Word8 or Integer matrix?` `Word8` can unbox while `Integer` won't. `How else can we switch from ByteString to a two-dimensional matrix?` There are infinite ways, why do you ask? `Are the procedures used the most efficient?` No. Lists are obviously not going to be "most efficient" - consider packed structures of word8 values. Consider smaller, notably self contained, examples. – Thomas M. DuBuisson Mar 26 '19 at 01:22
  • @ThomasM.DuBuisson : 1 (Switch from ByteString to a two-dimensional matrix): In Matrix library I only found "fold..." and "mapAccum..." with type ... ByteString -> a. The last one seemed inappropriate. You say that "there are infinite ways" to do it; please can you tell me just one that is better than the one chosen by me? – Alberto Capitani Mar 26 '19 at 06:42
  • @Elmex80s : Matrix [Integer] are list of rgba value. Ex. [255,100,30,0]. Regardless of whether to keep Word8 or Integer or Int in the matrix, I think it would be better to replace the list of the four RGBA values with a record structure. – Alberto Capitani Mar 26 '19 at 06:46
  • @ThomasM.DuBuisson : I chose to use Integers instead of Word8 because it seemed difficult to use the mathematical functions with the latter type. For example, if I want to halve a Red value, I can easily use the "div" operator with the Integer type; I don't think this is as easy as using the Word8 type. it's because I'm not sure and I may be wrong that I ask the forum for help. – Alberto Capitani Mar 26 '19 at 07:03

0 Answers0