5454
5555static void
5656gpujpeg_cuda_free_host (void * ptr );
57+ static void *
58+ gpujpeg_cuda_realloc_sized_host (void * ptr , int oldsz , int newsz );
5759#define STBI_NO_JPEG
58- #define STBI_NO_PNG
5960#define STBI_NO_PSD
6061#define STBI_NO_GIF
6162#define STBI_NO_HDR
@@ -64,7 +65,7 @@ gpujpeg_cuda_free_host(void* ptr);
6465// we want use custom allocator but only way to do this in stbi is to define the below
6566#define STBI_MALLOC gpujpeg_cuda_malloc_host
6667#define STBI_FREE gpujpeg_cuda_free_host
67- #define STBI_REALLOC_SIZED unneeded_so_undefined
68+ #define STBI_REALLOC_SIZED gpujpeg_cuda_realloc_sized_host
6869#define STB_IMAGE_IMPLEMENTATION
6970#include "stb_image.h"
7071
@@ -79,7 +80,6 @@ gpujpeg_cuda_free_host(void* ptr)
7980 GPUJPEG_CHECK_EX (cudaFreeHost (ptr ), "Could not free host pointer" , );
8081}
8182
82- #if 0
8383static void *
8484gpujpeg_cuda_realloc_sized_host (void * ptr , int oldsz , int newsz )
8585{
@@ -91,7 +91,6 @@ gpujpeg_cuda_realloc_sized_host(void* ptr, int oldsz, int newsz)
9191 gpujpeg_cuda_free_host (ptr );
9292 return nptr ;
9393}
94- #endif
9594
9695static int
9796stbi_load_delegate (const char * filename , size_t * image_size , void * * image_data , allocator_t alloc )
@@ -435,24 +434,35 @@ tst_image_probe_delegate(const char* filename, enum gpujpeg_image_file_format fo
435434static int
436435stbi_save_delegate (const char * filename , const struct gpujpeg_image_parameters * param_image , const char * data )
437436{
438- int (* write_func )(char const * filename , int x , int y , int comp , const void * data ) = NULL ;
437+ union {
438+ int (* func )(char const * filename , int x , int y , int comp , const void * data );
439+ int (* func_stridden )(char const * filename , int x , int y , int comp , const void * data , int stride_bytes );
440+ } write = {NULL };
441+ const int comp_count = gpujpeg_pixel_format_get_comp_count (param_image -> pixel_format );
442+ int stride_bytes = 0 ;
439443 const char * ext = strrchr (filename , '.' );
440444 if ( ext == NULL ) {
441445 ERROR_MSG ("[stbi] Output file %s doesn't contain extension!\n" , filename );
442446 }
443447 ext += 1 ;
444448 if ( strcasecmp (ext , "bmp" ) == 0 ) {
445- write_func = stbi_write_bmp ;
449+ write .func = stbi_write_bmp ;
450+ }
451+ else if ( strcasecmp (ext , "png" ) == 0 ) {
452+ write .func_stridden = stbi_write_png ;
453+ stride_bytes = param_image -> width * comp_count ;
446454 }
447455 else if ( strcasecmp (ext , "tga" ) == 0 ) {
448- write_func = stbi_write_tga ;
456+ write . func = stbi_write_tga ;
449457 }
450458 else {
451459 ERROR_MSG ("[stbi] Unhandled file %s extension!\n" , filename );
452460 return -1 ;
453461 }
454- if ( write_func (filename , param_image -> width , param_image -> height ,
455- gpujpeg_pixel_format_get_comp_count (param_image -> pixel_format ), data ) ) {
462+ const int rc = stride_bytes == 0 ? write .func (filename , param_image -> width , param_image -> height , comp_count , data )
463+ : write .func_stridden (filename , param_image -> width , param_image -> height ,
464+ comp_count , data , stride_bytes );
465+ if ( rc ) {
456466 return 0 ;
457467 }
458468 ERROR_MSG ("[stbi] Cannot write output file %s: %s\n" , filename , stbi_failure_reason ());
@@ -567,6 +577,7 @@ tst_image_load_delegate(const char* filename, size_t* image_size, void** image_d
567577image_load_delegate_t gpujpeg_get_image_load_delegate (enum gpujpeg_image_file_format format ) {
568578 switch (format ) {
569579 case GPUJPEG_IMAGE_FILE_BMP :
580+ case GPUJPEG_IMAGE_FILE_PNG :
570581 case GPUJPEG_IMAGE_FILE_TGA :
571582 return stbi_load_delegate ;
572583 case GPUJPEG_IMAGE_FILE_PGM :
@@ -596,6 +607,7 @@ image_probe_delegate_t gpujpeg_get_image_probe_delegate(enum gpujpeg_image_file_
596607{
597608 switch (format ) {
598609 case GPUJPEG_IMAGE_FILE_BMP :
610+ case GPUJPEG_IMAGE_FILE_PNG :
599611 case GPUJPEG_IMAGE_FILE_TGA :
600612 return stbi_image_probe_delegate ;
601613 case GPUJPEG_IMAGE_FILE_PGM :
@@ -625,6 +637,7 @@ image_save_delegate_t gpujpeg_get_image_save_delegate(enum gpujpeg_image_file_fo
625637{
626638 switch (format ) {
627639 case GPUJPEG_IMAGE_FILE_BMP :
640+ case GPUJPEG_IMAGE_FILE_PNG :
628641 case GPUJPEG_IMAGE_FILE_TGA :
629642 return stbi_save_delegate ;
630643 case GPUJPEG_IMAGE_FILE_PAM :
0 commit comments