kwo pushed a commit to branch master. http://git.enlightenment.org/legacy/imlib2.git/commit/?id=42f6004f96cdc1557620631a885fb4c1973b34e4
commit 42f6004f96cdc1557620631a885fb4c1973b34e4 Author: Kim Woelders <k...@woelders.dk> Date: Fri Mar 9 19:55:02 2018 +0100 PNM loader: Simplify (fixing ASCII format parsing issues T6751) ASCII format parsing is considerably simpler/safer now. P1 (ASCII pbm) case is somewhat slower than before (but more correct). P2 (ASCII pgm) case is roughly same speed. P3 (ASCII ppm) case is a bit faster. https://phab.enlightenment.org/T6751 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=406371 --- src/modules/loaders/loader_pnm.c | 175 ++++++++------------------------------- 1 file changed, 35 insertions(+), 140 deletions(-) diff --git a/src/modules/loaders/loader_pnm.c b/src/modules/loaders/loader_pnm.c index 8076289..9e4a2b4 100644 --- a/src/modules/loaders/loader_pnm.c +++ b/src/modules/loaders/loader_pnm.c @@ -139,9 +139,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, DATA8 *data = NULL; /* for the binary versions */ DATA8 *ptr = NULL; int *idata = NULL; /* for the ASCII versions */ - int *iptr; - char buf2[256]; - DATA32 *ptr2; + DATA32 *ptr2, rval, gval, bval; int i, j, x, y, pl = 0; char pper = 0; @@ -154,33 +152,20 @@ load(ImlibImage * im, ImlibProgressFunction progress, switch (p) { case '1': /* ASCII monochrome */ - buf[0] = 0; - i = 0; for (y = 0; y < h; y++) { - x = 0; - while (x < w) + for (x = 0; x < w; x++) { - if (!buf[i]) /* fill buffer */ - { - if (!fgets(buf, 255, f)) - goto quit_error; - i = 0; - } - while (buf[i] && isspace(buf[i])) - i++; - if (buf[i]) - { - if (buf[i] == '1') - *ptr2 = 0xff000000; - else if (buf[i] == '0') - *ptr2 = 0xffffffff; - else - goto quit_error; - ptr2++; - i++; - x++; - } + j = fscanf(f, "%u", &gval); + if (j <= 0) + goto quit_error; + + if (gval == 1) + *ptr2++ = 0xff000000; + else if (gval == 0) + *ptr2++ = 0xffffffff; + else + goto quit_error; } if (progress && do_progress(im, progress, progress_granularity, @@ -189,71 +174,27 @@ load(ImlibImage * im, ImlibProgressFunction progress, } break; case '2': /* ASCII greyscale */ - idata = malloc(sizeof(int) * w); - if (!idata) - goto quit_error; - - buf[0] = 0; - i = 0; - j = 0; for (y = 0; y < h; y++) { - iptr = idata; - x = 0; - while (x < w) + for (x = 0; x < w; x++) { - int k; + j = fscanf(f, "%u", &gval); + if (j <= 0) + goto quit_error; - /* check 4 chars ahead to see if we need to - * fill the buffer */ - for (k = 0; k < 4; k++) + if (v == 0 || v == 255) { - if (!buf[i + k]) /* fill buffer */ - { - if (fseek(f, -k, SEEK_CUR) == -1 || - !fgets(buf, 255, f)) - goto quit_error; - i = 0; - break; - } + *ptr2++ = + 0xff000000 | (gval << 16) | (gval << 8) | gval; } - while (buf[i] && isspace(buf[i])) - i++; - while (buf[i] && !isspace(buf[i])) - buf2[j++] = buf[i++]; - if (j) - { - buf2[j] = 0; - *(iptr++) = atoi(buf2); - j = 0; - x++; - } - } - iptr = idata; - if (v == 0 || v == 255) - { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | (iptr[0] << 16) | (iptr[0] << 8) - | iptr[0]; - ptr2++; - iptr++; - } - } - else - { - for (x = 0; x < w; x++) + else { - *ptr2 = - 0xff000000 | - (((iptr[0] * 255) / v) << 16) | - (((iptr[0] * 255) / v) << 8) | - ((iptr[0] * 255) / v); - ptr2++; - iptr++; + *ptr2++ = + 0xff000000 | (((gval * 255) / v) << 16) | + (((gval * 255) / v) << 8) | ((gval * 255) / v); } } + if (progress && do_progress(im, progress, progress_granularity, &pper, &pl, y)) @@ -261,71 +202,25 @@ load(ImlibImage * im, ImlibProgressFunction progress, } break; case '3': /* ASCII RGB */ - idata = malloc(3 * sizeof(int) * w); - if (!idata) - goto quit_error; - - buf[0] = 0; - i = 0; - j = 0; for (y = 0; y < h; y++) { - int w3 = 3 * w; - - iptr = idata; - x = 0; - while (x < w3) + for (x = 0; x < w; x++) { - int k; + j = fscanf(f, "%u %u %u", &rval, &gval, &bval); + if (j <= 2) + goto quit_error; - /* check 4 chars ahead to see if we need to - * fill the buffer */ - for (k = 0; k < 4; k++) + if (v == 0 || v == 255) { - if (!buf[i + k]) /* fill buffer */ - { - if (fseek(f, -k, SEEK_CUR) == -1 || - !fgets(buf, 255, f)) - goto quit_error; - i = 0; - break; - } + *ptr2++ = + 0xff000000 | (rval << 16) | (gval << 8) | bval; } - while (buf[i] && isspace(buf[i])) - i++; - while (buf[i] && !isspace(buf[i])) - buf2[j++] = buf[i++]; - if (j) + else { - buf2[j] = 0; - *(iptr++) = atoi(buf2); - j = 0; - x++; - } - } - iptr = idata; - if (v == 0 || v == 255) - { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | (iptr[0] << 16) | (iptr[1] << 8) - | iptr[2]; - ptr2++; - iptr += 3; - } - } - else - { - for (x = 0; x < w; x++) - { - *ptr2 = + *ptr2++ = 0xff000000 | - (((iptr[0] * 255) / v) << 16) | - (((iptr[1] * 255) / v) << 8) | - ((iptr[2] * 255) / v); - ptr2++; - iptr += 3; + (((rval * 255) / v) << 16) | + (((gval * 255) / v) << 8) | ((bval * 255) / v); } } if (progress && --