annotate OrthancServer/Internals/DicomImageDecoder.cpp @ 1294:910478b2d4e4

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 06 Feb 2015 13:48:05 +0100
parents 6e7e5ed91c2d
children f967bdf8534e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1206
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1206
diff changeset
4 * Department, University Hospital of Liege, Belgium
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "../PrecompiledHeadersServer.h"
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "DicomImageDecoder.h"
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
36
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
37 /*=========================================================================
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
38
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
39 This file is based on portions of the following project
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
40 (cf. function "DecodePsmctRle1()"):
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
41
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
42 Program: GDCM (Grassroots DICOM). A DICOM library
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
43 Module: http://gdcm.sourceforge.net/Copyright.html
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
44
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
45 Copyright (c) 2006-2011 Mathieu Malaterre
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
46 Copyright (c) 1993-2005 CREATIS
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
47 (CREATIS = Centre de Recherche et d'Applications en Traitement de l'Image)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
48 All rights reserved.
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
49
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
50 Redistribution and use in source and binary forms, with or without
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
51 modification, are permitted provided that the following conditions are met:
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
52
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
53 * Redistributions of source code must retain the above copyright notice,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
54 this list of conditions and the following disclaimer.
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
55
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
56 * Redistributions in binary form must reproduce the above copyright notice,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
57 this list of conditions and the following disclaimer in the documentation
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
58 and/or other materials provided with the distribution.
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
59
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
60 * Neither name of Mathieu Malaterre, or CREATIS, nor the names of any
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
61 contributors (CNRS, INSERM, UCB, Universite Lyon I), may be used to
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
62 endorse or promote products derived from this software without specific
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
63 prior written permission.
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
64
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
65 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
66 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
67 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
68 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
69 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
70 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
71 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
72 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
73 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
74 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
75
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
76 =========================================================================*/
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
77
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
78
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
79
874
87791ebc1f50 download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 863
diff changeset
80 #include "../PrecompiledHeadersServer.h"
87791ebc1f50 download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 863
diff changeset
81 #include "DicomImageDecoder.h"
87791ebc1f50 download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 863
diff changeset
82
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 #include "../../Core/OrthancException.h"
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
84 #include "../../Core/ImageFormats/ImageProcessing.h"
861
a546b05a43da fix PngWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 860
diff changeset
85 #include "../../Core/ImageFormats/PngWriter.h" // TODO REMOVE THIS
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
86 #include "../../Core/DicomFormat/DicomIntegerPixelAccessor.h"
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 #include "../ToDcmtkBridge.h"
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
88 #include "../FromDcmtkBridge.h"
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
90 #include <glog/logging.h>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
91
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
92 #include <boost/lexical_cast.hpp>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
93
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
94 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 #include <dcmtk/dcmjpls/djcodecd.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 #include <dcmtk/dcmjpls/djcparam.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 #include <dcmtk/dcmjpeg/djrplol.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 #endif
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 namespace Orthanc
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
103 static const DicomTag DICOM_TAG_CONTENT(0x07a1, 0x100a);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
104 static const DicomTag DICOM_TAG_COMPRESSION_TYPE(0x07a1, 0x1011);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
105
942
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
106
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
107 static bool IsJpegLossless(const DcmDataset& dataset)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
108 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
109 // http://support.dcmtk.org/docs/dcxfer_8h-source.html
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
110 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless ||
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
111 dataset.getOriginalXfer() == EXS_JPEGLSLossy);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
112 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
113
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
114
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
115 static bool IsPsmctRle1(DcmDataset& dataset)
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
116 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
117 DcmElement* e;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
118 char* c;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
119
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
120 // Check whether the DICOM instance contains an image encoded with
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
121 // the PMSCT_RLE1 scheme.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
122 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_COMPRESSION_TYPE), e).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
123 e == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
124 !e->isaString() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
125 !e->getString(c).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
126 c == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
127 strcmp("PMSCT_RLE1", c))
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
128 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
129 return false;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
130 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
131 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
132 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
133 return true;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
134 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
135 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
136
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
137
942
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
138 static bool DecodePsmctRle1(std::string& output,
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
139 DcmDataset& dataset)
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
140 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
141 // Check whether the DICOM instance contains an image encoded with
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
142 // the PMSCT_RLE1 scheme.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
143 if (!IsPsmctRle1(dataset))
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
144 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
145 return false;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
146 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
147
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
148 // OK, this is a custom RLE encoding from Philips. Get the pixel
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
149 // data from the appropriate private DICOM tag.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
150 Uint8* pixData = NULL;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
151 DcmElement* e;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
152 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_CONTENT), e).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
153 e == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
154 e->getUint8Array(pixData) != EC_Normal)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
155 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
156 return false;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
157 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
158
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
159 // The "unsigned" below IS VERY IMPORTANT
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
160 const uint8_t* inbuffer = reinterpret_cast<const uint8_t*>(pixData);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
161 const size_t length = e->getLength();
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
162
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
163 /**
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
164 * The code below is an adaptation of a sample code for GDCM by
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
165 * Mathieu Malaterre (under a BSD license).
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
166 * http://gdcm.sourceforge.net/html/rle2img_8cxx-example.html
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
167 **/
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
168
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
169 // RLE pass
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
170 std::vector<uint8_t> temp;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
171 temp.reserve(length);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
172 for (size_t i = 0; i < length; i++)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
173 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
174 if (inbuffer[i] == 0xa5)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
175 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
176 temp.push_back(inbuffer[i+2]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
177 for (uint8_t repeat = inbuffer[i + 1]; repeat != 0; repeat--)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
178 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
179 temp.push_back(inbuffer[i+2]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
180 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
181 i += 2;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
182 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
183 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
184 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
185 temp.push_back(inbuffer[i]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
186 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
187 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
188
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
189 // Delta encoding pass
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
190 uint16_t delta = 0;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
191 output.clear();
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
192 output.reserve(temp.size());
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
193 for (size_t i = 0; i < temp.size(); i++)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
194 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
195 uint16_t value;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
196
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
197 if (temp[i] == 0x5a)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
198 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
199 uint16_t v1 = temp[i + 1];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
200 uint16_t v2 = temp[i + 2];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
201 value = (v2 << 8) + v1;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
202 i += 2;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
203 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
204 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
205 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
206 value = delta + (int8_t) temp[i];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
207 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
208
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
209 output.push_back(value & 0xff);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
210 output.push_back(value >> 8);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
211 delta = value;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
212 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
213
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
214 if (output.size() % 2)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
215 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
216 output.resize(output.size() - 1);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
217 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
218
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
219 return true;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
220 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
221
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
222
942
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
223 class DicomImageDecoder::ImageSource
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
224 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
225 private:
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
226 std::string psmct_;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
227 std::auto_ptr<DicomIntegerPixelAccessor> slowAccessor_;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
228
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
229 public:
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
230 void Setup(DcmDataset& dataset,
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
231 unsigned int frame)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
232 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
233 psmct_.clear();
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
234 slowAccessor_.reset(NULL);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
235
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
236 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
237
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
238 DicomMap m;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
239 FromDcmtkBridge::Convert(m, dataset);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
240
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
241 /**
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
242 * Create an accessor to the raw values of the DICOM image.
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
243 **/
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
244
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
245 DcmElement* e;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
246 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() &&
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
247 e != NULL)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
248 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
249 Uint8* pixData = NULL;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
250 if (e->getUint8Array(pixData) == EC_Normal)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
251 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
252 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength()));
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
253 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
254 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
255 else if (DecodePsmctRle1(psmct_, dataset))
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
256 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
257 LOG(INFO) << "The PMSCT_RLE1 decoding has succeeded";
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
258 Uint8* pixData = NULL;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
259 if (psmct_.size() > 0)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
260 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
261 pixData = reinterpret_cast<Uint8*>(&psmct_[0]);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
262 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
263
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
264 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, psmct_.size()));
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
265 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
266
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
267 if (slowAccessor_.get() == NULL)
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
268 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
269 throw OrthancException(ErrorCode_BadFileFormat);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
270 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
271
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
272 slowAccessor_->SetCurrentFrame(frame);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
273 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
274
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
275 unsigned int GetWidth() const
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
276 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
277 assert(slowAccessor_.get() != NULL);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
278 return slowAccessor_->GetInformation().GetWidth();
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
279 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
280
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
281 unsigned int GetHeight() const
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
282 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
283 assert(slowAccessor_.get() != NULL);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
284 return slowAccessor_->GetInformation().GetHeight();
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
285 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
286
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
287 unsigned int GetChannelCount() const
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
288 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
289 assert(slowAccessor_.get() != NULL);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
290 return slowAccessor_->GetInformation().GetChannelCount();
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
291 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
292
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
293 const DicomIntegerPixelAccessor& GetAccessor() const
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
294 {
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
295 assert(slowAccessor_.get() != NULL);
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
296 return *slowAccessor_;
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
297 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
298
1190
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
299 unsigned int GetSize() const
942
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
300 {
1190
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
301 assert(slowAccessor_.get() != NULL);
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
302 return slowAccessor_->GetSize();
942
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
303 }
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
304 };
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
305
b3f6fb1130cd fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
306
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target,
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 DcmDataset& dataset)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 {
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
310 DicomMap m;
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
311 FromDcmtkBridge::Convert(m, dataset);
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
313 DicomImageInformation info(m);
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
314 PixelFormat format;
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
315
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
316 if (!info.ExtractPixelFormat(format))
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 {
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
318 LOG(WARNING) << "Unsupported DICOM image: " << info.GetBitsStored()
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
319 << "bpp, " << info.GetChannelCount() << " channels, "
860
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 859
diff changeset
320 << (info.IsSigned() ? "signed" : "unsigned")
1206
f5b0207967bc Fix issue 19 (YBR_FULL are decoded incorrectly)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1190
diff changeset
321 << (info.IsPlanar() ? ", planar, " : ", non-planar, ")
f5b0207967bc Fix issue 19 (YBR_FULL are decoded incorrectly)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1190
diff changeset
322 << EnumerationToString(info.GetPhotometricInterpretation())
f5b0207967bc Fix issue 19 (YBR_FULL are decoded incorrectly)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1190
diff changeset
323 << " photometric interpretation";
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 throw OrthancException(ErrorCode_NotImplemented);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 }
874
87791ebc1f50 download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 863
diff changeset
326
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
327 target.SetHeight(info.GetHeight());
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
328 target.SetWidth(info.GetWidth());
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
329 target.SetFormat(format);
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
333 bool DicomImageDecoder::IsUncompressedImage(const DcmDataset& dataset)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
334 {
851
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 850
diff changeset
335 // http://support.dcmtk.org/docs/dcxfer_8h-source.html
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
336 return (dataset.getOriginalXfer() == EXS_Unknown ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
337 dataset.getOriginalXfer() == EXS_LittleEndianImplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
338 dataset.getOriginalXfer() == EXS_BigEndianImplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
339 dataset.getOriginalXfer() == EXS_LittleEndianExplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
340 dataset.getOriginalXfer() == EXS_BigEndianExplicit);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
341 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
342
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
343
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
344 template <typename PixelType>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
345 static void CopyPixels(ImageAccessor& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
346 const DicomIntegerPixelAccessor& source)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
347 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
348 const PixelType minValue = std::numeric_limits<PixelType>::min();
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
349 const PixelType maxValue = std::numeric_limits<PixelType>::max();
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
350
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 852
diff changeset
351 for (unsigned int y = 0; y < source.GetInformation().GetHeight(); y++)
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
352 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
353 PixelType* pixel = reinterpret_cast<PixelType*>(target.GetRow(y));
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 852
diff changeset
354 for (unsigned int x = 0; x < source.GetInformation().GetWidth(); x++)
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
355 {
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 852
diff changeset
356 for (unsigned int c = 0; c < source.GetInformation().GetChannelCount(); c++, pixel++)
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
357 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
358 int32_t v = source.GetValue(x, y, c);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
359 if (v < static_cast<int32_t>(minValue))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
360 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
361 *pixel = minValue;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
362 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
363 else if (v > static_cast<int32_t>(maxValue))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
364 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
365 *pixel = maxValue;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
366 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
367 else
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
368 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
369 *pixel = static_cast<PixelType>(v);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
370 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
371 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
372 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
373 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
374 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
375
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
376
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
377 void DicomImageDecoder::DecodeUncompressedImage(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
378 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
379 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
380 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
381 if (!IsUncompressedImage(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
382 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
383 throw OrthancException(ErrorCode_BadParameterType);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
384 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
385
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
386 DecodeUncompressedImageInternal(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
387 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
388
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
389
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
390 void DicomImageDecoder::DecodeUncompressedImageInternal(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
391 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
392 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
393 {
852
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
394 ImageSource source;
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
395 source.Setup(dataset, frame);
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
396
858
ebc41566f742 removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
397
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
398 /**
858
ebc41566f742 removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
399 * Resize the target image.
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
400 **/
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
401
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
402 SetupImageBuffer(target, dataset);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
403
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
404 if (source.GetWidth() != target.GetWidth() ||
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
405 source.GetHeight() != target.GetHeight())
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
406 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
407 throw OrthancException(ErrorCode_InternalError);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
408 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
409
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
410
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
411 /**
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
412 * If the format of the DICOM buffer is natively supported, use a
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
413 * direct access to copy its values.
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
414 **/
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
415
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
416 ImageAccessor targetAccessor(target.GetAccessor());
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
417 const DicomImageInformation& info = source.GetAccessor().GetInformation();
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
418
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
419 bool fastVersionSuccess = false;
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
420 PixelFormat sourceFormat;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
421 if (!info.IsPlanar() &&
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
422 info.ExtractPixelFormat(sourceFormat))
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
423 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
424 try
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
425 {
1190
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
426 size_t frameSize = info.GetHeight() * info.GetWidth() * GetBytesPerPixel(sourceFormat);
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
427 if ((frame + 1) * frameSize <= source.GetSize())
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
428 {
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
429 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(source.GetAccessor().GetPixelData());
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
430
1190
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
431 ImageAccessor sourceImage;
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
432 sourceImage.AssignReadOnly(sourceFormat,
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
433 info.GetWidth(),
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
434 info.GetHeight(),
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
435 info.GetWidth() * GetBytesPerPixel(sourceFormat),
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
436 buffer + frame * frameSize);
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
437
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
438 ImageProcessing::Convert(targetAccessor, sourceImage);
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
439 ImageProcessing::ShiftRight(targetAccessor, info.GetShift());
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
440 fastVersionSuccess = true;
851a55d183c9 fix issue 22
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1015
diff changeset
441 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
442 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
443 catch (OrthancException&)
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
444 {
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
445 // Unsupported conversion, use the slow version
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
446 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
447 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
448
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
449 /**
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
450 * Slow version : loop over the DICOM buffer, storing its value
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
451 * into the target image.
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
452 **/
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
453
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
454 if (!fastVersionSuccess)
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
455 {
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
456 switch (target.GetFormat())
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
457 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
458 case PixelFormat_RGB24:
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
459 case PixelFormat_RGBA32:
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
460 case PixelFormat_Grayscale8:
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
461 CopyPixels<uint8_t>(targetAccessor, source.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
462 break;
860
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 859
diff changeset
463
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
464 case PixelFormat_Grayscale16:
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
465 CopyPixels<uint16_t>(targetAccessor, source.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
466 break;
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
467
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
468 case PixelFormat_SignedGrayscale16:
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
469 CopyPixels<int16_t>(targetAccessor, source.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
470 break;
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
471
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
472 default:
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
473 throw OrthancException(ErrorCode_InternalError);
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
474 }
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
475 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
476 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
477
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
478
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target,
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
481 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
482 unsigned int frame)
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 if (!IsJpegLossless(dataset))
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 throw OrthancException(ErrorCode_BadParameterType);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 DcmElement *element = NULL;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 DcmPixelSequence* pixelSequence = NULL;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 if (!pixelData.getEncapsulatedRepresentation
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 (dataset.getOriginalXfer(), NULL, pixelSequence).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 SetupImageBuffer(target, dataset);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
505 ImageAccessor targetAccessor(target.GetAccessor());
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 /**
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 * are exactly the same, except for the "supportedTransferSyntax()"
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 * virtual function.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 **/
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 Uint32 startFragment = 0; // Default
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 OFString decompressedColorModel; // Out
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 DJ_RPLossless representationParameter;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, &parameters,
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
521 &dataset, frame, startFragment, targetAccessor.GetBuffer(),
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
522 targetAccessor.GetSize(), decompressedColorModel);
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 if (!c.good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 throw OrthancException(ErrorCode_InternalError);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 #endif
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
530
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
531
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
532
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
533
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
534 bool DicomImageDecoder::Decode(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
535 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
536 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
537 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
538 if (IsUncompressedImage(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
539 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
540 DecodeUncompressedImage(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
541 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
542 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
543
852
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
544
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
545 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
546 if (IsJpegLossless(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
547 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
548 LOG(INFO) << "Decoding a JPEG-LS image";
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
549 DecodeJpegLossless(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
550 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
551 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
552 #endif
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
553
852
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
554
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
555 #if ORTHANC_JPEG_ENABLED == 1
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
556 // TODO Implement this part to speed up JPEG decompression
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
557 #endif
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
558
5944b8b80842 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 851
diff changeset
559
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
560 /**
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
561 * This DICOM image format is not natively supported by
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
562 * Orthanc. As a last resort, try and decode it through
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
563 * DCMTK. This will result in higher memory consumption. This is
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
564 * actually the second example of the following page:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
565 * http://support.dcmtk.org/docs/mod_dcmjpeg.html#Examples
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
566 **/
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
567
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
568 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
569 LOG(INFO) << "Using DCMTK to decode a compressed image";
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
570
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
571 std::auto_ptr<DcmDataset> converted(dynamic_cast<DcmDataset*>(dataset.clone()));
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
572 converted->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
573
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
574 if (converted->canWriteXfer(EXS_LittleEndianExplicit))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
575 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
576 DecodeUncompressedImageInternal(target, *converted, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
577 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
578 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
579 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
580
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
581 return false;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
582 }
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
583
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
584
1015
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
585 static bool IsColorImage(PixelFormat format)
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
586 {
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
587 return (format == PixelFormat_RGB24 ||
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
588 format == PixelFormat_RGBA32);
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
589 }
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
590
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
591
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
592 bool DicomImageDecoder::DecodeAndTruncate(ImageBuffer& target,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
593 DcmDataset& dataset,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
594 unsigned int frame,
1015
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
595 PixelFormat format,
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
596 bool allowColorConversion)
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
597 {
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
598 // TODO Special case for uncompressed images
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
599
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
600 ImageBuffer source;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
601 if (!Decode(source, dataset, frame))
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
602 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
603 return false;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
604 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
605
1015
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
606 // If specified, prevent the conversion between color and
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
607 // grayscale images
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
608 bool isSourceColor = IsColorImage(source.GetFormat());
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
609 bool isTargetColor = IsColorImage(format);
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
610
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
611 if (!allowColorConversion)
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
612 {
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
613 if (isSourceColor ^ isTargetColor)
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
614 {
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
615 return false;
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
616 }
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
617 }
f009f7c75069 fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 942
diff changeset
618
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
619 if (source.GetFormat() == format)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
620 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
621 // No conversion is required, return the temporary image
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
622 target.AcquireOwnership(source);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
623 return true;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
624 }
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
625
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
626 target.SetFormat(format);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
627 target.SetWidth(source.GetWidth());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
628 target.SetHeight(source.GetHeight());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
629
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
630 ImageAccessor targetAccessor(target.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
631 ImageAccessor sourceAccessor(source.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
632 ImageProcessing::Convert(targetAccessor, sourceAccessor);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
633
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
634 return true;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
635 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
636
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
637
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
638 bool DicomImageDecoder::DecodePreview(ImageBuffer& target,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
639 DcmDataset& dataset,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
640 unsigned int frame)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
641 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
642 // TODO Special case for uncompressed images
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
643
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
644 ImageBuffer source;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
645 if (!Decode(source, dataset, frame))
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
646 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
647 return false;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
648 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
649
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
650 switch (source.GetFormat())
861
a546b05a43da fix PngWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 860
diff changeset
651 {
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
652 case PixelFormat_RGB24:
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
653 {
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
654 // Directly return color images (RGB)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
655 target.AcquireOwnership(source);
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
656 return true;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
657 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
658
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
659 case PixelFormat_Grayscale8:
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
660 case PixelFormat_Grayscale16:
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
661 case PixelFormat_SignedGrayscale16:
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
662 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
663 // Grayscale image: Stretch its dynamics to the [0,255] range
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
664 target.SetFormat(PixelFormat_Grayscale8);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
665 target.SetWidth(source.GetWidth());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
666 target.SetHeight(source.GetHeight());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
667
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
668 ImageAccessor targetAccessor(target.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
669 ImageAccessor sourceAccessor(source.GetAccessor());
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
670
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
671 int64_t a, b;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
672 ImageProcessing::GetMinMaxValue(a, b, sourceAccessor);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
673
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
674 if (a == b)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
675 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
676 ImageProcessing::Set(targetAccessor, 0);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
677 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
678 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
679 {
876
e21d1a5f5934 fix msvc warnings
jodogne
parents: 874
diff changeset
680 ImageProcessing::ShiftScale(sourceAccessor, static_cast<float>(-a), 255.0f / static_cast<float>(b - a));
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
681
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
682 if (source.GetFormat() == PixelFormat_Grayscale8)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
683 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
684 target.AcquireOwnership(source);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
685 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
686 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
687 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
688 ImageProcessing::Convert(targetAccessor, sourceAccessor);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
689 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
690 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
691
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
692 return true;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
693 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 861
diff changeset
694
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
695 default:
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
696 throw OrthancException(ErrorCode_NotImplemented);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
697 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 858
diff changeset
698 }
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
699 }