Example usage for java.awt.image BufferedImage getRaster

List of usage examples for java.awt.image BufferedImage getRaster

Introduction

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

Prototype

public WritableRaster getRaster() 

Source Link

Document

Returns the WritableRaster .

Usage

From source file:org.openmrs.api.ObsServiceTest.java

/**
 * @throws IOException/*  w w w. j  av  a2s.c o  m*/
 * @see ObsService#getComplexObs(Integer,String)
 */
@Test
public void getComplexObs_shouldNotFailWithNullView() throws IOException {
    executeDataSet(COMPLEX_OBS_XML);
    // create gif file
    // make sure the file isn't there to begin with
    AdministrationService as = Context.getAdministrationService();
    File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(
            as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    File createdFile = new File(complexObsDir, "openmrs_logo_small.gif");
    if (createdFile.exists())
        createdFile.delete();
    int width = 10;
    int height = 10;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    int[] colorArray = new int[3];
    int h = 255;
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            if (i == 0 || j == 0 || i == width - 1 || j == height - 1
                    || (i > width / 3 && i < 2 * width / 3) && (j > height / 3 && j < 2 * height / 3)) {
                colorArray[0] = h;
                colorArray[1] = h;
                colorArray[2] = 0;
            } else {
                colorArray[0] = 0;
                colorArray[1] = 0;
                colorArray[2] = h;
            }
            raster.setPixel(i, j, colorArray);
        }
    }
    ImageIO.write(image, "gif", createdFile);
    // end create gif file
    ObsService os = Context.getObsService();

    os.getComplexObs(44, null);
    // delete gif file
    // we always have to delete this inside the same unit test because it is
    // outside the
    // database and hence can't be "rolled back" like everything else
    createdFile.delete();
}

From source file:org.openmrs.api.ObsServiceTest.java

/**
 * @throws IOException//from   w  w w  .ja va 2  s.co  m
 * @see ObsService#getComplexObs(Integer,String)
 */
@Test
public void getComplexObs_shouldFillInComplexDataObjectForComplexObs() throws IOException {
    executeDataSet(COMPLEX_OBS_XML);
    // create gif file
    // make sure the file isn't there to begin with
    AdministrationService as = Context.getAdministrationService();
    File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(
            as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    File createdFile = new File(complexObsDir, "openmrs_logo_small.gif");
    if (createdFile.exists())
        createdFile.delete();
    int width = 10;
    int height = 10;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    int[] colorArray = new int[3];
    int h = 255;
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            if (i == 0 || j == 0 || i == width - 1 || j == height - 1
                    || (i > width / 3 && i < 2 * width / 3) && (j > height / 3 && j < 2 * height / 3)) {
                colorArray[0] = h;
                colorArray[1] = h;
                colorArray[2] = 0;
            } else {
                colorArray[0] = 0;
                colorArray[1] = 0;
                colorArray[2] = h;
            }
            raster.setPixel(i, j, colorArray);
        }
    }
    ImageIO.write(image, "gif", createdFile);
    // end create gif file
    ObsService os = Context.getObsService();

    Obs complexObs = os.getComplexObs(44, ComplexObsHandler.RAW_VIEW);

    Assert.assertNotNull(complexObs);
    Assert.assertTrue(complexObs.isComplex());
    Assert.assertNotNull(complexObs.getValueComplex());
    Assert.assertNotNull(complexObs.getComplexData());
    // delete gif file
    // we always have to delete this inside the same unit test because it is
    // outside the
    // database and hence can't be "rolled back" like everything else
    createdFile.delete();
}

From source file:lucee.runtime.img.Image.java

private BufferedImage jpgImage(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    SampleModel srcSM = src.getSampleModel();
    WritableRaster srcWR = src.getRaster();
    java.awt.image.DataBuffer srcDB = srcWR.getDataBuffer();

    ColorModel rgb = new DirectColorModel(32, 0xff0000, 65280, 255);
    int[] bitMasks = new int[] { 0xff0000, 65280, 255 };

    SampleModel csm = new SinglePixelPackedSampleModel(3, w, h, bitMasks);
    int data[] = new int[w * h];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int pix[] = null;
            int sample[] = srcSM.getPixel(j, i, pix, srcDB);
            if (sample[3] == 0 && sample[2] == 0 && sample[1] == 0 && sample[0] == 0)
                data[i * w + j] = 0xffffff;
            else/*from  w w w .  jav a2  s.c o  m*/
                data[i * w + j] = sample[0] << 16 | sample[1] << 8 | sample[2];
        }

    }

    java.awt.image.DataBuffer db = new DataBufferInt(data, w * h * 3);
    WritableRaster wr = Raster.createWritableRaster(csm, db, new Point(0, 0));
    return new BufferedImage(rgb, wr, false, null);
}

From source file:com.simiacryptus.mindseye.lang.Tensor.java

/**
 * To gray png buffered png.// w  w  w. j a  va  2s .com
 *
 * @param band the band
 * @return the buffered png
 */
@Nonnull
public BufferedImage toGrayImage(final int band) {
    final int width = getDimensions()[0];
    final int height = getDimensions()[1];
    @Nonnull
    final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            final double v = get(x, y, band);
            image.getRaster().setSample(x, y, 0, v < 0 ? 0 : v > 255 ? 255 : v);
        }
    }
    return image;
}

From source file:org.opencastproject.videosegmenter.impl.jmf.ImageComparator.java

/**
 * Returns <code>true</code> if <code>image</code> differs from <code>currentImage</code>. In order to be treated a
 * different image, the <code>rgb</code> values of at least <code>changesThreshold</code> pixels must have changed.
 * <p>/*from w w w . j  a v  a  2 s.c  om*/
 * Note that <code>image</code> might contain an altered version of the image, which will facilitate in the comparison
 * next time when <code>image</code> is <code>currentImage</code>.
 * 
 * @param previousImage
 *          the previous image
 * @param image
 *          the new image
 * @param timestamp
 *          the image timestamp
 * 
 * @return <code>true</code> if the two images are different
 */
public boolean isDifferent(BufferedImage previousImage, BufferedImage image, long timestamp) {
    boolean differsFromCurrentScene = false;
    BufferedImage edgeImage = getEdgedImage(image);

    if (previousImage == null) {
        differsFromCurrentScene = true;
        logger.debug("First segment started");
    } else if (previousImage.getWidth() != image.getWidth() || previousImage.getHeight() != image.getHeight()) {
        differsFromCurrentScene = true;
        String currentResolution = previousImage.getWidth() + "x" + previousImage.getHeight();
        String newResolution = image.getWidth() + "x" + image.getHeight();
        logger.warn("Resolution change detected ({} -> {})", currentResolution, newResolution);
    } else {
        int changes = 0;
        long pixels = image.getWidth() * image.getHeight();
        long changesThresholdPixels = (long) (pixels * changesThreshold);

        imagecomparison: for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                if (edgeImage.getRGB(x, y) != previousImage.getRGB(x, y)) {
                    changes++;
                    if (changes > changesThresholdPixels) {
                        differsFromCurrentScene = true;
                        if (!collectStatistics)
                            break imagecomparison;
                    }
                }
            }
        }

        float percentage = ((float) changes) / ((float) pixels);
        if (differsFromCurrentScene)
            logger.debug("Differences found at {} s ({} change to previous image)", timestamp,
                    percentageNf.format(percentage));
        else
            logger.debug("Found {} changes at {} s to the previous frame", percentageNf.format(percentage),
                    timestamp);

        comparisons++;
        totalChanges += percentage;
    }

    // Write the images to disk for debugging and verification purposes
    if (tempDir != null) {
        try {
            FileUtils.forceMkdir(tempDir);
            ImageIO.write(image, "jpg", new File(tempDir, "image-" + timestamp + ".jpg"));
            ImageIO.write(edgeImage, "jpg", new File(tempDir, "image-" + timestamp + "-edged.jpg"));
        } catch (IOException e) {
            logger.warn("Error writing intermediary images to {}" + tempDir);
            e.printStackTrace();
        }
    }

    // Copy the resulting image for further reference to the original
    image.getRaster().setRect(edgeImage.getData());

    return differsFromCurrentScene;
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

private static boolean isNotBlank(BufferedImage image) {
    // yes, i know this is bonkers. yes, the next step is to try sampling
    // to avoid iterating over each pixel.
    // tests on my linux machine typically find a non-blank pixel within the
    // first 100 iterations, and fewer than 5 retries to get a non-blank
    // *image* (typically 1-2 retries though)
    DataBuffer buf = image.getRaster().getDataBuffer();
    ColorModel model = image.getColorModel();
    boolean result = false;
    int i;//from w  w  w  .  j  a  v  a  2s .  c om
    for (i = 0; i < buf.getSize(); i++) {
        // it's apparently not sufficient to simply grab the value directly;
        // Linux seems to store the "blank" value as -16777216 (-2^24, which
        // makes a lot of sense for images), while on OS X the blank value
        // is simply 0. i suspect the getRGB stuff is a candidate for
        // inlining by the JIT, but profiling is needed.
        int rgb = model.getRGB(buf.getElem(i));
        if (rgb != -16777216) {
            logger.trace("found non-blank value '{}' at index {}", rgb, i);
            result = true;
            break;
        }
    }
    logger.trace("{} iterations to return {}", i, result);
    return result;
}