OSDN Git Service

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