示例#1
0
  /**
   * Creates and returns a new Image object that is a rendering of this Image object. The input
   * object defines the rendering parameters of the new Image object. Imagemagick's convert program
   * is used to create a scaled bounding box of this Image with the given dimensions.
   *
   * @param {Object} input A JavaScript object specifying the rendering parameters. For example,
   *     <code> {maxWidth:200, maxHeight:100} </code>
   * @returns {Image} The rendered Image object
   * @throws Exception
   */
  public ImageObject jsFunction_render(Object input) throws Exception {
    if (input == null || !(input instanceof Scriptable)) {
      throw new RuntimeException("The first argument to render() must be a scriptable object.");
    }

    Scriptable s = (Scriptable) input;
    int maxWidth = toInt(s.get("maxWidth", s));
    int maxHeight = toInt(s.get("maxHeight", s));

    int cropWidth = toInt(s.get("cropWidth", s));
    int cropHeight = toInt(s.get("cropHeight", s));
    int cropXOffset = toInt(s.get("cropXOffset", s));
    int cropYOffset = toInt(s.get("cropYOffset", s));

    int currentWidth = (int) node.getInteger(WIDTH);
    int currentHeight = (int) node.getInteger(HEIGHT);

    String aname = null;
    if (maxWidth > 0 && maxHeight > 0 && currentWidth > 0 && currentHeight > 0) {
      int[] dims = this.computeResizedDimensions(maxWidth, maxHeight, currentWidth, currentHeight);
      aname = dims[0] + "x" + dims[1];
      maxWidth = dims[0];
      maxHeight = dims[1];
    } else if (cropWidth > 0 && cropHeight > 0) {
      aname = cropWidth + "x" + cropHeight + "_" + cropXOffset + "x" + cropYOffset;
    } else {
      throw new RuntimeException("render(), invalid parameter set.");
    }

    Object o = this.jsFunction_get(aname);
    if (o instanceof ImageObject) {
      return (ImageObject) o;
    }

    try {
      synchronized (this) {
        while (this.convertOps.contains(aname)) {
          this.wait();
        }
        this.convertOps.add(aname);
      }

      o = ((axiom.objectmodel.db.Node) this.node).getChildElement(aname, true);
      if (o instanceof Node) {
        return (ImageObject) Context.toObject(o, this.core.global);
      }

      ImageObject computedImg = null;
      String[] paths = getPaths();
      String imgPath = paths[0];
      String tmpPath = paths[1];
      String fileName = node.getString(FileObject.FILE_NAME);

      try {
        File tmpFile = new File(tmpPath);
        int[] dims = null;
        if (maxWidth > 0 && maxHeight > 0) {
          dims =
              this.resize(
                  maxWidth,
                  maxHeight,
                  (int) this.node.getInteger(WIDTH),
                  (int) this.node.getInteger(HEIGHT),
                  imgPath,
                  tmpPath,
                  true);
        } else {
          dims = this.crop(cropWidth, cropHeight, cropXOffset, cropYOffset, imgPath, tmpPath);
        }

        if (dims == null) {
          throw new Exception("ImageObject.render(), resizing the image failed.");
        }

        final String protoname = "Image";
        INode node =
            new axiom.objectmodel.db.Node(protoname, protoname, core.app.getWrappedNodeManager());
        computedImg = new ImageObject("Image", core, node, core.getPrototype(protoname), true);

        node.setString(FileObject.FILE_NAME, fileName);
        node.setString(FileObject.ACCESSNAME, FileObjectCtor.generateAccessName(fileName));
        node.setString(FileObject.CONTENT_TYPE, this.node.getString(FileObject.CONTENT_TYPE));
        node.setJavaObject(FileObject.SELF, computedImg);
        node.setInteger(ImageObject.WIDTH, dims[0]);
        node.setInteger(ImageObject.HEIGHT, dims[1]);
        node.setString(FileObject.RENDERED_CONTENT, "true");

        node.setInteger(FileObject.FILE_SIZE, tmpFile.length());
        computedImg.tmpPath = tmpPath;
      } catch (Exception ex) {
        throw new RuntimeException(
            "ImageObject.jsfunction_bound(): Could not write the image to temporary storage, "
                + ex.getMessage());
      }

      if (computedImg != null) {
        this.jsFunction_addThumbnail(computedImg, null);
        return computedImg;
      }
    } finally {
      synchronized (this) {
        this.convertOps.remove(aname);
        this.notifyAll();
      }
    }

    return null;
  }