class ImageFormat format => Readable img format where
decode :: format -> B.ByteString -> Either String img
-- | | |- returns codification error or the something of type img.
-- | |- This is the raw bytestring of the image
-- |- This is a type which implements the ImageFormat typeclass (see below)
my_bytestring :: ByteString
my_bytestring = ... whateve
wrong_my_image = decode PNG some_bytestring
-- You could think that this read an Image from a PNG but, which image should we pick??
-- There are many instances for this. For example:
-- Readable (Image VS RGB Double) PNG reads a PNG as a Vector of Doubles in the RGB color space
-- Readable (Image VS RGBA Word8) PNG reads a PNG as a Vector of 8-bits unsigned integers in the RGB with alpha color space
-- Readable (Image VS Y Word16) PNG reads a PNG as a Vector of 16-bits unsigned integers in the YUV color space
-- etc...
right_my_image_1 :: Either String (Image VS RGB Double) -- For example
right_my_image_1 = decode PNG my_bytestring
right_my_image_2 = decode PNG my_bytestring :: Either String (Image VS RGB Double)
-- Notice that you must(!!) specify the type you want to read to with explicit
-- annotations. Wheter the annotation is in two lines or one line is up to you.
1条答案
按热度按时间g6ll5ycj1#
当你有胖箭头的时候,类型类有点乱。类型类
Readable
的目的是定义可以从给定的ImageFormat
读取的类型集,其中ImageFormat
也是一个类型类。有点冗长,但下面会很清楚;请原谅我。现在,您可以在文档中检查每个类型类的示例。例如
ImageFormat
就有这些示例。你的第一个论点应该是其中之一。不仅如此,您还需要确保
img format
对实现了Readable
类型类。例如,Readable
有以下示例:Readable (Image VS RGB Double) PNG
Readable [Image VS RGB Double] (Seq GIF)
你可以将这些解释为“从一个
PNG
我可以读和Image VS RGB Double
和从一个序列的GIF
我可以读[Image VS RGB Double]
(图像列表)”。但更重要的是没有(!!)示例Readable [Image VS RGB Double] PNG
这意味着你不能从PNG中读取图像列表。
那么,如何使用
decode
函数呢?.好吧,我们知道第一个参数应该是ImageFormat
的一个示例,所以让我们选择PNG