-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Picture loading/serialization (in png, jpeg and bitmap)
--
-- This library can load and store images in various image formats, for
-- now mainly in PNG/Bitmap and Jpeg Version 1.3 changelog: - Fix
-- extractComponent function - Adding saving for YA8 functions
@package JuicyPixels
@version 1.3
-- | Module providing the basic types for image manipulation in the
-- library. Defining the types used to store all those _Juicy Pixels_
module Codec.Picture.Types
-- | 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.
data Image a
Image :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> Vector Word8 -> Image a
-- | Width of the image in pixels
imageWidth :: Image a -> {-# UNPACK #-} !Int
-- | Height of the image in pixels.
imageHeight :: Image a -> {-# UNPACK #-} !Int
-- | The real image, to extract pixels at some position you should use the
-- helpers functions.
imageData :: Image a -> Vector Word8
-- | 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.
data MutableImage s a
MutableImage :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> STVector s Word8 -> MutableImage s a
-- | Width of the image in pixels
mutableImageWidth :: MutableImage s a -> {-# UNPACK #-} !Int
-- | Height of the image in pixels.
mutableImageHeight :: MutableImage s a -> {-# UNPACK #-} !Int
-- | The real image, to extract pixels at some position you should use the
-- helpers functions.
mutableImageData :: MutableImage s a -> STVector s Word8
-- | Type allowing the loading of an image with different pixel structures
data DynamicImage
-- | A greyscale image.
ImageY8 :: (Image Pixel8) -> DynamicImage
-- | An image in greyscale with an alpha channel.
ImageYA8 :: (Image PixelYA8) -> DynamicImage
-- | An image in true color.
ImageRGB8 :: (Image PixelRGB8) -> DynamicImage
-- | An image in true color and an alpha channel.
ImageRGBA8 :: (Image PixelRGBA8) -> DynamicImage
-- | An image in the colorspace used by Jpeg images.
ImageYCbCr8 :: (Image PixelYCbCr8) -> DynamicImage
-- | Describe pixel kind at runtime
data PixelType
-- | For 2 bits pixels
PixelMonochromatic :: PixelType
PixelGreyscale :: PixelType
PixelGreyscaleAlpha :: PixelType
PixelRedGreenBlue8 :: PixelType
PixelRedGreenBlueAlpha8 :: PixelType
PixelYChromaRChromaB8 :: PixelType
-- | Simple alias for greyscale value in 8 bits.
type Pixel8 = Word8
-- | Pixel type storing Luminance (Y) and alpha information on 8 bits.
-- Value are stored in the following order :
--
--
data PixelYA8
PixelYA8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelYA8
-- | Pixel type storing classic pixel on 8 bits Value are stored in the
-- following order :
--
--
-- - Red
-- - Green
-- - Blue
--
data PixelRGB8
PixelRGB8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelRGB8
-- | Pixel type storing a classic pixel, with an alpha component. Values
-- are stored in the following order
--
--
-- - Red
-- - Green
-- - Blue
-- - Alpha
--
data PixelRGBA8
PixelRGBA8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelRGBA8
-- | Pixel storing data in the YCbCr colorspace, value are stored in the
-- following order :
--
--
-- - Y (luminance)
-- - Cr
-- - Cb
--
data PixelYCbCr8
PixelYCbCr8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelYCbCr8
-- | Implement upcasting for pixel types Minimal declaration declaration
-- promotePixel It is strongly recommanded to overload
-- promoteImage to keep performance acceptable
class (Pixel a, Pixel b) => ColorConvertible a b where promoteImage = pixelMap promotePixel
promotePixel :: ColorConvertible a b => a -> b
promoteImage :: ColorConvertible a b => Image a -> Image b
-- | Typeclass used to query a type about it's properties regarding casting
-- to other pixel types
class Serialize a => Pixel a where pixelBaseIndex (Image {imageWidth = w}) x y = (x + y * w) * componentCount (undefined :: a) mutablePixelBaseIndex (MutableImage {mutableImageWidth = w}) x y = (x + y * w) * componentCount (undefined :: a)
canPromoteTo :: Pixel a => a -> PixelType -> Bool
componentCount :: Pixel a => a -> Int
pixelBaseIndex :: Pixel a => Image a -> Int -> Int -> Int
mutablePixelBaseIndex :: Pixel a => MutableImage s a -> Int -> Int -> Int
promotionType :: Pixel a => a -> PixelType
pixelAt :: Pixel a => Image a -> Int -> Int -> a
readPixel :: Pixel a => MutableImage s a -> Int -> Int -> ST s a
writePixel :: Pixel a => MutableImage s a -> Int -> Int -> a -> ST s ()
-- | This class abstract colorspace conversion. This conversion can be
-- lossy, which ColorConvertible cannot
class (Pixel a, Pixel b) => ColorSpaceConvertible a b where convertImage = pixelMap convertPixel
convertPixel :: ColorSpaceConvertible a b => a -> b
convertImage :: ColorSpaceConvertible a b => Image a -> Image b
-- | Helper class to help extract a luma plane out of an image or a pixel
class Pixel a => LumaPlaneExtractable a where extractLumaPlane = pixelMap computeLuma
computeLuma :: LumaPlaneExtractable a => a -> Pixel8
extractLumaPlane :: LumaPlaneExtractable a => Image a -> Image Pixel8
-- | Class modeling transparent pixel, should provide a method to combine
-- transparent pixels
class (Pixel a, Pixel b) => TransparentPixel a b | a -> b
dropTransparency :: TransparentPixel a b => a -> b
-- | Tell if you can convert between two pixel types, both arguments are
-- unused.
canConvertTo :: (Pixel a, Pixel b) => a -> b -> Bool
-- | Extract an image plane of an image, returning an image which can be
-- represented by a gray scale image. If you ask a component out of
-- bound, the error function will be called
extractComponent :: Pixel a => Int -> Image a -> Image Pixel8
-- | 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)
--
pixelMap :: (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
-- | For any image with an alpha component (transparency), drop it,
-- returning a pure opaque image.
dropAlphaLayer :: TransparentPixel a b => Image a -> Image b
-- | Create an image given a function to generate pixels. The function will
-- receive value from 0 to width-1 for the x parameter and 0 to height-1
-- for the y parameter. The coordinate 0,0 is 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 -> Image PixelRGB8
-- imageCreator path = writePng path $ generateImage pixelRenderer 250 300
-- where pixelRenderer x y = PixelRGB8 x y 128
--
generateImage :: Pixel a => (Int -> Int -> a) -> Int -> Int -> Image a
-- | This function implement the same algorithm as generateImage,
-- and let use an user-defined state
generateFoldImage :: Pixel a => (acc -> Int -> Int -> (acc, a)) -> acc -> Int -> Int -> (acc, Image a)
instance Eq PixelType
instance ColorSpaceConvertible PixelYCbCr8 PixelRGB8
instance ColorSpaceConvertible PixelRGB8 PixelYCbCr8
instance Pixel a => ColorSpaceConvertible a a
instance Pixel PixelYCbCr8
instance Pixel PixelRGBA8
instance ColorConvertible PixelRGB8 PixelRGBA8
instance Pixel PixelRGB8
instance ColorConvertible PixelYA8 PixelRGBA8
instance ColorConvertible PixelYA8 PixelRGB8
instance Pixel PixelYA8
instance ColorConvertible Pixel8 PixelRGBA8
instance ColorConvertible Pixel8 PixelRGB8
instance ColorConvertible Pixel8 PixelYA8
instance Pixel Pixel8
instance Pixel a => ColorConvertible a a
instance LumaPlaneExtractable PixelYCbCr8
instance LumaPlaneExtractable PixelYA8
instance LumaPlaneExtractable PixelRGBA8
instance LumaPlaneExtractable PixelRGB8
instance LumaPlaneExtractable Pixel8
instance Storable PixelRGBA8
instance Serialize PixelRGBA8
instance Storable PixelYCbCr8
instance Serialize PixelYCbCr8
instance Storable PixelRGB8
instance Serialize PixelRGB8
instance Storable PixelYA8
instance Serialize PixelYA8
instance NFData DynamicImage
instance NFData (MutableImage s a)
instance NFData (Image a)
instance TransparentPixel PixelRGBA8 PixelRGB8
instance TransparentPixel PixelYA8 Pixel8
-- | Module used for loading & writing 'Portable Network Graphics'
-- (PNG) files. The API has two layers, the high level, which load the
-- image without looking deeply about it and the low level, allowing
-- access to data chunks contained in the PNG image.
--
-- For general use, please use decodePng function.
--
-- The loader has been validated against the pngsuite
-- (http:www.libpng.orgpubpng/pngsuite.html)
module Codec.Picture.Png
-- | Encode an image into a png if possible.
class PngSavable a
encodePng :: PngSavable a => Image a -> ByteString
-- | Transform a raw png image to an image, without modifying the
-- underlying pixel type. If the image is greyscale and < 8 bits, a
-- transformation to RGBA8 is performed. This should change in the
-- future. The resulting image let you manage the pixel types.
--
-- This function can output the following pixel types :
--
--
-- - PixelY8
-- - PixelYA8
-- - PixelRGB8
-- - PixelRGBA8
--
decodePng :: ByteString -> Either String DynamicImage
-- | Helper function to directly write an image as a png on disk.
writePng :: PngSavable pixel => FilePath -> Image pixel -> IO ()
-- | Encode a dynamic image in bmp if possible, supported pixel type are :
--
--
-- - Y8
-- - YA8
-- - RGB8
-- - RGBA8
--
encodeDynamicPng :: DynamicImage -> Either String ByteString
-- | Write a dynamic image in a .png image file if possible. The same
-- restriction as encodeDynamicPng apply.
writeDynamicPng :: FilePath -> DynamicImage -> IO (Either String Bool)
-- | Module used for JPEG file loading and writing.
module Codec.Picture.Jpg
-- | Try to decompress 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
--
-- This function can output the following pixel types :
--
--
-- - PixelY8
-- - PixelYCbCr8
--
decodeJpeg :: ByteString -> Either String DynamicImage
-- | Function to call to encode an image to jpeg. The quality factor should
-- be between 0 and 100 (100 being the best quality).
encodeJpegAtQuality :: Word8 -> Image PixelYCbCr8 -> ByteString
-- | Encode an image in jpeg at a reasonnable quality level. If you want
-- better quality or reduced file size, you should use
-- encodeJpegAtQuality
encodeJpeg :: Image PixelYCbCr8 -> ByteString
instance Eq JpgFrameKind
instance Show JpgFrameKind
instance Show JpgComponent
instance Show JpgFrameHeader
instance Show JpgScanSpecification
instance Show JpgScanHeader
instance Show JpgQuantTableSpec
instance Show JpgHuffmanTableSpec
instance Show JpgFrame
instance Show JpgImage
instance Serialize JpgScanHeader
instance Serialize JpgScanSpecification
instance Serialize JpgFrameHeader
instance Serialize JpgComponent
instance Serialize RestartInterval
instance Serialize JpgFrameKind
instance Serialize JpgImage
instance Serialize JpgHuffmanTableSpec
instance SizeCalculable JpgHuffmanTableSpec
instance Serialize JpgQuantTableSpec
instance SizeCalculable JpgQuantTableSpec
instance (SizeCalculable a, Serialize a) => Serialize (TableList a)
instance SizeCalculable JpgScanHeader
instance SizeCalculable JpgScanSpecification
instance SizeCalculable JpgComponent
instance SizeCalculable JpgFrameHeader
-- | Modules used for Bitmap file (.bmp) file loading and writing
module Codec.Picture.Bitmap
-- | Write an image in a file use the bitmap format.
writeBitmap :: BmpEncodable pixel => FilePath -> Image pixel -> IO ()
-- | Encode an image into a bytestring in .bmp format ready to be written
-- on disk.
encodeBitmap :: BmpEncodable pixel => Image pixel -> ByteString
-- | Try to decode a bitmap image. Right now this function can output the
-- following pixel types :
--
--
decodeBitmap :: ByteString -> Either String DynamicImage
-- | Encode a dynamic image in bmp if possible, supported pixel type are :
--
--
-- - RGB8
-- - RGBA8
-- - Y8
--
encodeDynamicBitmap :: DynamicImage -> Either String ByteString
-- | Write a dynamic image in a .bmp image file if possible. The same
-- restriction as encodeDynamicBitmap apply.
writeDynamicBitmap :: FilePath -> DynamicImage -> IO (Either String Bool)
-- | All the instance of this class can be written as a bitmap file using
-- this library.
class BmpEncodable pixel where defaultPalette _ = BmpPalette []
instance BmpEncodable PixelRGB8
instance BmpEncodable PixelRGBA8
instance BmpEncodable Pixel8
instance Serialize BmpInfoHeader
instance Serialize BmpHeader
module Codec.Picture.Saving
-- | This function will try to do anything to encode an image as JPEG, make
-- all color conversion and such. Equivalent of decodeImage for
-- jpeg encoding
imageToJpg :: Int -> DynamicImage -> ByteString
-- | This function will try to do anything to encode an image as PNG, make
-- all color conversion and such. Equivalent of decodeImage for
-- PNG encoding
imageToPng :: DynamicImage -> ByteString
-- | This function will try to do anything to encode an image as bitmap,
-- make all color conversion and such. Equivalent of decodeImage
-- for Bitmap encoding
imageToBitmap :: DynamicImage -> ByteString
-- | Main module exporting import/export functions 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
module Codec.Picture
-- | Load an image file without even thinking about it, it does everything
-- as decodeImage
readImage :: FilePath -> IO (Either String DynamicImage)
-- | 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 succeed will
-- return the decoded image in it's own colorspace
decodeImage :: ByteString -> Either String DynamicImage
-- | 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)
--
pixelMap :: (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
-- | Create an image given a function to generate pixels. The function will
-- receive value from 0 to width-1 for the x parameter and 0 to height-1
-- for the y parameter. The coordinate 0,0 is 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 -> Image PixelRGB8
-- imageCreator path = writePng path $ generateImage pixelRenderer 250 300
-- where pixelRenderer x y = PixelRGB8 x y 128
--
generateImage :: Pixel a => (Int -> Int -> a) -> Int -> Int -> Image a
-- | This function implement the same algorithm as generateImage,
-- and let use an user-defined state
generateFoldImage :: Pixel a => (acc -> Int -> Int -> (acc, a)) -> acc -> Int -> Int -> (acc, Image a)
-- | Save an image to a '.bmp' file, will do everything it can to save an
-- image.
saveBmpImage :: String -> DynamicImage -> IO ()
-- | Save an image to a '.jpg' file, will do everything it can to save an
-- image.
saveJpgImage :: Int -> String -> 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 <- decodeImage pathIn
-- case eitherImg of
-- Left _ -> return ()
-- Right img -> savePngImage img
--
savePngImage :: String -> DynamicImage -> IO ()
-- | All the instance of this class can be written as a bitmap file using
-- this library.
class BmpEncodable pixel where defaultPalette _ = BmpPalette []
-- | Write an image in a file use the bitmap format.
writeBitmap :: BmpEncodable pixel => FilePath -> Image pixel -> IO ()
-- | Encode an image into a bytestring in .bmp format ready to be written
-- on disk.
encodeBitmap :: BmpEncodable pixel => Image pixel -> ByteString
-- | Try to load a .bmp file. The colorspace would be RGB or RGBA
readBitmap :: FilePath -> IO (Either String DynamicImage)
-- | Try to decode a bitmap image. Right now this function can output the
-- following pixel types :
--
--
decodeBitmap :: ByteString -> Either String DynamicImage
-- | Encode a dynamic image in bmp if possible, supported pixel type are :
--
--
-- - RGB8
-- - RGBA8
-- - Y8
--
encodeDynamicBitmap :: DynamicImage -> Either String ByteString
-- | Write a dynamic image in a .bmp image file if possible. The same
-- restriction as encodeDynamicBitmap apply.
writeDynamicBitmap :: FilePath -> DynamicImage -> IO (Either String Bool)
-- | 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
readJpeg :: FilePath -> IO (Either String DynamicImage)
-- | Try to decompress 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
--
-- This function can output the following pixel types :
--
--
-- - PixelY8
-- - PixelYCbCr8
--
decodeJpeg :: ByteString -> Either String DynamicImage
-- | Encode an image in jpeg at a reasonnable quality level. If you want
-- better quality or reduced file size, you should use
-- encodeJpegAtQuality
encodeJpeg :: Image PixelYCbCr8 -> ByteString
-- | Function to call to encode an image to jpeg. The quality factor should
-- be between 0 and 100 (100 being the best quality).
encodeJpegAtQuality :: Word8 -> Image PixelYCbCr8 -> ByteString
-- | Encode an image into a png if possible.
class PngSavable a
encodePng :: PngSavable a => Image a -> ByteString
-- | Helper function trying to load a png file from a file on disk.
readPng :: FilePath -> IO (Either String DynamicImage)
-- | Transform a raw png image to an image, without modifying the
-- underlying pixel type. If the image is greyscale and < 8 bits, a
-- transformation to RGBA8 is performed. This should change in the
-- future. The resulting image let you manage the pixel types.
--
-- This function can output the following pixel types :
--
--
-- - PixelY8
-- - PixelYA8
-- - PixelRGB8
-- - PixelRGBA8
--
decodePng :: ByteString -> Either String DynamicImage
-- | Helper function to directly write an image as a png on disk.
writePng :: PngSavable pixel => FilePath -> Image pixel -> IO ()
-- | Encode a dynamic image in bmp if possible, supported pixel type are :
--
--
-- - Y8
-- - YA8
-- - RGB8
-- - RGBA8
--
encodeDynamicPng :: DynamicImage -> Either String ByteString
-- | Write a dynamic image in a .png image file if possible. The same
-- restriction as encodeDynamicPng apply.
writeDynamicPng :: FilePath -> DynamicImage -> IO (Either String Bool)
-- | 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.
data Image a
Image :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> Vector Word8 -> Image a
-- | Width of the image in pixels
imageWidth :: Image a -> {-# UNPACK #-} !Int
-- | Height of the image in pixels.
imageHeight :: Image a -> {-# UNPACK #-} !Int
-- | The real image, to extract pixels at some position you should use the
-- helpers functions.
imageData :: Image a -> Vector Word8
-- | Type allowing the loading of an image with different pixel structures
data DynamicImage
-- | A greyscale image.
ImageY8 :: (Image Pixel8) -> DynamicImage
-- | An image in greyscale with an alpha channel.
ImageYA8 :: (Image PixelYA8) -> DynamicImage
-- | An image in true color.
ImageRGB8 :: (Image PixelRGB8) -> DynamicImage
-- | An image in true color and an alpha channel.
ImageRGBA8 :: (Image PixelRGBA8) -> DynamicImage
-- | An image in the colorspace used by Jpeg images.
ImageYCbCr8 :: (Image PixelYCbCr8) -> DynamicImage
-- | Typeclass used to query a type about it's properties regarding casting
-- to other pixel types
class Serialize a => Pixel a where pixelBaseIndex (Image {imageWidth = w}) x y = (x + y * w) * componentCount (undefined :: a) mutablePixelBaseIndex (MutableImage {mutableImageWidth = w}) x y = (x + y * w) * componentCount (undefined :: a)
canPromoteTo :: Pixel a => a -> PixelType -> Bool
componentCount :: Pixel a => a -> Int
pixelBaseIndex :: Pixel a => Image a -> Int -> Int -> Int
mutablePixelBaseIndex :: Pixel a => MutableImage s a -> Int -> Int -> Int
promotionType :: Pixel a => a -> PixelType
pixelAt :: Pixel a => Image a -> Int -> Int -> a
readPixel :: Pixel a => MutableImage s a -> Int -> Int -> ST s a
writePixel :: Pixel a => MutableImage s a -> Int -> Int -> a -> ST s ()
-- | Simple alias for greyscale value in 8 bits.
type Pixel8 = Word8
-- | Pixel type storing Luminance (Y) and alpha information on 8 bits.
-- Value are stored in the following order :
--
--
data PixelYA8
PixelYA8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelYA8
-- | Pixel type storing classic pixel on 8 bits Value are stored in the
-- following order :
--
--
-- - Red
-- - Green
-- - Blue
--
data PixelRGB8
PixelRGB8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelRGB8
-- | Pixel type storing a classic pixel, with an alpha component. Values
-- are stored in the following order
--
--
-- - Red
-- - Green
-- - Blue
-- - Alpha
--
data PixelRGBA8
PixelRGBA8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelRGBA8
-- | Pixel storing data in the YCbCr colorspace, value are stored in the
-- following order :
--
--
-- - Y (luminance)
-- - Cr
-- - Cb
--
data PixelYCbCr8
PixelYCbCr8 :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> PixelYCbCr8