# there and back again a byte to double's tale (or a conversion question)

• 01-23-2003, 12:08 PM
Elvis
there and back again a byte to double's tale (or a conversion question)
Hello everybody, I'm having a little trouble converting a double back to
byte form. here is the code i use to convert the byte to a double and store
it in an array

private double[][] getBytePixels(ByteProcessor ip, int n) {
byte[] pix = (byte[]) ip.getPixels();
double[][] pix2d = new double[n][n];

for (int j = 0 ; j < n; j++) {
int offs = j * ip.getWidth();
for (int i = 0 ; i < n; i++) {
// conversions taking place here
pix2d[i][j] = (double) (pix[offs + i] & 0xff);
pix2d[i][j] = (pix2d[i][j] - 128) / 128.0;
}
}

return pix2d;
} // getBytePixels

this works. But after i'm done processing I need to return the pixel array
from a double to byte array and this bit of code isn't working. Does anyone
have a clue why?

private void setBytePixels(ByteProcessor ip, double[][] dp) {
ByteProcessor bp = new ByteProcessor(dp.length, dp[0].length);
byte[] pix = new byte[dp.length * dp[0].length];

for (int j = 0; j < dp.length; j++) {
int offs = j * dp.length;
for (int i = 0; i < dp.length; i++) {
// conversions taking place here - but it's not right :(
dp[i][j] = (dp[i][j] + 128) * 128.0;
pix[offs + i] = (byte) (dp[i][j]);
pix[offs + i] &= 0xff; // I think the problems here is this
the inverse
}
}

bp.setPixels(pix);
ip.insert(bp, 0, 0);
} // setBytePixels

I'm new to Java so feel free to offer up anything
TIA.