⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            mipMapSizes = null;
        }

        setFormat(format);
        this.width = width;
        this.height = height;
        this.data = new ArrayList<ByteBuffer>(1);
        this.data.add(data);
        this.mipMapSizes = mipMapSizes;
    }

    /**
     * Constructor instantiates a new <code>Image</code> object. The
     * attributes of the image are defined during construction.
     * 
     * @param type
     *            the data format of the image.
     * @param width
     *            the width of the image.
     * @param height
     *            the height of the image.
     * @param data
     *            the image data.
     */
    public Image(Format format, int width, int height, int depth, ArrayList<ByteBuffer> data) {
        this(format, width, height, depth, data, null);
    }

    /**
     * Constructor instantiates a new <code>Image</code> object. The
     * attributes of the image are defined during construction.
     * 
     * @param type
     *            the data format of the image.
     * @param width
     *            the width of the image.
     * @param height
     *            the height of the image.
     * @param data
     *            the image data.
     */
    public Image(Format format, int width, int height, ByteBuffer data) {
        this(format, width, height, data, null);
    }

    /**
     * <code>setData</code> sets the data that makes up the image. This data
     * is packed into an array of <code>ByteBuffer</code> objects.
     * 
     * @param data
     *            the data that contains the image information.
     */
    public void setData(ArrayList<ByteBuffer> data) {
        this.data = data;
    }

    /**
     * <code>setData</code> sets the data that makes up the image. This data
     * is packed into a single <code>ByteBuffer</code>.
     * 
     * @param data
     *            the data that contains the image information.
     */
    public void setData(ByteBuffer data) {
        this.data = new ArrayList<ByteBuffer>(1);
        this.data.add(data);
    }

    public void addData(ByteBuffer data) {
        if (this.data == null)
            this.data = new ArrayList<ByteBuffer>(1);
        this.data.add(data);
    }

    public void setData(int index, ByteBuffer data) {
        if (index >= 0) {
            while (this.data.size() <= index) {
                this.data.add(null);
            }
            this.data.set(index, data);
        } else {
            throw new IllegalArgumentException("index must be greater than or equal to 0.");
        }
    }

    /**
     * Sets the mipmap sizes stored in this image's data buffer. Mipmaps are
     * stored sequentially, and the first mipmap is the main image data. To
     * specify no mipmaps, pass null and this will automatically be expanded
     * into a single mipmap of the full
     * 
     * @param mipMapSizes
     *            the mipmap sizes array, or null for a single image map.
     */
    public void setMipMapSizes(int[] mipMapSizes) {
        if (mipMapSizes != null && mipMapSizes.length <= 1)
            mipMapSizes = null;

        this.mipMapSizes = mipMapSizes;
    }

    /**
     * <code>setHeight</code> sets the height value of the image. It is
     * typically a good idea to try to keep this as a multiple of 2.
     * 
     * @param height
     *            the height of the image.
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * <code>setDepth</code> sets the depth value of the image. It is
     * typically a good idea to try to keep this as a multiple of 2. This is
     * used for 3d images.
     * 
     * @param depth
     *            the depth of the image.
     */
    public void setDepth(int depth) {
        this.depth = depth;
    }

    /**
     * <code>setWidth</code> sets the width value of the image. It is
     * typically a good idea to try to keep this as a multiple of 2.
     * 
     * @param width
     *            the width of the image.
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * <code>setFormat</code> sets the image format for this image.
     * 
     * @param format
     *            the image format.
     * @throws NullPointerException
     *             if format is null
     * @see Format
     */
    public void setFormat(Format format) {
        if (format == null) {
            throw new NullPointerException("format may not be null.");
        }

        this.format = format;
    }

    /**
     * <code>getFormat</code> returns the image format for this image.
     * 
     * @return the image format.
     * @see Format
     */
    public Format getFormat() {
        return format;
    }

    /**
     * <code>getWidth</code> returns the width of this image.
     * 
     * @return the width of this image.
     */
    public int getWidth() {
        return width;
    }

    /**
     * <code>getHeight</code> returns the height of this image.
     * 
     * @return the height of this image.
     */
    public int getHeight() {
        return height;
    }

    /**
     * <code>getDepth</code> returns the depth of this image (for 3d images).
     * 
     * @return the depth of this image.
     */
    public int getDepth() {
        return depth;
    }

    /**
     * <code>getData</code> returns the data for this image. If the data is
     * undefined, null will be returned.
     * 
     * @return the data for this image.
     */
    public ArrayList<ByteBuffer> getData() {
        return data;
    }

    /**
     * <code>getData</code> returns the data for this image. If the data is
     * undefined, null will be returned.
     * 
     * @return the data for this image.
     */
    public ByteBuffer getData(int index) {
        if (data.size() > index)
            return data.get(index);
        else 
            return null;
    }

    /**
     * Returns whether the image data contains mipmaps.
     * 
     * @return true if the image data contains mipmaps, false if not.
     */
    public boolean hasMipmaps() {
        return mipMapSizes != null;
    }

    /**
     * Returns the mipmap sizes for this image.
     * 
     * @return the mipmap sizes for this image.
     */
    public int[] getMipMapSizes() {
        return mipMapSizes;
    }

    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof Image)) {
            return false;
        }
        Image that = (Image) other;
        if (this.getFormat() != that.getFormat())
            return false;
        if (this.getWidth() != that.getWidth())
            return false;
        if (this.getHeight() != that.getHeight())
            return false;
        if (this.getData() != null && !this.getData().equals(that.getData()))
            return false;
        if (this.getData() == null && that.getData() != null)
            return false;
        if (this.getMipMapSizes() != null
                && !Arrays.equals(this.getMipMapSizes(), that.getMipMapSizes()))
            return false;
        if (this.getMipMapSizes() == null && that.getMipMapSizes() != null)
            return false;

        return true;
    }

    public void write(JMEExporter e) throws IOException {
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(format, "format", Format.RGBA8);
        capsule.write(width, "width", 0);
        capsule.write(height, "height", 0);
        capsule.write(depth, "depth", 0);
        capsule.write(mipMapSizes, "mipMapSizes", null);
        capsule.writeByteBufferArrayList(data, "data", null);
    }

    public void read(JMEImporter e) throws IOException {
        InputCapsule capsule = e.getCapsule(this);
        format = capsule.readEnum("format", Format.class, Format.RGBA8);
        width = capsule.readInt("width", 0);
        height = capsule.readInt("height", 0);
        depth = capsule.readInt("depth", 0);
        mipMapSizes = capsule.readIntArray("mipMapSizes", null);
        data = capsule.readByteBufferArrayList("data", null);
    }

    public Class getClassTag() {
        return this.getClass();
    }

    /**
     * @param format
     *            image data format
     * @return an estimate of the size of a texture on the video card of an
     *         image in the given format. Mostly this is just a guess and could
     *         probably be cleaned up a lot.
     */
    public static int getEstimatedByteSize(Format format) {
        switch (format) {
            case RGBA2:
            case Alpha4:
            case Luminance4:
            case Intensity4:

            case Alpha8:
            case Luminance8:
            case Intensity8:
            case Luminance4Alpha4:
            case Luminance6Alpha2:

            case RGBA_TO_DXT1: // DXT1 = 1/8 * blocksize of 8 (.125 * 8 == 1)
            case NativeDXT1A:
            case RGB_TO_DXT1:
            case NativeDXT1:
                return 1;

            case R3G3B2: // (usually forced to 2 byte by the card)
            case RGB4:
            case RGB5:
            case RGBA4:
            case RGB5A1:

            case Alpha12:
            case Luminance12:
            case Intensity12:

            case Alpha16:
            case Alpha16F:
            case Luminance16:
            case Luminance16F:
            case Intensity16:
            case Intensity16F:
            case Luminance8Alpha8:
            case Luminance12Alpha4:

            case RGBA_TO_DXT3: // DXT3,5 = 1/8 * blocksize of 16 (.125 * 16 == 2)
            case NativeDXT3:
            case RGBA_TO_DXT5:
            case NativeDXT5:
            case Depth16:
                return 2;
                
            case RGB8:
            case Luminance12Alpha12:
            case Depth24:
                return 3;
                
            case RGBA8:
            case RGB10A2:
            case RGB10:
            case Alpha32F:
            case Luminance32F:
            case Intensity32F:
            case Luminance16Alpha16:
            case LuminanceAlpha16F:
            case Depth32:
                return 4;
                
            case RGB12:
                return 5;
                
            case RGBA12:
            case RGB16:
            case RGB16F:
                return 6;
                
            case RGBA16:
            case RGBA16F:
            case LuminanceAlpha32F:
                return 8;
                
            case RGB32F:
                return 12;
                
            case RGBA32F:
                return 16;
        }
        throw new IllegalArgumentException("unknown image format type: "+format);
    }
}

⌨️ 快捷键说明

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