*/
private static ImageLayout layoutHelper(String URLSpec,
int level,
int subImage) {
// Create an ImageLayout by construction or cloning.
ImageLayout il = new ImageLayout();
// Set the tile offsets to (0,0).
il.setTileGridXOffset(0);
il.setTileGridYOffset(0);
// Set the tile dimensions.
il.setTileWidth(TILE_SIZE);
il.setTileHeight(TILE_SIZE);
// Set the image origin to (0,0).
il.setMinX(0);
il.setMinY(0);
// Retrieve the number of resolutions available and the maximum
// width and height (the dimensions of resolution numRes - 1).
int maxWidth = -1;
int maxHeight = -1;
int numRes = -1;
int resolution = -1;
String[] cmd = new String[] {"OBJ=Max-size", "OBJ=Resolution-number"};
InputStream stream = postCommands(URLSpec, cmd);
String label = null;
while((label = getLabel(stream)) != null) {
if(label.equals("max-size")) {
String data = getDataAsString(stream, false);
int[] wh = stringToIntArray(data);
maxWidth = wh[0];
maxHeight = wh[1];
} else if(label.equals("resolution-number")) {
String data = getDataAsString(stream, false);
numRes = Integer.valueOf(data).intValue();
if(level < 0) {
resolution = 0;
} else if(level >= numRes) {
resolution = numRes - 1;
} else {
resolution = level;
}
} else {
checkError(label, stream, true);
}
}
closeStream(stream);
// Derive the width and height for this resolution level.
int w = maxWidth;
int h = maxHeight;
for(int i = numRes - 1; i > resolution; i--) {
w = (w + 1)/2;
h = (h + 1)/2;
}
il.setWidth(w);
il.setHeight(h);
// Determine image opacity attributes.
boolean hasAlpha = false;
boolean isAlphaPremultiplied = false;
cmd = new String[] {"OBJ=Colorspace,"+resolution+","+subImage};
stream = postCommands(URLSpec, cmd);
int colorSpaceIndex = 0;
int numBands = 0;
while((label = getLabel(stream)) != null) {
if(label.startsWith("colorspace")) {
int[] ia = stringToIntArray(getDataAsString(stream, false));
numBands = ia[3];
switch(ia[2]) {
case CS_MONOCHROME:
colorSpaceIndex = ColorSpace.CS_GRAY;
break;
case CS_PHOTOYCC:
colorSpaceIndex = ColorSpace.CS_PYCC;
break;
case CS_NIFRGB:
colorSpaceIndex = ColorSpace.CS_sRGB;
break;
default:
colorSpaceIndex = numBands < 3 ?
ColorSpace.CS_GRAY : ColorSpace.CS_sRGB;
}
for(int j = 1; j <= numBands; j++) {
if(ia[3+j] == CS_PLANE_ALPHA) {
hasAlpha = true;
}
}
isAlphaPremultiplied = ia[1] == 1;
} else {
checkError(label, stream, true);
}
}
closeStream(stream);
// Set the ColorModel.
ColorSpace cs = ColorSpace.getInstance(colorSpaceIndex);
int dtSize = DataBuffer.getDataTypeSize(DataBuffer.TYPE_BYTE);
int[] bits = new int[numBands];
for(int i = 0; i < numBands; i++) {
bits[i] = dtSize;
}
int transparency = hasAlpha ?
Transparency.TRANSLUCENT : Transparency.OPAQUE;
ColorModel cm = new ComponentColorModel(cs, bits,
hasAlpha, isAlphaPremultiplied,
transparency,
DataBuffer.TYPE_BYTE);
il.setColorModel(cm);
// Set the SampleModel.
int[] bandOffsets = new int[numBands];
for(int i = 0; i < numBands; i++) {
bandOffsets[i] = i;
}
il.setSampleModel(RasterFactory.createPixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE,
TILE_SIZE,
TILE_SIZE,
numBands,
numBands*TILE_SIZE,