comparison ViewerPlugin/IIIF.cpp @ 273:1c95010d9d2e iiif

clarification by removing regular expressions
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jul 2023 08:48:16 +0200
parents c766c25fe492
children 2805246064aa
comparison
equal deleted inserted replaced
272:c766c25fe492 273:1c95010d9d2e
30 #include <Images/Image.h> 30 #include <Images/Image.h>
31 #include <Images/ImageProcessing.h> 31 #include <Images/ImageProcessing.h>
32 #include <Logging.h> 32 #include <Logging.h>
33 #include <SerializationToolbox.h> 33 #include <SerializationToolbox.h>
34 34
35 #include <boost/regex.hpp>
36 #include <boost/math/special_functions/round.hpp> 35 #include <boost/math/special_functions/round.hpp>
37 36
38 37
39 static const char* const ROWS = "0028,0010"; 38 static const char* const ROWS = "0028,0010";
40 static const char* const COLUMNS = "0028,0011"; 39 static const char* const COLUMNS = "0028,0011";
222 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, encoded.c_str(), 221 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, encoded.c_str(),
223 encoded.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Jpeg)); 222 encoded.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Jpeg));
224 } 223 }
225 else 224 else
226 { 225 {
227 int regionX, regionY, regionWidth, regionHeight; 226 std::vector<std::string> tokens;
227 Orthanc::Toolbox::TokenizeString(tokens, region, ',');
228
229 uint32_t regionX, regionY, regionWidth, regionHeight;
230
231 if (tokens.size() != 4 ||
232 !Orthanc::SerializationToolbox::ParseUnsignedInteger32(regionX, tokens[0]) ||
233 !Orthanc::SerializationToolbox::ParseUnsignedInteger32(regionY, tokens[1]) ||
234 !Orthanc::SerializationToolbox::ParseUnsignedInteger32(regionWidth, tokens[2]) ||
235 !Orthanc::SerializationToolbox::ParseUnsignedInteger32(regionHeight, tokens[3]))
236 {
237 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (x,y,width,height) region, found: " + region);
238 }
239
240 uint32_t cropWidth, cropHeight;
241
242 Orthanc::Toolbox::TokenizeString(tokens, size, ',');
228 243
229 bool ok = false; 244 bool ok = false;
230 boost::regex regionPattern("([0-9]+),([0-9]+),([0-9]+),([0-9]+)"); 245 if (tokens.size() == 2 &&
231 boost::cmatch regionWhat; 246 Orthanc::SerializationToolbox::ParseUnsignedInteger32(cropWidth, tokens[0]))
232 if (regex_match(region.c_str(), regionWhat, regionPattern)) 247 {
233 { 248 if (tokens[1].empty())
234 try 249 {
235 { 250 cropHeight = cropWidth;
236 regionX = boost::lexical_cast<int>(regionWhat[1]); 251 ok = true;
237 regionY = boost::lexical_cast<int>(regionWhat[2]); 252 }
238 regionWidth = boost::lexical_cast<int>(regionWhat[3]); 253 else if (Orthanc::SerializationToolbox::ParseUnsignedInteger32(cropHeight, tokens[1]))
239 regionHeight = boost::lexical_cast<int>(regionWhat[4]); 254 {
240 ok = (regionX >= 0 && 255 ok = true;
241 regionY >= 0 &&
242 regionWidth > 0 &&
243 regionHeight > 0);
244 }
245 catch (boost::bad_lexical_cast&)
246 {
247 }
248 }
249
250 if (!ok)
251 {
252 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (x,y,width,height) region, found: " + region);
253 }
254
255 ok = false;
256 int cropWidth, cropHeight;
257 boost::regex sizePattern("([0-9]+),([0-9]+)");
258 boost::cmatch sizeWhat;
259 if (regex_match(size.c_str(), sizeWhat, sizePattern))
260 {
261 try
262 {
263 cropWidth = boost::lexical_cast<int>(sizeWhat[1]);
264 cropHeight = boost::lexical_cast<int>(sizeWhat[2]);
265 ok = (cropWidth > 0 && cropHeight > 0);
266 }
267 catch (boost::bad_lexical_cast&)
268 {
269 } 256 }
270 } 257 }
271 258
272 if (!ok) 259 if (!ok)
273 { 260 {