コード例 #1
0
ファイル: TileSet.java プロジェクト: dosage/nenya
  /**
   * Creates a copy of this tileset which will apply the supplied colorizations to its tileset image
   * when creating tiles.
   */
  public TileSet clone(Colorization[] zations) {
    try {
      TileSet tset = (TileSet) clone();
      tset._zations = zations;
      return tset;

    } catch (CloneNotSupportedException cnse) {
      log.warning(
          "Unable to clone tileset prior to colorization",
          "tset",
          this,
          "zations",
          zations,
          "error",
          cnse);
      return null;
    }
  }
コード例 #2
0
ファイル: TileSet.java プロジェクト: dosage/nenya
 /**
  * Returns a new tileset that is a clone of this tileset with the image path updated to reference
  * the given path. Useful for configuring a single tileset and then generating additional tilesets
  * with new images with the same configuration.
  */
 public TileSet clone(String imagePath) throws CloneNotSupportedException {
   TileSet dup = (TileSet) clone();
   dup.setImagePath(imagePath);
   return dup;
 }
コード例 #3
0
ファイル: TileManager.java プロジェクト: threerings/nenya
 /**
  * Returns the {@link Tile} object from the specified tileset at the specified index.
  *
  * @param tileSetId the tileset id.
  * @param tileIndex the index of the tile to be retrieved.
  * @return the tile object.
  */
 public Tile getTile(int tileSetId, int tileIndex, TileSet.Colorizer rizer)
     throws NoSuchTileSetException {
   TileSet set = getTileSet(tileSetId);
   return set.getTile(tileIndex, rizer);
 }
コード例 #4
0
ファイル: TileSetTrimmer.java プロジェクト: threerings/nenya
  /**
   * Generates a trimmed tileset image from the supplied source tileset. The source tileset must be
   * configured with an image provider so that the tile images can be obtained. The tile images will
   * be trimmed and a new tileset image generated and written to the <code>destImage</code> output
   * stream argument.
   *
   * @param source the source tileset.
   * @param destImage an output stream to which the new trimmed image will be written.
   * @param tmr a callback object that will be used to inform the caller of the trimmed tile
   *     metrics.
   * @param imgFormat the format in which to write the image file - or if null, use FastImageIO.
   */
  public static void trimTileSet(
      TileSet source, OutputStream destImage, TrimMetricsReceiver tmr, String imgFormat)
      throws IOException {
    int tcount = source.getTileCount();
    BufferedImage[] timgs = new BufferedImage[tcount];

    // these will contain the bounds of the trimmed image in the
    // coordinate system defined by the untrimmed image
    Rectangle[] tbounds = new Rectangle[tcount];

    // compute some tile metrics
    int nextx = 0, maxy = 0;
    for (int ii = 0; ii < tcount; ii++) {
      // extract the image from the original tileset
      try {
        timgs[ii] = source.getRawTileImage(ii);
      } catch (RasterFormatException rfe) {
        throw new IOException(
            "Failed to get tile image "
                + "[tidx="
                + ii
                + ", tset="
                + source
                + ", rfe="
                + rfe
                + "].");
      }

      // figure out how tightly we can trim it
      tbounds[ii] = new Rectangle();
      ImageUtil.computeTrimmedBounds(timgs[ii], tbounds[ii]);

      // let our caller know what we did
      tmr.trimmedTile(
          ii, nextx, 0, tbounds[ii].x, tbounds[ii].y, tbounds[ii].width, tbounds[ii].height);

      // adjust the new tileset image dimensions
      maxy = Math.max(maxy, tbounds[ii].height);
      nextx += tbounds[ii].width;
    }

    // create the new tileset image
    BufferedImage image = null;
    try {
      image = ImageUtil.createCompatibleImage(source.getRawTileSetImage(), nextx, maxy);

    } catch (RasterFormatException rfe) {
      throw new IOException(
          "Failed to create trimmed tileset image "
              + "[wid="
              + nextx
              + ", hei="
              + maxy
              + ", tset="
              + source
              + ", rfe="
              + rfe
              + "].");
    }

    // copy the tile data
    WritableRaster drast = image.getRaster();
    int xoff = 0;
    for (int ii = 0; ii < tcount; ii++) {
      Rectangle tb = tbounds[ii];
      Raster srast = timgs[ii].getRaster().createChild(tb.x, tb.y, tb.width, tb.height, 0, 0, null);
      drast.setRect(xoff, 0, srast);
      xoff += tb.width;
    }

    if (destImage != null) {
      // write out trimmed image
      if (imgFormat == null || FastImageIO.FILE_SUFFIX.equals(imgFormat)) {
        FastImageIO.write(image, destImage);
      } else {
        ImageIO.write(image, imgFormat, destImage);
      }
    }
  }