1 /*****************************************************************************
2 * x11_bitmap.cpp: X11 implementation of the Bitmap class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: x11_bitmap.cpp,v 1.6 2003/05/24 21:28:29 asmax Exp $
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * Emmanuel Puig <karibu@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,
24 *****************************************************************************/
28 //--- X11 -------------------------------------------------------------------
30 #include <X11/Xutil.h>
32 //--- VLC -------------------------------------------------------------------
35 //--- SKIN ------------------------------------------------------------------
36 #include "../os_api.h"
37 #include "../src/graphics.h"
38 #include "x11_graphics.h"
39 #include "../src/bitmap.h"
40 #include "x11_bitmap.h"
41 #include "../src/theme.h"
42 #include "../os_theme.h"
43 #include "../src/skin_common.h"
47 // macros to read little endian numbers
48 #define U16( p ) ( ((uint8_t*)(p))[0] | ((uint8_t*)(p))[1] << 8 )
49 #define U32( p ) ( U16( p ) | ((uint8_t*)(p))[2] << 16 | ((uint8_t*)(p))[3] << 24 )
51 //---------------------------------------------------------------------------
53 //---------------------------------------------------------------------------
54 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, string FileName, int AColor )
55 : Bitmap( p_intf, FileName, AColor )
60 display = p_intf->p_sys->display;
61 int screen = DefaultScreen( display );
62 int depth = DefaultDepth( display, screen );
63 Screen *screenptr = DefaultScreenOfDisplay( display );
64 Visual *visual = DefaultVisualOfScreen( screenptr );
69 // TODO: check for endianness issues
70 AlphaColor = (AColor & 0xff) << 16 | (AColor & 0xff00) |
71 (AColor & 0xff0000) >> 16;
75 data = LoadFromFile( FileName, depth, AlphaColor, Width, Height );
79 Bmp = XCreateImage( display, visual, depth, ZPixmap, 0, data, Width,
80 Height, 32, 4 * Width );
84 //---------------------------------------------------------------------------
85 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, Graphics *from, int x, int y,
86 int w, int h, int AColor ) : Bitmap( p_intf, from, x, y, w, h, AColor )
93 HDC fromDC = ( (X11Graphics *)from )->GetImageHandle();
96 bmpDC = CreateCompatibleDC( fromDC );
97 HBmp = CreateCompatibleBitmap( fromDC, Width, Height );
98 SelectObject( bmpDC, HBmp );
100 BitBlt( bmpDC, 0, 0, Width, Height, fromDC, x, y, SRCCOPY );*/
102 //---------------------------------------------------------------------------
103 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, Bitmap *c )
104 : Bitmap( p_intf, c )
110 c->GetSize( Width, Height );
111 AlphaColor = c->GetAlphaColor();
114 bmpDC = CreateCompatibleDC( NULL );
115 HBuf = CreateCompatibleBitmap( bmpDC, Width, Height );
116 SelectObject( bmpDC, HBuf );
118 BitBlt( bmpDC, 0, 0, Width, Height, ( (X11Bitmap *)c )->GetBmpDC(),
120 DeleteObject( HBuf );*/
122 //---------------------------------------------------------------------------
123 X11Bitmap::~X11Bitmap()
125 XDestroyImage( Bmp );
127 //---------------------------------------------------------------------------
128 void X11Bitmap::DrawBitmap( int x, int y, int w, int h, int xRef, int yRef,
131 Drawable destImg = ( (X11Graphics *)dest )->GetImage();
132 GC destGC = ( (X11Graphics *)dest )->GetGC();
133 XPutImage( display, destImg, destGC, Bmp, x, y, xRef, yRef, w, h );
135 //---------------------------------------------------------------------------
136 bool X11Bitmap::Hit( int x, int y)
138 unsigned int c = (unsigned int)GetBmpPixel( x, y );
140 if( c == -1 || c == AlphaColor )
145 //---------------------------------------------------------------------------
146 int X11Bitmap::GetBmpPixel( int x, int y )
148 if( !Bmp || x < 0 || x >= Width || y < 0 || y >= Height )
153 int rowstride, offset;
156 rowstride = gdk_pixbuf_get_rowstride( Bmp );
157 pixels = gdk_pixbuf_get_pixels( Bmp );
158 has_alpha = gdk_pixbuf_get_has_alpha( Bmp );
160 offset = y * rowstride + ( x * (has_alpha ? 4 : 3) );
162 int r = pixels [offset];
163 int g = pixels [offset + 1] << 8;
164 int b = pixels [offset + 2] << 16;
168 //---------------------------------------------------------------------------
169 void X11Bitmap::SetBmpPixel( int x, int y, int color )
171 // SetPixelV( bmpDC, x, y, color );
173 //---------------------------------------------------------------------------
174 char *X11Bitmap::LoadFromFile( string fileName, int depth, int AColor,
175 int &width, int &height )
183 uint32_t compression;
187 FILE *file = fopen( fileName.c_str(), "ro" );
190 msg_Warn( p_intf, "Cannot open %s", fileName.c_str() );
196 fread( headers, 54, 1, file );
198 fileSize = U32( headers + 2 );
199 dataOffset = U32( headers + 10 );
200 headerSize = U32( headers + 14 );
201 width = (int32_t) U32( headers + 18 );
202 height = (int32_t) U32( headers + 22 );
203 planes = U32( headers + 26 );
204 bpp = U32( headers + 28 );
205 compression = U32( headers + 30 );
206 dataSize = U32( headers + 34 );
207 nColors = U32( headers + 50 );
214 // Pad to a 32bit boundary
215 int pad = ((3 * width - 1) / 4) * 4 + 4 - 3 * width;
216 uint32_t *data = new uint32_t[height * width];
218 for( int j = 0; j < height; j++ )
220 ptr = data + width * (height - j - 1);
221 for( int i = 0; i < width; i++ )
225 fread( &pixel, 3, 1, file );
226 pixel = U32( &pixel );
227 // Handle transparency
228 if( pixel == 0 && AColor != 0 )
230 pixel = 10; // slight blue
232 else if( pixel == AColor )
234 pixel = 0; // global alpha color is black
238 fseek( file, pad, SEEK_CUR );
243 msg_Warn( p_intf, "%s : %d bbp not supported !", fileName.c_str(),
249 //---------------------------------------------------------------------------