Java Code Examples for java.awt.image.WritableRaster#setPixels()

The following examples show how to use java.awt.image.WritableRaster#setPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DCTFilter.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private WritableRaster fromBGRtoRGB(Raster raster)
{
    WritableRaster writableRaster = raster.createCompatibleWritableRaster();

    int width = raster.getWidth();
    int height = raster.getHeight();
    int w3 = width * 3;
    int[] tab = new int[w3];
    //BEWARE: handling the full image at a time is slower than one line at a time        
    for (int y = 0; y < height; y++)
    {
        raster.getPixels(0, y, width, 1, tab);
        for (int off = 0; off < w3; off += 3)
        {
            int tmp = tab[off];
            tab[off] = tab[off + 2];
            tab[off + 2] = tmp;
        }
        writableRaster.setPixels(0, y, width, 1, tab);
    }
    return writableRaster;
}
 
Example 2
Source File: MapRenderer.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
@Override
public Raster getRaster(int x, int y, int w, int h) {
    WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);

    int[] data = new int[w * h * 4];
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            double distance = mPoint.distance(x + i, y + j);
            double radius = mRadius.distance(0, 0);
            double ratio = distance / radius;
            if (ratio > 1.0) {
                ratio = 1.0;
            }

            int base = (j * w + i) * 4;
            data[base] = (int) (color1.getRed() + ratio * (color2.getRed() - color1.getRed()));
            data[base + 1] = (int) (color1.getGreen() + ratio * (color2.getGreen() - color1.getGreen()));
            data[base + 2] = (int) (color1.getBlue() + ratio * (color2.getBlue() - color1.getBlue()));
            data[base + 3] = (int) (color1.getAlpha() + ratio * (color2.getAlpha() - color1.getAlpha()));
        }
    }
    raster.setPixels(0, 0, w, h, data);

    return raster;
}
 
Example 3
Source File: RGBCompositeContext.java    From scrimage with Apache License 2.0 6 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    float alpha = this.alpha;

    int[] srcPix = null;
    int[] dstPix = null;

    int x = dstOut.getMinX();
    int w = dstOut.getWidth();
    int y0 = dstOut.getMinY();
    int y1 = y0 + dstOut.getHeight();

    for (int y = y0; y < y1; y++) {
        srcPix = src.getPixels(x, y, w, 1, srcPix);
        dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
        composeRGB(srcPix, dstPix, alpha);
        dstOut.setPixels(x, y, w, 1, dstPix);
    }
}
 
Example 4
Source File: RGBComposite.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
@Override
        public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
            float alpha = this.alpha;

            int[] srcPix = null;
            int[] dstPix = null;

            int x = dstOut.getMinX();
            int w = dstOut.getWidth();
            int y0 = dstOut.getMinY();
            int y1 = y0 + dstOut.getHeight();

            for (int y = y0; y < y1; y++) {
                srcPix = src.getPixels(x, y, w, 1, srcPix);
                dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
//                int srclength = srcPix.length;
//                int dstlength = dstPix.length;
//                if(srclength > dstlength) {
//                    continue;
//                }
                // System.out.println("RGBComposite$RGBCompositeContext.compose dstlength = " + dstlength + ", srclength = " + srclength);
                composeRGB(srcPix, dstPix, alpha);
                dstOut.setPixels(x, y, w, 1, dstPix);
            }
        }
 
Example 5
Source File: DCTFilter.java    From sambox with Apache License 2.0 6 votes vote down vote up
private WritableRaster fromBGRtoRGB(Raster raster)
{
    WritableRaster writableRaster = raster.createCompatibleWritableRaster();

    int width = raster.getWidth();
    int height = raster.getHeight();
    int w3 = width * 3;
    int[] tab = new int[w3];
    // BEWARE: handling the full image at a time is slower than one line at a time
    for (int y = 0; y < height; y++)
    {
        raster.getPixels(0, y, width, 1, tab);
        for (int off = 0; off < w3; off += 3)
        {
            int tmp = tab[off];
            tab[off] = tab[off + 2];
            tab[off + 2] = tmp;
        }
        writableRaster.setPixels(0, y, width, 1, tab);
    }
    return writableRaster;
}
 
Example 6
Source File: ImageConvert.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void invertPixelValue(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);
        for (int x = 0; x < stride; x++) {
            pixelRow[x] = 255 - pixelRow[x];
        }
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}
 
Example 7
Source File: RVizVisualization.java    From coordination_oru with GNU General Public License v3.0 5 votes vote down vote up
private BufferedImage toBlackAndWhite(BufferedImage image, int threshold) {
    BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    result.getGraphics().drawImage(image, 0, 0, null);
    WritableRaster raster = result.getRaster();
    int[] pixels = new int[image.getWidth()];
    for (int y = 0; y < image.getHeight(); y++) {
        raster.getPixels(0, y, image.getWidth(), 1, pixels);
        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] < threshold) pixels[i] = 0;
            else pixels[i] = 255;
        }
        raster.setPixels(0, y, image.getWidth(), 1, pixels);
    }
    return result;
}
 
Example 8
Source File: RoundGradientContext.java    From chipster with MIT License 5 votes vote down vote up
public Raster getRaster(int x, int y, int w, int h) {
  WritableRaster raster =
      getColorModel().createCompatibleWritableRaster(w, h);
  
  int[] data = new int[w * h * 4];
  for (int j = 0; j < h; j++) {
    for (int i = 0; i < w; i++) {
      double distance = mPoint.distance(x + i, y + j);
      double radius = mRadius.distance(0, 0);
      double ratio = distance / radius;
      if (ratio > 1.0)
        ratio = 1.0;
    
      int base = (j * w + i) * 4;
      data[base + 0] = (int)(mC1.getRed() + ratio *
          (mC2.getRed() - mC1.getRed()));
      data[base + 1] = (int)(mC1.getGreen() + ratio *
          (mC2.getGreen() - mC1.getGreen()));
      data[base + 2] = (int)(mC1.getBlue() + ratio *
          (mC2.getBlue() - mC1.getBlue()));
      data[base + 3] = (int)(mC1.getAlpha() + ratio *
          (mC2.getAlpha() - mC1.getAlpha()));
    }
  }
  raster.setPixels(0, 0, w, h, data);
  
  return raster;
}
 
Example 9
Source File: JPEGImageWriter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 10
Source File: JPEGImageWriter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 11
Source File: JPEGImageWriter.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 12
Source File: MiscCompositeContext.java    From scrimage with Apache License 2.0 4 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    float a = 0, ac = 0;
    float alpha = this.alpha;
    int t;

    int[] srcPix = null;
    int[] dstPix = null;

    int x = dstOut.getMinX();
    int w = dstOut.getWidth();

    int y0 = dstOut.getMinY();
    int y1 = y0 + dstOut.getHeight();

    for (int y = y0; y < y1; y++) {
        srcPix = src.getPixels(x, y, w, 1, srcPix);
        dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
        int i = 0;
        int end = w * 4;

        while (i < end) {
            int sr = srcPix[i];
            int dir = dstPix[i];
            int sg = srcPix[i + 1];
            int dig = dstPix[i + 1];
            int sb = srcPix[i + 2];
            int dib = dstPix[i + 2];
            int sa = srcPix[i + 3];
            int dia = dstPix[i + 3];
            int dor, dog, dob, doa;

            switch (rule) {
                case MiscComposite.ADD:
                default:
                    dor = dir + sr;
                    if (dor > 255)
                        dor = 255;
                    dog = dig + sg;
                    if (dog > 255)
                        dog = 255;
                    dob = dib + sb;
                    if (dob > 255)
                        dob = 255;
                    break;

                case MiscComposite.SUBTRACT:
                    dor = dir - sr;
                    if (dor < 0)
                        dor = 0;
                    dog = dig - sg;
                    if (dog < 0)
                        dog = 0;
                    dob = dib - sb;
                    if (dob < 0)
                        dob = 0;
                    break;

            }

            a = alpha * sa / 255f;
            ac = 1 - a;

            dstPix[i] = (int) (a * dor + ac * dir);
            dstPix[i + 1] = (int) (a * dog + ac * dig);
            dstPix[i + 2] = (int) (a * dob + ac * dib);
            dstPix[i + 3] = (int) (sa * alpha + dia * ac);
            i += 4;
        }
        dstOut.setPixels(x, y, w, 1, dstPix);

    }
}
 
Example 13
Source File: JPEGImageWriter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 14
Source File: JPEGImageWriter.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 15
Source File: ConicalGradientPaint.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Raster getRaster(final int X, final int Y, final int TILE_WIDTH, final int TILE_HEIGHT) {
    final double ROTATION_CENTER_X = -X + CENTER.getX();
    final double ROTATION_CENTER_Y = -Y + CENTER.getY();

    final int MAX = FRACTION_ANGLES.length - 1;

    // Create raster for given colormodel
    final WritableRaster RASTER = getColorModel().createCompatibleWritableRaster(TILE_WIDTH, TILE_HEIGHT);

    // Create data array with place for red, green, blue and alpha values
    final int[] data = new int[(TILE_WIDTH * TILE_HEIGHT * 4)];

    double dx;
    double dy;
    double distance;
    double angle;
    double currentRed = 0;
    double currentGreen = 0;
    double currentBlue = 0;
    double currentAlpha = 0;

    for (int tileY = 0; tileY < TILE_HEIGHT; tileY++) {
        for (int tileX = 0; tileX < TILE_WIDTH; tileX++) {

            // Calculate the distance between the current position and the rotation angle
            dx = tileX - ROTATION_CENTER_X;
            dy = tileY - ROTATION_CENTER_Y;
            distance = Math.sqrt(dx * dx + dy * dy);

            // Avoid division by zero
            if (distance == 0) {
                distance = 1;
            }

            // 0 degree on top
            angle = Math.abs(Math.toDegrees(Math.acos(dx / distance)));

            if (dx >= 0 && dy <= 0) {
                angle = 90.0 - angle;
            } else if (dx >= 0 && dy >= 0) {
                angle += 90.0;
            } else if (dx <= 0 && dy >= 0) {
                angle += 90.0;
            } else if (dx <= 0 && dy <= 0) {
                angle = 450.0 - angle;
            }

            // Check for each angle in fractionAngles array
            for (int i = 0; i < MAX; i++) {
                if ((angle >= FRACTION_ANGLES[i])) {
                    currentRed = COLORS[i].getRed() * INT_TO_FLOAT_CONST + (angle - FRACTION_ANGLES[i]) * RED_STEP_LOOKUP[i];
                    currentGreen = COLORS[i].getGreen() * INT_TO_FLOAT_CONST + (angle - FRACTION_ANGLES[i]) * GREEN_STEP_LOOKUP[i];
                    currentBlue = COLORS[i].getBlue() * INT_TO_FLOAT_CONST + (angle - FRACTION_ANGLES[i]) * BLUE_STEP_LOOKUP[i];
                    currentAlpha = COLORS[i].getAlpha() * INT_TO_FLOAT_CONST + (angle - FRACTION_ANGLES[i]) * ALPHA_STEP_LOOKUP[i];
                }
            }

            // Fill data array with calculated color values
            final int BASE = (tileY * TILE_WIDTH + tileX) * 4;
            data[BASE] = (int) (currentRed * 255);
            data[BASE + 1] = (int) (currentGreen * 255);
            data[BASE + 2] = (int) (currentBlue * 255);
            data[BASE + 3] = (int) (currentAlpha * 255);
        }
    }

    // Fill the raster with the data
    RASTER.setPixels(0, 0, TILE_WIDTH, TILE_HEIGHT, data);

    return RASTER;
}
 
Example 16
Source File: JPEGImageWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 17
Source File: BufferedImageLuminanceSource.java    From wechatGroupRobot with GNU General Public License v3.0 4 votes vote down vote up
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
    super(width, height);

    if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
        this.image = image;
    } else {
        int sourceWidth  = image.getWidth();
        int sourceHeight = image.getHeight();
        if (left + width > sourceWidth || top + height > sourceHeight) {
            throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
        }

        this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);

        WritableRaster raster = this.image.getRaster();
        int[]          buffer = new int[width];
        for (int y = top; y < top + height; y++) {
            image.getRGB(left, y, width, 1, buffer, 0, sourceWidth);
            for (int x = 0; x < width; x++) {
                int pixel = buffer[x];

                // The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
                // black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
                // barcode image. Force any such pixel to be white:
                if ((pixel & 0xFF000000) == 0) {
                    pixel = 0xFFFFFFFF;
                }

                // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
                // (306*R) >> 10 is approximately equal to R*0.299, and so on.
                // 0x200 >> 10 is 0.5, it implements rounding.
                buffer[x] =
                        (306 * ((pixel >> 16) & 0xFF) +
                                601 * ((pixel >> 8) & 0xFF) +
                                117 * (pixel & 0xFF) +
                                0x200) >> 10;
            }
            raster.setPixels(left, y, width, 1, buffer);
        }

    }
    this.left = left;
    this.top = top;
}
 
Example 18
Source File: PaletteColorChooserPaint.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public PaintContext createContext ( final ColorModel cm, final Rectangle deviceBounds, final Rectangle2D userBounds,
                                    final AffineTransform xform, final RenderingHints hints )
{
    return new PaintContext ()
    {
        private Map<Rectangle, WritableRaster> rastersCache = new HashMap<Rectangle, WritableRaster> ();

        @Override
        public void dispose ()
        {
            rastersCache.clear ();
        }

        @Override
        public ColorModel getColorModel ()
        {
            return model;
        }

        @Override
        public Raster getRaster ( int x, int y, final int w, final int h )
        {
            final Rectangle r = new Rectangle ( x, y, w, h );
            if ( rastersCache.containsKey ( r ) )
            {
                return rastersCache.get ( r );
            }
            else
            {
                final WritableRaster raster = model.createCompatibleWritableRaster ( w, h );

                x -= Math.round ( xform.getTranslateX () );
                y -= Math.round ( xform.getTranslateY () );

                final int[] data = new int[ w * h * 4 ];
                for ( int j = 0; j < h; j++ )
                {
                    for ( int i = 0; i < w; i++ )
                    {
                        final int base = ( j * w + i ) * 4;
                        data[ base ] = getRed ( x + i, y + j );
                        data[ base + 1 ] = getGreen ( x + i, y + j );
                        data[ base + 2 ] = getBlue ( x + i, y + j );
                        data[ base + 3 ] = 255;
                    }
                }
                raster.setPixels ( 0, 0, w, h, data );

                rastersCache.put ( r, raster );
                return raster;
            }
        }
    };
}
 
Example 19
Source File: JPEGImageWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}
 
Example 20
Source File: JPEGImageWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Put the scanline y of the source ROI view Raster into the
 * 1-line Raster for writing.  This handles ROI and band
 * rearrangements, and expands indexed images.  Subsampling is
 * done in the native code.
 * This is called by the native code.
 */
private void grabPixels(int y) {

    Raster sourceLine = null;
    if (indexed) {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        new int [] {0});
        // If the image has BITMASK transparency, we need to make sure
        // it gets converted to 32-bit ARGB, because the JPEG encoder
        // relies upon the full 8-bit alpha channel.
        boolean forceARGB =
            (indexCM.getTransparency() != Transparency.OPAQUE);
        BufferedImage temp = indexCM.convertToIntDiscrete(sourceLine,
                                                          forceARGB);
        sourceLine = temp.getRaster();
    } else {
        sourceLine = srcRas.createChild(sourceXOffset,
                                        sourceYOffset+y,
                                        sourceWidth, 1,
                                        0, 0,
                                        srcBands);
    }
    if (convertTosRGB) {
        if (debug) {
            System.out.println("Converting to sRGB");
        }
        // The first time through, converted is null, so
        // a new raster is allocated.  It is then reused
        // on subsequent lines.
        converted = convertOp.filter(sourceLine, converted);
        sourceLine = converted;
    }
    if (isAlphaPremultiplied) {
        WritableRaster wr = sourceLine.createCompatibleWritableRaster();
        int[] data = null;
        data = sourceLine.getPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                                    sourceLine.getWidth(), sourceLine.getHeight(),
                                    data);
        wr.setPixels(sourceLine.getMinX(), sourceLine.getMinY(),
                     sourceLine.getWidth(), sourceLine.getHeight(),
                     data);
        srcCM.coerceData(wr, false);
        sourceLine = wr.createChild(wr.getMinX(), wr.getMinY(),
                                    wr.getWidth(), wr.getHeight(),
                                    0, 0,
                                    srcBands);
    }
    raster.setRect(sourceLine);
    if ((y > 7) && (y%8 == 0)) {  // Every 8 scanlines
        cbLock.lock();
        try {
            processImageProgress((float) y / (float) sourceHeight * 100.0F);
        } finally {
            cbLock.unlock();
        }
    }
}