2020-02-29 14:55:16 +01:00
# include <cstring>
# include <cstdlib>
# include <cstdio>
2012-10-26 10:45:51 +02:00
2020-10-04 20:48:47 +02:00
# include "Common/Data/Format/PNGLoad.h"
# include "Common/Data/Format/ZIMLoad.h"
# include "Common/Data/Format/ZIMSave.h"
2012-10-26 10:45:51 +02:00
2020-09-29 12:44:47 +02:00
# include "Common/Common.h"
2012-10-26 10:45:51 +02:00
char magic [ 5 ] = " ZIMG " ;
2020-02-29 14:55:16 +01:00
const char * format_strings [ 4 ] = { " 8888 " , " 4444 " , " 565 " , " ETC1 " } ;
2020-02-29 17:45:58 +01:00
int formats [ 3 ] = { ZIM_RGBA8888 , ZIM_RGBA4444 , ZIM_RGB565 } ;
2012-10-26 10:45:51 +02:00
void printusage ( ) {
2021-05-16 09:39:39 -07:00
fprintf ( stderr , " Usage: zimtool infile.png outfile.zim [-f=FORMAT] [-m] [-g] [-z[LEVEL]] \n " ) ;
2020-02-29 14:55:16 +01:00
fprintf ( stderr , " Formats: 8888 4444 565 ETC1 \n " ) ;
2012-10-26 10:45:51 +02:00
}
int filesize ( const char * filename ) {
2020-02-29 14:55:16 +01:00
FILE * f = fopen ( filename , " rb " ) ;
fseek ( f , 0 , SEEK_END ) ;
int sz = ftell ( f ) ;
fclose ( f ) ;
return sz ;
2012-10-26 10:45:51 +02:00
}
int main ( int argc , char * * argv ) {
2020-02-29 14:55:16 +01:00
// Parse command line arguments.
const char * FLAGS_infile ;
const char * FLAGS_outfile ;
if ( argc > = 3 ) {
FLAGS_infile = argv [ 1 ] ;
FLAGS_outfile = argv [ 2 ] ;
} else {
fprintf ( stderr , " ERROR: Not enough parameters. \n " ) ;
printusage ( ) ;
return 1 ;
}
2012-10-26 10:45:51 +02:00
2020-02-29 14:55:16 +01:00
int flags = 0 ;
2021-05-16 09:39:39 -07:00
int level = 0 ;
2020-02-29 14:55:16 +01:00
bool format_set = false ;
for ( int i = 3 ; i < argc ; i + + ) {
if ( argv [ i ] [ 0 ] ! = ' - ' ) {
fprintf ( stderr , " Additional arguments must start with '-' \n " ) ;
return 1 ;
}
switch ( argv [ i ] [ 1 ] ) {
case ' m ' :
flags | = ZIM_HAS_MIPS ;
// Generates mips directly here. We can generate gamma
// corrected mips, and mips for ETC1.
break ;
case ' g ' :
flags | = ZIM_GEN_MIPS ;
break ;
case ' c ' :
flags | = ZIM_CLAMP ;
break ;
case ' f ' :
{
for ( int j = 0 ; j < 4 ; j + + ) {
if ( ! strcmp ( format_strings [ j ] , argv [ i ] + 3 ) ) {
flags | = j ;
format_set = true ;
}
}
}
break ;
2021-05-16 09:39:39 -07:00
case ' z ' :
flags | = ZIM_ZSTD_COMPRESSED ;
if ( argv [ i ] [ 2 ] ! = ' \0 ' ) {
int pos = 2 ;
while ( argv [ i ] [ pos ] > = ' 0 ' & & argv [ i ] [ pos ] < = ' 9 ' ) {
level = level * 10 + argv [ i ] [ pos ] - ' 0 ' ;
pos + + ;
}
}
break ;
2020-02-29 14:55:16 +01:00
}
}
2013-03-29 13:53:04 +01:00
// TODO: make setting?
flags | = ZIM_ETC1_MEDIUM ;
2020-02-29 14:55:16 +01:00
if ( ! format_set ) {
fprintf ( stderr , " Must set format \n " ) ;
printusage ( ) ;
return 1 ;
}
2012-10-26 10:45:51 +02:00
2020-02-29 14:55:16 +01:00
uint8_t * image_data ;
int width , height ;
2020-10-04 20:48:47 +02:00
if ( 1 ! = pngLoad ( FLAGS_infile , & width , & height , & image_data ) ) {
2020-02-29 14:55:16 +01:00
fprintf ( stderr , " Input not a PNG file \n " ) ;
printusage ( ) ;
return 1 ;
}
2020-02-29 15:24:22 +01:00
FILE * f = fopen ( FLAGS_outfile , " wb " ) ;
2021-05-16 09:39:39 -07:00
SaveZIM ( f , width , height , width * 4 , flags , image_data , level ) ;
2020-02-29 15:24:22 +01:00
fclose ( f ) ;
2020-02-29 14:55:16 +01:00
int in_file_size = filesize ( FLAGS_infile ) ;
int out_file_size = filesize ( FLAGS_outfile ) ;
fprintf ( stdout , " Converted %s to %s. %i b to %i b. %ix%i, %s. \n " , FLAGS_infile , FLAGS_outfile , in_file_size , out_file_size , width , height , format_strings [ flags & ZIM_FORMAT_MASK ] ) ;
return 0 ;
2012-10-26 10:45:51 +02:00
}