comparison Orthanc/Core/Enumerations.cpp @ 25:15acbf5e7545

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jun 2015 11:16:30 +0200
parents
children fe8dab5c051f
comparison
equal deleted inserted replaced
24:ed9acb0f938e 25:15acbf5e7545
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 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
39 namespace Orthanc
40 {
41 const char* EnumerationToString(HttpMethod method)
42 {
43 switch (method)
44 {
45 case HttpMethod_Get:
46 return "GET";
47
48 case HttpMethod_Post:
49 return "POST";
50
51 case HttpMethod_Delete:
52 return "DELETE";
53
54 case HttpMethod_Put:
55 return "PUT";
56
57 default:
58 return "?";
59 }
60 }
61
62
63 const char* EnumerationToString(HttpStatus status)
64 {
65 switch (status)
66 {
67 case HttpStatus_100_Continue:
68 return "Continue";
69
70 case HttpStatus_101_SwitchingProtocols:
71 return "Switching Protocols";
72
73 case HttpStatus_102_Processing:
74 return "Processing";
75
76 case HttpStatus_200_Ok:
77 return "OK";
78
79 case HttpStatus_201_Created:
80 return "Created";
81
82 case HttpStatus_202_Accepted:
83 return "Accepted";
84
85 case HttpStatus_203_NonAuthoritativeInformation:
86 return "Non-Authoritative Information";
87
88 case HttpStatus_204_NoContent:
89 return "No Content";
90
91 case HttpStatus_205_ResetContent:
92 return "Reset Content";
93
94 case HttpStatus_206_PartialContent:
95 return "Partial Content";
96
97 case HttpStatus_207_MultiStatus:
98 return "Multi-Status";
99
100 case HttpStatus_208_AlreadyReported:
101 return "Already Reported";
102
103 case HttpStatus_226_IMUsed:
104 return "IM Used";
105
106 case HttpStatus_300_MultipleChoices:
107 return "Multiple Choices";
108
109 case HttpStatus_301_MovedPermanently:
110 return "Moved Permanently";
111
112 case HttpStatus_302_Found:
113 return "Found";
114
115 case HttpStatus_303_SeeOther:
116 return "See Other";
117
118 case HttpStatus_304_NotModified:
119 return "Not Modified";
120
121 case HttpStatus_305_UseProxy:
122 return "Use Proxy";
123
124 case HttpStatus_307_TemporaryRedirect:
125 return "Temporary Redirect";
126
127 case HttpStatus_400_BadRequest:
128 return "Bad Request";
129
130 case HttpStatus_401_Unauthorized:
131 return "Unauthorized";
132
133 case HttpStatus_402_PaymentRequired:
134 return "Payment Required";
135
136 case HttpStatus_403_Forbidden:
137 return "Forbidden";
138
139 case HttpStatus_404_NotFound:
140 return "Not Found";
141
142 case HttpStatus_405_MethodNotAllowed:
143 return "Method Not Allowed";
144
145 case HttpStatus_406_NotAcceptable:
146 return "Not Acceptable";
147
148 case HttpStatus_407_ProxyAuthenticationRequired:
149 return "Proxy Authentication Required";
150
151 case HttpStatus_408_RequestTimeout:
152 return "Request Timeout";
153
154 case HttpStatus_409_Conflict:
155 return "Conflict";
156
157 case HttpStatus_410_Gone:
158 return "Gone";
159
160 case HttpStatus_411_LengthRequired:
161 return "Length Required";
162
163 case HttpStatus_412_PreconditionFailed:
164 return "Precondition Failed";
165
166 case HttpStatus_413_RequestEntityTooLarge:
167 return "Request Entity Too Large";
168
169 case HttpStatus_414_RequestUriTooLong:
170 return "Request-URI Too Long";
171
172 case HttpStatus_415_UnsupportedMediaType:
173 return "Unsupported Media Type";
174
175 case HttpStatus_416_RequestedRangeNotSatisfiable:
176 return "Requested Range Not Satisfiable";
177
178 case HttpStatus_417_ExpectationFailed:
179 return "Expectation Failed";
180
181 case HttpStatus_422_UnprocessableEntity:
182 return "Unprocessable Entity";
183
184 case HttpStatus_423_Locked:
185 return "Locked";
186
187 case HttpStatus_424_FailedDependency:
188 return "Failed Dependency";
189
190 case HttpStatus_426_UpgradeRequired:
191 return "Upgrade Required";
192
193 case HttpStatus_500_InternalServerError:
194 return "Internal Server Error";
195
196 case HttpStatus_501_NotImplemented:
197 return "Not Implemented";
198
199 case HttpStatus_502_BadGateway:
200 return "Bad Gateway";
201
202 case HttpStatus_503_ServiceUnavailable:
203 return "Service Unavailable";
204
205 case HttpStatus_504_GatewayTimeout:
206 return "Gateway Timeout";
207
208 case HttpStatus_505_HttpVersionNotSupported:
209 return "HTTP Version Not Supported";
210
211 case HttpStatus_506_VariantAlsoNegotiates:
212 return "Variant Also Negotiates";
213
214 case HttpStatus_507_InsufficientStorage:
215 return "Insufficient Storage";
216
217 case HttpStatus_509_BandwidthLimitExceeded:
218 return "Bandwidth Limit Exceeded";
219
220 case HttpStatus_510_NotExtended:
221 return "Not Extended";
222
223 default:
224 throw OrthancException(ErrorCode_ParameterOutOfRange);
225 }
226 }
227
228
229 const char* EnumerationToString(ResourceType type)
230 {
231 switch (type)
232 {
233 case ResourceType_Patient:
234 return "Patient";
235
236 case ResourceType_Study:
237 return "Study";
238
239 case ResourceType_Series:
240 return "Series";
241
242 case ResourceType_Instance:
243 return "Instance";
244
245 default:
246 throw OrthancException(ErrorCode_ParameterOutOfRange);
247 }
248 }
249
250
251 const char* EnumerationToString(ImageFormat format)
252 {
253 switch (format)
254 {
255 case ImageFormat_Png:
256 return "Png";
257
258 default:
259 throw OrthancException(ErrorCode_ParameterOutOfRange);
260 }
261 }
262
263
264 const char* EnumerationToString(Encoding encoding)
265 {
266 switch (encoding)
267 {
268 case Encoding_Ascii:
269 return "Ascii";
270
271 case Encoding_Utf8:
272 return "Utf8";
273
274 case Encoding_Latin1:
275 return "Latin1";
276
277 case Encoding_Latin2:
278 return "Latin2";
279
280 case Encoding_Latin3:
281 return "Latin3";
282
283 case Encoding_Latin4:
284 return "Latin4";
285
286 case Encoding_Latin5:
287 return "Latin5";
288
289 case Encoding_Cyrillic:
290 return "Cyrillic";
291
292 case Encoding_Windows1251:
293 return "Windows1251";
294
295 case Encoding_Arabic:
296 return "Arabic";
297
298 case Encoding_Greek:
299 return "Greek";
300
301 case Encoding_Hebrew:
302 return "Hebrew";
303
304 case Encoding_Thai:
305 return "Thai";
306
307 case Encoding_Japanese:
308 return "Japanese";
309
310 case Encoding_Chinese:
311 return "Chinese";
312
313 default:
314 throw OrthancException(ErrorCode_ParameterOutOfRange);
315 }
316 }
317
318
319 const char* EnumerationToString(PhotometricInterpretation photometric)
320 {
321 switch (photometric)
322 {
323 case PhotometricInterpretation_RGB:
324 return "RGB";
325
326 case PhotometricInterpretation_Monochrome1:
327 return "Monochrome1";
328
329 case PhotometricInterpretation_Monochrome2:
330 return "Monochrome2";
331
332 case PhotometricInterpretation_ARGB:
333 return "ARGB";
334
335 case PhotometricInterpretation_CMYK:
336 return "CMYK";
337
338 case PhotometricInterpretation_HSV:
339 return "HSV";
340
341 case PhotometricInterpretation_Palette:
342 return "Palette color";
343
344 case PhotometricInterpretation_YBRFull:
345 return "YBR full";
346
347 case PhotometricInterpretation_YBRFull422:
348 return "YBR full 422";
349
350 case PhotometricInterpretation_YBRPartial420:
351 return "YBR partial 420";
352
353 case PhotometricInterpretation_YBRPartial422:
354 return "YBR partial 422";
355
356 case PhotometricInterpretation_YBR_ICT:
357 return "YBR ICT";
358
359 case PhotometricInterpretation_YBR_RCT:
360 return "YBR RCT";
361
362 case PhotometricInterpretation_Unknown:
363 return "Unknown";
364
365 default:
366 throw OrthancException(ErrorCode_ParameterOutOfRange);
367 }
368 }
369
370
371 Encoding StringToEncoding(const char* encoding)
372 {
373 std::string s(encoding);
374 Toolbox::ToUpperCase(s);
375
376 if (s == "UTF8")
377 {
378 return Encoding_Utf8;
379 }
380
381 if (s == "ASCII")
382 {
383 return Encoding_Ascii;
384 }
385
386 if (s == "LATIN1")
387 {
388 return Encoding_Latin1;
389 }
390
391 if (s == "LATIN2")
392 {
393 return Encoding_Latin2;
394 }
395
396 if (s == "LATIN3")
397 {
398 return Encoding_Latin3;
399 }
400
401 if (s == "LATIN4")
402 {
403 return Encoding_Latin4;
404 }
405
406 if (s == "LATIN5")
407 {
408 return Encoding_Latin5;
409 }
410
411 if (s == "CYRILLIC")
412 {
413 return Encoding_Cyrillic;
414 }
415
416 if (s == "WINDOWS1251")
417 {
418 return Encoding_Windows1251;
419 }
420
421 if (s == "ARABIC")
422 {
423 return Encoding_Arabic;
424 }
425
426 if (s == "GREEK")
427 {
428 return Encoding_Greek;
429 }
430
431 if (s == "HEBREW")
432 {
433 return Encoding_Hebrew;
434 }
435
436 if (s == "THAI")
437 {
438 return Encoding_Thai;
439 }
440
441 if (s == "JAPANESE")
442 {
443 return Encoding_Japanese;
444 }
445
446 if (s == "CHINESE")
447 {
448 return Encoding_Chinese;
449 }
450
451 throw OrthancException(ErrorCode_ParameterOutOfRange);
452 }
453
454
455 ResourceType StringToResourceType(const char* type)
456 {
457 std::string s(type);
458 Toolbox::ToUpperCase(s);
459
460 if (s == "PATIENT" || s == "PATIENTS")
461 {
462 return ResourceType_Patient;
463 }
464 else if (s == "STUDY" || s == "STUDIES")
465 {
466 return ResourceType_Study;
467 }
468 else if (s == "SERIES")
469 {
470 return ResourceType_Series;
471 }
472 else if (s == "INSTANCE" || s == "IMAGE" ||
473 s == "INSTANCES" || s == "IMAGES")
474 {
475 return ResourceType_Instance;
476 }
477
478 throw OrthancException(ErrorCode_ParameterOutOfRange);
479 }
480
481
482 ImageFormat StringToImageFormat(const char* format)
483 {
484 std::string s(format);
485 Toolbox::ToUpperCase(s);
486
487 if (s == "PNG")
488 {
489 return ImageFormat_Png;
490 }
491
492 throw OrthancException(ErrorCode_ParameterOutOfRange);
493 }
494
495
496 unsigned int GetBytesPerPixel(PixelFormat format)
497 {
498 switch (format)
499 {
500 case PixelFormat_Grayscale8:
501 return 1;
502
503 case PixelFormat_Grayscale16:
504 case PixelFormat_SignedGrayscale16:
505 return 2;
506
507 case PixelFormat_RGB24:
508 return 3;
509
510 case PixelFormat_RGBA32:
511 return 4;
512
513 default:
514 throw OrthancException(ErrorCode_ParameterOutOfRange);
515 }
516 }
517
518
519 bool GetDicomEncoding(Encoding& encoding,
520 const char* specificCharacterSet)
521 {
522 std::string s = specificCharacterSet;
523 Toolbox::ToUpperCase(s);
524
525 // http://www.dabsoft.ch/dicom/3/C.12.1.1.2/
526 // https://github.com/dcm4che/dcm4che/blob/master/dcm4che-core/src/main/java/org/dcm4che3/data/SpecificCharacterSet.java
527 if (s == "ISO_IR 6" ||
528 s == "ISO_IR 192" ||
529 s == "ISO 2022 IR 6")
530 {
531 encoding = Encoding_Utf8;
532 }
533 else if (s == "ISO_IR 100" ||
534 s == "ISO 2022 IR 100")
535 {
536 encoding = Encoding_Latin1;
537 }
538 else if (s == "ISO_IR 101" ||
539 s == "ISO 2022 IR 101")
540 {
541 encoding = Encoding_Latin2;
542 }
543 else if (s == "ISO_IR 109" ||
544 s == "ISO 2022 IR 109")
545 {
546 encoding = Encoding_Latin3;
547 }
548 else if (s == "ISO_IR 110" ||
549 s == "ISO 2022 IR 110")
550 {
551 encoding = Encoding_Latin4;
552 }
553 else if (s == "ISO_IR 148" ||
554 s == "ISO 2022 IR 148")
555 {
556 encoding = Encoding_Latin5;
557 }
558 else if (s == "ISO_IR 144" ||
559 s == "ISO 2022 IR 144")
560 {
561 encoding = Encoding_Cyrillic;
562 }
563 else if (s == "ISO_IR 127" ||
564 s == "ISO 2022 IR 127")
565 {
566 encoding = Encoding_Arabic;
567 }
568 else if (s == "ISO_IR 126" ||
569 s == "ISO 2022 IR 126")
570 {
571 encoding = Encoding_Greek;
572 }
573 else if (s == "ISO_IR 138" ||
574 s == "ISO 2022 IR 138")
575 {
576 encoding = Encoding_Hebrew;
577 }
578 else if (s == "ISO_IR 166" || s == "ISO 2022 IR 166")
579 {
580 encoding = Encoding_Thai;
581 }
582 else if (s == "ISO_IR 13" || s == "ISO 2022 IR 13")
583 {
584 encoding = Encoding_Japanese;
585 }
586 else if (s == "GB18030")
587 {
588 encoding = Encoding_Chinese;
589 }
590 /*
591 else if (s == "ISO 2022 IR 149")
592 {
593 TODO
594 }
595 else if (s == "ISO 2022 IR 159")
596 {
597 TODO
598 }
599 else if (s == "ISO 2022 IR 87")
600 {
601 TODO
602 }
603 */
604 else
605 {
606 return false;
607 }
608
609 // The encoding was properly detected
610 return true;
611 }
612
613
614 const char* GetMimeType(FileContentType type)
615 {
616 switch (type)
617 {
618 case FileContentType_Dicom:
619 return "application/dicom";
620
621 case FileContentType_DicomAsJson:
622 return "application/json";
623
624 default:
625 return "application/octet-stream";
626 }
627 }
628 }