ImageWorker { public final ImageWorker rescaleToBytes() { if (isBytes()) { return this; } forceComponentColorModel(true, true); final double[][] extrema = getExtremas(); final int length = extrema[0].length; final double[] scale = new double[length]; final double[] offset = new double[length]; boolean computeRescale = false; for (int i = 0; i < length; i++) { final double delta = extrema[1][i] - extrema[0][i]; if (Math.abs(delta) > 1E-6 && ((extrema[1][i] - 255 > 1E-6) || (extrema[0][i] < -1E-6))) { computeRescale = true; scale[i] = 255 / delta; offset[i] = -scale[i] * extrema[0][i]; } else { scale[i] = 1; offset[i] = 0; } } final RenderingHints hints = getRenderingHints(DataBuffer.TYPE_BYTE); if (computeRescale) image = RescaleDescriptor.create(image, scale, offset, hints); else image = FormatDescriptor.create(image, DataBuffer.TYPE_BYTE, hints); invalidateStatistics(); assert isBytes(); return this; } ImageWorker();  ImageWorker(final File input);  ImageWorker(final RenderedImage image); final ImageWorker setImage(final RenderedImage image); final void load(final String source, final int imageChoice, final boolean readMetadata); final RenderedImage getRenderedImage(); final BufferedImage getBufferedImage(); final PlanarImage getPlanarImage(); final RenderedOp getRenderedOperation(); final ROI getImageAsROI(); final ROI getROI(); final ImageWorker setROI(final ROI roi); final Object getRenderingHint(final RenderingHints.Key key); final ImageWorker setRenderingHint(final RenderingHints.Key key, final Object value); final ImageWorker setRenderingHints(final RenderingHints hints); final ImageWorker removeRenderingHint(final RenderingHints.Key key); final RenderingHints getRenderingHints(); final ImageWorker tileCacheEnabled(final boolean status); final int getNumBands(); final int getTransparentPixel(); final double[] getMinimums(); final double[] getMaximums(); final boolean isBytes(); final boolean isBinary(); final boolean isIndexed(); final boolean isColorSpaceRGB(); final boolean isColorSpaceYCbCr(); final boolean isColorSpaceIHS(); final boolean isColorSpaceGRAYScale(); final boolean isTranslucent(); final ImageWorker rescaleToBytes(); final ImageWorker forceIndexColorModel(final boolean error); final ImageWorker forceBitmaskIndexColorModel(); final ImageWorker forceBitmaskIndexColorModel(int suggestedTransparent,
            final boolean errorDiffusion); final ImageWorker forceIndexColorModelForGIF(final boolean errorDiffusion); final ImageWorker forceComponentColorModel(); final ImageWorker forceComponentColorModel(boolean checkTransparent, boolean optimizeGray); final ImageWorker forceComponentColorModel(boolean checkTransparent); final ImageWorker forceColorSpaceRGB(); final ImageWorker forceColorSpaceYCbCr(); final ImageWorker forceColorSpaceIHS(); final ImageWorker bandMerge(int writeband); final ImageWorker addBand(RenderedImage image, boolean before); final ImageWorker forceColorSpaceGRAYScale(); final ImageWorker intensity(); final ImageWorker retainFirstBand(); final ImageWorker retainLastBand(); final ImageWorker retainBands(final int numBands); final ImageWorker retainBands(final int[] bands); final ImageWorker format(final int dataType); final ImageWorker binarize(); final ImageWorker binarize(double threshold); final ImageWorker binarize(final int value0, final int value1); final ImageWorker makeColorTransparent(final Color transparentColor); final ImageWorker invert(); final ImageWorker mask(RenderedImage mask, final boolean maskValue, int newValue); final ImageWorker addImage(final RenderedImage renderedImage); final ImageWorker multiplyConst(double[] inValues); final ImageWorker xorConst(int[] values); ImageWorker addTransparencyToIndexColorModel(final RenderedImage alphaChannel,
            final boolean errorDiffusion); final ImageWorker addTransparencyToIndexColorModel(final RenderedImage alphaChannel,
            final boolean translucent, int transparent, final boolean errorDiffusion); final ImageWorker tile(); ImageWorker applyOpacity(float opacity); final ImageWorker write(final File output); final void writePNG(final Object destination, final String compression,
            final float compressionRate, final boolean nativeAcc, final boolean paletted); final ImageWorker writeGIF(final Object destination, final String compression,
            final float compressionRate); final void writeJPEG(final Object destination, final String compression,
            final float compressionRate, final boolean nativeAcc); final void writeTIFF(final Object destination, final String compression,
            final float compressionRate, final int tileSizeX, final int tileSizeY); ImageWorker affine(AffineTransform tx, Interpolation interpolation, double[] bgValues); ImageWorker crop(float x, float y, float width, float height); final ImageWorker show(); final void dispose(); static void main(String[] args); static final String WARP_REDUCTION_ENABLED_KEY; static final Hints.Key TILING_ALLOWED; }
