]> git.jsancho.org Git - lugaru.git/blob - Source/TGALoader.cpp
License: Update GPLv2+ header to match current FSF recommendation
[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 modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru 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.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**> HEADER FILES <**/
21 #include "Game.h"
22 #include "TGALoader.h"
23
24 extern float texdetail;
25 extern TGAImageRec texture;
26 extern short vRefNum;
27 extern long dirID;
28 extern bool visibleloading;
29
30 extern bool LoadImage(const char * fname, TGAImageRec & tex);
31 /********************> LoadTGA() <*****/
32 bool upload_image(const unsigned char* filePath, bool hasalpha)
33 {
34     if (visibleloading)
35         Game::LoadingScreen();
36
37 #if !PLATFORM_MACOSX
38
39     // for Windows, just use TGA loader for now
40     char fileName[256];
41     CopyPascalStringToC( filePath, fileName);
42     /*
43         // change extension to .TGA
44         int len = strlen( fileName);
45         if (len > 3)
46         {
47             fileName[ len - 3] = 't';
48             fileName[ len - 2] = 'g';
49             fileName[ len - 1] = 'a';
50         }
51     */
52 //return (LoadTGA( fileName) != NULL);
53     return (LoadImage(fileName, texture));
54
55 #else
56
57     OSStatus err;
58     ComponentResult cr;
59
60     /*FSRef fsref;
61     Boolean isdir;
62     err = FSPathMakeRef((const UInt8*)filePath, &fsref, &isdir);
63     if(err)return;
64
65     FSSpec fsspec;
66     err = FSGetCatalogInfo(&fsref, kFSCatInfoNone, NULL, NULL, &fsspec, NULL);
67     if(err)return;
68     */
69
70     //Boolean isdir;
71     FSSpec fsspec;
72     //err = FSMakeFSSpec (0, 0, (const unsigned char*)filePath, &fsspec);
73     err = FSMakeFSSpec (0, 0, filePath, &fsspec);
74     //err=FSPathMakeFSSpec((const UInt8*)filePath,&fsspec,&isdir);*/
75     if (err)
76         return;
77
78     GraphicsImportComponent gi;
79     err = GetGraphicsImporterForFile(&fsspec, &gi);
80     if (err)
81         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)*/
91     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)
98         return;
99
100     cr = GraphicsImportSetGWorld(gi, gw, NULL);
101
102     natbounds.top = natbounds.bottom;
103     natbounds.bottom = 0;
104
105     cr = GraphicsImportSetBoundsRect(gi, &natbounds);
106
107     cr = GraphicsImportDraw(gi);
108
109     err = CloseComponent(gi);
110     if (err)
111         return;
112
113     /*glTexImage2D(textureTarget, 0, GL_RGBA, natbounds.right, natbounds.top, 0,
114     GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buf);
115     */
116
117     //free(buf);
118     DisposeGWorld(gw);
119
120     // Loop Through The Image Data
121     GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
122     GLuint temp; // Temporary Variable
123     GLuint bytesPerPixel; // Temporary Variable
124     bytesPerPixel = texture.bpp / 8;
125     imageSize = texture.sizeX * texture.sizeY * bytesPerPixel;
126     //~ int alltrans=10;
127
128     for ( GLuint i = 0; i < int( imageSize ); i += 4 ) {
129         // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
130         temp = texture.data[i]; // Temporarily Store The Value At Image Data 'i'
131         texture.data[i] = texture.data[i + 1]; // Set The 1st Byte To The Value Of The 3rd Byte
132         texture.data[i + 1] = texture.data[i + 2]; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
133         texture.data[i + 2] = texture.data[i + 3];
134         texture.data[i + 3] = temp;
135     }
136
137     //~ int tempplace;
138     //~ tempplace=0;
139     if (!hasalpha) {
140         for ( GLuint i = 0; i < int( imageSize ); i += 4 ) {
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             for ( GLuint i = 0; i < int( imageSize / texture.sizeY ); i += bytesPerPixel * texdetail ) {
155                 for ( GLuint b = 0; b < bytesPerPixel ; b ++ ) {
156                     temp = 0;
157                     howmany = 0;
158                     for ( GLuint l = 0; l < texdetail * texture.sizeX ; l += texture.sizeX ) {
159                         for ( GLuint j = 0; j < texdetail ; j ++ ) {
160                             temp += (int)texture.data[k + i + j * bytesPerPixel + l * bytesPerPixel + b]; // Set The 1st Byte To The Value Of The 3rd Byte
161                             howmany++;
162                         }
163                     }
164                     texture.data[which + b] = GLubyte(temp / howmany);
165                 }
166                 which += bytesPerPixel;
167             }
168         }
169         texture.sizeX /= texdetail;
170         texture.sizeY /= texdetail;
171     }
172
173     return true;
174
175 #endif
176 }