aboutsummaryrefslogtreecommitdiff
path: root/project/src/lib-image.h
blob: e7fc84b423f80a5f9f2c5fccbcf9e640a132e48a (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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127

/***************************************************************************
 *                         _______ _______ _______                         *
 *                        |  ___  |____   |  ___  |                        *
 *                        | |   |_|    / /| |   |_|                        *
 *                        | |_____    / / | |_____                         *
 *                        |_____  |  / /  |_____  |                        *
 *                         _    | | / /    _    | |                        *
 *                        | |___| |/ /____| |___| |                        *
 *                        |_______|_______|_______|                        *
 *                                                                         *
 *                            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-2022 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       *
 *                                                                         *
 ***************************************************************************/

#ifndef SZS_LIB_IMAGE_H
#define SZS_LIB_IMAGE_H 1

#define _GNU_SOURCE 1

#include "lib-std.h"
#include "lib-numeric.h"

//
///////////////////////////////////////////////////////////////////////////////
///////////////			relevant infos			///////////////
///////////////////////////////////////////////////////////////////////////////

// https://wiki.tockdom.de/index.php?title=Image_Formats

//
///////////////////////////////////////////////////////////////////////////////
///////////////			Color_t				///////////////
///////////////////////////////////////////////////////////////////////////////
// [[Color_t]]

typedef union Color_t
{
    struct
    {
	u8 r;		// red
	u8 g;		// green
	u8 b;		// blue
	u8 a;		// alpha
    };

    u8	rgba[4];	// array
    u32	val;		// combined value
}
__attribute__ ((packed)) Color_t;

#define SETCOLOR(C,R,G,B,A) (C).r=(R), (C).g=(G), (C).b=(B), (C).a=(A)

//
///////////////////////////////////////////////////////////////////////////////
///////////////			MipmapOptions_t			///////////////
///////////////////////////////////////////////////////////////////////////////
// [[MipmapOptions_t]]

struct Image_t;

typedef struct MipmapOptions_t
{
    bool	valid;		// TRUE: setup of this record done
    bool	force;		// TRUE: force 'n_mipmap'
    uint	n_mipmap;	// number of mipmaps
    uint	n_image;	// := n_mipmap+1
    uint	min_size;	// minimal mipmap width and height
}
MipmapOptions_t;

// both functions return 'dest', 'src' may be NULL
void SetupMipmapOptions	  ( MipmapOptions_t *mmo );
void SetupMipmapOptions1  ( MipmapOptions_t *mmo );
void CopyMipmapOptions    ( MipmapOptions_t *dest, const MipmapOptions_t *src );
void MipmapOptionsByImage ( MipmapOptions_t *mmo, const struct Image_t *img );

ccp InfoMipmapOptions ( const MipmapOptions_t *mmo );
ccp TextMipmapOptions ( const MipmapOptions_t *mmo );
void PrintMipmapOptions ( FILE *f, int indent, const MipmapOptions_t *mmo );

//
///////////////////////////////////////////////////////////////////////////////
///////////////			ImageGeometry_t			///////////////
///////////////////////////////////////////////////////////////////////////////
// [[ImageGeometry_t]]

typedef struct ImageGeometry_t
{
    image_format_t	iform;		// image format

    bool		is_x;		// true: is a IMG_X_* format
    bool		is_gray;	// true: is a gray scale format
    bool		has_alpha;	// true: supports alpha/transparent infos
    bool		has_blocks;	// true: is structured in blocks

    u16			max_palette;	// >0: max number of palette entries
    bool		read_support;	// true: tools can read this format
    bool		write_support;	// true: tools can write this format

    uint		bits_per_pixel;	// number of bits per pixel
    uint		block_width;	// width of a block in pixel
    uint		block_height;	// height of a block in pixel
    uint		block_size;	// size of a block in bytes

    ccp			name;		// name of image
}
ImageGeometry_t;

//-----------------------------------------------------------------------------

const ImageGeometry_t * GetImageGeometry ( image_format_t iform );
int PaletteToImageFormat ( int pform, int return_if_invalid );

uint CalcImageSize
(
    uint		width,		// width of image in pixel
    uint		height,		// height of image in pixel

    uint		bits_per_pixel,	// number of bits per pixel
    uint		block_width,	// width of a single block
    uint		block_height,	// height of a single block

    uint		* x_width,	// not NULL: store extended width here
    uint		* x_height,	// not NULL: store extended height here
    uint		* h_blocks,	// not NULL: store number of horizontal blocks
    uint		* v_blocks	// not NULL: store number of vertical blocks
);

const ImageGeometry_t * CalcImageGeometry
(
    image_format_t	iform,		// image format
    uint		width,		// width of image in pixel
    uint		height,		// height of image in pixel

    uint		* x_width,	// not NULL: store extended width here
    uint		* x_height,	// not NULL: store extended height here
    uint		* h_blocks,	// not NULL: store number of horizontal blocks
    uint		* v_blocks,	// not NULL: store number of vertical blocks
    uint		* img_size	// not NULL: return image size
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    Image_t			///////////////
///////////////////////////////////////////////////////////////////////////////
// [[Image_t]]

typedef struct Image_t
{
    //--- image data

    image_format_t	iform;		// current data format
    u8			* data;		// pointer to data or NULL if iform==IMG_INVALID
    uint		data_size;	// size of data in bytes
    bool		data_alloced;	// true: 'data' is alloced -> free() it

    uint		width;		// real width of image in pixel
    uint		height;		// real height of image in pixel
    bool		is_grayed;	// is grayed
    int			alpha_status;	// -1: alpha not used (all=0xff)
					//  0: unknown
					//  1: alpha channel used (at least one != 0xff)

    // X_GRAY+X_RGB: The image is always extended to multiple of 8*8
    // xwidth+xheight show this value and are generally = width,height
    uint		xwidth;		// extended width, only valid for IMG_X_ formats
    uint		xheight;	// extended height, only valid for IMG_X_ formats

    uint		seq_num;	// a unique number, changed for every conversion
					// copies will get the same number
    uint		conv_count;	// initialized with 0
					// it's incremented for every conversion

    //--- mipmap support

    struct Image_t	* mipmap;	// NULL or mipmap of next level


    //--- palette data

    palette_format_t	pform;		// palette format
    u8			* pal;		// NULL or pointer to palette
    uint		pal_size;	// size of 'pal' in bytes
    bool		pal_alloced;	// true: 'pal' is alloced -> free() it
    uint		n_pal;		// number of used palette entries


    //--- control data

    u8			* container;	// free it on reset
    const endian_func_t * endian;	// endian functions to access internal formats


    //--- additional image infos

    ccp			path;		// NULL or alloced filename of image
    bool		path_alloced;	// true: 'path' is alloced -> free() it
    bool		is_cup_icon;	// true: image contains cup icons
    file_format_t	info_fform;	// info about original file format
    image_format_t	info_iform;	// info about original image format
    palette_format_t	info_pform;	// info about original palette format
    uint		info_size;	// info about projected image size
    uint		info_n_image;	// number of images in original
    FileAttrib_t	fatt;		// file attributes


    //--- transforming results

    bool		tform_valid;	// false: the 'tform_' are invalid
    bool		tform_exec;	// true: job for ExecTransformIMG()
    bool		tform_gray;	// result is grayed
    bool		tform_noalpha;	// alpha channel must be cleard or removed
    file_format_t	tform_fform0;	// source value for transformation calculations
    image_format_t	tform_iform0;	//	"
    palette_format_t	tform_pform0;	//	"
    file_format_t	tform_fform;	// planned transformation
    image_format_t	tform_iform;	//	"
    palette_format_t	tform_pform;	//	"


    //--- patching infos

    bool		patch_done;	// true: image already patched
    PatchImage_t	patch_mode;	// only used by the patching list

} Image_t;

extern uint image_seq_num;

#define MM_COUNT(a) PRINT("MM-COUNT: %u [%s() @%u]\n", \
		CountMipmapsIMG(a),__FUNCTION__,__LINE__)
//#define MM_COUNT(a)

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

void InitializeIMG ( Image_t * img );
void ResetIMG ( Image_t * img );
void RemoveContainerIMG ( Image_t * img );

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

void FreeIMG
(
    Image_t		* img,		// destination image, dynamic data freed
    const Image_t	* src		// not NULL: copy parameters, but not data
);

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

void FreeMipmapsIMG
(
    Image_t		* img		// not NULL: remove mipmaps
);

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

uint CountMipmapsIMG
(
    const Image_t	* img		// count mipmaps
);

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

void CopyIMG
(
    Image_t		* dest,		// destination image
    bool		init_dest,	// true: initialize 'dest' first
    const Image_t	* src,		// valid source image
    bool		copy_mipmap	// true: copy mipmaps too
);

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

void MoveIMG
(
    Image_t		* dest,		// destination image
    bool		init_dest,	// true: initialize 'dest' first
    Image_t		* src		// NULL or source image, isresetted
);

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

void MoveDataIMG
(
    Image_t		* dest,		// valid destination image
    Image_t		* src		// valid source image, is resetted
);

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

void AssignDataGRAY
(
    Image_t		* dest,		// valid destination
    const Image_t	* src,		// NULL or image template
    u8			* data		// with AllocDataIMG() alloced data
);

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

void AssignDataRGB
(
    Image_t		* dest,		// valid destination
    const Image_t	* src,		// NULL or image template
    u8			* data		// with AllocDataIMG() alloced data
);

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

void ScanDataIMG
(
    Image_t		* img,		// destination image
    bool		init_img,	// true: initialize 'img' first
    const void		* data,		// data to scan
    uint		data_size	// size of 'data'
);

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

enumError AssignIMG
(
    Image_t		* img,		// pointer to valid img
    int			init_img,	// <0:none, =0:reset, >0:init
    const u8		* data,		// source data
    uint		data_size,	// size of 'data'
    uint		img_index,	// index of sub image, 0:main, >0:mipmaps
    bool		mipmaps,	// true: assign mipmaps
    const endian_func_t * endian,	// endian functions to read data
    ccp			fname		// object name, assigned
);

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

enumError LoadIMG
(
    Image_t		* img,		// pointer to valid img
    bool		init_img,	// true: initialize 'img'
    ccp			fname,		// filename of source
    uint		img_index,	// index of sub image, 0:main, >0:mipmaps
    bool		mipmaps,	// true: load and assign mipmaps
    bool		allow_subfile,	// allow to extract szs sub files
    bool		ignore_no_file	// ignore if file does not exists
					// and return warning ERR_NOT_EXISTS
);

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

enumError SaveIMG
(
    Image_t		* img,		// pointer to valid img
    file_format_t	fform,		// file format
    const MipmapOptions_t *mmo,		// NULL or mipmap options
    FILE		*f,		// output file, if NULL then use fname+overwrite
    ccp			fname,		// filename of source
    bool		overwrite	// true: force overwriting
);

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

enumError SaveTPL
(
    Image_t		* src_img,	// pointer to valid source img
    FILE		*f,		// output file, if NULL then use fname+overwrite
    ccp			fname,		// filename of source
    bool		overwrite	// true: force overwriting
);

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

enumError SaveBTI
(
    Image_t		* src_img,	// pointer to valid source img
    const MipmapOptions_t *mmo,		// NULL or mipmap options
    FILE		*f,		// output file, if NULL then use fname+overwrite
    ccp			fname,		// filename of source
    bool		overwrite	// true: force overwriting
);

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

enumError SaveTEX
(
    Image_t		* src_img,	// pointer to valid source img
    const MipmapOptions_t *mmo,		// NULL or mipmap options
    FILE		*f,		// output file, if NULL then use fname+overwrite
    ccp			fname,		// filename of source
    bool		overwrite,	// true: force overwriting
    bool		ctcode_support	// true: include a CT-CODE file
);

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

enumError SaveBREFTIMG
(
    Image_t		* src_img,	// pointer to valid img
    const MipmapOptions_t *mmo,		// NULL or mipmap options
    FILE		*f,		// output file, if NULL then use fname+overwrite
    ccp			fname,		// filename of source
    bool		overwrite	// true: force overwriting
);

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

bool NormalizeFrameIMG
(
    Image_t		* img		// pointer to valid img
);

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

int CheckAlphaIMG
(
    const Image_t	* img,		// pointer to valid img
    bool		force		// true: don't believe 'img->alpha_status'
);

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

bool IsGrayIMG
(
    const Image_t	* img		// pointer to valid img
);

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

image_format_t NormalizeIF
(
    image_format_t	* xform,	// not NULL: store the best IMG_X_* here
    image_format_t	iform,		// wanted image format
    image_format_t	x_default,	// default format, use if iform == AUTO
					// and return a X-format
    palette_format_t	pform		// related palette format
);

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

palette_format_t NormalizePF
(
    image_format_t	iform,		// related image format
    palette_format_t	pform,		// wanted palette format
    palette_format_t	default_pform	// palette format for auto
);

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

uint GetPaletteCountIF
(
    image_format_t	iform		// image format
);

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

enumError ConvertIMG
(
    Image_t		* dest,		// destination of conversion
    bool		init_dest,	// true: initialize 'dest' first
    const Image_t	* src,		// source of conversion.
					// if src==NULL or src==dest
					//   -> inplace conversion of 'dest'
    image_format_t	iform,		// new image format
    palette_format_t	pform		// new palette format
);

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

enumError ConvertToRGB
(
    Image_t		* dest_img,	// valid destination
    const Image_t	* src_img,	// valid source
    palette_format_t	pform		// wanted palette format => ignored
);

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

enumError ConvertToGRAY
(
    Image_t		* dest_img,	// valid destination
    const Image_t	* src_img,	// valid source
    palette_format_t	pform		// wanted palette format => ignored
);

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

enumError ConvertToPALETTE
(
    Image_t		* dest_img,	// valid destination
    const Image_t	* src_img,	// valid source
    uint		max_pal,	// maximum palette values, 0 = auto
    image_format_t	iform		// if a valid PAL format:
					//    convert to this and
					//    reduce 'max_pal' if neccessary
);

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

enumError LoadPNG
(
    Image_t		* img,		// destination image
    bool		init_img,	// true: initialize 'img' first
    bool		mipmaps,	// true: load and export mipmaps

    ccp			path1,		// NULL or part #1 of path
    ccp			path2		// NULL or part #2 of path
);

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

enumError ReadPNG
(
    Image_t		* img,		// destination image
    bool		mipmaps,	// true: try to load mipmaps
    File_t		* f,		// valid opened file
    u8			* cache,	// pre read data
    uint		cache_size	// size of 'cache'
);

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

enumError SavePNG
(
    Image_t		* img,		// pointer to valid img
    bool		mipmaps,	// true: save mipmaps (auto file name)
    FILE		*f,		// output file, if NULL then use path1+path2
    ccp			path1,		// NULL or part #1 of path
    ccp			path2,		// NULL or part #2 of path
    int			store_alpha,	// <0:no alpha, =0:auto alpha, >0:store alpha
    bool		overwrite,	// true: force overwriting
    StringField_t	*file_list	// not NULL: store filenames of created png files
);

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

enumError ExportPNG
(
    ccp			path1,		// NULL or part #1 of path
    ccp			path2,		// NULL or part #2 of path
    FileAttrib_t	* fatt,		// NULL or file attributes
    const void		* img_data,	// image data
    uint		img_size,	// image size, needed for validation
    uint		img_index,	// index of sub image, 0:main, >0:mipmaps
    bool		mipmaps,	// true: load and export mipmaps
    uint		* n_image,	// not null: store detected image count
    const endian_func_t * endian,	// endian functions to read data
    FormatFieldItem_t	* ffi,		// not null: store detected image+pal format
    bool		create_png,	// false: do some calculations but don't create png
    StringField_t	*file_list	// not NULL: store filenames of created png files
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			generic images			///////////////
///////////////////////////////////////////////////////////////////////////////

typedef struct BZ2Manager_t BZ2Manager_t;

typedef struct GenericImgParam_t
{
    //--- input, setup by CheckGenericIMG()

    mem_t		cmd_name;	// command
    mem_t		param;		// parameter string
    bool		ignore_unknown;	// ignore unknown commands and images

    //--- analysis, setup by CreateGenericIMG()

    const KeywordTab_t	*cmd;		// selected command
    int			width;		// width of image
    int			height;		// height of image
    Color_t		color;		// default color
    BZ2Manager_t	*font;		// NULL or selected font (red_36|blue_40)
}
GenericImgParam_t;

//-----------------------------------------------------------------------------

bool CheckGenericIMG // returns TRUE if command syntax is ok
(
    GenericImgParam_t	* par,		// parameter to setup
    ccp			fname,		// filename to check
    bool		ignore_unknown	// true: ignore unknown (sub-)commands
);

//-----------------------------------------------------------------------------

enumError CreateGenericIMG
(
    GenericImgParam_t	* par,		// valid parameters
    Image_t		* img,		// pointer to valid img
    bool		init_img	// true: initialize 'img'
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////		    scan file and image format		///////////////
///////////////////////////////////////////////////////////////////////////////

int  ScanOptTransform ( ccp arg );
void DumpTransformList ( FILE * f, int indent, bool force );

ccp PrintFormat3 // print only output (internal) formats
(
    file_format_t	fform,	// file format
    image_format_t	iform,	// image format
    palette_format_t	pform	// palette format
);

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

ccp PrintImageFormat // print any format, if needed as hex
(
    image_format_t	iform,	// image format
    palette_format_t	pform	// palette format
);

static inline ccp PrintFormatIMG ( const Image_t * img )
{
    DASSERT(img);
    return PrintImageFormat(img->iform,img->pform);
};

//
///////////////////////////////////////////////////////////////////////////////
///////////////		  image format transformations		///////////////
///////////////////////////////////////////////////////////////////////////////

bool Transform2InternIMG ( Image_t * img );
bool Transform2XIMG ( Image_t * img );
bool Transform2XRGB ( Image_t * img );
bool Transform2PaletteIMG ( Image_t * img );
bool Transform2NoPaletteIMG ( Image_t * img );
bool Transform2GrayIMG ( Image_t * img );
bool Transform2ColorIMG ( Image_t * img );
bool Transform2AlphaIMG ( Image_t * img );
bool Transform2NoAlphaIMG ( Image_t * img );

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

bool Transform3IMG
(
    Image_t		* img,		// valid image
    file_format_t	fform,		// file format
    image_format_t	iform,		// image format
    palette_format_t	pform,		// palette format
    bool		def_base	// define *form values as base
);

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

bool TransformIMG
(
    // The image is NOT converted, only the planned transformations
    // are calculated. Use ExecTransformIMG() to execute transformation
    //  -> returns TRUE if a rule match

    Image_t		* img,		// destination of transforming
    int			indent		// <0:  no logging
					// >=0: log transform with indent
);

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

enumError ExecTransformIMG
(
    Image_t		* img		// image to transform
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////		    image creation + drawing		///////////////
///////////////////////////////////////////////////////////////////////////////

enumError CreateIMG
(
    Image_t		* img,		// image object
    bool		init_img,	// true: initialize 'img' first
    uint		width,		// width a new image
    uint		height,		// height of new image
    Color_t		col		// fill color
);

//-----------------------------------------------------------------------------

enumError CopyRectIMG
(
    Image_t		* dest,		// dest image
    int			dest_x,		// left coordinate of dest
    int			dest_y,		// top coordinate of dest

    const Image_t	* src,		// source image
    int			src_x,		// left coordinate of src
    int			src_y,		// top coordinate of src

    int			width,		// width of rect to copy
    int			height,		// height of rect to copy

    PatchImage_t	patch_mode	// one of COPY, MIX, FOREGROUND, BACKGROUND
);

//-----------------------------------------------------------------------------

enumError DrawPointIMG
(
    Image_t		* img,		// image
    int			x,		// x position (robust)
    int			y,		// y position (robust)
    Color_t		col,		// draw color
    bool		combine		// true: combine 'col' with current one
);

//-----------------------------------------------------------------------------

enumError DrawHLineIMG
(
    Image_t		* img,		// image
    int			x1,		// start point (robust)
    int			x2,		// end point (not drawn, robust)
    int			y,		// common y coordinate (robust)
    Color_t		col		// draw color
);

//-----------------------------------------------------------------------------

enumError DrawVLineIMG
(
    Image_t		* img,		// image
    int			x,		// common x coordinate (robust)
    int			y1,		// start point (robust)
    int			y2,		// end point (not drawn, robust)
    Color_t		col		// draw color
);

//-----------------------------------------------------------------------------

enumError DrawFrameIMG
(
    Image_t		* img,		// image
    int			x1,		// left coordinate (robust)
    int			y1,		// top coordinate (robust)
    int			x2,		// right coordinate (robust)
    int			y2,		// bottom coordinate (robust)
    Color_t		col		// draw color
);

//-----------------------------------------------------------------------------

enumError FillRectIMG
(
    Image_t		* img,		// image
    int			x1,		// left coordinate (robust)
    int			y1,		// top coordinate (robust)
    int			x2,		// right coordinate (robust)
    int			y2,		// bottom coordinate (robust)
    Color_t		col,		// draw color
    bool		combine		// true: combine 'col' with current one
);

//-----------------------------------------------------------------------------

enumError FillHGradientIMG
(
    Image_t		* img,		// image
    int			x1,		// left coordinate (robust)
    int			y1,		// top coordinate (robust)
    int			x2,		// right coordinate (robust)
    int			y2,		// bottom coordinate (robust)
    Color_t		col1,		// draw color left
    Color_t		col2		// draw color right
);

//-----------------------------------------------------------------------------

enumError FillVGradientIMG
(
    Image_t		* img,		// image
    int			x1,		// left coordinate (robust)
    int			y1,		// top coordinate (robust)
    int			x2,		// right coordinate (robust)
    int			y2,		// bottom coordinate (robust)
    Color_t		col1,		// draw color top
    Color_t		col2		// draw color bottom
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			image resizing			///////////////
///////////////////////////////////////////////////////////////////////////////

extern bool fast_resize_enabled;

enumError ResizeIMG
(
    Image_t		* dest,		// dest image (may be same as src)
    bool		init_dest,	// true: initialize 'dest' first
    const Image_t	* src,		// source image (if NULL: use dest)
    uint		width,		// new width of image
    uint		height		// new height of image
					// if one of 'width' or 'height' is NULL
					//	-> keep aspect ratio
);

//-----------------------------------------------------------------------------

enumError FastResizeIMG
(
    Image_t		* dest,		// dest image (may be same as src)
    bool		init_dest,	// true: initialize 'dest' first
    const Image_t	* src,		// source image (if NULL: use dest)
    uint		width,		// new width of image
    uint		height		// new height of image
					// if one of 'width' or 'height' is NULL
					//	-> keep aspect ratio
);

//-----------------------------------------------------------------------------

enumError SmartResizeIMG
(
    Image_t		* dest,		// dest image (may be same as src)
    bool		init_dest,	// true: initialize 'dest' first
    const Image_t	* src,		// source image (if NULL: use dest)
    uint		width,		// new width of image
    uint		height		// new height of image
					// if one of 'width' or 'height' is NULL
					//	-> keep aspect ratio
);

//-----------------------------------------------------------------------------

enumError HalfIMG
(
    Image_t		* img		// source and dest
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			CMPR interface			///////////////
///////////////////////////////////////////////////////////////////////////////

#define CMPR_MAX_COL 16
#define CMPR_DATA_SIZE (4*CMPR_MAX_COL)

//-----------------------------------------------------------------------------
// [[cmpr_info_t]]

typedef struct cmpr_info_t
{
    ccp		name;			// name of method
    uint	opaque_count;		// number of opaque (=non transparent) pixels
    u8		p[4][4];		// 4 calculated values
					// p[0] + p[1]: values
					// if opaque_count < 16: p[2] == p[3]
    u8		default_vector[8];	// init by 'opt_cmpr_def'

    //--- statistics, only valid if 'fill_info' set

    u8		index[CMPR_MAX_COL];	// color index of each value
    uint	dist[CMPR_MAX_COL];	// distance of each value
    uint	total_dist;		// sum of all distances

    u8		cmpr[8];		// compressed data
    u64		usec;			// time of calculation

} cmpr_info_t;

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

static inline void InitializeCmprInfo ( cmpr_info_t *info )
{
    DASSERT(info);
    memset(info,0,sizeof(*info));
    memcpy(info->default_vector,opt_cmpr_def,sizeof(info->default_vector));
};

//-----------------------------------------------------------------------------

void CMPR_wiimm
(
    const u8		*data,		// source data
    cmpr_info_t		*info		// info data structure
);

//-----------------------------------------------------------------------------

void CMPR_close_info
(
    const u8		*data,		// source data
    cmpr_info_t		*info,		// info data structure
    u8			*dest,		// store destination data here (never 0)
    bool		fill_info	// true: fill info with statistics
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			patch interface			///////////////
///////////////////////////////////////////////////////////////////////////////

bool ScanPatchImage ( ccp arg, PatchImage_t * mode );
int ScanOptPatchImage ( ccp arg );

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

bool SetupPatchListIMG(); // returns true if patching enabled
void ResetPatchListIMG();

enumError PatchListIMG
(
    Image_t		* img		// image to patch
);

enumError PatchIMG
(
    Image_t		* dest_img,	// destination image, can be one of the sources
    Image_t		* src_img1,	// base source
    Image_t		* src_img2,	// patch source
    PatchImage_t	patch_mode	// patch mode
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			image lists			///////////////
///////////////////////////////////////////////////////////////////////////////

void PrintImageHead
(
    int			line_indent,	// indention of complete line
    uint		long_count	// set the verbosity
);

//-----------------------------------------------------------------------------

int PrintImage
(
    const void		* data,		// valid data pointer
					// if NULL: force data line
    uint		data_size,	// size of 'data'
    ccp			fname,		// file name for listing
    int			line_indent,	// indention of complete line
    int			fname_indent,	// indention of 'fname'
    uint		long_count,	// set the verbosity
    bool		ignore		// true: ignore non images
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			median cut			///////////////
///////////////////////////////////////////////////////////////////////////////

uint MedianCut
(
    const u32		* data,		// input: 4-tuple aka RGBA
					// data is read at the very beginning
					// and may overlay with 'index' and/or 'pal'
    u16			* index,	// not NULL: store index into palette,
					//           can be the space as 'data'

    // The follwing 3 parameters are used for 'data' and 'index'.
    // They allow to skip unused pixel at the end or each row.
    uint		width,		// used pixels per row
    uint		xwidth,		// >width: pixels per row
    uint		height,		// number or rows

    // pallette data
    u32			* pal,		// not NULL: store calculated palette
    uint		pal_elem	// number of wanted palette entries

    // return value = number of calculated palette values
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    TPL support			///////////////
///////////////////////////////////////////////////////////////////////////////

#define TPL_MAGIC_NUM 0x0020AF30

typedef struct tpl_header_t
{
    u8			magic[4];	// always TPL_MAGIC_NUM
    u32			n_image;	// number of images
    u32			imgtab_off;	// image table offset relative to file start
}
__attribute__ ((packed)) tpl_header_t;

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

typedef struct tpl_imgtab_t
{
    u32			image_off;	// image offset relative to file start
    u32			palette_off;	// palette offset relative to file start
}
__attribute__ ((packed)) tpl_imgtab_t;

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

typedef struct tpl_pal_header_t
{
    u16			n_entry;	// number of palette entries
    u16			unknown1;
    u32			pform;		// image format of palette
    u32			data_off;	// data offset relative to tpl_header_t
}
__attribute__ ((packed)) tpl_pal_header_t;

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

typedef struct tpl_img_header_t
{
    u16			height;		// height of image in pixel
    u16			width;		// width of image in pixel
    u32			iform;		// image format
    u32			data_off;	// data offset relative to tpl_header_t

    // the following is unknown
    // names taken from http://hitmen.c02.at/files/yagcd/yagcd/chap15.html#sec15.35

    u32			wrap_s;		// value 0 or 1
    u32			wrap_t;		// value 0 or 1
    u32			min_filter;	// always 1
    u32			mag_filter;	// always 1
    float32		load_bias;	// always 0
    u8			edge_load;	// always 0
    u8			min_load;	// always 0
    u8			max_load;	// always 0
    u8			unpacked;	// always 0
}
__attribute__ ((packed)) tpl_img_header_t;

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

uint GetNImagesTPL
(
    const u8			* data,		// TPL data
    uint			data_size,	// size of tpl data
    const endian_func_t		* endian	// endian functions
);

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

bool SetupPointerTPL
(
    const u8			* data,		// TPL data
    uint			data_size,	// size of tpl data
    uint			img_index,	// index of image to extract
    const tpl_header_t		** tpl_head,	// not NULL: store pointer here
    const tpl_imgtab_t		** tpl_tab,	// not NULL: store pointer here
    const tpl_pal_header_t	** tpl_pal,	// not NULL: store pointer here
    const tpl_img_header_t	** tpl_img,	// not NULL: store pointer here
    const u8			** pal_data,	// not NULL: store pointer to pal data
    const u8			** img_data,	// not NULL: store pointer to img data
    const endian_func_t		* endian	// endian functions
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    BTI support			///////////////
///////////////////////////////////////////////////////////////////////////////

typedef struct bti_header_t
{
    // --> unknown field names taken from http://www.amnoid.de/gc/bti.txt

    /* 00 */  u8	iform;		// image format
    /* 01 */  u8	unknown_01;	// 02 for 'posteffect.bti', 00 else
    /* 02 */  u16	width;		// width of image in pixel
    /* 04 */  u16	height;		// height of image in pixel

    /* 06 */  u8	wrap_s;		// 01 for 'posteffect.bti', 00 else
    /* 07 */  u8	wrap_t;		// 01 for 'posteffect.bti', 00 else

    /* 08 */  u16	pform;		// image format of palette
    /* 0a */  u16	n_pal;		// number of palette entries
    /* 0c */  u32	pal_off;	// palette offset relative file header

    /* 10 */  u32	unknown_10;	// always 0 in MKWii
    /* 14 */  u8	unknown_14;	// always 1 in MKWii
    /* 15 */  u8	unknown_15;	// always 1 in MKWii
    /* 16 */  u16	unknown_16;	// always 0 in MKWii
    /* 18 */  u8	n_image;	// number of images (main+mipmaps)
    /* 19 */  u8	unknown_19;	// always 0 in MKWii
    /* 1a */  u16	unknown_1a;	// always 0 in MKWii

    /* 1c */  u32	data_off;	// offset of image data relative file header
    /* 20 */
}
__attribute__ ((packed)) bti_header_t;

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

uint GetNImagesBTI
(
    const u8			* data,		// bti data
    uint			data_size	// size of bti data
);

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

#endif // SZS_LIB_IMAGE_H