33float *
read_pfm(
const char *filename,
int *wd,
int *ht)
35 FILE *
f = g_fopen(filename,
"rb");
39 fprintf(stderr,
"can't open input file\n");
44 char scale_factor_string[64] = { 0 };
47 unused = fscanf(
f,
"%c%c %d %d %63s%*[^\n]", &magic[0], &magic[1], &
width, &
height, scale_factor_string);
48 if(magic[0] !=
'P' || unused != 5 || fgetc(
f) !=
'\n')
50 fprintf(stderr,
"wrong input file format\n");
56 else if(magic[1] ==
'f')
60 fprintf(stderr,
"wrong input file format\n");
65 float scale_factor = g_ascii_strtod(scale_factor_string, NULL);
66 int swap_byte_order = (scale_factor >= 0.0) ^ (G_BYTE_ORDER == G_BIG_ENDIAN);
71 fprintf(stderr,
"error allocating memory\n");
78 int ret = fread(image, 3 *
sizeof(
float), (
size_t)
width *
height,
f);
81 fprintf(stderr,
"error reading PFM\n");
89 for(
int c = 0; c < 3; c++)
95 v.f = image[3 * (
i - 1) + c];
96 v.i = GUINT32_SWAP_LE_BE(
v.i);
97 image[3 * (
i - 1) + c] =
v.f;
102 for(
size_t j = 0; j <
height; j++)
109 int ret = fread(&
v.f,
sizeof(
float), 1,
f);
112 fprintf(stderr,
"error reading PFM\n");
117 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
118 image[3 * (
width * j +
i) + 2] = image[3 * (
width * j +
i) + 1] = image[3 * (
width * j +
i) + 0] =
v.f;
120 float *line = (
float *)calloc(3 *
width,
sizeof(
float));
121 for(
size_t j = 0; j <
height / 2; j++)
123 memcpy(line, image +
width * j * 3,
sizeof(
float) *
width * 3);
125 memcpy(image +
width * (
height - 1 - j) * 3, line,
sizeof(
float) *
width * 3);
137 FILE *
f = g_fopen(filename,
"wb");
145 for(
int j = 0; j <
height; j++)
148 const int row_in =
height - 1 - j;
149 const float *in = data + 3 * (size_t)
width * row_in;
150 float *
out = (
float *)buf_line;
153 memcpy(
out, in,
sizeof(
float) * 3);
155 int cnt = fwrite(buf_line,
sizeof(
float) * 3,
width,
f);
156 if(cnt !=
width)
break;
static void error(char *msg)
float * read_pfm(const char *filename, int *wd, int *ht)
void write_pfm(const char *filename, int width, int height, float *data)
const dt_aligned_pixel_t f
const dt_colormatrix_t dt_aligned_pixel_t out
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
#define dt_pixelpipe_cache_free_align(mem)
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...