]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/imagem8/m8.cpp
uncrustify! now the code is only ugly on the *inside*
[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 //    FILE      *f;
29         m8_header_t *m8_header;
30         rgb_t   *palette;
31         int i, num_pixels, size;
32         char text_buf[255] = { 0 };
33         char    *text_dot_pos;
34         unsigned int length;
35         unsigned char   *palette_ent, *buf_temp;
36         unsigned char       *buffer, *m8_file_buffer;
37
38         strcpy( text_buf, name );
39         text_dot_pos = strchr( text_buf, '.' );
40         if ( text_dot_pos ) {
41                 *text_dot_pos = 0;
42         }
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, 0 ) ) == (unsigned int) -1 ) {
48                 strcpy( text_buf, name );
49                 for ( i = ( strlen( text_buf ) - 1 ); i > 0; i-- )
50                 {
51                         if ( text_buf[i] == '.' ) {
52                                 text_buf[i] = 0;
53                                 break;
54                         }
55                 }
56                 strcat( text_buf, ".m8" );
57                 if ( length = vfsLoadFile( (char *) text_buf, (void **) &m8_file_buffer, 0 ) == (unsigned int) -1 ) {
58                         Sys_Printf( "Unable to open file %s\n",name );
59                         return;
60                 }
61         }
62
63         m8_header = (m8_header_t *)m8_file_buffer;
64
65         // make sure we have a valid M8 file
66         if ( m8_header->version != M8_VERSION ) {
67                 vfsFreeFile( m8_file_buffer );
68                 Sys_Printf( "Invalid M8 file %s\n", name );
69                 return;
70         }
71
72         // Get M8 Info
73         *width    = m8_header->width[0];        // Only interested in 1st MIP
74         *height   = m8_header->height[0];
75         num_pixels = ( *width ) * ( *height );
76         size = num_pixels * 4;
77
78         // Allocate buffer
79         buf_temp = (unsigned char *)( g_malloc( size ) );
80         *pic = buf_temp;
81
82         // Load Palette
83         palette = m8_header->palette;
84
85         // Image data
86         buffer = m8_file_buffer + m8_header->offsets[0];
87
88
89         // Load texture into buffer
90         palette_ent = buffer;
91         for ( i = 0; i < num_pixels; i++ )
92         {
93                 *buf_temp++ = palette[*palette_ent].r;
94                 *buf_temp++ = palette[*palette_ent].g;
95                 *buf_temp++ = palette[*palette_ent].b;
96                 *buf_temp++ = 255;  // No alpha
97                 palette_ent++;
98         }
99
100         Sys_Printf( "Loaded file %s\n",text_buf );
101
102         vfsFreeFile( m8_file_buffer );
103 }