]> git.vomp.tv Git - vompclient.git/blob - bitmap.cc
7dde8848b1e7e8b9ca33919556dae8fd617bdf5c
[vompclient.git] / bitmap.cc
1 /*
2     Copyright 2008 Mark Calderbank
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 #include "bitmap.h"
21
22
23 DisplayRegion::DisplayRegion()
24 {
25         framewidth=720;
26         frameheight=576;
27         windowx=0;
28         windowy=0;
29         windoww=719;
30         windowh=575;
31 }
32
33 Palette::Palette(UCHAR tBpp)
34 {
35   numColours = 0;
36   setBpp(tBpp);
37 }
38
39 void Palette::argb2yrba(ULONG argb, UCHAR& y, UCHAR& cr, UCHAR& cb, UCHAR& a)
40 {
41   a = ((argb & 0xFF000000) >> 24);
42   int r = (argb & 0x00FF0000) >> 16;
43   int g = (argb & 0x0000FF00) >> 8;
44   int b = (argb & 0x000000FF);
45   y  = (1052*r + 2065*g + 401*b + 4096*16 + 2048) / 4096;
46   cr = (1799*r - 1508*g - 291*b + 4096*128 + 2048) / 4096;
47   cb = (-608*r - 1191*g + 1799*b + 4096*128 + 2048) / 4096;
48 }
49
50 ULONG Palette::yrba2argb(UCHAR y, UCHAR cr, UCHAR cb, UCHAR a)
51 {
52   int r, g, b;
53   r = (4769*(y-16) + 6537*(cr-128) + 2048) / 4096;
54   g = (4769*(y-16) - 3329*(cr-128) - 1604*(cb-128) + 2048) / 4096;
55   b = (4769*(y-16) + 8263*(cb-128) + 2048) / 4096;
56   if (r < 0) r = 0; if (r > 255) r = 255;
57   if (g < 0) g = 0; if (g > 255) g = 255;
58   if (b < 0) b = 0; if (b > 255) b = 255;
59   return (a << 24) + (r << 16) + (g << 8) + b;
60 }
61
62 void Palette::setBpp(UCHAR tBpp)
63 {
64   bpp = tBpp;
65   if (bpp > MAX_DEPTH) bpp = MAX_DEPTH;
66   maxColours = 1 << bpp;
67   if (numColours > maxColours) numColours = maxColours;
68   colour.resize(maxColours,0xFF000000);
69   Y.resize(maxColours,16);
70   Cr.resize(maxColours,128);
71   Cb.resize(maxColours,128);
72   A.resize(maxColours,255);
73 }
74
75 void Palette::setColour(UCHAR index, ULONG tColour)
76 {
77   if (index >= maxColours) return;
78   if (index >= numColours) numColours = index + 1;
79   colour[index] = tColour;
80   argb2yrba(tColour, Y[index], Cr[index], Cb[index], A[index]);
81 }
82
83 void Palette::setYCrCbA(UCHAR index, UCHAR tY, UCHAR tCr, UCHAR tCb, UCHAR tA)
84 {
85   if (index >= maxColours) return;
86   if (index >= numColours) numColours = index + 1;
87   Y[index] = tY; Cr[index] = tCr; Cb[index] = tCb; A[index] = tA;
88   colour[index] = yrba2argb(tY, tCr, tCb, tA);
89 }
90
91 Bitmap::Bitmap(UINT tWidth, UINT tHeight, UCHAR tBpp)
92 {
93   setSize(tWidth, tHeight);
94   palette.setBpp(tBpp);
95 }
96
97 void Bitmap::setSize(UINT tWidth, UINT tHeight)
98 {
99   if (width == tWidth && height == tHeight) return;
100   width = tWidth; height = tHeight;
101   if (width == 0 || height == 0)
102     bitmap.clear();
103   else
104     bitmap.assign(width * height, 0);
105 }
106
107 bool Bitmap::setIndex(UINT x, UINT y, UCHAR index)
108 {
109   if (x <= width && y <= height)
110   {
111     bitmap[x + y*width] = index;
112     return true;
113   }
114   else return false;
115 }
116
117 UCHAR Bitmap::getIndex(UINT x, UINT y) const
118 {
119   if (x > width || y > height)
120     return 0;
121   else
122     return bitmap[x + y*width];
123 }
124
125 ULONG Bitmap::getColour(UINT x, UINT y) const
126 {
127   if (x > width || y > height)
128     return 0;
129   else
130     return palette.getColour(bitmap[x + y*width]);
131 }
132
133 void Bitmap::setAllIndices(UCHAR index)
134 {
135   bitmap.assign(width * height, index);
136 }