File: gdcmKAKADUCodec.cxx

package info (click to toggle)
gdcm 2.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,952 kB
  • ctags: 49,807
  • sloc: cpp: 193,527; ansic: 119,435; xml: 44,906; sh: 7,153; python: 3,670; cs: 2,202; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 119
file content (243 lines) | stat: -rw-r--r-- 6,314 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "gdcmKAKADUCodec.h"
#include "gdcmTransferSyntax.h"
#include "gdcmDataElement.h"
#include "gdcmFilename.h"
#include "gdcmSystem.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmPNMCodec.h"
#include "gdcmByteSwap.txx"

namespace gdcm
{
/*
*/

KAKADUCodec::KAKADUCodec()
{
  //NeedByteSwap = true;
}

KAKADUCodec::~KAKADUCodec()
{
}

bool KAKADUCodec::CanDecode(TransferSyntax const &ts) const
{
#ifndef GDCM_USE_KAKADU
  (void)ts;
  return false;
#else
  return ts == TransferSyntax::JPEG2000Lossless
      || ts == TransferSyntax::JPEG2000;
#endif
}

bool KAKADUCodec::CanCode(TransferSyntax const &) const
{
  return false;
}

/* KAKADU command line is a bit tricky to use:
 *
 * kdu_expand
 */
bool KAKADUCodec::Decode(DataElement const &in, DataElement &out)
{
#ifndef GDCM_USE_KAKADU
  (void)in;
  (void)out;
  return false;
#else
  // First thing creates a j2k file from the fragment:
  const SequenceOfFragments *sf = in.GetSequenceOfFragments();
  if(!sf) return false;

  if( NumberOfDimensions == 2 )
    {
    // http://msdn.microsoft.com/en-us/library/hs3e7355.aspx
    // -> check if tempnam needs the 'free'
    char *tempinput  = tempnam(0, "gdcminkduexp");
    char *tempoutput = tempnam(0, "gdcmoutkduexp");
    if( !tempinput || !tempoutput )
      {
      //free(input);
      //free(output);
      return false;
      }
    std::string input = tempinput;
    input += ".j2k";
    std::string output = tempoutput;
    output += ".rawl";

    std::ofstream outfile(input.c_str(), std::ios::binary);
    sf->WriteBuffer(outfile);
    outfile.close(); // flush !

    //Filename fn( System::GetCurrentProcessFileName() );
    //std::string executable_path = fn.GetPath();
#ifdef GDCM_USE_SYSTEM_KAKADU
    std::string kakadu_command = GDCM_KAKADU_EXPAND_EXECUTABLE;
    kakadu_command += " -quiet";
#else
#error not implemented
#endif
    // ./bin/kakadujpeg -d -s jpeg.jpg -ci 0 out.raw
    kakadu_command += " -i ";
    kakadu_command += input;
    kakadu_command += " -o ";
    kakadu_command += output;

    //std::cerr << kakadu_command << std::endl;
    gdcmDebugMacro( kakadu_command );
    int ret = system(kakadu_command.c_str());
    //std::cerr << "system: " << ret << std::endl;

    size_t len = System::FileSize(output.c_str());
    if(!len) return false;

    std::ifstream is(output.c_str(), std::ios::binary);
    char * buf = new char[len];
    is.read(buf, len);
    out.SetTag( Tag(0x7fe0,0x0010) );
    out.SetByteValue( buf, len );
    delete[] buf;

    if( !System::RemoveFile(input.c_str()) )
      {
      gdcmErrorMacro( "Could not delete input: " << input );
      }

    if( !System::RemoveFile(output.c_str()) )
      {
      gdcmErrorMacro( "Could not delete output: " << output );
      }

    free(tempinput);
    free(tempoutput);
    }
  else if ( NumberOfDimensions == 3 )
    {
    std::stringstream os;
    if( sf->GetNumberOfFragments() != Dimensions[2] )
      {
      gdcmErrorMacro( "Not handled" );
      return false;
      }

    for(unsigned int i = 0; i < sf->GetNumberOfFragments(); ++i)
      {
      // http://msdn.microsoft.com/en-us/library/hs3e7355.aspx
      // -> check if tempnam needs the 'free'
      char *tempinput  = tempnam(0, "gdcminkduexp");
      char *tempoutput = tempnam(0, "gdcmoutkduexp");
      if( !tempinput || !tempoutput )
        {
        //free(input);
        //free(output);
        return false;
        }
      std::string input = tempinput;
      input += ".j2k";
      std::string output = tempoutput;
      output += ".rawl";

      std::ofstream outfile(input.c_str(), std::ios::binary);
      const Fragment &frag = sf->GetFragment(i);
      assert( !frag.IsEmpty() );
      const ByteValue *bv = frag.GetByteValue();
      assert( bv );
      //sf->WriteBuffer(outfile);
      bv->WriteBuffer( outfile );
      outfile.close(); // flush !

      //Filename fn( System::GetCurrentProcessFileName() );
      //std::string executable_path = fn.GetPath();
#ifdef GDCM_USE_SYSTEM_KAKADU
      std::string kakadu_command = GDCM_KAKADU_EXPAND_EXECUTABLE;
      kakadu_command += " -quiet";
#else
#error not implemented
#endif
      // ./bin/kakadujpeg -d -s jpeg.jpg -ci 0 out.raw
      kakadu_command += " -i ";
      kakadu_command += input;
      kakadu_command += " -o ";
      kakadu_command += output;

      //std::cerr << kakadu_command << std::endl;
      gdcmDebugMacro( kakadu_command );
      int ret = system(kakadu_command.c_str());
      //std::cerr << "system: " << ret << std::endl;

      size_t len = System::FileSize(output.c_str());
      if(!len) return false;

      std::ifstream is(output.c_str(), std::ios::binary);
      char * buf = new char[len];
      is.read(buf, len);
      os.write(buf, len);
      //out.SetByteValue( buf, len );
      delete[] buf;

      if( !System::RemoveFile(input.c_str()) )
        {
        gdcmErrorMacro( "Could not delete input: " << input );
        }
      if( !System::RemoveFile(output.c_str()) )
        {
        gdcmErrorMacro( "Could not delete output: " << output );
        }
      free(tempinput);
      free(tempoutput);
      }
    std::string str = os.str();
    assert( str.size() );
    out.SetTag( Tag(0x7fe0,0x0010) );
    out.SetByteValue( &str[0], str.size() );
    }
  else
    {
    gdcmErrorMacro( "Not handled" );
    return false;
    }

  // FIXME:
  //LossyFlag = true;

  //return ImageCodec::Decode(in,out);
  return true;
#endif
}

// Compress into JPEG
bool KAKADUCodec::Code(DataElement const &in, DataElement &out)
{
#ifndef GDCM_USE_KAKADU
  (void)in;
  (void)out;
  return false;
#else
  // That would be neat, please contribute :)
  return false;
#endif
}

ImageCodec * KAKADUCodec::Clone() const
{
  return NULL;
}

} // end namespace gdcm