Skip to content

Commit

Permalink
enable unicode characters in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
dicengine committed Dec 10, 2021
1 parent f5936ef commit c9c5a75
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/ioutils/DICe_ImageIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ void read_image_dimensions(const char * file_name,
else{
DEBUG_MSG("read_image_dimensions(): (opencv) file name: " << file_name);
cv::Mat image = cv::imread(file_name, cv::ImreadModes::IMREAD_GRAYSCALE);
if (image.empty()) {
DEBUG_MSG("utils::read_image(): image is empty, it could have unicode characters in the name so trying to read with a buffer instead");
std::ifstream f(file_name,std::iostream::binary);
if(!f.good()){
std::cout << "utils::read_image_dimensions(): image read failed." << std::endl;
height = 0;
width = 0;
return;
}
std::filebuf* pbuf = f.rdbuf();
size_t size = pbuf->pubseekoff(0, f.end, f.in);
pbuf->pubseekpos(0, f.in);
std::vector<uchar> buffer(size);
pbuf->sgetn((char*)buffer.data(), size);
image = cv::imdecode(buffer, cv::IMREAD_GRAYSCALE);
if (image.empty()) {
std::cout << "utils::read_image_dimensions(): image read failed." << std::endl;
TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,"");
}
}
height = image.rows;
width = image.cols;
}
Expand Down Expand Up @@ -387,6 +407,25 @@ void read_image(const char * file_name,
// read the image using opencv:
cv::Mat image;
image = cv::imread(file_name, cv::ImreadModes::IMREAD_GRAYSCALE);
// make sure the filename didn't have any unicode characters causing problems
if (image.empty()) {
DEBUG_MSG("utils::read_image(): image is empty, it could have unicode characters in the name so trying to read with a buffer instead");
std::ifstream f(file_name,std::iostream::binary);
if(!f.good()){
std::cout << "utils::read_image(): image read failed." << std::endl;
}else{
std::filebuf* pbuf = f.rdbuf();
size_t size = pbuf->pubseekoff(0, f.end, f.in);
pbuf->pubseekpos(0, f.in);
std::vector<uchar> buffer(size);
pbuf->sgetn((char*)buffer.data(), size);
image = cv::imdecode(buffer, cv::IMREAD_GRAYSCALE);
if (image.empty()) {
std::cout << "utils::read_image(): image read failed." << std::endl;
TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,"");
}
}
}
width = sub_w==0?image.cols:sub_w;
height = sub_h==0?image.rows:sub_h;
assert(width+sub_offset_x <= image.cols);
Expand Down Expand Up @@ -630,8 +669,28 @@ cv::Mat read_image(const char * file_name){
}
}
return img;
}else
return cv::imread(file_name,cv::IMREAD_GRAYSCALE);
}else{
cv::Mat image = cv::imread(file_name,cv::IMREAD_GRAYSCALE);
if (image.empty()) {
DEBUG_MSG("utils::read_image(): image is empty, it could have unicode characters in the name so trying to read with a buffer instead");
std::ifstream f(file_name,std::iostream::binary);
if(!f.good()){
std::cout << "utils::read_image(): image read failed." << std::endl;
}else{
std::filebuf* pbuf = f.rdbuf();
size_t size = pbuf->pubseekoff(0, f.end, f.in);
pbuf->pubseekpos(0, f.in);
std::vector<uchar> buffer(size);
pbuf->sgetn((char*)buffer.data(), size);
image = cv::imdecode(buffer, cv::IMREAD_GRAYSCALE);
if (image.empty()) {
std::cout << "utils::read_image(): image read failed." << std::endl;
TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,"");
}
}
}
return image;
}
}

