class ImageFormat format => Readable img format where
decode :: format -> B.ByteString -> Either String img
Run Code Online (Sandbox Code Playgroud)
我不明白第一个参数应该是什么?例子会有所帮助。
当你有粗箭头时,类型类会有点混乱。typeclass 的目的Readable是定义可以从给定的 读取的类型集ImageFormat,其中 anImageFormat也是一个类型类。有点冗长,但下面会很清楚;请耐心听我说。
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)
Run Code Online (Sandbox Code Playgroud)
现在,您可以在文档中查看每个类型类的实例。比如ImageFormat有这些实例。
所以,你的第一个论点应该是其中之一。但不仅如此,您还需要确保该对img format实现了Readable类型类。例如Readable有这些实例(除其他外):
Readable (Image VS RGB Double) PNGReadable [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
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |