comparison Plugins/Samples/AutomatedJpeg2kCompression/Plugin.cpp @ 2884:497a637366b4 db-changes

integration mainline->db-changes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Oct 2018 15:18:10 +0200
parents 878b59270859
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
1762:2b91363cc1d1 2884:497a637366b4
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include <orthanc/OrthancCPlugin.h>
23
24 #include <string>
25
26 static OrthancPluginContext* context_ = NULL;
27
28
29 static bool ReadFile(std::string& result,
30 const std::string& path)
31 {
32 OrthancPluginMemoryBuffer tmp;
33 if (OrthancPluginReadFile(context_, &tmp, path.c_str()) == OrthancPluginErrorCode_Success)
34 {
35 result.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
36 OrthancPluginFreeMemoryBuffer(context_, &tmp);
37 return true;
38 }
39 else
40 {
41 return false;
42 }
43 }
44
45
46 OrthancPluginErrorCode OnStoredCallback(OrthancPluginDicomInstance* instance,
47 const char* instanceId)
48 {
49 char buffer[1024];
50 sprintf(buffer, "Just received a DICOM instance of size %d and ID %s from origin %d (AET %s)",
51 (int) OrthancPluginGetInstanceSize(context_, instance), instanceId,
52 OrthancPluginGetInstanceOrigin(context_, instance),
53 OrthancPluginGetInstanceRemoteAet(context_, instance));
54 OrthancPluginLogInfo(context_, buffer);
55
56 if (OrthancPluginGetInstanceOrigin(context_, instance) == OrthancPluginInstanceOrigin_Plugin)
57 {
58 // Do not compress twice the same file
59 return OrthancPluginErrorCode_Success;
60 }
61
62 // Write the uncompressed DICOM content to some temporary file
63 std::string uncompressed = "uncompressed-" + std::string(instanceId) + ".dcm";
64 OrthancPluginErrorCode error = OrthancPluginWriteFile(context_, uncompressed.c_str(),
65 OrthancPluginGetInstanceData(context_, instance),
66 OrthancPluginGetInstanceSize(context_, instance));
67 if (error)
68 {
69 return error;
70 }
71
72 // Remove the original DICOM instance
73 std::string uri = "/instances/" + std::string(instanceId);
74 error = OrthancPluginRestApiDelete(context_, uri.c_str());
75 if (error)
76 {
77 return error;
78 }
79
80 // Path to the temporary file that will contain the compressed DICOM content
81 std::string compressed = "compressed-" + std::string(instanceId) + ".dcm";
82
83 // Compress to JPEG2000 using gdcm
84 std::string command1 = "gdcmconv --j2k " + uncompressed + " " + compressed;
85
86 // Generate a new SOPInstanceUID for the JPEG2000 file, as gdcmconv
87 // does not do this by itself
88 std::string command2 = "dcmodify --no-backup -gin " + compressed;
89
90 // Make the required system calls
91 system(command1.c_str());
92 system(command2.c_str());
93
94 // Read the result of the JPEG2000 compression
95 std::string j2k;
96 bool ok = ReadFile(j2k, compressed);
97
98 // Remove the two temporary files
99 remove(compressed.c_str());
100 remove(uncompressed.c_str());
101
102 if (!ok)
103 {
104 return OrthancPluginErrorCode_Plugin;
105 }
106
107 // Upload the JPEG2000 file through the REST API
108 OrthancPluginMemoryBuffer tmp;
109 if (OrthancPluginRestApiPost(context_, &tmp, "/instances", j2k.c_str(), j2k.size()))
110 {
111 ok = false;
112 }
113
114 if (ok)
115 {
116 OrthancPluginFreeMemoryBuffer(context_, &tmp);
117 }
118
119 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_Plugin;
120 }
121
122
123 extern "C"
124 {
125 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
126 {
127 context_ = c;
128
129 /* Check the version of the Orthanc core */
130 if (OrthancPluginCheckVersion(c) == 0)
131 {
132 char info[1024];
133 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
134 context_->orthancVersion,
135 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
136 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
137 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
138 OrthancPluginLogError(context_, info);
139 return -1;
140 }
141
142 OrthancPluginRegisterOnStoredInstanceCallback(context_, OnStoredCallback);
143
144 return 0;
145 }
146
147
148 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
149 {
150 }
151
152
153 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
154 {
155 return "sample-jpeg2k";
156 }
157
158
159 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
160 {
161 return "0.0";
162 }
163 }