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

The following examples show how to use com.jme3.texture.Image#getColorSpace() . 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: TestImageRaster.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Image convertImage(Image image, Format newFormat) {
    int width = image.getWidth();
    int height = image.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer( (int)Math.ceil(newFormat.getBitsPerPixel() / 8.0) * width * height);
    Image convertedImage = new Image(newFormat, width, height, data,null, image.getColorSpace());
    
    ImageRaster sourceReader = ImageRaster.create(image);
    ImageRaster targetWriter = ImageRaster.create(convertedImage);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            ColorRGBA color = sourceReader.getPixel(x, y);
            targetWriter.setPixel(x, y, color);
        }
    }
    
    return convertedImage;
}
 
Example 2
Source File: SkyFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a cube-mapped sky using six textures.
 *
 * @param assetManager from which to load materials
 * @param west texture for the western face of the cube
 * @param east texture for the eastern face of the cube
 * @param north texture for the northern face of the cube
 * @param south texture for the southern face of the cube
 * @param up texture for the top face of the cube
 * @param down texture for the bottom face of the cube
 * @param normalScale The normal scale is multiplied by the 3D normal to get
 * a texture coordinate. Use Vector3f.UNIT_XYZ to not apply and
 * transformation to the normal.
 * @param sphereRadius the sky sphere's radius: for the sky to be visible,
 * its radius must fall between the near and far planes of the camera's
 * frustum
 * @return a new spatial representing the sky, ready to be attached to the
 * scene graph
 */
public static Spatial createSky(AssetManager assetManager, Texture west,
        Texture east, Texture north, Texture south, Texture up,
        Texture down, Vector3f normalScale, float sphereRadius) {

    Image westImg = west.getImage();
    Image eastImg = east.getImage();
    Image northImg = north.getImage();
    Image southImg = south.getImage();
    Image upImg = up.getImage();
    Image downImg = down.getImage();

    checkImagesForCubeMap(westImg, eastImg, northImg, southImg, upImg, downImg);

    Image cubeImage = new Image(westImg.getFormat(), westImg.getWidth(), westImg.getHeight(), null, westImg.getColorSpace());

    cubeImage.addData(westImg.getData(0));
    cubeImage.addData(eastImg.getData(0));
    cubeImage.addData(downImg.getData(0));
    cubeImage.addData(upImg.getData(0));
    cubeImage.addData(southImg.getData(0));
    cubeImage.addData(northImg.getData(0));
    
    TextureCubeMap cubeMap = new TextureCubeMap(cubeImage);
    return createSky(assetManager, cubeMap, normalScale, EnvMapType.CubeMap, sphereRadius);
}
 
Example 3
Source File: DefaultImageRaster.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DefaultImageRaster(Image image, int slice, int mipMapLevel, boolean convertToLinear) {
    int[] mipMapSizes = image.getMipMapSizes();
    int availableMips = mipMapSizes != null ? mipMapSizes.length : 1;
    
    if (mipMapLevel >= availableMips) {
        throw new IllegalStateException("Cannot create image raster for mipmap level #" + mipMapLevel + ". "
                                      + "Image only has " + availableMips + " mipmap levels.");
    }
    
    if (image.hasMipmaps()) {
        this.width  = Math.max(1, image.getWidth()  >> mipMapLevel);
        this.height = Math.max(1, image.getHeight() >> mipMapLevel);
        
        int mipOffset = 0;
        for (int i = 0; i < mipMapLevel; i++) {
            mipOffset += mipMapSizes[i];
        }
        
        this.offset = mipOffset;
    } else {
        this.width = image.getWidth();
        this.height = image.getHeight();
        this.offset = 0;
    }
    
    this.image = image;
    this.slice = slice;
    
    // Conversion to linear only needed if image's color space is sRGB.
    this.convertToLinear = convertToLinear && image.getColorSpace() == ColorSpace.sRGB;
    
    this.buffer = image.getData(slice);
    this.codec = ImageCodec.lookup(image.getFormat());
    
    if (codec instanceof ByteAlignedImageCodec || codec instanceof ByteOffsetImageCodec) {
        this.temp = new byte[codec.bpp];
    } else {
        this.temp = null;
    }
}
 
Example 4
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 5
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 6
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 7
Source File: MipMapGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Image scaleImage(Image inputImage, int outputWidth, int outputHeight) {
    int size = outputWidth * outputHeight * inputImage.getFormat().getBitsPerPixel() / 8;
    ByteBuffer buffer = BufferUtils.createByteBuffer(size);
    Image outputImage = new Image(inputImage.getFormat(), 
                                  outputWidth, 
                                  outputHeight, 
                                  buffer, 
                                  inputImage.getColorSpace());
    
    ImageRaster input = ImageRaster.create(inputImage, 0, 0, false);
    ImageRaster output = ImageRaster.create(outputImage, 0, 0, false);
    
    float xRatio = ((float)(input.getWidth()  - 1)) / output.getWidth();
    float yRatio = ((float)(input.getHeight() - 1)) / output.getHeight();

    ColorRGBA outputColor = new ColorRGBA(0, 0, 0, 0);
    ColorRGBA bottomLeft = new ColorRGBA();
    ColorRGBA bottomRight = new ColorRGBA();
    ColorRGBA topLeft = new ColorRGBA();
    ColorRGBA topRight = new ColorRGBA();
    
    for (int y = 0; y < outputHeight; y++) {
        for (int x = 0; x < outputWidth; x++) {
            float x2f = x * xRatio;
            float y2f = y * yRatio;
            
            int x2 = (int)x2f;
            int y2 = (int)y2f;

            input.getPixel(x2,     y2,     bottomLeft);
            input.getPixel(x2 + 1, y2,     bottomRight);
            input.getPixel(x2,     y2 + 1, topLeft);
            input.getPixel(x2 + 1, y2 + 1, topRight);

            outputColor.set(bottomLeft).addLocal(bottomRight)
                       .addLocal(topLeft).addLocal(topRight);
            outputColor.multLocal(1f / 4f);
            output.setPixel(x, y, outputColor);
        }
    }
    return outputImage;
}