Data buffers created with an offset are unusable

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P4
    • 26
    • Affects Version/s: 1.4.0, 1.4.1, 1.4.2, 21, 23
    • Component/s: client-libs
    • 2d
    • b23
    • generic
    • generic



      Name: pzR10082 Date: 11/14/2003


      The class DataBufferByte has constructor DataBufferByte(byte[],int,int)
      that allows for offset into the byte array to be specified. However,
      data buffers created using this constructor don't work properly. When
      used to create an Image, they behave as if offset is 0.

      The code below creates a 1x1 image using byte buffer { 0, -1, 0, 0 }.
      Since i specify an offset of 1, the only pixel should be red. However,
      it is green.

      ---------- BEGIN SOURCE CODE ----------
      import javax.swing.*;
      import java.awt.image.*;
      import java.awt.color.ColorSpace;
      import java.awt.*;

      public class Pix
      {
          public static void main(String[] args) {
              byte[] data = { 0, -1, 0, 0 };
              int[] bandOffsets = { 0, 1, 2 };
              DataBuffer databuf = new DataBufferByte(data, 3, 1);
              WritableRaster raster = Raster.createInterleavedRaster(databuf,
                      1, 1, 3, 3, bandOffsets, null);
              ColorModel colorModel = new ComponentColorModel(
                      ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                      ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
              BufferedImage img = new BufferedImage(colorModel, raster,
                      false, null);
              Icon icon = new Iconie(img);
              
              // show it
              JLabel l = new JLabel(icon);
              JFrame f = new JFrame();
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.getContentPane().add(l);
              f.pack();
              f.show();
          }
          
          static class Iconie implements Icon {
              Image img;
              final int W = 40;
              final int H = 40;
              
              public Iconie(Image img) {
                  this.img = img;
              }
              
              public void paintIcon(Component c, Graphics g, int x, int y) {
                  for (int i=0; i<W; i++) {
                      for (int j=0; j<H; j++) {
                          g.drawImage(img, i, j, c);
                      }
                  }
              }

              public int getIconWidth() {
                  return W;
              }

              public int getIconHeight() {
                  return H;
              }
          }
      }
      ----------- END SOURCE CODE -----------

      ======================================================================

            Assignee:
            Philip Race
            Reporter:
            Peter Zhelezniakov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: