Skip to content

Commit

Permalink
IHaskell in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Jun 18, 2020
1 parent 6b623b3 commit 01a4741
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 30 deletions.
2 changes: 1 addition & 1 deletion hip/hip.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ library
default-language: Haskell2010
hs-source-dirs: src

build-depends: base >= 4.8 && < 5
build-depends: base >= 4.11 && < 5
, deepseq
, massiv >= 0.5
, massiv-io >= 0.2.2
Expand Down
60 changes: 31 additions & 29 deletions hip/src/Graphics/Image/Processing/Geometric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -340,45 +340,47 @@ resize :: (RealFloat e, ColorModel cs e, Interpolation method) =>
-> Sz2 -- ^ Dimensions of a result image.
-> Image cs e -- ^ Source image.
-> Image cs e -- ^ Result image.
resize method border sz'@(Sz2 m' n') (Image arr) =
-- Image (A.makeArray (A.getComp arr) sz' getNewPx)
-- where
-- sz@(Sz2 m n) = A.size arr
-- !fM = fromIntegral m' / fromIntegral m
-- !fN = fromIntegral n' / fromIntegral n
-- getNewPx (i :. j) =
-- interpolate
-- method
-- (A.handleBorderIndex border sz (A.index' arr))
-- ( (fromIntegral i + 0.5) / fM - 0.5
-- , (fromIntegral j + 0.5) / fN - 0.5)
-- {-# INLINE getNewPx #-}
Image $ A.compute warr
resize method border sz' (Image arr) = Image $ A.compute $ A.extract' center sz' arr'
where
(center@(u :. _), neighborhood) = interpolationBox method
!darr = A.makeArray (A.getComp arr) sz' (getNewPx (A.handleBorderIndex border sz (A.index' arr)))
!warr =
A.insertWindow
darr
A.Window
{ A.windowStart = center
, A.windowSize = sz - neighborhood + Sz center
, A.windowIndex = getNewPx (A.unsafeIndex arr)
, A.windowUnrollIx2 = Just u
}
arr' = A.makeArrayR A.D (A.getComp arr) sz'' getNewPx
(center, neighborhood) = interpolationBox method
sz''@(Sz2 m' n') = sz' + neighborhood - 1
sz@(Sz2 m n) = A.size arr
!fM = fromIntegral m' / fromIntegral m
!fN = fromIntegral n' / fromIntegral n
getNewPx getOldPx (i :. j) =
getNewPx (i :. j) =
interpolate
method
getOldPx
(A.handleBorderIndex border sz (A.index' arr))
((fromIntegral i + 0.5) / fM - 0.5, (fromIntegral j + 0.5) / fN - 0.5)
{-# INLINE getNewPx #-}
-- Image $ A.compute warr
-- where
-- (center@(u :. _), neighborhood) = interpolationBox method
-- !darr = A.makeArray (A.getComp arr) sz' (getNewPx (A.handleBorderIndex border sz (A.index' arr)))
-- !warr =
-- A.insertWindow
-- darr
-- A.Window
-- { A.windowStart = center
-- , A.windowSize = sz - neighborhood + Sz center
-- , A.windowIndex = getNewPx (A.unsafeIndex arr)
-- , A.windowUnrollIx2 = Just u
-- }
-- sz@(Sz2 m n) = A.size arr
-- !fM = fromIntegral m' / fromIntegral m
-- !fN = fromIntegral n' / fromIntegral n
-- getNewPx getOldPx (i :. j) =
-- interpolate
-- method
-- getOldPx
-- ((fromIntegral i + 0.5) / fM - 0.5, (fromIntegral j + 0.5) / fN - 0.5)
-- {-# INLINE getNewPx #-}
{-# INLINE resize #-}

-- Note: Reducing the size seems to be better performance wise with windowed array, while
-- increasing not necesserally
-- Note: Reducing the size seems to be slightly better performance wise with windowed
-- array, while increasing the size might be opposite. Extracting windowed array is not
-- implemented though.

-- | Scale an image. Same as resize, except scaling factors are supplied
-- instead of new dimensions.
Expand Down
2 changes: 2 additions & 0 deletions ihaskell-hip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stack-work*
*~
5 changes: 5 additions & 0 deletions ihaskell-hip/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog for ihaskell-hip

## 0.1.0.0

* Initial release
30 changes: 30 additions & 0 deletions ihaskell-hip/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Alexey Kuleshevich (c) 2020

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Alexey Kuleshevich nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions ihaskell-hip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ihaskell-hip
34 changes: 34 additions & 0 deletions ihaskell-hip/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -Wall #-}
module Main (main) where

#ifndef MIN_VERSION_cabal_doctest
#define MIN_VERSION_cabal_doctest(x,y,z) 0
#endif

#if MIN_VERSION_cabal_doctest(1,0,0)

import Distribution.Extra.Doctest ( defaultMainWithDoctests )
main :: IO ()
main = defaultMainWithDoctests "doctests"

#else

#ifdef MIN_VERSION_Cabal
-- If the macro is defined, we have new cabal-install,
-- but for some reason we don't have cabal-doctest in package-db
--
-- Probably we are running cabal sdist, when otherwise using new-build
-- workflow
#warning You are configuring this package without cabal-doctest installed. \
The doctests test-suite will not work as a result. \
To fix this, install cabal-doctest before configuring.
#endif

import Distribution.Simple

main :: IO ()
main = defaultMain

#endif

50 changes: 50 additions & 0 deletions ihaskell-hip/ihaskell-hip.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: ihaskell-hip
version: 0.1.0.0
synopsis: Short description
description: Please see the README on GitHub at <https://github.com/lehins/ihaskell-hip#readme>
homepage: https://github.com/lehins/ihaskell-hip
license: BSD3
license-file: LICENSE
author: Alexey Kuleshevich
maintainer: [email protected]
copyright: 2020 Alexey Kuleshevich
category: Algorithms
build-type: Custom
extra-source-files: README.md
, CHANGELOG.md
cabal-version: 1.18
tested-with: GHC == 8.4.3
, GHC == 8.4.4
, GHC == 8.6.3
, GHC == 8.6.4
, GHC == 8.6.5
, GHC == 8.8.1
, GHC == 8.8.2
, GHC == 8.10.1
custom-setup
setup-depends:
base
, Cabal
, cabal-doctest >=1.0.6

library
hs-source-dirs: src
exposed-modules: IHaskell.Display.Image

other-modules:
build-depends: base >= 4.11 && < 5
, bytestring
, data-default-class
, hip >= 2.0
, ihaskell
, massiv-io

default-language: Haskell2010
ghc-options: -Wall
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wredundant-constraints

source-repository head
type: git
location: https://github.com/lehins/hip
68 changes: 68 additions & 0 deletions ihaskell-hip/src/IHaskell/Display/Image.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module : IHaskell.Display.Image
-- Copyright : (c) Alexey Kuleshevich 2020
-- License : BSD3
-- Maintainer : Alexey Kuleshevich <[email protected]>
-- Stability : experimental
-- Portability : non-portable
--
module IHaskell.Display.Image
( module IHaskell.Display
, module I
) where

import Data.List.NonEmpty
import Data.Default.Class (def)
import qualified Data.Massiv.Array.IO as M
import Data.Word
import qualified Graphics.Image as I

import Data.ByteString.Lazy (toStrict)
import IHaskell.Display (Base64, Display(..), DisplayData, Height,
IHaskellDisplay(display), Width, base64, gif, jpg, png)

instance IHaskellDisplay (I.Image (M.SRGB 'I.NonLinear) Word8) where
display = base64encode png M.PNG

instance IHaskellDisplay (I.Image (M.SRGB 'I.NonLinear) Word16) where
display = base64encode png M.PNG

instance IHaskellDisplay (I.Image (M.YCbCr (M.SRGB 'I.NonLinear)) Word8) where
display = base64encode jpg M.JPG

instance IHaskellDisplay (I.Image (M.CMYK (M.SRGB 'I.NonLinear)) Word8) where
display = base64encode jpg M.JPG

instance IHaskellDisplay (I.Image M.Y' Word8) where
display = base64encode png M.PNG

instance IHaskellDisplay (I.Image M.Y' Word16) where
display = base64encode png M.PNG

instance IHaskellDisplay (I.Image (I.Alpha M.Y') Word8) where
display = base64encode png M.PNG

instance IHaskellDisplay (I.Image (I.Alpha M.Y') Word16) where
display = base64encode png M.PNG

instance {-# OVERLAPPABLE #-} (M.ColorSpace cs i e, M.ColorSpace (M.BaseSpace cs) i e) =>
IHaskellDisplay (I.Image cs e) where
display = base64encode png (M.Auto M.PNG)



base64encode ::
(M.Writable f (M.Image I.S cs e), M.ColorModel cs e)
=> (Width -> Height -> Base64 -> DisplayData)
-> f
-> I.Image cs e
-> IO Display
base64encode toDisplayData format img@(I.Image arr) = do
let I.Sz2 m n = I.dims img
bs <- M.encodeM format def arr
pure $ Display [toDisplayData n m $ base64 $ toStrict bs]
80 changes: 80 additions & 0 deletions ihaskell-hip/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-16.1

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
- ../hip
extra-deps: #for now, since we might need to add changes to upstream deps
- github: lehins/massiv
commit: 4fdc959542193f95b9a5da7f44b7cc90868b68be
subdirs:
- massiv
- github: lehins/massiv-io
commit: 3501c8930179345a634e1ad66ea7f746ba599986
subdirs:
- massiv-io
- github: lehins/Color
commit: c566a3083a238eba70e61ba0e261f9dacb8481bb
subdirs:
- Color
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.3"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor

0 comments on commit 01a4741

Please sign in to comment.