Skip to content

Commit 459d0c9

Browse files
committed
Redesign load loops to make them OpenMP compatible
1 parent 43e5b11 commit 459d0c9

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/imageio/imageio_pfm.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,44 @@ dt_imageio_retval_t dt_imageio_open_pfm(dt_image_t *img, const char *filename, d
8080
float *buf = (float *)dt_mipmap_cache_alloc(mbuf, img);
8181
if(!buf) goto error_cache_full;
8282

83+
const size_t npixels = (size_t)img->width * img->height;
84+
85+
float *readbuf = dt_alloc_align_float(npixels * 4);
86+
if(!readbuf)
87+
goto error_cache_full;
88+
89+
union { float f; guint32 i; } value;
90+
8391
if(cols == 3)
8492
{
85-
ret = fread(buf, 3 * sizeof(float), (size_t)img->width * img->height, f);
86-
for(size_t i = (size_t)img->width * img->height; i > 0; i--)
87-
for(int c = 0; c < 3; c++)
93+
ret = fread(readbuf, 3 * sizeof(float), npixels, f);
94+
95+
for(size_t j = 0; j < img->height; j++)
96+
for(size_t i = 0; i < img->width; i++)
8897
{
89-
union { float f; guint32 i; } v;
90-
v.f = buf[3 * (i - 1) + c];
91-
if(swap_byte_order) v.i = GUINT32_SWAP_LE_BE(v.i);
92-
buf[4 * (i - 1) + c] = v.f;
98+
dt_aligned_pixel_t pix = {0.0f, 0.0f, 0.0f, 0.0f};
99+
for_three_channels(c)
100+
{
101+
value.f = readbuf[3 * (j * img->width + i) + c];
102+
if(swap_byte_order) value.i = GUINT32_SWAP_LE_BE(value.i);
103+
pix[c] = value.f;
104+
}
105+
copy_pixel_nontemporal(&buf[4 * (img->width * j + i)], pix);
93106
}
94107
}
95108
else
109+
{
110+
ret = fread(readbuf, sizeof(float), npixels, f);
111+
96112
for(size_t j = 0; j < img->height; j++)
97113
for(size_t i = 0; i < img->width; i++)
98114
{
99-
union { float f; guint32 i; } v;
100-
ret = fread(&v.f, sizeof(float), 1, f);
101-
if(swap_byte_order) v.i = GUINT32_SWAP_LE_BE(v.i);
115+
value.f = readbuf[(j * img->width + i)];
116+
if(swap_byte_order) value.i = GUINT32_SWAP_LE_BE(value.i);
102117
buf[4 * (img->width * j + i) + 2] = buf[4 * (img->width * j + i) + 1]
103-
= buf[4 * (img->width * j + i) + 0] = v.f;
118+
= buf[4 * (img->width * j + i) + 0] = value.f;
104119
}
120+
}
105121

106122
float *line = (float *)calloc(4 * img->width, sizeof(float));
107123
if(line == NULL) goto error_cache_full;
@@ -116,6 +132,7 @@ dt_imageio_retval_t dt_imageio_open_pfm(dt_image_t *img, const char *filename, d
116132

117133
free(line);
118134
fclose(f);
135+
dt_free_align(readbuf);
119136

120137
img->buf_dsc.cst = IOP_CS_RGB;
121138
img->buf_dsc.filters = 0u;

0 commit comments

Comments
 (0)