Debian Codegraph @

tools/quake3/q3map2/minimap.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 -------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* dependencies */
32 #include "q3map2.h"
33
34 /* minimap stuff */
35
36 typedef struct minimap_s
37 {
38 bspModel_t *model;
39 int width;
40 int height;
41 int samples;
42 float *sample_offsets;
43 float sharpen_boxmult;
44 float sharpen_centermult;
45 float boost, brightness, contrast;
46 float *data1f;
47 float *sharpendata1f;
48 vec3_t mins, size;
49 }
50 minimap_t;
51
52 static minimap_t minimap;
53
54 qboolean BrushIntersectionWithLine( bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out ){
55 int i;
56 qboolean in = qfalse, out = qfalse;
57 bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
58
59 for ( i = 0; i < brush->numSides; ++i )
60 {
61 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
62 float sn = DotProduct( start, p->normal );
63 float dn = DotProduct( dir, p->normal );
64 if ( dn == 0 ) {
65 if ( sn > p->dist ) {
66 return qfalse; // outside!
67 }
68 }
69 else
70 {
71 float t = ( p->dist - sn ) / dn;
72 if ( dn < 0 ) {
73 if ( !in || t > *t_in ) {
74 *t_in = t;
75 in = qtrue;
76 // as t_in can only increase, and t_out can only decrease, early out
77 if ( out && *t_in >= *t_out ) {
78 return qfalse;
79 }
80 }
81 }
82 else
83 {
84 if ( !out || t < *t_out ) {
85 *t_out = t;
86 out = qtrue;
87 // as t_in can only increase, and t_out can only decrease, early out
88 if ( in && *t_in >= *t_out ) {
89 return qfalse;
90 }
91 }
92 }
93 }
94 }
95 return in && out;
96 }
97
98 static float MiniMapSample( float x, float y ){
99 vec3_t org, dir;
100 int i, bi;
101 float t0, t1;
102 float samp;
103 bspBrush_t *b;
104 bspBrushSide_t *s;
105 // int cnt;
106
107 org[0] = x;
108 org[1] = y;
109 org[2] = 0;
110 dir[0] = 0;
111 dir[1] = 0;
112 dir[2] = 1;
113
114 // cnt = 0;
115 samp = 0;
116 for ( i = 0; i < minimap.model->numBSPBrushes; ++i )
117 {
118 bi = minimap.model->firstBSPBrush + i;
119 if ( opaqueBrushes[bi >> 3] & ( 1 << ( bi & 7 ) ) ) {
120 b = &bspBrushes[bi];
121
122 // sort out mins/maxs of the brush
123 s = &bspBrushSides[b->firstSide];
124 if ( x < -bspPlanes[s[0].planeNum].dist ) {
125 continue;
126 }
127 if ( x > +bspPlanes[s[1].planeNum].dist ) {
128 continue;
129 }
130 if ( y < -bspPlanes[s[2].planeNum].dist ) {
131 continue;
132 }
133 if ( y > +bspPlanes[s[3].planeNum].dist ) {
134 continue;
135 }
136
137 if ( BrushIntersectionWithLine( b, org, dir, &t0, &t1 ) ) {
138 samp += t1 - t0;
139 // ++cnt;
140 }
141 }
142 }
143
144 return samp;
145 }
146
147 void RandomVector2f( float v[2] ){
148 do
149 {
150 v[0] = 2 * Random() - 1;
151 v[1] = 2 * Random() - 1;
152 }
153 while ( v[0] * v[0] + v[1] * v[1] > 1 );
154 }
155
156 static void MiniMapRandomlySupersampled( int y ){
157 int x, i;
158 float *p = &minimap.data1f[y * minimap.width];
159 float ymin = minimap.mins[1] + minimap.size[1] * ( y / (float) minimap.height );
160 float dx = minimap.size[0] / (float) minimap.width;
161 float dy = minimap.size[1] / (float) minimap.height;
162 float uv[2];
163 float thisval;
164
165 for ( x = 0; x < minimap.width; ++x )
166 {
167 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
168 float val = 0;
169
170 for ( i = 0; i < minimap.samples; ++i )
171 {
172 RandomVector2f( uv );
173 thisval = MiniMapSample(
174 xmin + ( uv[0] + 0.5 ) * dx, /* exaggerated random pattern for better results */
175 ymin + ( uv[1] + 0.5 ) * dy /* exaggerated random pattern for better results */
176 );
177 val += thisval;
178 }
179 val /= minimap.samples * minimap.size[2];
180 *p++ = val;
181 }
182 }
183
184 static void MiniMapSupersampled( int y ){
185 int x, i;
186 float *p = &minimap.data1f[y * minimap.width];
187 float ymin = minimap.mins[1] + minimap.size[1] * ( y / (float) minimap.height );
188 float dx = minimap.size[0] / (float) minimap.width;
189 float dy = minimap.size[1] / (float) minimap.height;
190
191 for ( x = 0; x < minimap.width; ++x )
192 {
193 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
194 float val = 0;
195
196 for ( i = 0; i < minimap.samples; ++i )
197 {
198 float thisval = MiniMapSample(
199 xmin + minimap.sample_offsets[2 * i + 0] * dx,
200 ymin + minimap.sample_offsets[2 * i + 1] * dy
201 );
202 val += thisval;
203 }
204 val /= minimap.samples * minimap.size[2];
205 *p++ = val;
206 }
207 }
208
209 static void MiniMapNoSupersampling( int y ){
210 int x;
211 float *p = &minimap.data1f[y * minimap.width];
212 float ymin = minimap.mins[1] + minimap.size[1] * ( ( y + 0.5 ) / (float) minimap.height );
213
214 for ( x = 0; x < minimap.width; ++x )
215 {
216 float xmin = minimap.mins[0] + minimap.size[0] * ( ( x + 0.5 ) / (float) minimap.width );
217 *p++ = MiniMapSample( xmin, ymin ) / minimap.size[2];
218 }
219 }
220
221 static void MiniMapSharpen( int y ){
222 int x;
223 qboolean up = ( y > 0 );
224 qboolean down = ( y < minimap.height - 1 );
225 float *p = &minimap.data1f[y * minimap.width];
226 float *q = &minimap.sharpendata1f[y * minimap.width];
227
228 for ( x = 0; x < minimap.width; ++x )
229 {
230 qboolean left = ( x > 0 );
231 qboolean right = ( x < minimap.width - 1 );
232 float val = p[0] * minimap.sharpen_centermult;
233
234 if ( left && up ) {
235 val += p[-1 - minimap.width] * minimap.sharpen_boxmult;
236 }
237 if ( left && down ) {
238 val += p[-1 + minimap.width] * minimap.sharpen_boxmult;
239 }
240 if ( right && up ) {
241 val += p[+1 - minimap.width] * minimap.sharpen_boxmult;
242 }
243 if ( right && down ) {
244 val += p[+1 + minimap.width] * minimap.sharpen_boxmult;
245 }
246
247 if ( left ) {
248 val += p[-1] * minimap.sharpen_boxmult;
249 }
250 if ( right ) {
251 val += p[+1] * minimap.sharpen_boxmult;
252 }
253 if ( up ) {
254 val += p[-minimap.width] * minimap.sharpen_boxmult;
255 }
256 if ( down ) {
257 val += p[+minimap.width] * minimap.sharpen_boxmult;
258 }
259
260 ++p;
261 *q++ = val;
262 }
263 }
264
265 static void MiniMapContrastBoost( int y ){
266 int x;
267 float *q = &minimap.data1f[y * minimap.width];
268 for ( x = 0; x < minimap.width; ++x )
269 {
270 *q = *q * minimap.boost / ( ( minimap.boost - 1 ) * *q + 1 );
271 ++q;
272 }
273 }
274
275 static void MiniMapBrightnessContrast( int y ){
276 int x;
277 float *q = &minimap.data1f[y * minimap.width];
278 for ( x = 0; x < minimap.width; ++x )
279 {
280 *q = *q * minimap.contrast + minimap.brightness;
281 ++q;
282 }
283 }
284
285 // modify maxs and mins in place, copy them before calling this!
286 void MiniMapMakeMinsMaxs( vec3_t mins, vec3_t maxs, float border, qboolean keepaspect ){
287 vec3_t extend;
288
289 // line compatible to nexuiz mapinfo
290 Sys_Printf( "size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2] );
291
292 if ( keepaspect ) {
293 VectorSubtract( maxs, mins, extend );
294 if ( extend[1] > extend[0] ) {
295 mins[0] -= ( extend[1] - extend[0] ) * 0.5;
296 maxs[0] += ( extend[1] - extend[0] ) * 0.5;
297 }
298 else
299 {
300 mins[1] -= ( extend[0] - extend[1] ) * 0.5;
301 maxs[1] += ( extend[0] - extend[1] ) * 0.5;
302 }
303 }
304
305 /* border: amount of black area around the image */
306 /* input: border, 1-2*border, border but we need border/(1-2*border) */
307
308 VectorSubtract( maxs, mins, extend );
309 VectorScale( extend, border / ( 1 - 2 * border ), extend );
310
311 VectorSubtract( mins, extend, mins );
312 VectorAdd( maxs, extend, maxs );
313
314 VectorCopy( mins, minimap.mins );
315 VectorSubtract( maxs, mins, minimap.size );
316
317 // line compatible to nexuiz mapinfo
318 Sys_Printf( "size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2] );
319 }
320
321 /*
322 MiniMapSetupBrushes()
323 determines solid non-sky brushes in the world
324 */
325
326 void MiniMapSetupBrushes( void ){
327 SetupBrushesFlags( C_SOLID | C_SKY, C_SOLID, 0, 0 );
328 // at least one must be solid
329 // none may be sky
330 // not all may be nodraw
331 }
332
333 qboolean MiniMapEvaluateSampleOffsets( int *bestj, int *bestk, float *bestval ){
334 float val, dx, dy;
335 int j, k;
336
337 *bestj = *bestk = -1;
338 *bestval = 3; /* max possible val is 2 */
339
340 for ( j = 0; j < minimap.samples; ++j )
341 for ( k = j + 1; k < minimap.samples; ++k )
342 {
343 dx = minimap.sample_offsets[2 * j + 0] - minimap.sample_offsets[2 * k + 0];
344 dy = minimap.sample_offsets[2 * j + 1] - minimap.sample_offsets[2 * k + 1];
345 if ( dx > +0.5 ) {
346 dx -= 1;
347 }
348 if ( dx < -0.5 ) {
349 dx += 1;
350 }
351 if ( dy > +0.5 ) {
352 dy -= 1;
353 }
354 if ( dy < -0.5 ) {
355 dy += 1;
356 }
357 val = dx * dx + dy * dy;
358 if ( val < *bestval ) {
359 *bestj = j;
360 *bestk = k;
361 *bestval = val;
362 }
363 }
364
365 return *bestval < 3;
366 }
367
368 void MiniMapMakeSampleOffsets(){
369 int i, j, k, jj, kk;
370 float val, valj, valk, sx, sy, rx, ry;
371
372 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
373
374 /* start with entirely random samples */
375 for ( i = 0; i < minimap.samples; ++i )
376 {
377 minimap.sample_offsets[2 * i + 0] = Random();
378 minimap.sample_offsets[2 * i + 1] = Random();
379 }
380
381 for ( i = 0; i < 1000; ++i )
382 {
383 if ( MiniMapEvaluateSampleOffsets( &j, &k, &val ) ) {
384 sx = minimap.sample_offsets[2 * j + 0];
385 sy = minimap.sample_offsets[2 * j + 1];
386 minimap.sample_offsets[2 * j + 0] = rx = Random();
387 minimap.sample_offsets[2 * j + 1] = ry = Random();
388 if ( !MiniMapEvaluateSampleOffsets( &jj, &kk, &valj ) ) {
389 valj = -1;
390 }
391 minimap.sample_offsets[2 * j + 0] = sx;
392 minimap.sample_offsets[2 * j + 1] = sy;
393
394 sx = minimap.sample_offsets[2 * k + 0];
395 sy = minimap.sample_offsets[2 * k + 1];
396 minimap.sample_offsets[2 * k + 0] = rx;
397 minimap.sample_offsets[2 * k + 1] = ry;
398 if ( !MiniMapEvaluateSampleOffsets( &jj, &kk, &valk ) ) {
399 valk = -1;
400 }
401 minimap.sample_offsets[2 * k + 0] = sx;
402 minimap.sample_offsets[2 * k + 1] = sy;
403
404 if ( valj > valk ) {
405 if ( valj > val ) {
406 /* valj is the greatest */
407 minimap.sample_offsets[2 * j + 0] = rx;
408 minimap.sample_offsets[2 * j + 1] = ry;
409 i = -1;
410 }
411 else
412 {
413 /* valj is the greater and it is useless - forget it */
414 }
415 }
416 else
417 {
418 if ( valk > val ) {
419 /* valk is the greatest */
420 minimap.sample_offsets[2 * k + 0] = rx;
421 minimap.sample_offsets[2 * k + 1] = ry;
422 i = -1;
423 }
424 else
425 {
426 /* valk is the greater and it is useless - forget it */
427 }
428 }
429 }
430 else{
431 break;
432 }
433 }
434 }
435
436 void MergeRelativePath( char *out, const char *absolute, const char *relative ){
437 const char *endpos = absolute + strlen( absolute );
438 while ( endpos != absolute && ( endpos[-1] == '/' || endpos[-1] == '\\' ) )
439 --endpos;
440 while ( relative[0] == '.' && relative[1] == '.' && ( relative[2] == '/' || relative[2] == '\\' ) )
441 {
442 relative += 3;
443 while ( endpos != absolute )
444 {
445 --endpos;
446 if ( *endpos == '/' || *endpos == '\\' ) {
447 break;
448 }
449 }
450 while ( endpos != absolute && ( endpos[-1] == '/' || endpos[-1] == '\\' ) )
451 --endpos;
452 }
453 memcpy( out, absolute, endpos - absolute );
454 out[endpos - absolute] = '/';
455 strcpy( out + ( endpos - absolute + 1 ), relative );
456 }
457
458 int MiniMapBSPMain( int argc, char **argv ){
459 const size_t bufferLength = 1024;
460 char minimapFilename[ bufferLength ];
461 char basename[ bufferLength ];
462 char path[ bufferLength ];
463 char relativeMinimapFilename[ bufferLength ];
464 qboolean autolevel;
465 float minimapSharpen;
466 float border;
467 byte *data4b, *p;
468 float *q;
469 int x, y;
470 int i;
471 miniMapMode_t mode;
472 vec3_t mins, maxs;
473 qboolean keepaspect;
474
475 /* arg checking */
476 if ( argc < 2 ) {
477 Sys_Printf( "Usage: q3map [-v] -minimap [-size n] [-sharpen f] [-samples n | -random n] [-o filename.tga] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n" );
478 return 0;
479 }
480
481 /* load the BSP first */
482 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
483 StripExtension( source );
484 DefaultExtension( source, ".bsp" );
485 Sys_Printf( "Loading %s\n", source );
486 LoadShaderInfo();
487 LoadBSPFile( source );
488
489 minimap.model = &bspModels[0];
490 VectorCopy( minimap.model->mins, mins );
491 VectorCopy( minimap.model->maxs, maxs );
492
493 *minimapFilename = 0;
494 minimapSharpen = game->miniMapSharpen;
495 minimap.width = minimap.height = game->miniMapSize;
496 border = game->miniMapBorder;
497 keepaspect = game->miniMapKeepAspect;
498 mode = game->miniMapMode;
499
500 autolevel = qfalse;
501 minimap.samples = 1;
502 minimap.sample_offsets = NULL;
503 minimap.boost = 1.0;
504 minimap.brightness = 0.0;
505 minimap.contrast = 1.0;
506
507 /* process arguments */
508 for ( i = 1; i < ( argc - 1 ); i++ )
509 {
510 if ( !strcmp( argv[ i ], "-size" ) ) {
511 minimap.width = minimap.height = atoi( argv[i + 1] );
512 i++;
513 Sys_Printf( "Image size set to %i\n", minimap.width );
514 }
515 else if ( !strcmp( argv[ i ], "-sharpen" ) ) {
516 minimapSharpen = atof( argv[i + 1] );
517 i++;
518 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
519 }
520 else if ( !strcmp( argv[ i ], "-samples" ) ) {
521 minimap.samples = atoi( argv[i + 1] );
522 i++;
523 Sys_Printf( "Samples set to %i\n", minimap.samples );
524 if ( minimap.sample_offsets ) {
525 free( minimap.sample_offsets );
526 }
527 minimap.sample_offsets = malloc( 2 * sizeof( *minimap.sample_offsets ) * minimap.samples );
528 MiniMapMakeSampleOffsets();
529 }
530 else if ( !strcmp( argv[ i ], "-random" ) ) {
531 minimap.samples = atoi( argv[i + 1] );
532 i++;
533 Sys_Printf( "Random samples set to %i\n", minimap.samples );
534 if ( minimap.sample_offsets ) {
535 free( minimap.sample_offsets );
536 }
537 minimap.sample_offsets = NULL;
538 }
539 else if ( !strcmp( argv[ i ], "-border" ) ) {
540 border = atof( argv[i + 1] );
541 i++;
542 Sys_Printf( "Border set to %f\n", border );
543 }
544 else if ( !strcmp( argv[ i ], "-keepaspect" ) ) {
545 keepaspect = qtrue;
546 Sys_Printf( "Keeping aspect ratio by letterboxing\n", border );
547 }
548 else if ( !strcmp( argv[ i ], "-nokeepaspect" ) ) {
549 keepaspect = qfalse;
550 Sys_Printf( "Not keeping aspect ratio\n", border );
551 }
552 else if ( !strcmp( argv[ i ], "-o" ) ) {
553 strcpy( minimapFilename, argv[i + 1] );
554 i++;
555 Sys_Printf( "Output file name set to %s\n", minimapFilename );
556 }
557 else if ( !strcmp( argv[ i ], "-minmax" ) && i < ( argc - 7 ) ) {
558 mins[0] = atof( argv[i + 1] );
559 mins[1] = atof( argv[i + 2] );
560 mins[2] = atof( argv[i + 3] );
561 maxs[0] = atof( argv[i + 4] );
562 maxs[1] = atof( argv[i + 5] );
563 maxs[2] = atof( argv[i + 6] );
564 i += 6;
565 Sys_Printf( "Map mins/maxs overridden\n" );
566 }
567 else if ( !strcmp( argv[ i ], "-gray" ) ) {
568 mode = MINIMAP_MODE_GRAY;
569 Sys_Printf( "Writing as white-on-black image\n" );
570 }
571 else if ( !strcmp( argv[ i ], "-black" ) ) {
572 mode = MINIMAP_MODE_BLACK;
573 Sys_Printf( "Writing as black alpha image\n" );
574 }
575 else if ( !strcmp( argv[ i ], "-white" ) ) {
576 mode = MINIMAP_MODE_WHITE;
577 Sys_Printf( "Writing as white alpha image\n" );
578 }
579 else if ( !strcmp( argv[ i ], "-boost" ) && i < ( argc - 2 ) ) {
580 minimap.boost = atof( argv[i + 1] );
581 i++;
582 Sys_Printf( "Contrast boost set to %f\n", minimap.boost );
583 }
584 else if ( !strcmp( argv[ i ], "-brightness" ) && i < ( argc - 2 ) ) {
585 minimap.brightness = atof( argv[i + 1] );
586 i++;
587 Sys_Printf( "Brightness set to %f\n", minimap.brightness );
588 }
589 else if ( !strcmp( argv[ i ], "-contrast" ) && i < ( argc - 2 ) ) {
590 minimap.contrast = atof( argv[i + 1] );
591 i++;
592 Sys_Printf( "Contrast set to %f\n", minimap.contrast );
593 }
594 else if ( !strcmp( argv[ i ], "-autolevel" ) ) {
595 autolevel = qtrue;
596 Sys_Printf( "Auto level enabled\n", border );
597 }
598 else if ( !strcmp( argv[ i ], "-noautolevel" ) ) {
599 autolevel = qfalse;
600 Sys_Printf( "Auto level disabled\n", border );
601 }
602 }
603
604 vec3_t mins_out, maxs_out;
605 VectorCopy( mins, mins_out );
606 VectorCopy( maxs, maxs_out );
607 MiniMapMakeMinsMaxs( mins_out, maxs_out, border, keepaspect );
608
609 if ( !*minimapFilename ) {
610 ExtractFileBase( source, basename );
611 ExtractFilePath( source, path );
612 sprintf( relativeMinimapFilename, game->miniMapNameFormat, basename );
613 MergeRelativePath( minimapFilename, path, relativeMinimapFilename );
614 Sys_Printf( "Output file name automatically set to %s\n", minimapFilename );
615 }
616 ExtractFilePath( minimapFilename, path );
617 Q_mkdir( path );
618
619 if ( minimapSharpen >= 0 ) {
620 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
621 minimap.sharpen_boxmult = -minimapSharpen;
622 }
623
624 minimap.data1f = safe_malloc( minimap.width * minimap.height * sizeof( *minimap.data1f ) );
625 data4b = safe_malloc( minimap.width * minimap.height * 4 );
626 if ( minimapSharpen >= 0 ) {
627 minimap.sharpendata1f = safe_malloc( minimap.width * minimap.height * sizeof( *minimap.data1f ) );
628 }
629
630 MiniMapSetupBrushes();
631
632 if ( minimap.samples <= 1 ) {
633 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
634 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapNoSupersampling );
635 }
636 else
637 {
638 if ( minimap.sample_offsets ) {
639 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
640 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSupersampled );
641 }
642 else
643 {
644 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
645 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapRandomlySupersampled );
646 }
647 }
648
649 if ( minimap.boost != 1.0 ) {
650 Sys_Printf( "\n--- MiniMapContrastBoost (%d) ---\n", minimap.height );
651 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapContrastBoost );
652 }
653
654 if ( autolevel ) {
655 Sys_Printf( "\n--- MiniMapAutoLevel (%d) ---\n", minimap.height );
656 float mi = 1, ma = 0;
657 float s, o;
658
659 // TODO threads!
660 q = minimap.data1f;
661 for ( y = 0; y < minimap.height; ++y )
662 for ( x = 0; x < minimap.width; ++x )
663 {
664 float v = *q++;
665 if ( v < mi ) {
666 mi = v;
667 }
668 if ( v > ma ) {
669 ma = v;
670 }
671 }
672 if ( ma > mi ) {
673 s = 1 / ( ma - mi );
674 o = mi / ( ma - mi );
675
676 // equations:
677 // brightness + contrast * v
678 // after autolevel:
679 // brightness + contrast * (v * s - o)
680 // =
681 // (brightness - contrast * o) + (contrast * s) * v
682 minimap.brightness = minimap.brightness - minimap.contrast * o;
683 minimap.contrast *= s;
684
685 Sys_Printf( "Auto level: Brightness changed to %f\n", minimap.brightness );
686 Sys_Printf( "Auto level: Contrast changed to %f\n", minimap.contrast );
687 }
688 else{
689 Sys_Printf( "Auto level: failed because all pixels are the same value\n" );
690 }
691 }
692
693 if ( minimap.brightness != 0 || minimap.contrast != 1 ) {
694 Sys_Printf( "\n--- MiniMapBrightnessContrast (%d) ---\n", minimap.height );
695 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapBrightnessContrast );
696 }
697
698 if ( minimap.sharpendata1f ) {
699 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
700 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSharpen );
701 q = minimap.sharpendata1f;
702 }
703 else
704 {
705 q = minimap.data1f;
706 }
707
708 Sys_Printf( "\nConverting..." );
709
710 switch ( mode )
711 {
712 case MINIMAP_MODE_GRAY:
713 p = data4b;
714 for ( y = 0; y < minimap.height; ++y )
715 for ( x = 0; x < minimap.width; ++x )
716 {
717 byte b;
718 float v = *q++;
719 if ( v < 0 ) {
720 v = 0;
721 }
722 if ( v > 255.0 / 256.0 ) {
723 v = 255.0 / 256.0;
724 }
725 b = v * 256;
726 *p++ = b;
727 }
728 Sys_Printf( " writing to %s...", minimapFilename );
729 WriteTGAGray( minimapFilename, data4b, minimap.width, minimap.height );
730 break;
731 case MINIMAP_MODE_BLACK:
732 p = data4b;
733 for ( y = 0; y < minimap.height; ++y )
734 for ( x = 0; x < minimap.width; ++x )
735 {
736 byte b;
737 float v = *q++;
738 if ( v < 0 ) {
739 v = 0;
740 }
741 if ( v > 255.0 / 256.0 ) {
742 v = 255.0 / 256.0;
743 }
744 b = v * 256;
745 *p++ = 0;
746 *p++ = 0;
747 *p++ = 0;
748 *p++ = b;
749 }
750 Sys_Printf( " writing to %s...", minimapFilename );
751 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
752 break;
753 case MINIMAP_MODE_WHITE:
754 p = data4b;
755 for ( y = 0; y < minimap.height; ++y )
756 for ( x = 0; x < minimap.width; ++x )
757 {
758 byte b;
759 float v = *q++;
760 if ( v < 0 ) {
761 v = 0;
762 }
763 if ( v > 255.0 / 256.0 ) {
764 v = 255.0 / 256.0;
765 }
766 b = v * 256;
767 *p++ = 255;
768 *p++ = 255;
769 *p++ = 255;
770 *p++ = b;
771 }
772 Sys_Printf( " writing to %s...", minimapFilename );
773 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
774 break;
775 }
776
777 Sys_Printf( " done.\n" );
778
779 switch ( game->miniMapSidecarFormat )
780 {
781 case MINIMAP_SIDECAR_UNVANQUISHED:
782 {
783 char minimapBasename[ bufferLength ];
784 char minimapSidecarFilename[ bufferLength ];
785 const char *minimapSidecarExtension = ".minimap";
786 const size_t extensionLength = strlen( minimapSidecarExtension );
787 const size_t bufferWithoutExtensionLength = bufferLength - extensionLength;
788
789 strcpy( minimapBasename, minimapFilename );
790 StripExtension( minimapBasename );
791
792 snprintf( minimapSidecarFilename, bufferWithoutExtensionLength, "%s%s", minimapBasename, minimapSidecarExtension );
793
794 Sys_Printf( "Writing minimap sidecar to %s...", minimapSidecarFilename );
795
796 FILE *file = fopen( minimapSidecarFilename, "w" );
797 if ( file == NULL ) {
798 Sys_FPrintf( SYS_WRN, "WARNING: Unable to open minimap sidecar file %s for writing\n", minimapSidecarFilename );
799 break;
800 }
801
802 const char *minimapSidecarFormat =
803 "{\n"
804 "\tzone {\n"
805 "\t\tbounds %f %f %f %f %f %f\n"
806 "\t\timage \"minimaps/%s\" %f %f %f %f\n"
807 "\t}\n"
808 "}\n";
809
810 fprintf( file,
811 minimapSidecarFormat,
812 mins_out[0], mins_out[1], mins_out[2], maxs_out[0], maxs_out[1], maxs_out[2],
813 basename, mins_out[0], mins_out[1], maxs_out[0], maxs_out[1] );
814
815 fflush( file );
816 fclose( file );
817
818 Sys_Printf( " done.\n" );
819
820 break;
821 }
822 case MINIMAP_SIDECAR_NONE:
823 break;
824 }
825
826 /* return to sender */
827 return 0;
828 }