View Single Post
Old 31st May 2012, 18:10   #14  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Here Is the VB code for rec.601 to RGB and Back again.

Code:
Option Explicit
Option Base 0


' --------------------------------------------------------
Private Const cyb As Double = 0.114
Private Const cyg As Double = 0.587
Private Const cyr As Double = 0.299
Private Const cbu As Double = 2.018
Private Const crv As Double = 1.596
Private Const cgu As Double = 0.391
Private Const cgv As Double = 0.813
Private Const yDwnScale As Double = (219# / 255#)
Private Const yUpScale As Double = (1# / yDwnScale)
Private Const yOffset As Integer = 16
Private Const uvOffset As Integer = 128
' -----------------------------------------------------------------

Public Sub RGBtoYUV(r As Long, g As Long, b As Long, _
    ByRef Yref As Long, ByRef Uref As Long, ByRef Vref As Long)
Dim scaled_y As Double, b_y As Double, r_y As Double
Dim y As Long, U As Long, V As Long
    scaled_y = cyr * r + cyg * g + cyb * b
    b_y = b - scaled_y
    r_y = r - scaled_y
    y = Round(scaled_y * yDwnScale + yOffset)     ' CCIR Ranging
    U = Round(b_y / cbu + uvOffset)
    V = Round(r_y / crv + uvOffset)
    y = IIf(y < 0, 0, IIf(y > 255, 255, y))
    U = IIf(U < 0, 0, IIf(U > 255, 255, U))
    V = IIf(V < 0, 0, IIf(V > 255, 255, V))
    Yref = y
    Uref = U
    Vref = V
End Sub
        

Public Sub YUVtoRGB(y As Long, U As Long, V As Long, _
    ByRef Rref As Long, ByRef Gref As Long, ByRef Bref As Long)
Dim scaled_y As Double
Dim b_y As Double, r_y As Double
Dim r As Long, g As Long, b As Long
    scaled_y = (y - yOffset) * yUpScale
    r = Round(scaled_y + (V - uvOffset) * crv)
    g = Round(scaled_y - (U - uvOffset) * cgu - (V - uvOffset) * cgv)
    b = Round(scaled_y + (U - uvOffset) * cbu)
    r = IIf(r < 0, 0, IIf(r > 255, 255, r))
    g = IIf(g < 0, 0, IIf(g > 255, 255, g))
    b = IIf(b < 0, 0, IIf(b > 255, 255, b))
    Rref = r
    Gref = g
    Bref = b
End Sub
If someone in the know could provide the numbers for rec.709 and also for PC.601 and PC.709 then I could plug those
into the ColorYUV2 graffer (I kept reading different info on the conversions on the net and got miffed with about 17 different
versions claming to do the same, thats why I got a bit miffed and put it on the back burner for a year +).

I guess I could then add a selection option to let the graffer know what it is dealing with (PC.709 etc) and perhaps implement
the ability to eg load in from a number of sequencial bitmaps to see how the settings affected the whole clip (of a eg SelectEvery 100,
sub clip). Perhaps a future version might also be able to load in an AVI or AVS and use that in addition to a bitmap sequence,
when I eventually figure out how.
EDIT: Note, above code converts eg Rec601 Luma Y of 128 to RGB 128, new color conversion routines
being worked on right now will convert Rec601 Luma Y of 126 (actual Rec601 mid point) to RGB 128.
Using new method outlined here:

http://avisynth.org/mediawiki/Color_conversions
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 7th June 2012 at 10:03.
StainlessS is offline   Reply With Quote