comparison ViewerPlugin/IIIF.cpp @ 272:c766c25fe492 iiif

upgrade from IIIF Image API 2.1.1 to 3.0.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jul 2023 08:40:59 +0200
parents 45e3b5adf4ae
children 1c95010d9d2e
comparison
equal deleted inserted replaced
271:45e3b5adf4ae 272:c766c25fe492
116 tiles["width"] = pyramid.GetTileWidth(0); 116 tiles["width"] = pyramid.GetTileWidth(0);
117 tiles["height"] = pyramid.GetTileHeight(0); 117 tiles["height"] = pyramid.GetTileHeight(0);
118 tiles["scaleFactors"] = scaleFactors; 118 tiles["scaleFactors"] = scaleFactors;
119 119
120 Json::Value result; 120 Json::Value result;
121 result["@context"] = "http://iiif.io/api/image/2/context.json"; 121 result["@context"] = "http://iiif.io/api/image/3/context.json";
122 result["@id"] = iiifPublicUrl_ + "tiles/" + seriesId; 122 result["profile"] = "level0";
123 result["profile"] = "http://iiif.io/api/image/2/level0.json";
124 result["protocol"] = "http://iiif.io/api/image"; 123 result["protocol"] = "http://iiif.io/api/image";
124 result["type"] = "ImageService3";
125
126 result["id"] = iiifPublicUrl_ + "tiles/" + seriesId;
125 result["width"] = pyramid.GetLevelWidth(0); 127 result["width"] = pyramid.GetLevelWidth(0);
126 result["height"] = pyramid.GetLevelHeight(0); 128 result["height"] = pyramid.GetLevelHeight(0);
127 result["sizes"] = sizes; 129 result["sizes"] = sizes;
128
129 result["tiles"] = Json::arrayValue;
130 result["tiles"].append(tiles); 130 result["tiles"].append(tiles);
131 131
132 std::string s = result.toStyledString(); 132 std::string s = result.toStyledString();
133 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Json)); 133 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), s.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Json));
134 } 134 }
247 } 247 }
248 } 248 }
249 249
250 if (!ok) 250 if (!ok)
251 { 251 {
252 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (x,y,width,height) region: " + region); 252 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (x,y,width,height) region, found: " + region);
253 } 253 }
254 254
255 int cropWidth; 255 ok = false;
256 boost::regex sizePattern("([0-9]+),"); 256 int cropWidth, cropHeight;
257 boost::regex sizePattern("([0-9]+),([0-9]+)");
257 boost::cmatch sizeWhat; 258 boost::cmatch sizeWhat;
258 if (regex_match(size.c_str(), sizeWhat, sizePattern)) 259 if (regex_match(size.c_str(), sizeWhat, sizePattern))
259 { 260 {
260 try 261 try
261 { 262 {
262 cropWidth = boost::lexical_cast<int>(sizeWhat[1]); 263 cropWidth = boost::lexical_cast<int>(sizeWhat[1]);
263 ok = (cropWidth > 0); 264 cropHeight = boost::lexical_cast<int>(sizeWhat[2]);
265 ok = (cropWidth > 0 && cropHeight > 0);
264 } 266 }
265 catch (boost::bad_lexical_cast&) 267 catch (boost::bad_lexical_cast&)
266 { 268 {
267 } 269 }
268 } 270 }
269 271
270 if (!ok) 272 if (!ok)
271 { 273 {
272 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (width,) size: " + size); 274 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "IIIF - Not a (width,height) crop, found: " + size);
273 } 275 }
274 276
275 std::unique_ptr<OrthancWSI::RawTile> rawTile; 277 std::unique_ptr<OrthancWSI::RawTile> rawTile;
276 std::unique_ptr<Orthanc::ImageAccessor> toCrop; 278 std::unique_ptr<Orthanc::ImageAccessor> toCrop;
277 279
309 { 311 {
310 rawTile.reset(new OrthancWSI::RawTile(locker.GetPyramid(), level, 312 rawTile.reset(new OrthancWSI::RawTile(locker.GetPyramid(), level,
311 regionX / GetPhysicalTileWidth(pyramid, level), 313 regionX / GetPhysicalTileWidth(pyramid, level),
312 regionY / GetPhysicalTileHeight(pyramid, level))); 314 regionY / GetPhysicalTileHeight(pyramid, level)));
313 315
314 if (static_cast<unsigned int>(cropWidth) < pyramid.GetTileWidth(level)) 316 if (static_cast<unsigned int>(cropWidth) < pyramid.GetTileWidth(level) ||
317 static_cast<unsigned int>(cropHeight) < pyramid.GetTileHeight(level))
315 { 318 {
316 toCrop.reset(rawTile->Decode()); 319 toCrop.reset(rawTile->Decode());
317 rawTile.reset(NULL); 320 rawTile.reset(NULL);
318 } 321 }
319 } 322 }
327 rawTile->Answer(output, Orthanc::MimeType_Jpeg); 330 rawTile->Answer(output, Orthanc::MimeType_Jpeg);
328 } 331 }
329 else if (toCrop.get() != NULL) 332 else if (toCrop.get() != NULL)
330 { 333 {
331 assert(rawTile.get() == NULL); 334 assert(rawTile.get() == NULL);
332 assert(static_cast<unsigned int>(cropWidth) < toCrop->GetWidth()); 335
336 if (static_cast<unsigned int>(cropWidth) > toCrop->GetWidth() ||
337 static_cast<unsigned int>(cropHeight) > toCrop->GetHeight())
338 {
339 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "IIIF - Asking to crop outside of the tile size");
340 }
333 341
334 Orthanc::ImageAccessor cropped; 342 Orthanc::ImageAccessor cropped;
335 toCrop->GetRegion(cropped, 0, 0, cropWidth, toCrop->GetHeight()); 343 toCrop->GetRegion(cropped, 0, 0, cropWidth, cropHeight);
336 344
337 std::string encoded; 345 std::string encoded;
338 OrthancWSI::RawTile::Encode(encoded, cropped, Orthanc::MimeType_Jpeg); 346 OrthancWSI::RawTile::Encode(encoded, cropped, Orthanc::MimeType_Jpeg);
339 347
340 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, encoded.c_str(), 348 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, encoded.c_str(),
557 tile["height"] = height; 565 tile["height"] = height;
558 tile["width"] = width; 566 tile["width"] = width;
559 tile["scaleFactors"].append(1); 567 tile["scaleFactors"].append(1);
560 568
561 Json::Value result; 569 Json::Value result;
562 result["@context"] = "http://iiif.io/api/image/2/context.json"; 570 result["@context"] = "http://iiif.io/api/image/3/context.json";
563 result["@id"] = iiifPublicUrl_ + "frames/" + instanceId + "/" + frame; 571 result["profile"] = "http://iiif.io/api/image/3/level0.json";
564 result["profile"] = "http://iiif.io/api/image/2/level0.json";
565 result["protocol"] = "http://iiif.io/api/image"; 572 result["protocol"] = "http://iiif.io/api/image";
573 result["type"] = "ImageService3";
574
575 result["id"] = iiifPublicUrl_ + "frames/" + instanceId + "/" + frame;
566 result["width"] = width; 576 result["width"] = width;
567 result["height"] = height; 577 result["height"] = height;
568 result["tiles"].append(tile); 578 result["tiles"].append(tile);
569 579
570 std::string s = result.toStyledString(); 580 std::string s = result.toStyledString();