Mercurial > hg > orthanc-webviewer
comparison Plugin/DecodedImageAdapter.cpp @ 0:02f7a0400a91
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Feb 2015 13:45:35 +0100 |
parents | |
children | 111689a2c177 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:02f7a0400a91 |
---|---|
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 Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the 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 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "DecodedImageAdapter.h" | |
22 | |
23 #include "ViewerToolbox.h" | |
24 #include "ParsedDicomImage.h" | |
25 | |
26 #include <boost/lexical_cast.hpp> | |
27 #include <boost/algorithm/string/predicate.hpp> | |
28 #include <json/writer.h> | |
29 | |
30 namespace OrthancPlugins | |
31 { | |
32 bool DecodedImageAdapter::ParseUri(CompressionType& type, | |
33 uint8_t& compressionLevel, | |
34 std::string& instanceId, | |
35 const std::string& uri) | |
36 { | |
37 size_t separator = uri.find('-'); | |
38 if (separator == std::string::npos && | |
39 separator >= 1) | |
40 { | |
41 return false; | |
42 } | |
43 | |
44 std::string compression = uri.substr(0, separator); | |
45 instanceId = uri.substr(separator + 1); | |
46 | |
47 if (compression == "deflate") | |
48 { | |
49 type = CompressionType_Deflate; | |
50 } | |
51 else if (boost::starts_with(compression, "jpeg")) | |
52 { | |
53 type = CompressionType_Jpeg; | |
54 int level = boost::lexical_cast<int>(compression.substr(4)); | |
55 if (level <= 0 || level > 100) | |
56 { | |
57 return false; | |
58 } | |
59 | |
60 compressionLevel = static_cast<uint8_t>(level); | |
61 } | |
62 else | |
63 { | |
64 return false; | |
65 } | |
66 | |
67 return true; | |
68 } | |
69 | |
70 | |
71 | |
72 bool DecodedImageAdapter::Create(std::string& content, | |
73 const std::string& uri) | |
74 { | |
75 std::string message = "Decoding DICOM instance: " + uri; | |
76 OrthancPluginLogInfo(context_, message.c_str()); | |
77 | |
78 CompressionType type; | |
79 uint8_t level; | |
80 std::string instanceId; | |
81 | |
82 if (!ParseUri(type, level, instanceId, uri)) | |
83 { | |
84 return false; | |
85 } | |
86 | |
87 std::string file = "/instances/" + instanceId + "/file"; | |
88 | |
89 std::string dicom; | |
90 if (!GetStringFromOrthanc(dicom, context_, file)) | |
91 { | |
92 return false; | |
93 } | |
94 | |
95 ParsedDicomImage image(dicom); | |
96 | |
97 Json::Value json; | |
98 | |
99 if (type == CompressionType_Deflate) | |
100 { | |
101 if (!image.EncodeUsingDeflate(json, 9)) | |
102 { | |
103 return false; | |
104 } | |
105 } | |
106 else if (type == CompressionType_Jpeg) | |
107 { | |
108 if (!image.EncodeUsingJpeg(json, level)) | |
109 { | |
110 return false; | |
111 } | |
112 } | |
113 else | |
114 { | |
115 return false; | |
116 } | |
117 | |
118 Json::FastWriter writer; | |
119 content = writer.write(json); | |
120 return true; | |
121 } | |
122 } |