annotate OrthancServer/Internals/DicomImageDecoder.cpp @ 848:703e8d5b03fd jpeg

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Jun 2014 18:16:40 +0200
parents 03ea55da7429
children 9ee2e7a5efaf
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
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
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
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
45 Copyright (c) 2006-2011 Mathieu Malaterre
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
46 Copyright (c) 1993-2005 CREATIS
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
47 (CREATIS = Centre de Recherche et d'Applications en Traitement de l'Image)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
48 All rights reserved.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
49
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
50 Redistribution and use in source and binary forms, with or without
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
51 modification, are permitted provided that the following conditions are met:
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
52
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
53 * Redistributions of source code must retain the above copyright notice,
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
54 this list of conditions and the following disclaimer.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
55
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
56 * Redistributions in binary form must reproduce the above copyright notice,
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
57 this list of conditions and the following disclaimer in the documentation
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
58 and/or other materials provided with the distribution.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
59
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
60 * Neither name of Mathieu Malaterre, or CREATIS, nor the names of any
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
61 contributors (CNRS, INSERM, UCB, Universite Lyon I), may be used to
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
62 endorse or promote products derived from this software without specific
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
63 prior written permission.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
64
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
65 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
66 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
67 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
68 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
69 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
70 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
71 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
72 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
73 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
74 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
75
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
76 =========================================================================*/
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
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 #include "../../Core/OrthancException.h"
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
81 #include "../../Core/DicomFormat/DicomIntegerPixelAccessor.h"
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 #include "../ToDcmtkBridge.h"
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
83 #include "../FromDcmtkBridge.h"
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
85 #include <glog/logging.h>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
86
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
87 #include <boost/lexical_cast.hpp>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
88
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
89 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 #include <dcmtk/dcmjpls/djcodecd.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 #include <dcmtk/dcmjpls/djcparam.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 #include <dcmtk/dcmjpeg/djrplol.h>
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 #endif
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 namespace Orthanc
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
98 static const DicomTag DICOM_TAG_CONTENT(0x07a1, 0x100a);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
99 static const DicomTag DICOM_TAG_COMPRESSION_TYPE(0x07a1, 0x1011);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
100
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
101 bool DicomImageDecoder::IsPsmctRle1(DcmDataset& dataset)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
102 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
103 DcmElement* e;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
104 char* c;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
105
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
106 // Check whether the DICOM instance contains an image encoded with
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
107 // the PMSCT_RLE1 scheme.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
108 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_COMPRESSION_TYPE), e).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
109 e == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
110 !e->isaString() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
111 !e->getString(c).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
112 c == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
113 strcmp("PMSCT_RLE1", c))
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
114 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
115 return false;
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 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
118 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
119 return true;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
120 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
121 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
122
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
123
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
124 bool DicomImageDecoder::DecodePsmctRle1(std::string& output,
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
125 DcmDataset& dataset)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
126 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
127 // Check whether the DICOM instance contains an image encoded with
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
128 // the PMSCT_RLE1 scheme.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
129 if (!IsPsmctRle1(dataset))
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 return false;
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
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
134 // OK, this is a custom RLE encoding from Philips. Get the pixel
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
135 // data from the appropriate private DICOM tag.
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
136 Uint8* pixData = NULL;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
137 DcmElement* e;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
138 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_CONTENT), e).good() ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
139 e == NULL ||
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
140 e->getUint8Array(pixData) != EC_Normal)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
141 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
142 return false;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
143 }
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 // The "unsigned" below IS VERY IMPORTANT
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
146 const uint8_t* inbuffer = reinterpret_cast<const uint8_t*>(pixData);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
147 const size_t length = e->getLength();
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
148
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
149 /**
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
150 * 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
151 * Mathieu Malaterre (under a BSD license).
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
152 * http://gdcm.sourceforge.net/html/rle2img_8cxx-example.html
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
153 **/
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
154
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
155 // RLE pass
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
156 std::vector<uint8_t> temp;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
157 temp.reserve(length);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
158 for (size_t i = 0; i < length; i++)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
159 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
160 if (inbuffer[i] == 0xa5)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
161 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
162 temp.push_back(inbuffer[i+2]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
163 for (uint8_t repeat = inbuffer[i + 1]; repeat != 0; repeat--)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
164 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
165 temp.push_back(inbuffer[i+2]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
166 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
167 i += 2;
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 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
170 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
171 temp.push_back(inbuffer[i]);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
172 }
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
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
175 // Delta encoding pass
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
176 uint16_t delta = 0;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
177 output.clear();
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
178 output.reserve(temp.size());
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
179 for (size_t i = 0; i < temp.size(); i++)
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 uint16_t value;
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 if (temp[i] == 0x5a)
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 uint16_t v1 = temp[i + 1];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
186 uint16_t v2 = temp[i + 2];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
187 value = (v2 << 8) + v1;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
188 i += 2;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
189 }
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
190 else
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
191 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
192 value = delta + (int8_t) temp[i];
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
193 }
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 output.push_back(value & 0xff);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
196 output.push_back(value >> 8);
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
197 delta = value;
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
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
200 if (output.size() % 2)
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
201 {
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
202 output.resize(output.size() - 1);
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
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
205 return true;
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
206 }
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
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target,
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 DcmDataset& dataset)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 OFString value;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_COLUMNS), value).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 unsigned int width = boost::lexical_cast<unsigned int>(value.c_str());
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_ROWS), value).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 unsigned int height = boost::lexical_cast<unsigned int>(value.c_str());
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_BITS_STORED), value).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 unsigned int bitsStored = boost::lexical_cast<unsigned int>(value.c_str());
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_REPRESENTATION), value).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 bool isSigned = (boost::lexical_cast<unsigned int>(value.c_str()) != 0);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 unsigned int samplesPerPixel = 1; // By default
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 if (dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_SAMPLES_PER_PIXEL), value).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 samplesPerPixel = boost::lexical_cast<unsigned int>(value.c_str());
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 target.SetHeight(height);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 target.SetWidth(width);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 if (bitsStored == 8 && samplesPerPixel == 1 && !isSigned)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 target.SetFormat(PixelFormat_Grayscale8);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 else if (bitsStored == 8 && samplesPerPixel == 3 && !isSigned)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 target.SetFormat(PixelFormat_RGB24);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 else if (bitsStored == 16 && samplesPerPixel == 1 && !isSigned)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 target.SetFormat(PixelFormat_Grayscale16);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 else if (bitsStored == 16 && samplesPerPixel == 1 && isSigned)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 target.SetFormat(PixelFormat_SignedGrayscale16);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 else
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 throw OrthancException(ErrorCode_NotImplemented);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 bool DicomImageDecoder::IsJpegLossless(const DcmDataset& dataset)
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless ||
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 dataset.getOriginalXfer() == EXS_JPEGLSLossy);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
281 bool DicomImageDecoder::IsUncompressedImage(const DcmDataset& dataset)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
282 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
283 return (dataset.getOriginalXfer() == EXS_Unknown ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
284 dataset.getOriginalXfer() == EXS_LittleEndianImplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
285 dataset.getOriginalXfer() == EXS_BigEndianImplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
286 dataset.getOriginalXfer() == EXS_LittleEndianExplicit ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
287 dataset.getOriginalXfer() == EXS_BigEndianExplicit);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
288 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
289
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
290
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
291 template <typename PixelType>
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
292 static void CopyPixels(ImageAccessor& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
293 const DicomIntegerPixelAccessor& source)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
294 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
295 const PixelType minValue = std::numeric_limits<PixelType>::min();
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
296 const PixelType maxValue = std::numeric_limits<PixelType>::max();
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
297
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
298 for (unsigned int y = 0; y < source.GetHeight(); y++)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
299 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
300 PixelType* pixel = reinterpret_cast<PixelType*>(target.GetRow(y));
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
301 for (unsigned int x = 0; x < source.GetWidth(); x++)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
302 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
303 for (unsigned int c = 0; c < source.GetChannelCount(); c++, pixel++)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
304 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
305 int32_t v = source.GetValue(x, y, c);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
306 if (v < static_cast<int32_t>(minValue))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
307 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
308 *pixel = minValue;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
309 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
310 else if (v > static_cast<int32_t>(maxValue))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
311 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
312 *pixel = maxValue;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
313 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
314 else
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
315 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
316 *pixel = static_cast<PixelType>(v);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
317 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
318 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
319 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
320 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
321 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
322
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
323
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
324 void DicomImageDecoder::DecodeUncompressedImage(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
325 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
326 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
327 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
328 if (!IsUncompressedImage(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
329 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
330 throw OrthancException(ErrorCode_BadParameterType);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
331 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
332
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
333 DecodeUncompressedImageInternal(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
334 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
335
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
336
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
337 void DicomImageDecoder::DecodeUncompressedImageInternal(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
338 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
339 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
340 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
341 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data
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 std::auto_ptr<DicomIntegerPixelAccessor> source;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
344
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
345 DicomMap m;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
346 FromDcmtkBridge::Convert(m, dataset);
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
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
349 /**
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
350 * Create an accessor to the raw values of the DICOM image.
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
351 **/
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 std::string privateContent;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
354
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
355 DcmElement* e;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
356 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() &&
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
357 e != NULL)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
358 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
359 Uint8* pixData = NULL;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
360 if (e->getUint8Array(pixData) == EC_Normal)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
361 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
362 source.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength()));
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
363 }
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 else if (DicomImageDecoder::DecodePsmctRle1(privateContent, dataset))
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 LOG(INFO) << "The PMSCT_RLE1 decoding has succeeded";
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
368 Uint8* pixData = NULL;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
369 if (privateContent.size() > 0)
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 pixData = reinterpret_cast<Uint8*>(&privateContent[0]);
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 source.reset(new DicomIntegerPixelAccessor(m, pixData, privateContent.size()));
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 if (source.get() == NULL)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
378 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
379 throw OrthancException(ErrorCode_BadFileFormat);
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
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
382 source->SetCurrentFrame(frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
383
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 * Resize the target image, with some sanity checks.
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 SetupImageBuffer(target, dataset);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
390
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
391 if (target.GetWidth() != target.GetWidth() ||
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
392 target.GetHeight() != target.GetHeight())
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
393 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
394 throw OrthancException(ErrorCode_InternalError);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
395 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
396
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
397 bool ok;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
398 switch (target.GetFormat())
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
399 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
400 case PixelFormat_RGB24:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
401 ok = source->GetChannelCount() == 3;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
402 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
403
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
404 case PixelFormat_RGBA32:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
405 ok = source->GetChannelCount() == 4;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
406 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
407
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
408 case PixelFormat_Grayscale8:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
409 case PixelFormat_Grayscale16:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
410 case PixelFormat_SignedGrayscale16:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
411 ok = source->GetChannelCount() == 1;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
412 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
413
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
414 default:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
415 ok = false; // (*)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
416 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
417 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
418
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
419 if (!ok)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
420 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
421 throw OrthancException(ErrorCode_InternalError);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
422 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
423
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
424
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
425 /**
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
426 * Loop over the DICOM buffer, storing its value into the target
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
427 * image.
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
428 **/
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
429
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
430 ImageAccessor accessor(target.GetAccessor());
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
431
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
432 switch (target.GetFormat())
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
433 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
434 case PixelFormat_RGB24:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
435 case PixelFormat_RGBA32:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
436 case PixelFormat_Grayscale8:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
437 CopyPixels<uint8_t>(accessor, *source);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
438 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
439
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
440 case PixelFormat_Grayscale16:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
441 CopyPixels<uint16_t>(accessor, *source);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
442 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
443
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
444 case PixelFormat_SignedGrayscale16:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
445 CopyPixels<int16_t>(accessor, *source);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
446 break;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
447
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
448 default:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
449 throw OrthancException(ErrorCode_InternalError);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
450 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
451 }
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
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target,
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
456 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
457 unsigned int frame)
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 if (!IsJpegLossless(dataset))
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 throw OrthancException(ErrorCode_BadParameterType);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 DcmElement *element = NULL;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 DcmPixelSequence* pixelSequence = NULL;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 if (!pixelData.getEncapsulatedRepresentation
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 (dataset.getOriginalXfer(), NULL, pixelSequence).good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 throw OrthancException(ErrorCode_BadFileFormat);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 SetupImageBuffer(target, dataset);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 ImageAccessor accessor(target.GetAccessor());
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 /**
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 * are exactly the same, except for the "supportedTransferSyntax()"
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 * virtual function.
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html
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 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 Uint32 startFragment = 0; // Default
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 OFString decompressedColorModel; // Out
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 DJ_RPLossless representationParameter;
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, &parameters,
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
496 &dataset, frame, startFragment, accessor.GetBuffer(),
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 accessor.GetSize(), decompressedColorModel);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 if (!c.good())
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 {
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 throw OrthancException(ErrorCode_InternalError);
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 }
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 #endif
846
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
505
715ab7674993 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 845
diff changeset
506
847
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
507
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
508 bool DicomImageDecoder::Decode(ImageBuffer& target,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
509 DcmDataset& dataset,
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
510 unsigned int frame)
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
511 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
512 if (IsUncompressedImage(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
513 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
514 DecodeUncompressedImage(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
515 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
516 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
517
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
518 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
519 if (IsJpegLossless(dataset))
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
520 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
521 LOG(INFO) << "Decoding a JPEG-LS image";
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
522 DecodeJpegLossless(target, dataset, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
523 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
524 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
525 #endif
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
526
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
527 /**
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
528 * 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
529 * 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
530 * 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
531 * actually the second example of the following page:
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
532 * 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
533 **/
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
534
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
535 {
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
536 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
537
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
538 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
539 converted->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
540
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
541 if (converted->canWriteXfer(EXS_LittleEndianExplicit))
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 DecodeUncompressedImageInternal(target, *converted, frame);
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
544 return true;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
545 }
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
546 }
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 return false;
03ea55da7429 fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 846
diff changeset
549 }
845
48016722c770 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 }