Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Trying to convert from planar NV12 to RGB (John Obbele)
2. fmap versus bind (Sean Perry)
3. Re: fmap versus bind (Antoine Latter)
4. Re: fmap versus bind (Felipe Almeida Lessa)
----------------------------------------------------------------------
Message: 1
Date: Mon, 2 May 2011 22:11:32 +0200
From: John Obbele <[email protected]>
Subject: [Haskell-beginners] Trying to convert from planar NV12 to RGB
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I'm playing with some raw video streams. These raw data provide
YUV (or YCrCb) components interleaved between each other and it
is sometimes quite cumbersome to find a triplet of matching YUV
bytes before converting them to RGB.
For "packed" formats such as YUY2, the operation seems pretty
straight forward in Haskell:
>-- | Converting from packed YUY2 4:2:2 pixels to RGB ones.
>-- YUY2: Y0 U0 Y1 V0 < -- 2 macro pixels --> (Y0:U0:V0)+(Y1:U0:V0)
>yuy2ToRGB :: B.ByteString -> B.ByteString
>yuy2ToRGB bs = f [] bs
> where
> f acc xs | B.null xs = B.pack $ reverse acc --
> | otherwise = let [y0, u0, y1, v0] = B.unpack (B.take 4 xs)
> (r0,g0,b0) = yuv2rgb (y0,u0,v0)
> (r1,g1,b1) = yuv2rgb (y1,u0,v0)
> in f (b1:g1:r1:b0:g0:r0:acc) (B.drop 4 xs)
But on the other hand, I'm having some headaches converting from
planar NV12 to RGB.
For example, A width=4 and height=4 NV12 frame is stored in a
1-dimensional array of (4 * 4 * 3/2 = 24) elements:
| Y00 | Y01 | Y02 | Y03 | Y04 | Y05 | Y06 | Y07 | Y08 | Y09 | Y10 | Y11 | Y12 |
Y13 | Y14 | Y15 | U00 | V00 | U01 | V01 | U02 | V02 | U03 | V03 |
This array could be unfold to the following matrix
representation, and you see that the luminance (Y) bytes are
provided first, followed by a sequence of (U,V) pairs.
| Y00 | Y01 | Y02 | Y03 |
| Y04 | Y05 | Y06 | Y07 |
| Y08 | Y09 | Y10 | Y11 |
| Y12 | Y13 | Y14 | Y15 |
|-----------------------|
| U00 | V00 | U01 | V01 |
| U02 | V02 | U03 | V03 |
Now, if you want to get the RGB value for every pixel, you have
to fetch the corresponding (U,V) components at the end of the
frame.
pixel00 will need (Y00,U00,V00)
pixel02 will need (Y02,U01,V01)
...
pixel15 will need (Y15,U03,V03)
So far, I have this Haskell function to convert NV12 to RGB.
>nv12ToRGB w h bs =
> B.pack $ concatMap f [0.. w * h - 1]
> where
> f i = let m = (i `div` w) `div` 2 -- line index modulo 2
> n = (i `rem` w) `div` 2 -- column index modulo 2
> iuv = m * w `div` 2 + n
> (r,g,b) = yuv2rgb (pixelY i, pixelU iuv, pixelV iuv)
> in [r,g,b]
>
> pixelY i = B.index bs i
> pixelU i = B.index bs (w*h+2*i)
> pixelV i = B.index bs (w*h+2*i+1)
I don't know if converting ByteString to list, using map with a
not-so-simple f function, then concatenating and repacking the
ByteString is the best solution one could find in Haskell and I
would like to know if by chance some "senior" haskellers would
know some tricks (like list comprehension or
ByteString.unfoldWhatever) to improve the previous code.
regards,
/john
------------------------------
Message: 2
Date: Mon, 2 May 2011 18:56:31 -0700
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] fmap versus bind
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
i came across hlint today and ran it on my experiments. It pointed out places
where I was overly cautious with parens or used a $ and forget to remove it
later. But one suggestion it made I was curious about.
Why does it recommend using fmap over >>=? Idiomatically does fmap make more
sense when the Monad is more like a collection?
For instance I had "(applyOp op x y) >>= (flip (:) ds)". This seems more clear
than "fmap ((:) ds) (applyOp op x y)".
------------------------------
Message: 3
Date: Mon, 2 May 2011 21:14:13 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: Sean Perry <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Mon, May 2, 2011 at 8:56 PM, Sean Perry <[email protected]> wrote:
> i came across hlint today and ran it on my experiments. It pointed out places
> where I was overly cautious with parens or used a $ and forget to remove it
> later. But one suggestion it made I was curious about.
>
> Why does it recommend using fmap over >>=? Idiomatically does fmap make more
> sense when the Monad is more like a collection?
>
> For instance I had "(applyOp op x y) >>= (flip (:) ds)". This seems more
> clear than "fmap ((:) ds) (applyOp op x y)".
Part of the reason for the suggestion is that bind is "more powerful"
than fmap, which means that it asks more of the implementing type,
which then means that fewer type satisfy bind than satisfy fmap. So
your function is more general when written using fmap.
I might write this with the infix version of fmap, but some folks
don't like the explosion of operators:
> ((:) ds) <$> applyOp op x y
or closer to yours:
> flip (:) ds <$> applyOp op x y
If the type of the function is already fixed I think it comes down to
personal preference.
Antoine
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Mon, 2 May 2011 23:22:02 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: Antoine Latter <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Mon, May 2, 2011 at 11:14 PM, Antoine Latter <[email protected]> wrote:
> Part of the reason for the suggestion is that bind is "more powerful"
> than fmap, which means that it asks more of the implementing type,
> which then means that fewer type satisfy bind than satisfy fmap. So
> your function is more general when written using fmap.
And depending on the data type, 'f <$> m' may be more efficient than
'm >>= return . f'
> I might write this with the infix version of fmap, but some folks
> don't like the explosion of operators:
>
>> ((:) ds) <$> applyOp op x y
Note that this should be "(: ds) <$> applyOp op x y". I mean,
(:) ds === (ds :)
flip (:) ds === (\x -> x : ds) === (: ds)
> or closer to yours:
>
>> flip (:) ds <$> applyOp op x y
>
> If the type of the function is already fixed I think it comes down to
> personal preference.
Applicative style is very nice, specially when your functions have
multiple arguments. Consider
op1 >>= \x1 -> op2 >>= \x2 -> return (f x1 x2)
do {x1 <- op1
x2 <- op2
return (f x1 x2)}
against
f <$> op1 <*> op2
Cheers,
--
Felipe.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 3
****************************************