]> git.jsancho.org Git - lugaru.git/blob - Source/TGALoader.cpp
266cdc30ef982dac0daaba1c56fc31ebbb557a58
[lugaru.git] / Source / TGALoader.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program 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.  
14
15 See the 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 /**> HEADER FILES <**/
23 #include "Game.h"
24 #include "TGALoader.h"
25
26 extern float texdetail;
27 extern TGAImageRec texture;
28 extern short vRefNum;
29 extern long dirID;
30 extern bool visibleloading;
31 extern Game * pgame;
32
33 extern bool LoadImage(const char * fname, TGAImageRec & tex);
34 /********************> LoadTGA() <*****/
35 bool upload_image(const unsigned char* filePath, bool hasalpha)
36 {
37         if(visibleloading) 
38                 pgame->LoadingScreen();
39
40 #if !PLATFORM_MACOSX
41
42         // for Windows, just use TGA loader for now
43         char fileName[ 256];
44         CopyPascalStringToC( filePath, fileName);
45 /*
46         // change extension to .TGA
47         int len = strlen( fileName);
48         if (len > 3)
49         {
50                 fileName[ len - 3] = 't';
51                 fileName[ len - 2] = 'g';
52                 fileName[ len - 1] = 'a';
53         }
54 */
55 //      return (LoadTGA( fileName) != NULL);
56         return (LoadImage(fileName, texture));
57
58 #else
59
60         OSStatus err;
61         ComponentResult cr;
62
63         /*FSRef fsref;
64         Boolean isdir;
65         err = FSPathMakeRef((const UInt8*)filePath, &fsref, &isdir);
66         if(err)return;
67
68         FSSpec fsspec;
69         err = FSGetCatalogInfo(&fsref, kFSCatInfoNone, NULL, NULL, &fsspec, NULL);
70         if(err)return;
71         */
72
73         //Boolean isdir;
74         FSSpec fsspec;
75         //err = FSMakeFSSpec (0, 0, (const unsigned char*)filePath, &fsspec);
76         err = FSMakeFSSpec (0, 0, filePath, &fsspec);
77         //err=FSPathMakeFSSpec((const UInt8*)filePath,&fsspec,&isdir);*/
78         if(err)return;
79
80         GraphicsImportComponent gi;
81         err = GetGraphicsImporterForFile(&fsspec, &gi);
82         if(err)return;
83
84         Rect natbounds;
85         cr = GraphicsImportGetNaturalBounds(gi, &natbounds);
86
87         size_t buffersize = 4 * natbounds.bottom * natbounds.right;
88         //void* buf = malloc(buffersize);
89         texture.sizeX=natbounds.right;
90         texture.sizeY=natbounds.bottom;
91         /*if(hasalpha)*/texture.bpp = 32;
92         //if(!hasalpha)texture.bpp = 24;
93
94         GWorldPtr gw;
95         err = QTNewGWorldFromPtr(&gw, k32ARGBPixelFormat, &natbounds, NULL, NULL,
96                 0, texture.data, 4 * natbounds.right);
97         if(err)return;
98
99         cr = GraphicsImportSetGWorld(gi, gw, NULL);
100
101         natbounds.top = natbounds.bottom;
102         natbounds.bottom = 0;
103
104         cr = GraphicsImportSetBoundsRect(gi, &natbounds);
105
106         cr = GraphicsImportDraw(gi);
107
108         err = CloseComponent(gi);
109         if(err)return;
110
111         /*glTexImage2D(textureTarget, 0, GL_RGBA, natbounds.right, natbounds.top, 0,
112         GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf);
113         */
114
115         //free(buf);
116         DisposeGWorld(gw);
117
118         // Loop Through The Image Data
119         GLuint                  imageSize;                                                                      // Used To Store The Image Size When Setting Aside Ram
120         GLuint                  temp;                                                                           // Temporary Variable
121         GLuint                  bytesPerPixel;                                                                          // Temporary Variable
122         bytesPerPixel=texture.bpp/8;
123         imageSize = texture.sizeX * texture.sizeY * bytesPerPixel;
124         int alltrans=10;
125
126         for( GLuint i = 0; i < int( imageSize ); i += 4 )
127         {
128                 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
129                 temp = texture.data[i];                                 // Temporarily Store The Value At Image Data 'i'
130                 texture.data[i] = texture.data[i + 1];  // Set The 1st Byte To The Value Of The 3rd Byte
131                 texture.data[i + 1] = texture.data[i + 2];                              // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
132                 texture.data[i + 2] = texture.data[i + 3];
133                 texture.data[i + 3] = temp;
134         }
135
136         int tempplace;
137         tempplace=0;
138         if(!hasalpha){
139                 for( GLuint i = 0; i < int( imageSize ); i += 4 )
140                 {
141                         texture.data[i + 3] = 255;
142                         /*texture.data[tempplace] = texture.data[i];    // Set The 1st Byte To The Value Of The 3rd Byte
143                         texture.data[tempplace + 1] = texture.data[i + 1];                              // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
144                         texture.data[tempplace + 2] = texture.data[i + 2];
145                         tempplace+=3;*/
146                 }
147         }
148
149         if(texdetail>1){
150                 int which=0;
151                 float temp;
152                 float howmany;
153                 for( GLuint k = 0; k < int( imageSize); k += bytesPerPixel*texture.sizeX*texdetail )
154                 {
155                         for( GLuint i = 0; i < int( imageSize/texture.sizeY ); i += bytesPerPixel*texdetail )
156                         {
157                                 for( GLuint b = 0; b < bytesPerPixel ; b ++ ){  
158                                         temp=0;
159                                         howmany=0;
160                                         for( GLuint l = 0; l < texdetail*texture.sizeX ; l +=texture.sizeX ){
161                                                 for( GLuint j = 0; j < texdetail ; j ++ )
162                                                 {
163                                                         temp += (int)texture.data[k+i+j*bytesPerPixel+l*bytesPerPixel+b];       // Set The 1st Byte To The Value Of The 3rd Byte
164                                                         howmany++;
165                                                 }
166                                         }
167                                         texture.data[which+b]=GLubyte(temp/howmany);
168                                 }
169                                 which+=bytesPerPixel;
170                         }
171                 }
172                 texture.sizeX/=texdetail;
173                 texture.sizeY/=texdetail;
174         }
175
176         return true;
177
178 #endif
179 }