Windows는 DDB에 대해 어떤식으로 얼마나 메모리를 할당하는가?
:?
Programming Windows -Charles Petzold- 의 chapter 14 page 775 한글판에서...
//////////////////////////////////////////////////////////////////////////////////
DDB 생성
HBITMAP bBitmap;
hBitmap = CreateBitmap(cx, cy, cPlanes, cBitPixel, bits);
DeleteObject(hBitmap);
//////////////////////////////////////////////////////////////////////////////////
ex)
hBitmap = CreateBitmap(7, 9, 5, 3, NULL);
이 함수가 호출되는 동안 Windows는 함수에 전달된 정보를 저장하고 픽셀 bit를 위한
메모리를 할당하는 작업을 수행합니다. 대충 계산해서 이 비트맵에는 7x9x5x3 즉, 945bit
(118byte)가 필요합니다.
*그러나 Windows가 비트맵의 메모리를 할당할 때, *각 픽셀의 행은
*짝수번째 byte를 갖게된다. 따라서 다음과 같은 계산이 나옵니다.
(--> 한글판은 짝수번째라고 되어 있으나 아마 영문판에는 2배가 아닐런지.. 저의 착각일
수도.. ^^)
어쨌든...
iWidthBytes = 2 * ((cx * cBitPixel + 15) / 16);
or
iWidthBytes = (cx * BitsPixel + 15) & ~15) >> 3;
따라서, DDB에 할당된 메모리는 다음과 같이 계산됩니다.
iBitmapBytes = cy * cPlanes * iWidthBytes;
이 예제에서 iWidthBytes는 4byte이고 iBitmapBytes는 180byte가 됩니다.
질문의 요지는!
*Windows는 왜 각 행에 대해 2배의 메모리를 할당할까요?
Re: Windows는 DDB에 대해 어떤식으로 얼마나 메모리를 할당하는
ex) hBitmap = CreateBitmap(7, 9, 5, 3, NULL);
질문 드리고 싶은게 위 예제가 그 책에 나와 있는 건가요?
라인당 2배가 아니라, 짝수번째 바이트라면 라인당 많아야
2바이트 미만이 더 할당된다는 얘깁니다.
DIB 는 4배수로 정렬하는 것으로 알고 있구요. 특정 배수로
주소를 정렬하는 것은 효율과 bus error 같은 메모리 오류를
피하기 때문인 이유가 가장 일반적이라고 봐야 겠죠.
Orion Project : http://orionids.org
일단 BMP 포맷의 경우 4배수로 정렬이 됩니다. 그리고 컬러에
일단 BMP 포맷의 경우 4배수로 정렬이 됩니다.
그리고 컬러에 따라서 256 컬러면
1byte 1컬러(실제로는 팔레트 인덱스, 팔레트는 RGB 값을 가짐)
16bit 컬러면 2byte 1컬러, 32bit면 3byte 1 컬러가 됩니다.
그럼 고운 하루.
=========================
CharSyam ^^ --- 고운 하루
=========================
댓글 달기