Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally one would iterate over the image array from top to bottom over rows, and within each row from left to right over the columns. In this case you want the reverse: we want to iterate over each column, beginning at the left, and within the column we go over all rows and check if a black pixel is present.</p> <p>This will give you the left-most black pixel:</p> <pre><code>size_t maxIndex = height * bytesPerRow; for (size_t x = 0; x &lt; bytesPerRow; x += bytesPerPixel) { for (size_t index = x; index &lt; maxIndex; index += bytesPerRow) { if (rawData[index + 3] &gt; 0) { goto exitLoop; } } } exitLoop: if (x &lt; bytesPerRow) { x /= bytesPerPixel; // left most column is `x` } </code></pre> <p>Well, this is equal to mattjgalloway, just slightly optimized, and neater too :O</p> <p>Although a <code>goto</code> is usually permitted to abandon two loops from within the inner loop, it's still ugly. Makes me really miss those nifty flow control statements D has...</p> <p>The function you provided in the example code does something different though. It starts at a certain position in the image (defined by <code>xx</code> and <code>yy</code>), and goes over <code>count</code> pixels going from the starting position to the right, continuing to next rows. It adds those alpha values to some array I suspect.</p> <p>When passed <code>xx = yy = 0</code>, this will find the top-most pixel with certain conditions, not the left-most. This transformation is given by the code above. Do remind that a 2D image is simply a 1D array in memory, starting with the top row from left to right and proceeding with the next rows. Doing simple math one can iterate over rows or over columns.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload