1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "graphics/pixelformat.h"
24 #include "common/algorithm.h"
25
26 namespace Graphics {
27
toString() const28 Common::String PixelFormat::toString() const {
29 if (bytesPerPixel == 1)
30 return "CLUT8";
31
32 // We apply a trick to simplify the code here. We encode all the shift,
33 // loss, and component name in the component entry. By having the shift as
34 // highest entry we can sort according to shift.
35 // This works because in valid RGB PixelFormats shift values needs to be
36 // distinct except when the loss is 8. However, components with loss value
37 // of 8 are not printed, thus their position does not matter.
38 int component[4];
39 component[0] = (rShift << 16) | (rLoss << 8) | 'R';
40 component[1] = (gShift << 16) | (gLoss << 8) | 'G';
41 component[2] = (bShift << 16) | (bLoss << 8) | 'B';
42 component[3] = (aShift << 16) | (aLoss << 8) | 'A';
43
44 // Sort components according to descending shift value.
45 Common::sort(component, component + ARRAYSIZE(component), Common::Greater<int>());
46
47 Common::String letters, digits;
48 for (int i = 0; i < ARRAYSIZE(component); ++i) {
49 const int componentLoss = (component[i] >> 8) & 0xFF;
50 // A loss of 8 means that the component does not exist.
51 if (componentLoss == 8) {
52 continue;
53 }
54
55 const char componentName = component[i] & 0xFF;
56
57 letters += componentName;
58 digits += '0' + 8 - componentLoss;
59 }
60
61 return letters + digits + '@' + ('0' + bytesPerPixel);
62 }
63
64 } // End of namespace Graphics
65