44 const char *ext = filename + strlen(filename);
45 while(*ext !=
'.' && ext > filename) ext--;
47 FILE *
f = g_fopen(filename,
"rb");
52 char head[2] = {
'X',
'X' };
53 ret = fscanf(
f,
"%c%c\n", head, head + 1);
54 if(ret != 2 || head[0] !=
'P')
goto error_corrupt;
57 else if(head[1] ==
'f')
61 char width_string[10] = { 0 };
62 char height_string[10] = { 0 };
63 char scale_factor_string[64] = { 0 };
64 ret = fscanf(
f,
"%9s %9s %63s%*[^\n]", width_string, height_string, scale_factor_string);
65 if(ret != 3)
goto error_corrupt;
67 img->
width = strtol(width_string, NULL, 0);
68 img->
height = strtol(height_string, NULL, 0);
69 scale_factor = g_ascii_strtod(scale_factor_string, NULL);
70 if(errno != 0)
goto error_corrupt;
71 if(img->
width <= 0 || img->
height <= 0 )
goto error_corrupt;
72 ret = fread(&ret,
sizeof(
char), 1,
f);
73 if(ret != 1)
goto error_corrupt;
76 int swap_byte_order = (scale_factor >= 0.0) ^ (G_BYTE_ORDER == G_BIG_ENDIAN);
80 img->
dsc.
bpp = 4 *
sizeof(float);
83 img->
flags &= ~DT_IMAGE_LDR;
84 img->
flags &= ~DT_IMAGE_RAW;
85 img->
flags &= ~DT_IMAGE_S_RAW;
100 ret = fread(buf, 3 *
sizeof(
float), (
size_t)img->
width * img->
height,
f);
102 for(
int c = 0; c < 3; c++)
104 union {
float f; guint32
i; }
v;
105 v.f = buf[3 * (
i - 1) + c];
106 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
107 buf[4 * (
i - 1) + c] =
v.f;
111 for(
size_t j = 0; j < img->
height; j++)
112 for(
size_t i = 0;
i < img->
width;
i++)
114 union {
float f; guint32
i; }
v;
115 ret = fread(&
v.f,
sizeof(
float), 1,
f);
116 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
117 buf[4 * (img->
width * j +
i) + 2] = buf[4 * (img->
width * j +
i) + 1]
118 = buf[4 * (img->
width * j +
i) + 0] =
v.f;
120 float *line = (
float *)calloc(4 * img->
width,
sizeof(
float));
122 for(
size_t j = 0; j < img->
height / 2; j++)
124 memcpy(line, buf + img->
width * j * 4,
sizeof(
float) * 4 * img->
width);
125 memcpy(buf + img->
width * j * 4, buf + img->
width * (img->
height - 1 - j) * 4,
126 sizeof(
float) * 4 * img->
width);
127 memcpy(buf + img->
width * (img->
height - 1 - j) * 4, line,
sizeof(
float) * 4 * img->
width);