Renders the image using its gray magnitude

@param input (Input) Image image
@param maxAbsValue (Input) Largest absolute value of a pixel in the image
@param output (Output) Bitmap ARGB_8888 image.
@param storage Optional working buffer for Bitmap image.
517196
public static void grayMagnitude(GrayS32 input , int maxAbsValue , Bitmap output , byte[] storage) {
		shapeShape(input, output);

		if( storage == null )
			storage = declareStorage(output,null);

		if( maxAbsValue < 0 )
			maxAbsValue = ImageStatistics.maxAbs(input);

		int indexDst = 0;

		for( int y = 0; y < input.height; y++ ) {
			int indexSrc = input.startIndex + y*input.stride;
			for( int x = 0; x < input.width; x++ ) {
				byte gray = (byte)(255*Math.abs(input.data[ indexSrc++ ])/maxAbsValue);
				storage[indexDst++] = gray;
				storage[indexDst++] = gray;
				storage[indexDst++] = gray;
				storage[indexDst++] = (byte) 0xFF;
			}
		}

		output.copyPixelsFromBuffer(ByteBuffer.wrap(storage));
	}
596283
public static void histogram( GrayS8 input , int minValue , int histogram[] ) {
		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplImageStatistics_MT.histogram(input,minValue,histogram);
		} else {
			ImplImageStatistics.histogram(input,minValue,histogram);
		}
	}
1296443
public static <T extends ImageBase<T>> void abs( T input , T output )
	{
		if( input instanceof ImageGray) {
			if (GrayS8.class == input.getClass()) {
				PixelMath.abs((GrayS8) input, (GrayS8) output);
			} else if (GrayS16.class == input.getClass()) {
				PixelMath.abs((GrayS16) input, (GrayS16) output);
			} else if (GrayS32.class == input.getClass()) {
				PixelMath.abs((GrayS32) input, (GrayS32) output);
			} else if (GrayS64.class == input.getClass()) {
				PixelMath.abs((GrayS64) input, (GrayS64) output);
			} else if (GrayF32.class == input.getClass()) {
				PixelMath.abs((GrayF32) input, (GrayF32) output);
			} else if (GrayF64.class == input.getClass()) {
				PixelMath.abs((GrayF64) input, (GrayF64) output);
			}
			// otherwise assume it is an unsigned image type
		} else if( input instanceof ImageInterleaved ) {
			if (InterleavedS8.class == input.getClass()) {
				PixelMath.abs((InterleavedS8) input, (InterleavedS8) output);
			} else if (InterleavedS16.class == input.getClass()) {
				PixelMath.abs((InterleavedS16) input, (InterleavedS16) output);
			} else if (InterleavedS32.class == input.getClass()) {
				PixelMath.abs((InterleavedS32) input, (InterleavedS32) output);
			} else if (InterleavedS64.class == input.getClass()) {
				PixelMath.abs((InterleavedS64) input, (InterleavedS64) output);
			} else if (InterleavedF32.class == input.getClass()) {
				PixelMath.abs((InterleavedF32) input, (InterleavedF32) output);
			} else if (InterleavedF64.class == input.getClass()) {
				PixelMath.abs((InterleavedF64) input, (InterleavedF64) output);
			}
		} else {
			Planar in = (Planar)input;
			Planar out = (Planar)output;

			for (int i = 0; i < in.getNumBands(); i++) {
				abs(in.getBand(i),out.getBand(i));
			}
		}
	}
240366
public void encode(final BitOutputStream out, final long value, final long max)
    throws IOException {
    if (0 == max) {
      return;
    }
    int bits = (int) (Math.round(log2(2 * this.stdDev)) - 1);
    if (0 > bits) {
      bits = 0;
    }
    final long centralWindow = 1l << bits;
    if (centralWindow >= (max + 1) / 2.) {
      out.writeBoundedLong(value, max + 1);
      return;
    }
    long stdDevWindowStart = (long) (this.mean - centralWindow / 2);
    long stdDevWindowEnd = stdDevWindowStart + centralWindow;
    if (stdDevWindowStart < 0) {
      stdDevWindowEnd += -stdDevWindowStart;
      stdDevWindowStart += -stdDevWindowStart;
    }
    else {
      final long delta = stdDevWindowEnd - (max + 1);
      if (delta > 0) {
        stdDevWindowStart -= delta;
        stdDevWindowEnd -= delta;
      }
    }
    if (value < stdDevWindowStart) {
      out.write(false);
      if (stdDevWindowEnd <= max) {
        out.write(false);
      }
      out.writeBoundedLong(value, stdDevWindowStart);
    }
    else if (value < stdDevWindowEnd) {
      out.write(true);
      out.writeBoundedLong(value - stdDevWindowStart, centralWindow);
    }
    else {
      out.write(false);
      if (stdDevWindowStart > 0) {
        out.write(true);
      }
      out.writeBoundedLong(value - stdDevWindowEnd, 1 + max - stdDevWindowEnd);
    }
  }
