1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #error this file is not USED
|
---|
10 | #include "tconvert.hh"
|
---|
11 | #include "error/alert.hh"
|
---|
12 | #include "loaders/load.hh"
|
---|
13 | #include "math/point.hh"
|
---|
14 | #include "image/depth.hh"
|
---|
15 | #include "image/image8.hh"
|
---|
16 | #include "image/image32.hh"
|
---|
17 | #include "string/string.hh"
|
---|
18 | #include "error/alert.hh"
|
---|
19 | #include "g1_limits.hh"
|
---|
20 |
|
---|
21 | #ifndef G1_RETAIL
|
---|
22 | #include "memory/growarry.hh"
|
---|
23 | #include "quantize/histogram.hh"
|
---|
24 | #include "quantize/median.hh"
|
---|
25 | #include "loaders/fli_load.hh"
|
---|
26 | #include "loaders/bmp_write.hh"
|
---|
27 | #include "image/image2.hh"
|
---|
28 | #include "image/color.hh"
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | enum { MAX_MIPS=8 } ;
|
---|
32 |
|
---|
33 |
|
---|
34 | #ifndef G1_RETAIL
|
---|
35 | static i4_bool target_needs_update(const i4_const_str &source, const i4_const_str &target)
|
---|
36 | {
|
---|
37 | i4_file_status_struct stat_source, stat_target;
|
---|
38 | if (!i4_file_man.get_status(source,stat_source))
|
---|
39 | return i4_T;
|
---|
40 |
|
---|
41 | if (!i4_file_man.get_status(target,stat_target))
|
---|
42 | return i4_T;
|
---|
43 |
|
---|
44 | return (i4_bool) (stat_source.last_modified>stat_target.last_modified);
|
---|
45 | }
|
---|
46 | #endif
|
---|
47 |
|
---|
48 |
|
---|
49 | w32 g1_convert_pal_filename_to_id(const i4_const_str &name)
|
---|
50 | {
|
---|
51 | i4_const_str::iterator pal_end=name.begin();
|
---|
52 | w32 pal_file_id;
|
---|
53 |
|
---|
54 | pal_file_id= pal_end.get().value(); ++pal_end;
|
---|
55 | pal_file_id= (pal_file_id<<8) | pal_end.get().value(); ++pal_end;
|
---|
56 | pal_file_id= (pal_file_id<<8) | pal_end.get().value(); ++pal_end;
|
---|
57 | pal_file_id= (pal_file_id<<8) | pal_end.get().value(); ++pal_end;
|
---|
58 | ++pal_end;
|
---|
59 |
|
---|
60 | return pal_file_id;
|
---|
61 | }
|
---|
62 |
|
---|
63 | #ifndef G1_RETAIL
|
---|
64 |
|
---|
65 | static i4_image_class *load_if_valid(i4_const_str &fname,
|
---|
66 | i4_grow_array<w32> &used,
|
---|
67 | i4_bool new_only,
|
---|
68 | w32 &match_id)
|
---|
69 | {
|
---|
70 | i4_str *path, *filename, *extension;
|
---|
71 | i4_image_class *ret=0;
|
---|
72 |
|
---|
73 | i4_file_man.split_path(fname, path, filename, extension);
|
---|
74 | if (path) delete path;
|
---|
75 |
|
---|
76 | if (extension && *extension==i4gets("tga_ext"))
|
---|
77 | {
|
---|
78 | w32 id=g1_convert_pal_filename_to_id(*filename);
|
---|
79 | i4_bool ok=i4_T;
|
---|
80 |
|
---|
81 | if (new_only)
|
---|
82 | {
|
---|
83 | for (w32 j=0; j<used.size(); j++)
|
---|
84 | if (used[j]==id)
|
---|
85 | ok=i4_F;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | if (match_id != id)
|
---|
89 | ok=i4_F;
|
---|
90 |
|
---|
91 |
|
---|
92 | if (ok)
|
---|
93 | {
|
---|
94 | i4_alert(i4gets("best_pal"),100,filename);
|
---|
95 |
|
---|
96 | match_id=id;
|
---|
97 | i4_str *open_name=i4gets("default_tga").sprintf(100, filename, &i4gets("tga_ext"));
|
---|
98 | i4_file_class *fp=i4_file_man.open(*open_name);
|
---|
99 | delete open_name;
|
---|
100 |
|
---|
101 | if (fp)
|
---|
102 | {
|
---|
103 | ret=i4_load_image(fp);
|
---|
104 | if (ret)
|
---|
105 | used.add(id);
|
---|
106 |
|
---|
107 | delete fp;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | if (filename) delete filename;
|
---|
112 | if (extension) delete extension;
|
---|
113 |
|
---|
114 | return ret;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | static i4_str *get_bmp_name(const i4_const_str &name)
|
---|
119 | {
|
---|
120 | i4_str *path, *filename, *ext;
|
---|
121 | i4_file_man.split_path(name, path,filename,ext);
|
---|
122 |
|
---|
123 | i4_const_str::iterator pal_end=filename->begin();
|
---|
124 | for (w32 j=0; j<5; j++)
|
---|
125 | ++pal_end;
|
---|
126 | i4_str *pal_name=new i4_str(filename->begin(),pal_end,5);
|
---|
127 |
|
---|
128 | if (path) delete path;
|
---|
129 | delete filename;
|
---|
130 | if (ext) delete ext;
|
---|
131 |
|
---|
132 | i4_str *source=i4gets("pal_source_name").sprintf(100,pal_name);
|
---|
133 | return source;
|
---|
134 | }
|
---|
135 |
|
---|
136 | void g1_create_best_palettes()
|
---|
137 | {
|
---|
138 | i4_str **files, **dirs;
|
---|
139 | w32 tfiles, tdirs, i,j;
|
---|
140 | i4_image_class *im;
|
---|
141 |
|
---|
142 | i4_grow_array<w32> calced(128,"calced pals",64);
|
---|
143 |
|
---|
144 | if (i4_file_man.get_directory(i4gets("default_tga_dir"), files, tfiles, dirs, tdirs))
|
---|
145 | {
|
---|
146 | for (i=0; i<tfiles; i++)
|
---|
147 | {
|
---|
148 | i4_histogram_class hist;
|
---|
149 |
|
---|
150 | w32 id;
|
---|
151 | im=load_if_valid(*files[i], calced, i4_T, id);
|
---|
152 | if (im)
|
---|
153 | {
|
---|
154 | j=0;
|
---|
155 |
|
---|
156 | while (im)
|
---|
157 | {
|
---|
158 | hist.add_image_colors(im, 1);
|
---|
159 | delete im;
|
---|
160 | im=0;
|
---|
161 | while (j<tfiles && !im)
|
---|
162 | {
|
---|
163 | if (j!=i)
|
---|
164 | im=load_if_valid(*files[j], calced, i4_F, id);
|
---|
165 | j++;
|
---|
166 |
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | i4_pal_handle_class pal=i4_median_cut(&hist, 1);
|
---|
171 |
|
---|
172 | i4_unmatched_image8 *i8=new i4_unmatched_image8(256,1,pal);
|
---|
173 | i4_unmatched_image8::iterator it=i8->create_iterator(0,0);
|
---|
174 | for (w32 x=0; x<256; x++)
|
---|
175 | {
|
---|
176 | i8->iterator_store(it, x);
|
---|
177 | ++it;
|
---|
178 | }
|
---|
179 |
|
---|
180 | i4_file_man.mkdir(i4gets("texture_dir"));
|
---|
181 | i4_file_man.mkdir(i4gets("pal_dir"));
|
---|
182 |
|
---|
183 | i4_str *target=get_bmp_name(*files[i]);
|
---|
184 | i4_file_class *out=i4_file_man.open(*target,i4_file_manager_class::WRITE);
|
---|
185 | delete target;
|
---|
186 |
|
---|
187 | if (out)
|
---|
188 | {
|
---|
189 | i4_write_bmp(i8, out);
|
---|
190 | delete out;
|
---|
191 | }
|
---|
192 |
|
---|
193 | delete i8;
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 |
|
---|
199 | for (i=0; i<tfiles; i++)
|
---|
200 | delete files[i];
|
---|
201 |
|
---|
202 | for (i=0; i<tdirs; i++)
|
---|
203 | delete dirs[i];
|
---|
204 |
|
---|
205 | i4_free(files);
|
---|
206 | i4_free(dirs);
|
---|
207 | }
|
---|
208 |
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | static void null_pal_handle(i4_pal_handle_class &pal)
|
---|
213 | {
|
---|
214 | w32 data[256];
|
---|
215 | for (w32 i=0; i<256; i++)
|
---|
216 | data[i]=(i<<16)|(i<<8)|i;
|
---|
217 |
|
---|
218 | i4_pixel_format fmt;
|
---|
219 | fmt.pixel_depth=I4_8BIT;
|
---|
220 | fmt.lookup=data;
|
---|
221 |
|
---|
222 | pal=i4_pal_man.register_pal(&fmt);
|
---|
223 | }
|
---|
224 |
|
---|
225 | static void save_pal(const i4_const_str &source,
|
---|
226 | const i4_const_str &target,
|
---|
227 | i4_pal_handle_class &pal)
|
---|
228 |
|
---|
229 | {
|
---|
230 | i4_file_class *fp=i4_file_man.open(source);
|
---|
231 | if (!fp)
|
---|
232 | null_pal_handle(pal);
|
---|
233 | else
|
---|
234 | {
|
---|
235 | i4_image_class *im=i4_load_image(fp);
|
---|
236 | if (!im)
|
---|
237 | {
|
---|
238 | delete fp;
|
---|
239 | null_pal_handle(pal);
|
---|
240 | return;
|
---|
241 | }
|
---|
242 | i4_histogram_class *hist=new i4_histogram_class;
|
---|
243 | hist->add_image_colors(im, 1);
|
---|
244 | pal=i4_median_cut(hist, 1);
|
---|
245 |
|
---|
246 | i4_unmatched_image8 *i8=new i4_unmatched_image8(256,1,pal);
|
---|
247 | i4_unmatched_image8::iterator i=i8->create_iterator(0,0);
|
---|
248 | for (w32 x=0; x<256; x++)
|
---|
249 | {
|
---|
250 | i8->iterator_store(i, x);
|
---|
251 | ++i;
|
---|
252 | }
|
---|
253 |
|
---|
254 | i4_file_man.mkdir(i4gets("texture_dir"));
|
---|
255 | i4_file_man.mkdir(i4gets("pal_dir"));
|
---|
256 |
|
---|
257 | i4_file_class *out=i4_file_man.open(target,i4_file_manager_class::WRITE);
|
---|
258 | if (!out)
|
---|
259 | {
|
---|
260 | delete hist;
|
---|
261 | delete im;
|
---|
262 | delete fp;
|
---|
263 | null_pal_handle(pal);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | i4_write_bmp(i8, out);
|
---|
268 | delete out;
|
---|
269 | delete fp;
|
---|
270 | delete i8;
|
---|
271 |
|
---|
272 | delete hist;
|
---|
273 | delete im;
|
---|
274 | }
|
---|
275 |
|
---|
276 | }
|
---|
277 |
|
---|
278 | #else
|
---|
279 |
|
---|
280 | void g1_create_best_palettes() { ; }
|
---|
281 |
|
---|
282 | #endif
|
---|
283 |
|
---|
284 |
|
---|
285 | static void load_palette(const i4_const_str &name,
|
---|
286 | const i4_const_str &original_name,
|
---|
287 | w32 *buffer)
|
---|
288 | {
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 | w32 pal_file_id=g1_convert_pal_filename_to_id(name);
|
---|
293 |
|
---|
294 | i4_str *target=i4gets("pal_target_name").sprintf(100,
|
---|
295 | (pal_file_id>>24)&0xff,
|
---|
296 | (pal_file_id>>16)&0xff,
|
---|
297 | (pal_file_id>>8)&0xff,
|
---|
298 | (pal_file_id)&0xff
|
---|
299 | );
|
---|
300 |
|
---|
301 | #ifndef G1_RETAIL
|
---|
302 | i4_str *source=get_bmp_name(name);
|
---|
303 | if (target_needs_update(*source,*target))
|
---|
304 | {
|
---|
305 | i4_image_class *im=i4_load_image(*source);
|
---|
306 | i4_pal_handle_class pal;
|
---|
307 |
|
---|
308 | if (!im)
|
---|
309 | {
|
---|
310 | i4_alert(i4gets("pal_file_missing"),120,source,&name);
|
---|
311 | save_pal(original_name, *source, pal);
|
---|
312 | }
|
---|
313 | else
|
---|
314 | {
|
---|
315 | pal=im->get_pal();
|
---|
316 | delete im;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | if (pal.source_type()!=I4_8BIT)
|
---|
321 | i4_error("palette file is not 8 bit");
|
---|
322 |
|
---|
323 |
|
---|
324 | i4_file_class *fp=i4_file_man.open(*target,i4_file_manager_class::WRITE);
|
---|
325 | if (!fp)
|
---|
326 | i4_error("unable to open palette output file");
|
---|
327 |
|
---|
328 | w32 *pdata=pal.pal->source.lookup;
|
---|
329 | for (w32 i=0; i<256; i++)
|
---|
330 | fp->write_32(pdata[i]);
|
---|
331 | delete fp;
|
---|
332 | }
|
---|
333 |
|
---|
334 | delete source;
|
---|
335 | #endif
|
---|
336 |
|
---|
337 | i4_file_class *fp=i4_open(*target);
|
---|
338 | if (!fp)
|
---|
339 | {
|
---|
340 | i4_alert(i4gets("file_missing"),100,target);
|
---|
341 | i4_error("bye");
|
---|
342 | }
|
---|
343 |
|
---|
344 | for (w32 i=0; i<256; i++)
|
---|
345 | buffer[i]=fp->read_32();
|
---|
346 |
|
---|
347 | delete fp;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | #ifndef G1_RETAIL
|
---|
352 |
|
---|
353 | // this function will average 4 rgb packed colors and return the new rgb color
|
---|
354 | static inline i4_color average_4_32s(i4_color c1,
|
---|
355 | i4_color c2,
|
---|
356 | i4_color c3,
|
---|
357 | i4_color c4)
|
---|
358 | {
|
---|
359 | enum { r_mask=0xff0000,
|
---|
360 | g_mask=0xff00,
|
---|
361 | b_mask=0xff,
|
---|
362 | r_shift=16,
|
---|
363 | g_shift=8,
|
---|
364 | b_shift=0 };
|
---|
365 |
|
---|
366 |
|
---|
367 | w32 red=(((c1&r_mask)+
|
---|
368 | (c2&r_mask)+
|
---|
369 | (c3&r_mask)+
|
---|
370 | (c4&r_mask))>>r_shift)/4;
|
---|
371 |
|
---|
372 | w32 green=(((c1&g_mask)+
|
---|
373 | (c2&g_mask)+
|
---|
374 | (c3&g_mask)+
|
---|
375 | (c4&g_mask))>>g_shift)/4;
|
---|
376 |
|
---|
377 | w32 blue=(((c1&b_mask)+
|
---|
378 | (c2&b_mask)+
|
---|
379 | (c3&b_mask)+
|
---|
380 | (c4&b_mask))>>b_shift)/4;
|
---|
381 |
|
---|
382 | return (red<<r_shift)|(green<<g_shift)|(blue<<b_shift);
|
---|
383 | }
|
---|
384 |
|
---|
385 | // this will create a half sized image where every pixel is the averge of two of it's parents
|
---|
386 | // pixels
|
---|
387 | void mip_scale(i4_image32 *im, i4_image2 *holy, i4_image32 *&ret_32, i4_image2 *&ret_2)
|
---|
388 | {
|
---|
389 | i4_image32 *small=new i4_image32(im->width()/2,
|
---|
390 | im->height()/2,
|
---|
391 | im->get_pal());
|
---|
392 | i4_image2 *small_holy=0;
|
---|
393 | i4_image2::iterator hsource1, hsource2, hdest;
|
---|
394 | i4_image32::iterator source1, source2, dest;
|
---|
395 |
|
---|
396 | if (holy)
|
---|
397 | small_holy = new i4_image2(holy->width()/2,
|
---|
398 | holy->height()/2,
|
---|
399 | holy->get_pal());
|
---|
400 |
|
---|
401 |
|
---|
402 | w32 xcounter,ycounter,
|
---|
403 | end_skip=(im->width()&1); // in case the image width is odd we need to skip a byte
|
---|
404 |
|
---|
405 | source1=im->create_iterator(0,0);
|
---|
406 | source2=im->create_iterator(0,1);
|
---|
407 | dest=small->create_iterator(0,0);
|
---|
408 |
|
---|
409 | if (holy)
|
---|
410 | {
|
---|
411 | hsource1=holy->create_iterator(0,0);
|
---|
412 | hsource2=holy->create_iterator(0,1);
|
---|
413 | hdest=small_holy->create_iterator(0,0);
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | i4_color c1,c2,c3,c4, r,g,b;
|
---|
418 | sw32 total_pixels;
|
---|
419 |
|
---|
420 | for (ycounter=small->height(); ycounter; ycounter--)
|
---|
421 | {
|
---|
422 | for (xcounter=small->width(); xcounter; xcounter--)
|
---|
423 | {
|
---|
424 | c1=im->iterator_get(source1);
|
---|
425 | ++source1;
|
---|
426 | c2=im->iterator_get(source1);
|
---|
427 | ++source1;
|
---|
428 |
|
---|
429 | c3=im->iterator_get(source2);
|
---|
430 | ++source2;
|
---|
431 | c4=im->iterator_get(source2);
|
---|
432 | ++source2;
|
---|
433 |
|
---|
434 |
|
---|
435 | if (holy)
|
---|
436 | {
|
---|
437 | total_pixels=0;
|
---|
438 | r=g=b=0;
|
---|
439 |
|
---|
440 | if (holy->iterator_get(hsource1))
|
---|
441 | { total_pixels++; r+=(c1&0xff0000)>>16; g+=(c1&0xff00)>>8; b+=(c1&0xff)>>0; }
|
---|
442 | ++hsource1;
|
---|
443 |
|
---|
444 | if (holy->iterator_get(hsource1))
|
---|
445 | { total_pixels++; r+=(c2&0xff0000)>>16; g+=(c2&0xff00)>>8; b+=(c2&0xff)>>0; }
|
---|
446 | ++hsource1;
|
---|
447 |
|
---|
448 | if (holy->iterator_get(hsource2))
|
---|
449 | { total_pixels++; r+=(c3&0xff0000)>>16; g+=(c3&0xff00)>>8; b+=(c3&0xff)>>0; }
|
---|
450 | ++hsource2;
|
---|
451 |
|
---|
452 | if (holy->iterator_get(hsource2))
|
---|
453 | { total_pixels++; r+=(c4&0xff0000)>>16; g+=(c4&0xff00)>>8; b+=(c4&0xff)>>0; }
|
---|
454 | ++hsource2;
|
---|
455 |
|
---|
456 | if (total_pixels > (rand()%4))
|
---|
457 | {
|
---|
458 | small->iterator_store(dest, i4_rgb_to_32bit(r/total_pixels,
|
---|
459 | g/total_pixels,
|
---|
460 | b/total_pixels));
|
---|
461 |
|
---|
462 | small_holy->iterator_store(hdest, 1);
|
---|
463 | }
|
---|
464 | else
|
---|
465 | {
|
---|
466 | small->iterator_store(dest, 0);
|
---|
467 | small_holy->iterator_store(hdest, 0);
|
---|
468 | }
|
---|
469 |
|
---|
470 | ++hdest;
|
---|
471 | ++dest;
|
---|
472 | }
|
---|
473 | else
|
---|
474 | {
|
---|
475 | small->iterator_store(dest,average_4_32s(c1,c2,c3,c4));
|
---|
476 | ++dest;
|
---|
477 | }
|
---|
478 | }
|
---|
479 | if (holy)
|
---|
480 | {
|
---|
481 | hsource1+=end_skip+im->width();
|
---|
482 | hsource2+=end_skip+im->width();
|
---|
483 | }
|
---|
484 |
|
---|
485 | source1+=end_skip+im->width();
|
---|
486 | source2+=end_skip+im->width();
|
---|
487 |
|
---|
488 | }
|
---|
489 |
|
---|
490 | ret_32=small;
|
---|
491 | ret_2=small_holy;
|
---|
492 |
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | static i4_image32 *convert_8_to_32(i4_image_class *image8)
|
---|
497 | {
|
---|
498 | i4_pal_handle_class pal;
|
---|
499 | i4_unmatched_image8 *im8=(i4_unmatched_image8 *)image8;
|
---|
500 |
|
---|
501 | i4_image32 *new32=new i4_image32(im8->width(),
|
---|
502 | im8->height(),
|
---|
503 | pal);
|
---|
504 | i4_image32::iterator i32=new32->create_iterator(0,0);
|
---|
505 | i4_unmatched_image8::iterator i8=im8->create_iterator(0,0);
|
---|
506 |
|
---|
507 | w32 *pal_data=image8->get_pal().pal->source.lookup;
|
---|
508 |
|
---|
509 | w32 i,t=im8->width()*im8->height();
|
---|
510 | for (i=0; i<t; i++)
|
---|
511 | {
|
---|
512 | new32->iterator_store(i32, pal_data[ im8->iterator_get(i8) ]);
|
---|
513 | ++i32;
|
---|
514 | ++i8;
|
---|
515 | }
|
---|
516 |
|
---|
517 | return new32;
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | void clear_holes(i4_unmatched_image8 *im8,
|
---|
522 | i4_image2 *im2)
|
---|
523 | {
|
---|
524 | i4_unmatched_image8::iterator i8=im8->create_iterator(0,0);
|
---|
525 | i4_image2::iterator i2=im2->create_iterator(0,0);
|
---|
526 |
|
---|
527 | w32 t=im8->width() * im8->height(), i;
|
---|
528 |
|
---|
529 | for (i=0; i<t; i++)
|
---|
530 | {
|
---|
531 | if (!im2->iterator_get(i2))
|
---|
532 | im8->iterator_store(i8, 0);
|
---|
533 | ++i8;
|
---|
534 | ++i2;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | i4_image2 *get_holes(i4_unmatched_image8 *im8)
|
---|
539 | {
|
---|
540 | w32 pal_d[2]= {0, 0xffffffff};
|
---|
541 | i4_pixel_format fmt;
|
---|
542 | fmt.pixel_depth=I4_2BIT;
|
---|
543 | fmt.lookup=pal_d;
|
---|
544 |
|
---|
545 | i4_pal_handle_class pal=i4_pal_man.register_pal(&fmt);
|
---|
546 |
|
---|
547 | w32 t=im8->width() * im8->height(), i;
|
---|
548 |
|
---|
549 | i4_image2 *im2=new i4_image2(im8->width(), im8->height(), pal);
|
---|
550 |
|
---|
551 | i4_unmatched_image8::iterator i8=im8->create_iterator(0,0);
|
---|
552 | i4_image2::iterator i2=im2->create_iterator(0,0);
|
---|
553 |
|
---|
554 | w32 *pal_data=im8->get_pal().pal->source.lookup;
|
---|
555 |
|
---|
556 | w32 index,red,green,blue,rgbcolor;
|
---|
557 | i4_bool transparent;
|
---|
558 |
|
---|
559 | w32 r_mask = 0x00ff0000;
|
---|
560 | w32 g_mask = 0x0000ff00;
|
---|
561 | w32 b_mask = 0x000000ff;
|
---|
562 | w32 r_shift = 16;
|
---|
563 | w32 g_shift = 8;
|
---|
564 | w32 b_shift = 0;
|
---|
565 |
|
---|
566 | for (i=0; i<t; i++)
|
---|
567 | {
|
---|
568 | index = im8->iterator_get(i8);
|
---|
569 |
|
---|
570 | rgbcolor = pal_data[index];
|
---|
571 |
|
---|
572 | red = (rgbcolor & r_mask) >> r_shift;
|
---|
573 | green = (rgbcolor & g_mask) >> g_shift;
|
---|
574 | blue = (rgbcolor & b_mask) >> b_shift;
|
---|
575 |
|
---|
576 | transparent = (red==254 && green==2 && blue==166);
|
---|
577 |
|
---|
578 | if (!transparent)
|
---|
579 | im2->iterator_store(i2, 1);
|
---|
580 | else
|
---|
581 | im2->iterator_store(i2, 0);
|
---|
582 | ++i8;
|
---|
583 | ++i2;
|
---|
584 | }
|
---|
585 | return im2;
|
---|
586 | }
|
---|
587 |
|
---|
588 | static i4_bool save_texture(i4_image32 *im,
|
---|
589 | i4_image2 *holy_patern,
|
---|
590 | const i4_pal_handle_class &pal,
|
---|
591 | w32 pal_id,
|
---|
592 | const i4_const_str &filename)
|
---|
593 | {
|
---|
594 | i4_image32 *mip_levels[MAX_MIPS];
|
---|
595 | i4_image2 *holy_levels[MAX_MIPS];
|
---|
596 |
|
---|
597 | sw32 i,t_mips=0,w,h;
|
---|
598 |
|
---|
599 | i4_file_class *fp=i4_file_man.open(filename, i4_file_manager_class::WRITE);
|
---|
600 | if (!fp) return i4_F;
|
---|
601 | w=im->width();
|
---|
602 | h=im->height();
|
---|
603 |
|
---|
604 | mip_levels[0]=im;
|
---|
605 | holy_levels[0]=holy_patern;
|
---|
606 |
|
---|
607 | for (i=1; i<MAX_MIPS; i++)
|
---|
608 | {
|
---|
609 | // if the texture is too small, don't create anymore mip map levels
|
---|
610 | if (mip_levels[i-1]->width()<4 ||
|
---|
611 | mip_levels[i-1]->height()<2)
|
---|
612 | break;
|
---|
613 |
|
---|
614 | mip_scale(mip_levels[i-1],
|
---|
615 | holy_levels[i-1],
|
---|
616 | mip_levels[i],
|
---|
617 | holy_levels[i]);
|
---|
618 | t_mips++;
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | fp->write_8(G1_TEXTURE_FILE);
|
---|
623 | fp->write_32(pal_id);
|
---|
624 | fp->write_16(mip_levels[t_mips-1]->width());
|
---|
625 | fp->write_16(mip_levels[t_mips-1]->height());
|
---|
626 |
|
---|
627 |
|
---|
628 | for (i=t_mips-1; i>=0; i--)
|
---|
629 | {
|
---|
630 | i4_unmatched_image8 *i8=mip_levels[i]->quantize(pal,1,
|
---|
631 | 0,0,
|
---|
632 | mip_levels[i]->width(),
|
---|
633 | mip_levels[i]->height());
|
---|
634 | if (holy_patern)
|
---|
635 | clear_holes(i8, holy_levels[i]);
|
---|
636 |
|
---|
637 |
|
---|
638 | if (i==0)
|
---|
639 | {
|
---|
640 | fp->write_16(0);
|
---|
641 | fp->write_16(0);
|
---|
642 | }
|
---|
643 | else
|
---|
644 | {
|
---|
645 | fp->write_16(mip_levels[i-1]->width());
|
---|
646 | fp->write_16(mip_levels[i-1]->height());
|
---|
647 | }
|
---|
648 |
|
---|
649 | fp->write(i8->local_sub_data(0,0),
|
---|
650 | i8->width() * i8->height());
|
---|
651 |
|
---|
652 | delete i8;
|
---|
653 | delete mip_levels[i];
|
---|
654 | if (holy_patern)
|
---|
655 | delete holy_levels[i];
|
---|
656 | }
|
---|
657 |
|
---|
658 | delete fp;
|
---|
659 |
|
---|
660 | return i4_T;
|
---|
661 | }
|
---|
662 | #endif
|
---|
663 |
|
---|
664 | g1_texture_handle convert_tga(const i4_const_str &original_name,
|
---|
665 | const i4_const_str &basename,
|
---|
666 | g1_texture_mapper_class &tmapper)
|
---|
667 | {
|
---|
668 | i4_str *target=i4gets("text_name").sprintf(100, &basename);
|
---|
669 |
|
---|
670 | #ifndef G1_RETAIL
|
---|
671 | // see if we need to update the texture
|
---|
672 | if (target_needs_update(original_name, *target))
|
---|
673 | {
|
---|
674 | i4_alert(i4gets("updating_texture"),100,&original_name);
|
---|
675 | i4_image_class *im=i4_load_image(original_name);
|
---|
676 | if (!im)
|
---|
677 | {
|
---|
678 | i4_alert(i4gets("file_missing"), 100, &original_name);
|
---|
679 | delete target;
|
---|
680 | return 0;
|
---|
681 | }
|
---|
682 | else if (im->get_pal().source_type()!=I4_32BIT)
|
---|
683 | {
|
---|
684 | i4_alert(i4gets("not_32"), 100, &original_name);
|
---|
685 | delete target;
|
---|
686 | return 0;
|
---|
687 | }
|
---|
688 | else
|
---|
689 | {
|
---|
690 | w32 buf[256];
|
---|
691 | load_palette(basename, original_name, buf);
|
---|
692 | w32 pal_id=g1_convert_pal_filename_to_id(basename);
|
---|
693 |
|
---|
694 | i4_pixel_format fmt;
|
---|
695 | fmt.pixel_depth = I4_8BIT;
|
---|
696 | fmt.lookup = buf;
|
---|
697 |
|
---|
698 | i4_pal_handle_class pal=i4_pal_man.register_pal(&fmt);
|
---|
699 | save_texture((i4_image32 *)im, 0, pal, pal_id, *target);
|
---|
700 | }
|
---|
701 | }
|
---|
702 | #endif
|
---|
703 |
|
---|
704 | g1_file_handle han=g1_file_man.get_handle(*target);
|
---|
705 | g1_texture_handle th=tmapper.register_texture(han);
|
---|
706 | delete target;
|
---|
707 | g1_file_man.destroy_handle(han);
|
---|
708 | return th;
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 |
|
---|
713 | g1_texture_handle convert_flc(const i4_const_str &original_name,
|
---|
714 | const i4_const_str &basename,
|
---|
715 | g1_texture_mapper_class &tmapper)
|
---|
716 | {
|
---|
717 | i4_str *target=i4gets("anim_name").sprintf(100, &basename);
|
---|
718 |
|
---|
719 | #ifndef G1_RETAIL
|
---|
720 | // see if we need to update the texture
|
---|
721 | if (target_needs_update(original_name, *target))
|
---|
722 | {
|
---|
723 | i4_alert(i4gets("updating_anim"),100,&original_name);
|
---|
724 | w32 tframes;
|
---|
725 | //hacked by trey on 5-23-97 to load a series of .pcx's
|
---|
726 |
|
---|
727 | w32 buf[256];
|
---|
728 | load_palette(basename, original_name, buf);
|
---|
729 |
|
---|
730 | i4_pixel_format fmt;
|
---|
731 | fmt.pixel_depth = I4_8BIT;
|
---|
732 | fmt.lookup = buf;
|
---|
733 |
|
---|
734 | i4_pal_handle_class pal=i4_pal_man.register_pal(&fmt);
|
---|
735 | w32 pal_id=g1_convert_pal_filename_to_id(basename);
|
---|
736 |
|
---|
737 | i4_str *path,*bname,*extension;
|
---|
738 | i4_str *fname;
|
---|
739 |
|
---|
740 | i4_file_man.split_path(original_name,path,bname,extension);
|
---|
741 |
|
---|
742 | w32 i;
|
---|
743 | i = 1;
|
---|
744 | i4_file_class *fp;
|
---|
745 |
|
---|
746 | while (1)
|
---|
747 | {
|
---|
748 |
|
---|
749 | fname = i4gets("pcx_sequence").sprintf(100,path,bname,i);
|
---|
750 |
|
---|
751 | fp = NULL;
|
---|
752 | fp = i4_open(*fname);
|
---|
753 | if (!fp) {
|
---|
754 | delete fname;
|
---|
755 | break;
|
---|
756 | }
|
---|
757 |
|
---|
758 | i4_image_class *im = NULL;
|
---|
759 |
|
---|
760 | im=i4_load_image(fp);
|
---|
761 | delete fp;
|
---|
762 |
|
---|
763 | if (im) {
|
---|
764 | i4_str *frame_name=i4gets("anim_frame_name").sprintf(100,&basename,i-1);
|
---|
765 | i4_image32 *i32=convert_8_to_32((i4_unmatched_image8 *)im);
|
---|
766 | i4_image2 *i2=get_holes((i4_unmatched_image8 *)im);
|
---|
767 |
|
---|
768 | save_texture(i32, NULL/*i2*/, pal, pal_id, *frame_name);
|
---|
769 | delete frame_name;
|
---|
770 | delete im;
|
---|
771 | }
|
---|
772 | i++;
|
---|
773 | delete fname;
|
---|
774 | }
|
---|
775 |
|
---|
776 | tframes = i-1;
|
---|
777 |
|
---|
778 | if (path) delete path;
|
---|
779 | if (bname) delete bname;
|
---|
780 | if (extension) delete extension;
|
---|
781 |
|
---|
782 | fp=i4_file_man.open(*target,i4_file_manager_class::WRITE);
|
---|
783 | fp->write_8(G1_ANIMATION_FILE);
|
---|
784 | fp->write_16(tframes);
|
---|
785 | fp->write_counted_str(basename);
|
---|
786 | delete fp;
|
---|
787 | }
|
---|
788 | #endif
|
---|
789 |
|
---|
790 | g1_file_handle han=g1_file_man.get_handle(*target);
|
---|
791 | g1_texture_handle th=tmapper.register_texture(han);
|
---|
792 | delete target;
|
---|
793 | g1_file_man.destroy_handle(han);
|
---|
794 | return th;
|
---|
795 | }
|
---|
796 |
|
---|
797 |
|
---|
798 | g1_texture_handle convert_name_to_texture(const i4_const_str &name,
|
---|
799 | g1_texture_mapper_class &tmapper)
|
---|
800 | {
|
---|
801 | if (name.length()==0)
|
---|
802 | {
|
---|
803 | i4_warning("texture has no name");
|
---|
804 | return 0;
|
---|
805 | }
|
---|
806 |
|
---|
807 | i4_str *path,*basename,*extension;
|
---|
808 | i4_str *lower_name=new i4_str(name,name.length()+1);
|
---|
809 |
|
---|
810 | i4_file_man.split_path(*lower_name,path,basename,extension);
|
---|
811 | basename->to_lower();
|
---|
812 | lower_name->to_lower();
|
---|
813 |
|
---|
814 | if (path)
|
---|
815 | delete path;
|
---|
816 |
|
---|
817 |
|
---|
818 | if (extension)
|
---|
819 | {
|
---|
820 | #ifndef G1_RETAIL
|
---|
821 | i4_file_status_struct stat;
|
---|
822 | if (!i4_file_man.get_status(*lower_name, stat))
|
---|
823 | {
|
---|
824 | delete lower_name;
|
---|
825 | lower_name=i4gets("default_tga").sprintf(150, basename, extension);
|
---|
826 |
|
---|
827 | if (!i4_file_man.get_status(*lower_name, stat))
|
---|
828 | {
|
---|
829 | if (extension) delete extension;
|
---|
830 | if (basename) delete basename;
|
---|
831 | if (lower_name) delete lower_name;
|
---|
832 |
|
---|
833 | i4_alert(i4gets("file_missing"),100,&name);
|
---|
834 | return 0;
|
---|
835 | }
|
---|
836 | }
|
---|
837 | #endif
|
---|
838 | if (*extension==i4gets("flc_ext"))
|
---|
839 | {
|
---|
840 | g1_texture_handle ret=convert_flc(*lower_name,*basename,tmapper);
|
---|
841 | delete extension;
|
---|
842 | delete basename;
|
---|
843 | delete lower_name;
|
---|
844 | return ret;
|
---|
845 | }
|
---|
846 | else if (*extension==i4gets("tga_ext"))
|
---|
847 | {
|
---|
848 | g1_texture_handle ret=convert_tga(*lower_name,*basename,tmapper);
|
---|
849 | delete extension;
|
---|
850 | delete basename;
|
---|
851 | delete lower_name;
|
---|
852 | return ret;
|
---|
853 | }
|
---|
854 | }
|
---|
855 | else
|
---|
856 | {
|
---|
857 | delete extension;
|
---|
858 | delete basename;
|
---|
859 | delete lower_name;
|
---|
860 | i4_warning("no extension on texture");
|
---|
861 | return 0;
|
---|
862 | }
|
---|
863 | return 0;
|
---|
864 | }
|
---|
865 |
|
---|