annotate OrthancFramework/Sources/Images/NumpyWriter.cpp @ 4835:f3f93695d6df

fix numpy for SignedGrasycale16
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 27 Nov 2021 12:27:01 +0100
parents bec432ee1094
children 6f780611fc03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4834
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Lesser General Public License for more details.
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * License along with this program. If not, see
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * <http://www.gnu.org/licenses/>.
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "NumpyWriter.h"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #if ORTHANC_ENABLE_ZLIB == 1
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 # include "../Compression/ZipWriter.h"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #endif
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #if ORTHANC_SANDBOXED == 0
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 # include "../SystemToolbox.h"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #endif
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../OrthancException.h"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "../Toolbox.h"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <boost/lexical_cast.hpp>
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace Orthanc
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 void NumpyWriter::WriteHeader(ChunkedBuffer& target,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 unsigned int depth,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 unsigned int width,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 unsigned int height,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 PixelFormat format)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 // https://numpy.org/devdocs/reference/generated/numpy.lib.format.html
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 static const unsigned char VERSION[] = {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 0x93, 'N', 'U', 'M', 'P', 'Y',
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 0x01 /* major version: 1 */,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 0x00 /* minor version: 0 */
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 };
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 std::string datatype;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 switch (Toolbox::DetectEndianness())
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 case Endianness_Little:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 datatype = "<";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 case Endianness_Big:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 datatype = ">";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 default:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 throw OrthancException(ErrorCode_InternalError);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 unsigned int channels;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 switch (format)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 case PixelFormat_Grayscale8:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 datatype += "u1";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 channels = 1;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 case PixelFormat_Grayscale16:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 datatype += "u2";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 channels = 1;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 case PixelFormat_SignedGrayscale16:
4835
f3f93695d6df fix numpy for SignedGrasycale16
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4834
diff changeset
85 datatype += "i2";
4834
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 channels = 1;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 case PixelFormat_RGB24:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 datatype += "u1";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 channels = 3;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 case PixelFormat_Float32:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 datatype += "f4";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 channels = 1;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 break;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 default:
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 throw OrthancException(ErrorCode_NotImplemented);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 std::string depthString;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 if (depth != 0)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 depthString = boost::lexical_cast<std::string>(depth) + ", ";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 const std::string info = ("{'descr': '" + datatype + "', 'fortran_order': False, " +
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 "'shape': (" + depthString + boost::lexical_cast<std::string>(height) +
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 "," + boost::lexical_cast<std::string>(width) +
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 "," + boost::lexical_cast<std::string>(channels) + "), }");
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 const uint16_t minimumLength = sizeof(VERSION) + sizeof(uint16_t) + info.size() + 1 /* trailing '\n' */;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 // The length of the header must be evenly divisible by 64. This
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 // loop could be optimized by a "ceil()" operation, but we keep
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 // the code as simple as possible
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 uint16_t length = 64;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 while (length < minimumLength)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 length += 64;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 uint16_t countZeros = length - minimumLength;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 uint16_t headerLength = info.size() + countZeros + 1 /* trailing '\n' */;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 uint8_t highByte = headerLength / 256;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 uint8_t lowByte = headerLength % 256;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 target.AddChunk(VERSION, sizeof(VERSION));
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 target.AddChunk(&lowByte, 1);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 target.AddChunk(&highByte, 1);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 target.AddChunk(info);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 target.AddChunk(std::string(countZeros, ' '));
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 target.AddChunk("\n");
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 void NumpyWriter::WritePixels(ChunkedBuffer& target,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 const ImageAccessor& image)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 size_t rowSize = image.GetBytesPerPixel() * image.GetWidth();
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 for (unsigned int y = 0; y < image.GetHeight(); y++)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 target.AddChunk(image.GetConstRow(y), rowSize);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 void NumpyWriter::Finalize(std::string& target,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 ChunkedBuffer& source,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 bool compress)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 if (compress)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 #if ORTHANC_ENABLE_ZLIB == 1
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // This is the default name of the first array if arrays are
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 // specified as positional arguments in "numpy.savez()"
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 // https://numpy.org/doc/stable/reference/generated/numpy.savez.html
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 const char* ARRAY_NAME = "arr_0";
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 std::string uncompressed;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 source.Flatten(uncompressed);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 const bool isZip64 = (uncompressed.size() >= 1lu * 1024lu * 1024lu * 1024lu);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 ZipWriter writer;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 writer.SetMemoryOutput(target, isZip64);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 writer.Open();
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 writer.OpenFile(ARRAY_NAME);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 writer.Write(uncompressed);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 writer.Close();
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 #else
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 throw OrthancException(ErrorCode_InternalError, "Orthanc was compiled without support for zlib");
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 #endif
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 else
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 source.Flatten(target);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 #if ORTHANC_SANDBOXED == 0
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 void NumpyWriter::WriteToFileInternal(const std::string& filename,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 unsigned int width,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 unsigned int height,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 unsigned int pitch,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 PixelFormat format,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 const void* buffer)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 std::string content;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 WriteToMemoryInternal(content, width, height, pitch, format, buffer);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 SystemToolbox::WriteFile(content, filename);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 #endif
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 void NumpyWriter::WriteToMemoryInternal(std::string& content,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 unsigned int width,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 unsigned int height,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 unsigned int pitch,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 PixelFormat format,
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 const void* buffer)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 ChunkedBuffer chunks;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 WriteHeader(chunks, 0 /* no depth */, width, height, format);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 ImageAccessor image;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 image.AssignReadOnly(format, width, height, pitch, buffer);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 WritePixels(chunks, image);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 Finalize(content, chunks, compressed_);
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 NumpyWriter::NumpyWriter()
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 compressed_ = false;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 void NumpyWriter::SetCompressed(bool compressed)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 #if ORTHANC_ENABLE_ZLIB == 1
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 compressed_ = compressed;
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 #else
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 if (compressed)
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 throw OrthancException(ErrorCode_InternalError, "Orthanc was compiled without support for zlib");
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 #endif
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 }
bec432ee1094 download of numpy arrays from the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }