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 8482587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:51:44+00:00 2026-06-10T19:51:44+00:00

I have PNG files and the Gloss library has a Bitmap constructor for Picture

  • 0

I have PNG files and the Gloss library has a Bitmap constructor for Picture. I can’t use loadBMP :: FilePath -> IO Picture because of the filetype, so I’m searching how to load a PNG file, convert it to BMP, and feed it to either bitmapOfBMP :: BMP -> Picture, bitmapOfForeignPtr :: Int -> Int -> ForeignPtr Word8 -> Bool -> Picture or bitmapOfByteString :: Int -> Int -> ByteString -> Bool -> Picture.


Test with JuicyPixels

import Data.ByteString as B
import System.IO as A

import Codec.Picture.Png
import Graphics.Gloss.Interface.Pure.Game


main = do
    png <- B.readFile "samus.png"
    let img = decodePng png
    case img of
        Left x -> A.putStrLn x
        Right x -> do
            let bmp = encodeDynamicPng x
            case bmp of
                Left x -> A.putStrLn x
                Right x -> do
                    let pic = bitmapOfByteString 29 52 x True
                    game pic

game pic
    =  play
        (InWindow "Test" (700, 500) (10, 10))
        white
        30
        pic
        draw
        (const id)
        (const id)

draw bmp
    = bmp

Everything succeeds but the image is not the same at all.

  • 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-06-10T19:51:44+00:00Added an answer on June 10, 2026 at 7:51 pm

    Even though I didn’t come with the answer alone, but thanks to Thomas’ answer, I will post it here instead of inside the question.

    As a reminder, the goal is to convert a BMP file into a Gloss’ Picture, so I wrote a function called bmpToPic. I put it in a module, because it uses two others functions, and needs many imports.
    Also, repaToPicture from Thomas’ answer is slighty different here.

    module PngToPic
        (pngToPic)
        where
    
    import Data.ByteString as B
    import Data.Word
    
    import Codec.Picture.Png
    import Codec.Picture.Repa
    import qualified Data.ByteString.Internal as BI
    import Data.Array.Repa ((:.)(..), Z, Z(..), extent, DIM3, Array)
    import qualified Data.Array.Repa as R
    import qualified Data.Array.Repa.Repr.ForeignPtr as F
    import Graphics.Gloss.Data.Picture
    
    
    pngToPic :: ByteString -> Picture
    pngToPic png
        = let
            Right img -- unsafe
                = decodePng png
            repa
                = imgData (convertImage img :: Img RGBA)
        in repaToPicture True repa
    
    repaToPicture :: Bool -> Array F.F DIM3 Word8 -> Picture
    repaToPicture b arr
        = bitmapOfByteString row col bs b
        where
            bs
                = BI.fromForeignPtr fptr 0 (R.size sh)
            fptr
                = F.toForeignPtr arr'
            sh@(Z :. col :. row :. depth)
                = extent arr'
            arr'
                = flipVert arr
    
    flipVert :: Array F.F DIM3 Word8 -> Array F.F DIM3 Word8
    flipVert g
        = R.computeS $ R.backpermute e flop g
        where
            e@(Z :. x :. y :. _)
                = extent g
            flop (Z :. i         :. j         :. k)
                = Z :. x - i - 1 :. j :. k
    

    You use it like that:

    import Data.ByteString as B
    
    import Graphics.Gloss.Interface.Pure.Game
    
    import PngToPic
    
    main = do
        png <- B.readFile "someImage.png"
        game $ pngToPic png
    
    game pic
        = play
            (InWindow "Test" (700, 500) (10, 10))
            white
            30
            pic
            id
            (const id)
            (const id)
    

    and the image will show up in the middle of the window.

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

Sidebar

Related Questions

I have generated a bunch of png files to use as markers on my
I have a folder on my remote server that has a few .png files
I have a folder in my WPF app Images that has several .png files
I have like 45 PNG files that I want to use in a animation
I have a Bitmap variable and I copy smaller 32x32 png files (loaded as
I have a series of PNG files I need to use in a Flash
I have around 1 Gig of image .png files in my HDFS. Can anyone
I have several png image files with names like this - house_number_5.png house_number_512.png house_number_52352.png
Help needed! I have 10 different .png files and i want to display them
I have a Flash web app which displays user submitted PNG files. Files are

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.