Skip to content

Commit

Permalink
Use 2x resolution sprites for buttons, improve formatting of log output
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Feb 1, 2025
1 parent 6a2b4e0 commit bd5fde3
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 65 deletions.
Binary file modified images/words.afdesign
Binary file not shown.
Binary file added images/words@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 43 additions & 35 deletions source/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ import Brillo (
Display (InWindow),
Picture (
Bitmap,
ThickLineSmooth,
Pictures,
Scale,
ThickArc,
ThickCircle,
ThickLineSmooth,
Translate
),
Point,
Expand Down Expand Up @@ -117,7 +117,7 @@ import Linear (M33, V2 (V2), V3 (V3), V4 (V4), (!*))

import Correct (calculatePerspectiveTransform, determineSize)
import Home (handleHomeEvent)
import SimpleCV (Corners (..), applyMatrix3x3)
import SimpleCV (Corners (..), applyMatrix3x3, prettyShowCorners, prettyShowMatrix3x3)
import SimpleCV qualified as SCV
import Types (
AppState (..),
Expand All @@ -140,6 +140,7 @@ import Utils (
getWordSprite,
loadFileIntoState,
loadImage,
prettyPrintArray,
)


Expand Down Expand Up @@ -221,7 +222,7 @@ stepWorld :: Float -> AppState -> IO AppState
stepWorld _ appState =
if not appState.isRegistered
&& ( fromIntegral appState.tickCounter
< (bannerTime * fromIntegral ticksPerSecond)
< (bannerTime * fromIntegral ticksPerSecond)
)
then pure appState{tickCounter = appState.tickCounter + 1}
else pure appState{bannerIsVisible = False}
Expand Down Expand Up @@ -377,34 +378,34 @@ makePicture appState =
<&> Translate (-(sidebarWidthInteg / 2.0)) 0
)
<> [ drawSidebar
appWidthInteg
appState.appHeight
appState.sidebarWidth
appWidthInteg
appState.appHeight
appState.sidebarWidth
]
<> P.zipWith
(drawUiComponent appState)
appState.uiComponents
[0 ..]
<> [ if appState.bannerIsVisible
then Scale 0.5 0.5 bannerImage
else mempty
then Scale 0.5 0.5 bannerImage
else mempty
, if appState.bannerIsVisible
then
Translate 300 (-250) $
Scale 0.2 0.2 $
ThickArc
0 -- Start angle
-- End angle
( ( fromIntegral appState.tickCounter
/ (bannerTime * fromIntegral ticksPerSecond)
)
* 360
)
50 -- Radius
100 -- Thickness
-- \$
-- -
else mempty
then
Translate 300 (-250) $
Scale 0.2 0.2 $
ThickArc
0 -- Start angle
-- End angle
( ( fromIntegral appState.tickCounter
/ (bannerTime * fromIntegral ticksPerSecond)
)
* 360
)
50 -- Radius
100 -- Thickness
-- \$
-- -
else mempty
]
BannerView -> pure $ Pictures []

Expand Down Expand Up @@ -551,12 +552,10 @@ submitSelection appState exportMode = do
targetShape
exportMode

if appState.transformBackend == ImageMagickBackend
then
putText $
"Arguments for convert command:\n"
<> T.unlines convertArgs
else putText $ "Write file to " <> show image.outputPath
when (appState.transformBackend == ImageMagickBackend) $ do
putText $
"Arguments for convert command:\n"
<> T.unlines convertArgs

correctAndWrite
appState.transformBackend
Expand Down Expand Up @@ -888,8 +887,11 @@ correctAndWrite transformBackend inPath outPath ((bl, _), (tl, _), (tr, _), (br,
, bl_y = int2Double height
}

putText $ "Source corners: " <> show srcCorners
putText $ "Destination corners: " <> show dstCorners
putText "\nSource Corners:"
putText $ prettyShowCorners srcCorners

putText "\nDestination Corners:"
putText $ dstCorners & prettyShowCorners & T.replace ".0" ""

srcCornersPtr <- new srcCorners
dstCornersPtr <- new dstCorners
Expand All @@ -899,15 +901,18 @@ correctAndWrite transformBackend inPath outPath ((bl, _), (tl, _), (tr, _), (br,
free dstCornersPtr
transMat <- peek transMatPtr

putText $ "Transformation matrix: " <> show transMat
putText "\nTransformation Matrix:"
putText $ prettyShowMatrix3x3 transMat

pictureMetadataEither <- loadImage inPath
case pictureMetadataEither of
Left error -> do
free transMatPtr
P.putText error
Right (Bitmap bitmapData, metadata) -> do
P.print ("metadata" :: P.Text, metadata)
Right (Bitmap bitmapData, metadatas) -> do
P.putText "" -- Line break
prettyPrintArray metadatas

let
srcWidth = P.fst bitmapData.bitmapSize
srcHeight = P.snd bitmapData.bitmapSize
Expand Down Expand Up @@ -948,6 +953,9 @@ correctAndWrite transformBackend inPath outPath ((bl, _), (tl, _), (tr, _), (br,
bwImgForeignPtr <- newForeignPtr_ (castPtr bwImgPtr)
let bwImg = imageFromUnsafePtr width height bwImgForeignPtr
savePngImage pngOutPath (ImageRGBA8 bwImg)

putText $ "\n✅ Wrote file to:"
P.putStrLn pngOutPath
--
Right _ -> do
free transMatPtr
Expand Down
24 changes: 24 additions & 0 deletions source/SimpleCV.chs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import Protolude (
IO,
Bool,
Ptr,
show,
realToFrac,
Show,
return,
(*),
($),
(<>),
(>>=),
)

import Data.Text as T
import Foreign.C.Types (CUChar)
import Foreign.Ptr (castPtr)
import Foreign.Storable (Storable(..))
import Text.Printf (printf)

#include "simplecv.h"
#include "perspectivetransform.h"
Expand All @@ -34,6 +39,13 @@ data Corners = Corners
} deriving (Show)
{#pointer *Corners as CornersPtr foreign -> Corners#}


prettyShowCorners :: Corners -> Text
prettyShowCorners Corners{..} =
(show (tl_x, tl_y) <> " " <> show (tr_x, tr_y)) <> "\n" <>
(show (bl_x, bl_y) <> " " <> show (br_x, br_y))


data Matrix3x3 = Matrix3x3
{ m00 :: Double
, m01 :: Double
Expand All @@ -47,6 +59,18 @@ data Matrix3x3 = Matrix3x3
} deriving (Show)
{#pointer *Matrix3x3 as Matrix3x3Ptr foreign -> Matrix3x3#}


prettyShowMatrix3x3 :: Matrix3x3 -> Text
prettyShowMatrix3x3 Matrix3x3{..} =
let
fNum num = printf "% .5f " num
in
T.pack $
fNum m00 <> " " <> fNum m01 <> " " <> fNum m02 <> "\n" <>
fNum m10 <> " " <> fNum m11 <> " " <> fNum m12 <> "\n" <>
fNum m20 <> " " <> fNum m21 <> " " <> fNum m22


instance Storable Corners where
sizeOf _ = 8 * sizeOf (0.0 :: Double)
alignment _ = alignment (0.0 :: Double)
Expand Down
92 changes: 62 additions & 30 deletions source/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ import Brillo.Juicy (fromDynamicImage, loadJuicyWithMetadata)
import Codec.Picture (decodePng)
import Codec.Picture.Metadata (Keys (Exif), Metadatas, lookup)
import Codec.Picture.Metadata.Exif (ExifData (ExifShort), ExifTag (..))
import Control.Arrow ((>>>))
import Data.Aeson qualified as Aeson
import Data.Bifunctor (bimap)
import Data.ByteString.Lazy qualified as BL
import Data.FileEmbed (embedFile)
import Data.Text qualified as T
Expand All @@ -61,47 +63,59 @@ import System.FilePath (replaceBaseName, takeBaseName, takeExtension, (</>))
import System.Info (os)
import System.Process (readProcessWithExitCode)

import Brillo.Data.Picture (Picture (Scale))
import Types (AppState (..), Coordinate (..), Corner, ImageData (..), View (..))


wordsSprite :: ByteString
wordsSprite = $(embedFile "images/words.png")
-- | Embed the words sprite image with a scale factor of 2
wordsSprite :: (ByteString, Float)
wordsSprite = ($(embedFile "images/words@2x.png"), 2)


wordsPic :: Picture
wordsPic =
fromMaybe
mempty
( either (const Nothing) Just (decodePng wordsSprite)
>>= fromDynamicImage
)
fromMaybe mempty $
either (const Nothing) Just (decodePng $ P.fst wordsSprite)
>>= fromDynamicImage


{-| `rectPos` is the position of the content
`rectSize` ist the size of the content
-}
getWordSprite :: Text -> Picture
getWordSprite spriteText =
getWordSprite spriteText = do
let
scaleFactor = 1 / P.snd wordsSprite
scaleVal = fromIntegral >>> (* P.snd wordsSprite) >>> round
scaleRect rect =
Rectangle
{ rectPos = bimap scaleVal scaleVal rect.rectPos
, rectSize = bimap scaleVal scaleVal rect.rectSize
}
case wordsPic of
Bitmap bitmapData -> case spriteText of
"Save" ->
BitmapSection
Rectangle{rectPos = (-5, 40), rectSize = (150, 20)}
bitmapData
"Save BW" ->
BitmapSection
Rectangle{rectPos = (-5, 60), rectSize = (150, 20)}
bitmapData
"Save Gray" ->
BitmapSection
Rectangle{rectPos = (-5, 80), rectSize = (150, 20)}
bitmapData
"Select Files" ->
BitmapSection
Rectangle{rectPos = (-5, 140), rectSize = (150, 20)}
bitmapData
"Save BW Smooth" ->
BitmapSection
Rectangle{rectPos = (-5, 160), rectSize = (150, 20)}
bitmapData
_ -> mempty
Bitmap bitmapData ->
Scale scaleFactor scaleFactor $ case spriteText of
"Save" ->
BitmapSection
(scaleRect Rectangle{rectPos = (0, 40), rectSize = (40, 20)})
bitmapData
"Save BW" ->
BitmapSection
(scaleRect Rectangle{rectPos = (0, 60), rectSize = (74, 20)})
bitmapData
"Save Gray" ->
BitmapSection
(scaleRect Rectangle{rectPos = (0, 80), rectSize = (84, 20)})
bitmapData
"Select Files" ->
BitmapSection
(scaleRect Rectangle{rectPos = (0, 140), rectSize = (92, 20)})
bitmapData
"Save BW Smooth" ->
BitmapSection
(scaleRect Rectangle{rectPos = (0, 160), rectSize = (140, 20)})
bitmapData
_ -> mempty
_ -> mempty


Expand Down Expand Up @@ -371,3 +385,21 @@ loadFileIntoState appState = do
"Error: Loaded file is not a Bitmap image. "
<> "This error should not be possible."
pure appState


prettyPrintArray :: (P.Show a) => a -> IO ()
prettyPrintArray =
show
>>> T.replace "[" "\n[ "
>>> T.replace "]" "\n] "
>>> T.replace "," "\n, "
>>> P.putText


prettyPrintRecord :: (P.Show a) => a -> IO ()
prettyPrintRecord =
show
>>> T.replace "{" "\n{ "
>>> T.replace "}" "\n} "
>>> T.replace "," "\n,"
>>> P.putText

0 comments on commit bd5fde3

Please sign in to comment.