2025-04-19 03:31:30 +05:00
# include <algorithm>
2024-11-22 15:02:12 +00:00
# include <print>
# include <format>
# include <filesystem>
2025-07-03 21:07:39 +02:00
# include <fstream>
# include <vector>
2024-11-22 15:02:12 +00:00
# include <hyprgraphics/image/Image.hpp>
# include "shared.hpp"
using namespace Hyprgraphics ;
2025-07-03 21:07:39 +02:00
static bool tryLoadImageFromFile ( const std : : string & path ) {
2025-09-30 19:31:50 +01:00
auto image = path . ends_with ( " svg " ) ? CImage ( path , { 512 , 512 } ) : CImage ( path ) ;
2024-11-22 15:02:12 +00:00
if ( ! image . success ( ) ) {
std : : println ( " Failed to load {}: {} " , path , image . getError ( ) ) ;
return false ;
}
std : : println ( " Loaded {} successfully: Image is {}x{} of type {} " , path , image . cairoSurface ( ) - > size ( ) . x , image . cairoSurface ( ) - > size ( ) . y , image . getMime ( ) ) ;
2025-01-27 13:19:24 +00:00
const auto TEST_DIR = std : : filesystem : : current_path ( ) . string ( ) + " /test_output " ;
// try to write it for inspection
if ( ! std : : filesystem : : exists ( TEST_DIR ) )
std : : filesystem : : create_directory ( TEST_DIR ) ;
std : : string name = image . getMime ( ) ;
2025-04-19 03:31:30 +05:00
std : : ranges : : replace ( name , ' / ' , ' _ ' ) ;
2025-01-27 13:19:24 +00:00
2025-04-19 03:31:30 +05:00
//NOLINTNEXTLINE
2025-01-27 13:19:24 +00:00
return cairo_surface_write_to_png ( image . cairoSurface ( ) - > cairo ( ) , ( TEST_DIR + " / " + name + " .png " ) . c_str ( ) ) = = CAIRO_STATUS_SUCCESS ;
2024-11-22 15:02:12 +00:00
}
2025-08-20 09:30:02 +01:00
static bool tryLoadImageFromBuffer ( const std : : span < uint8_t > & data , eImageFormat format ) {
auto image = CImage ( data , format ) ;
2025-07-03 21:07:39 +02:00
if ( ! image . success ( ) ) {
std : : println ( " Failed to load embedded image: {} " , image . getError ( ) ) ;
return false ;
}
std : : println ( " Loaded embedded Image successfully: Image is {}x{} of type {} " , image . cairoSurface ( ) - > size ( ) . x , image . cairoSurface ( ) - > size ( ) . y , image . getMime ( ) ) ;
const auto TEST_DIR = std : : filesystem : : current_path ( ) . string ( ) + " /test_output " ;
// try to write it for inspection
if ( ! std : : filesystem : : exists ( TEST_DIR ) )
std : : filesystem : : create_directory ( TEST_DIR ) ;
std : : string name = image . getMime ( ) + " _embedded " ;
std : : ranges : : replace ( name , ' / ' , ' _ ' ) ;
//NOLINTNEXTLINE
return cairo_surface_write_to_png ( image . cairoSurface ( ) - > cairo ( ) , ( TEST_DIR + " / " + name + " .png " ) . c_str ( ) ) = = CAIRO_STATUS_SUCCESS ;
}
2025-07-06 15:14:40 +02:00
static std : : vector < uint8_t > getImageBuffer ( const std : : string & path ) {
2025-07-03 21:07:39 +02:00
std : : vector < uint8_t > buffer ;
2025-08-20 09:30:02 +01:00
std : : ifstream file ( path , std : : ios : : binary | std : : ios : : ate ) ;
2025-07-03 21:07:39 +02:00
std : : streamsize size = file . tellg ( ) ;
file . seekg ( 0 , std : : ios : : beg ) ;
buffer . resize ( size ) ;
file . read ( reinterpret_cast < char * > ( buffer . data ( ) ) , size ) ;
return buffer ;
}
2024-11-22 15:02:12 +00:00
int main ( int argc , char * * argv , char * * envp ) {
int ret = 0 ;
for ( auto & file : std : : filesystem : : directory_iterator ( " ./resource/images/ " ) ) {
if ( ! file . is_regular_file ( ) )
continue ;
2025-01-05 17:14:50 -05:00
auto expectation = true ;
# ifndef JXL_FOUND
2025-01-27 13:19:24 +00:00
if ( file . path ( ) . filename ( ) = = " hyprland.jxl " )
expectation = false ;
2025-08-20 09:30:02 +01:00
# endif
# ifndef HEIF_FOUND
if ( file . path ( ) . filename ( ) = = " hyprland.avif " )
expectation = false ;
2025-01-05 17:14:50 -05:00
# endif
2025-07-03 21:07:39 +02:00
EXPECT ( tryLoadImageFromFile ( file . path ( ) ) , expectation ) ;
2024-11-22 15:02:12 +00:00
}
2025-08-20 09:30:02 +01:00
auto pngBuffer = getImageBuffer ( " ./resource/images/hyprland.png " ) ;
EXPECT ( tryLoadImageFromBuffer ( pngBuffer , Hyprgraphics : : IMAGE_FORMAT_PNG ) , true ) ;
# ifdef HEIF_FOUND
auto avifBuffer = getImageBuffer ( " ./resource/images/hyprland.avif " ) ;
EXPECT ( tryLoadImageFromBuffer ( avifBuffer , Hyprgraphics : : IMAGE_FORMAT_AVIF ) , true ) ;
# endif
2025-07-03 21:07:39 +02:00
2024-11-22 15:02:12 +00:00
return ret ;
}