HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

ModuleJuicyPixels-3.3.8Haskell2010

Codec.Picture.Types

Module provides basic types for image manipulation in the library.

  • 33 types
  • 7 classes
  • 30 values
  • PackageJuicyPixels-3.3.8
  • Exports70
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTypes.hs

Types

0 declarations

Image types

datadata Image a
#

The main type of this package, one that most functions work on, is Image.

Parameterized by the underlying pixel format it forms a rigid type. If you wish to store images of different or unknown pixel formats use DynamicImage.

Image is essentially a rectangular pixel buffer of specified width and height. The coordinates are assumed to start from the upper-left corner of the image, with the horizontal position first and vertical second.

Constructors

  • Image
    • imageWidth :: !Int

      Width of the image in pixels

    • imageHeight :: !Int

      Height of the image in pixels.

    • imageData :: Vector (PixelBaseComponent a)

      Image pixel data. To extract pixels at a given position you should use the helper functions.

      Internally pixel data is stored as consecutively packed lines from top to bottom, scanned from left to right within individual lines, from first to last color component within each pixel.

Instances2Eq, NFData
datadata MutableImage s a
#

Image or pixel buffer, the coordinates are assumed to start from the upper-left corner of the image, with the horizontal position first, then the vertical one. The image can be transformed in place.

Constructors

Instances1NFData
datadata DynamicImage
#

Image type enumerating all predefined pixel types. It enables loading and use of images of different pixel types.

Constructors

Instances2Eq, NFData

Image functions

Image Lenses

typetype Traversal s t a b = forall (f :: Type -> Type). Applicative f => (a -> f b) -> s -> f t
#

Traversal type matching the definition in the Lens package.

valueimagePixels :: (Pixel pxa, Pixel pxb) => Traversal (Image pxa) (Image pxb) pxa pxb
#

Traversal in "raster" order, from left to right the top to bottom. This traversal is matching pixelMap in spirit.

Since 3.2.4

valueimageIPixels :: (Pixel pxa, Pixel pxb) => Traversal (Image pxa) (Image pxb) (Int, Int, pxa) pxb
#

Traversal providing the pixel position with it's value. The traversal in raster order, from lef to right, then top to bottom. The traversal match pixelMapXY in spirit.

Since 3.2.4

Pixel types

typetype Pixel8 = Word8
#

Type alias for 8bit greyscale pixels. For simplicity, greyscale pixels use plain numbers instead of a separate type.

typetype PixelF = Float
#

Type alias for 32bit floating point greyscale pixels. The standard bounded value range is mapped to the closed interval [0,1] i.e.

map promotePixel [0, 1 .. 255 :: Pixel8] == [0/255, 1/255 .. 1.0 :: PixelF]
datadata PixelYA8
#

Pixel type storing 8bit Luminance (Y) and alpha (A) information. Values are stored in the following order:

  • Luminance

  • Alpha

Constructors

Instances18Eq, Ord, Show, PngSavable, TiffSaveable, Pixel, …
datadata PixelYA16
#

Pixel type storing 16bit Luminance (Y) and alpha (A) information. Values are stored in the following order:

  • Luminance

  • Alpha

Constructors

Instances16Eq, Ord, Show, PngSavable, TiffSaveable, Pixel, …
datadata PixelRGB8
#

Classic pixel type storing 8bit red, green and blue (RGB) information. Values are stored in the following order:

  • Red

  • Green

  • Blue

Constructors

Instances29Eq, Ord, Show, BmpEncodable, PngPaletteSaveable, PngSavable, …
datadata PixelRGB16
#

Pixel type storing 16bit red, green and blue (RGB) information. Values are stored in the following order:

  • Red

  • Green

  • Blue

Instances22Eq, Ord, Show, PngSavable, TiffSaveable, Pixel, …
datadata PixelRGBF
#

HDR pixel type storing floating point 32bit red, green and blue (RGB) information. Same value range and comments apply as for PixelF. Values are stored in the following order:

  • Red

  • Green

  • Blue

Constructors

Instances13Eq, Ord, Show, Pixel, LumaPlaneExtractable, Decimable, …
datadata PixelRGBA8
#

Classical pixel type storing 8bit red, green, blue and alpha (RGBA) information. Values are stored in the following order:

  • Red

  • Green

  • Blue

  • Alpha

Instances23Eq, Ord, Show, BmpEncodable, PngPaletteSaveable, PngSavable, …
datadata PixelRGBA16
#

