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)
42 size = sizeof(*mp) + (w * h);
43 mp = (miptex_t *)SafeMalloc(size, "CreateBook8");
46 mp->version = MIP_VERSION;
48 for(i=j=0;i<256;i++,j+=3)
50 mp->palette[i].r = palette[j];
51 mp->palette[i].g = palette[j+1];
52 mp->palette[i].b = palette[j+2];
54 pos = (byte *)(mp + 1);
58 mp->offsets[0] = sizeof(*mp);
59 memcpy(pos, buffer, w * h);
65 miptex32_t *CreateBook32(long *buffer, int w, int h, int *FinalSize)
71 size = sizeof(*mp) + (w * h * 4);
72 mp = (miptex32_t *)SafeMalloc(size, "CreateBook32");
75 mp->version = MIP32_VERSION;
77 pos = (byte *)(mp + 1);
81 mp->offsets[0] = sizeof(*mp);
82 memcpy(pos, buffer, w * h * 4);
89 // Routines to chop a random sized image into gl texture friendly chunks
98 int GetCoords(int x, int store[MAX_MD2SKINS])
100 int index, start, delta;
106 store[index++] = start;
112 store[index++] = start;
123 int ChopImage(int w, int h, rect_t coords[MAX_MD2SKINS])
125 int xs[MAX_MD2SKINS], ys[MAX_MD2SKINS];
126 int xcount, ycount, x, y, index;
129 xcount = GetCoords(w, xs) - 1;
130 ycount = GetCoords(h, ys) - 1;
132 for(y = 0; y < ycount; y++)
134 for(x = 0; x < xcount; x++, index++)
136 coords[index].x = xs[x];
137 coords[index].y = ys[y];
138 coords[index].w = xs[x + 1] - xs[x];
139 coords[index].h = ys[y + 1] - ys[y];
140 coords[index].name[0] = x + '0';
141 coords[index].name[1] = y + '0';
142 coords[index].name[2] = 0;
158 int flags, value, contents;
161 unsigned long *destl, *sourcel;
166 float scale_x, scale_y;
168 rect_t coords[MAX_MD2SKINS];
169 bookframe_t bframes[MAX_MD2SKINS];
173 GetScriptToken (false);
174 strcpy (lumpname, token);
176 GetScriptToken (false);
178 GetScriptToken (false);
180 GetScriptToken (false);
182 GetScriptToken (false);
189 if ( (w & 7) || (h & 7) )
190 Error ("line %i: miptex sizes must be multiples of 8", scriptline);
196 scale_x = scale_y = 0.5;
206 if (xl >= longimagewidth || xh > longimagewidth ||
207 yl >= longimageheight || yh > longimageheight)
209 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight);
212 sourcel = (unsigned long *) longimage + (yl * longimagewidth) + xl;
213 destl = (unsigned long *) longimage;
214 linedelta = (longimagewidth - w);
216 for(y = yl; y < yh; y++)
218 for(x = xl; x < xh; x++)
220 *destl++ = *sourcel++; // RGBA
222 sourcel += linedelta;
225 // Get rectangles to chop into
226 numrects = ChopImage(w, h, coords);
229 for(i = 0; i < numrects; i++, bf++)
231 // Copy section of image to buffer
232 sourcel = (unsigned long *) longimage + (coords[i].y * w) + coords[i].x;
234 linedelta = w - coords[i].w;
236 for(y = 0; y < coords[i].h; y++)
238 for(x = 0; x < coords[i].w; x++)
240 *destl++ = *sourcel++;
242 sourcel += linedelta;
245 qtex32 = CreateBook32(bufferl, coords[i].w, coords[i].h, &size);
247 qtex32->flags = flags;
248 qtex32->contents = contents;
249 qtex32->value = value;
250 qtex32->scale_x = scale_x;
251 qtex32->scale_y = scale_y;
253 sprintf (filename, "%sbook/%s/%s_%s.m32", gamedir, book_prefix, lumpname, coords[i].name);
254 sprintf (qtex32->name, "%s/%s_%s.m32", book_prefix, lumpname, coords[i].name);
256 strcpy(bf->name, qtex32->name);
264 printf ("writing %s\n", filename);
265 SaveFile (filename, (byte *)qtex32, size);
275 if (xl >= byteimagewidth || xh > byteimagewidth ||
276 yl >= byteimageheight || yh > byteimageheight)
278 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight);
281 // Copy image to top left
282 source = byteimage + yl*byteimagewidth + xl;
284 linedelta = byteimagewidth - w;
286 for(y = yl; y < yh; y++)
288 for(x = xl; x < xh; x++)
295 // Get rectangles to chop into
296 numrects = ChopImage(w, h, coords);
299 for(i = 0; i < numrects; i++, bf++)
301 // Copy section of image to buffer
302 source = byteimage + (coords[i].y * w) + coords[i].x;
304 linedelta = w - coords[i].w;
306 for(y = 0; y < coords[i].h; y++)
308 for(x = 0; x < coords[i].w; x++)
315 qtex = CreateBook8(buffer, coords[i].w, coords[i].h, lbmpalette, &size);
318 qtex->contents = contents;
321 sprintf (filename, "%sbook/%s/%s_%s.m8", gamedir, book_prefix, lumpname, coords[i].name);
322 sprintf (qtex->name, "%s/%s_%s.m8", book_prefix, lumpname, coords[i].name);
324 strcpy(bf->name, qtex->name);
332 printf ("writing %s\n", filename);
333 SaveFile (filename, (byte *)qtex, size);
339 size = sizeof(bookframe_t) * numrects;
341 book.bheader.ident = IDBOOKHEADER;
342 book.bheader.version = BOOK_VERSION;
343 book.bheader.num_segments = numrects;
344 book.bheader.total_w = w;
345 book.bheader.total_h = h;
346 memcpy(book.bframes, bframes, size);
348 // Save out segment descriptor
349 sprintf (filename, "%sBook/%s/%s.bk", gamedir, book_prefix, lumpname);
350 printf ("writing %s\n", filename);
351 SaveFile (filename, (byte *)&book, size + sizeof(bookheader_t));
359 void Cmd_Bookdir (void)
363 GetScriptToken (false);
364 strcpy (book_prefix, token);
365 // create the directory if needed
366 sprintf (filename, "%sBook", gamedir);
368 sprintf (filename, "%sBook/%s", gamedir, book_prefix);