30#include <glib/gstdio.h>
36float *
read_pfm(
const char *filename,
int *wd,
int *ht)
38 FILE *
f = g_fopen(filename,
"rb");
42 fprintf(stderr,
"can't open input file\n");
47 char scale_factor_string[64] = { 0 };
50 unused = fscanf(
f,
"%c%c %d %d %63s%*[^\n]", &magic[0], &magic[1], &
width, &
height, scale_factor_string);
51 if(magic[0] !=
'P' || unused != 5 || fgetc(
f) !=
'\n')
53 fprintf(stderr,
"wrong input file format\n");
59 else if(magic[1] ==
'f')
63 fprintf(stderr,
"wrong input file format\n");
68 float scale_factor = g_ascii_strtod(scale_factor_string, NULL);
69 int swap_byte_order = (scale_factor >= 0.0) ^ (G_BYTE_ORDER == G_BIG_ENDIAN);
74 fprintf(stderr,
"error allocating memory\n");
81 int ret = fread(image, 3 *
sizeof(
float), (
size_t)
width *
height,
f);
84 fprintf(stderr,
"error reading PFM\n");
92 for(
int c = 0; c < 3; c++)
98 v.f = image[3 * (
i - 1) + c];
99 v.i = GUINT32_SWAP_LE_BE(
v.i);
100 image[3 * (
i - 1) + c] =
v.f;
105 for(
size_t j = 0; j <
height; j++)
112 int ret = fread(&
v.f,
sizeof(
float), 1,
f);
115 fprintf(stderr,
"error reading PFM\n");
120 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
121 image[3 * (
width * j +
i) + 2] = image[3 * (
width * j +
i) + 1] = image[3 * (
width * j +
i) + 0] =
v.f;
123 float *line = (
float *)calloc(3 *
width,
sizeof(
float));
124 for(
size_t j = 0; j <
height / 2; j++)
126 memcpy(line, image +
width * j * 3,
sizeof(
float) *
width * 3);
128 memcpy(image +
width * (
height - 1 - j) * 3, line,
sizeof(
float) *
width * 3);
140 FILE *
f = g_fopen(filename,
"wb");
148 for(
int j = 0; j <
height; j++)
151 const int row_in =
height - 1 - j;
152 const float *in = data + 3 * (size_t)
width * row_in;
153 float *
out = (
float *)buf_line;
156 memcpy(
out, in,
sizeof(
float) * 3);
158 int cnt = fwrite(buf_line,
sizeof(
float) * 3,
width,
f);
159 if(cnt !=
width)
break;
float * read_pfm(const char *filename, int *wd, int *ht)
void write_pfm(const char *filename, int width, int height, float *data)
static void error(char *msg)
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
const dt_colormatrix_t dt_aligned_pixel_t out
#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_pixelpipe_cache_alloc_align_float_cache(pixels, id)
#define dt_pixelpipe_cache_free_align(mem)