Textures.java :  » Java-3D » shapes3d4processing » shapes3d » utils » Java Open Source

Java Open Source » Java 3D » shapes3d4processing 
shapes3d4processing » shapes3d » utils » Textures.java
/*
  Part of the Shapes 3D library for Processing 
    http://www.lagers.org.uk

  Copyright (c) 2009 Peter Lager

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
 */

package shapes3d.utils;

import java.util.HashMap;

import processing.core.PApplet;
import processing.core.PImage;

/**
 * This class is used to load bitmap files and create images. <br>
 * 
 * Although there maybe multiple requests for a particular bitmap file only
 * one PImage is created for each file.
 * 
 * @author Peter Lager
 *
 */
public class Textures {

  private static HashMap<String, PImage> textures = new HashMap<String, PImage>();
  
  /**
   * Load an bitmap file return a reference to the PImage created.
   * 
   * @param app
   * @param filename
   * @return null if the file does not exist else the PImage object
   */
  public static PImage loadImage(PApplet app, String filename){
    if(textures.containsKey(filename)){
      return textures.get(filename);
    }
    PImage image = app.loadImage(filename);
    if(image != null){
      textures.put(filename, image);
    }
    else
      PApplet.println("Unable to load image from file '" + filename+"'");
    return image;
  }
  
  /**
   * Load images from multiple files
   * @param app
   * @param filename an array of filenames
   * @return an array of images
   */
  public static PImage[] loadImage(PApplet app, String[] filename){
    PImage[] images = new PImage[filename.length];
    for(int i = 0; i < images.length; i++)
      images[i] = loadImage(app, filename[i]);
    return images;
  }
  
  /**
   * Make multiple images from a given image.
   * 
   * @param app
   * @param img the tiled image
   * @param nCols number of tiles across
   * @param nRows number of tiles down
   * @return an array of images (tiles)
   */
  public static PImage[][] makeTiles(PApplet app, PImage img, int nCols, int nRows){
    PImage[][] imageTiles = new PImage[nCols][nRows];
    int tileW = img.width / nCols;
    int tileH = img.height / nRows;
    for(int y = 0; y < nRows; y++){
      for(int x = 0; x < nCols; x++){
        imageTiles[x][y] = app.createImage(tileW, tileH, PApplet.ARGB);
        imageTiles[x][y].copy(img, x * tileW, y * tileH, tileW, tileH, 0, 0, tileW, tileH);
      }
    }
    return imageTiles;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.