2014-03-12 02:00:41 -07:00
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
# include <Eina.h>
# include <Evas.h>
# include <Ecore.h>
# include <Ecore_Getopt.h>
# include <Ecore_Evas.h>
2019-11-28 00:59:41 -08:00
# if defined(_WIN32)
2016-12-20 02:00:10 -08:00
# define NO_SIGNAL
2017-09-14 09:59:02 -07:00
# else
# include <signal.h>
2016-12-20 02:00:10 -08:00
# endif
2014-03-12 02:00:41 -07:00
# undef EINA_LOG_DOMAIN_DEFAULT
# define EINA_LOG_DOMAIN_DEFAULT _log_dom
static int _log_dom = - 1 ;
const Ecore_Getopt optdesc = {
" ecore_evas_convert " ,
" %prog [options] <filename-source> <filename-destination> " ,
PACKAGE_VERSION ,
" (C) 2014 Enlightenment " ,
" BSD with advertisement clause " ,
" Simple application to convert image. " ,
0 ,
{
ECORE_GETOPT_STORE_INT ( ' q ' , " quality " , " define encoding quality in percent. " ) ,
ECORE_GETOPT_STORE_TRUE ( ' c ' , " compress " , " define if data should be compressed. " ) ,
2015-10-29 06:14:08 -07:00
ECORE_GETOPT_STORE_STR ( ' e ' , " encoding " , " define the codec (for TGV files: 'etc1', 'etc2', 'etc1+alpha') " ) ,
2019-07-22 01:27:10 -07:00
ECORE_GETOPT_STORE_STR ( ' m ' , " max-geometry " , " define the max size in pixels (WidthxHeight) of any converted image, splitting the image as necessary in a grid pattern ('_XxY' will be appended to the file name) " ) ,
2014-03-12 02:00:41 -07:00
ECORE_GETOPT_LICENSE ( ' L ' , " license " ) ,
ECORE_GETOPT_COPYRIGHT ( ' C ' , " copyright " ) ,
ECORE_GETOPT_VERSION ( ' V ' , " version " ) ,
ECORE_GETOPT_HELP ( ' h ' , " help " ) ,
ECORE_GETOPT_SENTINEL
}
} ;
2016-12-20 02:00:10 -08:00
typedef struct _Save_Job {
2015-04-07 00:48:49 -07:00
const char * output ;
2019-07-22 01:27:10 -07:00
const char * extension ;
2015-04-07 00:48:49 -07:00
const char * flags ;
Evas_Object * im ;
2016-12-20 02:00:10 -08:00
int ret ;
} Save_Job ;
2019-07-22 01:27:10 -07:00
static Save_Job job = { NULL , NULL , NULL , NULL , - 1 } ;
static unsigned int width = 0 , height = 0 ;
static unsigned int x = 0 , y = 0 ;
static int image_w , image_h ;
2015-04-07 00:48:49 -07:00
static void
2016-12-20 02:00:10 -08:00
_save_do ( void * data EINA_UNUSED )
2015-04-07 00:48:49 -07:00
{
2019-07-22 01:27:10 -07:00
const char * output = job . output ;
2016-12-20 02:00:10 -08:00
job . ret = 0 ;
2019-09-26 14:59:38 -07:00
if ( width & & height )
2019-07-22 01:27:10 -07:00
{
Eina_Slstr * str ;
str = eina_slstr_printf ( " %s_%ux%u.%s " , output , x / width , y / height , job . extension ) ;
output = str ;
evas_object_image_load_region_set ( job . im , x , y , width , height ) ;
x + = width ;
if ( ( int ) x > image_w )
{
y + = height ;
x = 0 ;
}
}
fprintf ( stderr , " Saving image '%s' \n " , output ) ;
if ( ! evas_object_image_save ( job . im , output , NULL , job . flags ) )
2015-04-07 00:48:49 -07:00
{
2016-12-20 02:00:10 -08:00
EINA_LOG_ERR ( " Could not convert file to '%s'. " , job . output ) ;
job . ret = 1 ;
2015-04-07 00:48:49 -07:00
}
2019-07-22 01:27:10 -07:00
if ( ! width | | ( ( int ) y > image_h ) ) ecore_main_loop_quit ( ) ;
else ecore_job_add ( _save_do , NULL ) ;
2015-04-07 00:48:49 -07:00
}
2016-12-20 02:00:10 -08:00
# ifndef NO_SIGNAL
static void
_sigint ( int sig EINA_UNUSED )
2015-04-07 00:48:49 -07:00
{
2016-12-20 02:00:10 -08:00
EINA_LOG_ERR ( " Image save interrupted by SIGINT: '%s'. " , job . output ) ;
// eina_file_unlink
unlink ( job . output ) ;
exit ( - 1 ) ;
2015-04-07 00:48:49 -07:00
}
2016-12-20 02:00:10 -08:00
# endif
2015-04-07 00:48:49 -07:00
2014-03-12 02:00:41 -07:00
int
main ( int argc , char * argv [ ] )
{
Ecore_Evas * ee ;
Evas * e ;
Evas_Object * im ;
int arg_index ;
2014-04-18 03:02:28 -07:00
int quality = - 1 ;
2014-03-12 02:00:41 -07:00
int r = - 1 ;
2014-06-12 23:22:26 -07:00
char * encoding = NULL ;
2019-07-22 01:27:10 -07:00
char * maxgeometry = NULL ;
2014-03-12 02:00:41 -07:00
Eina_Bool compress = 1 ;
Eina_Bool quit_option = EINA_FALSE ;
2014-06-12 23:22:26 -07:00
Eina_Strbuf * flags = NULL ;
2014-03-12 02:00:41 -07:00
Ecore_Getopt_Value values [ ] = {
ECORE_GETOPT_VALUE_INT ( quality ) ,
ECORE_GETOPT_VALUE_BOOL ( compress ) ,
2014-06-12 23:22:26 -07:00
ECORE_GETOPT_VALUE_STR ( encoding ) ,
2019-07-22 01:27:10 -07:00
ECORE_GETOPT_VALUE_STR ( maxgeometry ) ,
2014-03-12 02:00:41 -07:00
ECORE_GETOPT_VALUE_BOOL ( quit_option ) ,
ECORE_GETOPT_VALUE_BOOL ( quit_option ) ,
ECORE_GETOPT_VALUE_BOOL ( quit_option ) ,
ECORE_GETOPT_VALUE_BOOL ( quit_option ) ,
ECORE_GETOPT_VALUE_NONE
} ;
eina_init ( ) ;
_log_dom = eina_log_domain_register ( argv [ 0 ] , EINA_COLOR_CYAN ) ;
ecore_evas_init ( ) ;
arg_index = ecore_getopt_parse ( & optdesc , values , argc , argv ) ;
if ( quit_option ) goto end ;
if ( arg_index < 0 )
{
EINA_LOG_ERR ( " Could not parse argument. " ) ;
goto end ;
}
if ( arg_index + 2 ! = argc )
{
EINA_LOG_ERR ( " File not correctly specified. " ) ;
goto end ;
}
2019-07-22 01:27:10 -07:00
if ( maxgeometry )
{
if ( sscanf ( maxgeometry , " %ux%u " , & width , & height ) ! = 2 )
{
EINA_LOG_ERR ( " max-geometry should be specified as WidthxHeight, like 1920x1280. " ) ;
goto end ;
}
if ( width = = 0 | | height = = 0 )
{
EINA_LOG_ERR ( " max-geometry width and height must be greater than 0. " ) ;
goto end ;
}
}
2014-03-12 02:00:41 -07:00
ee = ecore_evas_buffer_new ( 1 , 1 ) ;
e = ecore_evas_get ( ee ) ;
if ( ! e )
{
EINA_LOG_ERR ( " Impossible to create a canvas to do the conversion. " ) ;
goto end ;
}
2014-06-12 23:22:26 -07:00
flags = eina_strbuf_new ( ) ;
eina_strbuf_append_printf ( flags , " compress=%d " , compress ) ;
2014-04-18 03:02:28 -07:00
if ( quality > = 0 )
2014-06-12 23:22:26 -07:00
eina_strbuf_append_printf ( flags , " quality=%d " , quality ) ;
if ( encoding )
eina_strbuf_append_printf ( flags , " encoding=%s " , encoding ) ;
2014-03-12 02:00:41 -07:00
im = evas_object_image_add ( e ) ;
evas_object_image_file_set ( im , argv [ arg_index ] , NULL ) ;
if ( evas_object_image_load_error_get ( im ) ! = EVAS_LOAD_ERROR_NONE )
{
EINA_LOG_ERR ( " Could not open '%s'. Error was \" %s \" . " ,
argv [ arg_index ] ,
evas_load_error_str ( evas_object_image_load_error_get ( im ) ) ) ;
goto end ;
}
2019-07-22 01:27:10 -07:00
evas_object_image_size_get ( im , & image_w , & image_h ) ;
2016-12-20 02:00:10 -08:00
# ifndef NO_SIGNAL
// Brutal way of
signal ( SIGINT , _sigint ) ;
# endif
2019-07-22 01:27:10 -07:00
// Find the extension and remove it
if ( width | | height )
{
char * tmp = strrchr ( argv [ arg_index + 1 ] , ' . ' ) ;
if ( tmp ) * tmp = ' \0 ' ;
if ( tmp ) job . extension = tmp + 1 ;
}
2015-04-07 00:48:49 -07:00
job . output = argv [ arg_index + 1 ] ;
job . flags = eina_strbuf_string_get ( flags ) ;
job . im = im ;
2016-12-20 02:00:10 -08:00
ecore_job_add ( _save_do , NULL ) ;
2015-04-07 00:48:49 -07:00
ecore_main_loop_begin ( ) ;
r = job . ret ;
2014-03-12 02:00:41 -07:00
end :
2014-06-12 23:22:26 -07:00
if ( flags ) eina_strbuf_free ( flags ) ;
2014-03-12 02:00:41 -07:00
ecore_evas_shutdown ( ) ;
2020-09-25 02:30:20 -07:00
eina_log_domain_unregister ( _log_dom ) ;
eina_shutdown ( ) ;
2014-03-12 02:00:41 -07:00
return r ;
}