]> git.jsancho.org Git - lugaru.git/blob - Source/TGALoader.cpp
172ed5f1218f7c5b03efa9c06a3405cf37f7967d
[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
32 extern bool LoadImage(const char * fname, TGAImageRec & tex);
33 /********************> LoadTGA() <*****/
34 bool upload_image(const unsigned char* filePath, bool hasalpha)
35 {
36         if(visibleloading) 
37                 Game::LoadingScreen();
38
39 #if !PLATFORM_MACOSX
40
41         // for Windows, just use TGA loader for now
42         char fileName[256];
43         CopyPascalStringToC( filePath, fileName);
44 /*
45         // change extension to .TGA
46         int len = strlen( fileName);
47         if (len > 3)
48         {
49                 fileName[ len - 3] = 't';
50                 fileName[ len - 2] = 'g';
51                 fileName[ len - 1] = 'a';
52         }
53 */
54 //      return (LoadTGA( fileName) != NULL);
55         return (LoadImage(fileName, texture));
56
57 #else
58
59         OSStatus err;
60         ComponentResult cr;
61
62         /*FSRef fsref;
63         Boolean isdir;
64         err = FSPathMakeRef((const UInt8*)filePath, &fsref, &isdir);
65         if(err)return;
66
67         FSSpec fsspec;
68         err = FSGetCatalogInfo(&fsref, kFSCatInfoNone, NULL, NULL, &fsspec, NULL);
69         if(err)return;
70         */
71
72         //Boolean isdir;
73         FSSpec fsspec;
74         //err = FSMakeFSSpec (0, 0, (const unsigned char*)filePath, &fsspec);
75         err = FSMakeFSSpec (0, 0, filePath, &fsspec);
76         //err=FSPathMakeFSSpec((const UInt8*)filePath,&fsspec,&isdir);*/
77         if(err)return;
78
79         GraphicsImportComponent gi;
80         err = GetGraphicsImporterForFile(&fsspec, &gi);
81         if(err)return;
82
83         Rect natbounds;
84         cr = GraphicsImportGetNaturalBounds(gi, &natbounds);
85
86         //~ size_t buffersize = 4 * natbounds.bottom * natbounds.right;
87         //void* buf = malloc(buffersize);
88         texture.sizeX=natbounds.right;
89         texture.sizeY=natbounds.bottom;
90         /*if(hasalpha)*/texture.bpp = 32;
91         //if(!hasalpha)texture.bpp = 24;
92
93         GWorldPtr gw;
94         err = QTNewGWorldFromPtr(&gw, k32ARGBPixelFormat, &natbounds, NULL, NULL,
95                 0, texture.data, 4 * natbounds.right);
96         if(err)return;
97
98         cr = GraphicsImportSetGWorld(gi, gw, NULL);
99
100         natbounds.top = natbounds.bottom;
101         natbounds.bottom = 0;
102
103         cr = GraphicsImportSetBoundsRect(gi, &natbounds);
104
105         cr = GraphicsImportDraw(gi);
106
107         err = CloseComponent(gi);
108         if(err)return;
109
110         /*glTexImage2D(textureTarget, 0, GL_RGBA, natbounds.right, natbounds.top, 0,
111         GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf);
112         */
113
114         //free(buf);
115         DisposeGWorld(gw);
116
117         // Loop Through The Image Data
118         GLuint                  imageSize;                                                                      // Used To Store The Image Size When Setting Aside Ram
119         GLuint                  temp;                                                                           // Temporary Variable
120         GLuint                  bytesPerPixel;                                                                          // Temporary Variable
121         bytesPerPixel=texture.bpp/8;
122         imageSize = texture.sizeX * texture.sizeY * bytesPerPixel;
123         //~ int alltrans=10;
124
125         for( GLuint i = 0; i < int( imageSize ); i += 4 )
126         {
127                 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
128                 temp = texture.data[i];                                 // Temporarily Store The Value At Image Data 'i'
129                 texture.data[i] = texture.data[i + 1];  // Set The 1st Byte To The Value Of The 3rd Byte
130                 texture.data[i + 1] = texture.data[i + 2];                              // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
131                 texture.data[i + 2] = texture.data[i + 3];
132                 texture.data[i + 3] = temp;
133         }
134
135         //~ int tempplace;
136         //~ tempplace=0;
137         if(!hasalpha){
138                 for( GLuint i = 0; i < int( imageSize ); i += 4 )
139                 {
140                         texture.data[i + 3] = 255;
141                         /*texture.data[tempplace] = texture.data[i];    // Set The 1st Byte To The Value Of The 3rd Byte
142                         texture.data[tempplace + 1] = texture.data[i + 1];                              // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
143                         texture.data[tempplace + 2] = texture.data[i + 2];
144                         tempplace+=3;*/
145                 }
146         }
147
148         if(texdetail>1){
149                 int which=0;
150                 float temp;
151                 float howmany;
152                 for( GLuint k = 0; k < int( imageSize); k += bytesPerPixel*texture.sizeX*texdetail )
153                 {
154                         for( GLuint i = 0; i < int( imageSize/texture.sizeY ); i += bytesPerPixel*texdetail )
155                         {
156                                 for( GLuint b = 0; b < bytesPerPixel ; b ++ ){  
157                                         temp=0;
158                                         howmany=0;
159                                         for( GLuint l = 0; l < texdetail*texture.sizeX ; l +=texture.sizeX ){
160                                                 for( GLuint j = 0; j < texdetail ; j ++ )
161                                                 {
162                                                         temp += (int)texture.data[k+i+j*bytesPerPixel+l*bytesPerPixel+b];       // Set The 1st Byte To The Value Of The 3rd Byte
163                                                         howmany++;
164                                                 }
165                                         }
166                                         texture.data[which+b]=GLubyte(temp/howmany);
167                                 }
168                                 which+=bytesPerPixel;
169                         }
170                 }
171                 texture.sizeX/=texdetail;
172                 texture.sizeY/=texdetail;
173         }
174
175         return true;
176
177 #endif
178 }