          imageSize = fromIntegral $ w * h * 3
        }
    let p' = pixelBytes (toList p) w
    handle <- openFile filename WriteMode
    writeHeader handle header
    B.hPut handle p'
    hClose handle

  writeHeader :: Handle ->  BMPHeader -> IO ()
  writeHeader handle header = do
    B.hPut handle . signature $ header