title: JPEG: YCrCb <-> RGB conversion precision
body: <p>I've implemented rgb->ycrcb and ycrcb->rgb conversion using JPEG conversion formulae from<br>
  <a href="http://www.w3.org/Graphics/JPEG/jfif3.pdf" rel="nofollow">http://www.w3.org/Graphics/JPEG/jfif3.pdf</a> <br>
  (the same at: <a href="http://en.wikipedia.org/wiki/YCbCr" rel="nofollow">http://en.wikipedia.org/wiki/YCbCr</a> (JPEG conversion)).</p>
  
  <p>When checking whether results are correct (original->YCrCb->RGB), some of pixels differ by one, e.g 201->200.    </p>
  
  <p>Average percent of precision errors is 0.1%, so it's not critical.</p>
  
  <pre><code>/// converts RGB pixel to YCrCb using { en.wikipedia.org/wiki/YCbCr: JPEG conversion }
  ivect4 rgb2ycrcb(int r, int g, int b)
  {
      int y =  round(0.299*r + 0.587*g + 0.114*b) ;
      int cb = round(128.0 - (0.1687*r) - (0.3313*g) + (0.5*b));
      int cr = round(128.0 + (0.5*r) - (0.4187*g) - (0.0813*b));
      return ivect4(y, cr, cb, 255);
  }
  /// converts YCrCb pixel to RGB using { en.wikipedia.org/wiki/YCbCr: JPEG conversion }
  ivect4 ycrcb2rgb(int y, int cr, int cb)
  {
      int r = round(1.402*(cr-128) + y);
      int g = round(-0.34414*(cb-128)-0.71414*(cr-128) + y);
      int b = round(1.772*(cb-128) + y);
      return ivect4(r, g, b, 255);
  }
  </code></pre>
  
  <p>I use round formula: <br>
  floor((x) + 0.5)</p>
  
  <p>When using other types of rounding, e.g. float(int), or std::ceil(), results are even worse.</p>
  
  <p>So, <strong>does there exist the way to do YCrCb &lt;-> RGB conversion without loss in precision?</strong></p>