rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 237771176e55b35a4c9ef9eec966c11522cbdf52
parent 1b44014de972e34812f635959c85e24d16755eed
Author: vaplv <vincent.forest@meso-star.com>
Date:   Thu, 30 Mar 2017 11:48:02 +0200

Refactor the image_ppm_write_stream function

An intermediary buffer was used to format the line priorly to their
writing into the stream. There is apparently no reason to do this. This
commit removes this zealous process and write the line directly into the
stream with fprintf.

Diffstat:
Msrc/image.c | 31++++++++++++-------------------
1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/src/image.c b/src/image.c @@ -245,7 +245,7 @@ image_ppm_write_stream const int Bpp, const unsigned char* buffer) { - char buf[BUFSIZ]; + int i; res_T res = RES_OK; if(!width || !height || !Bpp || !buffer || !fp) { @@ -253,19 +253,11 @@ image_ppm_write_stream goto error; } - #define FWRITE(Fp, String) \ - { \ - const size_t i = fwrite(String, sizeof(char), strlen(String), Fp); \ - if(i != strlen(String) * sizeof(char)) { res = RES_MEM_ERR; goto error; }\ - } (void)0 - #define SNPRINTF(Buf, Sz, Str, Arg0, Arg1, Arg2) \ - { \ - const int i = snprintf(Buf, Sz, Str, Arg0, Arg1, Arg2); \ - if( i >= BUFSIZ ) { res = RES_MEM_ERR; goto error; } \ - } (void)0 - - SNPRINTF(buf, BUFSIZ, "P3\n\n%i %i\n%i\n", width, height, 255); - FWRITE(fp, buf); + i = fprintf(fp, "P3\n\n%i %i\n%i\n", width, height, 255); + if(i < 0) { + res = RES_IO_ERR; + goto error; + } if(Bpp) { const long pitch = width * Bpp; @@ -274,17 +266,18 @@ image_ppm_write_stream const unsigned char* row = buffer + y * pitch; for(x = 0; x < width; ++x) { const unsigned char* pixel = row + x * Bpp; - SNPRINTF(buf, BUFSIZ, "%u %u %u\n", + i = fprintf(fp, "%u %u %u\n", pixel[0], Bpp > 1 ? pixel[1] : pixel[0], Bpp > 2 ? pixel[2] : pixel[0]); - FWRITE(fp, buf); + if(i < 0) { + res = RES_IO_ERR; + goto error; + } } - FWRITE(fp, "\n"); + fprintf(fp, "\n"); } } - #undef SNPRINTF - #undef FWRITE exit: return res;