packages feed

friday-scale-dct (empty) → 1.0.0.0

raw patch · 8 files changed

+328/−0 lines, 8 filesdep +JuicyPixelsdep +JuicyPixels-utildep +basesetup-changedbinary-added

Dependencies added: JuicyPixels, JuicyPixels-util, base, base-compat, carray, fft, friday, friday-juicypixels, friday-scale-dct, time, vector

Files

+ CHANGELOG.md view
@@ -0,0 +1,7 @@+# 1.0.0.0+- Convert phadej's JuicyPixels based code to use friday+- Make changes to allow any image type to be resized (this is probably too generous and will break HSV and possibly other colour spaces).++# 0.1.1.0++- Bundle missing test file
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Oleg Grenrus++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 Oleg Grenrus 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.
+ README.md view
@@ -0,0 +1,30 @@+# JuicyPixels-scale-dct++> Scale [friday](https://hackage.haskell.org/package/friday) images with DCT.+ +This library is based on the work of [Oleg Grenrus](https://github.com/phadej) on [JuicyPixels-scale-dct](https://github.com/phadej/JuicyPixels-scale-dct) and improves upon this work be allowing images of any type to be scaled (as long as their underlying pixel components are `Integral` and 'linear', is not `HSV`), not just RGBA8 images.++This will probably only work on colour spaces which are "linear", HSV is unlikely to work because hue is is an angle, and taking the DCT of angles probably doesn't make sense.++<!-- [![Build Status](https://travis-ci.org/phadej/JuicyPixels-scale-dct.svg?branch=master)](https://travis-ci.org/phadej/JuicyPixels-scale-dct) -->+<!-- [![Hackage](https://img.shields.io/hackage/v/JuicyPixels-scale-dct.svg)](http://hackage.haskell.org/package/JuicyPixels-scale-dct) -->+<!-- [![Stackage LTS 2](http://stackage.org/package/JuicyPixels-scale-dct/badge/lts-2)](http://stackage.org/lts-2/package/JuicyPixels-scale-dct) -->+<!-- [![Stackage LTS 3](http://stackage.org/package/JuicyPixels-scale-dct/badge/lts-3)](http://stackage.org/lts-3/package/JuicyPixels-scale-dct) -->+<!-- [![Stackage Nightly](http://stackage.org/package/JuicyPixels-scale-dct/badge/nightly)](http://stackage.org/nightly/package/JuicyPixels-scale-dct) -->++[friday](https://hackage.haskell.org/package/friday) is a Haskell library+for manipulating images in a functional way.++---++## Example image++![phadej](https://raw.githubusercontent.com/axman6/friday-scale-dct/master/phadej.png)++### smaller++![phadej](https://raw.githubusercontent.com/axman6/friday-scale-dct/master/phadej-small.png)++### larger++![phadej](https://raw.githubusercontent.com/axman6/friday-scale-dct/master/phadej-large.png)
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple+main :: IO ()+main = defaultMain
+ example/Example.hs view
@@ -0,0 +1,40 @@+module Main where++import           Codec.Picture                   (DynamicImage (..), readImage,+                                                  writePng)+import           Codec.Picture.RGBA8             (fromDynamicImage)+import           Data.Time                       (diffUTCTime, getCurrentTime)+import           Vision.Image.Transform.ScaleDCT (scale)++import           Vision.Image.JuicyPixels++main :: IO ()+main = do+    start <- getCurrentTime+    Right dimg <- readImage "phadej.png"+    let img = fromDynamicImage dimg+    let avasmall = scale (64, 64) (toFridayRGBA img)+    let avalarge = scale (600, 600) (toFridayRGBA img)+    let avahuge  = scale (1200, 1200) (toFridayRGBA img)+    writePng "bio-small.png" (toJuicyRGBA avasmall)+    writePng "bio-large.png" (toJuicyRGBA avalarge)+    writePng "bio-huge.png"  (toJuicyRGBA avahuge)+    end <- getCurrentTime+    print $ end `diffUTCTime` start+++showType :: DynamicImage  -> String+showType dimg = case dimg of+    ImageY8 _ -> "ImageY8"+    ImageY16 _ -> "ImageY16"+    ImageYF _ -> "ImageYF"+    ImageYA8 _ -> "ImageYA8"+    ImageYA16 _ -> "ImageYA16"+    ImageRGB8 _ -> "ImageRGB8"+    ImageRGB16 _ -> "ImageRGB16"+    ImageRGBF _ -> "ImageRGBF"+    ImageRGBA8 _ -> "ImageRGBA8"+    ImageRGBA16 _ -> "ImageRGBA16"+    ImageYCbCr8 _ -> "ImageYCbCr8"+    ImageCMYK8 _ -> "ImageCMYK8"+    ImageCMYK16 _ -> "ImageCMYK16"
+ friday-scale-dct.cabal view
@@ -0,0 +1,72 @@+-- This file has been generated from package.yaml by hpack version 0.8.0.+--+-- see: https://github.com/sol/hpack++name:           friday-scale-dct+version:        1.0.0.0+synopsis:       Scale Friday images with DCT+description:    Scale Friday Images with DCT+category:       Graphics+homepage:       https://github.com/axman6/friday-scale-dct#readme+bug-reports:    https://github.com/axman6/friday-scale-dctissues+author:         Alex Mason <axman6@gmail.com>, Oleg Grenrus <oleg.grenrus@iki.fi>+maintainer:     Alex Mason <axman6@gmail.com>+license:        BSD3+license-file:   LICENSE+tested-with:    GHC==7.8.4, GHC==7.10.2+build-type:     Simple+cabal-version:  >= 1.20++extra-source-files:+    CHANGELOG.md+    phadej.png+    README.md++Flag example+  Description: Build the example application.+  Default:     False++source-repository head+  type: git+  location: https://github.com/axman6/friday-scale-dct.git++library+  hs-source-dirs:+      src+  ghc-options: -Wall+  build-depends:+      base        >=4.7      && <4.9+    , base-compat >=0.6.0    && <0.9+    , friday      >=0.2.2.0  && <0.3+    , fft         >=0.1.8.1  && <0.2+    , carray      >=0.1.6.1  && <0.2+    , vector      >= 0.10.0  && < 0.11+  exposed-modules:+      Vision.Image.Transform.ScaleDCT+  default-language: Haskell2010++executable example+  -- type: exitcode-stdio-1.0+  if !flag(example)+    buildable: False+  if flag(example)+    buildable: True+    build-depends:+        base        >=4.7      && <4.9+      , base-compat >=0.6.0    && <0.9+      , JuicyPixels >=3.2.5.3  && <3.3+      , fft         >=0.1.8.1  && <0.2+      , carray      >=0.1.6.1  && <0.2+      , base+      , friday+      , friday-scale-dct+      , friday-juicypixels    >= 0.1 && < 0.2+      , JuicyPixels           >=3.2.5.3  && <3.3+      , JuicyPixels-util      >=0.2   && <0.3+      , time                  >=1.4.2 && <1.6++  main-is: Example.hs+  hs-source-dirs:+      example+  ghc-options: -Wall+  default-language: Haskell2010
+ phadej.png view

binary file changed (absent → 206060 bytes)

+ src/Vision/Image/Transform/ScaleDCT.hs view
@@ -0,0 +1,146 @@+{-# LANGUAGE BangPatterns        #-}+{-# LANGUAGE FlexibleContexts    #-}+{-# LANGUAGE GADTs               #-}+{-# LANGUAGE ScopedTypeVariables #-}+-----------------------------------------------------------------------------+-- |+-- Module      :  Vision.Image.Transform.ScaleDCT+-- Copyright   :  (C) 2015 Oleg Grenrus, 2015 Alex Mason+-- License     :  BSD3+-- Maintainer  :  Alex Mason <axman6@gmail.com>+--+-- Scale pictures using Discrete Cosine Transform.+--+module Vision.Image.Transform.ScaleDCT (scale) where++import           Prelude                ()+import           Prelude.Compat++import           Data.Array.CArray.Base (CArray (..))+import qualified Data.Vector.Storable   as VS+import           Vision.Image           hiding ((!))+import           Vision.Primitive.Shape++import           Data.Array.CArray      (amap, array, bounds, elems, size, (!))+import           Data.Ix                (range)+import           Math.FFT               (dct2N, dct3N)++import qualified Data.Vector            as V+++type Array2D = CArray (Int, Int) Double+++-- | @`scale' (w,h) img@ scales the image @img@ to size @w * h@.+-- It is unlikely to do the right thing for "non-linear" colour spaces, such as HSV, where hue is an angle.+-- In future versions there may be a class to restrict this function to only working on "linear" pixel types.+--+-- @pix@ is the type of pixels in the image (see `ImagePixel'),+-- @pixChan@ is the underlying (Integral) pixel component type, ie `Word8`, `Word16` (see `PixelChannel').+-- Future versions will support scaling floating point images with `Float' and `Double' channels.+--+-- Example types for this function:+--+-- > scale :: (Int,Int) -> Manifest RGBAPixel -> Manifest RGBAPixel+-- > scale :: (Int,Int) -> Delayed RGBPixel -> Manifest RGBPixel+--+--+-- Here @pix@ is, for example, 'RGBAPixel', with @pixChan@ being 'Word8'+--+-- Some assumptions are made about the @pix@ type, particularly that if @pix@+-- is made up of channels of type @pixChan@, then they are stored directly next to+-- each other by @pix@'s `Storable' instance. If this is not the case, then the+-- resulting image may produce garbage results. This should not be an issue for all+-- of the @friday@ pixel types.+{-# INLINEABLE scale #-}+-- {-# SPECIALIZE scale :: (Int,Int) -> Manifest RGBAPixel -> Manifest RGBAPixel #-}+-- {-# SPECIALIZE scale :: (Int,Int) -> Manifest RGBPixel -> Manifest RGBPixel #-}+scale ::+    ( ImagePixel i ~ pix+    , PixelChannel pix ~ pixChan+    , Integral pixChan+    , Pixel pix+    , VS.Storable pixChan+    , Image i)+      => (Int, Int)         -- ^ Output width, height+      -> i                  -- ^ Input image+      -> Manifest (ImagePixel i) -- ^ Output image+scale dim@(w,h) img = Manifest (Z :. w :. h) res'+  where++    Manifest ((Z :. iw) :. ih) ivec = compute img++    -- ivec' :: (VS.Storable pixChan) => VS.Vector pixChan+    !ivec' = castVec img ivec++    !nchans = nChannels img++    -- This is necessary to ensure that we don't constantly do a lookup+    -- for which implementation of fromIntegral we want to use in chanVec+    -- below+    fi :: (Integral pixChan) => pixChan -> Double+    fi x = fromIntegral x++    chanVec = V.generate nchans (\chan ->+                imageToArray (Manifest ((Z :. iw) :. ih) $+                    VS.generate (iw*ih) $ \ix ->+                        fi $ VS.unsafeIndex ivec' (ix * nchans + chan)))++    chanVec' :: V.Vector Array2D+    chanVec' = fmap transform chanVec++    {-# INLINE truncate' #-}+    truncate' :: (Image i, Integral (PixelChannel (ImagePixel i)), VS.Storable (ImagePixel i))+              => i+              -> Double+              -> PixelChannel (ImagePixel i)+    truncate' _ = truncate++    -- res :: (VS.Storable t', Integral t', t' ~ t) => VS.Vector t'+    res = VS.generate (nchans * h * w) (\ix -> case quotRem ix nchans of+                (i,v) -> truncate' img . limit $ V.unsafeIndex chanVec' v ! (quotRem i w)+                )++    res' :: (VS.Storable pix) => VS.Vector pix+    res' = VS.unsafeCast res++    {-# INLINE castVec #-}+    castVec :: (ImagePixel i ~ pix', Image i, VS.Storable pix', VS.Storable (PixelChannel pix'))+            => i+            -> VS.Vector pix'+            -> VS.Vector (PixelChannel pix')+    castVec _ = VS.unsafeCast++    transform ch = amap (k*) ch'+      where+        ch' = dct3N [1, 0] . cut dim . dct2N [0, 1] $ ch+        k   = imgNorm ch / imgNorm ch'+++{-# INLINE imgNorm #-}+imgNorm :: Array2D -> Double+imgNorm ch = sqrt . (/n) . sum . fmap sq . elems $ ch+  where sq x = x * x+        n = fromIntegral $ size ch++{-# INLINE cut #-}+cut :: (Int, Int) -> Array2D -> Array2D+cut (w, h) img = array b [ (i, pick i) | i <- range b ]+  where b            = ((0,0), (h-1, w-1))++        (_,(w',h'))  = bounds img+        pick i@(x,y) | x < h' && y < w' = img ! i+                     | otherwise    = 0+++{-# INLINE imageToArray #-}+imageToArray :: Manifest Double -> Array2D+imageToArray img = case img of+    Manifest ((Z :. h) :. w) vec -> case VS.unsafeToForeignPtr0 vec of+        (fptr, len) -> CArray (0,0) (h-1,w-1) len fptr++{-# INLINE limit #-}+limit :: Double -> Double+limit x | x < 0     = 0+        | x > 255   = 255+        | otherwise = x