Pixel type storing 16bit red, green, blue and alpha (RGBA) information. Values are stored in the following order:

  • Red

  • Green

  • Blue

  • Alpha

Instances20Eq, Ord, Show, PngSavable, TiffSaveable, Pixel, …
datadata PixelCMYK8
#

Pixel type storing 8bit cyan, magenta, yellow and black (CMYK) information. Values are stored in the following order:

  • Cyan

  • Magenta

  • Yellow

  • Black

Instances17Eq, Ord, Show, TiffSaveable, Pixel, JpgEncodable, …
datadata PixelCMYK16
#

Pixel type storing 16bit cyan, magenta, yellow and black (CMYK) information. Values are stored in the following order:

  • Cyan

  • Magenta

  • Yellow

  • Black

Instances15Eq, Ord, Show, TiffSaveable, Pixel, PackeablePixel, …
datadata PixelYCbCr8
#

Pixel type storing 8bit luminance, blue difference and red difference (YCbCr) information. Values are stored in the following order:

  • Y (luminance)

  • Cb

  • Cr

Instances13Eq, Ord, Show, TiffSaveable, Pixel, JpgEncodable, …
datadata PixelYCbCrK8
#

Pixel type storing value for the YCCK color space:

  • Y (Luminance)

  • Cb

  • Cr

  • Black

Instances7Eq, Ord, Show, Pixel, ColorSpaceConvertible, PixelBaseComponent, …

Type classes

5 declarations
classclass (Pixel a, Pixel b) => ColorConvertible a b where
#

Implement upcasting for pixel types. Minimal declaration of promotePixel. It is strongly recommended to overload promoteImage to keep performance acceptable

Methods

  • promotePixel :: a -> b

    Convert a pixel type to another pixel type. This operation should never lose any data.

  • promoteImage :: Image a -> Image b

    Change the underlying pixel type of an image by performing a full copy of it.

Instances22ColorConvertible, …
classclass (Storable (PixelBaseComponent a), Num (PixelBaseComponent a), Eq a) => Pixel a where
#

Definition of pixels used in images. Each pixel has a color space, and a representative component (Word8 or Float).

Associated types

  • type family PixelBaseComponent a

    Type of the pixel component, "classical" images would have Word8 type as their PixelBaseComponent, HDR image would have Float for instance

Methods

Instances15Pixel, …

The following graph describe the differents way to convert between pixel types,

  • Nodes describe pixel type

  • Arrows describe functions

image: docimages/pixelgraph.svg

classclass (Pixel a, Pixel b) => ColorSpaceConvertible a b where
#

This class abstract colorspace conversion. This conversion can be lossy, which ColorConvertible cannot

Methods

  • convertPixel :: a -> b

    Pass a pixel from a colorspace (say RGB) to the second one (say YCbCr)

  • convertImage :: Image a -> Image b

    Helper function to convert a whole image by taking a copy it.

Instances9ColorSpaceConvertible, …
classclass (Pixel a, Pixel (PixelBaseComponent a)) => LumaPlaneExtractable a where
#

Helper class to help extract a luma plane out of an image or a pixel

Methods

Instances10LumaPlaneExtractable, …
classclass (Pixel a, Pixel b) => TransparentPixel a b | a -> b where
#

Class modeling transparent pixel, should provide a method to combine transparent pixels

Methods

Instances4TransparentPixel

Helper functions

