2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 byte *byteimage, *lbmpalette;
26 int byteimagewidth, byteimageheight;
28 qboolean TrueColorImage;
29 int longimagewidth, longimageheight;
31 char book_prefix[1024];
32 byte buffer[640 * 480];
33 unsigned long bufferl[640 * 480];
35 miptex_t *CreateBook8( byte *buffer, int w, int h, byte *palette, int *FinalSize ){
41 size = sizeof( *mp ) + ( w * h );
42 mp = (miptex_t *)SafeMalloc( size, "CreateBook8" );
43 memset( mp, 0, size );
45 mp->version = MIP_VERSION;
47 for ( i = j = 0; i < 256; i++,j += 3 )
49 mp->palette[i].r = palette[j];
50 mp->palette[i].g = palette[j + 1];
51 mp->palette[i].b = palette[j + 2];
53 pos = (byte *)( mp + 1 );
57 mp->offsets[0] = sizeof( *mp );
58 memcpy( pos, buffer, w * h );
64 miptex32_t *CreateBook32( long *buffer, int w, int h, int *FinalSize ){
69 size = sizeof( *mp ) + ( w * h * 4 );
70 mp = (miptex32_t *)SafeMalloc( size, "CreateBook32" );
71 memset( mp, 0, size );
73 mp->version = MIP32_VERSION;
75 pos = (byte *)( mp + 1 );
79 mp->offsets[0] = sizeof( *mp );
80 memcpy( pos, buffer, w * h * 4 );
87 // Routines to chop a random sized image into gl texture friendly chunks
96 int GetCoords( int x, int store[MAX_MD2SKINS] ){
97 int index, start, delta;
103 store[index++] = start;
108 store[index++] = start;
119 int ChopImage( int w, int h, rect_t coords[MAX_MD2SKINS] ){
120 int xs[MAX_MD2SKINS], ys[MAX_MD2SKINS];
121 int xcount, ycount, x, y, index;
124 xcount = GetCoords( w, xs ) - 1;
125 ycount = GetCoords( h, ys ) - 1;
127 for ( y = 0; y < ycount; y++ )
129 for ( x = 0; x < xcount; x++, index++ )
131 coords[index].x = xs[x];
132 coords[index].y = ys[y];
133 coords[index].w = xs[x + 1] - xs[x];
134 coords[index].h = ys[y + 1] - ys[y];
135 coords[index].name[0] = x + '0';
136 coords[index].name[1] = y + '0';
137 coords[index].name[2] = 0;
152 int flags, value, contents;
155 unsigned long *destl, *sourcel;
160 float scale_x, scale_y;
162 rect_t coords[MAX_MD2SKINS];
163 bookframe_t bframes[MAX_MD2SKINS];
167 GetScriptToken( false );
168 strcpy( lumpname, token );
170 GetScriptToken( false );
172 GetScriptToken( false );
174 GetScriptToken( false );
176 GetScriptToken( false );
183 if ( ( w & 7 ) || ( h & 7 ) ) {
184 Error( "line %i: miptex sizes must be multiples of 8", scriptline );
191 scale_x = scale_y = 0.5;
197 if ( TrueColorImage ) {
201 if ( xl >= longimagewidth || xh > longimagewidth ||
202 yl >= longimageheight || yh > longimageheight ) {
203 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight );
206 sourcel = (unsigned long *) longimage + ( yl * longimagewidth ) + xl;
207 destl = (unsigned long *) longimage;
208 linedelta = ( longimagewidth - w );
210 for ( y = yl; y < yh; y++ )
212 for ( x = xl; x < xh; x++ )
214 *destl++ = *sourcel++; // RGBA
216 sourcel += linedelta;
219 // Get rectangles to chop into
220 numrects = ChopImage( w, h, coords );
223 for ( i = 0; i < numrects; i++, bf++ )
225 // Copy section of image to buffer
226 sourcel = (unsigned long *) longimage + ( coords[i].y * w ) + coords[i].x;
228 linedelta = w - coords[i].w;
230 for ( y = 0; y < coords[i].h; y++ )
232 for ( x = 0; x < coords[i].w; x++ )
234 *destl++ = *sourcel++;
236 sourcel += linedelta;
239 qtex32 = CreateBook32( bufferl, coords[i].w, coords[i].h, &size );
241 qtex32->flags = flags;
242 qtex32->contents = contents;
243 qtex32->value = value;
244 qtex32->scale_x = scale_x;
245 qtex32->scale_y = scale_y;
247 sprintf( filename, "%sbook/%s/%s_%s.m32", gamedir, book_prefix, lumpname, coords[i].name );
248 sprintf( qtex32->name, "%s/%s_%s.m32", book_prefix, lumpname, coords[i].name );
250 strcpy( bf->name, qtex32->name );
258 printf( "writing %s\n", filename );
259 SaveFile( filename, (byte *)qtex32, size );
269 if ( xl >= byteimagewidth || xh > byteimagewidth ||
270 yl >= byteimageheight || yh > byteimageheight ) {
271 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight );
274 // Copy image to top left
275 source = byteimage + yl * byteimagewidth + xl;
277 linedelta = byteimagewidth - w;
279 for ( y = yl; y < yh; y++ )
281 for ( x = xl; x < xh; x++ )
288 // Get rectangles to chop into
289 numrects = ChopImage( w, h, coords );
292 for ( i = 0; i < numrects; i++, bf++ )
294 // Copy section of image to buffer
295 source = byteimage + ( coords[i].y * w ) + coords[i].x;
297 linedelta = w - coords[i].w;
299 for ( y = 0; y < coords[i].h; y++ )
301 for ( x = 0; x < coords[i].w; x++ )
308 qtex = CreateBook8( buffer, coords[i].w, coords[i].h, lbmpalette, &size );
311 qtex->contents = contents;
314 sprintf( filename, "%sbook/%s/%s_%s.m8", gamedir, book_prefix, lumpname, coords[i].name );
315 sprintf( qtex->name, "%s/%s_%s.m8", book_prefix, lumpname, coords[i].name );
317 strcpy( bf->name, qtex->name );
325 printf( "writing %s\n", filename );
326 SaveFile( filename, (byte *)qtex, size );
332 size = sizeof( bookframe_t ) * numrects;
334 book.bheader.ident = IDBOOKHEADER;
335 book.bheader.version = BOOK_VERSION;
336 book.bheader.num_segments = numrects;
337 book.bheader.total_w = w;
338 book.bheader.total_h = h;
339 memcpy( book.bframes, bframes, size );
341 // Save out segment descriptor
342 sprintf( filename, "%sBook/%s/%s.bk", gamedir, book_prefix, lumpname );
343 printf( "writing %s\n", filename );
344 SaveFile( filename, (byte *)&book, size + sizeof( bookheader_t ) );
352 void Cmd_Bookdir( void ){
355 GetScriptToken( false );
356 strcpy( book_prefix, token );
357 // create the directory if needed
358 sprintf( filename, "%sBook", gamedir );
360 sprintf( filename, "%sBook/%s", gamedir, book_prefix );