https://beastit.tistory.com/36
글 시작 전에 위에 글을 먼저 보면 왜? 하는지 알 수 있다.
✅ 비트맵 이미지로 필요한 데이터 추출하기
맨 위부터 보면 Signature(2byte), File Size (4byte)...... 각 층마다 4byt의 해당 정보들을 가지고 있다.
우리가 필요한 정보는 Image Width, Height의 데이터를 추출하고 싶다.
19~22바이트, 23~26바이트에 접근하면 추출 할 수 있음
" 1314x736 "
import java.io.FileInputStream;
import java.io.IOException;
public class homework {
public static void main(String[] args) throws IOException {
// 4바이트 데이터 읽기
int n1 , n2, n3, n4 ;
int n5 , n6, n7 , n8 ;
// Image Width & Height
{
FileInputStream fis = new FileInputStream("res/pic1.bmp");
//필요없는 데이터 건너뛰
//Signature
fis.read();
fis.read();
//File Size
fis.read();
fis.read();
fis.read();
fis.read();
//Reserved 1,2
fis.read();
fis.read();
fis.read();
fis.read();
//File Offset to PixelArray
fis.read();
fis.read();
fis.read();
fis.read();
//DIB Header Size
fis.read();
fis.read();
fis.read();
fis.read();
//Image Width
n1=fis.read();
n2=fis.read();
n3=fis.read();
n4=fis.read();
//Image Height
n5=fis.read();
n6=fis.read();
n7=fis.read();
n8=fis.read();
int width = n1 << 0 | n2 << 8 | n3 << 16 | n4 << 24;
int height = n5 << 0 | n6 << 8 | n7 << 16 | n8 << 24;
System.out.printf("Width : %d, Height : %d\n",width,height );
}
}
}
✅ 결과
비트맵 파일을 바이너리 에디터로 보면 Image Width (19~22바이트), Image Height (23~26바이트)
🔸 Image Width : 034 005 000 000
🔸 Image Height : 224 002 000 000
인텔 CPU 계열에서는 낮은 주소에 데이터의 낮은 바이트부터 저장하는 방식인 "리틀 엔디안(Little Endian)" 방식으로 저장한다.
우리는 이것을 낮은 주소에 데이터의 높은 바이트부터 저장하는 방식인 "빅 엔디안(Big Endian)" 방식으로 비트연산자를 써서 저장할 것이다.
int width = n1 << 0 | n2 << 8 | n3 << 16 | n4 << 24;
int height = n5 << 0 | n6 << 8 | n7 << 16 | n8 << 24;
- n1 : 0010 0010 (34)
- n2 : 0000 0101 0000 0000 (5)
- n3 : 0000 0000 0000 0000 0000 0000 (0)
- n4 : 0000 0000 0000 0000 0000 0000 0000 0000 (0)
width : 0000 0000 0000 0000 0000 0101 0010 0010
or( | ) 연산자를 사용하면 위와 같이 비트 이동이 이루어 지면서 저장 값이 바뀌게 된다.
- n5 : 1110 0000 (224)
- n6 : 0000 0010 0000 0000 (5)
- n7 : 0000 0000 0000 0000 0000 0000 (0)
- n8 : 0000 0000 0000 0000 0000 0000 0000 0000 (0)
width : 0000 0000 0000 0000 0000 0010 1110 0000
'IT > JAVA' 카테고리의 다른 글
[JAVA] 변수와 상수변수 (0) | 2023.06.26 |
---|---|
[Java] 배열(Array) (0) | 2023.06.21 |
[Java] 문자열 (String) (2) | 2023.06.10 |
[Java] 부동소수점, 고정소수점 (0) | 2023.06.08 |
[Java] 개체(Entity) & 객체(Object) & 인스턴스(Instance) & 클래스(Class) (0) | 2023.06.08 |