https://beastit.tistory.com/36

 

[Byte Order]Big Endian(빅 엔디안) & Little Endian(리틀 엔디안)

🟩 Byte Order (바이트 저장 순서) 컴퓨터는 데이터를 메모리에 저장할 때 바이트(byte) 단위로 나눠서 저장합니다. 하지만 컴퓨터가 저장하는 데이터는 32비트(4바이트) 또는 64비트(8바이트)로 구성

beastit.tistory.com

 

글 시작 전에 위에 글을 먼저 보면 왜? 하는지 알 수 있다.

 

✅ 비트맵 이미지로 필요한 데이터 추출하기

 

맨 위부터 보면 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 

+ Recent posts