commit b510b6061ccb713ed37c2d2c179d8732c357db3a
parent 08c8ffa8fbf6f600099e6e2dc9c5af55daf094e6
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 18 Apr 2021 03:58:14 -0700
Improve speed and simplicity of ff_read
While the memory usage is much higher it makes a lot more sense.
I'm going to look into using 16 bit depth images directly with
Ximage or the like to further increase speed and remove the need
for two image buffers in ff_read.
In my environment this change reduced the time to load an image by
half.
Diffstat:
M | lel.c | | | 31 | +++++++++++-------------------- |
1 file changed, 11 insertions(+), 20 deletions(-)
diff --git a/lel.c b/lel.c
@@ -109,30 +109,21 @@ ff_open(struct img *img, FILE *fp)
static int
ff_read(struct img *img, FILE *fp)
{
- int i, j, off, row_len;
- uint16_t *row;
+ uint16_t *tmp;
+ unsigned int i, size;
- if (img->state & LOADED)
- return 0;
-
- row_len = img->width * strlen("RRGGBBAA");
- if (!(row = malloc(row_len)))
+ size = img->width * img->height * 4;
+ tmp = malloc(size * sizeof(*tmp));
+ if (tmp == NULL)
return -1;
- for (off = 0, i = 0; i < img->height; ++i) {
- if (fread(row, 1, (size_t)row_len, fp) != (size_t)row_len) {
- free(row);
- errno = EFTYPE;
- return -1;
- }
- for (j = 0; j < row_len / 2; j += 4, off += 4) {
- img->buf[off] = row[j];
- img->buf[off + 1] = row[j + 1];
- img->buf[off + 2] = row[j + 2];
- img->buf[off + 3] = row[j + 3];
- }
+ if (fread(tmp, sizeof(*tmp), size, fp) != size) {
+ errno = EFTYPE;
+ return -1;
}
- free(row);
+
+ for (i = 0; i < size; ++i)
+ img->buf[i] = tmp[i];
img->state |= LOADED;