]> git.jsancho.org Git - lugaru.git/blob - Source/TGALoader.cpp
748e8e3a6a43e84eb519ad7508ccbee972382c6c
[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     return (LoadImage(fileName, texture));
43
44 #else
45
46     OSStatus err;
47     ComponentResult cr;
48
49     //Boolean isdir;
50     FSSpec fsspec;
51     err = FSMakeFSSpec (0, 0, filePath, &fsspec);
52     if (err)
53         return;
54
55     GraphicsImportComponent gi;
56     err = GetGraphicsImporterForFile(&fsspec, &gi);
57     if (err)
58         return;
59
60     Rect natbounds;
61     cr = GraphicsImportGetNaturalBounds(gi, &natbounds);
62
63     texture.sizeX = natbounds.right;
64     texture.sizeY = natbounds.bottom;
65     texture.bpp = 32;
66
67     GWorldPtr gw;
68     err = QTNewGWorldFromPtr(&gw, k32ARGBPixelFormat, &natbounds, NULL, NULL,
69                              0, texture.data, 4 * natbounds.right);
70     if (err)
71         return;
72
73     cr = GraphicsImportSetGWorld(gi, gw, NULL);
74
75     natbounds.top = natbounds.bottom;
76     natbounds.bottom = 0;
77
78     cr = GraphicsImportSetBoundsRect(gi, &natbounds);
79
80     cr = GraphicsImportDraw(gi);
81
82     err = CloseComponent(gi);
83     if (err)
84         return;
85
86     DisposeGWorld(gw);
87
88     // Loop Through The Image Data
89     GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
90     GLuint temp; // Temporary Variable
91     GLuint bytesPerPixel; // Temporary Variable
92     bytesPerPixel = texture.bpp / 8;
93     imageSize = texture.sizeX * texture.sizeY * bytesPerPixel;
94
95     for ( GLuint i = 0; i < int( imageSize ); i += 4 ) {
96         // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
97         temp = texture.data[i]; // Temporarily Store The Value At Image Data 'i'
98         texture.data[i] = texture.data[i + 1]; // Set The 1st Byte To The Value Of The 3rd Byte
99         texture.data[i + 1] = texture.data[i + 2]; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
100         texture.data[i + 2] = texture.data[i + 3];
101         texture.data[i + 3] = temp;
102     }
103
104     if (!hasalpha) {
105         for ( GLuint i = 0; i < int( imageSize ); i += 4 ) {
106             texture.data[i + 3] = 255;
107         }
108     }
109
110     if (texdetail > 1) {
111         int which = 0;
112         float temp;
113         float howmany;
114         for ( GLuint k = 0; k < int( imageSize); k += bytesPerPixel * texture.sizeX * texdetail ) {
115             for ( GLuint i = 0; i < int( imageSize / texture.sizeY ); i += bytesPerPixel * texdetail ) {
116                 for ( GLuint b = 0; b < bytesPerPixel ; b ++ ) {
117                     temp = 0;
118                     howmany = 0;
119                     for ( GLuint l = 0; l < texdetail * texture.sizeX ; l += texture.sizeX ) {
120                         for ( GLuint j = 0; j < texdetail ; j ++ ) {
121                             temp += (int)texture.data[k + i + j * bytesPerPixel + l * bytesPerPixel + b]; // Set The 1st Byte To The Value Of The 3rd Byte
122                             howmany++;
123                         }
124                     }
125                     texture.data[which + b] = GLubyte(temp / howmany);
126                 }
127                 which += bytesPerPixel;
128             }
129         }
130         texture.sizeX /= texdetail;
131         texture.sizeY /= texdetail;
132     }
133
134     return true;
135
136 #endif
137 }