]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/imagem8/m8.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / plugins / imagem8 / m8.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 #include <stdio.h>\r
23 #include <string.h>\r
24 #include <glib.h>\r
25 #include "m8.h"\r
26 \r
27 void LoadM8(const char *name, unsigned char **pic, int *width, int *height)\r
28 {\r
29     FILE        *f;\r
30     m8_header_t *m8_header;\r
31     rgb_t       *palette;\r
32     int         i, num_pixels, size;\r
33     char        text_buf[255] = { 0 };\r
34     char        *text_dot_pos;\r
35     unsigned int        length;\r
36     unsigned char       *palette_ent, *buf_temp;\r
37     unsigned char       *buffer, *m8_file_buffer;\r
38 \r
39     strcpy(text_buf, name);\r
40     text_dot_pos = strchr(text_buf, '.');\r
41     if (text_dot_pos)\r
42       *text_dot_pos = 0;\r
43     // Fix for .pcx.m8 extention\r
44     strcat(text_buf, ".pcx.m8");\r
45 \r
46     // open file\r
47     if ( length = vfsLoadFile ((char *) text_buf, (void **) &m8_file_buffer) == (unsigned int) -1)\r
48     {\r
49         strcpy(text_buf, name);\r
50         for(i=(strlen(text_buf)-1); i>0; i--)\r
51         {\r
52                 if(text_buf[i]=='.')\r
53                 {\r
54                         text_buf[i]=0;\r
55                         break;\r
56                 }\r
57         }\r
58         strcat(text_buf, ".m8");\r
59         if ( length = vfsLoadFile ((char *) text_buf, (void **) &m8_file_buffer) == (unsigned int) -1)\r
60         {\r
61                 Sys_Printf("Unable to open file %s\n",name);\r
62                 return;\r
63         }\r
64     }\r
65     \r
66     m8_header = (m8_header_t *)m8_file_buffer;\r
67 \r
68     // make sure we have a valid M8 file\r
69     if ( m8_header->version != M8_VERSION)\r
70     {\r
71             vfsFreeFile(m8_file_buffer);\r
72             Sys_Printf("Invalid M8 file %s\n", name);\r
73             return;\r
74     }\r
75 \r
76     // Get M8 Info\r
77     *width    = m8_header->width[0];            // Only interested in 1st MIP\r
78     *height   = m8_header->height[0];\r
79     num_pixels = (*width) * (*height);\r
80     size = num_pixels*4;\r
81 \r
82     // Allocate buffer\r
83     buf_temp = (unsigned char *)(g_malloc(size));\r
84     *pic = buf_temp;\r
85 \r
86     // Load Palette\r
87     palette = m8_header->palette;\r
88 \r
89     // Image data\r
90     buffer = m8_file_buffer + m8_header->offsets[0];\r
91 \r
92 \r
93     // Load texture into buffer\r
94     palette_ent = buffer;\r
95     for(i=0; i<num_pixels; i++)\r
96     {\r
97         *buf_temp++ = palette[*palette_ent].r;\r
98         *buf_temp++ = palette[*palette_ent].g;\r
99         *buf_temp++ = palette[*palette_ent].b;\r
100         *buf_temp++ = 255;              // No alpha\r
101         palette_ent++;\r
102     }\r
103 \r
104     Sys_Printf("Loaded file %s\n",text_buf);\r
105 \r
106     vfsFreeFile(m8_file_buffer);\r
107 }\r
108 \r