Hi all,
I've got some old images that have a four byte header giving the image width and height. I'm trying to import the stacks via a macro that would read those bytes and pass them on to a call to open a Raw stack. This works fine for some of my stacks, but not others. The charCodeAt function doesn't seem to reliably report the byte value. If I print out the first four bytes of a non-working image, I get: 225,1,63,1. If I do an octal dump from the command line (od -t u1 -N 4) on the same file, I get: 135,1,63,1. The octal dump values give me the right width and height calculations. Is there a way for me to get at those first 4 bytes without invoking charCodeAt? I've included my code snippet in case anyone was interested. // Open a file and get x & y sizes myFile = File.openDialog("Choose an MCID file"); str = File.openAsRawString(myFile); wide = charCodeAt(str, 0) + 256 * charCodeAt(str, 1) + 1; high = charCodeAt(str, 2) + 256 * charCodeAt(str, 3) + 1; // Open all files in the directory run("Raw...", "open="+myFile+" image=8-bit width="+wide+" height="+high+ " offset=4 number=1 gap=0 open"); Thanks, Melissa -- Melissa A. Krueger, Ph.D. Div Pulmonary & Critical Care Medicine University of Washington Seattle, WA 98195-6522 |
Hi Melissa,
what about using an InputStream instead? String operations always suffer from the problem of different character sets, and you may get conversion between them. See http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html myInputStream = new FileInputStream(myFile); wide = (myInputStream.read() & 255) + 256 * (myInputStream.read() & 255); or myInputStream = new FileInputStream(myFile); byte[] myByteArray = new byte[4]; nBytesRead = myInputStream.read(myByteArray); //error checking for nBytesRead < 4 bytes should go here wide = (myByteArray[0] & 255) + 256 * (myByteArray[1] & 255); See the readShort() method of AVI_Reader.java for slightly nicer code for 2-byte -> short conversion in Intel byte order. http://rsb.info.nih.gov/ij/source/ij/plugin/AVI_Reader.java Hope this helps, Michael ________________________________________________________________ On 29 Apr 2009, at 00:50, Melissa Krueger wrote: > Hi all, > > I've got some old images that have a four byte header giving the > image width and height. I'm trying to import the stacks via a macro > that would read those bytes and pass them on to a call to open a > Raw stack. This works fine for some of my stacks, but not others. > > The charCodeAt function doesn't seem to reliably report the byte > value. If I print out the first four bytes of a non-working image, > I get: 225,1,63,1. If I do an octal dump from the command line (od - > t u1 -N 4) on the same file, I get: 135,1,63,1. The octal dump > values give me the right width and height calculations. > > Is there a way for me to get at those first 4 bytes without > invoking charCodeAt? > > I've included my code snippet in case anyone was interested. > > // Open a file and get x & y sizes > myFile = File.openDialog("Choose an MCID file"); > str = File.openAsRawString(myFile); > wide = charCodeAt(str, 0) + 256 * charCodeAt(str, 1) + 1; > high = charCodeAt(str, 2) + 256 * charCodeAt(str, 3) + 1; > > // Open all files in the directory > run("Raw...", "open="+myFile+" image=8-bit width="+wide+" > height="+high+ > " offset=4 number=1 gap=0 open"); > > Thanks, > Melissa > > > -- > Melissa A. Krueger, Ph.D. > Div Pulmonary & Critical Care Medicine > University of Washington > Seattle, WA 98195-6522 |
In reply to this post by Melissa Krueger
The charCodeAt() macro function should work as expected in the v1.42o
daily build, which fixes a bug in File.openAsRawString() that caused it to not correctly read bytes with values greater than 127. -wayne On Apr 28, 2009, at 6:50 PM, Melissa Krueger wrote: > Hi all, > > I've got some old images that have a four byte header giving the image > width and height. I'm trying to import the stacks via a macro that > would read those bytes and pass them on to a call to open a Raw stack. > This works fine for some of my stacks, but not others. > > The charCodeAt function doesn't seem to reliably report the byte > value. If I print out the first four bytes of a non-working image, I > get: 225,1,63,1. If I do an octal dump from the command line (od -t u1 > -N 4) on the same file, I get: 135,1,63,1. The octal dump values give > me the right width and height calculations. > > Is there a way for me to get at those first 4 bytes without invoking > charCodeAt? > > I've included my code snippet in case anyone was interested. > > // Open a file and get x & y sizes > myFile = File.openDialog("Choose an MCID file"); > str = File.openAsRawString(myFile); > wide = charCodeAt(str, 0) + 256 * charCodeAt(str, 1) + 1; > high = charCodeAt(str, 2) + 256 * charCodeAt(str, 3) + 1; > > // Open all files in the directory > run("Raw...", "open="+myFile+" image=8-bit width="+wide+" > height="+high+ > " offset=4 number=1 gap=0 open"); > > Thanks, > Melissa > > > -- > Melissa A. Krueger, Ph.D. > Div Pulmonary & Critical Care Medicine > University of Washington > Seattle, WA 98195-6522 > |
That works perfectly. Thanks!
Melissa On Apr 29, 2009, at 7:24 AM, Wayne Rasband wrote: > The charCodeAt() macro function should work as expected in the > v1.42o daily build, which fixes a bug in File.openAsRawString() that > caused it to not correctly read bytes with values greater than 127. > > -wayne > > On Apr 28, 2009, at 6:50 PM, Melissa Krueger wrote: > >> Hi all, >> >> I've got some old images that have a four byte header giving the >> image width and height. I'm trying to import the stacks via a macro >> that would read those bytes and pass them on to a call to open a >> Raw stack. This works fine for some of my stacks, but not others. >> >> The charCodeAt function doesn't seem to reliably report the byte >> value. If I print out the first four bytes of a non-working image, >> I get: 225,1,63,1. If I do an octal dump from the command line (od - >> t u1 -N 4) on the same file, I get: 135,1,63,1. The octal dump >> values give me the right width and height calculations. >> >> Is there a way for me to get at those first 4 bytes without >> invoking charCodeAt? >> >> I've included my code snippet in case anyone was interested. >> >> // Open a file and get x & y sizes >> myFile = File.openDialog("Choose an MCID file"); >> str = File.openAsRawString(myFile); >> wide = charCodeAt(str, 0) + 256 * charCodeAt(str, 1) + 1; >> high = charCodeAt(str, 2) + 256 * charCodeAt(str, 3) + 1; >> >> // Open all files in the directory >> run("Raw...", "open="+myFile+" image=8-bit width="+wide+" >> height="+high+ >> " offset=4 number=1 gap=0 open"); >> >> Thanks, >> Melissa >> >> >> -- >> Melissa A. Krueger, Ph.D. >> Div Pulmonary & Critical Care Medicine >> University of Washington >> Seattle, WA 98195-6522 >> |
In reply to this post by Melissa Krueger
Hi Melissa,
I'd try just opening the image temporarily with the File.openAsRawString(path, 4) function to read the 1st 4 bytes. Then close the file and reopen it as a raw image as before. -- Harry Parker Senior Imaging Systems Engineer ________________________________ From: Melissa Krueger <[hidden email]> To: [hidden email] Sent: Tuesday, April 28, 2009 6:50:50 PM Subject: Reading bytes from a file Hi all, I've got some old images that have a four byte header giving the image width and height. I'm trying to import the stacks via a macro that would read those bytes and pass them on to a call to open a Raw stack. This works fine for some of my stacks, but not others. The charCodeAt function doesn't seem to reliably report the byte value. If I print out the first four bytes of a non-working image, I get: 225,1,63,1. If I do an octal dump from the command line (od -t u1 -N 4) on the same file, I get: 135,1,63,1. The octal dump values give me the right width and height calculations. Is there a way for me to get at those first 4 bytes without invoking charCodeAt? I've included my code snippet in case anyone was interested. // Open a file and get x & y sizes myFile = File.openDialog("Choose an MCID file"); str = File.openAsRawString(myFile); wide = charCodeAt(str, 0) + 256 * charCodeAt(str, 1) + 1; high = charCodeAt(str, 2) + 256 * charCodeAt(str, 3) + 1; // Open all files in the directory run("Raw...", "open="+myFile+" image=8-bit width="+wide+" height="+high+ " offset=4 number=1 gap=0 open"); Thanks, Melissa --Melissa A. Krueger, Ph.D. Div Pulmonary & Critical Care Medicine University of Washington Seattle, WA 98195-6522 |
Free forum by Nabble | Edit this page |