template <typename S>
Expand Down
Binary file added tests/component/images/æbärnɃ0.tif
Binary file not shown.
1 change: 1 addition & 0 deletions tests/regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
SET(REGRESSION_TESTS
single_speckled_shift
incremental
unicode_filename
conformal_subset_def
dic_challenge_12
dic_challenge_12_sorted
Expand Down
121 changes: 121 additions & 0 deletions tests/regression/unicode_filename/gold/DICe_solution_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
***
*** Digital Image Correlation Engine (DICe), (git sha1: v3.0-beta.3-10-gd40d9e-dirty) Copyright 2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS)
***
*** Reference image: ../../component/images/æbärnɃ0.tif
*** Deformed image: ../../component/images/æbärnɃ0.tif
*** DIC method : local
*** Correlation method: ZNSSD
*** Interpolation method: KEYS_FOURTH
*** Image gradient method: FINITE_DIFFERENCE
*** Optimization method: GRADIENT_BASED
*** Projection method: DISPLACEMENT_BASED
*** Guess initialization method: USE_FIELD_VALUES
*** Seed location: N/A
*** Shape functions: Translation (u,v) Rotation (theta) Normal Strain (ex,ey) Shear Strain (gamma_xy)
*** Incremental correlation: false
*** Subset size: 15
*** Step size: x 50 y 50 (-1 implies not regular grid)
*** Strain window: N/A
*** Coordinates given with (0,0) as upper left corner of image, x positive right, y positive down
***
COORDINATE_X,COORDINATE_Y,DISPLACEMENT_X,DISPLACEMENT_Y
1.4000E+01,1.4000E+01,4.4044E-14,2.4566E-14
1.4000E+01,6.4000E+01,-9.1844E-15,-1.1572E-15
1.4000E+01,1.1400E+02,1.5927E-14,5.5322E-15
1.4000E+01,1.6400E+02,-1.1662E-15,1.5099E-14
1.4000E+01,2.1400E+02,1.5579E-14,2.3189E-14
1.4000E+01,2.6400E+02,3.0933E-14,-1.5367E-14
1.4000E+01,3.1400E+02,1.9098E-14,-1.8475E-14
1.4000E+01,3.6400E+02,5.9706E-14,-7.3713E-16
1.4000E+01,4.1400E+02,-7.1643E-15,-3.5990E-14
1.4000E+01,4.6400E+02,1.5983E-14,-5.8014E-14
6.4000E+01,4.6400E+02,3.2603E-14,-9.8172E-15
6.4000E+01,4.1400E+02,-1.7824E-14,-3.4804E-15
6.4000E+01,3.6400E+02,-3.0916E-14,1.7249E-14
6.4000E+01,3.1400E+02,1.0252E-15,1.4212E-14
6.4000E+01,2.6400E+02,2.1704E-14,-2.0368E-14
6.4000E+01,2.1400E+02,2.6899E-14,1.8397E-14
6.4000E+01,1.6400E+02,4.2431E-14,-9.6168E-16
6.4000E+01,1.1400E+02,-3.9176E-15,7.0447E-15
6.4000E+01,6.4000E+01,-3.7059E-14,-1.0107E-15
6.4000E+01,1.4000E+01,-2.5382E-15,8.0775E-15
1.1400E+02,1.4000E+01,8.1800E-14,-1.6392E-14
1.1400E+02,6.4000E+01,9.6078E-15,-1.6542E-14
1.1400E+02,1.1400E+02,2.3031E-14,6.8982E-14
1.1400E+02,1.6400E+02,3.5483E-14,-2.7076E-14
1.1400E+02,2.1400E+02,-1.3265E-15,-4.1625E-14
1.1400E+02,2.6400E+02,-1.0075E-14,2.0385E-14
1.1400E+02,3.1400E+02,-3.8241E-15,1.2681E-14
1.1400E+02,3.6400E+02,2.4758E-14,-2.2285E-15
1.1400E+02,4.1400E+02,-2.9489E-15,-4.4338E-15
1.1400E+02,4.6400E+02,-8.1745E-15,-9.8404E-15
1.6400E+02,4.6400E+02,3.5776E-14,2.2796E-14
1.6400E+02,4.1400E+02,-2.5729E-14,-7.7035E-14
1.6400E+02,3.6400E+02,-3.7585E-14,1.9088E-14
1.6400E+02,3.1400E+02,-3.7116E-14,-1.0737E-14
1.6400E+02,2.6400E+02,-9.6118E-16,2.5668E-14
1.6400E+02,2.1400E+02,-2.5164E-14,3.4926E-15
1.6400E+02,1.6400E+02,3.6228E-15,-5.8737E-15
1.6400E+02,1.1400E+02,4.7331E-14,-9.5260E-15
1.6400E+02,6.4000E+01,-1.2748E-14,-7.0546E-15
1.6400E+02,1.4000E+01,4.1061E-15,-7.1848E-15
2.1400E+02,1.4000E+01,-1.2881E-14,1.3311E-14
2.1400E+02,6.4000E+01,1.1743E-14,6.8009E-16
2.1400E+02,1.1400E+02,3.5518E-15,2.3601E-14
2.1400E+02,1.6400E+02,1.2208E-14,-2.7517E-14
2.1400E+02,2.1400E+02,3.0225E-14,-3.4731E-14
2.1400E+02,2.6400E+02,3.5041E-14,1.4662E-14
2.1400E+02,3.1400E+02,1.5267E-14,1.5042E-14
2.1400E+02,3.6400E+02,2.4440E-14,1.2853E-14
2.1400E+02,4.1400E+02,-7.8362E-15,2.2289E-14
2.1400E+02,4.6400E+02,4.0687E-16,1.4324E-14
2.6400E+02,4.6400E+02,3.9137E-14,1.5827E-14
2.6400E+02,4.1400E+02,2.2804E-14,-1.1630E-15
2.6400E+02,3.6400E+02,2.6821E-14,-1.5820E-14
2.6400E+02,3.1400E+02,5.8236E-14,8.2714E-14
2.6400E+02,2.6400E+02,1.2753E-14,1.3709E-14
2.6400E+02,2.1400E+02,1.2615E-14,-6.8342E-15
2.6400E+02,1.6400E+02,2.9350E-15,4.2982E-14
2.6400E+02,1.1400E+02,-3.6274E-15,2.4950E-14
2.6400E+02,6.4000E+01,-9.3434E-15,1.6646E-14
2.6400E+02,1.4000E+01,1.4911E-14,4.8673E-15
3.1400E+02,1.4000E+01,-9.1787E-15,2.5717E-14
3.1400E+02,6.4000E+01,-4.3543E-14,-6.0627E-14
3.1400E+02,1.1400E+02,2.9071E-14,7.4323E-14
3.1400E+02,1.6400E+02,6.9559E-15,1.4795E-14
3.1400E+02,2.1400E+02,-1.8781E-14,3.5287E-15
3.1400E+02,2.6400E+02,-1.5458E-15,1.0190E-14
3.1400E+02,3.1400E+02,-1.6197E-15,3.5331E-14
3.1400E+02,3.6400E+02,-4.2548E-14,-7.0871E-15
3.1400E+02,4.1400E+02,1.1736E-14,-7.7790E-15
3.1400E+02,4.6400E+02,1.7227E-14,-9.4241E-15
3.6400E+02,4.6400E+02,5.7045E-15,1.0933E-14
3.6400E+02,4.1400E+02,-3.7209E-15,4.7997E-15
3.6400E+02,3.6400E+02,-2.1955E-14,-1.3568E-14
3.6400E+02,3.1400E+02,3.7274E-14,8.7413E-14
3.6400E+02,2.6400E+02,-3.8726E-15,-4.8110E-14
3.6400E+02,2.1400E+02,1.0375E-14,-9.3443E-15
3.6400E+02,1.6400E+02,8.4048E-15,1.0566E-13
3.6400E+02,1.1400E+02,3.2470E-14,-1.2552E-14
3.6400E+02,6.4000E+01,4.5170E-14,3.9189E-14
3.6400E+02,1.4000E+01,-1.6555E-14,2.2183E-15
4.1400E+02,1.4000E+01,4.7226E-15,4.5933E-14
4.1400E+02,6.4000E+01,8.5930E-16,5.9585E-15
4.1400E+02,1.1400E+02,-3.1935E-16,1.3906E-14
4.1400E+02,1.6400E+02,-7.7349E-15,-6.6933E-15
4.1400E+02,2.1400E+02,1.0992E-14,6.5478E-15
4.1400E+02,2.6400E+02,5.0786E-14,-8.2060E-15
4.1400E+02,3.1400E+02,-2.5271E-14,-6.0688E-15
4.1400E+02,3.6400E+02,6.6824E-15,-1.9699E-14
4.1400E+02,4.1400E+02,1.7304E-14,4.9328E-14
4.1400E+02,4.6400E+02,6.2837E-14,3.2880E-15
4.6400E+02,4.6400E+02,1.5316E-14,-2.4425E-14
4.6400E+02,4.1400E+02,-3.2800E-14,-1.0259E-14
4.6400E+02,3.6400E+02,5.9778E-15,-3.2798E-14
4.6400E+02,3.1400E+02,6.2154E-14,6.0765E-15
4.6400E+02,2.6400E+02,1.6092E-14,1.1180E-14
4.6400E+02,2.1400E+02,-1.0120E-14,6.0485E-15
4.6400E+02,1.6400E+02,2.2205E-14,-3.8367E-14
4.6400E+02,1.1400E+02,-9.0637E-16,2.3501E-14
4.6400E+02,6.4000E+01,-6.4216E-15,1.0943E-15
4.6400E+02,1.4000E+01,-1.6362E-14,-2.6982E-14
13 changes: 13 additions & 0 deletions tests/regression/unicode_filename/input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<ParameterList>
<Parameter name="subset_size" type="int" value="15" />
<Parameter name="step_size" type="int" value="50" />
<Parameter name="output_folder" type="string" value="./results/" />
<Parameter name="image_folder" type="string" value="../../component/images/" />
<Parameter name="reference_image_index" type="int" value="0" />
<Parameter name="start_image_index" type="int" value="0" />
<Parameter name="end_image_index" type="int" value="0" />
<Parameter name="num_file_suffix_digits" type="int" value="1" />
<Parameter name="image_file_extension" type="string" value=".tif" />
<Parameter name="image_file_prefix" type="string" value="æbärnɃ" />
<Parameter name="correlation_parameters_file" type="string" value="params.xml" />
</ParameterList>
19 changes: 19 additions & 0 deletions tests/regression/unicode_filename/params.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ParameterList>
<Parameter name="interpolation_method" type="string" value="KEYS_FOURTH" />
<Parameter name="initialization_method" type="string" value="USE_FIELD_VALUES" />
<Parameter name="optimization_method" type="string" value="GRADIENT_BASED" />
<Parameter name="write_exodus_output" type="bool" value="false" />
<Parameter name="enable_translation" type="bool" value="true" />
<Parameter name="enable_rotation" type="bool" value="true" />
<Parameter name="enable_normal_strain" type="bool" value="true" />
<Parameter name="enable_shear_strain" type="bool" value="true" />
<Parameter name="output_delimiter" type="string" value="," />
<Parameter name="omit_output_row_id" type="bool" value="true" />
<!-- Output spec using the old way where the column index had to be specified -->
<ParameterList name="output_spec">
<Parameter name="COORDINATE_X" type="int" value="0" />
<Parameter name="COORDINATE_Y" type="int" value="1" />
<Parameter name="DISPLACEMENT_X" type="int" value="2" />
<Parameter name="DISPLACEMENT_Y" type="int" value="3" />
</ParameterList>
</ParameterList>

0 comments on commit c9c5a75

Please sign in to comment.