/** * Computes the sizes of each mipmap level in bytes, and stores it in sizes_[]. */ private void loadSizes() { int mipWidth = width; int mipHeight = height; sizes = new int[mipMapCount]; int outBpp = pixelFormat.getBitsPerPixel(); for (int i = 0; i < mipMapCount; i++) { int size; if (compressed) { size = ((mipWidth + 3) / 4) * ((mipHeight + 3) / 4) * outBpp * 2; } else { size = mipWidth * mipHeight * outBpp / 8; } sizes[i] = ((size + 3) / 4) * 4; mipWidth = Math.max(mipWidth / 2, 1); mipHeight = Math.max(mipHeight / 2, 1); } }
dataStore = BufferUtils.createByteBuffer(width * height * pixelFormat.getBitsPerPixel()); int bytesPerPixel = pixelFormat.getBitsPerPixel() / 8; int scanLineBytes = bytesPerPixel * width; for (int y = height - 1; y >= 0; y--) {
/** * Reads a grayscale image with mipmaps from the InputStream * @param flip Flip the loaded image by Y axis * @param totalSize Total size of the image in bytes including the mipmaps * @return A ByteBuffer containing the grayscale image data with mips. * @throws java.io.IOException If an error occured while reading from InputStream */ public ByteBuffer readGrayscale3D(boolean flip, int totalSize) throws IOException { ByteBuffer buffer = BufferUtils.createByteBuffer(totalSize * depth); if (bpp == 8) { logger.finest("Source image format: R8"); } assert bpp == pixelFormat.getBitsPerPixel(); for (int i = 0; i < depth; i++) { int mipWidth = width; int mipHeight = height; for (int mip = 0; mip < mipMapCount; mip++) { byte[] data = new byte[sizes[mip]]; in.readFully(data); if (flip) { data = flipData(data, mipWidth * bpp / 8, mipHeight); } buffer.put(data); mipWidth = Math.max(mipWidth / 2, 1); mipHeight = Math.max(mipHeight / 2, 1); } } buffer.rewind(); return buffer; }
depth = 1; int bytesPerPixel = image.getFormat().getBitsPerPixel() >> 3; ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(depth);
int targetBytesPP = pixelFormat.getBitsPerPixel() / 8;
int targetBytesPP = pixelFormat.getBitsPerPixel() / 8;
mipSizes = new int[]{data.capacity()}; } else { mipSizes = new int[]{width * height * jmeFormat.getBitsPerPixel() / 8};
int bytesPerPixel = format.getBitsPerPixel() / 8; int scanLineBytes = bytesPerPixel * width;
ByteBuffer data = BufferUtils.createByteBuffer(width * height * (image.getFormat().getBitsPerPixel() >> 3));
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(),
/** * Reads a grayscale image with mipmaps from the InputStream * @param flip Flip the loaded image by Y axis * @param totalSize Total size of the image in bytes including the mipmaps * @return A ByteBuffer containing the grayscale image data with mips. * @throws java.io.IOException If an error occured while reading from InputStream */ public ByteBuffer readGrayscale2D(boolean flip, int totalSize) throws IOException { ByteBuffer buffer = BufferUtils.createByteBuffer(totalSize); if (bpp == 8) { logger.finest("Source image format: R8"); } assert bpp == pixelFormat.getBitsPerPixel(); int mipWidth = width; int mipHeight = height; for (int mip = 0; mip < mipMapCount; mip++) { byte[] data = new byte[sizes[mip]]; in.readFully(data); if (flip) { data = flipData(data, mipWidth * bpp / 8, mipHeight); } buffer.put(data); mipWidth = Math.max(mipWidth / 2, 1); mipHeight = Math.max(mipHeight / 2, 1); } return buffer; }
/** * Creates an image of the given size and depth. * @param format * the image format * @param width * the image width * @param height * the image height * @param depth * the image depth * @return the new image instance */ public static Image createEmptyImage(Format format, int width, int height, int depth) { int bufferSize = width * height * (format.getBitsPerPixel() >> 3); if (depth < 2) { return new Image(format, width, height, BufferUtils.createByteBuffer(bufferSize), com.jme3.texture.image.ColorSpace.Linear); } ArrayList<ByteBuffer> data = new ArrayList<ByteBuffer>(depth); for (int i = 0; i < depth; ++i) { data.add(BufferUtils.createByteBuffer(bufferSize)); } return new Image(Format.RGB8, width, height, depth, data, com.jme3.texture.image.ColorSpace.Linear); }
/** * 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); } }
/** * Constructor. Allocates memory for data structures. * * @param compressedSize * the size of compressed image (or its mipmap) * @param widthToHeightRatio * width/height ratio for the image * @param format * the format of the image */ public DDSTexelData(int compressedSize, float widthToHeightRatio, Format format) { int texelsCount = compressedSize * 8 / format.getBitsPerPixel() / 16; this.colors = new TexturePixel[texelsCount][]; this.indexes = new long[texelsCount]; this.widthInPixels = (int) (0.5f * (float) Math.sqrt(this.getSizeInBytes() / widthToHeightRatio)); this.heightInPixels = (int) (this.widthInPixels / widthToHeightRatio); this.xTexelCount = widthInPixels >> 2; this.yCounter = (heightInPixels >> 2) - 1;// xCounter is 0 for now if (format == Format.DXT3 || format == Format.DXT5) { this.alphas = new float[texelsCount][]; this.alphaIndexes = new long[texelsCount]; } }
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); } }
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)); }
@Override public void render(final RenderManager renderManager) { if (jobs.isEmpty()) { return; } final SnapshotJob job = jobs.get(0); for (int i = 0; i < 6; i++) { viewports[i].clearScenes(); viewports[i].attachScene(job.scene); renderManager.renderViewPort(viewports[i], 0.16f); buffers[i] = BufferUtils.createByteBuffer(size * size * imageFormat.getBitsPerPixel() / 8); renderManager.getRenderer().readFrameBufferWithFormat(framebuffers[i], buffers[i], imageFormat); images[i] = new Image(imageFormat, size, size, buffers[i], ColorSpace.Linear); MipMapGenerator.generateMipMaps(images[i]); } final TextureCubeMap map = EnvMapUtils.makeCubeMap(images[0], images[1], images[2], images[3], images[4], images[5], imageFormat); debugEnv = map; job.callback.done(map); map.getImage().dispose(); jobs.remove(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; }
public void read(Image image, int layer, TexturePixel pixel, int x, int y) { int index = (y * image.getWidth() + x) * (image.getFormat().getBitsPerPixel() >> 3); this.read(image, layer, pixel, index); }
public void write(Image image, int layer, TexturePixel pixel, int x, int y) { int index = (y * image.getWidth() + x) * (image.getFormat().getBitsPerPixel() >> 3); this.write(image, layer, pixel, index); } }