title: Creating a bounding box in image with transparency
body: <p>I want to crop an image with one specific color acting like a transparency mask and I would like to create the smallest box that will enclose the image.</p>
  
  <p>A picture of what I mean will explain it better for sure:</p>
  
  <p><a href="https://i.stack.imgur.com/D7bw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D7bw2.png" alt="Images with bounding boxes"></a></p>
  
  <p>The RGB(255,0,255) is the transparent mask and the green frames represent the desired bounding box.</p>
  
  <p>I've already figured out the top line of the bounding box:</p>
  
  <pre><code>    int nAlloc = (128 * 128) * 4;
      unsigned char* buf = new unsigned char[nAlloc];
      GetBitmapBits(hBMP, nAlloc, buf);
      for(int i = 0; i &lt; nAlloc; i += 4)
      {
          if(buf[i] == 255 &amp;&amp; buf[i + 1] == 0 &amp;&amp; buf[i + 2] == 255)
          {
              continue;
          }
          // If I hit a first non-transparent pixel, 
          // I can then calculate the row where is that pixel located.
          else if(set_pixel == false)
          {
              set_pixel = true;
              index = ceil((float)(i / 4.0 / 128.0));
          }
          ... // Converting non-transparent pixels to Black&amp;White
      }
  
      //I'm then drawing the bitmap to window like so:
      TransparentBlt(hdc, 5, 305 - index, 128, 128, hDC, 0, 0, 128, 128, RGB(255, 0, 255));
  </code></pre>
  
  <p>I think, I know how to determine the last row as well but I'm not sure with that and have no idea how to find out the sides of the bounding box.</p>