comparison Framework/Orthanc/Core/Enumerations.cpp @ 1:2dbe613f6c93

add orthanc core
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Oct 2016 15:39:01 +0200
parents
children 9220cf4a63d5
comparison
equal deleted inserted replaced
0:351ab0da0150 1:2dbe613f6c93
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 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 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeaders.h"
34 #include "Enumerations.h"
35
36 #include "OrthancException.h"
37 #include "Toolbox.h"
38 #include "Logging.h"
39
40 #include <string.h>
41 #include <cassert>
42
43 namespace Orthanc
44 {
45 // This function is autogenerated by the script
46 // "Resources/GenerateErrorCodes.py"
47 const char* EnumerationToString(ErrorCode error)
48 {
49 switch (error)
50 {
51 case ErrorCode_InternalError:
52 return "Internal error";
53
54 case ErrorCode_Success:
55 return "Success";
56
57 case ErrorCode_Plugin:
58 return "Error encountered within the plugin engine";
59
60 case ErrorCode_NotImplemented:
61 return "Not implemented yet";
62
63 case ErrorCode_ParameterOutOfRange:
64 return "Parameter out of range";
65
66 case ErrorCode_NotEnoughMemory:
67 return "Not enough memory";
68
69 case ErrorCode_BadParameterType:
70 return "Bad type for a parameter";
71
72 case ErrorCode_BadSequenceOfCalls:
73 return "Bad sequence of calls";
74
75 case ErrorCode_InexistentItem:
76 return "Accessing an inexistent item";
77
78 case ErrorCode_BadRequest:
79 return "Bad request";
80
81 case ErrorCode_NetworkProtocol:
82 return "Error in the network protocol";
83
84 case ErrorCode_SystemCommand:
85 return "Error while calling a system command";
86
87 case ErrorCode_Database:
88 return "Error with the database engine";
89
90 case ErrorCode_UriSyntax:
91 return "Badly formatted URI";
92
93 case ErrorCode_InexistentFile:
94 return "Inexistent file";
95
96 case ErrorCode_CannotWriteFile:
97 return "Cannot write to file";
98
99 case ErrorCode_BadFileFormat:
100 return "Bad file format";
101
102 case ErrorCode_Timeout:
103 return "Timeout";
104
105 case ErrorCode_UnknownResource:
106 return "Unknown resource";
107
108 case ErrorCode_IncompatibleDatabaseVersion:
109 return "Incompatible version of the database";
110
111 case ErrorCode_FullStorage:
112 return "The file storage is full";
113
114 case ErrorCode_CorruptedFile:
115 return "Corrupted file (e.g. inconsistent MD5 hash)";
116
117 case ErrorCode_InexistentTag:
118 return "Inexistent tag";
119
120 case ErrorCode_ReadOnly:
121 return "Cannot modify a read-only data structure";
122
123 case ErrorCode_IncompatibleImageFormat:
124 return "Incompatible format of the images";
125
126 case ErrorCode_IncompatibleImageSize:
127 return "Incompatible size of the images";
128
129 case ErrorCode_SharedLibrary:
130 return "Error while using a shared library (plugin)";
131
132 case ErrorCode_UnknownPluginService:
133 return "Plugin invoking an unknown service";
134
135 case ErrorCode_UnknownDicomTag:
136 return "Unknown DICOM tag";
137
138 case ErrorCode_BadJson:
139 return "Cannot parse a JSON document";
140
141 case ErrorCode_Unauthorized:
142 return "Bad credentials were provided to an HTTP request";
143
144 case ErrorCode_BadFont:
145 return "Badly formatted font file";
146
147 case ErrorCode_DatabasePlugin:
148 return "The plugin implementing a custom database back-end does not fulfill the proper interface";
149
150 case ErrorCode_StorageAreaPlugin:
151 return "Error in the plugin implementing a custom storage area";
152
153 case ErrorCode_EmptyRequest:
154 return "The request is empty";
155
156 case ErrorCode_NotAcceptable:
157 return "Cannot send a response which is acceptable according to the Accept HTTP header";
158
159 case ErrorCode_SQLiteNotOpened:
160 return "SQLite: The database is not opened";
161
162 case ErrorCode_SQLiteAlreadyOpened:
163 return "SQLite: Connection is already open";
164
165 case ErrorCode_SQLiteCannotOpen:
166 return "SQLite: Unable to open the database";
167
168 case ErrorCode_SQLiteStatementAlreadyUsed:
169 return "SQLite: This cached statement is already being referred to";
170
171 case ErrorCode_SQLiteExecute:
172 return "SQLite: Cannot execute a command";
173
174 case ErrorCode_SQLiteRollbackWithoutTransaction:
175 return "SQLite: Rolling back a nonexistent transaction (have you called Begin()?)";
176
177 case ErrorCode_SQLiteCommitWithoutTransaction:
178 return "SQLite: Committing a nonexistent transaction";
179
180 case ErrorCode_SQLiteRegisterFunction:
181 return "SQLite: Unable to register a function";
182
183 case ErrorCode_SQLiteFlush:
184 return "SQLite: Unable to flush the database";
185
186 case ErrorCode_SQLiteCannotRun:
187 return "SQLite: Cannot run a cached statement";
188
189 case ErrorCode_SQLiteCannotStep:
190 return "SQLite: Cannot step over a cached statement";
191
192 case ErrorCode_SQLiteBindOutOfRange:
193 return "SQLite: Bing a value while out of range (serious error)";
194
195 case ErrorCode_SQLitePrepareStatement:
196 return "SQLite: Cannot prepare a cached statement";
197
198 case ErrorCode_SQLiteTransactionAlreadyStarted:
199 return "SQLite: Beginning the same transaction twice";
200
201 case ErrorCode_SQLiteTransactionCommit:
202 return "SQLite: Failure when committing the transaction";
203
204 case ErrorCode_SQLiteTransactionBegin:
205 return "SQLite: Cannot start a transaction";
206
207 case ErrorCode_DirectoryOverFile:
208 return "The directory to be created is already occupied by a regular file";
209
210 case ErrorCode_FileStorageCannotWrite:
211 return "Unable to create a subdirectory or a file in the file storage";
212
213 case ErrorCode_DirectoryExpected:
214 return "The specified path does not point to a directory";
215
216 case ErrorCode_HttpPortInUse:
217 return "The TCP port of the HTTP server is privileged or already in use";
218
219 case ErrorCode_DicomPortInUse:
220 return "The TCP port of the DICOM server is privileged or already in use";
221
222 case ErrorCode_BadHttpStatusInRest:
223 return "This HTTP status is not allowed in a REST API";
224
225 case ErrorCode_RegularFileExpected:
226 return "The specified path does not point to a regular file";
227
228 case ErrorCode_PathToExecutable:
229 return "Unable to get the path to the executable";
230
231 case ErrorCode_MakeDirectory:
232 return "Cannot create a directory";
233
234 case ErrorCode_BadApplicationEntityTitle:
235 return "An application entity title (AET) cannot be empty or be longer than 16 characters";
236
237 case ErrorCode_NoCFindHandler:
238 return "No request handler factory for DICOM C-FIND SCP";
239
240 case ErrorCode_NoCMoveHandler:
241 return "No request handler factory for DICOM C-MOVE SCP";
242
243 case ErrorCode_NoCStoreHandler:
244 return "No request handler factory for DICOM C-STORE SCP";
245
246 case ErrorCode_NoApplicationEntityFilter:
247 return "No application entity filter";
248
249 case ErrorCode_NoSopClassOrInstance:
250 return "DicomUserConnection: Unable to find the SOP class and instance";
251
252 case ErrorCode_NoPresentationContext:
253 return "DicomUserConnection: No acceptable presentation context for modality";
254
255 case ErrorCode_DicomFindUnavailable:
256 return "DicomUserConnection: The C-FIND command is not supported by the remote SCP";
257
258 case ErrorCode_DicomMoveUnavailable:
259 return "DicomUserConnection: The C-MOVE command is not supported by the remote SCP";
260
261 case ErrorCode_CannotStoreInstance:
262 return "Cannot store an instance";
263
264 case ErrorCode_CreateDicomNotString:
265 return "Only string values are supported when creating DICOM instances";
266
267 case ErrorCode_CreateDicomOverrideTag:
268 return "Trying to override a value inherited from a parent module";
269
270 case ErrorCode_CreateDicomUseContent:
271 return "Use \"Content\" to inject an image into a new DICOM instance";
272
273 case ErrorCode_CreateDicomNoPayload:
274 return "No payload is present for one instance in the series";
275
276 case ErrorCode_CreateDicomUseDataUriScheme:
277 return "The payload of the DICOM instance must be specified according to Data URI scheme";
278
279 case ErrorCode_CreateDicomBadParent:
280 return "Trying to attach a new DICOM instance to an inexistent resource";
281
282 case ErrorCode_CreateDicomParentIsInstance:
283 return "Trying to attach a new DICOM instance to an instance (must be a series, study or patient)";
284
285 case ErrorCode_CreateDicomParentEncoding:
286 return "Unable to get the encoding of the parent resource";
287
288 case ErrorCode_UnknownModality:
289 return "Unknown modality";
290
291 case ErrorCode_BadJobOrdering:
292 return "Bad ordering of filters in a job";
293
294 case ErrorCode_JsonToLuaTable:
295 return "Cannot convert the given JSON object to a Lua table";
296
297 case ErrorCode_CannotCreateLua:
298 return "Cannot create the Lua context";
299
300 case ErrorCode_CannotExecuteLua:
301 return "Cannot execute a Lua command";
302
303 case ErrorCode_LuaAlreadyExecuted:
304 return "Arguments cannot be pushed after the Lua function is executed";
305
306 case ErrorCode_LuaBadOutput:
307 return "The Lua function does not give the expected number of outputs";
308
309 case ErrorCode_NotLuaPredicate:
310 return "The Lua function is not a predicate (only true/false outputs allowed)";
311
312 case ErrorCode_LuaReturnsNoString:
313 return "The Lua function does not return a string";
314
315 case ErrorCode_StorageAreaAlreadyRegistered:
316 return "Another plugin has already registered a custom storage area";
317
318 case ErrorCode_DatabaseBackendAlreadyRegistered:
319 return "Another plugin has already registered a custom database back-end";
320
321 case ErrorCode_DatabaseNotInitialized:
322 return "Plugin trying to call the database during its initialization";
323
324 case ErrorCode_SslDisabled:
325 return "Orthanc has been built without SSL support";
326
327 case ErrorCode_CannotOrderSlices:
328 return "Unable to order the slices of the series";
329
330 case ErrorCode_NoWorklistHandler:
331 return "No request handler factory for DICOM C-Find Modality SCP";
332
333 case ErrorCode_AlreadyExistingTag:
334 return "Cannot override the value of a tag that already exists";
335
336 default:
337 if (error >= ErrorCode_START_PLUGINS)
338 {
339 return "Error encountered within some plugin";
340 }
341 else
342 {
343 return "Unknown error code";
344 }
345 }
346 }
347
348
349 const char* EnumerationToString(HttpMethod method)
350 {
351 switch (method)
352 {
353 case HttpMethod_Get:
354 return "GET";
355
356 case HttpMethod_Post:
357 return "POST";
358
359 case HttpMethod_Delete:
360 return "DELETE";
361
362 case HttpMethod_Put:
363 return "PUT";
364
365 default:
366 return "?";
367 }
368 }
369
370
371 const char* EnumerationToString(HttpStatus status)
372 {
373 switch (status)
374 {
375 case HttpStatus_100_Continue:
376 return "Continue";
377
378 case HttpStatus_101_SwitchingProtocols:
379 return "Switching Protocols";
380
381 case HttpStatus_102_Processing:
382 return "Processing";
383
384 case HttpStatus_200_Ok:
385 return "OK";
386
387 case HttpStatus_201_Created:
388 return "Created";
389
390 case HttpStatus_202_Accepted:
391 return "Accepted";
392
393 case HttpStatus_203_NonAuthoritativeInformation:
394 return "Non-Authoritative Information";
395
396 case HttpStatus_204_NoContent:
397 return "No Content";
398
399 case HttpStatus_205_ResetContent:
400 return "Reset Content";
401
402 case HttpStatus_206_PartialContent:
403 return "Partial Content";
404
405 case HttpStatus_207_MultiStatus:
406 return "Multi-Status";
407
408 case HttpStatus_208_AlreadyReported:
409 return "Already Reported";
410
411 case HttpStatus_226_IMUsed:
412 return "IM Used";
413
414 case HttpStatus_300_MultipleChoices:
415 return "Multiple Choices";
416
417 case HttpStatus_301_MovedPermanently:
418 return "Moved Permanently";
419
420 case HttpStatus_302_Found:
421 return "Found";
422
423 case HttpStatus_303_SeeOther:
424 return "See Other";
425
426 case HttpStatus_304_NotModified:
427 return "Not Modified";
428
429 case HttpStatus_305_UseProxy:
430 return "Use Proxy";
431
432 case HttpStatus_307_TemporaryRedirect:
433 return "Temporary Redirect";
434
435 case HttpStatus_400_BadRequest:
436 return "Bad Request";
437
438 case HttpStatus_401_Unauthorized:
439 return "Unauthorized";
440
441 case HttpStatus_402_PaymentRequired:
442 return "Payment Required";
443
444 case HttpStatus_403_Forbidden:
445 return "Forbidden";
446
447 case HttpStatus_404_NotFound:
448 return "Not Found";
449
450 case HttpStatus_405_MethodNotAllowed:
451 return "Method Not Allowed";
452
453 case HttpStatus_406_NotAcceptable:
454 return "Not Acceptable";
455
456 case HttpStatus_407_ProxyAuthenticationRequired:
457 return "Proxy Authentication Required";
458
459 case HttpStatus_408_RequestTimeout:
460 return "Request Timeout";
461
462 case HttpStatus_409_Conflict:
463 return "Conflict";
464
465 case HttpStatus_410_Gone:
466 return "Gone";
467
468 case HttpStatus_411_LengthRequired:
469 return "Length Required";
470
471 case HttpStatus_412_PreconditionFailed:
472 return "Precondition Failed";
473
474 case HttpStatus_413_RequestEntityTooLarge:
475 return "Request Entity Too Large";
476
477 case HttpStatus_414_RequestUriTooLong:
478 return "Request-URI Too Long";
479
480 case HttpStatus_415_UnsupportedMediaType:
481 return "Unsupported Media Type";
482
483 case HttpStatus_416_RequestedRangeNotSatisfiable:
484 return "Requested Range Not Satisfiable";
485
486 case HttpStatus_417_ExpectationFailed:
487 return "Expectation Failed";
488
489 case HttpStatus_422_UnprocessableEntity:
490 return "Unprocessable Entity";
491
492 case HttpStatus_423_Locked:
493 return "Locked";
494
495 case HttpStatus_424_FailedDependency:
496 return "Failed Dependency";
497
498 case HttpStatus_426_UpgradeRequired:
499 return "Upgrade Required";
500
501 case HttpStatus_500_InternalServerError:
502 return "Internal Server Error";
503
504 case HttpStatus_501_NotImplemented:
505 return "Not Implemented";
506
507 case HttpStatus_502_BadGateway:
508 return "Bad Gateway";
509
510 case HttpStatus_503_ServiceUnavailable:
511 return "Service Unavailable";
512
513 case HttpStatus_504_GatewayTimeout:
514 return "Gateway Timeout";
515
516 case HttpStatus_505_HttpVersionNotSupported:
517 return "HTTP Version Not Supported";
518
519 case HttpStatus_506_VariantAlsoNegotiates:
520 return "Variant Also Negotiates";
521
522 case HttpStatus_507_InsufficientStorage:
523 return "Insufficient Storage";
524
525 case HttpStatus_509_BandwidthLimitExceeded:
526 return "Bandwidth Limit Exceeded";
527
528 case HttpStatus_510_NotExtended:
529 return "Not Extended";
530
531 default:
532 throw OrthancException(ErrorCode_ParameterOutOfRange);
533 }
534 }
535
536
537 const char* EnumerationToString(ResourceType type)
538 {
539 switch (type)
540 {
541 case ResourceType_Patient:
542 return "Patient";
543
544 case ResourceType_Study:
545 return "Study";
546
547 case ResourceType_Series:
548 return "Series";
549
550 case ResourceType_Instance:
551 return "Instance";
552
553 default:
554 throw OrthancException(ErrorCode_ParameterOutOfRange);
555 }
556 }
557
558
559 const char* EnumerationToString(ImageFormat format)
560 {
561 switch (format)
562 {
563 case ImageFormat_Png:
564 return "Png";
565
566 default:
567 throw OrthancException(ErrorCode_ParameterOutOfRange);
568 }
569 }
570
571
572 const char* EnumerationToString(Encoding encoding)
573 {
574 switch (encoding)
575 {
576 case Encoding_Ascii:
577 return "Ascii";
578
579 case Encoding_Utf8:
580 return "Utf8";
581
582 case Encoding_Latin1:
583 return "Latin1";
584
585 case Encoding_Latin2:
586 return "Latin2";
587
588 case Encoding_Latin3:
589 return "Latin3";
590
591 case Encoding_Latin4:
592 return "Latin4";
593
594 case Encoding_Latin5:
595 return "Latin5";
596
597 case Encoding_Cyrillic:
598 return "Cyrillic";
599
600 case Encoding_Windows1251:
601 return "Windows1251";
602
603 case Encoding_Arabic:
604 return "Arabic";
605
606 case Encoding_Greek:
607 return "Greek";
608
609 case Encoding_Hebrew:
610 return "Hebrew";
611
612 case Encoding_Thai:
613 return "Thai";
614
615 case Encoding_Japanese:
616 return "Japanese";
617
618 case Encoding_Chinese:
619 return "Chinese";
620
621 default:
622 throw OrthancException(ErrorCode_ParameterOutOfRange);
623 }
624 }
625
626
627 const char* EnumerationToString(PhotometricInterpretation photometric)
628 {
629 switch (photometric)
630 {
631 case PhotometricInterpretation_RGB:
632 return "RGB";
633
634 case PhotometricInterpretation_Monochrome1:
635 return "Monochrome1";
636
637 case PhotometricInterpretation_Monochrome2:
638 return "Monochrome2";
639
640 case PhotometricInterpretation_ARGB:
641 return "ARGB";
642
643 case PhotometricInterpretation_CMYK:
644 return "CMYK";
645
646 case PhotometricInterpretation_HSV:
647 return "HSV";
648
649 case PhotometricInterpretation_Palette:
650 return "Palette color";
651
652 case PhotometricInterpretation_YBRFull:
653 return "YBR full";
654
655 case PhotometricInterpretation_YBRFull422:
656 return "YBR full 422";
657
658 case PhotometricInterpretation_YBRPartial420:
659 return "YBR partial 420";
660
661 case PhotometricInterpretation_YBRPartial422:
662 return "YBR partial 422";
663
664 case PhotometricInterpretation_YBR_ICT:
665 return "YBR ICT";
666
667 case PhotometricInterpretation_YBR_RCT:
668 return "YBR RCT";
669
670 case PhotometricInterpretation_Unknown:
671 return "Unknown";
672
673 default:
674 throw OrthancException(ErrorCode_ParameterOutOfRange);
675 }
676 }
677
678
679 const char* EnumerationToString(RequestOrigin origin)
680 {
681 switch (origin)
682 {
683 case RequestOrigin_Unknown:
684 return "Unknown";
685
686 case RequestOrigin_DicomProtocol:
687 return "DicomProtocol";
688
689 case RequestOrigin_RestApi:
690 return "RestApi";
691
692 case RequestOrigin_Plugins:
693 return "Plugins";
694
695 case RequestOrigin_Lua:
696 return "Lua";
697
698 default:
699 throw OrthancException(ErrorCode_ParameterOutOfRange);
700 }
701 }
702
703
704 const char* EnumerationToString(LogLevel level)
705 {
706 switch (level)
707 {
708 case LogLevel_Error:
709 return "ERROR";
710
711 case LogLevel_Warning:
712 return "WARNING";
713
714 case LogLevel_Info:
715 return "INFO";
716
717 case LogLevel_Trace:
718 return "TRACE";
719
720 default:
721 throw OrthancException(ErrorCode_ParameterOutOfRange);
722 }
723 }
724
725
726 const char* EnumerationToString(PixelFormat format)
727 {
728 switch (format)
729 {
730 case PixelFormat_RGB24:
731 return "RGB24";
732
733 case PixelFormat_RGBA32:
734 return "RGBA32";
735
736 case PixelFormat_BGRA32:
737 return "BGRA32";
738
739 case PixelFormat_Grayscale8:
740 return "Grayscale (unsigned 8bpp)";
741
742 case PixelFormat_Grayscale16:
743 return "Grayscale (unsigned 16bpp)";
744
745 case PixelFormat_SignedGrayscale16:
746 return "Grayscale (signed 16bpp)";
747
748 case PixelFormat_Float32:
749 return "Grayscale (float 32bpp)";
750
751 default:
752 throw OrthancException(ErrorCode_ParameterOutOfRange);
753 }
754 }
755
756
757 Encoding StringToEncoding(const char* encoding)
758 {
759 std::string s(encoding);
760 Toolbox::ToUpperCase(s);
761
762 if (s == "UTF8")
763 {
764 return Encoding_Utf8;
765 }
766
767 if (s == "ASCII")
768 {
769 return Encoding_Ascii;
770 }
771
772 if (s == "LATIN1")
773 {
774 return Encoding_Latin1;
775 }
776
777 if (s == "LATIN2")
778 {
779 return Encoding_Latin2;
780 }
781
782 if (s == "LATIN3")
783 {
784 return Encoding_Latin3;
785 }
786
787 if (s == "LATIN4")
788 {
789 return Encoding_Latin4;
790 }
791
792 if (s == "LATIN5")
793 {
794 return Encoding_Latin5;
795 }
796
797 if (s == "CYRILLIC")
798 {
799 return Encoding_Cyrillic;
800 }
801
802 if (s == "WINDOWS1251")
803 {
804 return Encoding_Windows1251;
805 }
806
807 if (s == "ARABIC")
808 {
809 return Encoding_Arabic;
810 }
811
812 if (s == "GREEK")
813 {
814 return Encoding_Greek;
815 }
816
817 if (s == "HEBREW")
818 {
819 return Encoding_Hebrew;
820 }
821
822 if (s == "THAI")
823 {
824 return Encoding_Thai;
825 }
826
827 if (s == "JAPANESE")
828 {
829 return Encoding_Japanese;
830 }
831
832 if (s == "CHINESE")
833 {
834 return Encoding_Chinese;
835 }
836
837 throw OrthancException(ErrorCode_ParameterOutOfRange);
838 }
839
840
841 ResourceType StringToResourceType(const char* type)
842 {
843 std::string s(type);
844 Toolbox::ToUpperCase(s);
845
846 if (s == "PATIENT" || s == "PATIENTS")
847 {
848 return ResourceType_Patient;
849 }
850 else if (s == "STUDY" || s == "STUDIES")
851 {
852 return ResourceType_Study;
853 }
854 else if (s == "SERIES")
855 {
856 return ResourceType_Series;
857 }
858 else if (s == "INSTANCE" || s == "IMAGE" ||
859 s == "INSTANCES" || s == "IMAGES")
860 {
861 return ResourceType_Instance;
862 }
863
864 throw OrthancException(ErrorCode_ParameterOutOfRange);
865 }
866
867
868 ImageFormat StringToImageFormat(const char* format)
869 {
870 std::string s(format);
871 Toolbox::ToUpperCase(s);
872
873 if (s == "PNG")
874 {
875 return ImageFormat_Png;
876 }
877
878 throw OrthancException(ErrorCode_ParameterOutOfRange);
879 }
880
881
882 LogLevel StringToLogLevel(const char *level)
883 {
884 if (strcmp(level, "ERROR") == 0)
885 {
886 return LogLevel_Error;
887 }
888 else if (strcmp(level, "WARNING") == 0)
889 {
890 return LogLevel_Warning;
891 }
892 else if (strcmp(level, "INFO") == 0)
893 {
894 return LogLevel_Info;
895 }
896 else if (strcmp(level, "TRACE") == 0)
897 {
898 return LogLevel_Trace;
899 }
900 else
901 {
902 throw OrthancException(ErrorCode_InternalError);
903 }
904 }
905
906
907 ValueRepresentation StringToValueRepresentation(const std::string& vr,
908 bool throwIfUnsupported)
909 {
910 if (vr == "AE")
911 {
912 return ValueRepresentation_ApplicationEntity;
913 }
914 else if (vr == "AS")
915 {
916 return ValueRepresentation_AgeString;
917 }
918 else if (vr == "AT")
919 {
920 return ValueRepresentation_AttributeTag;
921 }
922 else if (vr == "CS")
923 {
924 return ValueRepresentation_CodeString;
925 }
926 else if (vr == "DA")
927 {
928 return ValueRepresentation_Date;
929 }
930 else if (vr == "DS")
931 {
932 return ValueRepresentation_DecimalString;
933 }
934 else if (vr == "DT")
935 {
936 return ValueRepresentation_DateTime;
937 }
938 else if (vr == "FL")
939 {
940 return ValueRepresentation_FloatingPointSingle;
941 }
942 else if (vr == "FD")
943 {
944 return ValueRepresentation_FloatingPointDouble;
945 }
946 else if (vr == "IS")
947 {
948 return ValueRepresentation_IntegerString;
949 }
950 else if (vr == "LO")
951 {
952 return ValueRepresentation_LongString;
953 }
954 else if (vr == "LT")
955 {
956 return ValueRepresentation_LongText;
957 }
958 else if (vr == "OB")
959 {
960 return ValueRepresentation_OtherByte;
961 }
962 else if (vr == "OD")
963 {
964 return ValueRepresentation_OtherDouble;
965 }
966 else if (vr == "OF")
967 {
968 return ValueRepresentation_OtherFloat;
969 }
970 else if (vr == "OL")
971 {
972 return ValueRepresentation_OtherLong;
973 }
974 else if (vr == "OW")
975 {
976 return ValueRepresentation_OtherWord;
977 }
978 else if (vr == "PN")
979 {
980 return ValueRepresentation_PersonName;
981 }
982 else if (vr == "SH")
983 {
984 return ValueRepresentation_ShortString;
985 }
986 else if (vr == "SL")
987 {
988 return ValueRepresentation_SignedLong;
989 }
990 else if (vr == "SQ")
991 {
992 return ValueRepresentation_Sequence;
993 }
994 else if (vr == "SS")
995 {
996 return ValueRepresentation_SignedShort;
997 }
998 else if (vr == "ST")
999 {
1000 return ValueRepresentation_ShortText;
1001 }
1002 else if (vr == "TM")
1003 {
1004 return ValueRepresentation_Time;
1005 }
1006 else if (vr == "UC")
1007 {
1008 return ValueRepresentation_UnlimitedCharacters;
1009 }
1010 else if (vr == "UI")
1011 {
1012 return ValueRepresentation_UniqueIdentifier;
1013 }
1014 else if (vr == "UL")
1015 {
1016 return ValueRepresentation_UnsignedLong;
1017 }
1018 else if (vr == "UN")
1019 {
1020 return ValueRepresentation_Unknown;
1021 }
1022 else if (vr == "UR")
1023 {
1024 return ValueRepresentation_UniversalResource;
1025 }
1026 else if (vr == "US")
1027 {
1028 return ValueRepresentation_UnsignedShort;
1029 }
1030 else if (vr == "UT")
1031 {
1032 return ValueRepresentation_UnlimitedText;
1033 }
1034 else
1035 {
1036 std::string s = "Unsupported value representation encountered: " + vr;
1037
1038 if (throwIfUnsupported)
1039 {
1040 LOG(ERROR) << s;
1041 throw OrthancException(ErrorCode_ParameterOutOfRange);
1042 }
1043 else
1044 {
1045 LOG(INFO) << s;
1046 return ValueRepresentation_NotSupported;
1047 }
1048 }
1049 }
1050
1051
1052 unsigned int GetBytesPerPixel(PixelFormat format)
1053 {
1054 switch (format)
1055 {
1056 case PixelFormat_Grayscale8:
1057 return 1;
1058
1059 case PixelFormat_Grayscale16:
1060 case PixelFormat_SignedGrayscale16:
1061 return 2;
1062
1063 case PixelFormat_RGB24:
1064 return 3;
1065
1066 case PixelFormat_RGBA32:
1067 case PixelFormat_BGRA32:
1068 return 4;
1069
1070 case PixelFormat_Float32:
1071 assert(sizeof(float) == 4);
1072 return 4;
1073
1074 default:
1075 throw OrthancException(ErrorCode_ParameterOutOfRange);
1076 }
1077 }
1078
1079
1080 bool GetDicomEncoding(Encoding& encoding,
1081 const char* specificCharacterSet)
1082 {
1083 std::string s = specificCharacterSet;
1084 Toolbox::ToUpperCase(s);
1085
1086 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.12.1.1.2
1087 // https://github.com/dcm4che/dcm4che/blob/master/dcm4che-core/src/main/java/org/dcm4che3/data/SpecificCharacterSet.java
1088 if (s == "ISO_IR 6" ||
1089 s == "ISO_IR 192" ||
1090 s == "ISO 2022 IR 6")
1091 {
1092 encoding = Encoding_Utf8;
1093 }
1094 else if (s == "ISO_IR 100" ||
1095 s == "ISO 2022 IR 100")
1096 {
1097 encoding = Encoding_Latin1;
1098 }
1099 else if (s == "ISO_IR 101" ||
1100 s == "ISO 2022 IR 101")
1101 {
1102 encoding = Encoding_Latin2;
1103 }
1104 else if (s == "ISO_IR 109" ||
1105 s == "ISO 2022 IR 109")
1106 {
1107 encoding = Encoding_Latin3;
1108 }
1109 else if (s == "ISO_IR 110" ||
1110 s == "ISO 2022 IR 110")
1111 {
1112 encoding = Encoding_Latin4;
1113 }
1114 else if (s == "ISO_IR 148" ||
1115 s == "ISO 2022 IR 148")
1116 {
1117 encoding = Encoding_Latin5;
1118 }
1119 else if (s == "ISO_IR 144" ||
1120 s == "ISO 2022 IR 144")
1121 {
1122 encoding = Encoding_Cyrillic;
1123 }
1124 else if (s == "ISO_IR 127" ||
1125 s == "ISO 2022 IR 127")
1126 {
1127 encoding = Encoding_Arabic;
1128 }
1129 else if (s == "ISO_IR 126" ||
1130 s == "ISO 2022 IR 126")
1131 {
1132 encoding = Encoding_Greek;
1133 }
1134 else if (s == "ISO_IR 138" ||
1135 s == "ISO 2022 IR 138")
1136 {
1137 encoding = Encoding_Hebrew;
1138 }
1139 else if (s == "ISO_IR 166" || s == "ISO 2022 IR 166")
1140 {
1141 encoding = Encoding_Thai;
1142 }
1143 else if (s == "ISO_IR 13" || s == "ISO 2022 IR 13")
1144 {
1145 encoding = Encoding_Japanese;
1146 }
1147 else if (s == "GB18030")
1148 {
1149 encoding = Encoding_Chinese;
1150 }
1151 /*
1152 else if (s == "ISO 2022 IR 149")
1153 {
1154 TODO
1155 }
1156 else if (s == "ISO 2022 IR 159")
1157 {
1158 TODO
1159 }
1160 else if (s == "ISO 2022 IR 87")
1161 {
1162 TODO
1163 }
1164 */
1165 else
1166 {
1167 return false;
1168 }
1169
1170 // The encoding was properly detected
1171 return true;
1172 }
1173
1174
1175 ResourceType GetChildResourceType(ResourceType type)
1176 {
1177 switch (type)
1178 {
1179 case ResourceType_Patient:
1180 return ResourceType_Study;
1181
1182 case ResourceType_Study:
1183 return ResourceType_Series;
1184
1185 case ResourceType_Series:
1186 return ResourceType_Instance;
1187
1188 default:
1189 throw OrthancException(ErrorCode_ParameterOutOfRange);
1190 }
1191 }
1192
1193
1194 ResourceType GetParentResourceType(ResourceType type)
1195 {
1196 switch (type)
1197 {
1198 case ResourceType_Study:
1199 return ResourceType_Patient;
1200
1201 case ResourceType_Series:
1202 return ResourceType_Study;
1203
1204 case ResourceType_Instance:
1205 return ResourceType_Series;
1206
1207 default:
1208 throw OrthancException(ErrorCode_ParameterOutOfRange);
1209 }
1210 }
1211
1212
1213 DicomModule GetModule(ResourceType type)
1214 {
1215 switch (type)
1216 {
1217 case ResourceType_Patient:
1218 return DicomModule_Patient;
1219
1220 case ResourceType_Study:
1221 return DicomModule_Study;
1222
1223 case ResourceType_Series:
1224 return DicomModule_Series;
1225
1226 default:
1227 throw OrthancException(ErrorCode_ParameterOutOfRange);
1228 }
1229 }
1230
1231
1232
1233 const char* GetDicomSpecificCharacterSet(Encoding encoding)
1234 {
1235 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.12.1.1.2
1236 switch (encoding)
1237 {
1238 case Encoding_Utf8:
1239 case Encoding_Ascii:
1240 return "ISO_IR 192";
1241
1242 case Encoding_Latin1:
1243 return "ISO_IR 100";
1244
1245 case Encoding_Latin2:
1246 return "ISO_IR 101";
1247
1248 case Encoding_Latin3:
1249 return "ISO_IR 109";
1250
1251 case Encoding_Latin4:
1252 return "ISO_IR 110";
1253
1254 case Encoding_Latin5:
1255 return "ISO_IR 148";
1256
1257 case Encoding_Cyrillic:
1258 return "ISO_IR 144";
1259
1260 case Encoding_Arabic:
1261 return "ISO_IR 127";
1262
1263 case Encoding_Greek:
1264 return "ISO_IR 126";
1265
1266 case Encoding_Hebrew:
1267 return "ISO_IR 138";
1268
1269 case Encoding_Japanese:
1270 return "ISO_IR 13";
1271
1272 case Encoding_Chinese:
1273 return "GB18030";
1274
1275 case Encoding_Thai:
1276 return "ISO_IR 166";
1277
1278 default:
1279 throw OrthancException(ErrorCode_ParameterOutOfRange);
1280 }
1281 }
1282
1283
1284 // This function is autogenerated by the script
1285 // "Resources/GenerateErrorCodes.py"
1286 HttpStatus ConvertErrorCodeToHttpStatus(ErrorCode error)
1287 {
1288 switch (error)
1289 {
1290 case ErrorCode_Success:
1291 return HttpStatus_200_Ok;
1292
1293 case ErrorCode_ParameterOutOfRange:
1294 return HttpStatus_400_BadRequest;
1295
1296 case ErrorCode_BadParameterType:
1297 return HttpStatus_400_BadRequest;
1298
1299 case ErrorCode_InexistentItem:
1300 return HttpStatus_404_NotFound;
1301
1302 case ErrorCode_BadRequest:
1303 return HttpStatus_400_BadRequest;
1304
1305 case ErrorCode_UriSyntax:
1306 return HttpStatus_400_BadRequest;
1307
1308 case ErrorCode_InexistentFile:
1309 return HttpStatus_404_NotFound;
1310
1311 case ErrorCode_BadFileFormat:
1312 return HttpStatus_400_BadRequest;
1313
1314 case ErrorCode_UnknownResource:
1315 return HttpStatus_404_NotFound;
1316
1317 case ErrorCode_InexistentTag:
1318 return HttpStatus_404_NotFound;
1319
1320 case ErrorCode_BadJson:
1321 return HttpStatus_400_BadRequest;
1322
1323 case ErrorCode_Unauthorized:
1324 return HttpStatus_401_Unauthorized;
1325
1326 case ErrorCode_NotAcceptable:
1327 return HttpStatus_406_NotAcceptable;
1328
1329 default:
1330 return HttpStatus_500_InternalServerError;
1331 }
1332 }
1333
1334
1335 bool IsUserContentType(FileContentType type)
1336 {
1337 return (type >= FileContentType_StartUser &&
1338 type <= FileContentType_EndUser);
1339 }
1340
1341
1342 bool IsBinaryValueRepresentation(ValueRepresentation vr)
1343 {
1344 // http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html
1345
1346 switch (vr)
1347 {
1348 case ValueRepresentation_ApplicationEntity: // AE
1349 case ValueRepresentation_AgeString: // AS
1350 case ValueRepresentation_CodeString: // CS
1351 case ValueRepresentation_Date: // DA
1352 case ValueRepresentation_DecimalString: // DS
1353 case ValueRepresentation_DateTime: // DT
1354 case ValueRepresentation_IntegerString: // IS
1355 case ValueRepresentation_LongString: // LO
1356 case ValueRepresentation_LongText: // LT
1357 case ValueRepresentation_PersonName: // PN
1358 case ValueRepresentation_ShortString: // SH
1359 case ValueRepresentation_ShortText: // ST
1360 case ValueRepresentation_Time: // TM
1361 case ValueRepresentation_UnlimitedCharacters: // UC
1362 case ValueRepresentation_UniqueIdentifier: // UI (UID)
1363 case ValueRepresentation_UniversalResource: // UR (URI or URL)
1364 case ValueRepresentation_UnlimitedText: // UT
1365 {
1366 return false;
1367 }
1368
1369 /**
1370 * Below are all the VR whose character repertoire is tagged as
1371 * "not applicable"
1372 **/
1373 case ValueRepresentation_AttributeTag: // AT (2 x uint16_t)
1374 case ValueRepresentation_FloatingPointSingle: // FL (float)
1375 case ValueRepresentation_FloatingPointDouble: // FD (double)
1376 case ValueRepresentation_OtherByte: // OB
1377 case ValueRepresentation_OtherDouble: // OD
1378 case ValueRepresentation_OtherFloat: // OF
1379 case ValueRepresentation_OtherLong: // OL
1380 case ValueRepresentation_OtherWord: // OW
1381 case ValueRepresentation_SignedLong: // SL (int32_t)
1382 case ValueRepresentation_Sequence: // SQ
1383 case ValueRepresentation_SignedShort: // SS (int16_t)
1384 case ValueRepresentation_UnsignedLong: // UL (uint32_t)
1385 case ValueRepresentation_Unknown: // UN
1386 case ValueRepresentation_UnsignedShort: // US (uint16_t)
1387 {
1388 return true;
1389 }
1390
1391 case ValueRepresentation_NotSupported:
1392 default:
1393 throw OrthancException(ErrorCode_ParameterOutOfRange);
1394 }
1395 }
1396 }