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 #define AC_SRC_ALPHA 1
32 #include "win32_factory.hpp"
33 #include "win32_graphics.hpp"
34 #include "win32_window.hpp"
35 #include "../src/generic_bitmap.hpp"
37 Win32Graphics::Win32Graphics( intf_thread_t *pIntf, int width, int height ):
38 OSGraphics( pIntf ), m_width( width ), m_height( height ), m_hDC( NULL )
41 HDC hDC = GetDC( NULL );
42 hBmp = CreateCompatibleBitmap( hDC, m_width, m_height );
43 ReleaseDC( NULL, hDC );
45 m_hDC = CreateCompatibleDC( NULL );
46 SelectObject( m_hDC, hBmp );
50 m_mask = CreateRectRgn( 0, 0, 0, 0 );
54 Win32Graphics::~Win32Graphics()
57 DeleteObject( m_mask );
61 void Win32Graphics::clear()
63 // Clear the transparency mask
64 DeleteObject( m_mask );
65 m_mask = CreateRectRgn( 0, 0, 0, 0 );
69 void Win32Graphics::drawBitmap( const GenericBitmap &rBitmap,
70 int xSrc, int ySrc, int xDest, int yDest,
71 int width, int height, bool blend )
73 // Get the bitmap size if necessary
76 width = rBitmap.getWidth();
80 height = rBitmap.getHeight();
83 if( xDest + width > m_width || yDest + height > m_height )
85 msg_Err( getIntf(), "Bitmap too large !" );
89 // Get a buffer on the image data
90 uint8_t *pBmpData = rBitmap.getData();
91 if( pBmpData == NULL )
97 void *pBits; // pointer to DIB section
98 // Fill a BITMAPINFO structure
100 memset( &bmpInfo, 0, sizeof( bmpInfo ) );
101 bmpInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
102 bmpInfo.bmiHeader.biWidth = width;
103 bmpInfo.bmiHeader.biHeight = -height;
104 bmpInfo.bmiHeader.biPlanes = 1;
105 bmpInfo.bmiHeader.biBitCount = 32;
106 bmpInfo.bmiHeader.biCompression = BI_RGB;
107 bmpInfo.bmiHeader.biSizeImage = width * height * 4;
109 // Create a DIB (Device Independant Bitmap) and associate it with
111 HDC hDC = CreateCompatibleDC( m_hDC );
112 HBITMAP hBmp = CreateDIBSection( hDC, &bmpInfo, DIB_RGB_COLORS,
114 SelectObject( hDC, hBmp );
116 // Mask for transparency
117 HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
119 // Skip the first lines of the image
120 pBmpData += 4 * ySrc * rBitmap.getWidth();
122 // Copy the bitmap on the image and compute the mask
123 for( int y = 0; y < height; y++ )
125 // Skip uninteresting bytes at the beginning of the line
126 pBmpData += 4 * xSrc;
127 // Flag to say whether the previous pixel on the line was visible
128 bool wasVisible = false;
129 // Beginning of the current visible segment on the line
130 int visibleSegmentStart = 0;
131 for( int x = 0; x < width; x++ )
133 uint8_t b = *(pBmpData++);
134 uint8_t g = *(pBmpData++);
135 uint8_t r = *(pBmpData++);
136 uint8_t a = *(pBmpData++);
139 ((UINT32 *)pBits)[x + y * width] =
140 (a << 24) | (r << 16) | (g << 8) | b;
147 // Beginning of a visible segment
148 visibleSegmentStart = x;
154 // Pixel is transparent
157 // End of a visible segment: add it to the mask
158 addSegmentInRegion( mask, visibleSegmentStart, x, y );
165 // End of a visible segment: add it to the mask
166 addSegmentInRegion( mask, visibleSegmentStart, width, y );
168 // Skip uninteresting bytes at the end of the line
169 pBmpData += 4 * (rBitmap.getWidth() - width - xSrc);
172 // Apply the mask to the internal DC
173 OffsetRgn( mask, xDest, yDest );
174 SelectClipRgn( m_hDC, mask );
176 BLENDFUNCTION bf; // structure for alpha blending
177 bf.BlendOp = AC_SRC_OVER;
179 bf.SourceConstantAlpha = 0xff; // don't use constant alpha
180 bf.AlphaFormat = AC_SRC_ALPHA;
182 // Blend the image onto the internal DC
183 BOOL (WINAPI *AlphaBlend)( HDC, int, int, int, int, HDC, int, int,
184 int, int, BLENDFUNCTION );
185 AlphaBlend = ((Win32Factory*)OSFactory::instance( getIntf() ))->AlphaBlend;
187 !AlphaBlend( m_hDC, xDest, yDest, width, height, hDC, 0, 0,
188 width, height, bf ) )
190 msg_Err( getIntf(), "AlphaBlend() failed" );
192 else if( !AlphaBlend )
194 // Copy the image onto the internal DC
195 BitBlt( m_hDC, xDest, yDest, width, height, hDC, 0, 0, SRCCOPY );
198 // Add the bitmap mask to the global graphics mask
199 CombineRgn( m_mask, m_mask, mask, RGN_OR );
202 DeleteObject( hBmp );
203 DeleteObject( mask );
208 void Win32Graphics::drawGraphics( const OSGraphics &rGraphics, int xSrc,
209 int ySrc, int xDest, int yDest, int width,
214 width = rGraphics.getWidth();
218 height = rGraphics.getHeight();
221 // Create the mask for transparency
222 HRGN mask = CreateRectRgn( xSrc, ySrc, xSrc + width, ySrc + height );
223 CombineRgn( mask, ((Win32Graphics&)rGraphics).getMask(), mask, RGN_AND );
224 OffsetRgn( mask, xDest - xSrc, yDest - ySrc );
227 HDC srcDC = ((Win32Graphics&)rGraphics).getDC();
228 SelectClipRgn( m_hDC, mask );
229 BitBlt( m_hDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
231 // Add the source mask to the mask of the graphics
232 CombineRgn( m_mask, mask, m_mask, RGN_OR );
233 DeleteObject( mask );
237 void Win32Graphics::fillRect( int left, int top, int width, int height,
240 // Update the mask with the rectangle area
241 HRGN newMask = CreateRectRgn( left, top, left + width, top + height );
242 CombineRgn( m_mask, m_mask, newMask, RGN_OR );
243 SelectClipRgn( m_hDC, m_mask );
244 DeleteObject( newMask );
246 // Create a brush with the color
247 int red = (color & 0xff0000) >> 16;
248 int green = (color & 0xff00) >> 8;
249 int blue = color & 0xff;
250 HBRUSH hBrush = CreateSolidBrush( RGB( red, green, blue ) );
252 // Draw the rectangle
256 r.right = left + width;
257 r.bottom = top + height;
258 FillRect( m_hDC, &r, hBrush );
259 DeleteObject( hBrush );
263 void Win32Graphics::drawRect( int left, int top, int width, int height,
266 // Update the mask with the rectangle
267 HRGN l1 = CreateRectRgn( left, top, left + width, top + 1 );
268 HRGN l2 = CreateRectRgn( left + width - 1, top,
269 left + width, top + height );
270 HRGN l3 = CreateRectRgn( left, top + height - 1,
271 left + width, top + height );
272 HRGN l4 = CreateRectRgn( left, top, left + 1, top + height );
273 CombineRgn( m_mask, m_mask, l1, RGN_OR );
274 CombineRgn( m_mask, m_mask, l2, RGN_OR );
275 CombineRgn( m_mask, m_mask, l3, RGN_OR );
276 CombineRgn( m_mask, m_mask, l4, RGN_OR );
282 SelectClipRgn( m_hDC, m_mask );
284 // Create a pen with the color
285 int red = (color & 0xff0000) >> 16;
286 int green = (color & 0xff00) >> 8;
287 int blue = color & 0xff;
288 HPEN hPen = CreatePen( PS_SOLID, 0, RGB( red, green, blue ) );
289 SelectObject( m_hDC, hPen );
291 // Draw the rectangle
292 MoveToEx( m_hDC, left, top, NULL );
293 LineTo( m_hDC, left + width - 1, top );
294 LineTo( m_hDC, left + width - 1, top + height - 1 );
295 LineTo( m_hDC, left, top + height - 1 );
296 LineTo( m_hDC, left, top );
299 DeleteObject( hPen );
303 void Win32Graphics::applyMaskToWindow( OSWindow &rWindow )
306 HWND hWnd = ((Win32Window&)rWindow).getHandle();
309 // We need to copy the mask, because SetWindowRgn modifies it in our back
310 HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
311 CombineRgn( mask, m_mask, NULL, RGN_COPY );
312 SetWindowRgn( hWnd, mask, TRUE );
316 void Win32Graphics::copyToWindow( OSWindow &rWindow, int xSrc, int ySrc,
317 int width, int height, int xDest, int yDest )
319 // Initialize painting
320 HWND hWnd = ((Win32Window&)rWindow).getHandle();
321 HDC wndDC = GetWindowDC( hWnd );
324 // Draw image on window
325 BitBlt( wndDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
327 // Release window device context
328 ReleaseDC( hWnd, wndDC );
332 bool Win32Graphics::hit( int x, int y ) const
334 return PtInRegion( m_mask, x, y );
338 void Win32Graphics::addSegmentInRegion( HRGN &rMask, int start,
341 HRGN buffer = CreateRectRgn( start, line, end, line + 1 );
342 CombineRgn( rMask, buffer, rMask, RGN_OR );
343 DeleteObject( buffer );