~alcinnz/Mondrian

ad09e4dcc12102bec9cd36e59862157cd284142f — Adrian Cochrane 1 year, 4 months ago 9454742
Fix parsing of background-image, attempt to fix image rendering.
2 files changed, 12 insertions(+), 3 deletions(-)

M lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs
M lib/Graphics/Rendering/Rect/Image.hs
M lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs => lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs +1 -0
@@ 40,6 40,7 @@ instance PropertyParser (Backgrounds Text) where
        inner [Ident "none"] = Just ""
        inner [Ident "initial"] = Just ""
        inner [Url ret] = Just ret
        inner [Function "url", String ret, RightParen] = Just ret
        inner _ = Nothing
    longhand _ _ _ _ = Nothing


M lib/Graphics/Rendering/Rect/Image.hs => lib/Graphics/Rendering/Rect/Image.hs +11 -3
@@ 4,7 4,8 @@ module Graphics.Rendering.Rect.Image(

import qualified Data.HashMap.Lazy as HM
import Data.Text (Text)
import Codec.Picture (DynamicImage(..), Image(..), PixelRGBA8(..), generateImage)
import Codec.Picture (DynamicImage(..), Image(..), PixelRGBA8(..), PixelYCbCr8(..),
                        pixelMap, generateImage)
import Codec.Picture.Types (promoteImage, dynamicMap)

import Control.Monad.IO.Class (MonadIO(..))


@@ 36,7 37,7 @@ buildAtlas cb srcs = do

    forM (zip textures imgs) $ \(texture, dyn) -> do
        -- NOTE: `unsafe` crashes with a divide-by-zero given a `Vector ()`
        let img = dynamicMap (unsafeCast . imageData) dyn :: Vector Word
        let img = dynamicMap (unsafeCast . imageData) $ adjustGL dyn :: Vector Word
        let (format, word) = glFormat dyn
        liftIO $ glBindTexture GL_TEXTURE_2D texture
        liftIO $ unsafeWith img $ -- FIXME: Crashes


@@ 44,6 45,10 @@ buildAtlas cb srcs = do
                    (toEnum $ dynamicMap imageWidth dyn)
                    (toEnum $ dynamicMap imageHeight dyn)
                    0 format word . castPtr
        liftIO $ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR
        liftIO $ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR
        liftIO $ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT
        liftIO $ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT

    return $ Atlas $ HM.fromList $ zip srcs textures



@@ 51,7 56,10 @@ data Texture = Texture GLuint
atlasLookup :: Text -> Atlas -> Texture
atlasLookup key = Texture . fromMaybe 0 . HM.lookup key . unAtlas


-- OpenGL deals in CrCb not CbCr...
adjustGL (ImageYCbCr8 img) = ImageYCbCr8 $ pixelMap swapCrCb img
  where swapCrCb (PixelYCbCr8 y cb cr) = PixelYCbCr8 y cr cb
adjustGL img = img
glFormat (ImageY8 _) = (GL_LUMINANCE, GL_UNSIGNED_BYTE)
glFormat (ImageY16 _) = (GL_LUMINANCE, GL_UNSIGNED_SHORT)
glFormat (ImageY32 _) = (GL_LUMINANCE, GL_UNSIGNED_INT)