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.
23 DisplayRegion::DisplayRegion()
33 Palette::Palette(u1 tBpp)
39 void Palette::argb2yrba(u4 argb, u1& y, u1& cr, u1& cb, u1& a)
41 a = static_cast<u1>((argb & 0xFF000000) >> 24);
42 int r = (argb & 0x00FF0000) >> 16;
43 int g = (argb & 0x0000FF00) >> 8;
44 int b = (argb & 0x000000FF);
45 y = static_cast<u1>((1052*r + 2065*g + 401*b + 4096*16 + 2048) / 4096);
46 cr = static_cast<u1>((1799*r - 1508*g - 291*b + 4096*128 + 2048) / 4096);
47 cb = static_cast<u1>((-608*r - 1191*g + 1799*b + 4096*128 + 2048) / 4096);
50 u4 Palette::yrba2argb(u1 y, u1 cr, u1 cb, u1 a)
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;
62 return (a << 24) + (r << 16) + (g << 8) + b;
65 void Palette::setBpp(u1 tBpp)
68 if (bpp > MAX_DEPTH) bpp = MAX_DEPTH;
69 maxColours = 1 << bpp;
70 if (numColours > maxColours) numColours = maxColours;
71 colour.resize(maxColours,0xFF000000);
72 Y.resize(maxColours,16);
73 Cr.resize(maxColours,128);
74 Cb.resize(maxColours,128);
75 A.resize(maxColours,255);
78 void Palette::setColour(u1 index, u4 tColour)
80 if (index >= maxColours) return;
81 if (index >= numColours) numColours = index + 1;
82 colour[index] = tColour;
83 argb2yrba(tColour, Y[index], Cr[index], Cb[index], A[index]);
86 void Palette::setYCrCbA(u1 index, u1 tY, u1 tCr, u1 tCb, u1 tA)
88 if (index >= maxColours) return;
89 if (index >= numColours) numColours = index + 1;
90 Y[index] = tY; Cr[index] = tCr; Cb[index] = tCb; A[index] = tA;
91 colour[index] = yrba2argb(tY, tCr, tCb, tA);
94 Bitmap::Bitmap(u4 tWidth, u4 tHeight, u1 tBpp)
96 setSize(tWidth, tHeight);
100 void Bitmap::setSize(u4 tWidth, u4 tHeight)
102 if (width == tWidth && height == tHeight) return;
103 width = tWidth; height = tHeight;
104 if (width == 0 || height == 0)
107 bitmap.assign(width * height, 0);
110 bool Bitmap::setIndex(u4 x, u4 y, u1 index)
112 if (x <= width && y <= height)
114 bitmap[x + y*width] = index;
120 u1 Bitmap::getIndex(u4 x, u4 y) const
122 if (x > width || y > height)
125 return bitmap[x + y*width];
128 u4 Bitmap::getColour(u4 x, u4 y) const
130 if (x > width || y > height)
133 return palette.getColour(bitmap[x + y*width]);
136 void Bitmap::setAllIndices(u1 index)
138 bitmap.assign(width * height, index);