added new log filter (for subject)
[TortoiseGit.git] / src / Utils / MiscUI / DIB.cpp
blob2def2cce6cd77861ea89df07eea0d9ae578b55cd
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006 - Stefan Kueng
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "../stdex_vector.h"
21 #include "DIB.h"
23 CDib::CDib()
25 m_hBitmap = NULL;
26 DeleteObject();
29 CDib::~CDib()
31 DeleteObject();
34 int CDib::BytesPerLine(int nWidth, int nBitsPerPixel)
36 int nBytesPerLine = nWidth * nBitsPerPixel;
37 nBytesPerLine = ( (nBytesPerLine + 31) & (~31) ) / 8;
38 return nBytesPerLine;
41 void CDib::DeleteObject()
43 m_pBits = NULL;
44 if (m_hBitmap)
45 ::DeleteObject(m_hBitmap);
46 m_hBitmap = NULL;
48 memset(&m_BMinfo, 0, sizeof(m_BMinfo));
51 void CDib::Create32BitFromPicture (CPictureHolder* pPicture, int iWidth, int iHeight)
53 CRect r;
54 CBitmap newBMP;
55 CWindowDC dc(NULL);
56 CDC tempDC;
58 tempDC.CreateCompatibleDC(&dc);
60 newBMP.CreateDiscardableBitmap(&dc,iWidth,iHeight);
62 CBitmap* pOldBitmap = tempDC.SelectObject(&newBMP);
64 r.SetRect(0,0,iWidth,iHeight);
65 pPicture->Render(&tempDC,r,r);
67 // Create a 32 bit bitmap
68 stdex::vector<DWORD> pBits(iWidth * iHeight);
70 BITMAPINFO bi;
71 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
72 bi.bmiHeader.biWidth = iWidth;
73 bi.bmiHeader.biHeight = iHeight;
74 bi.bmiHeader.biPlanes = 1;
75 bi.bmiHeader.biBitCount = 32;
76 bi.bmiHeader.biCompression = BI_RGB;
77 bi.bmiHeader.biSizeImage = 0;
78 bi.bmiHeader.biXPelsPerMeter = 0;
79 bi.bmiHeader.biYPelsPerMeter = 0;
80 bi.bmiHeader.biClrUsed = 0;
81 bi.bmiHeader.biClrImportant = 0;
84 SetBitmap(&bi, pBits);
86 DWORD* pAr = (DWORD*)GetDIBits();
88 // Copy data into the 32 bit dib..
89 for(int i=0;i<iHeight;i++)
91 for(int j=0;j<iWidth;j++)
93 pAr[(i*iWidth)+j] = FixColorRef(tempDC.GetPixel(j,i));
97 tempDC.SelectObject(pOldBitmap);
100 BOOL CDib::SetBitmap(const LPBITMAPINFO lpBitmapInfo, const LPVOID lpBits)
102 DeleteObject();
104 if (!lpBitmapInfo || !lpBits)
105 return FALSE;
107 HDC hDC = NULL;
109 DWORD dwBitmapInfoSize = sizeof(BITMAPINFO);
111 memcpy(&m_BMinfo, lpBitmapInfo, dwBitmapInfoSize);
113 hDC = ::GetDC(NULL);
114 if (!hDC)
116 DeleteObject();
117 return FALSE;
120 m_hBitmap = CreateDIBSection(hDC, &m_BMinfo,
121 DIB_RGB_COLORS, &m_pBits, NULL, 0);
122 ::ReleaseDC(NULL, hDC);
123 if (!m_hBitmap)
125 DeleteObject();
126 return FALSE;
129 DWORD dwImageSize = m_BMinfo.bmiHeader.biSizeImage;
130 if (dwImageSize == 0)
132 int nBytesPerLine = BytesPerLine(lpBitmapInfo->bmiHeader.biWidth,
133 lpBitmapInfo->bmiHeader.biBitCount);
134 dwImageSize = nBytesPerLine * lpBitmapInfo->bmiHeader.biHeight;
137 GdiFlush();
139 memcpy(m_pBits, lpBits, dwImageSize);
141 return TRUE;
144 BOOL CDib::Draw(CDC* pDC, CPoint ptDest)
146 if (!m_hBitmap)
147 return FALSE;
149 CSize size = GetSize();
150 CPoint SrcOrigin = CPoint(0,0);
152 BOOL resVal = FALSE;
154 resVal = SetDIBitsToDevice(pDC->GetSafeHdc(),
155 ptDest.x, ptDest.y,
156 size.cx, size.cy,
157 SrcOrigin.x, SrcOrigin.y,
158 SrcOrigin.y, size.cy - SrcOrigin.y,
159 GetDIBits(), &m_BMinfo,
160 DIB_RGB_COLORS);
162 return resVal;
165 COLORREF CDib::FixColorRef(COLORREF clr)
167 int r = GetRValue(clr);
168 int g = GetGValue(clr);
169 int b = GetBValue(clr);
171 return RGB(b,g,r);