comparison Plugins/Samples/AutomatedJpeg2kCompression/Plugin.cpp @ 1816:87c069c94ac9

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