HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleJuicyPixels-3.3.8Haskell2010

Codec.Picture

Main module for image import/export into various image formats.

To use the library without thinking about it, look after decodeImage and readImage.

Generally, the read* functions read the images from a file and try to decode it, and the decode* functions try to decode a bytestring.

For an easy image writing use the saveBmpImage, saveJpgImage & savePngImage functions

  • 22 types
  • 5 classes
  • 66 values
  • PackageJuicyPixels-3.3.8
  • Exports94
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourcePicture.hs

Generic functions

12 declarations

If you want to decode an image in a bytestring without even thinking in term of format or whatever, this is the function to use. It will try to decode in each known format and if one decoding succeeds, it will return the decoded image in it's own colorspace.

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)
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)
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).

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).

RGB helper functions

3 declarations

Convert by any means possible a dynamic image to an image in RGB. The process can lose precision while converting from 16bits pixels or Floating point pixels. Any alpha layer will be dropped

Convert by any means possible a dynamic image to an image in RGB. The process can lose precision while converting from 32bits pixels or Floating point pixels. Any alpha layer will be dropped

Convert by any means possible a dynamic image to an image in RGBA. The process can lose precision while converting from 16bits pixels or Floating point pixels.

Lens compatibility

3 declarations
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

Generic image writing

6 declarations
valuesavePngImage :: FilePath -> DynamicImage -> IO ()
#

Save an image to a '.png' file, will do everything it can to save an image. For example, a simple transcoder to png

transcodeToPng :: FilePath -> FilePath -> IO ()
transcodeToPng pathIn pathOut = do
   eitherImg <- readImage pathIn
   case eitherImg of
       Left _ -> return ()
       Right img -> savePngImage pathOut img

Specific image format functions

0 declarations

Bitmap handling

Gif handling

Gif animation

typetype GifDelay = Int
#

Delay to wait before showing the next Gif image. The delay is expressed in 100th of seconds.

datadata GifLooping
#

Help to control the behaviour of GIF animation looping.

Constructors

Jpeg handling

Try to load a jpeg file and decompress. The colorspace is still YCbCr if you want to perform computation on the luma part. You can convert it to RGB using colorSpaceConversion.

Png handling

classclass PngSavable a where
#

Encode an image into a png if possible.

Methods

Instances8PngSavable, …

TGA handling

Tiff handling

classclass Pixel px => TiffSaveable px where
#

Class defining which pixel types can be serialized in a Tiff file.

Instances13TiffSaveable, …

HDR (Radiance/RGBE) handling

Color Quantization

Define which palette creation method is used.

Constructors

  • MedianMeanCut

    MedianMeanCut method, provide the best results (visualy) at the cost of increased calculations.

  • Uniform

    Very fast algorithm (one pass), doesn't provide good looking results.

Image types and pixel types

0 declarations

Image

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 DynamicImage
#

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

Constructors

Instances2Eq, NFData

Pixels

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, …
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 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 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, …

Foreign unsafe import

1 declaration