46 const char *ext = filename + strlen(filename);
47 while(*ext !=
'.' && ext > filename) ext--;
49 FILE *
f = g_fopen(filename,
"rb");
54 char head[2] = {
'X',
'X' };
55 ret = fscanf(
f,
"%c%c\n", head, head + 1);
56 if(ret != 2 || head[0] !=
'P')
goto error_corrupt;
59 else if(head[1] ==
'f')
63 char width_string[10] = { 0 };
64 char height_string[10] = { 0 };
65 char scale_factor_string[64] = { 0 };
66 ret = fscanf(
f,
"%9s %9s %63s%*[^\n]", width_string, height_string, scale_factor_string);
67 if(ret != 3)
goto error_corrupt;
69 img->
width = strtol(width_string, NULL, 0);
70 img->
height = strtol(height_string, NULL, 0);
71 scale_factor = g_ascii_strtod(scale_factor_string, NULL);
72 if(errno != 0)
goto error_corrupt;
73 if(img->
width <= 0 || img->
height <= 0 )
goto error_corrupt;
74 ret = fread(&ret,
sizeof(
char), 1,
f);
75 if(ret != 1)
goto error_corrupt;
78 int swap_byte_order = (scale_factor >= 0.0) ^ (G_BYTE_ORDER == G_BIG_ENDIAN);
82 img->
dsc.
bpp = 4 *
sizeof(float);
85 img->
flags &= ~DT_IMAGE_LDR;
86 img->
flags &= ~DT_IMAGE_RAW;
87 img->
flags &= ~DT_IMAGE_S_RAW;
102 ret = fread(buf, 3 *
sizeof(
float), (
size_t)img->
width * img->
height,
f);
104 for(
int c = 0; c < 3; c++)
106 union {
float f; guint32
i; }
v;
107 v.f = buf[3 * (
i - 1) + c];
108 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
109 buf[4 * (
i - 1) + c] =
v.f;
113 for(
size_t j = 0; j < img->
height; j++)
114 for(
size_t i = 0;
i < img->
width;
i++)
116 union {
float f; guint32
i; }
v;
117 ret = fread(&
v.f,
sizeof(
float), 1,
f);
118 if(swap_byte_order)
v.i = GUINT32_SWAP_LE_BE(
v.i);
119 buf[4 * (img->
width * j +
i) + 2] = buf[4 * (img->
width * j +
i) + 1]
120 = buf[4 * (img->
width * j +
i) + 0] =
v.f;
122 float *line = (
float *)calloc(4 * img->
width,
sizeof(
float));
124 for(
size_t j = 0; j < img->
height / 2; j++)
126 memcpy(line, buf + img->
width * j * 4,
sizeof(
float) * 4 * img->
width);
127 memcpy(buf + img->
width * j * 4, buf + img->
width * (img->
height - 1 - j) * 4,
128 sizeof(
float) * 4 * img->
width);
129 memcpy(buf + img->
width * (img->
height - 1 - j) * 4, line,
sizeof(
float) * 4 * img->
width);