76 4 Image Processing and Manipulation
111
112 // use one of these three statements to set the interpolation method to
be used
113 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION NEAREST NEIGHBOR ) ;
114 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION BILINEAR ) ;
115 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION BICUBIC ) ;
116
117 t a r g e t graphics . drawImage ( source , tx , ty , tw , th , null);
118 }
Listing 4.10 Java image scaling and subsetting.
1 public static void drawImageToImage(BufferedImage source ,
2 BoundingBox source bb , BufferedImage target ,
3 BoundingBox target bb ) {
4 double xd = ta rget bb .maxX − target bb .minX;
5 double yd = ta rget bb .maxY − target bb .minY;
6 double wd = ( double) target . getWidth () ;
7 double hd = ( double ) target . getHeight () ;
8 double targdpx = xd / wd;
9 double targdpy = yd / hd;
10 double srcdpx = ( source bb .maxX − source bb .minX) / source . getWidth () ;
11 double srcdpy = ( source bb .maxY − source bb .minY) / source . getHeight () ;
12 int tx = ( int ) Math. round ((( source bb .minX − target bb .minX) / targdpx ) ) ;
13 int ty = target . getHeight () − ( int ) Math. round ((( source bb .maxY − target bb
.minY) / yd) ∗ hd ) − 1;
14 int tw = ( int ) Math. ceil ((( srcdpx / targdpx ) ∗ source . getWidth () ) ) ;
15 int th = ( int ) Math. ceil ((( srcdpy / targdpy ) ∗ source . getHeight () ));
16 Graphics2D target graphics = (Graphics2D) target . getGraphics() ;
17
18 // use one of these three statements to set the interpolation method to be
used
19 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION NEAREST NEIGHBOR ) ;
20 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION BILINEAR ) ;
21 t a r g e t graphics . setRenderingHint(RenderingHints.KEY INTERPOLATION,
RenderingHints .VALUE INTERPOLATION BICUBIC ) ;
22
23 t a r g e t graphics .drawImage(source , tx , ty , tw, th , null );
24 }
Listing 4.11 Python image scaling and subsetting.
1 import Image , ImageDraw # requires the Python Imaging Library ( PIL) addon to
python
2
3 def drawImageToImage ( source , sourceBoundingBox , target , targetBoundingBox ) :
4 # calculations to determine the degrees per pixel for each dimension of the
target image
5 targetXDelta = targetBoundingBox .maxX − targetBoundingBox .minX
6 targetYDelta = targetBoundingBox .maxY − targetBoundingBox .minY
7 targetWidth = target . size [0]
8 targetHeight = target . size[1]
9 targetDegPerPixelX = targetXDelta / float (targetWidth)
10 targetDegPerPixelY = targetYDelta / float ( targetHeight )
11
12 # calculations to determine the degrees per pixel for each dimension of the
source image
13 # (we collapse the equations into two lines )
14 sourceXDelta = sourceBoundingBox .maxX − sourceBoundingBox .minX
15 sourceYDelta = sourceBoundingBox .maxY − sourceBoundingBox .minY