1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
29 #include "win32_factory.hpp"
30 #include "win32_graphics.hpp"
31 #include "win32_window.hpp"
32 #include "../src/generic_bitmap.hpp"
35 #define AC_SRC_ALPHA 1
38 Win32Graphics::Win32Graphics( intf_thread_t *pIntf, int width, int height ):
39 OSGraphics( pIntf ), m_width( width ), m_height( height ), m_hDC( NULL )
42 HDC hDC = GetDC( NULL );
43 hBmp = CreateCompatibleBitmap( hDC, m_width, m_height );
44 ReleaseDC( NULL, hDC );
46 m_hDC = CreateCompatibleDC( NULL );
47 SelectObject( m_hDC, hBmp );
51 m_mask = CreateRectRgn( 0, 0, 0, 0 );
55 Win32Graphics::~Win32Graphics()
58 DeleteObject( m_mask );
62 void Win32Graphics::clear()
64 // Clear the transparency mask
65 DeleteObject( m_mask );
66 m_mask = CreateRectRgn( 0, 0, 0, 0 );
70 void Win32Graphics::drawBitmap( const GenericBitmap &rBitmap,
71 int xSrc, int ySrc, int xDest, int yDest,
72 int width, int height, bool blend )
74 // Get the bitmap size if necessary
77 width = rBitmap.getWidth();
81 height = rBitmap.getHeight();
84 if( xDest + width > m_width || yDest + height > m_height )
86 msg_Err( getIntf(), "Bitmap too large !" );
90 // Get a buffer on the image data
91 uint8_t *pBmpData = rBitmap.getData();
92 if( pBmpData == NULL )
98 void *pBits; // pointer to DIB section
99 // Fill a BITMAPINFO structure
101 memset( &bmpInfo, 0, sizeof( bmpInfo ) );
102 bmpInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
103 bmpInfo.bmiHeader.biWidth = width;
104 bmpInfo.bmiHeader.biHeight = -height;
105 bmpInfo.bmiHeader.biPlanes = 1;
106 bmpInfo.bmiHeader.biBitCount = 32;
107 bmpInfo.bmiHeader.biCompression = BI_RGB;
108 bmpInfo.bmiHeader.biSizeImage = width * height * 4;
110 // Create a DIB (Device Independant Bitmap) and associate it with
112 HDC hDC = CreateCompatibleDC( m_hDC );
113 HBITMAP hBmp = CreateDIBSection( hDC, &bmpInfo, DIB_RGB_COLORS,
115 SelectObject( hDC, hBmp );
117 // Mask for transparency
118 HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
120 // Skip the first lines of the image
121 pBmpData += 4 * ySrc * rBitmap.getWidth();
123 // Copy the bitmap on the image and compute the mask
124 for( int y = 0; y < height; y++ )
126 // Skip uninteresting bytes at the beginning of the line
127 pBmpData += 4 * xSrc;
128 // Flag to say whether the previous pixel on the line was visible
129 bool wasVisible = false;
130 // Beginning of the current visible segment on the line
131 int visibleSegmentStart = 0;
132 for( int x = 0; x < width; x++ )
134 uint8_t b = *(pBmpData++);
135 uint8_t g = *(pBmpData++);
136 uint8_t r = *(pBmpData++);
137 uint8_t a = *(pBmpData++);
140 ((UINT32 *)pBits)[x + y * width] =
141 (a << 24) | (r << 16) | (g << 8) | b;
148 // Beginning of a visible segment
149 visibleSegmentStart = x;
155 // Pixel is transparent
158 // End of a visible segment: add it to the mask
159 addSegmentInRegion( mask, visibleSegmentStart, x, y );
166 // End of a visible segment: add it to the mask
167 addSegmentInRegion( mask, visibleSegmentStart, width, y );
169 // Skip uninteresting bytes at the end of the line
170 pBmpData += 4 * (rBitmap.getWidth() - width - xSrc);
173 // Apply the mask to the internal DC
174 OffsetRgn( mask, xDest, yDest );
175 SelectClipRgn( m_hDC, mask );
177 BLENDFUNCTION bf; // structure for alpha blending
178 bf.BlendOp = AC_SRC_OVER;
180 bf.SourceConstantAlpha = 0xff; // don't use constant alpha
181 bf.AlphaFormat = AC_SRC_ALPHA;
183 // Blend the image onto the internal DC
184 BOOL (WINAPI *AlphaBlend)( HDC, int, int, int, int, HDC, int, int,
185 int, int, BLENDFUNCTION );
186 AlphaBlend = ((Win32Factory*)OSFactory::instance( getIntf() ))->AlphaBlend;
188 !AlphaBlend( m_hDC, xDest, yDest, width, height, hDC, 0, 0,
189 width, height, bf ) )
191 msg_Err( getIntf(), "AlphaBlend() failed" );
193 else if( !AlphaBlend )
195 // Copy the image onto the internal DC
196 BitBlt( m_hDC, xDest, yDest, width, height, hDC, 0, 0, SRCCOPY );
199 // Add the bitmap mask to the global graphics mask
200 CombineRgn( m_mask, m_mask, mask, RGN_OR );
203 DeleteObject( hBmp );
204 DeleteObject( mask );
209 void Win32Graphics::drawGraphics( const OSGraphics &rGraphics, int xSrc,
210 int ySrc, int xDest, int yDest, int width,
215 width = rGraphics.getWidth();
219 height = rGraphics.getHeight();
222 // Create the mask for transparency
223 HRGN mask = CreateRectRgn( xSrc, ySrc, xSrc + width, ySrc + height );
224 CombineRgn( mask, ((Win32Graphics&)rGraphics).getMask(), mask, RGN_AND );
225 OffsetRgn( mask, xDest - xSrc, yDest - ySrc );
228 HDC srcDC = ((Win32Graphics&)rGraphics).getDC();
229 SelectClipRgn( m_hDC, mask );
230 BitBlt( m_hDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
232 // Add the source mask to the mask of the graphics
233 CombineRgn( m_mask, mask, m_mask, RGN_OR );
234 DeleteObject( mask );
238 void Win32Graphics::fillRect( int left, int top, int width, int height,
241 // Update the mask with the rectangle area
242 HRGN newMask = CreateRectRgn( left, top, left + width, top + height );
243 CombineRgn( m_mask, m_mask, newMask, RGN_OR );
244 SelectClipRgn( m_hDC, m_mask );
245 DeleteObject( newMask );
247 // Create a brush with the color
248 int red = (color & 0xff0000) >> 16;
249 int green = (color & 0xff00) >> 8;
250 int blue = color & 0xff;
251 HBRUSH hBrush = CreateSolidBrush( RGB( red, green, blue ) );
253 // Draw the rectangle
257 r.right = left + width;
258 r.bottom = top + height;
259 FillRect( m_hDC, &r, hBrush );
260 DeleteObject( hBrush );
264 void Win32Graphics::drawRect( int left, int top, int width, int height,
267 // Update the mask with the rectangle
268 HRGN l1 = CreateRectRgn( left, top, left + width, top + 1 );
269 HRGN l2 = CreateRectRgn( left + width - 1, top,
270 left + width, top + height );
271 HRGN l3 = CreateRectRgn( left, top + height - 1,
272 left + width, top + height );
273 HRGN l4 = CreateRectRgn( left, top, left + 1, top + height );
274 CombineRgn( m_mask, m_mask, l1, RGN_OR );
275 CombineRgn( m_mask, m_mask, l2, RGN_OR );
276 CombineRgn( m_mask, m_mask, l3, RGN_OR );
277 CombineRgn( m_mask, m_mask, l4, RGN_OR );
283 SelectClipRgn( m_hDC, m_mask );
285 // Create a pen with the color
286 int red = (color & 0xff0000) >> 16;
287 int green = (color & 0xff00) >> 8;
288 int blue = color & 0xff;
289 HPEN hPen = CreatePen( PS_SOLID, 0, RGB( red, green, blue ) );
290 SelectObject( m_hDC, hPen );
292 // Draw the rectangle
293 MoveToEx( m_hDC, left, top, NULL );
294 LineTo( m_hDC, left + width - 1, top );
295 LineTo( m_hDC, left + width - 1, top + height - 1 );
296 LineTo( m_hDC, left, top + height - 1 );
297 LineTo( m_hDC, left, top );
300 DeleteObject( hPen );
304 void Win32Graphics::applyMaskToWindow( OSWindow &rWindow )
307 HWND hWnd = ((Win32Window&)rWindow).getHandle();
310 // We need to copy the mask, because SetWindowRgn modifies it in our back
311 HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
312 CombineRgn( mask, m_mask, NULL, RGN_COPY );
313 SetWindowRgn( hWnd, mask, TRUE );
317 void Win32Graphics::copyToWindow( OSWindow &rWindow, int xSrc, int ySrc,
318 int width, int height, int xDest, int yDest )
320 // Initialize painting
321 HWND hWnd = ((Win32Window&)rWindow).getHandle();
322 HDC wndDC = GetWindowDC( hWnd );
325 // Draw image on window
326 BitBlt( wndDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
328 // Release window device context
329 ReleaseDC( hWnd, wndDC );
333 bool Win32Graphics::hit( int x, int y ) const
335 return PtInRegion( m_mask, x, y ) != 0;
339 void Win32Graphics::addSegmentInRegion( HRGN &rMask, int start,
342 HRGN buffer = CreateRectRgn( start, line, end, line + 1 );
343 CombineRgn( rMask, buffer, rMask, RGN_OR );
344 DeleteObject( buffer );