8000 Fix Windows-specific rasterfile module issue with file names that contain Unicode characters by victoryforce · Pull Request #19063 · darktable-org/darktable · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Windows-specific rasterfile module issue with file names that contain Unicode characters #19063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions src/common/pfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,46 @@
#include <stdlib.h>
#include <string.h>

float *dt_read_pfm(const char *filename, int *err, int *wd, int *ht, int *ch, const size_t planes)
float *dt_read_pfm(const char *filename,
int *err,
int *wd,
int *ht,
int *ch,
const size_t planes)
{
if(wd) *wd = 0;
if(ht) *ht = 0;
if(ch) *ch = 0;
if(err) *err = DT_IMAGEIO_OK;

if(!filename || filename[0] == 0)
{
if(err) *err = DT_IMAGEIO_FILE_NOT_FOUND;
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] no filename provided for 'dt_read_pfm'");
return NULL;
}

float *readbuf = NULL;
float *image = NULL;
FILE *f = g_fopen(filename, "rb");

// The file path is expected to be normalized before dt_read_pfm is called
// (for example, a non-normalized file name may simply not be displayed
// in the menu widget). Therefore, normalization here is expected to be
// redundant (and do nothing) and is added more just in case, as
// additional protection against unsanitized input data.
char *normalized_filename = dt_util_normalize_path(filename);
FILE *f = g_fopen(normalized_filename, "rb");
if(!f)
{
g_free(normalized_filename);
if(err) *err = DT_IMAGEIO_FILE_NOT_FOUND;
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] failed to open file '%s'",
filename);
return NULL;
}
g_free(normalized_filename);

char head[2] = { 'X', 'X' };
char scale_factor_string[64] = { 0 };
Expand All @@ -56,6 +76,9 @@ float *dt_read_pfm(const char *filename, int *err, int *wd, int *ht, int *ch, co
if(ret != 2 || head[0] != 'P')
{
if(err) *err = DT_IMAGEIO_FILE_CORRUPTED;
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] format of data is not Portable Float Map in '%s'",
filename);
goto error;
}

Expand All @@ -64,6 +87,9 @@ float *dt_read_pfm(const char *filename, int *err, int *wd, int *ht, int *ch, co
else
{
if(err) *err = DT_IMAGEIO_FILE_CORRUPTED;
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] format of data is not Portable Float Map in '%s'",
filename);
goto error;
}
if(ch) *ch = channels;
Expand Down Expand Up @@ -123,15 +149,19 @@ float *dt_read_pfm(const char *filename, int *err, int *wd, int *ht, int *ch, co
if(!readbuf || !image)
{
if(err) *err = DT_IMAGEIO_IOERROR;
dt_print(DT_DEBUG_ALWAYS, "can't allocate memory for pfm file `%s'", filename);
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] failed to allocate memory for PFM file `%s'",
filename);
goto error;
}

ret = fread(readbuf, sizeof(float) * channels, npixels, f);
if(ret != npixels)
{
if(err) *err = DT_IMAGEIO_IOERROR;
dt_print(DT_DEBUG_ALWAYS, "can't read all pfm file contents from '%s'", filename);
dt_print(DT_DEBUG_ALWAYS,
"[pfm_read] failed to read all PFM file contents from '%s'",
filename);
goto error;
}

Expand Down Expand Up @@ -188,30 +218,41 @@ float *dt_read_pfm(const char *filename, int *err, int *wd, int *ht, int *ch, co
return NULL;
}

void dt_write_pfm(const char *filename, const size_t width, const size_t height, const void *data, const size_t bpp)
void dt_write_pfm(const char *filename,
const size_t width,
const size_t height,
const void *data,
const size_t bpp)
{
if(!filename || filename[0] == 0)
{
dt_print(DT_DEBUG_ALWAYS, "no filename provided for 'dt_write_pfm'");
dt_print(DT_DEBUG_ALWAYS,
"[pfm_write] no filename provided for 'dt_write_pfm'");
return;
}

FILE *f = g_fopen(filename, "wb");
if(!f)
{
dt_print(DT_DEBUG_ALWAYS, "can't write file `%s'", filename);
dt_print(DT_DEBUG_ALWAYS,
"[pfm_write] failed to open file '%s'",
filename);
return;
}

if(bpp==2)
if(bpp == 2)
fprintf(f, "P5\n%d %d\n", (int)width, (int)height);
else
fprintf(f, "P%s\n%d %d\n-1.0\n", (bpp == sizeof(float)) ? "f" : "F", (int)width, (int)height);
fprintf(f,
"P%s\n%d %d\n-1.0\n",
(bpp == sizeof(float)) ? "f" : "F",
(int)width,
(int)height);

void *buf_line = dt_alloc_align_float(width * 4);
for(size_t row = 0; row < height; row++)
{
// NOTE: pfm has rows in reverse order
// NOTE: PFM has rows in reverse order
const size_t row_in = height - 1 - row;
if(bpp == 4*sizeof(float))
{
Expand Down Expand Up @@ -254,4 +295,3 @@ void dt_write_pfm(const char *filename, const size_t width, const size_t height,
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
// clang-format on

4 changes: 3 additions & 1 deletion src/iop/rasterfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ static void _update_filepath(dt_iop_module_t *self)
for(int i = 0; i < numentries; i++)
{
const char *file = entries[i]->d_name;
dt_bauhaus_combobox_add_aligned(g->file, file, DT_BAUHAUS_COMBOBOX_ALIGN_LEFT);
char *normalized_filename = g_locale_to_utf8(file, -1, NULL, NULL, NULL);
dt_bauhaus_combobox_add_aligned(g->file, normalized_filename, DT_BAUHAUS_COMBOBOX_ALIGN_LEFT);
free(entries[i]);
g_free(normalized_filename);
}
if(numentries != -1) free(entries);

Expand Down
Loading
0