libsuperderpy/cmake/ImgToWebp.cmake
Sebastian Krzyszkowiak e89f654559
ImgToWebp: add ability to use lossless compression
Good for pixel art
2019-01-24 23:57:23 +01:00

42 lines
1.6 KiB
CMake

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
include(AssetCache)
find_program(CONVERT NAMES convert NO_CMAKE_FIND_ROOT_PATH)
if(CONVERT)
file(GLOB_RECURSE IMAGE_FILES RELATIVE ${DATADIR} ${DATADIR}/*.png ${DATADIR}/*.jpg ${DATADIR}/*.JPG ${DATADIR}/*.webp)
message(STATUS "ImgToWebp engaging... (using ${CONVERT})")
foreach(file IN LISTS IMAGE_FILES)
if (NOT file MATCHES "^icons/")
message(STATUS ${file})
string(REGEX REPLACE "^(.+)(\\.[^.]+)$" "\\1" filepath ${file})
if (RESIZE)
set(resizecmd -resize ${RESIZE})
else()
set(resizecmd "")
endif()
if (LOSSLESS)
set(losslesscmd -define webp:lossless=true)
else()
set(losslesscmd "")
endif()
GetFromAssetCache(${CACHE} ${DATADIR}/${file} "${LOSSLESS}-${QUALITY}-${RESIZE}-${PARAMS}" CACHED HASH)
if (CACHED)
file(REMOVE ${DATADIR}/${file})
configure_file(${CACHED} ${DATADIR}/${filepath}.wbp COPYONLY)
unset(CACHED)
else()
separate_arguments(PARAMS)
set(CONVERT_CMD ${CONVERT} ${file} -quality ${QUALITY} ${PARAMS} ${resizecmd} ${losslesscmd} webp:${filepath}.wbp)
execute_process(COMMAND ${CONVERT_CMD} WORKING_DIRECTORY ${DATADIR} RESULT_VARIABLE CONVERT_RESULT)
if(CONVERT_RESULT)
message(WARNING "ERROR: ${CONVERT_RESULT}")
else()
file(REMOVE ${DATADIR}/${file})
AddToAssetCache(${CACHE} ${HASH} "${LOSSLESS}-${QUALITY}-${RESIZE}-${PARAMS}" "${DATADIR}/${filepath}.wbp")
endif()
endif()
endif()
endforeach(file)
else(CONVERT)
message(WARNING "ImgToWebp: can't find convert!")
endif(CONVERT)