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