PaletteManager.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2006 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "PaletteManager.h"
00022 #include "IDataSource.h"
00023 #include "RenderSurface.h"
00024 #include "Texture.h"
00025 
00026 PaletteManager* PaletteManager::palettemanager = 0;
00027 
00028 PaletteManager::PaletteManager(RenderSurface *rs)
00029         : rendersurface(rs)
00030 {
00031         con.Print(MM_INFO, "Creating PaletteManager...\n");
00032 
00033         assert(palettemanager == 0);
00034         palettemanager = this;
00035 }
00036 
00037 PaletteManager::~PaletteManager()
00038 {
00039         reset();
00040         con.Print(MM_INFO, "Destroying PaletteManager...\n");
00041         palettemanager = 0;
00042 }
00043 
00044 // Reset the Palette Manager
00045 void PaletteManager::reset()
00046 {
00047         con.Print(MM_INFO, "Resetting PaletteManager...\n");
00048 
00049         for (unsigned int i = 0; i < palettes.size(); ++i)
00050                 delete palettes[i];
00051         palettes.clear();
00052 }
00053 
00054 void PaletteManager::updatedFont(PalIndex index)
00055 {
00056         Pentagram::Palette* pal = getPalette(index);
00057         if (pal)
00058                 rendersurface->CreateNativePalette(pal); // convert to native format
00059 }
00060 
00061 // Reset all the transforms back to default
00062 void PaletteManager::resetTransforms()
00063 {
00064         con.Print(MM_INFO, "Resetting Palette Transforms...\n");
00065 
00066         sint16 matrix[12];
00067         getTransformMatrix(matrix, Pentagram::Transform_None);
00068 
00069         for (unsigned int i = 0; i < palettes.size(); ++i)
00070         {
00071                 Pentagram::Palette* pal = palettes[i];
00072                 if (!pal) continue;
00073                 pal->transform = Pentagram::Transform_None;
00074                 for (int j = 0; j < 12; j++) pal->matrix[j] = matrix[j];
00075                 rendersurface->CreateNativePalette(pal); // convert to native format
00076         }
00077 }
00078 
00079 // Change the Render Surface used by the PaletteManager
00080 void PaletteManager::RenderSurfaceChanged(RenderSurface* rs)
00081 {
00082         rendersurface = rs;
00083 
00084         // Create native palettes for all currently loaded palettes
00085         for (unsigned int i = 0; i < palettes.size(); ++i)
00086                 if (palettes[i])
00087                         rendersurface->CreateNativePalette(palettes[i]); 
00088 }
00089 
00090 void PaletteManager::load(PalIndex index, IDataSource& ds,IDataSource &xformds)
00091 {
00092         if (palettes.size() <= static_cast<unsigned int>(index))
00093                 palettes.resize(index+1);
00094 
00095         if (palettes[index])
00096                 delete palettes[index];
00097 
00098         Pentagram::Palette* pal = new Pentagram::Palette;
00099         pal->load(ds,xformds);
00100         rendersurface->CreateNativePalette(pal); // convert to native format
00101 
00102         palettes[index] = pal;
00103 }
00104 
00105 void PaletteManager::load(PalIndex index, IDataSource& ds)
00106 {
00107         if (palettes.size() <= static_cast<unsigned int>(index))
00108                 palettes.resize(index+1);
00109 
00110         if (palettes[index])
00111                 delete palettes[index];
00112 
00113         Pentagram::Palette* pal = new Pentagram::Palette;
00114         pal->load(ds);
00115         rendersurface->CreateNativePalette(pal); // convert to native format
00116 
00117         palettes[index] = pal;
00118 }
00119 
00120 void PaletteManager::duplicate(PalIndex src, PalIndex dest)
00121 {
00122         Pentagram::Palette* newpal = getPalette(dest);
00123         if (!newpal)
00124                 newpal = new Pentagram::Palette;
00125         Pentagram::Palette* srcpal = getPalette(src);
00126         if (srcpal)
00127                 *newpal = *srcpal;
00128 
00129         rendersurface->CreateNativePalette(newpal); // convert to native format
00130         if (palettes.size() <= static_cast<unsigned int>(dest))
00131                 palettes.resize(dest+1);
00132         palettes[dest] = newpal;
00133 }
00134 
00135 Pentagram::Palette* PaletteManager::getPalette(PalIndex index)
00136 {
00137         if (static_cast<unsigned int>(index) >= palettes.size())
00138                 return 0;
00139 
00140         return palettes[index];
00141 }
00142 
00143 void PaletteManager::transformPalette(PalIndex index, sint16 matrix[12])
00144 {
00145         Pentagram::Palette *pal = getPalette(index);
00146 
00147         if (!pal) return;
00148 
00149         for (int i = 0; i < 12; i++) pal->matrix[i] = matrix[i];
00150         rendersurface->CreateNativePalette(pal); // convert to native format
00151 }
00152 
00153 void PaletteManager::untransformPalette(PalIndex index)
00154 {
00155         Pentagram::Palette *pal = getPalette(index);
00156 
00157         if (!pal) return;
00158 
00159         pal->transform = Pentagram::Transform_None;
00160         sint16 matrix[12];
00161         getTransformMatrix(matrix, Pentagram::Transform_None);
00162         transformPalette(index, matrix);
00163 }
00164 
00165 void PaletteManager::getTransformMatrix(sint16 matrix[12], Pentagram::PalTransforms trans)
00166 {
00167         switch (trans)
00168         {
00169                 // Normal untransformed palette
00170                 case Pentagram::Transform_None:
00171                 {
00172                         matrix[0] = 0x800;      matrix[1] = 0;          matrix[2]  = 0;         matrix[3]  = 0;
00173                         matrix[4] = 0;          matrix[5] = 0x800;      matrix[6]  = 0;         matrix[7]  = 0;
00174                         matrix[8] = 0;          matrix[9] = 0;          matrix[10] = 0x800;     matrix[11] = 0;
00175                 }
00176                 break;
00177 
00178                 // O[i] = I[r]*0.375 + I[g]*0.5 + I[b]*0.125;
00179                 case Pentagram::Transform_Greyscale:
00180                 {
00181                         for (int i = 0; i < 3; i++)
00182                         {
00183                                 matrix[i*4+0] = 0x0300; 
00184                                 matrix[i*4+1] = 0x0400; 
00185                                 matrix[i*4+2] = 0x0100; 
00186                                 matrix[i*4+3] = 0;
00187                         }
00188                 }
00189                 break;
00190 
00191                 // O[r] = 0;
00192                 case Pentagram::Transform_NoRed:
00193                 {
00194                         matrix[0] = 0;  matrix[1] = 0;          matrix[2]  = 0;         matrix[3]  = 0;
00195                         matrix[4] = 0;  matrix[5] = 0x800;      matrix[6]  = 0;         matrix[7]  = 0;
00196                         matrix[8] = 0;  matrix[9] = 0;          matrix[10] = 0x800;     matrix[11] = 0;
00197                 }
00198                 break;
00199 
00200                 // O[i] = (I[i] + Grey)*0.25 + 0.1875;
00201                 case Pentagram::Transform_RainStorm:
00202                 {
00203                         for (int i = 0; i < 3; i++)
00204                         {
00205                                 matrix[i*4+0] = (0x0300*0x0200)>>11;
00206                                 matrix[i*4+1] = (0x0400*0x0200)>>11;    
00207                                 matrix[i*4+2] = (0x0100*0x0200)>>11;    
00208 
00209                                 matrix[i*4+i]+= 0x0200;
00210 
00211                                 matrix[i*4+3] = 0x0180;
00212                         }
00213                 }
00214                 break;
00215 
00216                 // O[r] = I[r]*0.5 + Grey*0.5  + 0.1875; 
00217                 // O[g] = I[g]*0.5 + Grey*0.25; 
00218                 // O[b] = I[b]*0.5;
00219                 case Pentagram::Transform_FireStorm:
00220                 {
00221                         // O[r] = I[r]*0.5 + Grey*0.5 + 0.1875; 
00222                         matrix[0] =((0x0300*0x0400)>>11) + 0x0400;
00223                         matrix[1] = (0x0400*0x0400)>>11;        
00224                         matrix[2] = (0x0100*0x0400)>>11;        
00225                         matrix[3]  = 0x0180;
00226 
00227                         // O[g] = I[g]*0.5 + Grey*0.25; 
00228                         matrix[4] = (0x0300*0x0200)>>11;
00229                         matrix[5] =((0x0400*0x0200)>>11) + 0x0400;      
00230                         matrix[6] = (0x0100*0x0200)>>11;        
00231                         matrix[7]  = 0;
00232 
00233                         // O[b] = I[b]*0.5;
00234                         matrix[8]  = 0; 
00235                         matrix[9]  = 0; 
00236                         matrix[10] = 0x0400;    
00237                         matrix[11] = 0;
00238                 }
00239                 break;
00240 
00241                 // O[i] = I[i]*2 -Grey;
00242                 case Pentagram::Transform_Saturate:
00243                 {
00244                         for (int i = 0; i < 3; i++)
00245                         {
00246                                 matrix[i*4+0] = -0x0300;        
00247                                 matrix[i*4+1] = -0x0400;        
00248                                 matrix[i*4+2] = -0x0100;        
00249                                 matrix[i*4+3] = 0;
00250                                 matrix[i*4+i] += 0x1000;        
00251                         }
00252                 }
00253                 break;
00254 
00255                 // O[b] = I[r]; O[r] = I[g]; O[g] = I[b];
00256                 case Pentagram::Transform_BRG:
00257                 {
00258                         matrix[0] = 0;          matrix[1] = 0x800;      matrix[2]  = 0;         matrix[3]  = 0;
00259                         matrix[4] = 0;          matrix[5] = 0;          matrix[6]  = 0x800;     matrix[7]  = 0;
00260                         matrix[8] = 0x800;      matrix[9] = 0;          matrix[10] = 0;         matrix[11] = 0;
00261                 }
00262                 break;
00263 
00264                 // O[g] = I[r]; O[b] = I[g]; O[r] = I[b];
00265                 case Pentagram::Transform_GBR:
00266                 {
00267                         matrix[0] = 0;          matrix[1] = 0;          matrix[2]  = 0x800;     matrix[3]  = 0;
00268                         matrix[4] = 0x800;      matrix[5] = 0;          matrix[6]  = 0;         matrix[7]  = 0;
00269                         matrix[8] = 0;          matrix[9] = 0x800;      matrix[10] = 0;         matrix[11] = 0;
00270                 }
00271                 break;
00272 
00273                 // Unknown
00274         default:
00275                 {
00276                         perr << "Unknown Palette Transformation: " << trans << std::endl;
00277                         matrix[0] = 0x800;      matrix[1] = 0;          matrix[2]  = 0;         matrix[3]  = 0;
00278                         matrix[4] = 0;          matrix[5] = 0x800;      matrix[6]  = 0;         matrix[7]  = 0;
00279                         matrix[8] = 0;          matrix[9] = 0;          matrix[10] = 0x800;     matrix[11] = 0;
00280                 }
00281                 break;
00282         }
00283 }
00284 
00285 
00286 void PaletteManager::getTransformMatrix(sint16 matrix[12], uint32 col32)
00287 {
00288         matrix[0]  = (static_cast<sint32>(TEX32_A(col32))*0x800)/255;   
00289         matrix[1]  = 0; 
00290         matrix[2]  = 0; 
00291         matrix[3]  = (static_cast<sint32>(TEX32_R(col32))*0x800)/255;
00292 
00293         matrix[4]  = 0; 
00294         matrix[5]  = (static_cast<sint32>(TEX32_A(col32))*0x800)/255;   
00295         matrix[6]  = 0; 
00296         matrix[7]  = (static_cast<sint32>(TEX32_G(col32))*0x800)/255;
00297 
00298         matrix[8]  = 0; 
00299         matrix[9]  = 0; 
00300         matrix[10] = (static_cast<sint32>(TEX32_A(col32))*0x800)/255;   
00301         matrix[11] = (static_cast<sint32>(TEX32_B(col32))*0x800)/255;
00302 }

Generated on Fri Jul 27 22:27:28 2007 for pentagram by  doxygen 1.4.7