From ffb766759376b5556a66d97f27e084bc47711b05 Mon Sep 17 00:00:00 2001 From: mpetavy Date: Tue, 6 Mar 2018 12:58:29 +0100 Subject: [PATCH 1/2] improve I/O performance on BMP writing through buffering --- THANKS.md | 1 + src/bin/jp2/convertbmp.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/THANKS.md b/THANKS.md index 460eab10..3334e6ad 100644 --- a/THANKS.md +++ b/THANKS.md @@ -37,3 +37,4 @@ it complete and exempt of errors. * Vincent Torri * Yannick Verschueren * Peter Wimmer +* Marcel Petavy diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 084f70bb..ee2a8d7d 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -955,6 +955,10 @@ int imagetobmp(opj_image_t * image, const char *outfile) adjustB = 0; } + int max = 1024*1024; + OPJ_UINT8 *pBuffer = (OPJ_UINT8 *) calloc(1, max); + int c = 0; + for (i = 0; i < w * h; i++) { OPJ_UINT8 rc, gc, bc; int r, g, b; @@ -995,15 +999,35 @@ int imagetobmp(opj_image_t * image, const char *outfile) } bc = (OPJ_UINT8)b; - fprintf(fdest, "%c%c%c", bc, gc, rc); + if(c >= (max - 3)) { + fwrite(pBuffer,1,c,fdest); + c = 0; + } + + *(pBuffer + c++) = bc; + *(pBuffer + c++) = gc; + *(pBuffer + c++) = rc; if ((i + 1) % w == 0) { for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */ - fprintf(fdest, "%c", 0); + + if(c >= (max - 1)) { + fwrite(pBuffer,1,c,fdest); + c = 0; + } + + *(pBuffer + c++) = 0; } } } + + if(c > 0) { + fwrite(pBuffer,1,c,fdest); + } + fclose(fdest); + + free(pBuffer); } else { /* Gray-scale */ /* -->> -->> -->> -->> From 785199d3513a4c3a6fe43ecc1f93195111f2c76f Mon Sep 17 00:00:00 2001 From: mpetavy Date: Thu, 19 Jul 2018 11:12:19 +0200 Subject: [PATCH 2/2] Fixed compiler warnings (errors) --- src/bin/jp2/convertbmp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index ee2a8d7d..76a5d654 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "openjpeg.h" #include "convert.h" @@ -660,7 +661,8 @@ static OPJ_BOOL bmp_read_rle4_data(FILE* IN, OPJ_UINT8* pData, opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) { - opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */ + opj_image_cmptparm_t cmptparm[4]; /* + imum of 4 components */ OPJ_UINT8 lut_R[256], lut_G[256], lut_B[256]; OPJ_UINT8 const* pLUT[3]; opj_image_t * image = NULL; @@ -862,6 +864,8 @@ int imagetobmp(opj_image_t * image, const char *outfile) int i, pad; FILE *fdest = NULL; int adjustR, adjustG, adjustB; + OPJ_UINT8 *pBuffer = (OPJ_UINT8 *) calloc(1, 1024*1024); + OPJ_SIZE_T c = 0; if (image->comps[0].prec < 8) { fprintf(stderr, "imagetobmp: Unsupported precision: %d\n", @@ -955,10 +959,6 @@ int imagetobmp(opj_image_t * image, const char *outfile) adjustB = 0; } - int max = 1024*1024; - OPJ_UINT8 *pBuffer = (OPJ_UINT8 *) calloc(1, max); - int c = 0; - for (i = 0; i < w * h; i++) { OPJ_UINT8 rc, gc, bc; int r, g, b; @@ -999,7 +999,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) } bc = (OPJ_UINT8)b; - if(c >= (max - 3)) { + if(c >= (malloc_usable_size(pBuffer) - 3)) { fwrite(pBuffer,1,c,fdest); c = 0; } @@ -1011,7 +1011,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) if ((i + 1) % w == 0) { for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) { /* ADD */ - if(c >= (max - 1)) { + if(c >= (malloc_usable_size(pBuffer) - 1)) { fwrite(pBuffer,1,c,fdest); c = 0; }