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

MemoryImageSource constructor seems to ignore offset

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.1
    • client-libs
    • 2d
    • merlin
    • generic
    • generic



      Name: rlT66838 Date: 08/04/99


      The wrong image was created from a MemoryImageSource
      because the constructor for the MemoryImageSource seemed to
      ignore the offset provided.

      For the code sample:
      usecolors is a ColorModel
      database is a byte[] that is width*height*depth long
      area = width*height

      This piece of code produces an Image that uses the first

      //Test case
      import java.awt.* ;
      import java.awt.image.* ;
      import java.awt.Color ;
      import java.awt.Graphics ;


      public class ImageBug extends Frame implements ImageObserver, Runnable
      {

        int height = 100 ;
        int width = 100 ;
        int levels = 3 ;
        IndexColorModel cm ;
        Image image ;
        Thread irun ;
        boolean first = true ;

        byte[] db = new byte[ height*width*levels] ;


        public boolean imageUpdate( Image i
                                  , int flags
                                  , int a
                                  , int b
                                  , int c
                                  , int d )
        { return ( true ) ;
        }

        public void createColorModel()
        {
          byte red[] = new byte[256] ;
          byte green[] = new byte[256] ;
          byte blue[] = new byte[256] ;
       
          for ( int i=0 ; i<256 ; i++ )
          { red[i] = (byte)( i & 0xff ) ;
            //green[i] = (byte)( i & 0xff ) ;
            blue[i] = (byte)( i & 0xff ) ;
            //commented out green so I could get purple
          }

          cm = new IndexColorModel( 8, 256, red, green, blue ) ;
        }

        public void generateBuggyImage( int offset )
        { // Initialize the database, Three slices, different shades of grey
          int l = 0 ;
          if ( first )
          { for ( int k=0 ; k<levels ;k++ )
            { for ( int i=0 ; i<height ; i++ )
              { for ( int j=0 ; j<width ; j++ )
                { if( k== 0)
                       db[l] = (byte)( 70 & 0xff ) ;
                    if (k==1)
                       db[l] = (byte)( 150 & 0xff ) ;
                    if (k==2)
                       db[l] = ( byte)( 230 & 0xff ) ;
                    l++ ;
            } } }
            first = false ;
           }
           
           System.out.println("db length="+db.length) ;
           System.out.println("offset= "+offset) ;
           // Here the image is created using the offset,
           image = createImage( new MemoryImageSource( width, height, (ColorModel)cm,
                                                      db, offset, width )) ;
         }

         public void generateForcedImage( int offset )
         {
           int area = width*height ;
           byte[] tempdb = new byte[area] ;

           // using the offset grab just the slice of the db we want to display
           for ( int i=0 ; i<area ; i++ )
              tempdb[i] = db[i+offset] ;

           // Create the image with an offset of zero, image is ok
           image = createImage( new MemoryImageSource( width, height, (ColorModel)cm,
                                                       tempdb, 0, width )) ;
         }

         public void paint( Graphics g )
         {
            g.drawImage( image, 10, 10, this ) ;
         }

         public void run()
         {
           // The thread runs generating six images. The first three attempt to
           // traverse the three slices of the database in turn. The output is
           // as if the offset is always 0 in the MemoryImageSource.
           // The second three cut out only the part of the database we are trying
           // to display. The output is fine.
           try
           { System.out.println("These images are created using the offset");
             for ( int i=0 ; i<3 ; i++ )
             { generateBuggyImage( i*width*height ) ;
               repaint() ;
               Thread.currentThread().sleep( 5000 ) ;
               // five second delay between new images
             }
             System.out.println("These are forcing the db and using offset of zero");
             for ( int i=0 ;i<3 ;i++ )
             { generateForcedImage( i*width*height ) ;
               repaint() ;
               Thread.currentThread().sleep( 5000 ) ;
             }
           }
           catch ( InterruptedException e )
           {}
           System.exit(0) ;
         }
             
         public void showBug()
         { irun = new Thread( this ) ;
           irun.start() ;
         }


         public static void main( String args[] )
         {
           
            ImageBug ib = new ImageBug() ;
            ib.createColorModel() ;
            ib.setSize(100,100) ;
            ib.show() ;
            ib.showBug() ;
         }
      }
      (Review ID: 83512)
      ======================================================================

            tdv Dmitri Trembovetski (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: