import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class SignedToUnsigned { public SignedToUnsigned(String srcImgName, String tgtImgName) { try { byte[] srcBuffer = new byte[4096]; byte[] tgtBuffer = new byte[4096]; // Source data File srcImgFile = new File(srcImgName); FileInputStream srcImgStream = new FileInputStream(srcImgFile); long srcDataSize = srcImgFile.length(); // Target data File tgtImgFile = new File(tgtImgName); FileOutputStream tgtImgStream = new FileOutputStream(tgtImgFile); // Read data into the buffer until the end of the source file long totalBytesRead = 0; while (totalBytesRead < srcDataSize) { // Read source data into the buffer int bytesRead = (int)srcImgStream.read(srcBuffer); totalBytesRead += bytesRead; // Convert signed shorts to unsigned shorts for (int i = 0; i < bytesRead; i+=2) { byte b1 = srcBuffer[i]; byte b2 = srcBuffer[i+1]; int s = (b1 << 8) + (b2 << 0); // Map (-32768,32767) to (0,32767) s = (s + 32768)/2; tgtBuffer[i] = (byte)((s >>> 8) & 0xFF); tgtBuffer[i+1] = (byte)(s & 0xFF); } // Write the target data to the new file tgtImgStream.write(tgtBuffer, 0, bytesRead); } // Close tgtImgStream.flush(); tgtImgStream.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { SignedToUnsigned stu = new SignedToUnsigned(args[0], args[1]); } }