Java Code Examples for com.jme3.texture.Image#getDepth()

The following examples show how to use com.jme3.texture.Image#getDepth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void uploadTexture(Image image,
                          int target,
                          int index,
                          boolean linearizeSrgb) {

    boolean getSrgbFormat = image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    Image.Format jmeFormat = image.getFormat();
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);

    ByteBuffer data = null;
    int sliceCount = 1;
    
    if (index >= 0) {
        data = image.getData(index);
    }
    
    if (image.getData() != null && image.getData().size() > 0) {
        sliceCount = image.getData().size();
    }

    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    
    int[] mipSizes = image.getMipMapSizes();
    int pos = 0;
    // TODO: Remove unnecessary allocation
    if (mipSizes == null) {
        if (data != null) {
            mipSizes = new int[]{data.capacity()};
        } else {
            mipSizes = new int[]{width * height * jmeFormat.getBitsPerPixel() / 8};
        }
    }

    int samples = image.getMultiSamples();
    
    // For OGL3 core: setup texture swizzle.
    if (oglFormat.swizzleRequired) {
        setupTextureSwizzle(target, jmeFormat);
    }

    for (int i = 0; i < mipSizes.length; i++) {
        int mipWidth = Math.max(1, width >> i);
        int mipHeight = Math.max(1, height >> i);
        int mipDepth = Math.max(1, depth >> i);

        if (data != null) {
            data.position(pos);
            data.limit(pos + mipSizes[i]);
        }

        uploadTextureLevel(oglFormat, target, i, index, sliceCount, mipWidth, mipHeight, mipDepth, samples, data);

        pos += mipSizes[i];
    }
}
 
Example 2
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @deprecated Use uploadSubTexture(int target,  Image src, int index,int targetX, int targetY,int srcX,int srcY,  int areaWidth,int areaHeight, boolean linearizeSrgb) 
 */
@Deprecated
public void uploadSubTexture(Image image, int target, int index, int x, int y, boolean linearizeSrgb) {
    if (target != GL.GL_TEXTURE_2D || image.getDepth() > 1) {
        throw new UnsupportedOperationException("Updating non-2D texture is not supported");
    }
    
    if (image.getMipMapSizes() != null) {
        throw new UnsupportedOperationException("Updating mip-mapped images is not supported");
    }
    
    if (image.getMultiSamples() > 1) {
        throw new UnsupportedOperationException("Updating multisampled images is not supported");
    }
    
    Image.Format jmeFormat = image.getFormat();
    
    if (jmeFormat.isCompressed()) {
        throw new UnsupportedOperationException("Updating compressed images is not supported");
    } else if (jmeFormat.isDepthFormat()) {
        throw new UnsupportedOperationException("Updating depth images is not supported");
    }
    
    boolean getSrgbFormat = image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);
    
    ByteBuffer data = null;
    
    if (index >= 0) {
        data = image.getData(index);
    }
    
    if (data == null) {
        throw new IndexOutOfBoundsException("The image index " + index + " is not valid for the given image");
    }

    data.position(0);
    data.limit(data.capacity());
    
    gl.glTexSubImage2D(target, 0, x, y, image.getWidth(), image.getHeight(), 
                       oglFormat.format, oglFormat.dataType, data);
}
 
Example 3
Source File: TextureUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void uploadSubTexture(int target, Image src, int index, int targetX, int targetY, int areaX, int areaY, int areaWidth, int areaHeight, boolean linearizeSrgb) {
    if (target != GL.GL_TEXTURE_2D || src.getDepth() > 1) {
        throw new UnsupportedOperationException("Updating non-2D texture is not supported");
    }

    if (src.getMipMapSizes() != null) {
        throw new UnsupportedOperationException("Updating mip-mappped images is not supported");
    }

    if (src.getMultiSamples() > 1) {
        throw new UnsupportedOperationException("Updating multisampled images is not supported");
    }

    Image.Format jmeFormat = src.getFormat();

    if (jmeFormat.isCompressed()) {
        throw new UnsupportedOperationException("Updating compressed images is not supported");
    } else if (jmeFormat.isDepthFormat()) {
        throw new UnsupportedOperationException("Updating depth images is not supported");
    }

    boolean getSrgbFormat = src.getColorSpace() == ColorSpace.sRGB && linearizeSrgb;
    GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat);

    ByteBuffer data = src.getData(index);

    if (data == null) {
        throw new IndexOutOfBoundsException("The image index " + index + " is not valid for the given image");
    }

    int Bpp = src.getFormat().getBitsPerPixel() / 8;

    int srcWidth = src.getWidth();
    int cpos = data.position();
    int skip = areaX;
    skip += areaY * srcWidth;
    skip *= Bpp;

    data.position(skip);

    boolean needsStride = srcWidth != areaWidth;

    if (needsStride && (!supportUnpackRowLength)) { // doesn't support stride, copy row by row (slower).
        for (int i = 0; i < areaHeight; i++) {
            data.position(skip + (srcWidth * Bpp * i));
            gl.glTexSubImage2D(target, 0, targetX, targetY + i, areaWidth, 1, oglFormat.format, oglFormat.dataType, data);
        }
    } else {
        if (needsStride)
            gl2.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH, srcWidth);
        gl.glTexSubImage2D(target, 0, targetX, targetY, areaWidth, areaHeight, oglFormat.format, oglFormat.dataType, data);
        if (needsStride)
            gl2.glPixelStorei(GL.GL_UNPACK_ROW_LENGTH, 0);
    }
    data.position(cpos);

}
 
Example 4
Source File: TextureBlenderAWT.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Image blend(Image image, Image baseImage, BlenderContext blenderContext) {
    this.prepareImagesForBlending(image, baseImage);

    float[] pixelColor = new float[] { color[0], color[1], color[2], 1.0f };
    Format format = image.getFormat();

    PixelInputOutput basePixelIO = null, pixelReader = PixelIOFactory.getPixelIO(format);
    TexturePixel basePixel = null, pixel = new TexturePixel();
    float[] materialColor = this.materialColor;
    if (baseImage != null) {
        basePixelIO = PixelIOFactory.getPixelIO(baseImage.getFormat());
        materialColor = new float[this.materialColor.length];
        basePixel = new TexturePixel();
    }

    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    if (depth == 0) {
        depth = 1;
    }
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);

    float[] resultPixel = new float[4];
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        ByteBuffer newData = BufferUtils.createByteBuffer(width * height * 4);

        int dataIndex = 0, x = 0, y = 0, index = 0;
        while (index < data.limit()) {
            // getting the proper material color if the base texture is applied
            if (basePixelIO != null) {
                basePixelIO.read(baseImage, dataLayerIndex, basePixel, x, y);
                basePixel.toRGBA(materialColor);
            }

            // reading the current texture's pixel
            pixelReader.read(image, dataLayerIndex, pixel, index);
            index += image.getFormat().getBitsPerPixel() >> 3;
            pixel.toRGBA(pixelColor);
            if (negateTexture) {
                pixel.negate();
            }

            this.blendPixel(resultPixel, materialColor, pixelColor, blenderContext);
            newData.put(dataIndex++, (byte) (resultPixel[0] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[1] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[2] * 255.0f));
            newData.put(dataIndex++, (byte) (pixelColor[3] * 255.0f));

            ++x;
            if (x >= width) {
                x = 0;
                ++y;
            }
        }
        dataArray.add(newData);
    }

    Image result = depth > 1 ? new Image(Format.RGBA8, width, height, depth, dataArray) : new Image(Format.RGBA8, width, height, dataArray.get(0));
    if (image.getMipMapSizes() != null) {
        result.setMipMapSizes(image.getMipMapSizes().clone());
    }
    return result;
}
 
