Re: regarding 2d image and boundary

Ramesh-

Mantri, Mr. Ramesh wrote:
All,
   Please find appended a program which is a modification to an example program 
from the tutorial
on VisAD. Essential it draws iso-contours for temperature values and fills 
these contours. Most
experts will find it a trivial program.
   My question is this. The image generated is not filling up the screen i.e., 
there seems to be a padded
area drawn around the actual image. How can I eliminate this "border" so that 
the required image fills
up the entire space of (NumXPixels * NumYPixels)? Appreciate your help.
sincerely,
Ramesh Mantri

In your program, you have the following lines:

      ContourControl isoControl = (ContourControl) tempIsoMap.getControl();
      float[] levels = {295.0f, 305.0f, 500.0f};
      isoControl.setLevels(levels, 294.0f, false);
      isoControl.setContourFill(true);

the setLevels with a float array specifies the contour lines that
should be displayed.  Since you have values less than 295, any
values below the lowest contour are not filled.  If you change to:

      float[] levels = {200.0f, 295.0f, 305.0f, 500.0f};

the entire image is filled.  The lower value (200) does not really
matter, as long as it is below the lowest data value.

Also, the 294.0f is ignored since you are specifying the exact
levels that should be contoured.

Also, there was a missing closing brace in the code you sent:

       if (NROWS != NCOLS) {
          boolean Xmax = (NCOLS > NROWS);
          if (Xmax) {
             NumYPixels = (int) ((NumXPixels * NROWS) / NCOLS);
          } else {
             NumXPixels = (int) ((NumYPixels * NCOLS) / NROWS);
       }

should be:

       if (NROWS != NCOLS) {
          boolean Xmax = (NCOLS > NROWS);
          if (Xmax) {
             NumYPixels = (int) ((NumXPixels * NROWS) / NCOLS);
          } else {
             NumXPixels = (int) ((NumYPixels * NCOLS) / NROWS);
          }
       }

Good luck.

Don


// ===========================================================================
import visad.*;
import visad.java2d.DisplayImplJ2D;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Image2D {
   private RealType longitude, latitude;
   private RealType temperature;

   private RealTupleType domain_tuple;

   private FunctionType func_domain_range;

   private Set domain_set;

   private FlatField vals_ff;

   private DataReferenceImpl data_ref;

   private DisplayImpl display;
   private ScalarMap latMap, lonMap;
   private ScalarMap tempIsoMap, tempRGBMap;

   public Image2D(float[] data, int NCOLS, int NROWS) throws Exception {
      latitude = RealType.getRealType("latitude");
      longitude = RealType.getRealType("longitude");

      domain_tuple = new RealTupleType(latitude, longitude);

      temperature = RealType.getRealType("temperature");

      func_domain_range = new FunctionType( domain_tuple, temperature);

      domain_set = new Linear2DSet(domain_tuple, 1,NROWS,NROWS, 1,NCOLS,NCOLS);

      float[][] set_samples = domain_set.getSamples(true);

      float[][] flat_samples = new float[1][NCOLS * NROWS];

      for (int dataIndex=0; dataIndex < data.length; dataIndex++) {
         int dataCol = (dataIndex % NCOLS);
         int dataRow = (dataIndex / NCOLS);

         int sampleIndex = ((dataCol * NROWS) + dataRow);

         flat_samples[0][sampleIndex] = data[dataIndex];
      }

      vals_ff = new FlatField( func_domain_range, domain_set);
      vals_ff.setSamples( flat_samples , false );

      int NumXPixels = 450;
      int NumYPixels = 450;

      if (NROWS != NCOLS) {
         boolean Xmax = (NCOLS > NROWS);
         if (Xmax) {
            NumYPixels = (int) ((NumXPixels * NROWS) / NCOLS);
         } else {
            NumXPixels = (int) ((NumYPixels * NCOLS) / NROWS);
      }

      display = new DisplayImplJ2D("display1", NumXPixels, NumYPixels);

      latMap = new ScalarMap( latitude,    Display.YAxis );
      lonMap = new ScalarMap( longitude, Display.XAxis );
      tempIsoMap = new ScalarMap( temperature,  Display.IsoContour );
      tempRGBMap = new ScalarMap( temperature,  Display.RGB );

      display.addMap( latMap );
      display.addMap( lonMap );
      display.addMap( tempIsoMap );
      display.addMap( tempRGBMap );

      ContourControl isoControl = (ContourControl) tempIsoMap.getControl();
      float[] levels = {295.0f, 305.0f, 500.0f};
      isoControl.setLevels(levels, 294.0f, false);
      isoControl.setContourFill(true);

      data_ref = new DataReferenceImpl("data_ref");
      data_ref.setData( vals_ff );

      display.addReference( data_ref );

      double ar = ((double) NumYPixels) / ((double) NumXPixels);
      ProjectionControl pc = display.getProjectionControl();
      pc.setAspect(new double[] {1.0, ar});
   }

   public void writeToFile() throws IOException {
      BufferedImage image = display.getImage(true);
      File file = new File("iso_contour_image.png");
      ImageIO.write(image, "PNG", file);
   }

   public static void main(String[] args) {
      try {
         float[] data = {
                           318.0f, 312.0f, 297.0f, 299.0f, 302.0f,
                           315.0f, 311.0f, 296.0f, 297.0f, 299.0f,
                           306.0f, 298.0f, 297.0f, 294.0f, 294.0f,
                           297.0f, 296.0f, 294.0f, 291.0f, 294.0f,
                           297.0f, 291.0f, 293.0f, 287.0f, 285.0f,
                           297.0f, 296.0f, 296.0f, 291.0f, 291.0f
         };

         Image2D imgObj = new Image2D(data, 5, 6);
         imgObj.writeToFile();

         System.out.println("Image written to file");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
// ===========================================================================

--
*************************************************************
Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307
http://www.unidata.ucar.edu/staff/donm
*************************************************************



  • 2003 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: