Tôi có một phương thức chuyển đổi BufferedImages loại nào là TYPE_CUSTOM thành TYPE_INT_RGB. Tôi đang sử dụng các mã sau đây, tuy nhiên tôi thực sự muốn tìm một cách nhanh hơn để làm điều này.Thay thế nhanh hơn cho ColorConvertOp
BufferedImage newImg = new BufferedImage(
src.getWidth(),
src.getHeight(),
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, newImg);
Nó hoạt động tốt, tuy nhiên nó khá chậm và tôi tự hỏi nếu có một cách nhanh hơn để thực hiện chuyển đổi này.
ColorModel Trước khi chuyển đổi:
ColorModel: #pixelBits = 24 numComponents = 3 color space = [email protected] transparency = 1 has alpha = false isAlphaPre = false
ColorModel Sau khi chuyển đổi:
DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
Cảm ơn!
Cập nhật:
Hóa ra làm việc với các dữ liệu pixel nguyên bản là cách tốt nhất. Kể từ khi TYPE_CUSTOM thực sự RGB chuyển đổi nó bằng tay là đơn giản và nhanh hơn khoảng 95% so với ColorConvertOp.
public static BufferedImage makeCompatible(BufferedImage img) throws IOException {
// Allocate the new image
BufferedImage dstImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
// Check if the ColorSpace is RGB and the TransferType is BYTE.
// Otherwise this fast method does not work as expected
ColorModel cm = img.getColorModel();
if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB && img.getRaster().getTransferType() == DataBuffer.TYPE_BYTE) {
//Allocate arrays
int len = img.getWidth()*img.getHeight();
byte[] src = new byte[len*3];
int[] dst = new int[len];
// Read the src image data into the array
img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), src);
// Convert to INT_RGB
int j = 0;
for (int i=0; i<len; i++) {
dst[i] = (((int)src[j++] & 0xFF) << 16) |
(((int)src[j++] & 0xFF) << 8) |
(((int)src[j++] & 0xFF));
}
// Set the dst image data
dstImage.getRaster().setDataElements(0, 0, img.getWidth(), img.getHeight(), dst);
return dstImage;
}
ColorConvertOp op = new ColorConvertOp(null);
op.filter(img, dstImage);
return dstImage;
}
Đẹp! Làm việc với dữ liệu thô cắt giảm thời gian xử lý 95% !! Xem bài đăng đã chỉnh sửa để biết chính xác cách tôi kết thúc việc này. –
Tôi đã không kết thúc bằng cách sử dụng các lớp ByteInterleavedRaster và IntegerInterleavedRaster vì một số lý do tôi không có các gói mặt trời. –
Holy crap Tôi vừa mới xử lý tệp 100MB + psd trong 10 phút đến 8 giây. Cảm ơn! – EdgeCaseBerg