Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004 TightVNC Team. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- PixelFormatList class |
| 20 | |
| 21 | #include <rfbplayer/PixelFormatList.h> |
| 22 | |
| 23 | using namespace rfb; |
| 24 | using namespace rfb::win32; |
| 25 | |
| 26 | PixelFormatList::PixelFormatList() { |
| 27 | PixelFormatListElement PFElem; |
| 28 | |
| 29 | // -=- Add the default pixel formats to list |
| 30 | // PF_BGR233 |
| 31 | PFElem.setName("8-bits depth (BGR233)"); |
| 32 | PFElem.setPF(&PixelFormat(8,8,0,1,7,7,3,0,3,6)); |
| 33 | PFList.push_back(PFElem); |
| 34 | // PF_RGB555 |
| 35 | PFElem.setName("15-bits depth (RGB555)"); |
| 36 | PFElem.setPF(&PixelFormat(16,15,0,1,31,31,31,10,5,0)); |
| 37 | PFList.push_back(PFElem); |
| 38 | // PF_BGR555 |
| 39 | PFElem.setName("15-bits depth (BGR555)"); |
| 40 | PFElem.setPF(&PixelFormat(16,15,0,1,31,31,31,0,5,10)); |
| 41 | PFList.push_back(PFElem); |
| 42 | // PF_RGB565 |
| 43 | PFElem.setName("16-bits depth (RGB565)"); |
| 44 | PFElem.setPF(&PixelFormat(16,16,0,1,31,63,31,11,5,0)); |
| 45 | PFList.push_back(PFElem); |
| 46 | // PF_BGR565 |
| 47 | PFElem.setName("16-bits depth (BGR565)"); |
| 48 | PFElem.setPF(&PixelFormat(16,16,0,1,31,63,31,0,5,11)); |
| 49 | PFList.push_back(PFElem); |
| 50 | // PF_RGB888 |
| 51 | PFElem.setName("24-bits depth (RGB888)"); |
| 52 | PFElem.setPF(&PixelFormat(32,24,0,1,255,255,255,16,8,0)); |
| 53 | PFList.push_back(PFElem); |
| 54 | // PF_BGR888 |
| 55 | PFElem.setName("24-bits depth (BGR888)"); |
| 56 | PFElem.setPF(&PixelFormat(32,24,0,1,255,255,255,0,8,16)); |
| 57 | PFList.push_back(PFElem); |
| 58 | |
| 59 | PF_DEFAULT_COUNT = PFList.size(); |
| 60 | } |
| 61 | |
| 62 | PixelFormatListElement *PixelFormatList::operator[](int index) { |
| 63 | return &(*getIterator(index)); |
| 64 | } |
| 65 | |
| 66 | void PixelFormatList::add(char *format_name, PixelFormat PF) { |
| 67 | PixelFormatListElement PFElem; |
| 68 | PFElem.setName(format_name); |
| 69 | PFElem.setPF(&PF); |
| 70 | PFList.push_back(PFElem); |
| 71 | } |
| 72 | |
| 73 | void PixelFormatList::insert(int index, char *format_name, PixelFormat PF) { |
| 74 | if (isDefaultPF(index)) |
| 75 | rdr::Exception("PixelFormatList:can't insert to the default pixel format place"); |
| 76 | |
| 77 | PixelFormatListElement PFElem; |
| 78 | PFElem.setName(format_name); |
| 79 | PFElem.setPF(&PF); |
| 80 | PFList.insert(getIterator(index), PFElem); |
| 81 | } |
| 82 | |
| 83 | void PixelFormatList::remove(int index) { |
| 84 | if (isDefaultPF(index)) |
| 85 | rdr::Exception("PixelFormatList:can't remove the default pixel format"); |
| 86 | PFList.erase(getIterator(index)); |
| 87 | } |
| 88 | |
| 89 | list <PixelFormatListElement>::iterator PixelFormatList::getIterator(int index) { |
| 90 | if ((index >= PFList.size()) || (index < 0)) |
| 91 | rdr::Exception("PixelFormatList:out of range"); |
| 92 | |
| 93 | int i = 0; |
| 94 | list <PixelFormatListElement>::iterator iter; |
| 95 | for (iter = PFList.begin(); iter != PFList.end(); iter++) { |
| 96 | if (i++ == index) break; |
| 97 | } |
| 98 | return iter; |
| 99 | } |
| 100 | |
| 101 | bool PixelFormatList::isDefaultPF(int index) { |
| 102 | if (index < PF_DEFAULT_COUNT) return true; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | void PixelFormatList::readUserDefinedPF(HKEY root, const char *keypath) { |
| 107 | RegKey regKey; |
| 108 | regKey.createKey(root, keypath); |
| 109 | int count = regKey.getInt(_T("PixelFormatCount"), 0); |
| 110 | if (count > 0) { |
| 111 | // Erase all user defined pixel formats |
| 112 | int upf_count = getUserPFCount(); |
| 113 | if (upf_count > 0) { |
| 114 | for(int i = 0; i < upf_count; i++) { |
| 115 | remove(PF_DEFAULT_COUNT); |
| 116 | } |
| 117 | } |
| 118 | // Add the user defined pixel formats from the registry |
| 119 | for(int i = 0; i < count; i++) { |
| 120 | char upf_name[20] = "\0"; |
| 121 | sprintf(upf_name, "%s%i", "Upf", i); |
| 122 | int size = sizeof(PixelFormatListElement); |
| 123 | PixelFormatListElement *pPFElem = 0;// = &PFElem; |
| 124 | regKey.getBinary(upf_name, (void**)&pPFElem, &size); |
| 125 | PFList.push_back(*pPFElem); |
| 126 | if (pPFElem) delete pPFElem; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void PixelFormatList::writeUserDefinedPF(HKEY root, const char *keypath) { |
| 132 | RegKey regKey; |
| 133 | |
| 134 | // Delete all user defined pixel formats from the regisry |
| 135 | regKey.createKey(root, keypath);//_T("Software\\TightVnc\\RfbPlayer\\UserDefinedPF")); |
| 136 | int count = regKey.getInt(_T("PixelFormatCount"), 0); |
| 137 | for (int i = 0; i < count; i++) { |
| 138 | char upf_name[20] = "\0"; |
| 139 | sprintf(upf_name, "%s%i", "Upf", i); |
| 140 | regKey.deleteValue(upf_name); |
| 141 | } |
| 142 | regKey.setInt(_T("PixelFormatCount"), 0); |
| 143 | |
| 144 | // Write new user defined pixel formats to the registry |
| 145 | regKey.setInt(_T("PixelFormatCount"), getUserPFCount()); |
| 146 | for (i = 0; i < getUserPFCount(); i++) { |
| 147 | char upf_name[20] = "\0"; |
| 148 | sprintf(upf_name, "%s%i", "Upf", i); |
| 149 | regKey.setBinary(upf_name, (void *)operator[](i+getDefaultPFCount()), |
| 150 | sizeof(PixelFormatListElement)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | int PixelFormatList::getIndexByPFName(const char *format_name) { |
| 155 | for (int i = 0; i < PixelFormatList::count(); i++) { |
| 156 | if (_stricmp(operator[](i)->format_name, format_name) == 0) return i; |
| 157 | } |
| 158 | return -1; |
| 159 | } |