我想加载一个PNG文件,得到未压缩的RGBA字节,然后将它们发送到gzip或zlib软件包。如何将一个(StorableArray(Int,Int)Word8)转换为一个懒惰的ByteString?
pngload包将图像数据作为(StorableArray(Int,Int)Word8)返回,并且压缩包会使用惰性ByteStrings。因此,我试图构建一个(StorableArray(Int,Int)Word8 - > ByteString)函数。
到目前为止,我已经试过如下:
import qualified Codec.Image.PNG as PNG
import Control.Monad (mapM)
import Data.Array.Storable (withStorableArray)
import qualified Data.ByteString.Lazy as LB (ByteString, pack, take)
import Data.Word (Word8)
import Foreign (Ptr, peekByteOff)
main = do
-- Load PNG into "image"...
bytes <- withStorableArray
(PNG.imageData image)
(bytesFromPointer lengthOfImageData)
bytesFromPointer :: Int -> Ptr Word8 -> IO LB.ByteString
bytesFromPointer count pointer = LB.pack $
mapM (peekByteOff pointer) [0..(count-1)]
这将导致堆栈耗尽内存,这么清楚我做的东西非常错误的。我可以用Ptr和ForeignPtr尝试更多的东西,但是那里有很多“不安全”的功能。
任何帮助在这里将不胜感激;我很难过。
这工作得很好。谢谢您的帮助! – 2010-06-27 18:13:48