]> git.sesse.net Git - vlc/blob - plugins/beos/DrawingTidbits.cpp
cf04ebccec9fc65d89128ce6ca5e362109d14a04
[vlc] / plugins / beos / DrawingTidbits.cpp
1 /*****************************************************************************
2  * DrawingTidbits.cpp
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  *
6  * Authors: 
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #include <Bitmap.h>
24 #include <Debug.h>
25 #include <Screen.h>
26
27 #include "DrawingTidbits.h"
28
29 inline uchar
30 ShiftComponent(uchar component, float percent)
31 {
32         // change the color by <percent>, make sure we aren't rounding
33         // off significant bits
34         if (percent >= 1)
35                 return (uchar)(component * (2 - percent));
36         else
37                 return (uchar)(255 - percent * (255 - component));
38 }
39
40 rgb_color
41 ShiftColor(rgb_color color, float percent)
42 {
43         rgb_color result = {
44                 ShiftComponent(color.red, percent),
45                 ShiftComponent(color.green, percent),
46                 ShiftComponent(color.blue, percent),
47                 0
48         };
49         
50         return result;
51 }
52
53 static bool
54 CompareColors(const rgb_color a, const rgb_color b)
55 {
56         return a.red == b.red
57                 && a.green == b.green
58                 && a.blue == b.blue
59                 && a.alpha == b.alpha;
60 }
61
62 bool 
63 operator==(const rgb_color &a, const rgb_color &b)
64 {
65         return CompareColors(a, b);
66 }
67
68 bool 
69 operator!=(const rgb_color &a, const rgb_color &b)
70 {
71         return !CompareColors(a, b);
72 }
73
74 void
75 ReplaceColor(BBitmap *bitmap, rgb_color from, rgb_color to)
76 {
77         ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
78         
79         BScreen screen(B_MAIN_SCREEN_ID);
80         uint32 fromIndex = screen.IndexForColor(from);
81         uint32 toIndex = screen.IndexForColor(to); 
82         
83         uchar *bits = (uchar *)bitmap->Bits();
84         int32 bitsLength = bitmap->BitsLength();        
85         for (int32 index = 0; index < bitsLength; index++) 
86                 if (bits[index] == fromIndex)
87                         bits[index] = toIndex;
88 }
89
90 void 
91 ReplaceTransparentColor(BBitmap *bitmap, rgb_color with)
92 {
93         ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
94         
95         BScreen screen(B_MAIN_SCREEN_ID);
96         uint32 withIndex = screen.IndexForColor(with); 
97         
98         uchar *bits = (uchar *)bitmap->Bits();
99         int32 bitsLength = bitmap->BitsLength();        
100         for (int32 index = 0; index < bitsLength; index++) 
101                 if (bits[index] == B_TRANSPARENT_8_BIT)
102                         bits[index] = withIndex;
103 }
104