public static DefineBits createDefineBits(InputStream in,String mimeType) throws IOException {
  byte[] bytes=FileUtils.toByteArray(in);
  Image image=ImageUtil.getImage(bytes);
  if (mimeType == null) {
    throw new IOException("Unsupported MIME type");
  }
  PixelGrabber pixelGrabber=null;
  try {
    pixelGrabber=ImageUtil.getPixelGrabber(image,null);
  }
 catch (  Exception e) {
    throw new IOException("Error reading image");
  }
  int width=pixelGrabber.getWidth();
  int height=pixelGrabber.getHeight();
  if (MIME_JPG.equals(mimeType) || MIME_JPEG.equals(mimeType)) {
    DefineBits imageTag=new DefineBits(Tag.stagDefineBitsJPEG2);
    imageTag.data=bytes;
    imageTag.width=width;
    imageTag.height=height;
    return imageTag;
  }
 else   if (MIME_PNG.equals(mimeType) || MIME_GIF.equals(mimeType)) {
    int[] pixels=(int[])pixelGrabber.getPixels();
    DefineBitsLossless imageTag=createDefineBitsLossless(pixels,width,height);
    return imageTag;
  }
 else {
    throw new IOException("Unsupported MIME type: " + mimeType);
  }
}

public static DefineBits createDefineBits ( InputStream in , String mimeType ) throws IOException { byte [ ] bytes = FileUtils . toByteArray ( in ) ; Image image = ImageUtil . getImage ( bytes ) ; if ( mimeType == null ) { throw new IOException ( "Unsupported MIME type" ) ; } PixelGrabber pixelGrabber = null ; try { pixelGrabber = ImageUtil . getPixelGrabber ( image , null ) ; } catch ( Exception e ) { throw new IOException ( "Error reading image" ) ; } int width = pixelGrabber . getWidth ( ) ; int height = pixelGrabber . getHeight ( ) ; if ( MIME_JPG . equals ( mimeType ) || MIME_JPEG . equals ( mimeType ) ) { DefineBits imageTag = new DefineBits ( Tag . stagDefineBitsJPEG2 ) ; imageTag . data = bytes ; imageTag . width = width ; imageTag . height = height ; return imageTag ; } else if ( MIME_PNG . equals ( mimeType ) || MIME_GIF . equals ( mimeType ) ) { int [ ] pixels = ( int [ ] ) pixelGrabber . getPixels ( ) ; DefineBitsLossless imageTag = createDefineBitsLossless ( pixels , width , height ) ; return imageTag ; } else { throw new IOException ( "Unsupported MIME type: " + mimeType ) ; } }
creates the define bits .
