Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
53941c4f
Commit
53941c4f
authored
1 year ago
by
Wuttke, Joachim
Browse files
Options
Downloads
Patches
Plain Diff
corr file header
parent
1faa2ae3
No related branches found
No related tags found
1 merge request
!1628
simplify and unify API and code for reading or writing data files
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Device/IO/ReadWriteTiff.cpp
+59
-59
59 additions, 59 deletions
Device/IO/ReadWriteTiff.cpp
Device/IO/ReadWriteTiff.h
+2
-2
2 additions, 2 deletions
Device/IO/ReadWriteTiff.h
with
61 additions
and
61 deletions
Device/IO/ReadWriteTiff.cpp
+
59
−
59
View file @
53941c4f
...
...
@@ -3,7 +3,7 @@
// BornAgain: simulate and fit reflection and scattering
//
//! @file Device/IO/ReadWriteTiff.cpp
//! @brief Implements
clas
s
R
ead
W
riteTiff
//! @brief Implements
function
s
r
ead
Tiff, w
riteTiff
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
...
...
@@ -26,64 +26,6 @@
#include
<sstream>
#include
<tiffio.hxx>
void
IO
::
writeTiff
(
const
Datafield
&
data
,
std
::
ostream
&
output_stream
)
{
if
(
data
.
rank
()
!=
2
)
throw
std
::
runtime_error
(
"Cannot read TIFF file: unsupported data rank"
);
TIFF
*
tiffstream
=
TIFFStreamOpen
(
"MemTIFF"
,
&
output_stream
);
ASSERT
(
tiffstream
);
const
size_t
m_width
=
data
.
axis
(
0
).
size
();
const
size_t
m_height
=
data
.
axis
(
1
).
size
();
// this does not exist for 1d data
//... Write header.
TIFFSetField
(
tiffstream
,
TIFFTAG_ARTIST
,
"BornAgain.IOFactory"
);
TIFFSetField
(
tiffstream
,
TIFFTAG_DATETIME
,
BaseUtil
::
System
::
getCurrentDateAndTime
().
c_str
());
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGEDESCRIPTION
,
"Image converted from BornAgain intensity file."
);
TIFFSetField
(
tiffstream
,
TIFFTAG_SOFTWARE
,
"BornAgain"
);
const
auto
width
=
static_cast
<
uint32_t
>
(
m_width
);
const
auto
height
=
static_cast
<
uint32_t
>
(
m_height
);
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGEWIDTH
,
width
);
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGELENGTH
,
height
);
// output format, hardcoded here
const
uint16_t
bitPerSample
=
32
;
const
uint16_t
samplesPerPixel
=
1
;
TIFFSetField
(
tiffstream
,
TIFFTAG_BITSPERSAMPLE
,
bitPerSample
);
TIFFSetField
(
tiffstream
,
TIFFTAG_SAMPLESPERPIXEL
,
samplesPerPixel
);
TIFFSetField
(
tiffstream
,
TIFFTAG_PHOTOMETRIC
,
PHOTOMETRIC_MINISWHITE
);
//... Write data.
using
sample_t
=
int
;
const
tmsize_t
buf_size
=
sizeof
(
sample_t
)
*
m_width
;
const
tdata_t
buf
=
_TIFFmalloc
(
buf_size
);
if
(
!
buf
)
throw
std
::
runtime_error
(
"Cannot write TIFF file: failed allocating buffer"
);
std
::
vector
<
sample_t
>
line_buf
;
line_buf
.
resize
(
m_width
,
0
);
std
::
vector
<
unsigned
>
axes_indices
(
2
);
for
(
unsigned
row
=
0
;
row
<
(
uint32_t
)
m_height
;
row
++
)
{
for
(
unsigned
col
=
0
;
col
<
line_buf
.
size
();
++
col
)
{
axes_indices
[
0
]
=
col
;
axes_indices
[
1
]
=
static_cast
<
unsigned
>
(
m_height
)
-
1
-
row
;
const
size_t
global_index
=
data
.
frame
().
toGlobalIndex
(
axes_indices
);
line_buf
[
col
]
=
static_cast
<
sample_t
>
(
data
[
global_index
]);
}
memcpy
(
buf
,
&
line_buf
[
0
],
buf_size
);
if
(
TIFFWriteScanline
(
tiffstream
,
buf
,
row
)
<
0
)
throw
std
::
runtime_error
(
"Cannot write TIFF file: error in TIFFWriteScanline"
);
}
_TIFFfree
(
buf
);
TIFFFlush
(
tiffstream
);
TIFFClose
(
tiffstream
);
}
Datafield
*
IO
::
readTiff
(
std
::
istream
&
input_stream
)
{
TIFF
*
tiffstream
=
TIFFStreamOpen
(
"MemTIFF"
,
&
input_stream
);
...
...
@@ -224,4 +166,62 @@ Datafield* IO::readTiff(std::istream& input_stream)
return
data
.
release
();
}
void
IO
::
writeTiff
(
const
Datafield
&
data
,
std
::
ostream
&
output_stream
)
{
if
(
data
.
rank
()
!=
2
)
throw
std
::
runtime_error
(
"Cannot read TIFF file: unsupported data rank"
);
TIFF
*
tiffstream
=
TIFFStreamOpen
(
"MemTIFF"
,
&
output_stream
);
ASSERT
(
tiffstream
);
const
size_t
m_width
=
data
.
axis
(
0
).
size
();
const
size_t
m_height
=
data
.
axis
(
1
).
size
();
// this does not exist for 1d data
//... Write header.
TIFFSetField
(
tiffstream
,
TIFFTAG_ARTIST
,
"BornAgain.IOFactory"
);
TIFFSetField
(
tiffstream
,
TIFFTAG_DATETIME
,
BaseUtil
::
System
::
getCurrentDateAndTime
().
c_str
());
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGEDESCRIPTION
,
"Image converted from BornAgain intensity file."
);
TIFFSetField
(
tiffstream
,
TIFFTAG_SOFTWARE
,
"BornAgain"
);
const
auto
width
=
static_cast
<
uint32_t
>
(
m_width
);
const
auto
height
=
static_cast
<
uint32_t
>
(
m_height
);
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGEWIDTH
,
width
);
TIFFSetField
(
tiffstream
,
TIFFTAG_IMAGELENGTH
,
height
);
// output format, hardcoded here
const
uint16_t
bitPerSample
=
32
;
const
uint16_t
samplesPerPixel
=
1
;
TIFFSetField
(
tiffstream
,
TIFFTAG_BITSPERSAMPLE
,
bitPerSample
);
TIFFSetField
(
tiffstream
,
TIFFTAG_SAMPLESPERPIXEL
,
samplesPerPixel
);
TIFFSetField
(
tiffstream
,
TIFFTAG_PHOTOMETRIC
,
PHOTOMETRIC_MINISWHITE
);
//... Write data.
using
sample_t
=
int
;
const
tmsize_t
buf_size
=
sizeof
(
sample_t
)
*
m_width
;
const
tdata_t
buf
=
_TIFFmalloc
(
buf_size
);
if
(
!
buf
)
throw
std
::
runtime_error
(
"Cannot write TIFF file: failed allocating buffer"
);
std
::
vector
<
sample_t
>
line_buf
;
line_buf
.
resize
(
m_width
,
0
);
std
::
vector
<
unsigned
>
axes_indices
(
2
);
for
(
unsigned
row
=
0
;
row
<
(
uint32_t
)
m_height
;
row
++
)
{
for
(
unsigned
col
=
0
;
col
<
line_buf
.
size
();
++
col
)
{
axes_indices
[
0
]
=
col
;
axes_indices
[
1
]
=
static_cast
<
unsigned
>
(
m_height
)
-
1
-
row
;
const
size_t
global_index
=
data
.
frame
().
toGlobalIndex
(
axes_indices
);
line_buf
[
col
]
=
static_cast
<
sample_t
>
(
data
[
global_index
]);
}
memcpy
(
buf
,
&
line_buf
[
0
],
buf_size
);
if
(
TIFFWriteScanline
(
tiffstream
,
buf
,
row
)
<
0
)
throw
std
::
runtime_error
(
"Cannot write TIFF file: error in TIFFWriteScanline"
);
}
_TIFFfree
(
buf
);
TIFFFlush
(
tiffstream
);
TIFFClose
(
tiffstream
);
}
#endif // BA_TIFF_SUPPORT
This diff is collapsed.
Click to expand it.
Device/IO/ReadWriteTiff.h
+
2
−
2
View file @
53941c4f
...
...
@@ -3,7 +3,7 @@
// BornAgain: simulate and fit reflection and scattering
//
//! @file Device/IO/ReadWriteTiff.h
//! @brief De
fines clas
s
R
ead
W
riteTiff
//! @brief De
clares function
s
r
ead
Tiff, w
riteTiff
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
...
...
@@ -25,8 +25,8 @@ class Datafield;
namespace
IO
{
void
writeTiff
(
const
Datafield
&
data
,
std
::
ostream
&
output_stream
);
Datafield
*
readTiff
(
std
::
istream
&
input_stream
);
void
writeTiff
(
const
Datafield
&
data
,
std
::
ostream
&
output_stream
);
}
// namespace IO
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment