Example usage for com.google.common.math IntMath isPowerOfTwo

List of usage examples for com.google.common.math IntMath isPowerOfTwo

Introduction

In this page you can find the example usage for com.google.common.math IntMath isPowerOfTwo.

Prototype

public static boolean isPowerOfTwo(int x) 

Source Link

Document

Returns true if x represents a power of two.

Usage

From source file:org.terasology.world.block.loader.TileLoader.java

@Override
public TileData load(Module module, InputStream stream, List<URL> urls, List<URL> deltas) throws IOException {
    BufferedImage image = ImageIO.read(stream);
    if (!IntMath.isPowerOfTwo(image.getHeight()) || !(image.getWidth() == image.getHeight())) {
        throw new IOException("Invalid tile - must be square with power-of-two sides");
    }/*from  w ww  . j a v  a  2s  .  co m*/
    return new TileData(image);
}

From source file:org.terasology.world.block.tiles.TileFormat.java

@Override
public TileData load(ResourceUrn resourceUrn, List<AssetDataFile> list) throws IOException {
    boolean auto = list.get(0).getPath().contains("auto");
    try (InputStream stream = list.get(0).openStream()) {
        BufferedImage image = ImageIO.read(stream);
        if (!IntMath.isPowerOfTwo(image.getHeight()) || !(image.getWidth() == image.getHeight())) {
            throw new IOException("Invalid tile - must be square with power-of-two sides");
        }/*from   w  w  w . j a v  a  2s. c  o  m*/
        return new TileData(image, auto);
    }
}

From source file:org.terasology.rendering.assets.texture.TextureData.java

public TextureData(int width, int height, int depth, ByteBuffer[] mipmaps, Texture.WrapMode wrapMode,
        Texture.FilterMode filterMode) {
    this(width, height, wrapMode, filterMode);
    this.depth = depth;
    this.type = Texture.Type.TEXTURE3D;

    this.data = Arrays.copyOf(mipmaps, mipmaps.length);

    if (data.length > 0) {
        if (width <= 0 || height <= 0 || depth <= 0) {
            throw new IllegalArgumentException("Width, height and depth must be positive");
        }//  w  w w.  j  a  va  2  s  . c o  m
        if (mipmaps.length == 0) {
            throw new IllegalArgumentException("Must supply at least one mipmap");
        }
        if (mipmaps[0].limit() != width * height * depth * BYTES_PER_PIXEL) {
            throw new IllegalArgumentException(
                    "Texture data size incorrect, must be a set of RGBA values for each pixel (width * height * depth)");
        }
        if (mipmaps.length > 1 && !(IntMath.isPowerOfTwo(width) && IntMath.isPowerOfTwo(height)
                && IntMath.isPowerOfTwo(depth))) {
            throw new IllegalArgumentException(
                    "Texture width, height and depth must be powers of 2 for mipmapping");
        }
        for (int i = 1; i < mipmaps.length; ++i) {
            int mipWidth = width >> i;
            int mipHeight = height >> i;
            int mipDepth = depth >> i;
            if (mipWidth * mipHeight * mipDepth * BYTES_PER_PIXEL != mipmaps[i].limit()) {
                throw new IllegalArgumentException("Mipmap has wrong dimensions");
            }
        }
    }
}

From source file:org.terasology.rendering.assets.texture.TextureData.java

public TextureData(int width, int height, ByteBuffer[] mipmaps, Texture.WrapMode wrapMode,
        Texture.FilterMode filterMode) {
    this(width, height, wrapMode, filterMode);
    this.depth = 1;
    this.type = Texture.Type.TEXTURE2D;

    this.data = Arrays.copyOf(mipmaps, mipmaps.length);

    if (data.length > 0) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("Width and height must be positive");
        }/*from  w ww  . j a  v a 2 s  .c o  m*/
        if (mipmaps.length == 0) {
            throw new IllegalArgumentException("Must supply at least one mipmap");
        }
        if (mipmaps[0].limit() != width * height * BYTES_PER_PIXEL) {
            throw new IllegalArgumentException(
                    "Texture data size incorrect, must be a set of RGBA values for each pixel (width * height)");
        }
        if (mipmaps.length > 1 && !(IntMath.isPowerOfTwo(width) && IntMath.isPowerOfTwo(height))) {
            throw new IllegalArgumentException("Texture width and height must be powers of 2 for mipmapping");
        }
        for (int i = 1; i < mipmaps.length; ++i) {
            int mipWidth = width >> i;
            int mipHeight = height >> i;
            if (mipWidth * mipHeight * BYTES_PER_PIXEL != mipmaps[i].limit()) {
                throw new IllegalArgumentException("Mipmap has wrong dimensions");
            }
        }
    }
}

From source file:org.terasology.world.block.loader.WorldAtlasImpl.java

private boolean checkTile(TileData tile) {
    return tile.getImage().getWidth() == tile.getImage().getHeight()
            && IntMath.isPowerOfTwo(tile.getImage().getWidth());
}

From source file:org.terasology.world.block.tiles.WorldAtlasImpl.java

private boolean checkTile(BlockTile tile) {
    return tile.getImage().getWidth() == tile.getImage().getHeight()
            && IntMath.isPowerOfTwo(tile.getImage().getWidth());
}