aboutsummaryrefslogtreecommitdiff
path: root/project/src/wimgt.c
blob: 5071f5ce1157a4f60e2c2f4b994299a801360ab2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

/***************************************************************************
 *                         _______ _______ _______                         *
 *                        |  ___  |____   |  ___  |                        *
 *                        | |   |_|    / /| |   |_|                        *
 *                        | |_____    / / | |_____                         *
 *                        |_____  |  / /  |_____  |                        *
 *                         _    | | / /    _    | |                        *
 *                        | |___| |/ /____| |___| |                        *
 *                        |_______|_______|_______|                        *
 *                                                                         *
 *                            Wiimms SZS Tools                             *
 *                          https://szs.wiimm.de/                          *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This file is part of the SZS project.                                 *
 *   Visit https://szs.wiimm.de/ for project details and sources.          *
 *                                                                         *
 *   Copyright (c) 2011-2024 by Dirk Clemens <wiimm@wiimm.de>              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#include <sys/stat.h>
#include <unistd.h>

#include "lib-image.h"
#include "ui.h" // [[dclib]] wrapper
#include "ui-wimgt.c"

//
///////////////////////////////////////////////////////////////////////////////
///////////////		functions not neded by wimgt		///////////////
///////////////////////////////////////////////////////////////////////////////

#if !SZS_WRAPPER
 bool DefineIntVar ( VarMap_t * vm, ccp varname, int value ) { return false; }
#endif

//
///////////////////////////////////////////////////////////////////////////////
///////////////			definitions			///////////////
///////////////////////////////////////////////////////////////////////////////

#define TITLE WIMGT_SHORT ": " WIMGT_LONG " v" VERSION " r" REVISION \
	" " SYSTEM2 " - " AUTHOR " - " DATE

//
///////////////////////////////////////////////////////////////////////////////

static void help_exit ( bool xmode )
{
    SetupPager();
    fputs( TITLE "\n", stdout );

    if (xmode)
    {
	int cmd;
	for ( cmd = 0; cmd < CMD__N; cmd++ )
	    PrintHelpCmd(&InfoUI_wimgt,stdout,0,cmd,0,0,URI_HOME);
    }
    else
	PrintHelpCmd(&InfoUI_wimgt,stdout,0,0,"HELP",0,URI_HOME);

    ClosePager();
    exit(ERR_OK);
}

///////////////////////////////////////////////////////////////////////////////

static void print_version_section ( bool print_sect_header )
{
    cmd_version_section(print_sect_header,WIMGT_SHORT,WIMGT_LONG,long_count-1);
}

///////////////////////////////////////////////////////////////////////////////

static void version_exit()
{
    if ( brief_count > 1 )
	fputs( VERSION "\n", stdout );
    else if (brief_count)
	fputs( VERSION " r" REVISION " " SYSTEM2 "\n", stdout );
    else if (print_sections)
	print_version_section(true);
    else if (long_count)
	print_version_section(false);
    else
	fputs( TITLE "\n", stdout );

    exit(ERR_OK);
}

///////////////////////////////////////////////////////////////////////////////

static void print_title ( FILE * f )
{
    static bool done = false;
    if (!done)
    {
	done = true;
	if (print_sections)
	    print_version_section(true);
	else if ( verbose >= 1 && f == stdout )
	    fprintf(f,"\n%s\n\n",TITLE);
	else
	    fprintf(f,"*****  %s  *****\n",TITLE);
    }
}

///////////////////////////////////////////////////////////////////////////////

static const KeywordTab_t * current_command = 0;

static void hint_exit ( enumError stat )
{
    if ( current_command )
	fprintf(stderr,
	    "-> Type '%s help %s' (pipe it to a pager like 'less') for more help.\n\n",
	    ProgInfo.progname, CommandInfo[current_command->id].name1 );
    else
	fprintf(stderr,
	    "-> Type '%s -h' or '%s help' (pipe it to a pager like 'less') for more help.\n\n",
	    ProgInfo.progname, ProgInfo.progname );
    exit(stat);
}

///////////////////////////////////////////////////////////////////////////////

#define SYNTAX_ERROR syntax_error(__FUNCTION__,__FILE__,__LINE__)

static void syntax_error ( ccp func, ccp file, uint line )
{
    if ( current_command
	&& current_command->id >= 0
	&& current_command->id < InfoUI_wimgt.n_cmd )
    {
	const InfoCommand_t *ic = InfoUI_wimgt.cmd_info + current_command->id;
	if (strchr(ic->syntax,'\n'))
	{
	    ccp src = ic->syntax;
	    char *dest = iobuf;
	    while (*src)
		if ( (*dest++ = *src++) == '\n' )
		{
		    *dest++ = ' ';
		    *dest++ = ' ';
		    *dest++ = ' ';
		}
	    *dest = 0;
	    PrintErrorFile(func,file,line,0,0,ERR_SYNTAX,"Syntax:\n   %s\n",iobuf);
	}
	else
	    PrintErrorFile(func,file,line,0,0,ERR_SYNTAX,"Syntax: %s\n",ic->syntax);
    }
    else
	PrintErrorFile(func,file,line,0,0,ERR_SYNTAX,"Syntax Error!\n");
    exit(ERR_SYNTAX);
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			command test			///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError cmd_test_options()
{
    printf("\nOptions (compatibility: %s; format: hex=dec):\n",PrintOptCompatible());

    printf("  test:        %16x = %12d\n",testmode,testmode);
    printf("  verbose:     %16x = %12d\n",verbose,verbose);
    printf("  width:       %16x = %12d\n",opt_width,opt_width);
    printf("  escape-char: %16x = %12d\n",escape_char,escape_char);
    printf("  cmpr-default:         valid=%d, RGB565: %04x %04x, RGB: %06x %06x\n",
		opt_cmpr_valid,
		be16(opt_cmpr_def), be16(opt_cmpr_def+2),
		RGB565_to_RGB(be16(opt_cmpr_def)),
		RGB565_to_RGB(be16(opt_cmpr_def+2)) );
    printf("  n-mipmaps:   %16x = %12d\n",opt_n_images-1,opt_n_images-1);
    printf("  max-mipmaps: %16x = %12d\n",opt_max_images-1,opt_max_images-1);
    printf("  mipmap-size: %16x = %12d\n",opt_min_mipmap_size,opt_min_mipmap_size);
    printf("  mipmaps:     %16x = %12d\n",opt_mipmaps,opt_mipmaps);
    printf("  fast-mipmaps:%16x = %12d\n",fast_resize_enabled,fast_resize_enabled);

#if defined(TEST) || defined(DEBUG) || HAVE_WIIMM_EXT
    MipmapOptions_t mmo;
    SetupMipmapOptions(&mmo);
    PrintMipmapOptions(stdout,2,&mmo);
#endif

    if (patch_image_list.used)
    {
	printf("  image patch list:\n");
	int i;
	for ( i = 0; i < patch_image_list.used; i++ )
	{
	    const FormatFieldItem_t *ffi = patch_image_list.list + i;
	    printf("\t%04x=%s\n",ffi->patch_mode,ffi->key);
	}
    }

    DumpTransformList(stdout,1,false);
    return ERR_OK;
}

///////////////////////////////////////////////////////////////////////////////

static enumError cmd_test()
{
 #if 1 || !defined(TEST) // test options

    return cmd_test_options();

 #elif 1

    ParamList_t *param;
    for ( param = first_param; param; param = param->next )
    {
	NORMALIZE_FILENAME_PARAM(param);

	Image_t img;
	if (!LoadPNG(&img,true,true,param->arg,0))
	{
	    char path[PATH_MAX];
	    char *end = StringCopyS(path,sizeof(path),param->arg);
	    if ( end > path + 4 && !strcasecmp(end-4,".png") )
		end -= 4;
	    *end = 0;

	    StringCopyE(end,path+sizeof(path),"-std.png");
	    SavePNG(&img,true,0,path,0,0,false,0);

	    ConvertIMG(&img,false,0,IMG_X_RGB,PAL_INVALID);
	    StringCopyE(end,path+sizeof(path),"-rgb.png");
	    SavePNG(&img,true,0,path,0,0,false,0);

	    ConvertIMG(&img,false,0,IMG_X_GRAY,PAL_INVALID);
	    StringCopyE(end,path+sizeof(path),"-gray.png");
	    SavePNG(&img,true,0,path,0,0,false,0);

	    ConvertIMG(&img,false,0,IMG_X_PAL,PAL_INVALID);
	    StringCopyE(end,path+sizeof(path),"-pal.png");
	    SavePNG(&img,true,0,path,0,0,false,0);
	}
	ResetIMG(&img);
    }
    return ERR_OK;

 #else
    return ERR_OK;
 #endif
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			command list			///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError cmd_list ( int long_level )
{
    if ( long_level > 0 )
    {
	RegisterOptionByIndex(&InfoUI_wimgt,OPT_LONG,long_level,false);
	long_count += long_level;
    }

    if (print_header)
	PrintImageHead(0,long_count);

    StringField_t plist = {0};
    CollectExpandParam(&plist,first_param,-1,WM__DEFAULT);

    for ( int argi = 0; argi < plist.used; argi++ )
    {
	ccp arg = plist.field[argi];

	char buf[CHECK_FILE_SIZE];
	enumError err = LoadFILE(arg,0,0,buf,sizeof(buf),1,0,false);
	if ( err > ERR_WARNING )
	{
	    if (opt_ignore)
		continue;
	    memset(buf,0,sizeof(buf));
	}
	PrintImage(buf,CHECK_FILE_SIZE,arg,0,0,long_count,false);
    }

    if (print_header)
	putchar('\n');

    ResetStringField(&plist);
    return ERR_OK;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			command decode			///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError cmd_decode()
{
    ccp def_path = "\1P/\1F.png";
    CheckOptDest(def_path,false);

    enumError max_err = ERR_OK;

    StringField_t plist = {0};
    CollectExpandParam(&plist,first_param,-1,WM__DEFAULT);

    for ( int argi = 0; argi < plist.used; argi++ )
    {
	ccp arg = plist.field[argi];

	Image_t img;
	enumError err = LoadIMG(&img, true, arg,
				0, opt_mipmaps >= 0, true, opt_ignore>0 );
	if ( max_err < err )
	    max_err = err;
	if (err)
	    continue;

	char dest[PATH_MAX];
	SubstDest(dest,sizeof(dest),arg,opt_dest,def_path,0,false);

	if ( verbose >= 0 || testmode )
	    fprintf(stdlog,"%s%sDECODE %s:%s -> PNG:%s\n",
			verbose > 0 ? "\n" : "",
			testmode ? "WOULD " : "",
			PrintFormat3(img.info_fform,img.info_iform,img.info_pform),
			img.path, dest );

	TransformIMG(&img,2);
	if (opt_pre_convert)
	{
	    enumError err = ExecTransformIMG(&img);
	    if ( max_err < err )
	         max_err = err;
	}

	if (!testmode)
	{
	    err = SavePNG(&img,true,0,dest,0,0,false,0);
	    if ( max_err < err )
	         max_err = err;
	}
	ResetIMG(&img);
    }

    ResetStringField(&plist);
    return max_err;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////		    command encode/convert		///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError cmd_convert ( int cmd_id, ccp cmd_name, ccp def_path )
{
    CheckOptDest(def_path,false);

    StringField_t plist = {0};
    CollectExpandParam(&plist,first_param,-1,WM__DEFAULT);

    for ( int argi = 0; argi < plist.used; argi++ )
    {
	ccp arg = plist.field[argi];

	Image_t img;
	enumError err = LoadIMG( &img, true, arg, 0,
					opt_mipmaps >= 0, false, opt_ignore );
	if ( err == ERR_NOT_EXISTS || err == ERR_WARNING )
	    continue;
	if ( err > ERR_WARNING )
	    return err;

	const file_format_t	src_f = img.info_fform;
	const image_format_t	src_i = img.info_iform;
	const palette_format_t	src_p = img.info_pform;
	TransformIMG(&img,-1);

	char dest[PATH_MAX];
	SubstDest(dest,sizeof(dest),arg,opt_dest,def_path,0,false);
	const file_format_t fform
		= GetImageFF( img.tform_valid ? img.tform_fform : FF_INVALID,
				FF_INVALID,
				dest,
				img.info_fform,
				cmd_id == CMD_CONVERT,
				FF_TEX );

	if ( fform == FF_PNG )
	    Transform2XIMG(&img);
	else
	    Transform2InternIMG(&img);

	const bool samefile = !strcmp(arg,dest);
	if ( !all_count
		&& !img.conv_count
		&& !img.tform_exec
		&& !img.patch_done
		&& fform == img.info_fform
		&& samefile )
	{
	    continue;
	}

	if ( verbose >= 0 || testmode )
	{
	    image_format_t   iform = img.tform_valid && img.tform_iform != IMG_INVALID
					? img.tform_iform : img.iform;
	    palette_format_t pform = img.tform_valid && img.tform_pform != PAL_INVALID
					? img.tform_pform : img.pform;

	    fprintf(stdlog,"%s%s%s %s:%s -> %s:%s\n",
			verbose > 0 ? "\n" : "",
			testmode ? "WOULD " : "", cmd_name,
			PrintFormat3(src_f,src_i,src_p),
			arg,
			PrintFormat3(fform,iform,pform),
			dest );
	}

	if (opt_pre_convert)
	    TransformIMG(&img,2);

	if (!testmode)
	{
	    err = SaveIMG(&img,fform,0,0,dest,samefile);
	    if ( err > ERR_WARNING )
		return err;
	}
	ResetIMG(&img);
    }

    ResetStringField(&plist);
    return ERR_OK;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			command copy			///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError cmd_copy()
{
    RegisterOptionByIndex(&InfoUI_wimgt,OPT_OVERWRITE,1,false);
    opt_overwrite = true;

    if (!opt_dest)
    {
	if (!first_param)
	    return ERROR0(ERR_MISSING_PARAM, "Missing destination parameter!\n" );

	ParamList_t *param;
	for ( param = first_param; param->next; param = param->next )
	    ;
	ASSERT(param);
	ASSERT(!param->next);
	SetDest(param->arg,false);
	param->arg = 0;
    }

    file_format_t fform_dest = FF_UNKNOWN;
    ccp point = strrchr(opt_dest,'.');
    if (point)
	fform_dest = IsImageFF(GetByNameFF(point+1),true);
    PRINT("fform(dest) = %s\n",GetNameFF(0,fform_dest));

    int done_count = 0;
    StringField_t plist = {0};
    CollectExpandParam(&plist,first_param,-1,WM__DEFAULT);

    for ( int argi = 0; argi < plist.used; argi++ )
    {
	ccp arg = plist.field[argi];
	done_count++;

	Image_t img;
	enumError err = LoadIMG( &img, true, arg, 0,
					opt_mipmaps >= 0, false, opt_ignore );
	if ( err == ERR_NOT_EXISTS || err == ERR_WARNING )
	    continue;
	if ( err > ERR_WARNING )
	    return err;

	const file_format_t	src_f = img.info_fform;
	const image_format_t	src_i = img.info_iform;
	const palette_format_t	src_p = img.info_pform;
	TransformIMG(&img,-1);

	file_format_t fform = img.tform_valid && img.tform_fform != FF_INVALID
				? img.tform_fform
				: fform_dest != FF_UNKNOWN
					? fform_dest
					: src_f;
	PRINT0("FFORM = %s\n",GetNameFF(0,fform));

	char dest[PATH_MAX];
	SubstDest(dest,sizeof(dest),arg,opt_dest,0,0,false);

	if ( fform == FF_PNG )
	    Transform2XIMG(&img);
	else
	    Transform2InternIMG(&img);

	const bool samefile = !strcmp(arg,dest);
	if ( !all_count
		&& !img.conv_count
		&& !img.tform_exec
		&& !img.patch_done
		&& fform == img.info_fform
		&& samefile )
	{
	    continue;
	}

	const bool create_tpl = IsTplFF(fform);
	if ( !create_tpl && ( verbose >= 0 || testmode ))
	{
	    image_format_t   iform = img.tform_valid && img.tform_iform != IMG_INVALID
					? img.tform_iform : img.iform;
	    palette_format_t pform = img.tform_valid && img.tform_pform != PAL_INVALID
					? img.tform_pform : img.pform;

	    fprintf(stdlog,"%s%sCOPY %s:%s -> %s:%s\n",
			verbose > 0 ? "\n" : "",
			testmode ? "WOULD " : "",
			PrintFormat3(src_f,src_i,src_p),
			arg,
			PrintFormat3(fform,iform,pform),
			dest );
	}

	if (opt_pre_convert)
	    TransformIMG(&img,2);

	tpl_raw_t raw;
	if (create_tpl)
	{
	    err = CreateRawTPL(&raw,&img,fform);

	    if ( verbose >= 0 || testmode )
	    {
		fform = IsTplHeaderEx(raw.data.ptr,raw.data.len) ? FF_TPLX : FF_TPL;
		fprintf(stdlog,"%s%sCOPY %s:%s -> %s:%s\n",
			    verbose > 0 ? "\n" : "",
			    testmode ? "WOULD " : "",
			    PrintFormat3(src_f,src_i,src_p),
			    arg,
			    PrintFormat3(fform,img.iform,img.pform),
			    dest );
	    }
	}

	if (!testmode)
	{
	    err = create_tpl
		? SaveRawTPL(&raw,0,dest,samefile)
		: SaveIMG(&img,fform,0,0,dest,samefile);;
	    if ( err > ERR_WARNING )
		return err;
	}
	if (create_tpl)
	    ResetRawTPL(&raw);
	ResetIMG(&img);
    }

    if (!done_count)
	SYNTAX_ERROR;

    ResetStringField(&plist);
    return ERR_OK;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////                   check options                 ///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError CheckOptions ( int argc, char ** argv, bool is_env )
{
    TRACE("CheckOptions(%d,%p,%d) optind=%d\n",argc,argv,is_env,optind);

    optind = 0;
    int err = 0;

    for(;;)
    {
      const int opt_stat = getopt_long(argc,argv,OptionShort,OptionLong,0);
      if ( opt_stat == -1 )
	break;

      RegisterOptionByName(&InfoUI_wimgt,opt_stat,1,is_env);

      switch ((enumGetOpt)opt_stat)
      {
	case GO__ERR:		err++; break;

	case GO_VERSION:	version_exit();
	case GO_HELP:		help_exit(false);
	case GO_XHELP:		help_exit(true);
	case GO_CONFIG:		opt_config = optarg;
	case GO_YDEBUG:		enable_ydebug++; break;
	case GO_ALLOW_ALL:	allow_all = true; break;
        case GO_COMPATIBLE:	err += ScanOptCompatible(optarg); break;
	case GO_WIDTH:		err += ScanOptWidth(optarg); break;
	case GO_MAX_WIDTH:	err += ScanOptMaxWidth(optarg); break;
	case GO_NO_PAGER:	opt_no_pager = true; break;
	case GO_QUIET:		verbose = verbose > -1 ? -1 : verbose - 1; break;
	case GO_VERBOSE:	verbose = verbose <  0 ?  0 : verbose + 1; break;
	case GO_LOGGING:	logging++; break;
	case GO_EXT_ERRORS:	ext_errors++; break;
	case GO_TIMING:		log_timing++; break;
	case GO_WARN:		err += ScanOptWarn(optarg); break;
	case GO_DE:		use_de = true; break;
	case GO_CT_CODE:	ctcode_enabled = true; break;
	case GO_LE_CODE:	lecode_enabled = true; break; // optional argument ignored
	case GO_LE_04X:		lecode_04x = true; break;
	case GO_COLORS:		err += ScanOptColorize(0,optarg,0); break;
	case GO_NO_COLORS:	opt_colorize = COLMD_OFF; break;

	case GO_CHDIR:		err += ScanOptChdir(optarg); break;
	case GO_CONST:		break; // [[2do]] err += ScanOptConst(optarg); break;

	case GO_UTF_8:		use_utf8 = true; break;
	case GO_NO_UTF_8:	use_utf8 = false; break;

	case GO_TEST:		testmode++; break;
	case GO_FORCE:		force_count++; break;
	case GO_REPAIR_MAGICS:	err += ScanOptRepairMagic(optarg); break;

 #if OPT_OLD_NEW
	case GO_OLD:		opt_new = opt_new>0 ? -1 : opt_new-1; break;
	case GO_STD:		opt_new = 0; break;
	case GO_NEW:		opt_new = opt_new<0 ? +1 : opt_new+1; break;
 #endif
	case GO_EXTRACT:	opt_extract = optarg; break;

	case GO_ESC:		err += ScanEscapeChar(optarg) < 0; break;
	case GO_DEST:		SetDest(optarg,false); break;
	case GO_DEST2:		SetDest(optarg,true); break;
	case GO_OVERWRITE:	opt_overwrite = true; break;
	case GO_NUMBER:		opt_number = true; break;
	case GO_REMOVE_DEST:	opt_remove_dest = true; break;
	case GO_UPDATE:		opt_update = true; break;
	case GO_PRESERVE:	opt_preserve = true; break;
	case GO_IGNORE:		opt_ignore++; break;

	case GO_MAX_FILE_SIZE:	err += ScanOptMaxFileSize(optarg); break;
	case GO_TRACKS:		err += ScanOptTracks(optarg); break;
	case GO_ARENAS:		err += ScanOptArenas(optarg); break;

	case GO_LONG:		long_count++; break;
	case GO_NO_HEADER:	print_header = false; break;
	case GO_BRIEF:		brief_count++; break;
	case GO_NO_WILDCARDS:	no_wildcards_count++; break;
	case GO_IN_ORDER:	inorder_count++; break;
	case GO_SECTIONS:	print_sections++; break;

	case GO_ALL:		all_count++; break;
	case GO_CMPR_DEFAULT:	err += ScanOptCmprDefault(optarg); break;
	case GO_N_MIPMAPS:	err += ScanOptNMipmaps(optarg); break;
	case GO_MAX_MIPMAPS:	err += ScanOptMaxMipmaps(optarg); break;
	case GO_MIPMAP_SIZE:	err += ScanOptMipmapSize(optarg); break;
	case GO_MIPMAPS:	opt_mipmaps = +1; break;
	case GO_NO_MIPMAPS:	opt_mipmaps = -1; break;
	case GO_FAST_MIPMAPS:	fast_resize_enabled = true; break;
	case GO_PRE_CONVERT:	opt_pre_convert = true; break;
	case GO_PATCH:		err += ScanOptPatchImage(optarg); break;
	case GO_TRANSFORM:	err += ScanOptTransform(optarg); break;
	case GO_STRIP:		opt_strip = true; break;

	// no default case defined
	//	=> compiler checks the existence of all enum values
      }
    }

 #ifdef DEBUG
    DumpUsedOptions(&InfoUI_wimgt,TRACE_FILE,11);
 #endif
    NormalizeOptions( verbose > 3 && !is_env );

    return !err ? ERR_OK : ProgInfo.max_error ? ProgInfo.max_error : ERR_SYNTAX;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////                   check command                 ///////////////
///////////////////////////////////////////////////////////////////////////////

static enumError CheckCommand ( int argc, char ** argv )
{
    const KeywordTab_t * cmd_ct = CheckCommandHelper(argc,argv,CommandTab);
    if (!cmd_ct)
	hint_exit(ERR_SYNTAX);

    TRACE("COMMAND FOUND: #%lld = %s\n",(u64)cmd_ct->id,cmd_ct->name1);
    current_command = cmd_ct;

    if (!allow_all)
    {
	enumError err = VerifySpecificOptions(&InfoUI_wimgt,cmd_ct);
	if (err)
	    hint_exit(err);
    }
    WarnDepractedOptions(&InfoUI_wimgt);

    if ( cmd_ct->id != CMD_ARGTEST )
    {
	argc -= optind+1;
	argv += optind+1;

	if ( cmd_ct->id == CMD_TEST )
	    while ( argc-- > 0 )
		AddParam(*argv++);
	else
	    while ( argc-- > 0 )
		AtFileHelper(*argv++,AddParam);
    }

    enumError err = ERR_OK;
    switch ((enumCommands)cmd_ct->id)
    {
	case CMD_VERSION:	version_exit();
	case CMD_HELP:		PrintHelpColor(&InfoUI_wimgt); break;
	case CMD_CONFIG:	err = cmd_config(); break;
	case CMD_ARGTEST:	err = cmd_argtest(argc,argv); break;
	case CMD_EXPAND:	err = cmd_expand(argc,argv); break;
	case CMD_TEST:		err = cmd_test(); break;
	case CMD_COLORS:	err = Command_COLORS(brief_count?-brief_count:long_count,0,0); break;
	case CMD_ERROR:		err = cmd_error(); break;
	case CMD_FILETYPE:	err = cmd_filetype(); break;
	case CMD_FILEATTRIB:	err = cmd_fileattrib(); break;

	case CMD_LIST:		err = cmd_list(0); break;
	case CMD_LIST_L:	err = cmd_list(1); break;
	case CMD_LIST_LL:	err = cmd_list(2); break;

	case CMD_DECODE:	err = cmd_decode(); break;
	case CMD_ENCODE:	err = cmd_convert(cmd_ct->id,"ENCODE","\1P/\1N"); break;
	case CMD_CONVERT:	err = cmd_convert(cmd_ct->id,"CONVERT",0); break;
	case CMD_COPY:		err = cmd_copy(); break;

	// no default case defined
	//	=> compiler checks the existence of all enum values

	case CMD__NONE:
	case CMD__N:
	    help_exit(false);
    }
    ResetPatchListIMG();
    return PrintErrorStat(err,verbose,cmd_ct->name1);
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			   main()			///////////////
///////////////////////////////////////////////////////////////////////////////

#if SZS_WRAPPER
    int main_wimgt ( int argc, char ** argv )
#else
    int main ( int argc, char ** argv )
#endif
{
 #if !SZS_WRAPPER
    ArgManager_t am = {0};
    SetupArgManager(&am,LOUP_AUTO,argc,argv,false);
    ExpandAtArgManager(&am,AMXM_SHORT,10,false);
    argc = am.argc;
    argv = am.argv;
 #endif

    tool_name = "wimgt";
    print_title_func = print_title;
    SetupLib(argc,argv,WIMGT_SHORT,VERSION,TITLE);

    //----- process arguments

    if ( argc < 2 )
    {
	printf("\n%s\n%s\nVisit %s%s for more info.\n\n",
		text_logo, TITLE, URI_HOME, WIMGT_SHORT );
	hint_exit(ERR_OK);
    }

    enumError err = CheckEnvOptions2("WIMGT_OPT",CheckOptions);
    if (err)
	hint_exit(err);

    err = CheckOptions(argc,argv,false);
    if (err)
	hint_exit(err);

    err = CheckCommand(argc,argv);
    DUMP_TRACE_ALLOC(TRACE_FILE);

    if (SIGINT_level)
	err = ERROR0(ERR_INTERRUPT,"Program interrupted by user.");
    ClosePager();
    return err;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    END				///////////////
///////////////////////////////////////////////////////////////////////////////