Example 5
Source File: TextureBlenderLuminance.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Image blend(Image image, Image baseImage, BlenderContext blenderContext) {
    this.prepareImagesForBlending(image, baseImage);

    Format format = image.getFormat();
    PixelInputOutput basePixelIO = null;
    TexturePixel basePixel = null;
    float[] materialColor = this.materialColor;
    if (baseImage != null) {
        basePixelIO = PixelIOFactory.getPixelIO(baseImage.getFormat());
        materialColor = new float[this.materialColor.length];
        basePixel = new TexturePixel();
    }

    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    if (depth == 0) {
        depth = 1;
    }
    ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);

    float[] resultPixel = new float[4];
    float[] tinAndAlpha = new float[2];
    for (int dataLayerIndex = 0; dataLayerIndex < depth; ++dataLayerIndex) {
        ByteBuffer data = image.getData(dataLayerIndex);
        data.rewind();
        ByteBuffer newData = BufferUtils.createByteBuffer(width * height * 4);

        int dataIndex = 0, x = 0, y = 0;
        while (data.hasRemaining()) {
            // getting the proper material color if the base texture is applied
            if (basePixelIO != null) {
                basePixelIO.read(baseImage, dataLayerIndex, basePixel, x, y);
                basePixel.toRGBA(materialColor);
            }

            this.getTinAndAlpha(data, format, negateTexture, tinAndAlpha);
            this.blendPixel(resultPixel, materialColor, color, tinAndAlpha[0], blendFactor, blendType, blenderContext);
            newData.put(dataIndex++, (byte) (resultPixel[0] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[1] * 255.0f));
            newData.put(dataIndex++, (byte) (resultPixel[2] * 255.0f));
            newData.put(dataIndex++, (byte) (tinAndAlpha[1] * 255.0f));

            ++x;
            if (x >= width) {
                x = 0;
                ++y;
            }
        }
        dataArray.add(newData);
    }

    Image result = depth > 1 ? new Image(Format.RGBA8, width, height, depth, dataArray) : new Image(Format.RGBA8, width, height, dataArray.get(0));
    if (image.getMipMapSizes() != null) {
        result.setMipMapSizes(image.getMipMapSizes().clone());
    }
    return result;
}
 
Example 6
Source File: CombinedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * This method determines if the given texture has no alpha channel.
 * 
 * @param texture
 *            the texture to check for alpha channel
 * @return <b>true</b> if the texture has no alpha channel and <b>false</b>
 *         otherwise
 */
private boolean isWithoutAlpha(TextureData textureData, BlenderContext blenderContext) {
    ColorBand colorBand = new ColorBand(textureData.textureStructure, blenderContext);
    if (!colorBand.hasTransparencies()) {
        int type = ((Number) textureData.textureStructure.getFieldValue("type")).intValue();
        if (type == TextureHelper.TEX_MAGIC) {
            return true;
        }
        if (type == TextureHelper.TEX_VORONOI) {
            int voronoiColorType = ((Number) textureData.textureStructure.getFieldValue("vn_coltype")).intValue();
            return voronoiColorType != 0;// voronoiColorType == 0:
                                         // intensity, voronoiColorType
                                         // != 0: col1, col2 or col3
        }
        if (type == TextureHelper.TEX_CLOUDS) {
            int sType = ((Number) textureData.textureStructure.getFieldValue("stype")).intValue();
            return sType == 1;// sType==0: without colors, sType==1: with
                              // colors
        }

        // checking the flat textures for alpha values presence
        if (type == TextureHelper.TEX_IMAGE) {
            Image image = textureData.texture.getImage();
            switch (image.getFormat()) {
                case BGR8:
                case DXT1:
                case Luminance16:
                case Luminance16F:
                case Luminance32F:
                case Luminance8:
                case RGB10:
                case RGB111110F:
                case RGB16:
                case RGB16F:
                case RGB32F:
                case RGB565:
                case RGB8:
                    return true;// these types have no alpha by definition
                case ABGR8:
                case DXT3:
                case DXT5:
                case Luminance16Alpha16:
                case Luminance16FAlpha16F:
                case Luminance8Alpha8:
                case RGBA16:
                case RGBA16F:
                case RGBA32F:
                case RGBA8:// with these types it is better to make sure if the texture is or is not transparent
                    PixelInputOutput pixelInputOutput = PixelIOFactory.getPixelIO(image.getFormat());
                    TexturePixel pixel = new TexturePixel();
                    int depth = image.getDepth() == 0 ? 1 : image.getDepth();
                    for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
                        for (int x = 0; x < image.getWidth(); ++x) {
                            for (int y = 0; y < image.getHeight(); ++y) {
                                pixelInputOutput.read(image, layerIndex, pixel, x, y);
                                if (pixel.alpha < 1.0f) {
                                    return false;
                                }
                            }
                        }
                    }
                    return true;
            }
        }
    }
    return false;
}