From 5e844ffe70ca88fe56777e73c4fe50b24cb722bd Mon Sep 17 00:00:00 2001 From: John Tullos Date: Mon, 1 Jan 2018 18:08:30 -0600 Subject: [PATCH 1/4] Using secure versions of CRT calls to avoid Microsoft Visual C/C++ compiler errors/warnings. --- stb_image_write.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/stb_image_write.h b/stb_image_write.h index 9d553e0..17f9f3f 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -230,7 +230,12 @@ static void stbi__stdio_write(void *context, void *data, int size) static int stbi__start_write_file(stbi__write_context *s, const char *filename) { - FILE *f = fopen(filename, "wb"); + FILE *f; +#ifdef _MSC_VER + fopen_s(&f, filename, "wb"); +#else + f = fopen(filename, "wb"); +#endif stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); return f != NULL; } @@ -626,7 +631,11 @@ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, f char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; s->func(s->context, header, sizeof(header)-1); +#ifdef _MSC_VER + len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); +#else len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); +#endif s->func(s->context, buffer, len); for(i=0; i < y; i++) @@ -1010,7 +1019,11 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const int len; unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; +#ifdef _MSC_VER + fopen_s(&f, filename, "wb"); +#else f = fopen(filename, "wb"); +#endif if (!f) { STBIW_FREE(png); return 0; } fwrite(png, 1, len, f); fclose(f); From 32a7d5ab68bd35455b24aa852d6d6b7d0f55b494 Mon Sep 17 00:00:00 2001 From: John Tullos Date: Mon, 1 Jan 2018 18:54:26 -0600 Subject: [PATCH 2/4] Added STBI_MSC_SECURE_CRT to support newer MSVC compilers as optional For issue #533 --- stb_image_write.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stb_image_write.h b/stb_image_write.h index 17f9f3f..b977440 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -10,6 +10,11 @@ Will probably not work correctly with strict-aliasing optimizations. + If using a modern Microsoft Compiler non-safe versions of CRT calls may cause + compilation warnings or even errors. To avoid this, aldo before #including, + + #define STBI_MSC_SECURE_CRT + ABOUT: This header file is a library for writing images to C stdio. It could be @@ -231,7 +236,7 @@ static void stbi__stdio_write(void *context, void *data, int size) static int stbi__start_write_file(stbi__write_context *s, const char *filename) { FILE *f; -#ifdef _MSC_VER +#ifdef STBI_MSC_SECURE_CRT fopen_s(&f, filename, "wb"); #else f = fopen(filename, "wb"); @@ -631,7 +636,7 @@ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, f char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; s->func(s->context, header, sizeof(header)-1); -#ifdef _MSC_VER +#ifdef STBI_MSC_SECURE_CRT len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); #else len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); @@ -1019,7 +1024,7 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const int len; unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); if (png == NULL) return 0; -#ifdef _MSC_VER +#ifdef STBI_MSC_SECURE_CRT fopen_s(&f, filename, "wb"); #else f = fopen(filename, "wb"); From eb17d8a6dd32a7b6ce8f345c75505df423be071a Mon Sep 17 00:00:00 2001 From: John Tullos Date: Mon, 1 Jan 2018 18:56:36 -0600 Subject: [PATCH 3/4] Fixed grammar, spelling issues in comments --- stb_image_write.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_image_write.h b/stb_image_write.h index b977440..2e644cf 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -10,8 +10,8 @@ Will probably not work correctly with strict-aliasing optimizations. - If using a modern Microsoft Compiler non-safe versions of CRT calls may cause - compilation warnings or even errors. To avoid this, aldo before #including, + If using a modern Microsoft Compiler, non-safe versions of CRT calls may cause + compilation warnings or even errors. To avoid this, also before #including, #define STBI_MSC_SECURE_CRT From 841862a6225c891d32d194267ba4b81f1499755a Mon Sep 17 00:00:00 2001 From: John Tullos Date: Mon, 1 Jan 2018 18:56:36 -0600 Subject: [PATCH 4/4] Fixed grammar, spelling issues in comments issue #533 --- stb_image_write.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_image_write.h b/stb_image_write.h index b977440..2e644cf 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -10,8 +10,8 @@ Will probably not work correctly with strict-aliasing optimizations. - If using a modern Microsoft Compiler non-safe versions of CRT calls may cause - compilation warnings or even errors. To avoid this, aldo before #including, + If using a modern Microsoft Compiler, non-safe versions of CRT calls may cause + compilation warnings or even errors. To avoid this, also before #including, #define STBI_MSC_SECURE_CRT