| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 3 |
{-# LANGUAGE EmptyDataDecls #-} |
| 4 |
{-# LANGUAGE FlexibleContexts #-} |
| 5 |
{-# LANGUAGE GADTs #-} |
| 6 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 7 |
{-# LANGUAGE TypeFamilies #-} |
| 8 |
{-# LANGUAGE UndecidableInstances #-} |
| 9 |
|
| 10 |
----------------------------------------------------------------------------- |
| 11 |
-- | |
| 12 |
-- Module : Diagrams.TwoD.Image |
| 13 |
-- Copyright : (c) 2011 diagrams-lib team (see LICENSE) |
| 14 |
-- License : BSD-style (see LICENSE) |
| 15 |
-- Maintainer : [email protected] |
| 16 |
-- |
| 17 |
-- Importing external images into diagrams. Usage example: To create |
| 18 |
-- a diagram from an embedded image with width 1 and height set |
| 19 |
-- according to the aspect ratio, use @image img # scaleUToX 1@, where |
| 20 |
-- @img@ is a value of type @DImage n e@, created with a function like |
| 21 |
-- 'loadImageEmb', 'loadImageExt', or 'raster'. |
| 22 |
----------------------------------------------------------------------------- |
| 23 |
|
| 24 |
module Diagrams.TwoD.Image |
| 25 |
( |
| 26 |
DImage(..), ImageData(..) |
| 27 |
, Embedded, External, Native |
| 28 |
, image |
| 29 |
, embeddedImage |
| 30 |
, loadImageEmb |
| 31 |
, loadImageEmbBS |
| 32 |
, loadImageExt |
| 33 |
, uncheckedImageRef |
| 34 |
, raster |
| 35 |
, rasterDia |
| 36 |
) where |
| 37 |
|
| 38 |
import Codec.Picture |
| 39 |
|
| 40 |
import Data.Colour (AlphaColour) |
| 41 |
import Data.Kind (Type) |
| 42 |
import Data.Semigroup |
| 43 |
import Data.Typeable (Typeable) |
| 44 |
|
| 45 |
import Diagrams.Core |
| 46 |
|
| 47 |
import Diagrams.Attributes (colorToSRGBA) |
| 48 |
import Diagrams.Path (Path) |
| 49 |
import Diagrams.Query |
| 50 |
import Diagrams.TwoD.Path (isInsideEvenOdd) |
| 51 |
import Diagrams.TwoD.Shapes (rect) |
| 52 |
import Diagrams.TwoD.Types |
| 53 |
|
| 54 |
import Data.ByteString |
| 55 |
|
| 56 |
import Linear.Affine |
| 57 |
|
| 58 |
data Embedded deriving Typeable |
| 59 |
data External deriving Typeable |
| 60 |
data Native (t :: Type) deriving Typeable |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
data ImageData :: Type -> Type where |
| 66 |
ImageRaster :: DynamicImage -> ImageData Embedded |
| 67 |
ImageRef :: FilePath -> ImageData External |
| 68 |
ImageNative :: t -> ImageData (Native t) |
| 69 |
|
| 70 |
------------------------------------------------------------------------------- |
| 71 |
-- | An image primitive, the two ints are width followed by height. |
| 72 |
-- Will typically be created by @loadImageEmb@ or @loadImageExt@ which, |
| 73 |
-- will handle setting the width and height to the actual width and height |
| 74 |
-- of the image. |
| 75 |
data DImage :: Type -> Type -> Type where |
| 76 |
DImage :: ImageData t -> Int -> Int -> Transformation V2 n -> DImage n t |
| 77 |
deriving Typeable |
| 78 |
|
| 79 |
type instance V (DImage n a) = V2 |
| 80 |
type instance N (DImage n a) = n |
| 81 |
|
| 82 |
instance RealFloat n => HasQuery (DImage n a) Any where |
| 83 |
getQuery (DImage _ w h _) = -- transform t $ |
| 84 |
Query $ \p -> Any (isInsideEvenOdd r p) |
| 85 |
where |
| 86 |
r = rectPath (fromIntegral w) (fromIntegral h) |
| 87 |
|
| 88 |
instance Fractional n => Transformable (DImage n a) where |
| 89 |
transform t1 (DImage iD w h t2) = DImage iD w h (t1 <> t2) |
| 90 |
|
| 91 |
instance Fractional n => HasOrigin (DImage n a) where |
| 92 |
moveOriginTo p = translate (origin .-. p) |
| 93 |
|
| 94 |
|
| 95 |
image :: (TypeableFloat n, Typeable a, Renderable (DImage n a) b) |
| 96 |
=> DImage n a -> QDiagram b V2 n Any |
| 97 |
image img |
| 98 |
= mkQD (Prim img) |
| 99 |
(getEnvelope r) |
| 100 |
(getTrace r) |
| 101 |
mempty |
| 102 |
(Query $ \p -> Any (isInsideEvenOdd r p)) |
| 103 |
where |
| 104 |
r = rectPath (fromIntegral w) (fromIntegral h) |
| 105 |
-- should we use the transform here? |
| 106 |
DImage _ w h _ = img |
| 107 |
|
| 108 |
rectPath :: RealFloat n => n -> n -> Path V2 n |
| 109 |
rectPath = rect |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
embeddedImage :: Num n => DynamicImage -> DImage n Embedded |
| 114 |
embeddedImage img = DImage (ImageRaster img) w h mempty |
| 115 |
where |
| 116 |
w = dynamicMap imageWidth img |
| 117 |
h = dynamicMap imageHeight img |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
loadImageEmb :: Num n => FilePath -> IO (Either String (DImage n Embedded)) |
| 122 |
loadImageEmb path = fmap embeddedImage `fmap` readImage path |
| 123 |
|
| 124 |
|
| 125 |
loadImageEmbBS :: Num n => ByteString -> Either String (DImage n Embedded) |
| 126 |
loadImageEmbBS bs = embeddedImage `fmap` decodeImage bs |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
loadImageExt :: Num n => FilePath -> IO (Either String (DImage n External)) |
| 132 |
loadImageExt path = do |
| 133 |
dImg <- readImage path |
| 134 |
return $ case dImg of |
| 135 |
Left msg -> Left msg |
| 136 |
Right img -> Right $ DImage (ImageRef path) w h mempty |
| 137 |
where |
| 138 |
w = dynamicMap imageWidth img |
| 139 |
h = dynamicMap imageHeight img |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
uncheckedImageRef :: Num n => FilePath -> Int -> Int -> DImage n External |
| 145 |
uncheckedImageRef path w h = DImage (ImageRef path) w h mempty |
| 146 |
|
| 147 |
|
| 148 |
rasterDia :: (TypeableFloat n, Renderable (DImage n Embedded) b) |
| 149 |
=> (Int -> Int -> AlphaColour Double) -> Int -> Int -> QDiagram b V2 n Any |
| 150 |
rasterDia f w h = image $ raster f w h |
| 151 |
|
| 152 |
|
| 153 |
raster :: Num n => (Int -> Int -> AlphaColour Double) -> Int -> Int -> DImage n Embedded |
| 154 |
raster f w h = DImage (ImageRaster (ImageRGBA8 img)) w h mempty |
| 155 |
where |
| 156 |
img = generateImage g w h |
| 157 |
g x y = fromAlphaColour $ f x y |
| 158 |
|
| 159 |
fromAlphaColour :: AlphaColour Double -> PixelRGBA8 |
| 160 |
fromAlphaColour c = PixelRGBA8 r g b a |
| 161 |
where |
| 162 |
(r, g, b, a) = (int r', int g', int b', int a') |
| 163 |
(r', g', b', a') = colorToSRGBA c |
| 164 |
int x = round (255 * x) |
| 165 |
|
| 166 |
instance Fractional n => (Renderable (DImage n a) NullBackend) where |
| 167 |
render _ _ = mempty |
| 168 |
|