Important: Changes to Xith3Ds texture loading functionality and API

Are you recommending that I change the source code of TextureLoader2 as shown in the referenced message?

Yes, I also think that the current value of this flag in the source is a bug.

Ciao Matthias Mann

Hi,

I briefly checked the implementation of TextureLoader2 and have no vote against its inclusion in the core.

I personally do not use TextureLoader and will probably not use TextureLoader2 (except maybe using DDS loading directly or after some refactoring into my private classes), but there are some
comments that may significantly improve all your applications in favor of reducing their memory requirements.

In both implementations (TextureLoader and TextureLoader2) ImageIO.read() is used to load image data to BufferedImage, and then convert it to data buffer either by painting or by direct manual conversion (correct me if I am wrong). This causes DOUBLE memory usage during texture loading, and increases app startup time because of need to copy/convert data (file -> BufferedImage -> data buffer).

From the other side, ImageIO API gives us ability to load texture directly into DirectBufferedImage, so we do not need to use intermediary BufferedImage. The code is rather complicated, because of these functions of ImageIO API are not so well documented, but I have positive result with them.

There is my code for direct texture loading:


	public static final Texture2D setupRGBXTexture(String resource, int sizeLimit, int boundaryWidth) 
	{
		BufferedImage bi = null;
		boolean noUpscaleNeeded = false;
		boolean rgba = true;
		try
		{
			ImageInputStream stream = ImageIO.createImageInputStream(J3D2Utils.class.getResourceAsStream(resource));
			Iterator iter = ImageIO.getImageReaders(stream);
			if (iter.hasNext()) 
			{
				ImageReader reader = (ImageReader)iter.next();
				ImageReadParam param = reader.getDefaultReadParam();
				reader.setInput(stream, true, true);
				int iw = reader.getWidth(0);
				int ih = reader.getHeight(0);
				Iterator it = reader.getImageTypes(0);
				if (it.hasNext())
				{
					ImageTypeSpecifier its = (ImageTypeSpecifier) it.next();
					if (its != null)
					{
						ColorModel cm = its.getColorModel();
						if (cm != null)
						{
							rgba = cm.hasAlpha(); 
						}
					}
				}
				int piw = pow2(iw);
				int pih = pow2(ih);
				if ((iw == piw) && (ih == pih))
				{
					if (rgba)
					{
						BufferedImage dst = DirectBufferedImage.getDirectImageRGBA(piw, pih);
						param.setDestination(dst);
					}
					else
					{
						BufferedImage dst = DirectBufferedImage.getDirectImageRGB(piw, pih);
						param.setDestination(dst);
					}
				}
				bi = reader.read(0, param);
				stream.close();
				reader.dispose();
				if (sizeLimit > 0)
				{
					if ((piw <= sizeLimit) && (pih <= sizeLimit))
						noUpscaleNeeded = true;
				}
				else
					noUpscaleNeeded = true;
			}
			else
				stream.close(); 
		}
		catch (Exception e)
		{
			System.err.println("Error loading " + resource);
			e.printStackTrace();
		}
		BufferedImage bimg1;
		if ((bi instanceof DirectBufferedImage) && noUpscaleNeeded)
			bimg1 = bi;
		else
		{
			if (rgba)
				bimg1 = upScaleRGBA(bi, sizeLimit);
			else
				bimg1 = upScaleRGB(bi, sizeLimit);
		}
		ImageComponent2D imgc1 = new ImageComponent2D(rgba ? ImageComponent.FORMAT_RGBA : ImageComponent.FORMAT_RGB, bimg1.getWidth(), bimg1.getHeight(), bimg1);
		if (rgba)
			return setupRGBATexture(imgc1, boundaryWidth);
		else
			return setupRGBTexture(imgc1, boundaryWidth);
	}

Please note that I detect image size and presence of Alpha channel BEFORE starting reading the pixels, so ImageIO after reads pixel data directly into DirectImage of neccessary format.

Best regards,
Yuri

[quote]In both implementations (TextureLoader and TextureLoader2) ImageIO.read() is used to load image data to BufferedImage, and then convert it to data buffer either by painting or by direct manual conversion (correct me if I am wrong).
[/quote]
But doesn’t it support also other means of texture loading? Isn’t it that what all these interfaces in TextureLoader2 are for?

bump

OK, the deadline was missed, partly due to the fact these forums were down.

I play to commit these changes in the coming week. If anyone has any objections, speak up quickly :slight_smile:

Cheers,

Will.

I have committed these changes.

Aplogies for the delay (though you have been able to use them though the experimental build anyway).

Will.

When will the new Java Web Start xith3d.jar come out?

Since you are asking for them, give me a week. I’ll put the MD2 loader into the tookit first.

Cheers,

Will.

And can you either add the URL to JGF’s xith3d page, or if you want a mirror upload the jar file instead? If you message / email me your username on JGF I’ll make sure you’ve got ownership of the xith3d lib (if you hven’t already).

Yuri

i wanted to use your reduced memory texture loader, but some methods are missing. Any chance of posting them??

   upScaleRGB(bi, sizeLimit);
	 
 setupRGBTexture(imgc1, boundaryWidth);

 pow2