title: C++ how to load a 16bit TIFF file in a container to perform math operations on its data?
body: <p>I have writtent a small C++ console application with code::blocks that loads
  an array of values from a CSV file, performs a special "inverted" random dithering on the values, and exports the result as a PBM file (a bitmap).</p>
  
  <p>The density of black pixels on the final PBM picture depends on 3 independent variables: "Reflectance of the white", "Reflectance of the black", and the values of the CSV.</p>
  
  <p><strong>The reason I use a CSV file is because I don't know how I can directly load a TIFF file into my script.</strong> The values of my file "wall.csv" are produced by a python script that transforms any tiff file in a csv...</p>
  
  <p><strong>Could you please check my code and advise for a solution to load a TIFF and detect automatically the size of the image in pixels?
  The variables <code>colo</code> and <code>lines</code> define the size of the image contained as ASCII data in the CSV...
  And the image values are loaded in the <code>vector &lt;float&gt; CSV</code></strong></p>
  
  <p>What library would you use to load the tiff?</p>
  
  <p>Thanks!</p>
  
  <p><strong>code:</strong></p>
  
  <pre><code>#include &lt;deque&gt;
  #include &lt;cmath&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;algorithm&gt;
  #include &lt;random&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  deque &lt;float&gt; CSV;          // CSV input values, "PHOTOMETRY"
  deque &lt;float&gt; RND;          // will contain random values from 0.0 to 1.0
  
  int   colo   = 0;           // variables inputed
  int   lines  = 0;           // lines
  float YBK    = 0;           // Reflectance White
  float YW     = 0;           // Reflectance Black
  
  float Lmax   = 0;           // variables to be computed
  float Lmin   = 10000000;    // arbitrarily high value
  float NBK    = 0;           // will contain a normalized Black value
  float NW     = 1;           // normalized white value
  float CRATIO = 0;           // Black to White dynamic ratio
  float LRATIO = 0;           // Lowest to Highest pixel value dynamic ratio
  
  float Z      = 0;           // processing variables
  float X      = 0;
  float aBK    = 0;           // computed density of black at each pixel
  float vRND   = 0;           // random value container
  float IO     = 0;
  
  int main(){
  
  cout &lt;&lt; "please put a file named wall.csv" &lt;&lt; endl &lt;&lt; "in the same forler as this executable" &lt;&lt; endl &lt;&lt; endl;
  cout &lt;&lt; "how many:" &lt;&lt; endl &lt;&lt; "columns does the CSV has?" &lt;&lt; endl;
  cin  &gt;&gt; colo;
  cout &lt;&lt; "lines   does the CSV has?" &lt;&lt; endl;
  cin  &gt;&gt; lines;
  cout &lt;&lt; "reflectance of the WHITE (CIE Y)?" &lt;&lt; endl;
  cin  &gt;&gt; YW;
  cout &lt;&lt; "reflectance of the BLACK (CIE Y)?" &lt;&lt; endl;
  cin  &gt;&gt; YBK;
  
  NBK    = YBK / YW;      // normalized BK
  CRATIO = NW / NBK;      // correction Ratio
  int C  = lines * colo;  // cells
  
  cout &lt;&lt; endl &lt;&lt; "   there are: "               &lt;&lt; colo   &lt;&lt; "  columns";
  cout &lt;&lt; endl &lt;&lt; "   and      : "               &lt;&lt; lines  &lt;&lt; "  lines " ;
  cout &lt;&lt; endl &lt;&lt; "   that makes "               &lt;&lt; C      &lt;&lt; " cells " &lt;&lt; endl;
  cout &lt;&lt; endl &lt;&lt; "   correction ratio is: " &lt;&lt; CRATIO &lt;&lt; endl &lt;&lt; endl;
  
  ///_____ IMPORT THE PHOTOMETRIC DATA
  
  cout &lt;&lt; "...importing the photometric data" &lt;&lt; endl;
  
  float x = 0;     // a variable that will contain a value from the file
  
      ifstream ifs ("wall.csv");
  
      char dummy;
      for (int i = 0; i &lt; lines; ++i){
          for (int i = 0; i &lt; colo; ++i){
              ifs &gt;&gt; x;
  
              if (x &gt; Lmax) {
                  Lmax = x;     // determines the highest pixel value
              }
              if (x &lt; Lmin) {
                  Lmin = x;     // determines the lowest  pixel value
              }
  
              CSV.push_back(x);
  
              // So the dummy won't eat digits
              if (i &lt; (colo - 1))
                  ifs &gt;&gt; dummy;
      }}
  
      ifstream ifs_close();
      LRATIO = Lmax / Lmin;
  
  cout &lt;&lt; "...photometric data imported"    &lt;&lt; endl;
  cout &lt;&lt; endl &lt;&lt; "   maximum Luminance  is: " &lt;&lt; Lmax;
  cout &lt;&lt; endl &lt;&lt; "   minimum Luminance  is: " &lt;&lt; Lmin &lt;&lt; endl;
  cout &lt;&lt; endl &lt;&lt; "...luminance ratio is: " &lt;&lt; LRATIO;
  
  if (LRATIO &gt; CRATIO) {
      cout &lt;&lt; endl &lt;&lt; "...luminance ratio is: " &lt;&lt; LRATIO;
      cout &lt;&lt; endl &lt;&lt; "...this is too high, ending..." &lt;&lt; '\a';
      return(0);
  }
  
  cout &lt;&lt; endl &lt;&lt; "...luminance can be corrected :)" &lt;&lt; endl;
  
  ///______ CREATE RANDOM VALUES BETWEEN 0 &amp; 1
  
    std::default_random_engine generator;
    std::uniform_real_distribution &lt;double&gt; distribution(0.0,1.0);
  
    for (int i=0; i&lt;C; ++i) {
      double number = distribution(generator);
      RND.push_back(number);
    }
  
  cout &lt;&lt; endl &lt;&lt; "...random values created" &lt;&lt; endl;
  
  
  ///_______ process &amp; export to PBM
  
  ofstream output_file("./wall.pbm");
  
  output_file &lt;&lt; "P1" &lt;&lt; "\n" &lt;&lt; colo &lt;&lt; " " &lt;&lt; lines &lt;&lt; "\n"; /// PBM HEADER
  
  cout &lt;&lt; endl &lt;&lt; "...file header written" &lt;&lt; endl;
  cout &lt;&lt; endl &lt;&lt; "...computing";
  
  int CELLS   = C;  // copy the amount of cells
  int LINEW   = colo;
  int PERCENT = 100;
  
      while (CELLS &gt; 0) {
          while (LINEW &gt; 0) {
  
              Z    = Lmin/CSV.front();        /// processing calculus
              X    = (NBK - Z)/(NBK - NW);
              aBK  = (1 - X);
              vRND = RND.front();
  
  
                              if (aBK &gt; (vRND)) {
                                  IO = 1;
                              }
                              else {
                                  IO = 0;
                              }
  
  
              LINEW   = LINEW - 1;
              CELLS   = CELLS - 1;
              PERCENT = PERCENT - CELLS / C;
  
              output_file &lt;&lt; IO &lt;&lt; "\n";
              //cout &lt;&lt; ERR &lt;&lt; " "; /// fancy...
  
              CSV.erase(CSV.begin());
              RND.erase(RND.begin());
              }
  
          LINEW = colo;
      }
  
  
  cout &lt;&lt; endl &lt;&lt; "...computing done" &lt;&lt; endl;
  cout &lt;&lt; "...file written";
  
  output_file.close();
  return(0);
  }
  </code></pre>