From c4d84efd244a5c761210f9e508c6013103e5be72 Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.anjos@idiap.ch> Date: Fri, 22 Dec 2017 11:57:06 +0100 Subject: [PATCH 1/2] Add tests to detect the problem reported on this issue --- bob/io/image/data/test_corrupted.pbm | 3 +++ bob/io/image/data/test_corrupted.pgm | Bin 0 -> 37 bytes bob/io/image/data/test_corrupted.ppm | Bin 0 -> 85 bytes bob/io/image/test.py | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 bob/io/image/data/test_corrupted.pbm create mode 100644 bob/io/image/data/test_corrupted.pgm create mode 100644 bob/io/image/data/test_corrupted.ppm diff --git a/bob/io/image/data/test_corrupted.pbm b/bob/io/image/data/test_corrupted.pbm new file mode 100644 index 0000000..5d28f04 --- /dev/null +++ b/bob/io/image/data/test_corrupted.pbm @@ -0,0 +1,3 @@ +P4 +4 6 +�@ @a diff --git a/bob/io/image/data/test_corrupted.pgm b/bob/io/image/data/test_corrupted.pgm new file mode 100644 index 0000000000000000000000000000000000000000..8f34579e2f6c6710677b5689d15099638912a948 GIT binary patch literal 37 lcmWGA<uXw)<1#We<r4b)|Nno6{|MqA!~g$=fBz?P0RTam6kz}W literal 0 HcmV?d00001 diff --git a/bob/io/image/data/test_corrupted.ppm b/bob/io/image/data/test_corrupted.ppm new file mode 100644 index 0000000000000000000000000000000000000000..fa528857de00ad3392bee8a35be1e94e06ffa500 GIT binary patch literal 85 vcmWGA<1$e&<1#We<q{GS`uq3qe;8n3V1O|}G*<4vfB)d(hK7a^?TK6fyQw(3 literal 0 HcmV?d00001 diff --git a/bob/io/image/test.py b/bob/io/image/test.py index d6f4db2..7e21f38 100644 --- a/bob/io/image/test.py +++ b/bob/io/image/test.py @@ -107,7 +107,7 @@ def test_netpbm(): def test_image_load(): # test that the generic bob.io.image.load function works as expected - for filename in ('test.jpg', 'cmyk.jpg', 'test.pbm', 'test.pgm', 'test.ppm', 'img_rgba_color.png'): + for filename in ('test.jpg', 'cmyk.jpg', 'test.pbm', 'test_corrupted.pbm', 'test.pgm', 'test_corrupted.pgm', 'test.ppm', 'test_corrupted.ppm', 'img_rgba_color.png'): full_file = test_utils.datafile(filename, __name__) # load with just image name i1 = bob.io.image.load(full_file) -- GitLab From ab2e9b94eded648e632a1aa1e52e4d16e54c789e Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Wed, 14 Feb 2018 16:05:53 +0100 Subject: [PATCH 2/2] Allow garbage at the end of PNM iaimages not affect readout (closes #30) --- bob/io/image/cpp/netpbm.cpp | 20 ++++++-------------- bob/io/image/cpp/pnmio.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/bob/io/image/cpp/netpbm.cpp b/bob/io/image/cpp/netpbm.cpp index d74c400..abf510f 100644 --- a/bob/io/image/cpp/netpbm.cpp +++ b/bob/io/image/cpp/netpbm.cpp @@ -117,16 +117,8 @@ pm_openw(const char * const name) { void pm_close(FILE * const f) { - // fprintf(stderr, "I am closing a file\n"); - fflush( f ); - // if ( ferror( f ) ) - // boost::format m("a file read or write error occurred at some point"); - // throw std::runtime_error(m.str()); - if ( f != stdin ) - fclose( f ); - // if ( fclose( f ) != 0 ) - // boost::format m("cannot close file."); - // throw std::runtime_error(m.str()); + fflush(f); + if (f != stdin) fclose( f ); } static boost::shared_ptr<std::FILE> make_cfile(const char *filename, const char *flags) @@ -168,12 +160,12 @@ static void pnm_readpaminit(FILE *file, struct pam * const pamP, const int size) if (img_colors >> 8 == 0) pamP->bytes_per_sample = 1; else if (img_colors >> 16 == 0) pamP->bytes_per_sample = 2; } else { - boost::format m("Unknown PNM/PFM image format."); + boost::format m("pnm_readpaminit(): Unknown PNM/PFM image format."); throw std::runtime_error(m.str()); } if (read_err != 0) { - boost::format m("Something went wrong when reading the image file."); + boost::format m("pnm_readpaminit(): Something went wrong when reading the image file."); throw std::runtime_error(m.str()); } @@ -214,7 +206,7 @@ static void pnm_readpam(struct pam * const pamP, int *img_data) { } if (read_err != 0) { - boost::format m("Something went wrong when reading the image file."); + boost::format m("pnm_readpam(): Something went wrong when reading the image file."); throw std::runtime_error(m.str()); } } @@ -240,7 +232,7 @@ static void pnm_writepam(struct pam * const pamP, int *img_data) { } if (write_err != 0) { - boost::format m("Something went wrong when writing the image file."); + boost::format m("pnm_writepam(): Something went wrong when writing the image file."); throw std::runtime_error(m.str()); } } diff --git a/bob/io/image/cpp/pnmio.cpp b/bob/io/image/cpp/pnmio.cpp index b8555cb..0b43def 100644 --- a/bob/io/image/cpp/pnmio.cpp +++ b/bob/io/image/cpp/pnmio.cpp @@ -309,13 +309,13 @@ int read_pbm_data(FILE *f, int *img_in, int img_size, int is_ascii, int img_widt if (is_ascii == 1) { read_count = fscanf(f, "%d", &lum_val); if (read_count < 1) return -1; - if (i >= img_size) return -1; + if (i >= img_size) break; img_in[i++] = lum_val; } else { lum_val = fgetc(f); /* Decode the image contents byte-by-byte. */ for (k = 0; k < 8; k++) { - if (i >= img_size) return -1; + if (i >= img_size) break; img_in[i++] = (lum_val >> (7-k)) & 0x1; // fprintf(stderr, "i: %d, %d\n", i, img_in[i]); row_position++; @@ -355,7 +355,7 @@ int read_pgm_data(FILE *f, int *img_in, int img_size, int is_ascii, lum_val |= fgetc(f); } } - if (i >= img_size) return -1; + if (i >= img_size) break; img_in[i++] = lum_val; } // fclose(f); @@ -397,7 +397,7 @@ int read_ppm_data(FILE *f, int *img_in, int img_size, int is_ascii, b_val |= fgetc(f); } } - if (i >= img_size) return -1; + if (i >= img_size) break; img_in[i++] = r_val; img_in[i++] = g_val; img_in[i++] = b_val; -- GitLab