-
Notifications
You must be signed in to change notification settings - Fork 31
/
ImageIO.json.cc
90 lines (82 loc) · 2.78 KB
/
ImageIO.json.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/rasterize/ImageIO.json.cc
//---------------------------------------------------------------------------//
#include "ImageIO.json.hh"
#include "corecel/Assert.hh"
#include "corecel/Types.hh"
#include "corecel/cont/Array.hh"
#include "corecel/cont/ArrayIO.json.hh"
#include "corecel/io/JsonUtils.json.hh"
#include "corecel/math/ArrayOperators.hh"
#include "geocel/detail/LengthUnits.hh"
#include "Image.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
//!@{
//! I/O routines for JSON
#define IM_LOAD_OPTION(NAME) CELER_JSON_LOAD_OPTION(j, v, NAME)
#define IM_LOAD_REQUIRED(NAME) CELER_JSON_LOAD_REQUIRED(j, v, NAME)
void from_json(nlohmann::json const& j, ImageInput& v)
{
IM_LOAD_REQUIRED(lower_left);
IM_LOAD_REQUIRED(upper_right);
IM_LOAD_REQUIRED(rightward);
IM_LOAD_REQUIRED(vertical_pixels);
IM_LOAD_OPTION(horizontal_divisor);
real_type length{lengthunits::centimeter};
if (auto iter = j.find("_units"); iter != j.end())
{
switch (to_unit_system(iter->get<std::string>()))
{
case UnitSystem::cgs:
length = lengthunits::centimeter;
break;
case UnitSystem::si:
length = lengthunits::meter;
break;
case UnitSystem::clhep:
length = lengthunits::millimeter;
break;
default:
CELER_ASSERT_UNREACHABLE();
}
}
if (length != 1)
{
v.lower_left *= length;
v.upper_right *= length;
}
}
void to_json(nlohmann::json& j, ImageInput const& v)
{
j = nlohmann::json{
CELER_JSON_PAIR(v, lower_left),
CELER_JSON_PAIR(v, upper_right),
CELER_JSON_PAIR(v, rightward),
CELER_JSON_PAIR(v, vertical_pixels),
CELER_JSON_PAIR(v, horizontal_divisor),
{"_units", to_cstring(UnitSystem::native)},
};
}
void to_json(nlohmann::json& j, ImageParams const& p)
{
auto const& scalars = p.host_ref().scalars;
j = nlohmann::json{
CELER_JSON_PAIR(scalars, origin),
CELER_JSON_PAIR(scalars, down),
CELER_JSON_PAIR(scalars, right),
CELER_JSON_PAIR(scalars, pixel_width),
CELER_JSON_PAIR(scalars, dims),
{"_units", to_cstring(UnitSystem::native)},
};
}
#undef IM_LOAD_OPTION
#undef IM_LOAD_REQUIRED
//!@}
//---------------------------------------------------------------------------//
} // namespace celeritas