code: public static byte[] renderedImage2ByteArray( RenderedImage renderedImage, boolean doRowsThenCols ) {
          int width = renderedImage.getWidth();
          int height = renderedImage.getHeight();
  
          byte[] values = new byte[width * height];
          RandomIter imageIter = RandomIterFactory.create(renderedImage, null);
          int index = 0;
          if (doRowsThenCols) {
              for( int y = 0; y < height; y++ ) {
                  for( int x = 0; x < width; x++ ) {
                      double sample = imageIter.getSampleDouble(x, y, 0);
                      values[index++] = (byte) sample;
                  }
              }
          } else {
              for( int x = 0; x < width; x++ ) {
                  for( int y = 0; y < height; y++ ) {
                      double sample = imageIter.getSampleDouble(x, y, 0);
                      values[index++] = (byte) sample;
                  }
              }
          }
          imageIter.done();
          return values;
      }
docs: Transform a double values rendered image in its byte array.
  
  <p>No check is done if the double value fits in a byte.</p>
  
  @param renderedImage the rendered image to transform.
  @param doRowsThenCols if <code>true</code>, rows are processed in the outer loop.
  @return the array holding the data.