title: Creating a custom BufferedImage
body: <p>I'm writing a method that takes three 2D int arrays (representing R, G, and B values of an image), a width and height, and a boolean denoting if the specified image is color or black and white. What I want the method to do is create a BufferedImage from the given input data.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>public static BufferedImage createImage(int[][] r, int[][] g, int[][] b,
      int width, int height, boolean isColor) {
  
      BufferedImage ret;
      if (isColor)
          ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      else
          ret = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
      WritableRaster raster = ret.getRaster();
  
      for (int i = 0; i &lt; height; i++) {
          for (int j = 0; j &lt; width; j++) {
              if (isColor) {
                  raster.setSample(j, i, 0, r[i][j]);
                  raster.setSample(j, i, 1, g[i][j]);
                  raster.setSample(j, i, 2, b[i][j]);
              }
              else
                  raster.setSample(j, i, 0, r[i][j]);
          }
      }
  
      return ret;
  }
  </code></pre>
  
  <p>However, I wrote a unit test which should create a 5x5 colored image filled with white (255, 255, 255), but the raster data is filled with extremely large values and not 255. I'm not too familiar with this image stuff and AWT in general, so I feel like I'm not creating the BufferedImage correctly. I've been trying to reference the Java API for BufferedImage, but haven't had much luck figuring it out. Any help would be appreciated.</p>