Java Code Examples for java.awt.RenderingHints#VALUE_INTERPOLATION_NEAREST_NEIGHBOR

The following examples show how to use java.awt.RenderingHints#VALUE_INTERPOLATION_NEAREST_NEIGHBOR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphicsBox.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public GraphicsBox(BufferedImage image, float width, float height, float size, int interpolation) {
    this.image = image;
    this.width = width;
    this.height = height;
    this.scl = 1 / size;
    depth = 0;
    shift = 0;
    switch (interpolation) {
    case BILINEAR :
        interp = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
        break;
    case NEAREST_NEIGHBOR :
        interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
        break;
    case BICUBIC :
        interp = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
        break;
    default :
        interp = null;
    }
}
 
Example 2
Source File: RPlotPanel.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
    super.paint(g);
    
    int w = getWidth();
    int h = getHeight();
    
    if (renderingQuality != null && g instanceof Graphics2D) {
        Object interpolation = renderingQuality ? RenderingHints.VALUE_INTERPOLATION_BICUBIC :
                                                  RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation);
    }
    
    Image img = offscreenImage; // not synchronized, createPlotImage() called from worker thread
    if (img != null && w > 0 && h > 0) g.drawImage(img, 0, 0, w, h, null);
}
 
Example 3
Source File: ImageContext.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * Return the interpolation rendering hint to use. This will not return
 * null. If this is undefined, then the antialiasing and rendering hints are
 * consulted (which will return either NEAREST_NEIGHBOR or BILINEAR). If
 * nothing is defined then this returns BILINEAR.
 */
protected Object getInterpolationRenderingHint() {
	Object v = renderingHints.get(RenderingHints.KEY_INTERPOLATION);
	if (v != null)
		return v;

	v = renderingHints.get(RenderingHints.KEY_ANTIALIASING);
	if (RenderingHints.VALUE_ANTIALIAS_ON.equals(v)) {
		return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
	} else if (RenderingHints.VALUE_ANTIALIAS_OFF.equals(v)) {
		return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
	}

	v = renderingHints.get(RenderingHints.KEY_RENDERING);
	if (RenderingHints.VALUE_RENDER_QUALITY.equals(v)) {
		return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
	} else if (RenderingHints.VALUE_RENDER_SPEED.equals(v)) {
		return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
	}

	// nothing is defined:
	return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
}
 
Example 4
Source File: ImageUtils.java    From bladecoder-adventure-engine with Apache License 2.0 6 votes vote down vote up
public static void scaleImageFile(File org, File dest, float scale) throws IOException {
	BufferedImage destImg = null;
	BufferedImage orgImg = ImageIO.read(org);

	if (orgImg != null) {
		Object interpolation;

		if (orgImg.getWidth() < 20) {
			interpolation = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
		} else {
			interpolation = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
		}

		destImg = scaleImage(Math.max(1, (int) (orgImg.getWidth() * scale)),
				Math.max(1, (int) (orgImg.getHeight() * scale)), orgImg, interpolation);
		ImageIO.write(destImg, org.getName().substring(org.getName().lastIndexOf('.') + 1), dest);
	}
}
 
Example 5
Source File: ImageShape.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set interpolation string
 * @param value Interpolation string
 */
public void setInterpolation(String value){
    switch (value){
        case "nearest":
            this.interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
            break;
        case "bilinear":
            this.interp = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
            break;
        case "bicubic":
            this.interp = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
            break;
    }
}
 
Example 6
Source File: RasterLayer.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set interpolation
 *
 * @param value Interpolation
 */
public void setInterpolation(String value) {
    switch (value) {
        case "nearest":
            interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
            break;
        case "bilinear":
            interp = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
            break;
        case "bicubic":
            interp = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
            break;
    }
}
 
Example 7
Source File: ImageLayer.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 */
public ImageLayer() {
    super();
    this.setLayerType(LayerTypes.ImageLayer);
    this.setShapeType(ShapeTypes.Image);
    _isSetTransColor = false;
    _transparencyColor = Color.black;
    this.interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
}
 
Example 8
Source File: ImageLayer.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set interpolation string
 *
 * @param value Interpolation string
 */
public void setInterpolation(String value) {
    switch (value) {
        case "nearest":
            this.interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
            break;
        case "bilinear":
            this.interp = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
            break;
        case "bicubic":
            this.interp = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
            break;
    }
}
 
Example 9
Source File: ImageLayer.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set interpolation
 *
 * @param value Interpolation
 */
public void setInterpolation(String value) {
    switch (value) {
        case "nearest":
            interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
            break;
        case "bilinear":
            interp = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
            break;
        case "bicubic":
            interp = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
            break;
    }
}
 
Example 10
Source File: AffineTransformOp.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an {@code AffineTransformOp} given an affine transform.
 * The interpolation type is determined from the
 * {@code RenderingHints} object.  If the interpolation hint is
 * defined, it will be used. Otherwise, if the rendering quality hint is
 * defined, the interpolation type is determined from its value.  If no
 * hints are specified ({@code hints} is null),
 * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR
 * TYPE_NEAREST_NEIGHBOR}.
 *
 * @param xform The {@code AffineTransform} to use for the
 * operation.
 *
 * @param hints The {@code RenderingHints} object used to specify
 * the interpolation type for the operation.
 *
 * @throws ImagingOpException if the transform is non-invertible.
 * @see java.awt.RenderingHints#KEY_INTERPOLATION
 * @see java.awt.RenderingHints#KEY_RENDERING
 */
public AffineTransformOp(AffineTransform xform, RenderingHints hints){
    validateTransform(xform);
    this.xform = (AffineTransform) xform.clone();
    this.hints = hints;

    if (hints != null) {
        Object value = hints.get(RenderingHints.KEY_INTERPOLATION);
        if (value == null) {
            value = hints.get(RenderingHints.KEY_RENDERING);
            if (value == RenderingHints.VALUE_RENDER_SPEED) {
                interpolationType = TYPE_NEAREST_NEIGHBOR;
            }
            else if (value == RenderingHints.VALUE_RENDER_QUALITY) {
                interpolationType = TYPE_BILINEAR;
            }
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
            interpolationType = TYPE_NEAREST_NEIGHBOR;
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_BILINEAR) {
            interpolationType = TYPE_BILINEAR;
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_BICUBIC) {
            interpolationType = TYPE_BICUBIC;
        }
    }
    else {
        interpolationType = TYPE_NEAREST_NEIGHBOR;
    }
}
 
Example 11
Source File: AffineTransformOp.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an {@code AffineTransformOp} given an affine transform.
 * The interpolation type is determined from the
 * {@code RenderingHints} object.  If the interpolation hint is
 * defined, it will be used. Otherwise, if the rendering quality hint is
 * defined, the interpolation type is determined from its value.  If no
 * hints are specified ({@code hints} is null),
 * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR
 * TYPE_NEAREST_NEIGHBOR}.
 *
 * @param xform The {@code AffineTransform} to use for the
 * operation.
 *
 * @param hints The {@code RenderingHints} object used to specify
 * the interpolation type for the operation.
 *
 * @throws ImagingOpException if the transform is non-invertible.
 * @see java.awt.RenderingHints#KEY_INTERPOLATION
 * @see java.awt.RenderingHints#KEY_RENDERING
 */
public AffineTransformOp(AffineTransform xform, RenderingHints hints){
    validateTransform(xform);
    this.xform = (AffineTransform) xform.clone();
    this.hints = hints;

    if (hints != null) {
        Object value = hints.get(RenderingHints.KEY_INTERPOLATION);
        if (value == null) {
            value = hints.get(RenderingHints.KEY_RENDERING);
            if (value == RenderingHints.VALUE_RENDER_SPEED) {
                interpolationType = TYPE_NEAREST_NEIGHBOR;
            }
            else if (value == RenderingHints.VALUE_RENDER_QUALITY) {
                interpolationType = TYPE_BILINEAR;
            }
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
            interpolationType = TYPE_NEAREST_NEIGHBOR;
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_BILINEAR) {
            interpolationType = TYPE_BILINEAR;
        }
        else if (value == RenderingHints.VALUE_INTERPOLATION_BICUBIC) {
            interpolationType = TYPE_BICUBIC;
        }
    }
    else {
        interpolationType = TYPE_NEAREST_NEIGHBOR;
    }
}
 
