Commit 9f30492c authored by Tobias Ellinghaus's avatar Tobias Ellinghaus

Avoid reading floats with scanf

This is failing when the used locale uses ',' as the decimal separator.
src/develop/lightroom.c still needs fixing!
parent 8c81fd8c
......@@ -198,7 +198,7 @@ chart_t *parse_cht(const char *filename)
if(*c == 'F')
{
float x0, y0, x1, y1, x2, y2, x3, y3;
// using sscanf would be nice, but parsign floats does only work with LANG=C
// using sscanf would be nice, but parsing floats does only work with LANG=C
// if(sscanf(line, " F _ _ %f %f %f %f %f %f %f %f", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3) != 8)
// ERROR;
c++;
......
......@@ -1741,11 +1741,32 @@ static int parse_csv(dt_lut_t *self, const char *filename, double **target_L_ptr
*target_b_ptr = target_b;
*source_Lab_ptr = source_Lab;
char line[512];
for(int i = 0; i < N; i++)
{
char patchname[512];
r += fscanf(f, "%[^;];%lg;%lg;%lg;%lg;%lg;%lg\n", patchname, source_Lab + 3 * i, source_Lab + 3 * i + 1,
source_Lab + 3 * i + 2, target_L + i, target_a + i, target_b + i);
char *patchname = line, *iter = line, *endptr;
if(fgets(line, sizeof(line) / sizeof(*line), f) == 0) break;
while(*iter != ';') iter++;
*iter++ = '\0';
source_Lab[3 * i] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != ';') break;
iter = endptr + 1;
source_Lab[3 * i + 1] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != ';') break;
iter = endptr + 1;
source_Lab[3 * i + 2] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != ';') break;
iter = endptr + 1;
target_L[i] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != ';') break;
iter = endptr + 1;
target_a[i] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != ';') break;
iter = endptr + 1;
target_b[i] = g_ascii_strtod(iter, &endptr);
if(iter == endptr || *endptr != '\n') break;
double d[3] = { target_L[i], target_a[i], target_b[i] };
if(sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]) > thrs)
{
......@@ -1758,7 +1779,7 @@ static int parse_csv(dt_lut_t *self, const char *filename, double **target_L_ptr
}
}
if(r == 0 || unused == EOF) fprintf(stderr, "just keeping compiler happy\n");
if(r != 0 || unused == EOF) fprintf(stderr, "just keeping compiler happy\n");
fclose(f);
return N;
}
......
......@@ -167,8 +167,6 @@ int RGBE_WriteHeader(FILE *fp, int width, int height, rgbe_header_info *info)
int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
{
char buf[128];
float tempf;
size_t i;
if(info)
{
......@@ -185,6 +183,7 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
else if(info)
{
info->valid |= RGBE_VALID_PROGRAMTYPE;
size_t i;
for(i = 0; i < sizeof(info->programtype) - 1; i++)
{
if((buf[i + 2] == 0) || isspace(buf[i + 2])) break;
......@@ -193,31 +192,43 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
info->programtype[i] = 0;
if(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) == 0) return rgbe_error(rgbe_read_error, NULL);
}
gboolean format_is_rgbe = FALSE;
for(;;)
{
if((buf[0] == 0) || (buf[0] == '\n'))
return rgbe_error(rgbe_format_error, "no FORMAT specifier found");
break;
else if(strcmp(buf, "FORMAT=32-bit_rle_rgbe\n") == 0)
break; /* format found so break out of loop */
else if(info && (sscanf(buf, "GAMMA=%g", &tempf) == 1))
{
info->gamma = tempf;
info->valid |= RGBE_VALID_GAMMA;
}
else if(info && (sscanf(buf, "EXPOSURE=%g", &tempf) == 1))
format_is_rgbe = TRUE;
else if(info)
{
info->exposure = tempf;
info->valid |= RGBE_VALID_EXPOSURE;
if(g_str_has_prefix(buf, "GAMMA="))
{
char *startptr = buf + strlen("GAMMA="), *endptr;
float tmp = g_ascii_strtod(startptr, &endptr);
if(startptr != endptr)
{
info->gamma = tmp;
info->valid |= RGBE_VALID_GAMMA;
}
}
else if(g_str_has_prefix(buf, "EXPOSURE="))
{
char *startptr = buf + strlen("EXPOSURE="), *endptr;
float tmp = g_ascii_strtod(startptr, &endptr);
if(startptr != endptr)
{
info->exposure = tmp;
info->valid |= RGBE_VALID_EXPOSURE;
}
}
}
if(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) == 0) return rgbe_error(rgbe_read_error, NULL);
}
if(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) == 0) return rgbe_error(rgbe_read_error, NULL);
// if (strcmp(buf,"\n") != 0)
// return rgbe_error(rgbe_format_error,
// "missing blank line after FORMAT specifier");
while(strcmp(buf, "\n") != 0)
if(!format_is_rgbe)
return rgbe_error(rgbe_format_error, "no FORMAT specifier found or it's not 32-bit_rle_rgbe");
while(!strcmp(buf, "\n")) // be nice and accept more than one blank line
if(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) == 0) return rgbe_error(rgbe_read_error, NULL);
if(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) == 0) return rgbe_error(rgbe_read_error, NULL);
if(sscanf(buf, "-Y %d +X %d", height, width) < 2)
return rgbe_error(rgbe_format_error, "missing image size specifier");
return RGBE_RETURN_SUCCESS;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment