imagemodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,229 行 · 第 1/4 页

JAVA
2,229
字号
				 sx, sy, sx+w, sy+h, null);    return true;  }  /**   * Copy and merge part of an image   */  public static boolean imagecopymerge(QuercusImage dest, QuercusImage src,				       int dx, int dy, int sx, int sy,				       int w, int h, int pct)  {    BufferedImage rgba =      new BufferedImage(dest.getWidth(), dest.getHeight(),			BufferedImage.TYPE_INT_ARGB);    rgba.getGraphics().drawImage(src._bufferedImage, 0, 0, null);    BufferedImageOp rescaleOp =       new RescaleOp(new float[] { 1, 1, 1, ((float)pct)/100 },		    new float[] { 0, 0, 0, 0 },		    null);    BufferedImage rescaledImage =      rescaleOp.filter(rgba, null);    Graphics2D g = (Graphics2D)dest.getGraphics().create();    g.setComposite(AlphaComposite.SrcOver);    g.drawImage(rescaledImage,		dx, dy, dx+w, dy+h,		sx, sy, sx+w, sy+h, null);    return true;  }  /**   * Copy and merge part of an image with gray scale   */  public static boolean imagecopymergegray(QuercusImage dest, QuercusImage src,					   int dx, int dy, int sx, int sy,					   int w, int h, int pct)  {    BufferedImage rgba =      new BufferedImage(dest.getWidth(), dest.getHeight(),			BufferedImage.TYPE_INT_ARGB);    rgba.getGraphics().drawImage(src._bufferedImage, 0, 0, null);    BufferedImageOp rescaleOp =       new RescaleOp(new float[] { 1, 1, 1, ((float)pct)/100 },		    new float[] { 0, 0, 0, 0 },		    null);    BufferedImage rescaledImage =      rescaleOp.filter(rgba, null);    ColorConvertOp colorConvertOp =      new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);    colorConvertOp.filter(dest._bufferedImage, dest._bufferedImage);    Graphics2D g = (Graphics2D)dest.getGraphics().create();    g.setComposite(AlphaComposite.SrcOver);    g.drawImage(rescaledImage,		dx, dy, dx+w, dy+h,		sx, sy, sx+w, sy+h, null);    return true;  }  /**   * Copy and resize part of an image with resampling   */  public static boolean imagecopyresampled(QuercusImage dest, QuercusImage src,					   int dx, int dy, int sx, int sy, 					   int dw, int dh, int sw, int sh)  {    Graphics2D g = (Graphics2D)dest.getGraphics().create();    g.setRenderingHint(RenderingHints.KEY_RENDERING,		       RenderingHints.VALUE_RENDER_QUALITY);    g.drawImage(src._bufferedImage,		dx, dy, dx+dw, dy+dh,		sx, sy, sx+sw, sy+sh, null);    g.setRenderingHint(RenderingHints.KEY_RENDERING,		       RenderingHints.VALUE_RENDER_DEFAULT);    return true;  }  /**   * Copy and resize part of an image   */  public static boolean imagecopyresized(QuercusImage dest, QuercusImage src,					 int dx, int dy, int sx, int sy, 					 int dw, int dh, int sw, int sh)  {    Graphics2D g = (Graphics2D)dest.getGraphics().create();    g.drawImage(src._bufferedImage,		dx, dy, dx+dw, dy+dh,		sx, sy, sx+sw, sy+sh, null);    return true;  }  /**   * Create a new palette based image   */  public static Value imagecreate(int width, int height)  {    return new QuercusImage(width, height);  }  /**   * Create a new image from GD2 file or URL   */  public static void imagecreatefromgd2(Path file)  {    throw new QuercusException(".gd images are not supported");  }  /**   * Create a new image from a given part of GD2 file or URL   */  public static void imagecreatefromgd2part(Path file,					    int srcX, int srcY,					    int width, int height)  {    throw new QuercusException(".gd images are not supported");  }  /**   * Create a new image from GD file or URL   */  public static void imagecreatefromgd(Path file)  {    throw new QuercusException(".gd images are not supported");  }  /**   * Create a new image from file or URL   */  public static QuercusImage imagecreatefromgif(Env env, Path filename)  {    return new QuercusImage(env, filename);  }  /**   * Create a new image from file or URL   */  @ReturnNullAsFalse  public static QuercusImage imagecreatefromjpeg(Env env, Path filename)  {    try {      return new QuercusImage(env, filename);    } catch (Exception e) {      env.warning(L.l("Can't open {0} as a jpeg image.\n{1}",		      filename, e));      log.log(Level.FINE, e.toString(), e);      return null;    }  }  /**   * Create a new image from file or URL   */  public static QuercusImage imagecreatefrompng(Env env, Path filename)  {    return new QuercusImage(env, filename);  }  /**   * Create a new image from the image stream in the string   */  public static QuercusImage imagecreatefromstring(Env env, InputStream data)  {    if (data == null)      return null;    return new QuercusImage(data);  }  /**   * Create a new image from file or URL   */  public static QuercusImage imagecreatefromwbmp(Env env, Path filename)  {    return new QuercusImage(env, filename);  }  /**   * Create a new image from file or URL   */  public static Value imagecreatefromxbm(Env env, Path filename)  {    return new QuercusImage(env, filename);  }  /**   * Create a new image from file or URL   */  public static QuercusImage imagecreatefromxpm(Env env, Path filename)  {    return new QuercusImage(env, filename);  }  /**   * Create a new true color image   */  public static Value imagecreatetruecolor(int width, int height)  {    return new QuercusImage(width, height);  }  /**   * Draw a dashed line   */  public static boolean imagedashedline(QuercusImage image,					int x1, int y1, int x2, int y2,					int color)  {    Graphics2D g = image.getGraphics();    Stroke stroke = g.getStroke();    g.setColor(intToColor(color));    g.setStroke(new BasicStroke(1, BasicStroke.JOIN_ROUND,				BasicStroke.CAP_ROUND, 1,				new float[] { 5, 5 }, 0));    g.draw(new Line2D.Float(x1, y1, x2, y2));    g.setStroke(stroke);    return true;  }  /**   * Destroy an image   */  public static boolean imagedestroy(QuercusImage image)  {    // no-op    return true;  }  /**   * Draw an ellipse   */  public static boolean imageellipse(QuercusImage image,				     double cx, double cy,				     double width, double height,				     int color)  {    Shape shape = new Ellipse2D.Double(cx-width/2, cy-height/2, width, height);    image.stroke(shape, color);    return true;  }  /**   * Flood fill   */  public static boolean imagefill(QuercusImage image, int x, int y, int color)  {    image.flood(x, y, color);    return true;  }  /**   * Draw a partial ellipse and fill it   */  public static boolean imagefilledarc(QuercusImage image,				       double cx, double cy,				       double width, double height,				       double start, double end,				       int color,				       int style)  {    int type = Arc2D.PIE;    if ((style & IMG_ARC_CHORD)!=0) type = Arc2D.CHORD;    if ((style & IMG_ARC_PIE)!=0)   type = Arc2D.PIE;    Arc2D arc =      new Arc2D.Double(cx-width/2, cy-height/2,		       width, height, -1 * start,		       -1 *(end-start), type);    if ((style & IMG_ARC_NOFILL)==0) image.fill(arc, color);    if ((style & IMG_ARC_EDGED)!=0)  image.stroke(arc, color);    return true;  }  /**   * Draw a filled ellipse   */  public static boolean imagefilledellipse(QuercusImage image,					   double cx, double cy,					   double width, double height,					   int color)  {    Ellipse2D ellipse =      new Ellipse2D.Double(cx-width/2, cy-height/2, width, height);    image.fill(ellipse, color);    return true;  }  /**   * Draw a filled polygon   */  public static boolean imagefilledpolygon(Env env,                                           QuercusImage image,                                           ArrayValue points,                                           int numPoints, int color)  {    image.fill(arrayToPolygon(env, points, numPoints), color);    return true;  }  /**   * Draw a filled rectangle   */  public static boolean imagefilledrectangle(QuercusImage image, int x1, int y1,					     int x2, int y2, int color)  {    image.fill(new Rectangle2D.Float(x1, y1, x2-x1+1, y2-y1+1), color);    return true;  }  /**   * Flood fill to specific color   */  public static boolean imagefilltoborder(QuercusImage image, int x, int y,					  int border, int color)  {    image.flood(x, y, color, border);    return true;  }  // Filters /////////////////////////////////////////////////////////  /**   * Applies a filter to an image   */  public static boolean imagefilter(Env env, QuercusImage image, int filterType,				    @Optional int arg1, @Optional int arg2,				    @Optional int arg3)  {    switch(filterType)      {	case IMG_FILTER_NEGATE:	  // Reverses all colors of the image.	  env.warning(L.l("imagefilter(IMG_FILTER_NEGATE) unimplemented"));	  return false;	case IMG_FILTER_GRAYSCALE:	  // Converts the image into grayscale.	  env.warning(L.l("imagefilter(IMG_FILTER_GRAYSCALE) unimplemented"));	  return false;	case IMG_FILTER_BRIGHTNESS:	  // Changes brightness of the image. Arg1 sets level of brightness.	  env.warning(L.l("imagefilter(IMG_FILTER_BRIGHTNESS) unimplementetd"));	  return false;	case IMG_FILTER_CONTRAST:	  // Changes contrast of the image. Use arg1 to set level of contrast.	  env.warning(L.l("imagefilter(IMG_FILTER_CONTRAST) unimplementetd"));	  return false;	case IMG_FILTER_COLORIZE:	  // Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use	  // arg1, arg2 and arg3 in the form of red, blue, green. The range	  // for each color is 0 to 255.	  env.warning(L.l("imagefilter(IMG_FILTER_COLORIZE) unimplemented"));	  return false;	case IMG_FILTER_EDGEDETECT:	  // Uses edge detection to highlight the edges in the image.	  env.warning(L.l("imagefilter(IMG_FILTER_EDGEDETECT) unimplemented"));	  return false;	case IMG_FILTER_EMBOSS:	  // Embosses the image.	  env.warning(L.l("imagefilter(IMG_FILTER_EMBOSS) unimplemented"));	  return false;	case IMG_FILTER_GAUSSIAN_BLUR:	  // Blurs the image using the Gaussian method.	  env.warning(L.l("imagefilter(IMG_FILTER_GAUSSIAN_BLUR) "+			  "unimplemented"));	  return false;	case IMG_FILTER_SELECTIVE_BLUR:	  // Blurs the image.	  env.warning(L.l("imagefilter(IMG_FILTER_SELECTIVE_BLUR) "+			  "unimplemented"));	  return false;	case IMG_FILTER_MEAN_REMOVAL:	  // Uses mean removal to achieve a "sketchy" effect.	  env.warning(L.l("imagefilter(IMG_FILTER_MEAN_REMOVAL) "+			  "unimplemented"));	  return false;	case IMG_FILTER_SMOOTH:	  // Makes the image smoother. Use arg1 to set the level of smoothness.	  env.warning(L.l("imagefilter(IMG_FILTER_SMOOTH) unimplemented"));	  return false;	default:	  throw new QuercusException("unknown filterType in imagefilter()");      }  }  /**   * Get font height.   *   * @param font a font previously loaded with {@link #imageloadfont},   *             or 1 -5 for built-in fonts   */  public static int imagefontheight(int font)  {    if (font < 1)      return 8;    else if (font == 1)      return 8;    else if (font == 2)      return 13;    else if (font == 3)      return 13;    else if (font == 4)      return 16;    else if (font == 5)      return 15;    else      return 15;  }  /**   * Get font width.   *   * @param font a font previously loaded with {@link #imageloadfont},   *             or 1 -5 for built-in fonts   */  public static int imagefontwidth(int font)  {    if (font < 1)      return 5;    else if (font == 1)      return 5;    else if (font == 2)      return 6;    else if (font == 3)      return 7;    else if (font == 4)      return 8;    else if (font == 5)      return 9;    else      return 9;  }  /**   * draws a true type font image   */  public static Value imageftbbox(Env env,				  double size,				  double angle,				  String fontFile,				  String text,				  @Optional ArrayValue extra)  {    try {      QuercusImage image = new QuercusImage(100, 100);            Graphics2D g = image.getGraphics();      Font font = image.getTrueTypeFont(env, fontFile);      if (font == null)	font = image.getFont(1);      font = font.deriveFont((float) (size * 96.0 / 72.0));      Font oldFont = g.getFont();      g.setFont(font);      Rectangle2D rect = font.getStringBounds(text, g.getFontRenderContext());      int descent = g.getFontMetrics(font).getDescent();      g.setFont(oldFont);      double x1 = rect.getX();      double y1 = 0;      double x2 = rect.getX() + rect.getWidth();      double y2 = rect.getY() + descent - 1;            ArrayValue bbox = new ArrayValueImpl();      bbox.put(new LongValue(Math.round(x1)));      bbox.put(new LongValue(Math.round(y1)));            bbox.put(new LongValue(Math.round(x2)));      bbox.put(new LongValue(Math.round(y1)));            bbox.put(new LongValue(Math.round(x2)));      bbox.put(new LongValue(Math.round(y2)));      bbox.put(new LongValue(Math.round(x1)));      bbox.put(new LongValue(Math.round(y2)));            return bbox;    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);      return NullValue.NULL;    }  }  /**   * draws a true type font image   */  public static Value imagefttext(Env env,				  @NotNull QuercusImage image,				  double size,				  double angle,				  int x,				  int y,				  int color,				  String fontFile,				  String text,				  @Optional ArrayValue extra)  {    try {       Graphics2D g = image.getGraphics();      g.setColor(intToColor(color));      Font font = image.getTrueTypeFont(env, fontFile);      if (font == null)	font = image.getFont(1);      font = font.deriveFont((float) (size * 96.0 / 72.0));      g.setFont(font);      Object oldAntiAlias	= g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,			 RenderingHints.VALUE_ANTIALIAS_ON);            AffineTransform oldTransform = g.getTransform();      if (angle != 0) {	g.translate(x, y);	g.rotate(- Math.toRadians(angle));	g.drawString(text, 0, 0);      }      else	g.drawString(text, x, y);            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntiAlias);      g.setTransform(oldTransform);          return NullValue.NULL;    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);      return NullValue.NULL;    }  }  /**   * Apply a gamma correction to a GD image   */  public static boolean imagegammacorrect(QuercusImage image,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?