2 Copyright 2008 Mark Calderbank
4 This file is part of VOMP.
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.
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.
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.
22 Palette::Palette(UCHAR tBpp)
28 void Palette::argb2yrba(ULONG argb, UCHAR& y, UCHAR& cr, UCHAR& cb, UCHAR& a)
30 a = ((argb & 0xFF000000) >> 24);
31 int r = (argb & 0x00FF0000) >> 16;
32 int g = (argb & 0x0000FF00) >> 8;
33 int b = (argb & 0x000000FF);
34 y = (1052*r + 2065*g + 401*b + 4096*16 + 2048) / 4096;
35 cr = (1799*r - 1508*g - 291*b + 4096*128 + 2048) / 4096;
36 cb = (-608*r - 1191*g + 1799*b + 4096*128 + 2048) / 4096;
39 ULONG Palette::yrba2argb(UCHAR y, UCHAR cr, UCHAR cb, UCHAR a)
42 r = (4769*(y-16) + 6537*(cr-128) + 2048) / 4096;
43 g = (4769*(y-16) - 3329*(cr-128) - 1604*(cb-128) + 2048) / 4096;
44 b = (4769*(y-16) + 8263*(cb-128) + 2048) / 4096;
45 if (r < 0) r = 0; if (r > 255) r = 255;
46 if (g < 0) g = 0; if (g > 255) g = 255;
47 if (b < 0) b = 0; if (b > 255) b = 255;
48 return (a << 24) + (r << 16) + (g << 8) + b;
51 void Palette::setBpp(UCHAR tBpp)
54 if (bpp > MAX_DEPTH) bpp = MAX_DEPTH;
55 maxColours = 1 << bpp;
56 if (numColours > maxColours) numColours = maxColours;
57 colour.resize(maxColours,0xFF000000);
58 Y.resize(maxColours,16);
59 Cr.resize(maxColours,128);
60 Cb.resize(maxColours,128);
61 A.resize(maxColours,255);
64 void Palette::setColour(UCHAR index, ULONG tColour)
66 if (index >= maxColours) return;
67 if (index >= numColours) numColours = index + 1;
68 colour[index] = tColour;
69 argb2yrba(tColour, Y[index], Cr[index], Cb[index], A[index]);
72 void Palette::setYCrCbA(UCHAR index, UCHAR tY, UCHAR tCr, UCHAR tCb, UCHAR tA)
74 if (index >= maxColours) return;
75 if (index >= numColours) numColours = index + 1;
76 Y[index] = tY; Cr[index] = tCr; Cb[index] = tCb; A[index] = tA;
77 colour[index] = yrba2argb(tY, tCr, tCb, tA);
80 Bitmap::Bitmap(UINT tWidth, UINT tHeight, UCHAR tBpp)
82 setSize(tWidth, tHeight);
86 void Bitmap::setSize(UINT tWidth, UINT tHeight)
88 if (width == tWidth && height == tHeight) return;
89 width = tWidth; height = tHeight;
90 if (width == 0 || height == 0)
93 bitmap.assign(width * height, 0);
96 bool Bitmap::setIndex(UINT x, UINT y, UCHAR index)
98 if (x <= width && y <= height)
100 bitmap[x + y*width] = index;
106 UCHAR Bitmap::getIndex(UINT x, UINT y) const
108 if (x > width || y > height)
111 return bitmap[x + y*width];
114 ULONG Bitmap::getColour(UINT x, UINT y) const
116 if (x > width || y > height)
119 return palette.getColour(bitmap[x + y*width]);
122 void Bitmap::setAllIndices(UCHAR index)
124 bitmap.assign(width * height, index);