Skip to content

Instantly share code, notes, and snippets.

@paolorotolo
Created May 16, 2017 15:11
Show Gist options
  • Save paolorotolo/8e5ae0634311cdb70c0549cdb2ffadb2 to your computer and use it in GitHub Desktop.
Save paolorotolo/8e5ae0634311cdb70c0549cdb2ffadb2 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
File f = new File("/home/paolorotolo/anna2.bmp");
Path path = Paths.get(f.toURI());
byte[] bytes = null;
try {
bytes = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer buffer = null;
byte[] widthBytes = Arrays.copyOfRange(bytes, 18, 22);
System.out.println(widthBytes.length);
int width = buffer.wrap(widthBytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println("Width is: " + width);
byte[] heightBytes = Arrays.copyOfRange(bytes, 22, 26);
int height = buffer.wrap(heightBytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println("Height is " + height);
System.out.println("Size is " + bytes.length);
byte[] headerBytes = Arrays.copyOfRange(bytes, 0, 1078);
byte[] imageByte = Arrays.copyOfRange(bytes, 1079, bytes.length);
for (int i = 0; i < imageByte.length; i++) {
int convertedValue = ((int) imageByte[i] & 0xFF);
imageByte[i] = (byte) (255 - convertedValue);
}
try {
FileOutputStream fos = new FileOutputStream("convertito.bpm");
fos.write(headerBytes);
fos.write(imageByte);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment