@Override public int getHeight() { return image.getHeight(); } }
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"); } }
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; }
public RenderImageJme(Texture2D texture){ if (texture.getImage() == null) { throw new IllegalArgumentException("texture.getImage() cannot be null"); } this.texture = texture; this.image = texture.getImage(); width = image.getWidth(); height = image.getHeight(); }
/** * 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 static void checkImagesForCubeMap(Image... images) { if (images.length == 1) { return; } Format fmt = images[0].getFormat(); int width = images[0].getWidth(); int height = images[0].getHeight(); ByteBuffer data = images[0].getData(0); int size = data != null ? data.capacity() : 0; checkImage(images[0]); for (int i = 1; i < images.length; i++) { Image image = images[i]; checkImage(images[i]); if (image.getFormat() != fmt) { throw new IllegalArgumentException("Images must have same format"); } if (image.getWidth() != width || image.getHeight() != height) { throw new IllegalArgumentException("Images must have same resolution"); } ByteBuffer data2 = image.getData(0); if (data2 != null){ if (data2.capacity() != size) { throw new IllegalArgumentException("Images must have same size"); } } } }
public static Image resizeToPowerOf2(Image original){ int potWidth = FastMath.nearestPowerOfTwo(original.getWidth()); int potHeight = FastMath.nearestPowerOfTwo(original.getHeight()); return scaleImage(original, potWidth, potHeight); }
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; } }
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); } }
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; }
@Override public void setTexture(int unit, Texture tex) { Image image = tex.getImage(); if (image.isUpdateNeeded() || (image.isGeneratedMipmapsRequired() && !image.isMipmapsGenerated())) { // Check NPOT requirements boolean scaleToPot = false; try { checkNonPowerOfTwo(tex); } catch (RendererException ex) { if (logger.isLoggable(Level.WARNING)) { int nextWidth = FastMath.nearestPowerOfTwo(tex.getImage().getWidth()); int nextHeight = FastMath.nearestPowerOfTwo(tex.getImage().getHeight()); logger.log(Level.WARNING, "Non-power-of-2 textures are not supported! Scaling texture '" + tex.getName() + "' of size " + tex.getImage().getWidth() + "x" + tex.getImage().getHeight() + " to " + nextWidth + "x" + nextHeight); } scaleToPot = true; } updateTexImageData(image, tex.getType(), unit, scaleToPot); } int texId = image.getId(); assert texId != -1; setupTextureParams(unit, tex); }
public RenderImageJme(String filename, boolean linear, NiftyJmeDisplay display){ TextureKey key = new TextureKey(filename, true); key.setAnisotropy(0); key.setGenerateMips(false); texture = (Texture2D) display.getAssetManager().loadTexture(key); texture.setMagFilter(linear ? MagFilter.Bilinear : MagFilter.Nearest); texture.setMinFilter(linear ? MinFilter.BilinearNoMipMaps : MinFilter.NearestNoMipMaps); image = texture.getImage(); width = image.getWidth(); height = image.getHeight(); }
/** * This method draws the source image on the target image starting with the * specified positions. * * @param target * the target image * @param source * the source image * @param targetXPos * start X position on the target image * @param targetYPos * start Y position on the target image */ private void draw(Image target, Image source, int targetXPos, int targetYPos) { PixelInputOutput sourceIO = PixelIOFactory.getPixelIO(source.getFormat()); PixelInputOutput targetIO = PixelIOFactory.getPixelIO(target.getFormat()); TexturePixel pixel = new TexturePixel(); for (int x = 0; x < source.getWidth(); ++x) { for (int y = 0; y < source.getHeight(); ++y) { sourceIO.read(source, 0, pixel, x, y); targetIO.write(target, 0, pixel, targetXPos + x, targetYPos + y); } } }
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); }
Texture tex = environment.getApplication().getAssetManager().loadTexture(texture); mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D)tex, true); ySize = tex.getImage().getHeight(); mouseImage.setHeight(ySize); mouseImage.setWidth(tex.getImage().getWidth()); Texture tex = environment.getApplication().getAssetManager().loadTexture(texture); mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D)tex, true); ySize = tex.getImage().getHeight(); mouseImage.setHeight(ySize); mouseImage.setWidth(tex.getImage().getWidth());
@Override public void simpleInitApp() { // create a simple plane/quad Quad quadMesh = new Quad(1, 1); quadMesh.updateGeometry(1, 1, true); Geometry quad = new Geometry("Textured Quad", quadMesh); assetManager.registerLocator("https://raw.githubusercontent.com/jMonkeyEngine/BookSamples/master/assets/Textures/", UrlLocator.class); TextureKey key = new TextureKey("mucha-window.png", false); key.setGenerateMips(true); Texture tex = assetManager.loadTexture(key); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setTexture("ColorMap", tex); quad.setMaterial(mat); float aspect = tex.getImage().getWidth() / (float) tex.getImage().getHeight(); quad.setLocalScale(new Vector3f(aspect * 1.5f, 1.5f, 1)); quad.center(); rootNode.attachChild(quad); }