Example 12
Source File: ImageDrawer.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
@CalledOnlyBy(AmidstThread.EDT)
private Object getRenderingHint(Graphics2D g2d) {
	if (g2d.getTransform().getScaleX() < 1.0f) {
		return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
	} else {
		return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
	}
}
 
Example 13
Source File: ImageDrawer.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
@CalledOnlyBy(AmidstThread.EDT)
private Object getRenderingHint(Graphics2D g2d) {
	if (g2d.getTransform().getScaleX() < 1.0f) {
		return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
	} else {
		return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
	}
}
 
Example 14
Source File: ImageScaling.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
public ImageScaling(String scalingTypeParameter, String scalingSizeParameter) {
	higherQuality = false;
	targetSize = 1024 * 768;
	if (scalingSizeParameter != null) {
		targetSize = Integer.parseInt(scalingSizeParameter);
	}
	hint = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
	if (scalingTypeParameter.equals("nn")) {
		hint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
	}
}
 
Example 15
Source File: ScaledImageIcon.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
@Override
	public void paintIcon( Component c, Graphics g, int x, int y ) {
/*debug
		g.setColor( Color.red );
		g.drawRect( x, y, getIconWidth() - 1, getIconHeight() - 1 );
debug*/

		// scale factor
		double systemScaleFactor = UIScale.getSystemScaleFactor( (Graphics2D) g );
		float userScaleFactor = UIScale.getUserScaleFactor();
		double scaleFactor = systemScaleFactor * userScaleFactor;

		// paint input image icon if not necessary to scale
		if( scaleFactor == 1 && iconWidth == imageIcon.getIconWidth() && iconHeight == imageIcon.getIconHeight() ) {
			imageIcon.paintIcon( c, g, x, y );
			return;
		}

		// paint cached scaled icon
		if( systemScaleFactor == lastSystemScaleFactor &&
			userScaleFactor == lastUserScaleFactor &&
			lastImage != null )
		{
			paintLastImage( g, x, y );
			return;
		}

		// destination image size
		int destImageWidth = (int) Math.round( iconWidth * scaleFactor );
		int destImageHeight = (int) Math.round( iconHeight * scaleFactor );

		// get resolution variant of image if it is a multi-resolution image
		Image image = getResolutionVariant( destImageWidth, destImageHeight );

		// size of image
		int imageWidth = image.getWidth( null );
		int imageHeight = image.getHeight( null );

		// scale image if necessary to destination size
		if( imageWidth != destImageWidth || imageHeight != destImageHeight ) {
			// determine scaling method; default is "quality"
			Object scalingInterpolation = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
			float imageScaleFactor = (float) destImageWidth / (float) imageWidth;
			if( ((int) imageScaleFactor) == imageScaleFactor &&
				imageScaleFactor > 1f &&
				imageWidth <= 16 &&
				imageHeight <= 16 )
			{
				// use "speed" scaling for small icons if the scale factor is an integer
				// to avoid blurred icons
				scalingInterpolation = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
			}

			// scale image
			BufferedImage bufferedImage = image2bufferedImage( image );
			image = scaleImage( bufferedImage, destImageWidth, destImageHeight, scalingInterpolation );
		}

		// cache image
		lastSystemScaleFactor = systemScaleFactor;
		lastUserScaleFactor = userScaleFactor;
		lastImage = image;

		// paint image
		paintLastImage( g, x, y );
	}
 
Example 16
Source File: ImageShape.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Constructor
 */
public ImageShape(){
    super();
    interp = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
    this.coords = new ArrayList<>();
}