hadronized / quaazar

Realtime 3D engine
BSD 3-Clause "New" or "Revised" License
6 stars 2 forks source link

Support format YCbCr #75

Closed hadronized closed 9 years ago

hadronized commented 9 years ago

The change is pretty straightforward. We have to iterate over pixels and change them. To iterate over them, we can use this:

[mapPixel image x y | x <- [0..imageWidth image - 1], y <- [0..imageHeight image - 1]]

That’s pretty straightforward. We can then retrieve the pixels and store them in a list. mapPixel could simply take the pixel, and output a [Float]. We could even generalize that to any kind of image:

class (Pixel a) => ConvertPixel a where
  convertPixel :: a -> [Float]

Let’s see the implementation for PixelRGBF for instance:

instance ConvertPixel PixelRGBF where
  convertPixel (PixelRGBF r g b) = [realToFrac r,realToFrac g,realToFrac b]

How simple is that heh? :) For YCbCr8, using this conversion formula:

instance ConvertPixel PixelYCbCr8 where
  convertPixel (PixelYCbCr8 y cb cr) =
      [
        y' + 1.402 * cr'
      , y' - 0.34414 * cb' - 0.71414 * cr'
      , y' + 1.772 * cb'
      ]
    where
      cr' = realToFrac cr - 128
      cb' = realToFrac cb - 128