552138
public static boolean write(Image image, ImageWriter writer, ImageOutputStream output, float quality) {
		if (writer == null) {
			return false;
		}

		writer.setOutput(output);
		final RenderedImage renderedImage = toRenderedImage(image);
		// 设置质量
		ImageWriteParam imgWriteParams = null;
		if (quality > 0 && quality < 1) {
			imgWriteParams = writer.getDefaultWriteParam();
			if (imgWriteParams.canWriteCompressed()) {
				imgWriteParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
				imgWriteParams.setCompressionQuality(quality);
				final ColorModel colorModel = renderedImage.getColorModel();// ColorModel.getRGBdefault();
				imgWriteParams.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
			}
		}

		try {
			if (null != imgWriteParams) {
				writer.write(null, new IIOImage(renderedImage, null, null), imgWriteParams);
			} else {
				writer.write(renderedImage);
			}
			output.flush();
		} catch (IOException e) {
			throw new IORuntimeException(e);
		} finally {
			writer.dispose();
		}
		return true;
	}
1295434
public static void write (BufferedImage image, OutputStream out)
        throws IOException
    {
        DataOutputStream dout = new DataOutputStream(out);

        // write the image dimensions
        int width = image.getWidth(), height = image.getHeight();
        dout.writeInt(width);
        dout.writeInt(height);

        // write the color model information
        IndexColorModel cmodel = (IndexColorModel)image.getColorModel();
        int tpixel = cmodel.getTransparentPixel();
        dout.writeInt(tpixel);
        int msize = cmodel.getMapSize();
        int[] map = new int[msize];
        cmodel.getRGBs(map);
        dout.writeInt(msize);
        for (int element : map) {
            dout.writeInt(element);
        }

        // write the raster data
        DataBufferByte dbuf = (DataBufferByte)image.getRaster().getDataBuffer();
        byte[] data = dbuf.getData();
        if (data.length != width * height) {
            String errmsg = "Raster data not same size as image! [" +
                width + "x" + height + " != " + data.length + "]";
            throw new IllegalStateException(errmsg);
        }
        dout.write(data);
        dout.flush();
    }
636862
public static void bufferRgbToMsU8( byte []input , Planar<GrayU8> output ) {
		GrayU8 band0 = output.getBand(0);
		GrayU8 band1 = output.getBand(1);
		GrayU8 band2 = output.getBand(2);

		int indexIn = 0;
		for( int y = 0; y < output.height; y++ ) {
			int indexOut = output.startIndex + y*output.stride;
			for( int x = 0; x < output.width; x++ , indexOut++ ) {
				band0.data[indexOut] = input[indexIn++];
				band1.data[indexOut] = input[indexIn++];
				band2.data[indexOut] = input[indexIn++];
			}
		}
	}
95462
public static <T extends ImageGray<T>> void maximum(Planar<T> input, T output) {
		maximum(input, output, 0, input.getNumBands() - 1);
	}
135058
public static BufferedImage createCompatibleImage (BufferedImage source, int width, int height)
    {
        WritableRaster raster = source.getRaster().createCompatibleWritableRaster(width, height);
        return new BufferedImage(source.getColorModel(), raster, false, null);
    }
968475
function ByteBuffer(arg) {
    var initial_size;
    if (arg == null) {
        initial_size = 1024 * 1024;
    } else if (typeof arg === "number") {
        initial_size = arg;
    } else if (arg instanceof Uint8Array) {
        this.buffer = arg;
        this.position = 0;  // Overwrite
        return;
    } else {
        // typeof arg -> String
        throw typeof arg + " is invalid parameter type for ByteBuffer constructor";
    }
    // arg is null or number
    this.buffer = new Uint8Array(initial_size);
    this.position = 0;
}
549794
public static BufferedImage renderBinary(GrayU8 binaryImage, boolean invert, BufferedImage out) {

		if( out == null || ( out.getWidth() != binaryImage.width || out.getHeight() != binaryImage.height) ) {
			out = new BufferedImage(binaryImage.getWidth(),binaryImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
		}

		try {
			WritableRaster raster = out.getRaster();
			DataBuffer buffer = raster.getDataBuffer();
			if( buffer.getDataType() == DataBuffer.TYPE_BYTE ) {
				renderBinary(binaryImage, invert, (DataBufferByte)buffer, raster);
			} else if( buffer.getDataType() == DataBuffer.TYPE_INT ) {
				renderBinary(binaryImage, invert, (DataBufferInt)buffer, raster);
			} else {
				_renderBinary(binaryImage, invert,  out);
			}
		} catch( SecurityException e ) {
			_renderBinary(binaryImage, invert, out);
		}
		// hack so that it knows the buffer has been modified
		out.setRGB(0,0,out.getRGB(0,0));
		return out;
	}
968248
static BufferedImage convertImage (BufferedImage image) {
    switch (image.getType()) {
    case BufferedImage.TYPE_INT_ARGB_PRE:
      return image; // Already good to go
    case BufferedImage.TYPE_4BYTE_ABGR:
      image.coerceData(true); // Just premultiply the alpha and it's fine
      return image;
    }

    // Didn't know an easy thing to do, so create a whole new image in our preferred format
    BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(),
                                                     BufferedImage.TYPE_INT_ARGB_PRE);
    Graphics g = convertedImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, image.getWidth(), image.getHeight());
    g.drawImage(image, 0, 0, null);

    return convertedImage;
  }
664277
public static InterleavedS64 convert( Planar<GrayS64> input , InterleavedS64 output ) {
		if (output == null) {
			output = new InterleavedS64(input.width, input.height,input.getNumBands());
		} else {
			output.reshape(input.width,input.height,input.getNumBands());
		}

		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplConvertImage_MT.convert(input,output);
		} else {
			ImplConvertImage.convert(input,output);
		}

		return output;
	}
566432
public static void histogram( GrayS32 input , int minValue , int histogram[] ) {
		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplImageStatistics_MT.histogram(input,minValue,histogram);
		} else {
			ImplImageStatistics.histogram(input,minValue,histogram);
		}
	}
196332
public static void boundImage( GrayS64 img , long min , long max ) {
		ImplPixelMath.boundImage(img,min,max);
	}
175014
public static byte[] getPixelsBGR(BufferedImage img, int y, int w, byte[] array, int[] temp) {
    final int x = 0;
    final int h = 1;

    assert array.length == temp.length * nrChannels(img);
    assert (temp.length == w);

    int imageType = img.getType();
    Raster raster;
    switch (imageType) {
      case BufferedImage.TYPE_3BYTE_BGR:
      case BufferedImage.TYPE_4BYTE_ABGR:
      case BufferedImage.TYPE_4BYTE_ABGR_PRE:
      case BufferedImage.TYPE_BYTE_GRAY:
        raster = img.getRaster();
        //int ttype= raster.getTransferType();
        raster.getDataElements(x, y, w, h, array);
        break;
      case BufferedImage.TYPE_INT_BGR:
        raster = img.getRaster();
        raster.getDataElements(x, y, w, h, temp);
        ints2bytes(temp, array, 0, 1, 2);  // bgr -->  bgr
        break;
      case BufferedImage.TYPE_INT_RGB:
        raster = img.getRaster();
        raster.getDataElements(x, y, w, h, temp);
        ints2bytes(temp, array, 2, 1, 0);  // rgb -->  bgr
        break;
      case BufferedImage.TYPE_INT_ARGB:
      case BufferedImage.TYPE_INT_ARGB_PRE:
        raster = img.getRaster();
        raster.getDataElements(x, y, w, h, temp);
        ints2bytes(temp, array, 2, 1, 0, 3);  // argb -->  abgr
        break;
      case BufferedImage.TYPE_CUSTOM: // TODO: works for my icon image loader, but else ???
        img.getRGB(x, y, w, h, temp, 0, w);
        ints2bytes(temp, array, 2, 1, 0, 3);  // argb -->  abgr
        break;
      default:
        img.getRGB(x, y, w, h, temp, 0, w);
        ints2bytes(temp, array, 2, 1, 0);  // rgb -->  bgr
        break;
    }

    return array;
  }
471312
public static int Maximum(ImageSource fastBitmap, int startX, int startY, int width, int height) {
        int max = 0;

        if (fastBitmap.isGrayscale()) {
            for (int i = startX; i < height; i++) {
                for (int j = startY; j < width; j++) {
                    int gray = fastBitmap.getRGB(j, i);
                    if (gray > max) {
                        max = gray;
                    }
                }
            }
        } else {
            for (int i = startX; i < height; i++) {
                for (int j = startY; j < width; j++) {
                    int gray = fastBitmap.getG(j, i);
                    if (gray > max) {
                        max = gray;
                    }
                }
            }
        }

        return max;
    }