File: YCbCr.cs

package info (click to toggle)
wordpress 6.8.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 134,984 kB
  • sloc: javascript: 501,260; php: 293,711; cs: 6,126; sh: 457; xml: 22; makefile: 14
file content (59 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// Copyright (c) 2008 Jeffrey Powers for Fluxcapacity Open Source.
/// Under the MIT License, details: License.txt.

using System;

namespace FluxJpeg.Core
{
    internal class YCbCr 
    {

        public static void toRGB(ref byte c1, ref byte c2, ref byte c3)
        {
            double dY = (double)c1;
            double dCb2 = (double)c2 - 128;
            double dCr2 = (double)c3 - 128;

            double dR = dY + 1.402 * dCr2;
            double dG = dY - 0.34414 * dCb2 - 0.71414 * dCr2;
            double dB = dY + 1.772 * dCb2;

            c1 = dR > 255 ? (byte)255 : dR < 0 ? (byte)0 : (byte)dR;
            c2 = dG > 255 ? (byte)255 : dG < 0 ? (byte)0 : (byte)dG;
            c3 = dB > 255 ? (byte)255 : dB < 0 ? (byte)0 : (byte)dB;
        }

        public static void fromRGB(ref byte c1, ref byte c2, ref byte c3)
        {
            double dR = (double)c1;
            double dG = (double)c2;
            double dB = (double)c3;

            c1 = (byte)(0.299 * dR + 0.587 * dG + 0.114 * dB);
            c2 = (byte)(-0.16874 * dR - 0.33126 * dG + 0.5 * dB + 128);
            c3 = (byte)(0.5 * dR - 0.41869 * dG - 0.08131 * dB + 128);
        }

        ///* RGB to YCbCr range 0-255 */
        //public static void fromRGB(byte[] rgb, byte[] ycbcr)
        //{
        //    ycbcr[0] = (byte)((0.299 * (float)rgb[0] + 0.587 * (float)rgb[1] + 0.114 * (float)rgb[2]));
        //    ycbcr[1] = (byte)(128 + (byte)((-0.16874 * (float)rgb[0] - 0.33126 * (float)rgb[1] + 0.5 * (float)rgb[2])));
        //    ycbcr[2] = (byte)(128 + (byte)((0.5 * (float)rgb[0] - 0.41869 * (float)rgb[1] - 0.08131 * (float)rgb[2])));
        //}


        /* RGB to YCbCr range 0-255 */
        public static float[] fromRGB(float[] data)
        {
            float[] dest = new float[3];

            dest[0] = (float)((0.299 * (float)data[0] + 0.587 * (float)data[1] + 0.114 * (float)data[2]));
            dest[1] = 128 + (float)((-0.16874 * (float)data[0] - 0.33126 * (float)data[1] + 0.5 * (float)data[2]));
            dest[2] = 128 + (float)((0.5 * (float)data[0] - 0.41869 * (float)data[1] - 0.08131 * (float)data[2]));

            return (dest);
        }
    }

}