Skip to content

Commit 0871ce9

Browse files
committed
support for png
Do not advertise much yet - it is not much tested and it can be eg. slow (the same _may_ apply also for other stbi-backed formats /BMP+TGA/ but since png is non-trivially /not just RLE/ compressed, it may be slowish).
1 parent eab7971 commit 0871ce9

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

libgpujpeg/gpujpeg_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ enum gpujpeg_image_file_format {
325325
/// RGBA file format, simple data format without header [R G B A] [R G B A] ...
326326
GPUJPEG_IMAGE_FILE_RGBA,
327327
GPUJPEG_IMAGE_FILE_BMP,
328+
GPUJPEG_IMAGE_FILE_PNG,
328329
GPUJPEG_IMAGE_FILE_TGA,
329330
/// PNM file format
330331
GPUJPEG_IMAGE_FILE_PGM,

src/gpujpeg_common.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ gpujpeg_image_get_file_format(const char* filename)
393393
{ "jpeg", GPUJPEG_IMAGE_FILE_JPEG},
394394
{ "jfif", GPUJPEG_IMAGE_FILE_JPEG},
395395
{ "bmp", GPUJPEG_IMAGE_FILE_BMP},
396+
{ "png", GPUJPEG_IMAGE_FILE_BMP},
396397
{ "tga", GPUJPEG_IMAGE_FILE_TGA},
397398
//{ "pbm", GPUJPEG_IMAGE_FILE_PNM},
398399
{ "pnm", GPUJPEG_IMAGE_FILE_PNM},
@@ -440,6 +441,7 @@ static enum { FF_CS_NONE, FF_CS_RGB, FF_CS_YCBCR } get_file_type_cs(enum gpujpeg
440441
case GPUJPEG_IMAGE_FILE_RGB:
441442
case GPUJPEG_IMAGE_FILE_RGBA:
442443
case GPUJPEG_IMAGE_FILE_BMP:
444+
case GPUJPEG_IMAGE_FILE_PNG:
443445
case GPUJPEG_IMAGE_FILE_TGA:
444446
case GPUJPEG_IMAGE_FILE_PGM:
445447
case GPUJPEG_IMAGE_FILE_PPM:
@@ -1287,6 +1289,7 @@ gpujpeg_image_get_properties(const char *filename, struct gpujpeg_image_paramete
12871289
param_image->pixel_format = GPUJPEG_420_U8_P0P1P2;
12881290
break;
12891291
case GPUJPEG_IMAGE_FILE_BMP:
1292+
case GPUJPEG_IMAGE_FILE_PNG:
12901293
case GPUJPEG_IMAGE_FILE_TGA:
12911294
case GPUJPEG_IMAGE_FILE_PAM:
12921295
case GPUJPEG_IMAGE_FILE_PGM:

src/utils/image_delegate.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@
5454

5555
static void
5656
gpujpeg_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
8383
static void*
8484
gpujpeg_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

9695
static int
9796
stbi_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
435434
static int
436435
stbi_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
567577
image_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

Comments
 (0)