calculate Optimal Texture Resolution - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

calculate Optimal Texture Resolution

Demo Code


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Main{
    /**//from www.j  ava 2  s  . com
     * 
     * @param sourceImg
     * @param maxTexDim
     * @param thresholdPercent
     */
    public static int calculateOptimalTextureResolution(Image sourceImg,
            int maxTexDim, int thresholdPercent) {
        if (null != sourceImg) {
            int imgWidth = sourceImg.getWidth();
            int imgHeight = sourceImg.getHeight();

            final int max = Math.max(imgWidth, imgHeight);

            //first, shrink to fit
            while (max < (maxTexDim >> 1))
                maxTexDim >>= 1;

            long pixelRealestate = Utilities3d.getNumTextureMatrixPerRow(
                    sourceImg, maxTexDim)
                    * Utilities3d.getNumTextureMatrixPerColumn(sourceImg,
                            maxTexDim) * (maxTexDim * maxTexDim);

            int maxTexDimCandidate = maxTexDim >> 1;
            long pixelRealestateCandidate = Utilities3d
                    .getNumTextureMatrixPerRow(sourceImg,
                            maxTexDimCandidate)
                    * Utilities3d.getNumTextureMatrixPerColumn(sourceImg,
                            maxTexDimCandidate)
                    * (maxTexDimCandidate * maxTexDimCandidate);

            while ((int) (((pixelRealestateCandidate * 1000000l) / pixelRealestate) / 10000l) <= thresholdPercent) {
                pixelRealestate = pixelRealestateCandidate;
                maxTexDim = maxTexDimCandidate;

                maxTexDimCandidate = maxTexDim >> 1;
                pixelRealestateCandidate = Utilities3d
                        .getNumTextureMatrixPerRow(sourceImg,
                                maxTexDimCandidate)
                        * Utilities3d.getNumTextureMatrixPerColumn(
                                sourceImg, maxTexDimCandidate)
                        * (maxTexDimCandidate * maxTexDimCandidate);
            }

            return maxTexDim;
        }

        return -1;
    }
    /**
     * Returns the number of horizontal subdivisions the argument image needs to
     * be divided into to respect argument maxTexDim size
     * 
     * @param sourceImg
     * @param maxTexDim
     * @return the number of texture matrices
     */
    public static int getNumTextureMatrixPerRow(Image sourceImg,
            int maxTexDim) {
        if (null != sourceImg) {
            //define number of needed subdivisions
            int numSubTexPerRow = 0, imgW = sourceImg.getWidth();

            while (maxTexDim * numSubTexPerRow < imgW)
                ++numSubTexPerRow;

            return numSubTexPerRow;
        }

        return -1;
    }
    /**
     * Returns the number of vertical subdivisions the argument image needs to
     * be divided into to respect argument maxTexDim size
     * 
     * @param sourceImg
     * @param maxTexDim
     * @return the number of texture matrices
     */
    public static int getNumTextureMatrixPerColumn(Image sourceImg,
            int maxTexDim) {
        if (null != sourceImg) {
            //define number of needed subdivisions
            int numSubTexPerColumn = 0, imgH = sourceImg.getHeight();

            while (maxTexDim * numSubTexPerColumn < imgH)
                ++numSubTexPerColumn;

            return numSubTexPerColumn;
        }

        return -1;
    }
}

Related Tutorials