public static void resizeToPowerOf2(Image image){ BufferedImage original = ImageToAwt.convert(image, false, true, 0); int potWidth = FastMath.nearestPowerOfTwo(image.getWidth()); int potHeight = FastMath.nearestPowerOfTwo(image.getHeight()); int potSize = Math.max(potWidth, potHeight); BufferedImage scaled = scaleDown(original, potSize, potSize); AWTLoader loader = new AWTLoader(); Image output = loader.load(scaled, false); image.setWidth(potSize); image.setHeight(potSize); image.setDepth(0); image.setData(output.getData(0)); image.setFormat(output.getFormat()); image.setMipMapSizes(null); }
super(); int width = images.get(0).getWidth(); int height = images.get(0).getHeight(); Format format = images.get(0).getFormat(); ColorSpace colorSpace = images.get(0).getColorSpace(); int[] mipMapSizes = images.get(0).getMipMapSizes(); Image arrayImage = new Image(format, width, height, null, colorSpace); arrayImage.setMipMapSizes(mipMapSizes); if (img.getHeight() != height || img.getWidth() != width) { throw new IllegalArgumentException("Images in texture array must have same dimensions"); if (img.getFormat() != format) { throw new IllegalArgumentException("Images in texture array must have same format"); if (!Arrays.equals(mipMapSizes, img.getMipMapSizes())) { throw new IllegalArgumentException("Images in texture array must have same mipmap sizes"); arrayImage.addData(img.getData(0));
/** * Called by the Renderer when a texture has been set. * * @param image The image that was set * @param wasSwitched If true, the texture has required a state switch */ public void onTextureUse(Image image, boolean wasSwitched){ assert image.getId() >= 1; if( !enabled ) return; if (!texturesUsed.containsKey(image.getId())) texturesUsed.put(image.getId(), null); if (wasSwitched) numTextureBinds ++; }
private static void checkImage(Image image) { // if (image.getDepth() != 1) // throw new IllegalArgumentException("3D/Array images not allowed"); if (image.getWidth() != image.getHeight()) { throw new IllegalArgumentException("Image width and height must be the same"); } if (image.getMultiSamples() != 1) { throw new IllegalArgumentException("Multisample textures not allowed"); } }
@Override 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; if (this.getMultiSamples() != that.getMultiSamples()) return false; return true; }
/** * @param multiSamples Set the number of samples to use for this image, * setting this to a value higher than 1 turns this image/texture * into a multisample texture (on OpenGL3.1 and higher). */ public void setMultiSamples(int multiSamples) { if (multiSamples <= 0) throw new IllegalArgumentException("multiSamples must be > 0"); if (getData(0) != null) throw new IllegalArgumentException("Cannot upload data as multisample texture"); if (hasMipmaps()) throw new IllegalArgumentException("Multisample textures do not support mipmaps"); this.multiSamples = multiSamples; }
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-mappped images is not supported"); if (image.getMultiSamples() > 1) { throw new UnsupportedOperationException("Updating multisampled images is not supported"); Image.Format jmeFormat = image.getFormat(); boolean getSrgbFormat = image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb; GLImageFormat oglFormat = getImageFormatWithError(jmeFormat, getSrgbFormat); data = image.getData(index); data.limit(data.capacity()); gl.glTexSubImage2D(target, 0, x, y, image.getWidth(), image.getHeight(), oglFormat.format, oglFormat.dataType, data);
int texId = img.getId(); if (texId == -1) { img.setId(texId); objManager.registerObject(img); int target = convertTextureType(type, img.getMultiSamples(), -1); bindTextureAndUnit(target, img, unit); int imageSamples = img.getMultiSamples(); if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) { img.setMipmapsGenerated(true); } else { if (img.hasMipmaps()) { gl.glTexParameteri(target, GL2.GL_TEXTURE_MAX_LEVEL, img.getMipMapSizes().length - 1); } else { if (img.isGeneratedMipmapsRequired() || img.hasMipmaps()) { throw new RendererException("Multisample textures do not support mipmaps"); if (img.getFormat().isDepthFormat()) { img.setMultiSamples(Math.min(limits.get(Limits.DepthTextureSamples), imageSamples)); } else { img.setMultiSamples(Math.min(limits.get(Limits.ColorTextureSamples), imageSamples));
dualEyeTex.setMagFilter(Texture.MagFilter.Bilinear); logger.config("Dual eye texture "+dualEyeTex.getName()+" ("+dualEyeTex.getImage().getId()+")"); logger.config(" Type: "+dualEyeTex.getType()); logger.config(" Size: "+dualEyeTex.getImage().getWidth()+"x"+dualEyeTex.getImage().getHeight()); logger.config(" Image depth: "+dualEyeTex.getImage().getDepth()); logger.config(" Image format: "+dualEyeTex.getImage().getFormat()); logger.config(" Image color space: "+dualEyeTex.getImage().getColorSpace());
public MipMapImageRaster(Image image, int slice) { this.image = image; this.slice = slice; this.buffer = image.getData(slice); this.codec = ImageCodec.lookup(image.getFormat()); if (image.hasMipmaps()) { int nbMipMap = image.getMipMapSizes().length; this.width = new int[nbMipMap]; this.height = new int[nbMipMap]; this.offsets = new int[nbMipMap]; for (int i = 0; i < nbMipMap; i++) { width[i] = Math.max(1, image.getWidth() >> i); height[i] = Math.max(1, image.getHeight() >> i); if (i > 0) { offsets[i] = image.getMipMapSizes()[i - 1] + offsets[i - 1]; } } } else { throw new IllegalArgumentException("Image must have MipMapSizes initialized."); } if (codec instanceof ByteAlignedImageCodec || codec instanceof ByteOffsetImageCodec) { this.temp = new byte[codec.bpp]; } else { this.temp = null; } }
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));
Image cubeImage = new Image(format, leftImg.getWidth(), leftImg.getHeight(), null, ColorSpace.Linear); cubeImage.addData(rightImg.getData(0)); cubeImage.addData(leftImg.getData(0)); cubeImage.addData(upImg.getData(0)); cubeImage.addData(downImg.getData(0)); cubeImage.addData(backImg.getData(0)); cubeImage.addData(frontImg.getData(0)); cubeImage.setMipMapSizes(rightImg.getMipMapSizes());
public static void createData(Image image, boolean mipmaps){ int bpp = image.getFormat().getBitsPerPixel(); int w = image.getWidth(); int h = image.getHeight(); if (!mipmaps){ image.setData(BufferUtils.createByteBuffer(w*h*bpp/8)); return; } int expectedMipmaps = 1 + (int) Math.ceil(Math.log(Math.max(h, w)) / LOG2); int[] mipMapSizes = new int[expectedMipmaps]; int total = 0; for (int i = 0; i < mipMapSizes.length; i++){ int size = (w * h * bpp) / 8; total += size; mipMapSizes[i] = size; w /= 2; h /= 2; } image.setMipMapSizes(mipMapSizes); image.setData(BufferUtils.createByteBuffer(total)); }
/** * Inits the mip maps of a cube map witht he given number of mip maps * @param nbMipMaps the number of mip maps to initialize */ public void initMipMaps(int nbMipMaps) { int maxMipMap = (int) (Math.log(image.getWidth()) / Math.log(2) + 1); if (nbMipMaps > maxMipMap) { throw new IllegalArgumentException("Max mip map number for a " + image.getWidth() + "x" + image.getHeight() + " cube map is " + maxMipMap); } sizes = new int[nbMipMaps]; int totalSize = 0; for (int i = 0; i < nbMipMaps; i++) { int size = (int) pow(2, maxMipMap - 1 - i); sizes[i] = size * size * image.getFormat().getBitsPerPixel() / 8; totalSize += sizes[i]; } image.setMipMapSizes(sizes); image.getData().clear(); for (int i = 0; i < 6; i++) { image.addData(BufferUtils.createByteBuffer(totalSize)); } mipMapRaster = new MipMapImageRaster(image, 0); } }
/** * This method computes the final UV coordinates for the image (after it * is combined with other images and drawed on the result image). * * @param totalImageWidth * the result image width * @param totalImageHeight * the result image height * @param xPos * the most left x coordinate of the image * @param yPos * the most top y coordinate of the image * @param result * a vector where the result is stored */ public void computeFinalUVCoordinates(int totalImageWidth, int totalImageHeight, int xPos, int yPos, Vector2f[] result) { for (int i = 0; i < 3; ++i) { result[i] = new Vector2f(); result[i].x = xPos / (float) totalImageWidth + uv[i].x * (image.getWidth() / (float) totalImageWidth); result[i].y = yPos / (float) totalImageHeight + uv[i].y * (image.getHeight() / (float) totalImageHeight); } }
public static void flipImage(Image img, int index){ if (img.getFormat().isCompressed()) throw new UnsupportedOperationException("Flipping compressed " + "images is unsupported."); int w = img.getWidth(); int h = img.getHeight(); int halfH = h / 2; // bytes per pixel int bpp = img.getFormat().getBitsPerPixel() / 8; int scanline = w * bpp; ByteBuffer data = img.getData(index); ByteBuffer temp = BufferUtils.createByteBuffer(scanline); data.rewind(); for (int y = 0; y < halfH; y++){ int oppY = h - y - 1; // read in scanline data.position(y * scanline); data.limit(data.position() + scanline); temp.rewind(); temp.put(data); } }
private void checkSetTexture(Texture tex, boolean depth){ Image img = tex.getImage(); if (img == null) throw new IllegalArgumentException("Texture not initialized with RTT."); if (depth && !img.getFormat().isDepthFormat()) throw new IllegalArgumentException("Texture image format must be depth."); else if (!depth && img.getFormat().isDepthFormat()) throw new IllegalArgumentException("Texture image format must be color/luminance."); // check that resolution matches texture resolution if (width != img.getWidth() || height != img.getHeight()) throw new IllegalArgumentException("Texture image resolution " + "must match FB resolution"); if (samples != tex.getImage().getMultiSamples()) throw new IllegalStateException("Texture samples must match framebuffer samples"); }
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; }
private Image convertImageToAwt(Image source) { //use awt dependent classes without actual dependency via reflection try { Class clazz = Class.forName("jme3tools.converters.ImageToAwt"); if (clazz == null) { return null; } Image newImage = new Image(format, source.getWidth(), source.getHeight(), BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4), null, ColorSpace.Linear); clazz.getMethod("convert", Image.class, Image.class).invoke(clazz.newInstance(), source, newImage); return newImage; } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (IllegalArgumentException ex) { } catch (InvocationTargetException ex) { } catch (NoSuchMethodException ex) { } catch (SecurityException ex) { } catch (ClassNotFoundException ex) { } return null; }