Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:34:43+00:00 2026-05-15T10:34:43+00:00

I am trying to load a PNG file, get the uncompressed RGBA bytes, then

  • 0

I am trying to load a PNG file, get the uncompressed RGBA bytes, then send them to the gzip or zlib packages.

The pngload package returns image data as a (StorableArray (Int, Int) Word8), and the compression packages take lazy ByteStrings. Therefore, I am attempting to build a (StorableArray (Int, Int) Word8 -> ByteString) function.

So far, I have tried the following:

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)]

This causes the stack to run out of memory, so clearly I am doing something very wrong. There are more things I could try with Ptr’s and ForeignPtr’s, but there are a lot of “unsafe” functions in there.

Any help here would be appreciated; I’m fairly stumped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:34:44+00:00Added an answer on May 15, 2026 at 10:34 am

    Generally, pack and unpack are a bad idea for performance. If you have a Ptr, and a length in bytes, you can generate a strict bytestring in two different ways:

    • packCStringLen
    • unsafePackCStringLen

    Like this:

    import qualified Codec.Image.PNG as PNG
    import Control.Monad
    import Data.Array.Storable (withStorableArray)
    
    import Codec.Compression.GZip
    
    import qualified Data.ByteString.Lazy   as L
    import qualified Data.ByteString.Unsafe as S
    
    import Data.Word
    import Foreign
    
    -- Pack a Ptr Word8 as a strict bytestring, then box it to a lazy one, very
    -- efficiently
    bytesFromPointer :: Int -> Ptr Word8 -> IO L.ByteString
    bytesFromPointer n ptr = do
        s <- S.unsafePackCStringLen (castPtr ptr, n)
        return $! L.fromChunks [s]
    
    -- Dummies, since they were not provided 
    image = undefined
    lengthOfImageData = 10^3
    
    -- Load a PNG, and compress it, writing it back to disk
    main = do
        bytes <- withStorableArray
            (PNG.imageData image)
            (bytesFromPointer lengthOfImageData)
        L.writeFile "foo" . compress $ bytes
    

    I’m using the O(1) version, that just repackages the Ptr from the StorableArray. You might wish to copy it first, via “packCStringLen”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems trying to load png file into my application. It seems to
WinRT project, C++/CX. I'm trying to load a PNG file into a WriteableBitmap. I'm
Trying to load a small .txt file into mysql but get all my data
I trying to load BitmapImage from bmp file. When i load png or jpg
I am trying to load into a scrollview 50 png images, and I chose
Trying to load content into a div, then on click load the previous content
I am trying to load all the .png files from an internal application folder
I'm trying to load a custom cursor from a resource file (.rc) in Visual
I'm trying to get QImage (or anything in PyQt4, for that matter) to load
I am trying to post the $GLABALS variable to another php file and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.