16 declarations
valuepixelMap :: (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
#

map equivalent for an image, working at the pixel level. Little example : a brightness function for an rgb image

brightnessRGB8 :: Int -> Image PixelRGB8 -> Image PixelRGB8
brightnessRGB8 add = pixelMap brightFunction
     where up v = fromIntegral (fromIntegral v + add)
           brightFunction (PixelRGB8 r g b) =
                   PixelRGB8 (up r) (up g) (up b)
valuepixelFold
  1. :: Pixel pixel
  2. => acc -> Int -> Int -> pixel -> acc
  3. -> acc
  4. -> Image pixel
  5. -> acc
#

Fold over the pixel of an image with a raster scan order: from top to bottom, left to right

valuepixelFoldM
  1. :: (Pixel pixel, Monad m)
  2. => (acc -> Int -> Int -> pixel -> m acc)

    monadic mapping function

  3. -> acc

    Initial state

  4. -> Image pixel

    Image to fold over

  5. -> m acc
#

Fold over the pixel of an image with a raster scan order: from top to bottom, left to right, carrying out a state

valuepixelFoldMap :: (Pixel px, Monoid m) => (px -> m) -> Image px -> m
#

Fold over the pixel of an image with a raster scan order: from top to bottom, left to right. This functions is analog to the foldMap from the Foldable typeclass, but due to the Pixel constraint, Image cannot be made an instance of it.

valuedynamicMap
  1. :: forall pixel. Pixel pixel => Image pixel -> a
  2. -> DynamicImage
  3. -> a
#

Helper function to help extract information from dynamic image. To get the width of a dynamic image, you can use the following snippet:

dynWidth :: DynamicImage -> Int
dynWidth img = dynamicMap imageWidth img
valuedynamicPixelMap
  1. :: forall pixel. Pixel pixel => Image pixel -> Image pixel
  2. -> DynamicImage
  3. -> DynamicImage
#

Equivalent of the pixelMap function for the dynamic images. You can perform pixel colorspace independant operations with this function.

For instance, if you want to extract a square crop of any image, without caring about colorspace, you can use the following snippet.

dynSquare :: DynamicImage -> DynamicImage
dynSquare = dynamicPixelMap squareImage

squareImage :: Pixel a => Image a -> Image a
squareImage img = generateImage (\x y -> pixelAt img x y) edge edge
   where edge = min (imageWidth img) (imageHeight img)
valuewithImage
  1. :: (Pixel pixel, PrimMonad m)
  2. => Int

    Image width

  3. -> Int

    Image height

  4. -> (Int -> Int -> m pixel)

    Generating functions

  5. -> m (Image pixel)
#

Create an image using a monadic initializer function. The function will receive values from 0 to width-1 for the x parameter and 0 to height-1 for the y parameter. The coordinates 0,0 are the upper left corner of the image, and (width-1, height-1) the lower right corner.

The function is called for each pixel in the line from left to right (0 to width - 1) and for each line (0 to height - 1).

Combine, pixel by pixel and component by component the values of 3 different images. Usage example:

averageBrightNess c1 c2 c3 = clamp $ toInt c1 + toInt c2 + toInt c3
  where clamp = fromIntegral . min 0 . max 255
        toInt :: a -> Int
        toInt = fromIntegral
ziPixelComponent3 averageBrightNess img1 img2 img3
valuegenerateImage
  1. :: Pixel px
  2. => (Int -> Int -> px)

    Generating function, with x and y params.

  3. -> Int

    Width in pixels

  4. -> Int

    Height in pixels

  5. -> Image px
#

Create an image given a function to generate pixels. The function will receive values from 0 to width-1 for the x parameter and 0 to height-1 for the y parameter. The coordinates 0,0 are the upper left corner of the image, and (width-1, height-1) the lower right corner.

for example, to create a small gradient image:

imageCreator :: String -> IO ()
imageCreator path = writePng path $ generateImage pixelRenderer 250 300
   where pixelRenderer x y = PixelRGB8 (fromIntegral x) (fromIntegral y) 128
valuegenerateFoldImage
  1. :: Pixel a
  2. => (acc -> Int -> Int -> (acc, a))

    Function taking the state, x and y

  3. -> acc

    Initial state

  4. -> Int

    Width in pixels

  5. -> Int

    Height in pixels

  6. -> (acc, Image a)
#

Create an image given a function to generate pixels. The function will receive values from 0 to width-1 for the x parameter and 0 to height-1 for the y parameter. The coordinates 0,0 are the upper left corner of the image, and (width-1, height-1) the lower right corner.

the acc parameter is a user defined one.

The function is called for each pixel in the line from left to right (0 to width - 1) and for each line (0 to height - 1).

Color plane extraction

14 declarations
classclass ColorPlane pixel planeToken where
#

Class used to describle plane present in the pixel type. If a pixel has a plane description associated, you can use the plane name to extract planes independently.

Instances32ColorPlane, …
datadata PlaneGreen
#

Define the plane for the green color component

Instances5ColorPlane
datadata PlaneBlue
#

Define the plane for the blue color component

Instances5ColorPlane

Packeable writing (unsafe but faster)

5 declarations
classclass PackeablePixel a where
#

This typeclass exist for performance reason, it allow to pack a pixel value to a simpler "primitive" data type to allow faster writing to moemory.

Associated types

  • type family PackedRepresentation a

    Primitive type asociated to the current pixel It's Word32 for PixelRGBA8 for instance

Methods

Instances10PackeablePixel, …