T
Tom Peel
I'm trying to use these classes directly for the first time and not
quite getting the results I expect. The application reads a file
containing terrain altitude data and is supposed to display an image
with the terrain altitudes represented by different colours- green
fading to yellow to brown.
// I create a SampleModel, DataBuffer and WritableRaster:
SampleModel sm = new SinglePixelPackedSampleModel(
DataBuffer.TYPE_INT, stdX, stdY, new int[] {0xFF000000, 0xFF0000,
0xFF00, 0xFF});
DataBuffer db = new DataBufferInt(stdX*stdY);
WritableRaster wr = Raster.createWritableRaster(sm, db, null);
float hue, sat, bri;
int iRGB;
int hgt;
float hgtff;
int k = 0;
// this loop maps the terrain data from hgtdata in HSB values and sets
the elements in the DataBuffer:
for (iy = 0; iy<stdY; iy++) {
for (ix = 0; ix<stdX; ix++) {
hgt = hgtdata[ix][iy];
hgtff = hgt/2000.0F;
hue = 0.22F - 0.05F*hgtff;
sat = 1.00F - 0.45F*hgtff;
bri = 1.00F - 0.13F*hgtff;
iRGB = Color.HSBtoRGB(hue, sat, bri);
db.setElem(k, iRGB);
k++;
}
}
bi = new BufferedImage(stdX, stdY, BufferedImage.TYPE_INT_ARGB);
bi.setData(wr);
// at another part of the program there is a PaintComponent that calls
g2d.drawImage(bi, . . .)
The effect is that the image is drawn, but the colours are wrong.
Looking at the loop with Eclipse debug, I see for example a value of
hgt=136 giving a hue of 0.22F, which should be bright green, and iRGB =
-4716291. The resulting colour however is an extremely washed out
pale purple-magenta. The values in the hgtdata array are correct, BTW.
Reading the Color.HSBtoRGB documentation, hue=0.22F should give me 80
degrees, which is green.
Is the bitmap array in SinglePixelPackedSampleModel correct? I could not
find any examples. Should I be setting the alpha chanel? I really don't
need alpha, but I'm having difficulty understanding the intricacies of
the raster system, and using ARGB seems to be easiest. I tried RGB but
had no success getting the program to work at all.
Help/advice much appreciated.
T.
quite getting the results I expect. The application reads a file
containing terrain altitude data and is supposed to display an image
with the terrain altitudes represented by different colours- green
fading to yellow to brown.
// I create a SampleModel, DataBuffer and WritableRaster:
SampleModel sm = new SinglePixelPackedSampleModel(
DataBuffer.TYPE_INT, stdX, stdY, new int[] {0xFF000000, 0xFF0000,
0xFF00, 0xFF});
DataBuffer db = new DataBufferInt(stdX*stdY);
WritableRaster wr = Raster.createWritableRaster(sm, db, null);
float hue, sat, bri;
int iRGB;
int hgt;
float hgtff;
int k = 0;
// this loop maps the terrain data from hgtdata in HSB values and sets
the elements in the DataBuffer:
for (iy = 0; iy<stdY; iy++) {
for (ix = 0; ix<stdX; ix++) {
hgt = hgtdata[ix][iy];
hgtff = hgt/2000.0F;
hue = 0.22F - 0.05F*hgtff;
sat = 1.00F - 0.45F*hgtff;
bri = 1.00F - 0.13F*hgtff;
iRGB = Color.HSBtoRGB(hue, sat, bri);
db.setElem(k, iRGB);
k++;
}
}
bi = new BufferedImage(stdX, stdY, BufferedImage.TYPE_INT_ARGB);
bi.setData(wr);
// at another part of the program there is a PaintComponent that calls
g2d.drawImage(bi, . . .)
The effect is that the image is drawn, but the colours are wrong.
Looking at the loop with Eclipse debug, I see for example a value of
hgt=136 giving a hue of 0.22F, which should be bright green, and iRGB =
-4716291. The resulting colour however is an extremely washed out
pale purple-magenta. The values in the hgtdata array are correct, BTW.
Reading the Color.HSBtoRGB documentation, hue=0.22F should give me 80
degrees, which is green.
Is the bitmap array in SinglePixelPackedSampleModel correct? I could not
find any examples. Should I be setting the alpha chanel? I really don't
need alpha, but I'm having difficulty understanding the intricacies of
the raster system, and using ARGB seems to be easiest. I tried RGB but
had no success getting the program to work at all.
Help/advice much appreciated.
T.