Example usage for java.awt.image DataBufferUShort DataBufferUShort

List of usage examples for java.awt.image DataBufferUShort DataBufferUShort

Introduction

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

Prototype

public DataBufferUShort(short[][] dataArray, int size) 

Source Link

Document

Constructs an unsigned-short based DataBuffer with the specified arrays.

Usage

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

/**
 * Builds a new tile, eventually recycling the data array backing it
 *///from ww  w.  j a  v  a 2s . 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);
}

From source file:org.photovault.dcraw.RawImage.java

/**
 * Load the raw image using dcraw. No processing is yet done for the image,
 * however, the histogram & white point is calculated.
 *//*ww  w  .j  a v  a  2s  .  c  o m*/
private void loadRawImage() {
    long startTime = System.currentTimeMillis();
    log.debug("begin:loadRawImage");
    if (lrd == null) {
        openRaw();
        log.debug("openRaw() " + (System.currentTimeMillis() - startTime));
        if (lrd == null) {
            throw new IllegalStateException("Called loadRawImage before opening file");
        }
        lr.libraw_unpack(lrd);
        log.debug("unpacked " + (System.currentTimeMillis() - startTime));
    }
    /*
     * Copy the unprocessed data to temporary array so that we can restore 
     * lrd to the state it had after unpack()
     */

    int oldFlags = lrd.progress_flags;
    int oldFilters = lrd.idata.filters;
    int rawImageSize = lrd.sizes.iwidth * lrd.sizes.iheight * 4;
    short rawWidth = lrd.sizes.width;
    short rawHeight = lrd.sizes.height;
    this.width = lrd.sizes.width;
    this.height = lrd.sizes.height;
    short[] rawData = lrd.image.getShortArray(0, rawImageSize);
    lr.libraw_dcraw_process(lrd);
    log.debug("processed " + (System.currentTimeMillis() - startTime));
    int procWidth = lrd.sizes.width;
    int procHeight = lrd.sizes.height;

    int postSubsample = (lrd.output_params.half_size > 0) ? subsample / 2 : subsample;
    /*
     * Copy the raw image to Java raster, using box filter to subsample
     */
    int scaledW = procWidth / postSubsample;
    int scaledH = procHeight / postSubsample;
    short[] buf = new short[scaledW * scaledH * 3];
    int pos = 0;
    for (int row = 0; row < scaledH; row++) {
        for (int col = 0; col < scaledW; col++) {
            int rsum = 0;
            int gsum = 0;
            int bsum = 0;
            for (int or = row * postSubsample; or < (row + 1) * postSubsample; or++) {
                for (int oc = col * postSubsample; oc < (col + 1) * postSubsample; oc++) {
                    int r = lrd.image.getShort(8 * (oc + procWidth * or));
                    rsum += (r & 0xffff);
                    int g = lrd.image.getShort(8 * (oc + procWidth * or) + 2);
                    gsum += (g & 0xffff);
                    int b = lrd.image.getShort(8 * (oc + procWidth * or) + 4);
                    bsum += (b & 0xffff);
                }
            }
            buf[pos++] = (short) (rsum / (postSubsample * postSubsample));
            buf[pos++] = (short) (gsum / (postSubsample * postSubsample));
            buf[pos++] = (short) (bsum / (postSubsample * postSubsample));
        }
    }
    log.debug("subsampled " + (System.currentTimeMillis() - startTime));

    // Restore LibRaw state to what it was before dcraw_process
    lrd.image.write(0, rawData, 0, rawImageSize);
    lrd.progress_flags = oldFlags;
    lrd.sizes.width = rawWidth;
    lrd.sizes.height = rawHeight;
    lrd.idata.filters = oldFilters;
    rawData = null;

    // closeRaw();

    DataBuffer db = new DataBufferUShort(buf, buf.length);
    SampleModel sampleModel = RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_USHORT, scaledW,
            scaledH, 3, 3 * scaledW, new int[] { 0, 1, 2 });
    WritableRaster r = Raster.createWritableRaster(sampleModel, db, new Point(0, 0));
    log.debug("raster created " + (System.currentTimeMillis() - startTime));

    if (this.chanMultipliers == null) {
        chanMultipliers = cameraMultipliers.clone();
        calcCTemp();
    }

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
    ColorModel targetCM = new ComponentColorModel(cs, new int[] { 16, 16, 16 }, false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    rawImage = new TiledImage(new BufferedImage(targetCM, r, true, null), 256, 256);
    rawImage.setProperty("org.photovault.opname", "dcraw_data");

    if (preRotation.getJaiTransposeType() != null) {
        rawImage = TransposeDescriptor.create(rawImage, preRotation.getJaiTransposeType(), null);
        rawImage.setProperty("org.photovault.opname", "orientation_adjustment");
    }

    final float[] DEFAULT_KERNEL_1D = { 0.25f, 0.5f, 0.25f };
    ParameterBlock pb = new ParameterBlock();
    KernelJAI kernel = new KernelJAI(DEFAULT_KERNEL_1D.length, DEFAULT_KERNEL_1D.length,
            DEFAULT_KERNEL_1D.length / 2, DEFAULT_KERNEL_1D.length / 2, DEFAULT_KERNEL_1D, DEFAULT_KERNEL_1D);
    pb.add(kernel);
    BorderExtender extender = BorderExtender.createInstance(BorderExtender.BORDER_COPY);
    RenderingHints hints = JAI.getDefaultInstance().getRenderingHints();
    if (hints == null) {
        hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
    } else {
        hints.put(JAI.KEY_BORDER_EXTENDER, extender);
    }

    RenderedOp filter = new RenderedOp("convolve", pb, hints);
    // javax.media.jai.operator.BoxFilterDescriptor.create( null, new Integer(2), new Integer(2), new Integer(0), new Integer(0), null );

    // Add the subsampling operation.
    pb = new ParameterBlock();
    pb.addSource(filter);
    pb.add(new Float(0.5F)).add(new Float(0.5F));
    pb.add(new Float(0.0F)).add(new Float(0.0F));
    pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
    RenderedOp downSampler = new RenderedOp("scale", pb, null);
    // downSampler = javax.media.jai.operator.BoxFilterDescriptor.create( null, new Integer(2), new Integer(2), new Integer(0), new Integer(0), null );

    RenderableOp rawImageRenderable = RenderableDescriptor.createRenderable(rawImage, downSampler, null, null,
            null, null, null);
    double colorCorrMat[][] = new double[][] { { colorCorr[0], 0.0, 0.0, 0.0 }, { 0.0, colorCorr[1], 0.0, 0.0 },
            { 0.0, 0.0, colorCorr[2], 0.0 } };

    RenderingHints nonCachedHints = new RenderingHints(JAI.KEY_TILE_CACHE, null);
    wbAdjustedRawImage = BandCombineDescriptor.createRenderable(rawImageRenderable, colorCorrMat,
            nonCachedHints);
    wbAdjustedRawImage.setProperty("org.photovault.opname", "wb_adjusted_image");

    //            reader.getImageMetadata( 0 );
    //            rawIsHalfSized = dcraw.ishalfSize();
    //
    //            createHistogram();
    //        } catch (FileNotFoundException ex) {
    //            ex.printStackTrace();
    //        } catch (IOException ex) {
    //            ex.printStackTrace();
    //        } catch (PhotovaultException ex) {
    //            ex.printStackTrace();
    //        }
    log.debug("image ready " + (System.currentTimeMillis() - startTime));

    if (autoExposeRequested) {
        doAutoExpose();
    }
    log.debug("exit: loadRawImage " + (System.currentTimeMillis() - startTime));

}