Example usage for java.awt.image DataBuffer getDataTypeSize

List of usage examples for java.awt.image DataBuffer getDataTypeSize

Introduction

In this page you can find the example usage for java.awt.image DataBuffer getDataTypeSize.

Prototype

public static int getDataTypeSize(int type) 

Source Link

Document

Returns the size (in bits) of the data type, given a datatype tag.

Usage

From source file:com.embedler.moon.jtxt2img.mmap.MappedFileBuffer.java

private MappedFileBuffer(final int type, final int size, final int numBanks) {
    super(type, size, numBanks);

    Validate.isTrue(size >= 0, "Integer overflow for size: %d", size);
    Validate.isTrue(numBanks >= 0, "Number of banks must be positive", numBanks);

    int componentSize = DataBuffer.getDataTypeSize(type) / 8;

    try {/*  w  ww  .  j  a  va 2 s  .c om*/
        tempFile = File.createTempFile(String.format("%s-", getClass().getSimpleName().toLowerCase()), ".tmp");
        try (RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
                FileChannel channel = raf.getChannel()) {

            long length = ((long) size) * componentSize * numBanks;
            raf.setLength(length);

            byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, length);

            switch (type) {
            case DataBuffer.TYPE_BYTE:
                buffer = byteBuffer;
                break;
            case DataBuffer.TYPE_USHORT:
                buffer = byteBuffer.asShortBuffer();
                break;
            case DataBuffer.TYPE_INT:
                buffer = byteBuffer.asIntBuffer();
                break;
            default:
                throw new IllegalArgumentException("Unsupported data type: " + type);
            }
        } finally {
            if (!tempFile.delete()) {
                tempFile.deleteOnExit();
            }
        }
    } catch (Exception e) {
        throw new JTxt2ImgIoRuntimeException(e);
    }
}

From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java

private Path decompressTile(int tileIndex, int level) throws IOException {
    Path tileFile = PathUtils.get(cacheDir, PathUtils.getFileNameWithoutExtension(imageFile).toLowerCase()
            + "_tile_" + String.valueOf(tileIndex) + "_" + String.valueOf(level) + ".tif");
    if ((!Files.exists(tileFile)) || (diffLastModifiedTimes(tileFile.toFile(), imageFile.toFile()) < 0L)) {
        final OpjExecutor decompress = new OpjExecutor(OpenJpegExecRetriever.getOpjDecompress());
        final Map<String, String> params = new HashMap<String, String>() {
            {/*from w ww. j  a v a  2  s . c o  m*/
                put("-i", GetIterativeShortPathNameW(imageFile.toString()));
                put("-r", String.valueOf(level));
                put("-l", "20");
            }
        };
        String tileFileName;
        if (org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS && (tileFile.getParent() != null)) {
            tileFileName = Utils.GetIterativeShortPathNameW(tileFile.getParent().toString()) + File.separator
                    + tileFile.getName(tileFile.getNameCount() - 1);
        } else {
            tileFileName = tileFile.toString();
        }

        params.put("-o", tileFileName);
        params.put("-t", String.valueOf(tileIndex));
        params.put("-p", String.valueOf(DataBuffer.getDataTypeSize(this.getSampleModel().getDataType())));
        params.put("-threads", "ALL_CPUS");

        if (decompress.execute(params) != 0) {
            logger.severe(decompress.getLastError());
            tileFile = null;
        } else {
            logger.fine("Decompressed tile #" + String.valueOf(tileIndex) + " @ resolution "
                    + String.valueOf(level));
        }
    }
    return tileFile;
}

From source file:org.geoserver.jai.ConcurrentTileFactory.java

/**
 * Builds a new tile, eventually recycling the data array backing it
 *//*from   w  w w .  j  a v a2 s . co  m*/
public WritableRaster createTile(SampleModel sampleModel, Point location) {
    // sanity checks
    if (sampleModel == null) {
        throw new NullPointerException("sampleModel cannot be null");
    }
    if (location == null) {
        location = new Point(0, 0);
    }

    DataBuffer db = null;

    // get the three elements making the key into the recycled array map
    int type = sampleModel.getTransferType();
    long numBanks = 0;
    long size = 0;
    if (sampleModel instanceof ComponentSampleModel) {
        ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
        numBanks = getNumBanksCSM(csm);
        size = getBufferSizeCSM(csm);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel;
        numBanks = 1;
        int dataTypeSize = DataBuffer.getDataTypeSize(type);
        size = mppsm.getScanlineStride() * mppsm.getHeight()
                + (mppsm.getDataBitOffset() + dataTypeSize - 1) / dataTypeSize;
    } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        numBanks = 1;
        size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + sppsm.getWidth();
    }

    if (size > 0) {
        // try to build a new data buffer starting from 
        Object array = recycledArrays.getRecycledArray(type, numBanks, size);
        if (array != null) {
            switch (type) {
            case DataBuffer.TYPE_BYTE: {
                byte[][] bankData = (byte[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (byte) 0);
                }
                db = new DataBufferByte(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_USHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferUShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_SHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_INT: {
                int[][] bankData = (int[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0);
                }
                db = new DataBufferInt(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_FLOAT: {
                float[][] bankData = (float[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0F);
                }
                db = DataBufferUtils.createDataBufferFloat(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_DOUBLE: {
                double[][] bankData = (double[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0);
                }
                db = DataBufferUtils.createDataBufferDouble(bankData, (int) size);
            }
                break;
            default:
                throw new IllegalArgumentException("Unknown array type");
            }
        }
    }

    if (db == null) {
        db = sampleModel.createDataBuffer();
    }

    return Raster.createWritableRaster(sampleModel, db, location);
}