Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4293263

BufferedImage.setRGB()/getRGB() fails for TYPE_BYTE_BINARY

    XMLWordPrintable

Details

    • 2d
    • kestrel
    • generic, x86
    • generic, windows_nt

    Description

      Name: mc57594 Date: 11/21/99

      Reproduced on Solaris 2.7 with jdk1.2.2
      [chamness]
      =-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      java version "1.2.2"

      Classic VM (build JDK-1.2.2-W, green threads, sunwjit)
      If I execute a setRGB(int) and then a getRGB() on a BufferedImage that was
      constructed as TYPE_BYTE_BINARY, I get inconsistent results.

      Here is some code that shows the problem:
      import java.awt.image.*;
      import java.awt.*;

      public class BinaryTest {
        public static void main(String arg[]) {
          BufferedImage binaryImage = new BufferedImage(2, 2,
      BufferedImage.TYPE_BYTE_BINARY);
          int white = Color.white.getRGB();
          int black = Color.black.getRGB();
          System.out.println("White: "+white+" Black: "+black);
          binaryImage.setRGB(0,0,white);
          binaryImage.setRGB(0,1,black);
          binaryImage.setRGB(1,0,black);
          binaryImage.setRGB(1,1,white);
          System.out.println("Pixel values: "+binaryImage.getRGB(0,0)+", "+
      binaryImage.getRGB(0,1)+", "+
      binaryImage.getRGB(1,0)+", "+
      binaryImage.getRGB(1,1));
        }
      }

      If I construct the BufferedImage with TYPE_BYTE_GRAY, or TYPE_INT_RGB I get:
      Pixel values: -1, -16777216, -16777216, -1

      However, if I construct the image with TYPE_BYTE_BINARY, I get:
      Pixel values: -1, -16777216, -1, -16777216
      which is not consistent with the way the pixels were set.
      (Review ID: 97325)
      ======================================================================

      Name: mc57594 Date: 12/05/99


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      I'm trying to do a quick-and-dirty WBMP encoding from a BufferedImage. The
      getRGB(int x,int y) method seems to malfunction when the image type is
      TYPE_BYTE_BINARY.

      Here is the source code :

      <PRE>
      /*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*/

      package com.ubicco.wap;

      import java.io.*;
      import java.awt.*;
      import java.awt.image.*;

      public class WapBufferedImage extends BufferedImage {
      private static final IndexColorModel COLOR_MODEL=new IndexColorModel
      (1, 2, new byte[] {
      (byte)0, (byte)255
      }, new byte[] {
      (byte)0, (byte)255
      }, new byte[] {
      (byte)0, (byte)255
      });

      public WapBufferedImage(int widthP, int heightP) {

      /*
      * super(COLOR_MODEL,
      * Raster.createPackedRaster(DataBuffer.TYPE_BYTE, widthP,
      heightP, 1, 1, new Point(0, 0)),
      * false, new java.util.Properties());
      */
      super(widthP, heightP, BufferedImage.TYPE_BYTE_BINARY);
      }

      public byte[] wapEncode() throws IOException {
      ByteArrayOutputStream baosP=new ByteArrayOutputStream();
      int
      wP=getWidth(), hP=getHeight();
      int iP, jP;

      baosP.write(multiByteEncode(0));
      baosP.write(0);
      baosP.write(multiByteEncode(wP));
      baosP.write(multiByteEncode(hP));

      for(iP=0;iP<hP;iP++) {
      int bP=0;

      for(jP=0;jP<wP;jP++) {
      boolean onP=(getRGB(iP, jP)&0xFFFFFF)!=0;

      System.out.print(onP ? "X" : ".");

      if(onP) {
      bP|=1;
      }

      if(jP%8==7) {
      baosP.write(bP);

      bP=0;
      }
      else {
      bP<<=1;
      }
      }

      if(jP%8!=7) {
      bP<<=(7-jP%8);
      baosP.write(bP);
      }

      System.out.println();
      }

      baosP.close();

      return baosP.toByteArray();
      }

      private static byte[] multiByteEncode(int valueP) {
      if(valueP==0) {
      return new byte[] {
      (byte)0
      };
      }

      ByteArrayOutputStream baosP=new ByteArrayOutputStream();
      int
      maskP=0x7F;
      boolean firstP=true;

      while(valueP>0) {
      int digitP=valueP&maskP;

      if(firstP) {
      firstP=false;
      }
      else {
      digitP|=0x80;
      }

      baosP.write(digitP);

      valueP>>=7;
      }

      byte[] reversedP=baosP.toByteArray();
      int iP, lP=reversedP.length;
      byte[] resultP=new byte[lP];

      for(iP=0;iP<lP;iP++) {
      resultP[iP]=reversedP[lP-1-iP];
      }

      return resultP;
      }

      public static void main(String[] argsP) throws IOException {
      Frame frameP=new Frame
      ("Test");
      WapBufferedImage imageP=new WapBufferedImage(25, 25);
      Graphics gP=imageP.getGraphics();

      gP.drawOval(0, 0, 20, 20);
      gP.drawLine(0, 0, 20, 20);

      FileOutputStream fosP=new FileOutputStream("cloudy.wbmp");

      fosP.write(imageP.wapEncode());
      fosP.close();
      frameP.add(BorderLayout.CENTER,
      new javax.swing.JLabel("Hello,
      world !", new javax.swing.ImageIcon(imageP),

      javax.swing.SwingConstants.CENTER));
      frameP.pack();
      frameP.setVisible(true);
      }

      }

      /*--- formatting done in "Fi System" style on 11-24-1999 ---*/
      </PRE>

      Try switching from TYPE_BYTE_BINARY to TYPE_BYTE_GRAY on line 25 and you'll see
      that the diagonal line does not look the same. In fact the coordinate
      resolution in TYPE_BYTE_BINARY seems to be wrong.
      (Review ID: 98243)
      ======================================================================

      Attachments

        Activity

          People

            jehung Jeannette Hung
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: