title: BufferedImage from 4-bit data buffer
body: <p>Hello I have a problem with converting my 4-bit data buffer to WritableRaster.
  Image resolution: 1024x768 (786432)  </p>
  
  <p>Here is description what I'm doing.</p>
  
  <p>1) Create 4-bit BufferedImage</p>
  
  <pre><code>bit4Image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
  Graphics graphics = bit4Image.getGraphics();
  graphics.drawImage(originalImage, 0, 0, null);
  graphics.dispose();
  //4-bit BufferedImage created. 4-bit BufferedImage is properly made cause it can be saved to hdd and looks good
  </code></pre>
  
  <p>2) Get byte array from DataBuffer from 4-bit</p>
  
  <pre><code>byte[] pixelData = ((DataBufferByte)  bit4Image.getRaster().getDataBuffer()).getData();
  // pixelData length is 393216
  </code></pre>
  
  <p>3) Now I want to create BufferedImage from this byte array pixelData</p>
  
  <pre><code>BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
  DataBufferByte buffer = new DataBufferByte(pixelData, pixelData.length);
  WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, width, 1, new int[]{0}, new Point(0, 0));
  dest.setData(raster);
  </code></pre>
  
  <p>Problem is when I call <code>Raster.createInterleavedRaster</code>.  Exception:
  <strong><code>java.awt.image.RasterFormatException: Data array too small (should be &gt; 786431 )</code></strong></p>
  
  <p>I also tried something like this  </p>
  
  <pre><code>BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY, MY_BIT_4_COLOR_MODEL);
  dest.getRaster().setDataElements(0, 0, width, height, pixelData);
  </code></pre>
  
  <p>But this one gives me similar failure:<br>
    <strong><code>java.lang.ArrayIndexOutOfBoundsException: 393216</code></strong></p>
  
  <p>Could someone give me a hint or show the proper way of setting this 4-bit pixelData to WritableRaster?</p>