32#include <glib/gstdio.h>
44 int bytes_needed = (img->
width + 7) / 8;
45 uint8_t *line = calloc(bytes_needed,
sizeof(uint8_t));
47 float *buf_iter = buf;
48 for(
size_t y = 0; y < img->
height; y++)
50 if(fread(line,
sizeof(uint8_t), (
size_t)bytes_needed,
f) != bytes_needed)
55 for(
size_t x = 0;
x < bytes_needed;
x++)
57 uint8_t
byte = line[
x] ^ 0xff;
58 for(
int bit = 0; bit < 8 &&
x * 8 + bit < img->
width; bit++)
60 float value = ((
byte & 0x80) >> 7) * 1.0;
61 buf_iter[0] = buf_iter[1] = buf_iter[2] =
value;
80 int ret = fscanf(
f,
"%u", &
max);
85 uint8_t *line = calloc(img->
width,
sizeof(uint8_t));
87 float *buf_iter = buf;
88 for(
size_t y = 0; y < img->
height; y++)
90 if(fread(line,
sizeof(uint8_t), (
size_t)img->
width,
f) != img->
width)
95 for(
size_t x = 0;
x < img->
width;
x++)
97 float value = (float)line[
x] / (
float)
max;
98 buf_iter[0] = buf_iter[1] = buf_iter[2] =
value;
107 uint16_t *line = calloc(img->
width,
sizeof(uint16_t));
109 float *buf_iter = buf;
110 for(
size_t y = 0; y < img->
height; y++)
112 if(fread(line,
sizeof(uint16_t), (
size_t)img->
width,
f) != img->
width)
117 for(
size_t x = 0;
x < img->
width;
x++)
119 uint16_t intvalue = line[
x];
120 if(G_BYTE_ORDER != G_BIG_ENDIAN)
121 intvalue = GUINT16_SWAP_LE_BE(intvalue);
122 float value = (float)intvalue / (
float)
max;
123 buf_iter[0] = buf_iter[1] = buf_iter[2] =
value;
140 int ret = fscanf(
f,
"%u", &
max);
145 uint8_t *line = calloc((
size_t)3 * img->
width,
sizeof(uint8_t));
147 float *buf_iter = buf;
148 for(
size_t y = 0; y < img->
height; y++)
150 if(fread(line, 3 *
sizeof(uint8_t), (
size_t)img->
width,
f) != img->
width)
155 for(
size_t x = 0;
x < img->
width;
x++)
157 for(
int c = 0; c < 3; c++)
159 float value = (float)line[
x * 3 + c] / (
float)
max;
169 uint16_t *line = calloc((
size_t)3 * img->
width,
sizeof(uint16_t));
171 float *buf_iter = buf;
172 for(
size_t y = 0; y < img->
height; y++)
174 if(fread(line, 3 *
sizeof(uint16_t), (
size_t)img->
width,
f) != img->
width)
179 for(
size_t x = 0;
x < img->
width;
x++)
181 for(
int c = 0; c < 3; c++)
183 uint16_t intvalue = line[
x * 3 + c];
185 if(G_BYTE_ORDER != G_BIG_ENDIAN)
186 intvalue = GUINT16_SWAP_LE_BE(intvalue);
187 float value = (float)intvalue / (
float)
max;
201 const char *ext = filename + strlen(filename);
202 while(*ext !=
'.' && ext > filename) ext--;
204 FILE *
f = g_fopen(filename,
"rb");
209 char head[2] = {
'X',
'X' };
210 ret = fscanf(
f,
"%c%c ", head, head + 1);
211 if(ret != 2 || head[0] !=
'P')
goto end;
213 char width_string[10] = { 0 };
214 char height_string[10] = { 0 };
215 ret = fscanf(
f,
"%9s %9s ", width_string, height_string);
216 if(ret != 2)
goto end;
219 img->
width = strtol(width_string, NULL, 0);
220 img->
height = strtol(height_string, NULL, 0);
221 if(errno != 0 || img->
width <= 0 || img->
height <= 0)
goto end;
225 img->
dsc.
bpp = 4 *
sizeof(float);
228 img->
flags &= ~DT_IMAGE_RAW;
229 img->
flags &= ~DT_IMAGE_S_RAW;
230 img->
flags &= ~DT_IMAGE_HDR;
250 else if(head[1] ==
'5')
252 else if(head[1] ==
'6')
@ DT_IMAGEIO_FILE_CORRUPTED
static dt_imageio_retval_t _read_pgm(dt_image_t *img, FILE *f, float *buf)
dt_imageio_retval_t dt_imageio_open_pnm(dt_image_t *img, const char *filename, dt_mipmap_buffer_t *mbuf)
static dt_imageio_retval_t _read_pbm(dt_image_t *img, FILE *f, float *buf)
static dt_imageio_retval_t _read_ppm(dt_image_t *img, FILE *f, float *buf)
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
void * dt_mipmap_cache_alloc(dt_mipmap_buffer_t *buf, const dt_image_t *img)
static const dt_aligned_pixel_simd_t value
dt_iop_buffer_type_t datatype