下面列出了java.awt.image.Raster#createCompatibleWritableRaster() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
static synchronized WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
@NotNull
@Override
public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) {
Raster src = bufferedImage.getData();
WritableRaster dest = src.createCompatibleWritableRaster();
bufferedImage.copyData(dest);
for (int y = pixelateSection.y; y < pixelateSection.y + pixelateSection.getHeight(); y += PIX_SIZE) {
for (int x = pixelateSection.x; x < pixelateSection.x + pixelateSection.getWidth(); x += PIX_SIZE) {
double[] pixel = new double[3];
pixel = src.getPixel(x, y, pixel);
for (int yd = y; (yd < y + PIX_SIZE) && (yd < dest.getHeight()); yd++) {
for (int xd = x; (xd < x + PIX_SIZE) && (xd < dest.getWidth()); xd++) {
dest.setPixel(xd, yd, pixel);
}
}
}
}
bufferedImage.setData(dest);
return bufferedImage;
}
private WritableRaster fromYCCKtoCMYK(Raster raster)
{
WritableRaster writableRaster = raster.createCompatibleWritableRaster();
int[] value = new int[4];
for (int y = 0, height = raster.getHeight(); y < height; y++)
{
for (int x = 0, width = raster.getWidth(); x < width; x++)
{
raster.getPixel(x, y, value);
// 4-channels 0..255
float Y = value[0];
float Cb = value[1];
float Cr = value[2];
float K = value[3];
// YCCK to RGB, see http://software.intel.com/en-us/node/442744
int r = clamp(Y + 1.402f * Cr - 179.456f);
int g = clamp(Y - 0.34414f * Cb - 0.71414f * Cr + 135.45984f);
int b = clamp(Y + 1.772f * Cb - 226.816f);
// naive RGB to CMYK
int cyan = 255 - r;
int magenta = 255 - g;
int yellow = 255 - b;
// update new raster
value[0] = cyan;
value[1] = magenta;
value[2] = yellow;
value[3] = (int)K;
writableRaster.setPixel(x, y, value);
}
}
return writableRaster;
}
private static Raster flipRaster(Raster r) {
int w = r.getWidth();
int h = r.getHeight();
WritableRaster rf = r.createCompatibleWritableRaster(r.getMinX(),r.getMinY(), w,h);
int[] p = new int[w*3];
for (int y=r.getMinY(); y<r.getMinY()+h; y++) {
p = r.getPixels(r.getMinX(),y,w,1,p);
rf.setPixels(r.getMinX(),r.getMinY()+h-(y-r.getMinY())-1,w,1,p);
}
return rf;
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}