1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 2<HTML 3><HEAD 4><TITLE 5>Colorspaces</TITLE 6><META 7NAME="GENERATOR" 8CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK 9REL="HOME" 10TITLE="Video for Linux Two API Specification" 11HREF="book1.htm"><LINK 12REL="UP" 13TITLE="Image Formats" 14HREF="c2030.htm"><LINK 15REL="PREVIOUS" 16TITLE="Image Formats" 17HREF="c2030.htm"><LINK 18REL="NEXT" 19TITLE="Indexed Format" 20HREF="x2428.htm"></HEAD 21><BODY 22CLASS="SECTION" 23BGCOLOR="#FFFFFF" 24TEXT="#000000" 25LINK="#0000FF" 26VLINK="#840084" 27ALINK="#0000FF" 28><DIV 29CLASS="NAVHEADER" 30><TABLE 31SUMMARY="Header navigation table" 32WIDTH="100%" 33BORDER="0" 34CELLPADDING="0" 35CELLSPACING="0" 36><TR 37><TH 38COLSPAN="3" 39ALIGN="center" 40>Video for Linux Two API Specification: Revision 0.24</TH 41></TR 42><TR 43><TD 44WIDTH="10%" 45ALIGN="left" 46VALIGN="bottom" 47><A 48HREF="c2030.htm" 49ACCESSKEY="P" 50>Prev</A 51></TD 52><TD 53WIDTH="80%" 54ALIGN="center" 55VALIGN="bottom" 56>Chapter 2. Image Formats</TD 57><TD 58WIDTH="10%" 59ALIGN="right" 60VALIGN="bottom" 61><A 62HREF="x2428.htm" 63ACCESSKEY="N" 64>Next</A 65></TD 66></TR 67></TABLE 68><HR 69ALIGN="LEFT" 70WIDTH="100%"></DIV 71><DIV 72CLASS="SECTION" 73><H1 74CLASS="SECTION" 75><A 76NAME="COLORSPACES" 77>2.2. Colorspaces</A 78></H1 79><P 80>[intro]</P 81><P 82> <P 83></P 84><DIV 85CLASS="VARIABLELIST" 86><DL 87><DT 88>Gamma Correction</DT 89><DD 90><P 91>[to do]</P 92><P 93>E'<SUB 94>R</SUB 95> = f(R)</P 96><P 97>E'<SUB 98>G</SUB 99> = f(G)</P 100><P 101>E'<SUB 102>B</SUB 103> = f(B)</P 104></DD 105><DT 106>Construction of luminance and color-difference 107signals</DT 108><DD 109><P 110>[to do]</P 111><P 112>E'<SUB 113>Y</SUB 114> = 115Coeff<SUB 116>R</SUB 117> E'<SUB 118>R</SUB 119> 120+ Coeff<SUB 121>G</SUB 122> E'<SUB 123>G</SUB 124> 125+ Coeff<SUB 126>B</SUB 127> E'<SUB 128>B</SUB 129></P 130><P 131>(E'<SUB 132>R</SUB 133> - E'<SUB 134>Y</SUB 135>) = E'<SUB 136>R</SUB 137> 138- Coeff<SUB 139>R</SUB 140> E'<SUB 141>R</SUB 142> 143- Coeff<SUB 144>G</SUB 145> E'<SUB 146>G</SUB 147> 148- Coeff<SUB 149>B</SUB 150> E'<SUB 151>B</SUB 152></P 153><P 154>(E'<SUB 155>B</SUB 156> - E'<SUB 157>Y</SUB 158>) = E'<SUB 159>B</SUB 160> 161- Coeff<SUB 162>R</SUB 163> E'<SUB 164>R</SUB 165> 166- Coeff<SUB 167>G</SUB 168> E'<SUB 169>G</SUB 170> 171- Coeff<SUB 172>B</SUB 173> E'<SUB 174>B</SUB 175></P 176></DD 177><DT 178>Re-normalized color-difference signals</DT 179><DD 180><P 181>The color-difference signals are scaled back to unity 182range [-0.5;+0.5]:</P 183><P 184>K<SUB 185>B</SUB 186> = 0.5 / (1 - Coeff<SUB 187>B</SUB 188>)</P 189><P 190>K<SUB 191>R</SUB 192> = 0.5 / (1 - Coeff<SUB 193>R</SUB 194>)</P 195><P 196>P<SUB 197>B</SUB 198> = 199K<SUB 200>B</SUB 201> (E'<SUB 202>B</SUB 203> - E'<SUB 204>Y</SUB 205>) = 206 0.5 (Coeff<SUB 207>R</SUB 208> / Coeff<SUB 209>B</SUB 210>) E'<SUB 211>R</SUB 212> 213+ 0.5 (Coeff<SUB 214>G</SUB 215> / Coeff<SUB 216>B</SUB 217>) E'<SUB 218>G</SUB 219> 220+ 0.5 E'<SUB 221>B</SUB 222></P 223><P 224>P<SUB 225>R</SUB 226> = 227K<SUB 228>R</SUB 229> (E'<SUB 230>R</SUB 231> - E'<SUB 232>Y</SUB 233>) = 234 0.5 E'<SUB 235>R</SUB 236> 237+ 0.5 (Coeff<SUB 238>G</SUB 239> / Coeff<SUB 240>R</SUB 241>) E'<SUB 242>G</SUB 243> 244+ 0.5 (Coeff<SUB 245>B</SUB 246> / Coeff<SUB 247>R</SUB 248>) E'<SUB 249>B</SUB 250></P 251></DD 252><DT 253>Quantization</DT 254><DD 255><P 256>[to do]</P 257><P 258>Y' = (Lum. Levels - 1) · E'<SUB 259>Y</SUB 260> + Lum. Offset</P 261><P 262>C<SUB 263>B</SUB 264> = (Chrom. Levels - 1) 265· P<SUB 266>B</SUB 267> + Chrom. Offset</P 268><P 269>C<SUB 270>R</SUB 271> = (Chrom. Levels - 1) 272· P<SUB 273>R</SUB 274> + Chrom. Offset</P 275><P 276>Rounding to the nearest integer and clamping to the range 277[0;255] finally yields the digital color components Y'CbCr 278stored in YUV images.</P 279></DD 280></DL 281></DIV 282> 283 </P 284><DIV 285CLASS="EXAMPLE" 286><A 287NAME="AEN2217" 288></A 289><P 290><B 291>Example 2-1. ITU-R Rec. BT.601 color conversion</B 292></P 293><P 294>Forward Transformation</P 295><PRE 296CLASS="PROGRAMLISTING" 297>int ER, EG, EB; /* gamma corrected RGB input [0;255] */ 298int Y1, Cb, Cr; /* output [0;255] */ 299 300double r, g, b; /* temporaries */ 301double y1, pb, pr; 302 303int 304clamp (double x) 305{ 306 int r = x; /* round to nearest */ 307 308 if (r < 0) return 0; 309 else if (r > 255) return 255; 310 else return r; 311} 312 313r = ER / 255.0; 314g = EG / 255.0; 315b = EB / 255.0; 316 317y1 = 0.299 * r + 0.587 * g + 0.114 * b; 318pb = -0.169 * r - 0.331 * g + 0.5 * b; 319pr = 0.5 * r - 0.419 * g - 0.081 * b; 320 321Y1 = clamp (219 * y1 + 16); 322Cb = clamp (224 * pb + 128); 323Cr = clamp (224 * pr + 128); 324 325/* or shorter */ 326 327y1 = 0.299 * ER + 0.587 * EG + 0.114 * EB; 328 329Y1 = clamp ( (219 / 255.0) * y1 + 16); 330Cb = clamp (((224 / 255.0) / (2 - 2 * 0.114)) * (EB - y1) + 128); 331Cr = clamp (((224 / 255.0) / (2 - 2 * 0.299)) * (ER - y1) + 128); 332 </PRE 333><P 334>Inverse Transformation</P 335><PRE 336CLASS="PROGRAMLISTING" 337>int Y1, Cb, Cr; /* gamma pre-corrected input [0;255] */ 338int ER, EG, EB; /* output [0;255] */ 339 340double r, g, b; /* temporaries */ 341double y1, pb, pr; 342 343int 344clamp (double x) 345{ 346 int r = x; /* round to nearest */ 347 348 if (r < 0) return 0; 349 else if (r > 255) return 255; 350 else return r; 351} 352 353y1 = (255 / 219.0) * (Y1 - 16); 354pb = (255 / 224.0) * (Cb - 128); 355pr = (255 / 224.0) * (Cr - 128); 356 357r = 1.0 * y1 + 0 * pb + 1.402 * pr; 358g = 1.0 * y1 - 0.344 * pb - 0.714 * pr; 359b = 1.0 * y1 + 1.772 * pb + 0 * pr; 360 361ER = clamp (r * 255); /* [ok? one should prob. limit y1,pb,pr] */ 362EG = clamp (g * 255); 363EB = clamp (b * 255); 364 </PRE 365></DIV 366><DIV 367CLASS="TABLE" 368><A 369NAME="V4L2-COLORSPACE" 370></A 371><P 372><B 373>Table 2-2. enum v4l2_colorspace</B 374></P 375><TABLE 376BORDER="1" 377WIDTH="100%" 378CLASS="CALSTABLE" 379><COL 380WIDTH="9%" 381ALIGN="LEFT"><COL 382WIDTH="9%" 383ALIGN="CENTER"><COL 384WIDTH="9%" 385ALIGN="LEFT"><COL 386WIDTH="9%" 387TITLE="CR"><COL 388WIDTH="9%" 389TITLE="CG"><COL 390WIDTH="9%" 391TITLE="CB"><COL 392WIDTH="9%" 393TITLE="WP"><COL 394WIDTH="9%" 395TITLE="GC"><COL 396WIDTH="9%" 397TITLE="LUM"><COL 398WIDTH="9%" 399TITLE="QY"><COL 400WIDTH="9%" 401TITLE="QC"><THEAD 402><TR 403><TH 404ROWSPAN="2" 405>Identifier</TH 406><TH 407ROWSPAN="2" 408>Value</TH 409><TH 410ROWSPAN="2" 411>Description</TH 412><TH 413COLSPAN="3" 414>Chromaticities<A 415NAME="AEN2246" 416HREF="x2123.htm#FTN.AEN2246" 417><SPAN 418CLASS="footnote" 419>[a]</SPAN 420></A 421></TH 422><TH 423ROWSPAN="2" 424>White Point</TH 425><TH 426ROWSPAN="2" 427>Gamma Correction</TH 428><TH 429ROWSPAN="2" 430>Luminance E'<SUB 431>Y</SUB 432></TH 433><TH 434COLSPAN="2" 435>Quantization</TH 436></TR 437><TR 438><TH 439>Red</TH 440><TH 441>Green</TH 442><TH 443>Blue</TH 444><TH 445>Y'</TH 446><TH 447>Cb, Cr</TH 448></TR 449></THEAD 450><TBODY 451VALIGN="TOP" 452><TR 453><TD 454><CODE 455CLASS="CONSTANT" 456>V4L2_COLORSPACE_SMPTE170M</CODE 457></TD 458><TD 459>1</TD 460><TD 461>NTSC/PAL according to <A 462HREF="b17127.htm#SMPTE170M" 463><ABBR 464CLASS="ABBREV" 465>SMPTE 170M</ABBR 466></A 467>, 468<A 469HREF="b17127.htm#ITU601" 470><ABBR 471CLASS="ABBREV" 472>ITU BT.601</ABBR 473></A 474></TD 475><TD 476>x = 0.630, y = 0.340</TD 477><TD 478>x = 0.310, y = 0.595</TD 479><TD 480>x = 0.155, y = 0.070</TD 481><TD 482>x = 0.3127, y = 0.3290, 483 Illuminant D<SUB 484>65</SUB 485></TD 486><TD 487>E' = 4.5 I for I ≤0.018, 4881.099 I<SUP 489>0.45</SUP 490> - 0.099 for 0.018 < I</TD 491><TD 492>0.299 E'<SUB 493>R</SUB 494> 495+ 0.587 E'<SUB 496>G</SUB 497> 498+ 0.114 E'<SUB 499>B</SUB 500></TD 501><TD 502>219 E'<SUB 503>Y</SUB 504> + 16</TD 505><TD 506>224 P<SUB 507>B,R</SUB 508> + 128</TD 509></TR 510><TR 511><TD 512><CODE 513CLASS="CONSTANT" 514>V4L2_COLORSPACE_SMPTE240M</CODE 515></TD 516><TD 517>2</TD 518><TD 519>1125-Line (US) HDTV, see <A 520HREF="b17127.htm#SMPTE240M" 521><ABBR 522CLASS="ABBREV" 523>SMPTE 240M</ABBR 524></A 525></TD 526><TD 527>x = 0.630, y = 0.340</TD 528><TD 529>x = 0.310, y = 0.595</TD 530><TD 531>x = 0.155, y = 0.070</TD 532><TD 533>x = 0.3127, y = 0.3290, 534 Illuminant D<SUB 535>65</SUB 536></TD 537><TD 538>E' = 4 I for I ≤0.0228, 5391.1115 I<SUP 540>0.45</SUP 541> - 0.1115 for 0.0228 < I</TD 542><TD 543>0.212 E'<SUB 544>R</SUB 545> 546+ 0.701 E'<SUB 547>G</SUB 548> 549+ 0.087 E'<SUB 550>B</SUB 551></TD 552><TD 553>219 E'<SUB 554>Y</SUB 555> + 16</TD 556><TD 557>224 P<SUB 558>B,R</SUB 559> + 128</TD 560></TR 561><TR 562><TD 563><CODE 564CLASS="CONSTANT" 565>V4L2_COLORSPACE_REC709</CODE 566></TD 567><TD 568>3</TD 569><TD 570>HDTV and modern devices, see <A 571HREF="b17127.htm#ITU709" 572><ABBR 573CLASS="ABBREV" 574>ITU BT.709</ABBR 575></A 576></TD 577><TD 578>x = 0.640, y = 0.330</TD 579><TD 580>x = 0.300, y = 0.600</TD 581><TD 582>x = 0.150, y = 0.060</TD 583><TD 584>x = 0.3127, y = 0.3290, 585 Illuminant D<SUB 586>65</SUB 587></TD 588><TD 589>E' = 4.5 I for I ≤0.018, 5901.099 I<SUP 591>0.45</SUP 592> - 0.099 for 0.018 < I</TD 593><TD 594>0.2125 E'<SUB 595>R</SUB 596> 597+ 0.7154 E'<SUB 598>G</SUB 599> 600+ 0.0721 E'<SUB 601>B</SUB 602></TD 603><TD 604>219 E'<SUB 605>Y</SUB 606> + 16</TD 607><TD 608>224 P<SUB 609>B,R</SUB 610> + 128</TD 611></TR 612><TR 613><TD 614><CODE 615CLASS="CONSTANT" 616>V4L2_COLORSPACE_BT878</CODE 617></TD 618><TD 619>4</TD 620><TD 621>Broken Bt878 extents<A 622NAME="AEN2329" 623HREF="x2123.htm#FTN.AEN2329" 624><SPAN 625CLASS="footnote" 626>[b]</SPAN 627></A 628>, <A 629HREF="b17127.htm#ITU601" 630><ABBR 631CLASS="ABBREV" 632>ITU BT.601</ABBR 633></A 634></TD 635><TD 636>?</TD 637><TD 638>?</TD 639><TD 640>?</TD 641><TD 642>?</TD 643><TD 644>?</TD 645><TD 646>0.299 E'<SUB 647>R</SUB 648> 649+ 0.587 E'<SUB 650>G</SUB 651> 652+ 0.114 E'<SUB 653>B</SUB 654></TD 655><TD 656><SPAN 657CLASS="emphasis" 658><I 659CLASS="EMPHASIS" 660>237</I 661></SPAN 662> E'<SUB 663>Y</SUB 664> + 16</TD 665><TD 666>224 P<SUB 667>B,R</SUB 668> + 128 (probably)</TD 669></TR 670><TR 671><TD 672><CODE 673CLASS="CONSTANT" 674>V4L2_COLORSPACE_470_SYSTEM_M</CODE 675></TD 676><TD 677>5</TD 678><TD 679>M/NTSC<A 680NAME="AEN2352" 681HREF="x2123.htm#FTN.AEN2352" 682><SPAN 683CLASS="footnote" 684>[c]</SPAN 685></A 686> according to <A 687HREF="b17127.htm#ITU470" 688><ABBR 689CLASS="ABBREV" 690>ITU BT.470</ABBR 691></A 692>, <A 693HREF="b17127.htm#ITU601" 694><ABBR 695CLASS="ABBREV" 696>ITU BT.601</ABBR 697></A 698></TD 699><TD 700>x = 0.67, y = 0.33</TD 701><TD 702>x = 0.21, y = 0.71</TD 703><TD 704>x = 0.14, y = 0.08</TD 705><TD 706>x = 0.310, y = 0.316, Illuminant C</TD 707><TD 708>?</TD 709><TD 710>0.299 E'<SUB 711>R</SUB 712> 713+ 0.587 E'<SUB 714>G</SUB 715> 716+ 0.114 E'<SUB 717>B</SUB 718></TD 719><TD 720>219 E'<SUB 721>Y</SUB 722> + 16</TD 723><TD 724>224 P<SUB 725>B,R</SUB 726> + 128</TD 727></TR 728><TR 729><TD 730><CODE 731CLASS="CONSTANT" 732>V4L2_COLORSPACE_470_SYSTEM_BG</CODE 733></TD 734><TD 735>6</TD 736><TD 737>625-line PAL and SECAM systems according to <A 738HREF="b17127.htm#ITU470" 739><ABBR 740CLASS="ABBREV" 741>ITU BT.470</ABBR 742></A 743>, <A 744HREF="b17127.htm#ITU601" 745><ABBR 746CLASS="ABBREV" 747>ITU BT.601</ABBR 748></A 749></TD 750><TD 751>x = 0.64, y = 0.33</TD 752><TD 753>x = 0.29, y = 0.60</TD 754><TD 755>x = 0.15, y = 0.06</TD 756><TD 757>x = 0.313, y = 0.329, 758Illuminant D<SUB 759>65</SUB 760></TD 761><TD 762>?</TD 763><TD 764>0.299 E'<SUB 765>R</SUB 766> 767+ 0.587 E'<SUB 768>G</SUB 769> 770+ 0.114 E'<SUB 771>B</SUB 772></TD 773><TD 774>219 E'<SUB 775>Y</SUB 776> + 16</TD 777><TD 778>224 P<SUB 779>B,R</SUB 780> + 128</TD 781></TR 782><TR 783><TD 784><CODE 785CLASS="CONSTANT" 786>V4L2_COLORSPACE_JPEG</CODE 787></TD 788><TD 789>7</TD 790><TD 791>JPEG Y'CbCr, see <A 792HREF="b17127.htm#JFIF" 793><ABBR 794CLASS="ABBREV" 795>JFIF</ABBR 796></A 797>, <A 798HREF="b17127.htm#ITU601" 799><ABBR 800CLASS="ABBREV" 801>ITU BT.601</ABBR 802></A 803></TD 804><TD 805>?</TD 806><TD 807>?</TD 808><TD 809>?</TD 810><TD 811>?</TD 812><TD 813>?</TD 814><TD 815>0.299 E'<SUB 816>R</SUB 817> 818+ 0.587 E'<SUB 819>G</SUB 820> 821+ 0.114 E'<SUB 822>B</SUB 823></TD 824><TD 825>256 E'<SUB 826>Y</SUB 827> + 16<A 828NAME="AEN2408" 829HREF="x2123.htm#FTN.AEN2408" 830><SPAN 831CLASS="footnote" 832>[d]</SPAN 833></A 834></TD 835><TD 836>256 P<SUB 837>B,R</SUB 838> + 128</TD 839></TR 840><TR 841><TD 842><CODE 843CLASS="CONSTANT" 844>V4L2_COLORSPACE_SRGB</CODE 845></TD 846><TD 847>8</TD 848><TD 849>[?]</TD 850><TD 851>x = 0.640, y = 0.330</TD 852><TD 853>x = 0.300, y = 0.600</TD 854><TD 855>x = 0.150, y = 0.060</TD 856><TD 857>x = 0.3127, y = 0.3290, 858 Illuminant D<SUB 859>65</SUB 860></TD 861><TD 862>E' = 4.5 I for I ≤0.018, 8631.099 I<SUP 864>0.45</SUP 865> - 0.099 for 0.018 < I</TD 866><TD 867COLSPAN="3" 868>n/a</TD 869></TR 870></TBODY 871><TR 872><TD 873COLSPAN="11" 874>Notes:<BR><A 875NAME="FTN.AEN2246" 876>a. </A 877>The coordinates of the color primaries are 878given in the CIE system (1931)<BR><A 879NAME="FTN.AEN2329" 880>b. </A 881>The ubiquitous Bt878 video capture chip 882quantizes E'<SUB 883>Y</SUB 884> to 238 levels, yielding a range 885of Y' = 16 … 253, unlike Rec. 601 Y' = 16 … 886235. This is not a typo in the Bt878 documentation, it has been 887implemented in silicon. The chroma extents are unclear.<BR><A 888NAME="FTN.AEN2352" 889>c. </A 890>No identifier exists for M/PAL which uses 891the chromaticities of M/NTSC, the remaining parameters are equal to B and 892G/PAL.<BR><A 893NAME="FTN.AEN2408" 894>d. </A 895>Note JFIF quantizes 896Y'P<SUB 897>B</SUB 898>P<SUB 899>R</SUB 900> in range [0;+1] and 901[-0.5;+0.5] to <SPAN 902CLASS="emphasis" 903><I 904CLASS="EMPHASIS" 905>257</I 906></SPAN 907> levels, however Y'CbCr signals 908are still clamped to [0;255].<BR></TD 909></TR 910></TABLE 911></DIV 912></DIV 913><DIV 914CLASS="NAVFOOTER" 915><HR 916ALIGN="LEFT" 917WIDTH="100%"><TABLE 918SUMMARY="Footer navigation table" 919WIDTH="100%" 920BORDER="0" 921CELLPADDING="0" 922CELLSPACING="0" 923><TR 924><TD 925WIDTH="33%" 926ALIGN="left" 927VALIGN="top" 928><A 929HREF="c2030.htm" 930ACCESSKEY="P" 931>Prev</A 932></TD 933><TD 934WIDTH="34%" 935ALIGN="center" 936VALIGN="top" 937><A 938HREF="book1.htm" 939ACCESSKEY="H" 940>Home</A 941></TD 942><TD 943WIDTH="33%" 944ALIGN="right" 945VALIGN="top" 946><A 947HREF="x2428.htm" 948ACCESSKEY="N" 949>Next</A 950></TD 951></TR 952><TR 953><TD 954WIDTH="33%" 955ALIGN="left" 956VALIGN="top" 957>Image Formats</TD 958><TD 959WIDTH="34%" 960ALIGN="center" 961VALIGN="top" 962><A 963HREF="c2030.htm" 964ACCESSKEY="U" 965>Up</A 966></TD 967><TD 968WIDTH="33%" 969ALIGN="right" 970VALIGN="top" 971>Indexed Format</TD 972></TR 973></TABLE 974></DIV 975></BODY 976></HTML 977> 978