title: sRGB <-> linear RGB conversion precision
body: <p>I am trying to understand the basics of image/video processing and recently I learned that all the processing should be applied to a linear representation of an image. So, I wrote two functions for sRGB -> RGB and RGB -> sRGB:</p>
  
  <pre><code>void sRGB2lRGB(std::vector&lt;unsigned char&gt;&amp; in, std::vector&lt;unsigned char&gt;&amp; out)
  {
      std::vector&lt;double&gt; temp(in.begin(), in.end());
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          temp[i] /= 255.0;
      }
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          if (temp[i] &lt;= 0.04045)
          {
              temp[i] /= 12.92;
          }
          else
          {
              temp[i] = std::pow((temp[i] + 0.055) / 1.055, 2.4);
          }
      }
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          out[i] = temp[i] * (int)255 + 0.5;
      }
  }
  
  void lRGB2sRGB(std::vector&lt;unsigned char&gt;&amp; in, std::vector&lt;unsigned char&gt;&amp; out)
  {
      std::vector&lt;double&gt; temp(in.begin(), in.end());
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          temp[i] /= 255.0;
      }
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          if (temp[i] &lt;= 0.0031308)
          {
              temp[i] *= 12.92;
          }
          else
          {
              temp[i] = 1.055 * std::pow(temp[i], 1.0 / 2.4) - 0.055;
          }
      }
  
      for (int i = 0; i &lt; temp.size(); i++)
      {
          out[i] = temp[i] * (int)255 + 0.5;
      }
  }
  </code></pre>
  
  <p>To test it I tried this:</p>
  
  <pre><code>int main()
  {
      std::vector&lt;unsigned char&gt; in(255);
      std::vector&lt;unsigned char&gt; out(255);
      std::vector&lt;unsigned char&gt; out2(255);
  
      for (int i = 0; i &lt; 255; i++)
      {
          in[i] = i;
      }
  
      sRGB2lRGB(in, out);
      lRGB2sRGB(out, out2);
  
      for (int i = 0; i &lt; 255; i++)
      {
          if (out2[i] != i)
          {
              std::cout &lt;&lt; "was: " &lt;&lt; (int)in[i] &lt;&lt; ", now: " &lt;&lt; (int)out2[i] &lt;&lt; '\n';
          }
      }
  }
  </code></pre>
  
  <p>But it appeared that the closer the value to 0, the more inaccurate the result would be. The output is:</p>
  
  <pre><code>was: 1, now: 0
  was: 2, now: 0
  was: 3, now: 0
  was: 4, now: 0
  was: 5, now: 0
  was: 6, now: 0
  was: 7, now: 13
  was: 8, now: 13
  was: 9, now: 13
  was: 10, now: 13
  was: 11, now: 13
  was: 12, now: 13
  was: 14, now: 13
  was: 15, now: 13
  was: 16, now: 13
  was: 17, now: 13
  was: 18, now: 22
  was: 19, now: 22
  was: 20, now: 22
  was: 21, now: 22
  was: 23, now: 22
  was: 24, now: 22
  was: 25, now: 22
  was: 26, now: 28
  was: 27, now: 28
  was: 29, now: 28
  was: 30, now: 28
  was: 31, now: 28
  was: 32, now: 34
  was: 33, now: 34
  was: 35, now: 34
  was: 36, now: 34
  was: 37, now: 38
  was: 39, now: 38
  was: 40, now: 38
  was: 41, now: 42
  was: 43, now: 42
  was: 44, now: 42
  was: 45, now: 46
  was: 47, now: 46
  was: 48, now: 50
  was: 49, now: 50
  was: 51, now: 50
  was: 52, now: 53
  was: 54, now: 53
  was: 55, now: 56
  was: 57, now: 56
  was: 58, now: 59
  was: 60, now: 61
  was: 62, now: 61
  was: 63, now: 64
  was: 65, now: 64
  was: 67, now: 66
  was: 68, now: 69
  was: 70, now: 71
  was: 72, now: 73
  was: 74, now: 73
  was: 76, now: 75
  was: 78, now: 77
  was: 80, now: 79
  was: 82, now: 83
  was: 84, now: 85
  was: 87, now: 86
  was: 89, now: 88
  was: 91, now: 92
  was: 94, now: 95
  was: 97, now: 96
  was: 100, now: 99
  was: 103, now: 104
  was: 107, now: 106
  was: 111, now: 112
  was: 116, now: 117
  was: 123, now: 124
  </code></pre>
  
  <p>Where am I wrong?</p>