rpfcachehandler.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 950 行 · 第 1/3 页

JAVA
950
字号
                int offsetX = x - start.x;                int offsetY = y - start.y;                // previous values were:                // int offsetX = start.x - x;                // int offsetY = start.y - y;                int newX = currentBox.startIndexes.x + offsetX;                int newY = currentBox.startIndexes.y + offsetY;                if (cacheIt) {                    /*                     * Subframe isn't cached; allocate new entry and decompress                     * it                     */                    index = getLRU();                    if (index < 0 || index >= subframeCacheSize                            || subframeCount >= subframeCacheSize) {                        ret = null;                    } else {                        referenceCache(index);                        RpfSubframe subframe = cache.subframe[index];                        // DFD - not sure we need to do this.  It seems like it's more important to make sure the version changes to some other value that matches what's in the subframe, rather than keep it stuck on MAX_VALUE.                        // if (subframe.version < Byte.MAX_VALUE) {                        //     subframe.version++;                        // }                        subframeIndex[y][x] = (byte) index;                        subframeVersion[y][x] = subframe.version;                        ret = cache.subframe[index];                    }                }                if (ret == null) {                    try {                        ret = new RpfSubframe(viewAttributes.colorModel);                        // See NOTE below on setScalingTo                        ret.setScalingTo(scalingWidth, scalingHeight);                    } catch (java.lang.OutOfMemoryError oome) {                        Debug.error("RpfCacheHandler: Out of memory!  No subframe for you!  Next up!");                        return null;                    }                }                if (loadSubframe(ret, currentBox, newX, newY)) {                    return ret;                } else if (cacheIt) {                    freeCache(index);                    subframeIndex[y][x] = NOT_PRESENT;                }            }        }        return null;    }    /**     * Get a subframe from the cache if possible, otherwise allocate a new cache     * entry and decompress it. Each cache entry has a version number that is     * incremented whenever it is replaced by a new subframe. This ensures the     * replacement is detected. Use this method when you are sure that the     * subframe cache is big enough to handle all the subframes on the map.     *      * @param cbx the x index of subframe in the rcbIndex A.TOC space.     * @param cby the y index of subframe in the rcbIndex A.TOC space.     */    protected RpfSubframe getCached(int cbx, int cby) {        return getCached(cbx, cby, -1);    }    /**     * Get a subframe from the cache if possible, otherwise allocate a new cache     * entry and decompress it. Each cache entry has a version number that is     * incremented whenever it is replaced by a new subframe. This ensures the     * replacement is detected. If you are not sure that the number of subframes     * that go on the map is less than or equal to the size of the subframe     * cache, then use this method to provide a running count of how many     * subframes you've already called for to use in the current map. If this     * number gets bigger than the cache size, then the RpfCacheHandler will     * keep fetching data without storing the extra subframes in the cache.     * Otherwise, the previous images in the cache would be replaced before they     * were painted, and they would not appear on the map. If subframeCount is     * less than subframe size, then the latest retrieved subframe will be     * stored in the cache.     *      * @param cbx the x index of subframe in the rcbIndex A.TOC space.     * @param cby the y index of subframe in the rcbIndex A.TOC space.     * @param subframeCount a running count of the number of subframes retrieved     *        so far for the current map. Should be used if there is concern     *        that the number of subframes needed for the map is greater than     *        the size of the cache.     */    protected RpfSubframe getCached(int cbx, int cby, int subframeCount) {        RpfSubframe ret;        RpfCoverageBox currentBox = null;        // x, y are the subframe indexes in the cache matrix        int x = cbx + subframeBuffer;        int y = cby + subframeBuffer;        /* If beyond the image boundary, forget it */        if (subframeIndex != null                && (coverageBoxes == null || coverageBoxes.size() == 0 || y < 0                        || x < 0 || y >= subframeIndex.length || x >= subframeIndex[0].length)) {            return null;        }        try {            currentBox = (RpfCoverageBox) coverageBoxes.elementAt(0);        } catch (ArrayIndexOutOfBoundsException aioobe) {            return null;        }        int index = NOT_CACHED;        if (subframeIndex != null) {            index = subframeIndex[y][x];        }        if (index == NOT_PRESENT) {            return null;        } else if (index != NOT_CACHED && cache != null                && cache.subframe[index].version == subframeVersion[y][x]                && subframeCount < subframeCacheSize) {            /* We found it and it's ours; return the cached image */            referenceCache(index);            ret = cache.subframe[index];            if (DEBUG_RPF) {                Debug.output("RpfCacheHandler: found subframe " + x + ", " + y                        + " in cache.");            }            // Need to check the current opaqueness value against the            // frame, and reset the pixel values if needed.            if (viewAttributes.colorModel == OMRasterObject.COLORMODEL_DIRECT) {                if (ret.opaqueness != viewAttributes.opaqueness) {                    int[] pixels = ret.image.getPixels();                    if (pixels != null) {                        ret.opaqueness = viewAttributes.opaqueness;                        for (int i = 0; i < pixels.length; i++) {                            pixels[i] = (0x00FFFFFF & pixels[i])                                    | (viewAttributes.opaqueness << 24);                        }                        ret.image.setNeedToRegenerate(true);                    }                }            } else {                if (ret.opaqueness != viewAttributes.opaqueness) {                    ret.opaqueness = viewAttributes.opaqueness;                    ret.image.setTransparent(viewAttributes.opaqueness);                    ret.image.setNeedToRegenerate(true);                }            }            // Check to see if the attribute text has even been            // retrieved from the RpfFrameProvider. If it hasn't, and            // needs to be, get it.            if (frameProvider != null                    && viewAttributes.showInfo                    && (ret.getAttributeText() == null || ret.getAttributeText()                            .equals(""))) {                // It's needed but not here.                ret.setAttributeText(frameProvider.getSubframeAttributes(currentBox.tocNumber,                        currentBox.entryNumber,                        x,                        y));                // NOTE on setScalingTo                // Doesn't do what you might think. setScalingTo                // doesn't set the scaling filter on the OMRasters,                // because the RpfSubframe now uses OMScalingRasters.                // However, setScalingTo still needs to be called to                // let the RpfSubframe be able to tell if the                // attribute information string should be used when                // showing attributes.                ret.setScalingTo(scalingWidth, scalingHeight);            }            return ret;        } else {            /*             * Subframe isn't cached; allocate new entry and decompress it             */            index = getLRU();            // Meet the requirements for not caching...            if (index < 0 || index >= subframeCacheSize                    || subframeCount >= subframeCacheSize) {                try {                    ret = new RpfSubframe(viewAttributes.colorModel);                    if (DEBUG_RPF) {                        Debug.output("RpfCacheHandler: using uncached subframe.");                    }                } catch (java.lang.OutOfMemoryError oome) {                    Debug.error("RpfCacheHandler: Out of memory!  No subframe for you!  Next up!");                    return null;                }            } else { // or set the cache for the new subframe                referenceCache(index);                // DFD - I'm not sure it ever gets to the point where version                // gets bigger than a byte can hold, and we don't                // want the byte overrunning, do we? Then again, does it matter,                // if the frame version number matches the array setting for it,                // even                // if it's negative?                cache.subframe[index].version++;                subframeIndex[y][x] = (byte) index;                subframeVersion[y][x] = cache.subframe[index].version;                ret = cache.subframe[index];            }            if (loadSubframe(ret, currentBox, cbx, cby)) {                return ret;            } else {                freeCache(index);                if (subframeIndex != null) {                    subframeIndex[y][x] = NOT_PRESENT;                }            }        }        return null;    }    /**     * Contacts the frame provider to put the subframe image in the RpfSubframe.     *      * @param subframe the RpfSubframe to load the image data into.     * @param coverageBox that has toc and entry numbers to use.     * @param x the coveragebox x index for the subframe.     * @param y the coveragebox y index for the subframe.     * @return true if successful.     */    protected boolean loadSubframe(RpfSubframe subframe,                                   RpfCoverageBox coverageBox, int x, int y) {        boolean good = false;        int[] pixels = null;        if (frameProvider == null) {            Debug.message("rpf",                    "RpfCacheHandler.loadSubframes(): null frameProvider");            return false;        }        subframe.opaqueness = viewAttributes.opaqueness;        if (viewAttributes.colorModel == OMRasterObject.COLORMODEL_DIRECT) {            pixels = frameProvider.getSubframeData(coverageBox.tocNumber,                    coverageBox.entryNumber,                    x,                    y);            if (pixels != null) {                subframe.image.setPixels(pixels);                good = true;            }        } else if (viewAttributes.colorModel == OMRasterObject.COLORMODEL_INDEXED) {            RpfIndexedImageData riid = frameProvider.getRawSubframeData(coverageBox.tocNumber,                    coverageBox.entryNumber,                    x,                    y);            if (riid != null && riid.imageData != null                    && riid.colortable != null) {                subframe.image.setBits(riid.imageData);                subframe.image.setColors(riid.colortable);                subframe.image.setTransparent(viewAttributes.opaqueness);                good = true;            }        } else {            Debug.error("RpfCacheHandler: Frame Provider colormodel not handled.");            return false;        }        if (good) {            // LOAD UP the geographic stuff into            // cache.subframe[index].image            float lat, lon, lat2, lon2;            double xlloffset, ylloffset;            ylloffset = (double) (y * coverageBox.subframeLatInterval);            xlloffset = (double) (x * coverageBox.subframeLonInterval);            lat = (float) ((coverageBox.nw_lat) - ylloffset);            lon = (float) ((coverageBox.nw_lon) + xlloffset);            lat2 = (float) (lat - coverageBox.subframeLatInterval);            lon2 = (float) (lon + coverageBox.subframeLonInterval);            String data;            if (viewAttributes != null                    && (viewAttributes.autofetchAttributes || viewAttributes.showInfo)) {                data = frameProvider.getSubframeAttributes(coverageBox.tocNumber,                        coverageBox.entryNumber,                        x,                        y);            } else {                data = "";            }            if (DEBUG_RPFDETAIL) {                Debug.output("Attribute data for subframe " + x + ", " + y                        + ":\n" + data);            }            // fill in the information for the subframe.            subframe.setLocation(lat, lon, lat2, lon2);            subframe.setAttributeText(data);            return true;        } else {            subframe.setAttributeText("");        }        return false;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?