Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I get you right, then if you have an image like this</p> <pre><code>p00|p01|p02|... ---+---+------- p10|p11|p12|... ---+---+------- p20|p21|p22|... ---+---+---+--- ...|...|...|... </code></pre> <p>Which is stored in an array in left-to-right row scan like this:</p> <pre><code>p00,p01,...,p0n, p10,p11,...,p1n, p20,p21, ... </code></pre> <p>If I understand you correctly, what you want to be able to do, is to take a given rectangle (from a certain x and y with a certain width and height) from the image. Here is code to do this, with explanations:</p> <pre><code>byte[] crop_area (byte[] source_image, int image_width, int image_height, int start_x, int start_y, int result_width, int result_height) { byte[] result = new byte[result_width * result_height]; int endX = x + result_width; int endY = y + result_height; int pos = 0; for (int y = startY; y &lt; endY; y++) for (int x = startX; x &lt; endX; x++) { /* To get to the pixel in the row I (starting from I=1), we need * to skip I-1 rows. Since our y indexes start from row 0 (not 1), * then we don't need to subtract 1. * * So, the offset of the pixel at (x,y) is: * * y * image_width + x * |-----------------------| |-----------------| * Skip pixels of y rows Offset inside row */ result[pos] = source_image[y * image_width + x]; /* Advance to the next pixel in the result image */ pos++; } return result; } </code></pre> <p>Then, to take the block in the row I and column J (I,J=0,...,14) do:</p> <pre><code>crop_area (source_image, image_width, image_height, J*image_width/15, I*image_height/15, image_width/15, image_height/15) </code></pre>
 

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