title: Is there a way to compare the RGB values of a 1024x1024 bitmap with the values from an input file with only 1,048,576 comparisons?
body: <p>What I did was:</p>
  <p>Open an input file (txt) with the RGB values of 64 color entries.</p>
  <p>Open another input file that is a 1024x1024 bitmap with color values (3 bytes per pixel).</p>
  <p>Read from the txt file and allocate the values from that into an array of structures.</p>
  <p>The array of structures holds R, G, B values and the name of the color in question.
  Next thing that I did was read the bitmap for RGB values and compare them to the values in the struct.</p>
  <p>I created a for loop with 1,048,576 as the limit and another for loop inside with 64 as the limit to compare the values. Problem is the count of comparisons will result in 67,108,864.
  Theoretical minimum is 1,048,576.</p>
  <p>This is what I have so far:</p>
  <pre><code> for (int i = 0;!bitmap.eof(); i += 3)
  {
      unsigned char red, green, blue;
      bitmap.read((char*)&amp;blue, sizeof(unsigned char));
      bitmap.read((char*)&amp;green, sizeof(unsigned char));
      bitmap.read((char*)&amp;red, sizeof(unsigned char));
      for (int j = 0; j &lt; 64; j++)
      {
          if (int(blue) == bmiColors[j].b &amp;&amp; int(green) == bmiColors[j].g &amp;&amp; int(red) == bmiColors[j].r)
          {
              //cout &lt;&lt; &quot;B: &quot; &lt;&lt; int(b) &lt;&lt; &quot; G: &quot; &lt;&lt; int(g) &lt;&lt; &quot; R: &quot; &lt;&lt; int(r) &lt;&lt; endl;
                  //cout &lt;&lt; &quot;Position: &quot; &lt;&lt; bitmap.tellg() &lt;&lt; endl;
              bmiColors[j].matchCount += 1;
              count++;
          }
          else
          {
              bmiColors[j].skipCount += 1;
              count++;
          }
      }
  }
                             
  </code></pre>
  <p>How would you go about optimizing it? The hint that I got was to use the pixel color as the index into a table. Please help me out.</p>