Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TextureConversions.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: TextureConversions.cpp,v 1.4 2002/01/11 14:34:59 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/TeddyConfig.h"
00026 #include "Teddy/Graphics/Device.h"
00027 #include "Teddy/Graphics/Texture.h"
00028 
00029 
00030 namespace Teddy    {
00031 namespace Graphics {
00032 
00033 
00035 void Texture::convert_to_a(){
00036     switch( work_data_pixel_format ){
00037     case GL_RGB:  convert_rgb_to_a (); break;
00038     case GL_RGBA: convert_rgba_to_a(); break;
00039     default: break;
00040     }
00041 
00042     work_data_pixel_format = GL_ALPHA;
00043 }
00044 
00045 
00047 void Texture::convert_to_rgb(){
00048     switch( work_data_pixel_format ){
00049     case GL_ALPHA: convert_a_to_rgb   (); break;
00050     case GL_RGBA:  convert_rgba_to_rgb(); break;
00051     default: break;
00052     }
00053 
00054     work_data_pixel_format = GL_RGB;
00055 }
00056 
00057 /*
00058 void Texture::convert_to_rgba(){
00059     switch( work_format ){
00060     case FORMAT_A:   convert_a_to_rgb   (); 
00061     case FORMAT_RGB: convert_rgb_to_rgba(); break;
00062     default: break;
00063     }
00064 
00065     work_format = FORMAT_RGBA;
00066 }*/
00067 
00068 
00070 void Texture::convert_a_to_rgb(){
00071     unsigned char *in  = work_data;
00072     unsigned char *out = new unsigned char[size[0]*size[1]];
00073     int            x   = 0;
00074     int            y   = 0;
00075     int            a   = 0;
00076 
00077     for( y=0; y<size[1]; y++ ){
00078         for( x=0; x<size[0]; x++ ){
00079             a = in[y*size[0]+x];
00080             out[y*size[0]*3+x*3+0] = a;
00081             out[y*size[0]*3+x*3+1] = a;
00082             out[y*size[0]*3+x*3+2] = a;
00083         }
00084     }
00085 
00086     setWorkData( out );
00087 }
00088 
00089 
00091 void Texture::convert_rgb_to_a(){
00092     unsigned char *in  = work_data;
00093     unsigned char *out = new unsigned char[size[0]*size[1]];
00094     int            x   = 0;
00095     int            y   = 0;
00096     int            r   = 0;
00097     int            g   = 0;
00098     int            b   = 0;
00099     int            a   = 0;
00100 
00101     for( y=0; y<size[1]; y++ ){
00102         for( x=0; x<size[0]; x++ ){
00103             r = in[y*size[0]*3+x*3+0];
00104             g = in[y*size[0]*3+x*3+1];
00105             b = in[y*size[0]*3+x*3+2];
00106             a = r;
00107             out[y*size[0]+x] = a;
00108         }
00109     }
00110 
00111     setWorkData( out );
00112 }
00113 
00114 
00116 void Texture::convert_rgba_to_a(){
00117     unsigned char *in  = work_data;
00118     unsigned char *out = new unsigned char[size[0]*size[1]];
00119     int            x   = 0;
00120     int            y   = 0;
00121     int            r   = 0;
00122     int            g   = 0;
00123     int            b   = 0;
00124     int            a   = 0;
00125 
00126     for( y=0; y<size[1]; y++ ){
00127         for( x=0; x<size[0]; x++ ){
00128             r = in[y*size[0]*4+x*4+0];
00129             g = in[y*size[0]*4+x*4+1];
00130             b = in[y*size[0]*4+x*4+2];
00131             a = in[y*size[0]*4+x*4+2];
00132             out[y*size[0]+x] = a;
00133         }
00134     }
00135 
00136     setWorkData( out );
00137 }
00138 
00139 
00141 void Texture::convert_rgba_to_rgb(){
00142     unsigned char *in  = work_data;
00143     unsigned char *out = new unsigned char[int(size[0]*size[1])*3];
00144     int            x   = 0;
00145     int            y   = 0;
00146     int            r   = 0;
00147     int            g   = 0;
00148     int            b   = 0;
00149     int            a   = 0;
00150 
00151     for( y=0; y<size[1]; y++ ){
00152         for( x=0; x<size[0]; x++ ){
00153             r = in[y*size[0]*4+x*4+0];
00154             g = in[y*size[0]*4+x*4+1];
00155             b = in[y*size[0]*4+x*4+2];
00156             a = in[y*size[0]*4+x*4+2];
00157             out[y*size[0]*3+x*3+0] = r;
00158             out[y*size[0]*3+x*3+1] = g;
00159             out[y*size[0]*3+x*3+2] = b;
00160         }
00161     }
00162 
00163     setWorkData( out );
00164 }
00165 
00166 
00167 };  //  namespace Graphics
00168 };  //  namespace Teddy
00169