comparison ViewerPlugin/Plugin.cpp @ 277:ac1508b438b1 iiif

support of "Accept" header in /wsi/tiles/{id}/{z}/{x}/{y}
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jul 2023 16:14:19 +0200
parents ef8a673b5fb9
children 7020852a8fa9
comparison
equal deleted inserted replaced
276:ef8a673b5fb9 277:ac1508b438b1
25 #include "DicomPyramidCache.h" 25 #include "DicomPyramidCache.h"
26 #include "IIIF.h" 26 #include "IIIF.h"
27 #include "RawTile.h" 27 #include "RawTile.h"
28 28
29 #include <Compatibility.h> // For std::unique_ptr 29 #include <Compatibility.h> // For std::unique_ptr
30 #include <Logging.h>
31 #include <Images/Image.h> 30 #include <Images/Image.h>
32 #include <Images/ImageProcessing.h> 31 #include <Images/ImageProcessing.h>
32 #include <Logging.h>
33 #include <OrthancException.h> 33 #include <OrthancException.h>
34 #include <SystemToolbox.h> 34 #include <SystemToolbox.h>
35 35
36 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" 36 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
37 37
159 static_cast<unsigned int>(level), 159 static_cast<unsigned int>(level),
160 static_cast<unsigned int>(tileX), 160 static_cast<unsigned int>(tileX),
161 static_cast<unsigned int>(tileY))); 161 static_cast<unsigned int>(tileY)));
162 } 162 }
163 163
164 Orthanc::MimeType mime;
165
164 if (rawTile->GetCompression() == OrthancWSI::ImageCompression_Jpeg) 166 if (rawTile->GetCompression() == OrthancWSI::ImageCompression_Jpeg)
165 { 167 {
166 // The tile is already a JPEG image. In such a case, we can 168 // The tile is already a JPEG image. In such a case, we can
167 // serve it as such, because any Web browser can handle JPEG. 169 // serve it as such, because any Web browser can handle JPEG.
168 rawTile->Answer(output, Orthanc::MimeType_Jpeg); 170 mime = Orthanc::MimeType_Jpeg;
169 } 171 }
170 else 172 else
171 { 173 {
172 // This is a lossless frame (coming from a JPEG2000 or an 174 // This is a lossless frame (coming from JPEG2000 or uncompressed
173 // uncompressed DICOM instance), which is not a DICOM-JPEG 175 // DICOM instance), not a DICOM-JPEG instance. Decompress the raw
174 // instance. We need to decompress the raw tile, then transcode it 176 // tile, then transcode it to PNG to prevent lossy compression and
175 // to the PNG/JPEG, depending on the "encoding". 177 // to avoid JPEG2000 that is not supported by all the browsers.
176 rawTile->Answer(output, Orthanc::MimeType_Png); 178 mime = Orthanc::MimeType_Png;
177 } 179 }
180
181 // Lookup whether a "Accept" HTTP header is present, to overwrite
182 // the default MIME type
183 for (uint32_t i = 0; i < request->headersCount; i++)
184 {
185 std::string key(request->headersKeys[i]);
186 Orthanc::Toolbox::ToLowerCase(key);
187
188 if (key == "accept")
189 {
190 std::vector<std::string> tokens;
191 Orthanc::Toolbox::TokenizeString(tokens, request->headersValues[i], ',');
192
193 bool found = false;
194
195 for (size_t j = 0; j < tokens.size(); j++)
196 {
197 std::string s = Orthanc::Toolbox::StripSpaces(tokens[j]);
198
199 if (s == Orthanc::EnumerationToString(Orthanc::MimeType_Png))
200 {
201 mime = Orthanc::MimeType_Png;
202 found = true;
203 }
204 else if (s == Orthanc::EnumerationToString(Orthanc::MimeType_Jpeg))
205 {
206 mime = Orthanc::MimeType_Jpeg;
207 found = true;
208 }
209 else if (s == Orthanc::EnumerationToString(Orthanc::MimeType_Jpeg2000))
210 {
211 mime = Orthanc::MimeType_Jpeg2000;
212 found = true;
213 }
214 else if (s == "*/*" ||
215 s == "image/*")
216 {
217 found = true;
218 }
219 }
220
221 if (!found)
222 {
223 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 406 /* Not acceptable */);
224 return;
225 }
226 }
227 }
228
229 rawTile->Answer(output, mime);
178 } 230 }
179 231
180 232
181 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, 233 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
182 OrthancPluginResourceType resourceType, 234 OrthancPluginResourceType resourceType,