use of javax.imageio.plugins.jpeg.JPEGImageReadParam in project jdk8u_jdk by JetBrains.
the class ImageTypeProducer method readInternal.
private Raster readInternal(int imageIndex, ImageReadParam param, boolean wantRaster) throws IOException {
readHeader(imageIndex, false);
WritableRaster imRas = null;
int numImageBands = 0;
if (!wantRaster) {
// Can we read this image?
Iterator imageTypes = getImageTypes(imageIndex);
if (imageTypes.hasNext() == false) {
throw new IIOException("Unsupported Image Type");
}
image = getDestination(param, imageTypes, width, height);
imRas = image.getRaster();
// The destination may still be incompatible.
numImageBands = image.getSampleModel().getNumBands();
// Check whether we can handle any implied color conversion
// Throws IIOException if the stream and the image are
// incompatible, and sets convert if a java conversion
// is necessary
checkColorConversion(image, param);
// Check the source and destination bands in the param
checkReadParamBandSettings(param, numComponents, numImageBands);
} else {
// Set the output color space equal to the input colorspace
// This disables all conversions
setOutColorSpace(structPointer, colorSpaceCode);
image = null;
}
// Create an intermediate 1-line Raster that will hold the decoded,
// subsampled, clipped, band-selected image data in a single
// byte-interleaved buffer. The above transformations
// will occur in C for performance. Every time this Raster
// is filled we will call back to acceptPixels below to copy
// this to whatever kind of buffer our image has.
int[] srcBands = JPEG.bandOffsets[numComponents - 1];
int numRasterBands = (wantRaster ? numComponents : numImageBands);
destinationBands = null;
Rectangle srcROI = new Rectangle(0, 0, 0, 0);
destROI = new Rectangle(0, 0, 0, 0);
computeRegions(param, width, height, image, srcROI, destROI);
int periodX = 1;
int periodY = 1;
minProgressivePass = 0;
maxProgressivePass = Integer.MAX_VALUE;
if (param != null) {
periodX = param.getSourceXSubsampling();
periodY = param.getSourceYSubsampling();
int[] sBands = param.getSourceBands();
if (sBands != null) {
srcBands = sBands;
numRasterBands = srcBands.length;
}
if (!wantRaster) {
// ignore dest bands for Raster
destinationBands = param.getDestinationBands();
}
minProgressivePass = param.getSourceMinProgressivePass();
maxProgressivePass = param.getSourceMaxProgressivePass();
if (param instanceof JPEGImageReadParam) {
JPEGImageReadParam jparam = (JPEGImageReadParam) param;
if (jparam.areTablesSet()) {
abbrevQTables = jparam.getQTables();
abbrevDCHuffmanTables = jparam.getDCHuffmanTables();
abbrevACHuffmanTables = jparam.getACHuffmanTables();
}
}
}
int lineSize = destROI.width * numRasterBands;
buffer = new DataBufferByte(lineSize);
int[] bandOffs = JPEG.bandOffsets[numRasterBands - 1];
raster = Raster.createInterleavedRaster(buffer, destROI.width, 1, lineSize, numRasterBands, bandOffs, null);
// target Raster that will permit a simple setRect for each scanline
if (wantRaster) {
target = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, destROI.width, destROI.height, lineSize, numRasterBands, bandOffs, null);
} else {
target = imRas;
}
int[] bandSizes = target.getSampleModel().getSampleSize();
for (int i = 0; i < bandSizes.length; i++) {
if (bandSizes[i] <= 0 || bandSizes[i] > 8) {
throw new IIOException("Illegal band size: should be 0 < size <= 8");
}
}
/*
* If the process is sequential, and we have restart markers,
* we could skip to the correct restart marker, if the library
* lets us. That's an optimization to investigate later.
*/
// Check for update listeners (don't call back if none)
boolean callbackUpdates = ((updateListeners != null) || (progressListeners != null));
// Set up progression data
initProgressData();
// and set knownPassCount
if (imageIndex == imageMetadataIndex) {
// We have metadata
knownPassCount = 0;
for (Iterator iter = imageMetadata.markerSequence.iterator(); iter.hasNext(); ) {
if (iter.next() instanceof SOSMarkerSegment) {
knownPassCount++;
}
}
}
progInterval = Math.max((target.getHeight() - 1) / 20, 1);
if (knownPassCount > 0) {
progInterval *= knownPassCount;
} else if (maxProgressivePass != Integer.MAX_VALUE) {
progInterval *= (maxProgressivePass - minProgressivePass + 1);
}
if (debug) {
System.out.println("**** Read Data *****");
System.out.println("numRasterBands is " + numRasterBands);
System.out.print("srcBands:");
for (int i = 0; i < srcBands.length; i++) System.out.print(" " + srcBands[i]);
System.out.println();
System.out.println("destination bands is " + destinationBands);
if (destinationBands != null) {
for (int i = 0; i < destinationBands.length; i++) {
System.out.print(" " + destinationBands[i]);
}
System.out.println();
}
System.out.println("sourceROI is " + srcROI);
System.out.println("destROI is " + destROI);
System.out.println("periodX is " + periodX);
System.out.println("periodY is " + periodY);
System.out.println("minProgressivePass is " + minProgressivePass);
System.out.println("maxProgressivePass is " + maxProgressivePass);
System.out.println("callbackUpdates is " + callbackUpdates);
}
// Finally, we are ready to read
processImageStarted(currentImage);
boolean aborted = false;
// Note that getData disables acceleration on buffer, but it is
// just a 1-line intermediate data transfer buffer that will not
// affect the acceleration of the resulting image.
aborted = readImage(structPointer, buffer.getData(), numRasterBands, srcBands, bandSizes, srcROI.x, srcROI.y, srcROI.width, srcROI.height, periodX, periodY, abbrevQTables, abbrevDCHuffmanTables, abbrevACHuffmanTables, minProgressivePass, maxProgressivePass, callbackUpdates);
if (aborted) {
processReadAborted();
} else {
processImageComplete();
}
return target;
}
Aggregations