@Test public void rescaleToBytes(){ assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus()); final RenderedImage test1= ConstantDescriptor.create(128.0f, 128.0f, new Double[]{20000.0}, null); final RenderedImage test2= ConstantDescriptor.create(128.0f, 128.0f, new Double[]{255.0}, null); final RenderedImage test3= getSynthetic(20000); final RenderedImage test4= getSynthetic(255); final ImageWorker test1I=new ImageWorker(test1).rescaleToBytes(); Assert.assertEquals("Format",test1I.getRenderedOperation().getOperationName()); final double[] maximums1 = test1I.getMaximums(); Assert.assertTrue(maximums1.length==1); Assert.assertEquals(255.0,maximums1[0],1E-10); final double[] minimums1 = test1I.getMinimums(); Assert.assertTrue(minimums1.length==1); Assert.assertEquals(255.0,minimums1[0],1E-10); final ImageWorker test2I=new ImageWorker(test2).rescaleToBytes(); Assert.assertEquals("Format",test2I.getRenderedOperation().getOperationName()); final double[] maximums2 = test1I.getMaximums(); Assert.assertTrue(maximums2.length==1); Assert.assertEquals(255.0,maximums2[0],1E-10); final double[] minimums2 = test1I.getMinimums(); Assert.assertTrue(minimums2.length==1); Assert.assertEquals(255.0,minimums2[0],1E-10); ImageWorker test3I=new ImageWorker(test3); final double[] maximums3a = test3I.getMaximums(); final double[] minimums3a = test3I.getMinimums(); test3I.rescaleToBytes(); Assert.assertEquals("Rescale",test3I.getRenderedOperation().getOperationName()); final double[] maximums3b = test3I.getMaximums(); final double[] minimums3b = test3I.getMinimums(); if(maximums3a[0]>255) { Assert.assertTrue(Math.abs(maximums3a[0]-maximums3b[0])>1E-10); Assert.assertTrue(Math.abs(255.0-maximums3b[0])>=0); } if(minimums3a[0]<0) { Assert.assertTrue(minimums3b[0]>=0); } ImageWorker test4I=new ImageWorker(test4); final double[] maximums4a = test4I.getMaximums(); final double[] minimums4a = test4I.getMinimums(); test4I.rescaleToBytes(); Assert.assertEquals("Format",test4I.getRenderedOperation().getOperationName()); final double[] maximums4b = test4I.getMaximums(); final double[] minimums4b = test4I.getMinimums(); Assert.assertEquals(maximums4a[0],maximums4b[0],1E-10); Assert.assertEquals(minimums4a[0],minimums4b[0],1E-10); final RenderedImage multiband=BandMergeDescriptor.create(test2, test3, null); ImageWorker testmultibandI=new ImageWorker(multiband); final double[] maximums5a = testmultibandI.getMaximums(); final double[] minimums5a = testmultibandI.getMinimums(); testmultibandI.rescaleToBytes(); final double[] maximums5b = testmultibandI.getMaximums(); final double[] minimums5b = testmultibandI.getMinimums(); Assert.assertEquals(maximums5a[0],maximums5b[0],1E-10); Assert.assertEquals(minimums5a[0],minimums5b[0],1E-10); Assert.assertTrue(Math.abs(maximums5a[1]-maximums5b[1])>1E-10); Assert.assertTrue(Math.abs(minimums5a[1]-minimums5b[1])>1E-10); }