title: Group 8x8 blocks using for loops in Java
body: <p>I am trying to group 8x8 blocks of pixel data that I retrieve from BufferedImage.</p>
  
  <ol>
  <li><code>jpegPixels</code> is a 3D int array with height, width, and an RGB size (0=Red, 1=Green, 2=Blue).</li>
  <li><code>pixelMatrix</code> is a 2D int array with 8x8 block size</li>
  </ol>
  
  <p>So far the code works because I get a resultant image back out. Below is a partial sample of the code that groups pixels into 8x8 (it only shows 2 rows because code is too long).</p>
  
  <pre><code>int [][][] jpegPixels = new int[h][w][3]; //h = height size, w = width size
  int [][] pixelMatrix = new int [8][8];
  
  for(int i=6; i&lt;jpegPixels.length-7; i++)
    {
       for(int j=6; j&lt;jpegPixels[0].length-7; j++)
       {
         for(int x=0; x&lt;2; x++)
         {  
          //************  ROW 1  ******************
          //********** Pixel 1 ********************
             pixelMatrix[0][0] = jpegPixels[i-1][j-1][x];
  
             //********** Pixel 2 ********************
             pixelMatrix[0][1] = jpegPixels[i-1][j][x];          
  
          //********** Pixel 3 ********************
             pixelMatrix[0][2] = jpegPixels[i-1][j+1][x];
  
          //********** Pixel 4 ********************
             pixelMatrix[0][3] = jpegPixels[i-1][j+2][x];
  
             //********** Pixel 5 ********************
             pixelMatrix[0][4] = jpegPixels[i-1][j+3][x];          
  
             //********** Pixel 6 ********************
             pixelMatrix[0][5] = jpegPixels[i-1][j+4][x];          
  
          //********** Pixel 7 ********************
             pixelMatrix[0][6] = jpegPixels[i-1][j+5][x];
  
          //********** Pixel 8 ********************
             pixelMatrix[0][7] = jpegPixels[i-1][j+6][x];
  
          //************  ROW 2  ******************
          //********** Pixel 9 ********************
             pixelMatrix[1][0] = jpegPixels[i][j-1][x];
  
             //********** Pixel 10 ********************
             pixelMatrix[1][1] = jpegPixels[i][j][x];
  
          //********** Pixel 11 ********************
             pixelMatrix[1][2] = jpegPixels[i][j+1][x];
  
             //********** Pixel 12 ********************
             pixelMatrix[1][3] = jpegPixels[i][j+2][x];
  
          //********** Pixel 13 ********************
             pixelMatrix[1][4] = jpegPixels[i][j+3][x];
  
             //********** Pixel 14 ********************
             pixelMatrix[1][5] = jpegPixels[i][j+4][x];
  
          //********** Pixel 15 ********************
             pixelMatrix[1][6] = jpegPixels[i][j+5][x];
  
             //********** Pixel 16 ********************
             pixelMatrix[1][7] = jpegPixels[i][j+6][x];
  
                 //********** Pixel 17 - 64 ********************
                 // ******* Too long to print the rest ********
            }
        }
   }
  </code></pre>
  
  <p>Can someone identify is this the correct way of grouping values into 8x8 blocks of a 3D array of h x w size? The reason is, is that when I  test print out values of the original pixels and resultant pixels, I am getting values from the resultant pixels from a higher ofset? </p>
  
  <p>For e.g.: <em>Original pixels</em> - 2234445556788, <em>Resultant pixels</em> - 0000000002234445556788</p>
  
  <p>Hope this is clear.
  Thanks</p>