29 #include "../../idlib/precompiled.h"
32 #ifdef ID_DEBUG_MEMORY
42 #define BIG_DISTANCE 10000000L
44 #define DIST(r1,g1,b1,r2,g2,b2) \
45 (long) (3L*(long)((r1)-(r2))*(long)((r1)-(r2)) + \
46 4L*(long)((g1)-(g2))*(long)((g1)-(g2)) + \
47 2L*(long)((b1)-(b2))*(long)((b1)-(b2)))
50 static unsigned char masktable[] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };
88 BITMAPINFOHEADER bmInfo;
90 memset(&bmInfo,0,
sizeof(BITMAPINFOHEADER));
91 bmInfo.biSize =
sizeof(BITMAPINFOHEADER);
92 bmInfo.biWidth =
width;
95 bmInfo.biBitCount =
bits;
96 bmInfo.biCompression = BI_RGB;
102 bytes = (bmInfo.biBitCount*bmInfo.biWidth)>>3;
104 width = bmInfo.biWidth;
110 m_pVoid = (
void *)malloc(size);
114 memcpy((
void *)&
m_pInfo->bmiHeader,(
void *)&bmInfo,
sizeof(BITMAPINFOHEADER));
115 m_pRGB = (RGBQUAD *)((
unsigned char *)
m_pVoid +
sizeof(BITMAPINFOHEADER)) ;
138 for(i=0,rgb =
m_pRGB; i <
size; i++,rgb++,palette+=3)
142 rgb->rgbRed =
Gamma[palette[0]];
143 rgb->rgbGreen =
Gamma[palette[1]];
144 rgb->rgbBlue =
Gamma[palette[2]];
148 rgb->rgbRed = palette[0];
149 rgb->rgbGreen = palette[1];
150 rgb->rgbBlue = palette[2];
152 rgb->rgbReserved = (BYTE)0;
161 memcpy(
m_pRGB,pRGB,size*
sizeof(RGBQUAD));
173 switch(bmInfo.biBitCount)
190 ASSERT(x >= 0 && y >=0);
196 *ptr++ = (
unsigned char)GetBValue(color);
197 *ptr++ = (
unsigned char)GetGValue(color);
198 *ptr++ = (
unsigned char)GetRValue(color);
203 unsigned char *ptr,*aptr;
204 ASSERT(x >= 0 && y >=0);
220 ASSERT(x >= 0 && y >=0);
226 color = RGB(*(ptr+2),*(ptr+1),*ptr);
235 hBitmap = CreateDIBitmap(dc.m_hDC,
243 temp = CBitmap::FromHandle(hBitmap);
252 hBitmap = CreateDIBitmap(dc.m_hDC,
260 temp = CBitmap::FromHandle(hBitmap);
265 temp->GetBitmap(&bmp);
266 lpVoid = malloc(bmp.bmWidthBytes*bmp.bmHeight);
267 if(!lpVoid)
return NULL;
268 temp->GetBitmapBits(bmp.bmWidthBytes*bmp.bmHeight,lpVoid);
269 CBitmap *newBmp =
new CBitmap;
270 newBmp->CreateBitmapIndirect(&bmp);
271 newBmp->SetBitmapBits(bmp.bmWidthBytes*bmp.bmHeight,lpVoid);
283 ASSERT(dest <= height && dest >= 0);
284 if(source == dest)
return;
287 memcpy(dst,src,
bytes);
295 if(
m_pInfo->bmiHeader.biBitCount == 24)
297 unsigned char col[3];
298 col[0]=GetBValue(color);
299 col[1]=GetGValue(color);
300 col[2]=GetRValue(color);
304 for(j=0; j <
width ; j++,ptr+=3)
314 memset(ptr,(BYTE)color,
bytes);
322 SetDIBitsToDevice(hDest,nXDest,nYDest,nWidth,nHeight,xSrc,
Height()-ySrc-nHeight,0,
Height(),
m_pBits,
m_pInfo,DIB_RGB_COLORS);
328 err = StretchDIBits(hDest,nXDest,nYDest,nDWidth,nDHeight,xSrc,ySrc,nSWidth,nSHeight,
m_pBits,(CONST BITMAPINFO * )&
m_pInfo->bmiHeader,DIB_RGB_COLORS,SRCCOPY);
335 nSWidth = xSrc+nSWidth > dibSrc.
width ? dibSrc.
width-xSrc : nSWidth;
336 nSHeight = ySrc+nSHeight > dibSrc.
height? dibSrc.
height-ySrc : nSHeight;
338 Expand(nXDest,nYDest,xRatio,yRatio,dibSrc,xSrc,ySrc,nSWidth,nSHeight);
343 int xNum,yNum,xErr,yErr;
344 int nDWidth,nDHeight;
346 nDWidth = nSWidth*xRatio;
347 nDHeight = nSHeight*yRatio;
349 nDWidth = nXDest+nDWidth >
width ?
width-nXDest : nDWidth ;
350 nDHeight = nYDest+nDHeight >
height ?
height-nYDest : nDHeight;
352 xNum = nDWidth/xRatio;
353 yNum = nDHeight/yRatio;
354 xErr = nDWidth%xRatio;
355 yErr = nDHeight%yRatio;
357 unsigned char *
buffer,*srcPtr,*destPtr,*ptr;
360 buffer = (
unsigned char *)malloc(nDWidth+20);
363 for(i=0; i < yNum; i++,ySrc++)
367 for(j=0; j < xNum; j++,ptr+=xRatio)
369 memset(ptr,*(srcPtr+j),xRatio);
372 memset(ptr,(
unsigned char)k,xErr);
373 for(j=0; j < yRatio ; j++,nYDest++)
376 memcpy(destPtr,buffer,nDWidth);
379 for(j=0; j < yErr; j++,nYDest++)
382 memcpy(destPtr,buffer,nDWidth);
390 nDWidth = nXDest+nDWidth >
width ?
width-nXDest : nDWidth ;
391 nDHeight = nYDest+nDHeight >
height ?
height-nYDest : nDHeight;
393 nSWidth = xSrc+nSWidth > dibSrc.
width ? dibSrc.
width-xSrc : nSWidth;
394 nSHeight = ySrc+nSHeight > dibSrc.
height? dibSrc.
height-ySrc : nSHeight;
399 xDiv = nDWidth/nSWidth;
400 xMod = nDWidth%nSWidth;
402 yDiv = nDHeight/nSHeight;
403 yMod = nDHeight%nSHeight;
405 if(!xMod && !yMod && xDiv > 0 && yDiv > 0)
407 ExpandBlt(nXDest,nYDest,xDiv,yDiv,dibSrc,xSrc,ySrc,nSWidth,nSHeight);
411 unsigned char *tempPtr,*srcPix,*destPix,*
q;
412 tempPtr = (
unsigned char *)malloc(nDWidth+20);
415 for(i=yErr=m=0; i < nSHeight; i++)
419 for(j=l=xErr=0; j < nSWidth; j++,srcPix++)
429 while(l < nDWidth && x < k)
449 while(m < nDHeight && y < k)
452 memcpy(destPix,tempPtr,nDWidth);
460 memcpy(destPix,tempPtr,nDWidth);
466 void CDIB::BitBlt(
int nXDest,
int nYDest,
int nWidth,
int nHeight,
CDIB& dibSrc,
int nSrcX,
int nSrcY,BYTE *colors)
493 nWidth = nXDest+nWidth >
width ?
width-nXDest : nWidth ;
494 nHeight = nYDest+nHeight >
height ?
height-nYDest : nHeight;
496 nWidth = nSrcX+nWidth > dibSrc.
width ? dibSrc.
width-nSrcX : nWidth;
497 nHeight = nSrcY+nHeight > dibSrc.
height? dibSrc.
height-nSrcY : nHeight;
499 nWidth = __max(0,nWidth);
500 nHeight = __max(0,nHeight);
502 unsigned char *srcPtr,*destPtr;
505 for(i=0,k=nSrcY,l=nYDest; i < nHeight; i++,k++,l++)
515 memcpy(destPtr+nXDest,srcPtr+nSrcX,nWidth);
521 for(i=0,k=nSrcY,l=nYDest; i < nHeight; i++,k++,l++)
531 for(j=0; j < nWidth; j++,srcPtr++,destPtr++)
533 if(colors[*srcPtr]) *destPtr=*srcPtr;
566 for(j=0; j <
width; j++)
568 if(ptr[j] == oldColor) ptr[
j] = newColor;
589 dib.
BitBlt(0,0,nCx,nCy,*
this,0,0);
606 hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,nSize);
608 UCHAR *lpVoid,*pBits;
609 LPBITMAPINFOHEADER pHead;
611 lpVoid = (UCHAR *)GlobalLock(hMem);
612 pHead = (LPBITMAPINFOHEADER )lpVoid;
613 memcpy(pHead,&
m_pInfo->bmiHeader,
sizeof(BITMAPINFOHEADER));
614 pRgb = (RGBQUAD *)(lpVoid +
sizeof(BITMAPINFOHEADER) );
616 pBits = lpVoid +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*
GetPaletteSize();
618 GlobalUnlock(lpVoid);
625 UCHAR *lpVoid,*pBits;
626 LPBITMAPINFOHEADER pHead;
628 lpVoid = (UCHAR *)GlobalLock(hMem);
629 pHead = (LPBITMAPINFOHEADER )lpVoid;
630 width = pHead->biWidth;
633 if(pHead->biCompression != BI_RGB)
635 GlobalUnlock(lpVoid);
638 if(pHead->biBitCount >= 15)
640 if(pHead->biBitCount != 24)
642 GlobalUnlock(lpVoid);
648 GlobalUnlock(lpVoid);
651 pRgb = (RGBQUAD *)(lpVoid +
sizeof(BITMAPINFOHEADER) );
653 pBits = lpVoid +
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*
GetPaletteSize();
655 GlobalUnlock(lpVoid);
739 unsigned char *sPtr,*dPtr;
740 unsigned char cols[2];
750 for(j=0 ; j <
w; j++,dPtr++)
752 if(!(sPtr[j>>3] & masktable[j&7])) *dPtr = cols[0];
753 else *dPtr = cols[1];
762 unsigned char *sPtr,*dPtr;
763 unsigned char cols[16];
767 for(i=0; i < 16; i++)
775 for(j=0 ; j <
w; j++,dPtr++)
777 if(!(j&1)) n = (*sPtr & 0xf0)>>4;
792 unsigned char *sPtr,*dPtr;
793 unsigned char cols[256];
797 for(i=0; i < 256; i++)
805 for(j=0 ; j <
w; j++,sPtr++,dPtr++)
819 for(i=0; i <
pSize;i++)
823 if(!memcmp((
void *)&
CacheQuad[i],(
void *)pRgb,3))
829 for(i=0; i <
pSize; i++,pQuad++)
852 d = 3*(unsigned)((rgb1.rgbRed)-(rgb2.rgbRed))*(unsigned)((rgb1.rgbRed)-(rgb2.rgbRed));
853 d += 4*(unsigned)((rgb1.rgbGreen)-(rgb2.rgbGreen))*(unsigned)((rgb1.rgbGreen)-(rgb2.rgbGreen)) ;
854 d += 2*(unsigned)((rgb1.rgbBlue)-(rgb2.rgbBlue))*(unsigned)((rgb1.rgbBlue)-(rgb2.rgbBlue));
861 if(!file.Open(csFileName,CFile::modeRead | CFile::typeBinary))
875 if(!file.Open(csFileName,CFile::modeCreate | CFile::typeBinary))
892 BITMAPFILEHEADER bFile;
894 if(!file.Open(csFileName,CFile::modeWrite | CFile::typeBinary))
898 ::ZeroMemory(&bFile,
sizeof(bFile));
899 memcpy((
void *)&bFile.bfType,
"BM",2);
901 bFile.bfOffBits =
sizeof(BITMAPINFOHEADER) +
GetPaletteSize()*
sizeof(RGBQUAD) +
sizeof(BITMAPFILEHEADER);
902 file.Write(&bFile,
sizeof(bFile));
911 BITMAPFILEHEADER bFile;
912 BITMAPINFOHEADER head;
914 if(!file.Open(csFileName,CFile::modeRead | CFile::typeBinary))
918 file.Read(&bFile,
sizeof(bFile));
919 if(memcmp((
void *)&bFile.bfType,
"BM",2))
924 file.Read(&head,
sizeof(head));
931 file.Seek(bFile.bfOffBits,CFile::begin);
942 BYTE colors[256],*ptr;
946 memset(colors,0,256);
950 for(j=0; j <
w; j++,ptr++)
973 for(j=0; j <
w; j++,ptr++)
987 ASSERT(nIndex < 256);
988 RGBQUAD *pRGB=
m_pRGB+nIndex;
989 return RGB(pRGB->rgbRed,pRGB->rgbGreen,pRGB->rgbBlue);
995 unsigned char *sPtr,*dPtr;
996 BYTE *index_ptr=
NULL;
1000 index_ptr = (BYTE *)malloc(0x7FFF+1);
1001 if(!index_ptr)
return FALSE;
1003 for(i=0; i <= 0x7FFF; i++)
1005 rgb.rgbRed = (((i & 0x7C00)>>10) << 3) | 0x07;
1006 rgb.rgbGreen = (((i & 0x3e0)>>5) << 3) | 0x07;
1007 rgb.rgbBlue = ((i & 0x1F)<<3) | 0x07;
1010 for(i=0; i < h; i++)
1014 for(j=0 ; j <
w; j++,dPtr++,sPtr+=3)
1016 c = (*sPtr >> 3) | ((*(sPtr+1) >> 3) << 5) | ((*(sPtr+2) >> 3) << 10);
1017 *dPtr = index_ptr[
c];
GLdouble GLdouble GLdouble GLdouble q
BOOL OpenBMP(CString &csFileName)
void SetPalette(unsigned char *palette)
static unsigned int Distance(RGBQUAD &rgb1, RGBQUAD &rgb2)
int int int int int int ySrc
CONST PIXELFORMATDESCRIPTOR UINT
COLORREF GetPixel(int x, int y)
const HANDLE const LPVOID const DWORD * pSize
typedef HANDLE(WINAPI *PFNWGLCREATEBUFFERREGIONARBPROC)(HDC hDC
BOOL SwitchFromFour(CDIB &dib)
void StretchBlt(HDC hDest, int nXDest, int nYDest, int nDWidth, int nDHeight, int xSrc, int ySrc, int nSWidth, int nSHeight)
void UseGamma(float fg, BOOL bUse=TRUE)
HANDLE GetDIBits(int nStartX=-1, int nStartY=-1, int nCx=-1, int nCy=-1)
GLuint GLuint GLsizei GLenum type
unsigned char * GetLinePtr(int line)
int ClosestColor(RGBQUAD *pRgb)
void SetPixel8(int x, int y, unsigned char color)
GLsizei GLsizei GLcharARB * source
BOOL SwitchFromOne(CDIB &dib)
BOOL OpenDIB(CString &fileName)
void ReplaceColor(unsigned char oldColor, unsigned char newColor)
BOOL SwitchPalette(CDIB &dib)
void Expand(int nXDest, int nYDest, int xRatio, int yRatio, CDIB &dibSrc, int xSrc, int ySrc, int nSWidth, int nSHeight)
GLubyte GLubyte GLubyte GLubyte w
BOOL SaveBMP(CString &csFileName)
void CopyLine(int source, int dest)
COLORREF PaletteColor(int index)
void BitBlt(HDC hDest, int nXDest, int nYDest, int nWidth, int nHeight, int xSrc, int ySrc)
void SetPixel(int x, int y, COLORREF color)
typedef HDC(WINAPI *PFNWGLGETCURRENTREADDCARBPROC)(void)
int EnumColors(BYTE *colors)
GLenum GLsizei GLsizei height
void ExpandBlt(int nXDest, int nYDest, int xRatio, int yRatio, CDIB &dibSrc, int xSrc, int ySrc, int nSWidth, int nSHeight)
void InitDIB(COLORREF color)
CDIB & operator=(CDIB &dib)
BOOL SwitchFrom24(CDIB &dib)
static WindowRef ValidModeCallbackProc inCallback OSStatus err
BOOL Create(int width, int height, int bits=24)
typedef LPVOID(WINAPI *PFNWGLCREATEIMAGEBUFFERI3DPROC)(HDC hDC
CBitmap * GetBitmap(CDC &dc)
CBitmap * GetTempBitmap(CDC &dc)
CDIB(HANDLE hDib=NULL, int nBits=8)
BOOL CreateFromHandle(HANDLE hDib, int nBits)
BOOL SaveDIB(CString &fileName, BitmapType type)