]> Creatis software - gdcm.git/blob - vtk/vtkGdcmReader.cxx
* vtk/vtkGdcmReader.[h|cxx] : fix the read of 3 gdcmHeader when making an
[gdcm.git] / vtk / vtkGdcmReader.cxx
1 // vtkGdcmReader.cxx
2 //-----------------------------------------------------------------------------
3 // //////////////////////////////////////////////////////////////
4 // WARNING TODO CLENAME 
5 // Actual limitations of this code:
6 //
7 // /////// Redundant and unnecessary header parsing
8 // In it's current state this code actually parses three times the Dicom
9 // header of a file before the corresponding image gets loaded in the
10 // ad-hoc vtkData !
11 // Here is the process:
12 //  1/ First loading happens in ExecuteInformation which in order to
13 //     positionate the vtk extents calls CheckFileCoherence. The purpose
14 //     of CheckFileCoherence is to make sure all the images in the future
15 //     stack are "homogenous" (same size, same representation...). This
16 //     can only be achieved by parsing all the Dicom headers...
17 //  2/ ExecuteData is then responsible for the next two loadings:
18 //  2a/ ExecuteData calls AllocateOutputData that in turn seems to 
19 //      (indirectely call) ExecuteInformation which ends up in a second
20 //      header parsing
21 //      This is fixed by adding a test at the beginning of ExecuteInformation
22 //      on the modification of the object instance. If a modification have been
23 //      made (method Modified() ), the MTime value is increased. The fileTime
24 //      is compared to this new value to find a modification in the class
25 //      parameters
26 //  2b/ the core of ExecuteData then needs gdcmFile (which in turns
27 //      initialises gdcmHeader in the constructor) in order to access
28 //      the data-image.
29 //
30 // Possible solution:
31 // maintain a list of gdcmFiles (created by say ExecuteInformation) created
32 // once and for all accross the life of vtkGdcmHeader (it would only load
33 // new gdcmFile if the user changes the list). ExecuteData would then use 
34 // those gdcmFile and hence avoid calling the construtor:
35 //  - advantage: the header of the files would only be parser once.
36 //  - drawback: once execute information is called (i.e. on creation of
37 //              a vtkGdcmHeader) the gdcmFile structure is loaded in memory.
38 //              The average size of a gdcmHeader being of 100Ko, is one
39 //              loads 10 stacks of images with say 200 images each, you
40 //              end-up with a loss of 200Mo...
41 //
42 // /////// Never unallocated memory:
43 // ExecuteData allocates space for the pixel data [which will get pointed
44 // by the vtkPointData() through the call
45 // data->GetPointData()->GetScalars()->SetVoidArray(mem, StackNumPixels, 0);]
46 // This data is never "freed" neither in the destructor nor when the
47 // filename list is extended, ExecuteData is called a second (or third)
48 // time...
49 // //////////////////////////////////////////////////////////////
50
51 #include "gdcmFile.h"
52 #include "gdcmHeaderHelper.h"
53 #include "vtkGdcmReader.h"
54
55 //#include <stdio.h>
56 #include <vtkObjectFactory.h>
57 #include <vtkImageData.h>
58 #include <vtkPointData.h>
59 #include <vtkLookupTable.h>
60
61 //-----------------------------------------------------------------------------
62 // Constructor / Destructor
63 vtkGdcmReader::vtkGdcmReader()
64 {
65    this->LookupTable = NULL;
66 }
67
68 vtkGdcmReader::~vtkGdcmReader()
69 {
70    this->RemoveAllFileName();
71    this->InternalFileNameList.clear();
72    if(this->LookupTable) 
73       this->LookupTable->Delete();
74 }
75
76 //-----------------------------------------------------------------------------
77 // Print
78 void vtkGdcmReader::PrintSelf(ostream& os, vtkIndent indent)
79 {
80    vtkImageReader::PrintSelf(os,indent);
81    os << indent << "Filenames  : " << endl;
82    vtkIndent nextIndent = indent.GetNextIndent();
83    for (std::list<std::string>::iterator FileName  = FileNameList.begin();
84         FileName != FileNameList.end();
85         ++FileName)
86    {
87       os << nextIndent << FileName->c_str() << endl ;
88    }
89 }
90
91 //-----------------------------------------------------------------------------
92 // Public
93 /*
94  * Remove all files from the list of images to read.
95  */
96 void vtkGdcmReader::RemoveAllFileName(void)
97 {
98    this->FileNameList.clear();
99    this->Modified();
100 }
101
102 /*
103  * Adds a file name to the list of images to read.
104  */
105 void vtkGdcmReader::AddFileName(const char* name)
106 {
107    // We need to bypass the const pointer [since list<>.push_bash() only
108    // takes a char* (but not a const char*)] by making a local copy:
109    char * LocalName = new char[strlen(name) + 1];
110    strcpy(LocalName, name);
111    this->FileNameList.push_back(LocalName);
112    delete[] LocalName;
113    this->Modified();
114 }
115
116 /*
117  * Sets up a filename to be read.
118  */
119 void vtkGdcmReader::SetFileName(const char *name) 
120 {
121    vtkImageReader2::SetFileName(name);
122    // Since we maintain a list of filenames, when building a volume,
123    // (see vtkGdcmReader::AddFileName), we additionaly need to purge
124    // this list when we manually positionate the filename.
125    this->FileNameList.clear();
126    this->Modified();
127 }
128
129 //-----------------------------------------------------------------------------
130 // Protected
131 /*
132  * Configure the output e.g. WholeExtent, spacing, origin, scalar type...
133  */
134 void vtkGdcmReader::ExecuteInformation()
135 {
136    if(this->MTime>this->fileTime)
137    {
138       this->TotalNumberOfPlanes = this->CheckFileCoherence();
139       if ( this->TotalNumberOfPlanes == 0)
140       {
141          vtkErrorMacro("File set is not coherent. Exiting...");
142          return;
143       }
144       
145       // if the user has not set the extent, but has set the VOI
146       // set the z axis extent to the VOI z axis
147       if (this->DataExtent[4]==0 && this->DataExtent[5] == 0 &&
148       (this->DataVOI[4] || this->DataVOI[5]))
149       {
150          this->DataExtent[4] = this->DataVOI[4];
151          this->DataExtent[5] = this->DataVOI[5];
152       }
153
154       // When the user has set the VOI, check it's coherence with the file content.
155       if (this->DataVOI[0] || this->DataVOI[1] || 
156       this->DataVOI[2] || this->DataVOI[3] ||
157       this->DataVOI[4] || this->DataVOI[5])
158       { 
159          if ((this->DataVOI[0] < 0) ||
160              (this->DataVOI[1] >= this->NumColumns) ||
161              (this->DataVOI[2] < 0) ||
162              (this->DataVOI[3] >= this->NumLines) ||
163              (this->DataVOI[4] < 0) ||
164              (this->DataVOI[5] >= this->TotalNumberOfPlanes ))
165          {
166             vtkWarningMacro("The requested VOI is larger than expected extent.");
167             this->DataVOI[0] = 0;
168             this->DataVOI[1] = this->NumColumns - 1;
169             this->DataVOI[2] = 0;
170             this->DataVOI[3] = this->NumLines - 1;
171             this->DataVOI[4] = 0;
172             this->DataVOI[5] = this->TotalNumberOfPlanes - 1;
173          }
174       }
175
176       // Positionate the Extent.
177       this->DataExtent[0] = 0;
178       this->DataExtent[1] = this->NumColumns - 1;
179       this->DataExtent[2] = 0;
180       this->DataExtent[3] = this->NumLines - 1;
181       this->DataExtent[4] = 0;
182       this->DataExtent[5] = this->TotalNumberOfPlanes - 1;
183   
184       // We don't need to positionate the Endian related stuff (by using
185       // this->SetDataByteOrderToBigEndian() or SetDataByteOrderToLittleEndian()
186       // since the reading of the file is done by gdcm.
187       // But we do need to set up the data type for downstream filters:
188       if      ( ImageType == "8U" )
189       {
190          vtkDebugMacro("8 bits unsigned image");
191          this->SetDataScalarTypeToUnsignedChar(); 
192       }
193       else if ( ImageType == "8S" )
194       {
195          vtkErrorMacro("Cannot handle 8 bit signed files");
196          return;
197       }
198       else if ( ImageType == "16U" )
199       {
200          vtkDebugMacro("16 bits unsigned image");
201          this->SetDataScalarTypeToUnsignedShort();
202       }
203       else if ( ImageType == "16S" )
204       {
205          vtkDebugMacro("16 bits signed image");
206          this->SetDataScalarTypeToShort();
207          //vtkErrorMacro("Cannot handle 16 bit signed files");
208       }
209       else if ( ImageType == "32U" )
210       {
211          vtkDebugMacro("32 bits unsigned image");
212          vtkDebugMacro("WARNING: forced to signed int !");
213          this->SetDataScalarTypeToInt();
214       }
215       else if ( ImageType == "32S" )
216       {
217          vtkDebugMacro("32 bits signed image");
218          this->SetDataScalarTypeToInt();
219       }
220       else if ( ImageType == "FD" )
221       {
222          vtkDebugMacro("64 bits Double image");
223          this->SetDataScalarTypeToDouble();
224       }
225       //Set number of scalar components:
226       this->SetNumberOfScalarComponents(this->NumComponents);
227
228       this->fileTime=this->MTime;
229    }
230
231    this->Superclass::ExecuteInformation();
232 }
233
234 /*
235  * Update => ouput->Update => UpdateData => Execute => ExecuteData 
236  * (see vtkSource.cxx for last step).
237  * This function (redefinition of vtkImageReader::ExecuteData, see 
238  * VTK/IO/vtkImageReader.cxx) reads a data from a file. The datas
239  * extent/axes are assumed to be the same as the file extent/order.
240  */
241 void vtkGdcmReader::ExecuteData(vtkDataObject *output)
242 {
243    if (this->InternalFileNameList.empty())
244    {
245       vtkErrorMacro("A least a valid FileName must be specified.");
246       return;
247    }
248
249    // FIXME : extraneous parsing of header is made when allocating OuputData
250    vtkImageData *data = this->AllocateOutputData(output);
251    data->SetExtent(this->DataExtent);
252    data->GetPointData()->GetScalars()->SetName("DicomImage-Volume");
253
254    // Test if output has valid extent
255    // Prevent memory errors
256    if((this->DataExtent[1]-this->DataExtent[0]>=0) &&
257       (this->DataExtent[3]-this->DataExtent[2]>=0) &&
258       (this->DataExtent[5]-this->DataExtent[4]>=0))
259    {
260       // The memory size for a full stack of images of course depends
261       // on the number of planes and the size of each image:
262       size_t StackNumPixels = this->NumColumns * this->NumLines
263                             * this->TotalNumberOfPlanes * this->NumComponents;
264       size_t stack_size = StackNumPixels * this->PixelSize;
265       // Allocate pixel data space itself.
266
267       // Variables for the UpdateProgress. We shall use 50 steps to signify
268       // the advance of the process:
269       unsigned long UpdateProgressTarget = (unsigned long) ceil (this->NumLines
270                                          * this->TotalNumberOfPlanes
271                                          / 50.0);
272       // The actual advance measure:
273       unsigned long UpdateProgressCount = 0;
274
275       // Feeling the allocated memory space with each image/volume:
276       unsigned char *Dest = (unsigned char *)data->GetPointData()->GetScalars()->GetVoidPointer(0);
277       for (std::list<std::string>::iterator FileName  = InternalFileNameList.begin();
278            FileName != InternalFileNameList.end();
279            ++FileName)
280       { 
281          // Images that were tagged as unreadable in CheckFileCoherence()
282          // are substituted with a black image to let the caller visually
283          // notice something wrong is going on:
284          if (*FileName != "GDCM_UNREADABLE")
285          {
286             // Update progress related for good files is made in LoadImageInMemory
287             Dest += this->LoadImageInMemory(*FileName, Dest,
288                                             UpdateProgressTarget,
289                                             UpdateProgressCount);
290          } 
291          else 
292          {
293             // We insert a black image in the stack for the user to be aware that
294             // this image/volume couldn't be loaded. We simply skip one image
295             // size:
296             Dest += this->NumColumns * this->NumLines * this->PixelSize;
297
298             // Update progress related for bad files:
299             UpdateProgressCount += this->NumLines;
300             if (UpdateProgressTarget > 0)
301             {
302                if (!(UpdateProgressCount%UpdateProgressTarget))
303                {
304                   this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
305                }
306             }
307          } // Else, file not loadable
308       } // Loop on files
309    }
310 }
311
312 /*
313  * vtkGdcmReader can have the file names specified through two ways:
314  * (1) by calling the vtkImageReader2::SetFileName(), SetFilePrefix() and
315  *     SetFilePattern()
316  * (2) By successive calls to vtkGdcmReader::AddFileName()
317  * When the first method was used by caller we need to update the local
318  * filename list
319  */
320 void vtkGdcmReader::BuildFileListFromPattern()
321 {
322    if ((! this->FileNameList.empty()) && this->FileName )
323    {
324       vtkErrorMacro("Both file patterns and AddFileName schemes were used");
325       vtkErrorMacro("Only the files specified with AddFileName shall be used");
326       return;
327    }
328
329    if (! this->FileNameList.empty()  )
330    {
331       vtkDebugMacro("Using the AddFileName specified files");
332       this->InternalFileNameList=this->FileNameList;
333       return;
334    }
335
336    if (!this->FileName && !this->FilePattern)
337    {
338       vtkErrorMacro("FileNames are not set. Either use AddFileName() or");
339       vtkErrorMacro("specify a FileName or FilePattern.");
340       return;
341    }
342
343    this->RemoveAllInternalFileName();
344    if( this->FileNameList.empty() )
345    {
346       //Multiframe case:
347       this->ComputeInternalFileName(this->DataExtent[4]);
348       vtkDebugMacro("Adding file " << this->InternalFileName);
349       this->AddInternalFileName(this->InternalFileName);
350    }
351    else
352    {
353       //stack of 2D dicom case:
354       for (int idx = this->DataExtent[4]; idx <= this->DataExtent[5]; ++idx)
355       {
356          this->ComputeInternalFileName(idx);
357          vtkDebugMacro("Adding file " << this->InternalFileName);
358          this->AddInternalFileName(this->InternalFileName);
359       }
360    }
361 }
362
363 /*
364  * When more than one filename is specified (i.e. we expect loading
365  * a stack or volume) we need to check that the corresponding images/volumes
366  * to be loaded are coherent i.e. to make sure:
367  *     - they all share the same X dimensions
368  *     - they all share the same Y dimensions
369  *     - they all share the same ImageType ( 8 bit signed, or unsigned...)
370  *
371  * Eventually, we emit a warning when all the files do NOT share the
372  * Z dimension, since we can still build a stack but the
373  * files are not coherent in Z, which is probably a source a trouble...
374  *   When files are not readable (either the file cannot be opened or
375  * because gdcm cannot parse it), they are flagged as "GDCM_UNREADABLE".  
376  *   This method returns the total number of planar images to be loaded
377  * (i.e. an image represents one plane, but a volume represents many planes)
378  */
379 int vtkGdcmReader::CheckFileCoherence()
380 {
381    int ReturnedTotalNumberOfPlanes = 0;   // The returned value.
382
383    this->BuildFileListFromPattern();
384    if (this->InternalFileNameList.empty())
385    {
386       vtkErrorMacro("FileNames are not set.");
387       return 0;
388    }
389
390    bool FoundReferenceFile = false;
391    int  ReferenceNZ = 0;
392
393    // Loop on the filenames:
394    // - check for their existence and gdcm "parsability"
395    // - get the coherence check done:
396    for (std::list<std::string>::iterator FileName = InternalFileNameList.begin();
397         FileName != InternalFileNameList.end();
398         ++FileName)
399    {
400       // The file is always added in the number of planes
401       //  - If file doesn't exist, it will be replaced by a black plane in the 
402       //    ExecuteData method
403       //  - If file has more than 1 plane, other planes will be added later to
404       //    to the ReturnedTotalNumberOfPlanes variable counter
405       ReturnedTotalNumberOfPlanes += 1;
406
407       /////// Stage 0: check for file name:
408       if(*FileName==std::string("GDCM_UNREADABLE"))
409          continue;
410
411       /////// Stage 1: check for file readability:
412       // Stage 1.1: check for file existence.
413       FILE *fp;
414       fp = fopen(FileName->c_str(),"rb");
415       if (!fp)
416       {
417          vtkErrorMacro("Unable to open file " << FileName->c_str());
418          vtkErrorMacro("Removing this file from readed files "
419                      << FileName->c_str());
420          *FileName = "GDCM_UNREADABLE";
421          continue;
422       }
423       fclose(fp);
424    
425       // Stage 1.2: check for Gdcm parsability
426       gdcmHeaderHelper GdcmHeader(FileName->c_str());
427       if (!GdcmHeader.IsReadable())
428       {
429          vtkErrorMacro("Gdcm cannot parse file " << FileName->c_str());
430          vtkErrorMacro("Removing this file from readed files "
431                         << FileName->c_str());
432          *FileName = "GDCM_UNREADABLE";
433          continue;
434       }
435
436       // Stage 1.3: further gdcm compatibility on PixelType
437       std::string type = GdcmHeader.GetPixelType();
438       if (   (type !=  "8U") && (type !=  "8S")
439       && (type != "16U") && (type != "16S")
440       && (type != "32U") && (type != "32S") )
441       {
442          vtkErrorMacro("Bad File Type for file" << FileName->c_str());
443          vtkErrorMacro("                      " << type.c_str());
444          vtkErrorMacro("Removing this file from readed files "
445                         << FileName->c_str());
446          *FileName = "GDCM_UNREADABLE";
447          continue;
448       }
449
450       // Stage 2: check coherence of the set of files
451       int NX = GdcmHeader.GetXSize();
452       int NY = GdcmHeader.GetYSize();
453       int NZ = GdcmHeader.GetZSize();
454       if (FoundReferenceFile) 
455       {
456          // Stage 2.1: mandatory coherence stage:
457          if (   ( NX   != this->NumColumns )
458          || ( NY   != this->NumLines )
459          || ( type != this->ImageType ) ) 
460          {
461             vtkErrorMacro("This file is not coherent with previous ones"
462                            << FileName->c_str());
463             vtkErrorMacro("Removing this file from readed files "
464                            << FileName->c_str());
465             *FileName = "GDCM_UNREADABLE";
466             continue;
467          }
468
469          // Stage 2.2: optional coherence stage
470          if ( NZ != ReferenceNZ )
471          {
472             vtkErrorMacro("File is not coherent in Z with previous ones"
473                            << FileName->c_str());
474          }
475          else
476          {
477             vtkDebugMacro("File is coherent with previous ones"
478                            << FileName->c_str());
479          }
480
481          // Stage 2.3: when the file contains a volume (as opposed to an image),
482          // notify the caller.
483          if (NZ > 1)
484          {
485             vtkErrorMacro("This file contains multiple planes (images)"
486                            << FileName->c_str());
487          }
488
489          // Eventually, this file can be added on the stack. Update the
490          // full size of the stack
491          vtkDebugMacro("Number of planes added to the stack: " << NZ);
492          ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
493          continue;
494
495       } 
496       else 
497       {
498          // We didn't have a workable reference file yet. Set this one
499          // as the reference.
500          FoundReferenceFile = true;
501          vtkDebugMacro("This file taken as coherence reference:"
502                         << FileName->c_str());
503          vtkDebugMacro("Image dimension of reference file as read from Gdcm:" 
504                         << NX << " " << NY << " " << NZ);
505          vtkDebugMacro("Number of planes added to the stack: " << NZ);
506          // Set aside the size of the image
507          this->NumColumns = NX;
508          this->NumLines   = NY;
509          ReferenceNZ      = NZ;
510          ReturnedTotalNumberOfPlanes += NZ - 1; // First plane already added
511          this->ImageType = type;
512          this->PixelSize = GdcmHeader.GetPixelSize();
513
514          if( GdcmHeader.HasLUT() )
515          {
516             this->NumComponents = GdcmHeader.GetNumberOfScalarComponentsRaw();
517          }
518          else
519          {
520             this->NumComponents = GdcmHeader.GetNumberOfScalarComponents(); //rgb or mono
521          }
522        
523          //Set image spacing
524          this->DataSpacing[0] = GdcmHeader.GetXSpacing();
525          this->DataSpacing[1] = GdcmHeader.GetYSpacing();
526          this->DataSpacing[2] = GdcmHeader.GetZSpacing();
527
528          //Set image origin
529          this->DataOrigin[0] = GdcmHeader.GetXOrigin();
530          this->DataOrigin[1] = GdcmHeader.GetYOrigin();
531          this->DataOrigin[2] = GdcmHeader.GetZOrigin();
532
533       }
534    } // End of loop on FileName
535
536    ///////// The files we CANNOT load are flaged. On debugging purposes
537    // count the loadable number of files and display their number:
538    int NumberCoherentFiles = 0;
539    for (std::list<std::string>::iterator Filename = InternalFileNameList.begin();
540         Filename != InternalFileNameList.end();
541         ++Filename)
542    {
543      if (*Filename != "GDCM_UNREADABLE")
544         NumberCoherentFiles++;    
545    }
546    vtkDebugMacro("Number of coherent files: " << NumberCoherentFiles);
547
548    if (ReturnedTotalNumberOfPlanes == 0)
549    {
550       vtkErrorMacro("No loadable file.");
551    }
552
553    vtkDebugMacro("Total number of planes on the stack: "
554                   << ReturnedTotalNumberOfPlanes);
555    
556    return ReturnedTotalNumberOfPlanes;
557 }
558
559 //-----------------------------------------------------------------------------
560 // Private
561 /*
562  * Remove all file names to the internal list of images to read.
563  */
564 void vtkGdcmReader::RemoveAllInternalFileName(void)
565 {
566    this->InternalFileNameList.clear();
567 }
568
569 /*
570  * Adds a file name to the internal list of images to read.
571  */
572 void vtkGdcmReader::AddInternalFileName(const char* name)
573 {
574    char * LocalName = new char[strlen(name) + 1];
575    strcpy(LocalName, name);
576    this->InternalFileNameList.push_back(LocalName);
577    delete[] LocalName;
578 }
579
580 /*
581  * Loads the contents of the image/volume contained by Filename at
582  * the Dest memory address. Returns the size of the data loaded.
583  */
584 size_t vtkGdcmReader::LoadImageInMemory(
585              std::string FileName, 
586              unsigned char * Dest,
587              const unsigned long UpdateProgressTarget,
588              unsigned long & UpdateProgressCount)
589 {
590    vtkDebugMacro("Copying to memory image" << FileName.c_str());
591    gdcmFile GdcmFile(FileName.c_str());
592    size_t size;
593
594    // If the data structure of vtk for image/volume representation
595    // were straigthforwards the following would be enough:
596    //    GdcmFile.GetImageDataIntoVector((void*)Dest, size);
597    // But vtk chooses to invert the lines of an image, that is the last
598    // line comes first (for some axis related reasons?). Hence we need
599    // to load the image line by line, starting from the end.
600
601    int NumColumns = GdcmFile.GetHeader()->GetXSize();
602    int NumLines   = GdcmFile.GetHeader()->GetYSize();
603    int NumPlanes  = GdcmFile.GetHeader()->GetZSize();
604    int LineSize   = NumComponents * NumColumns * GdcmFile.GetHeader()->GetPixelSize();
605
606    unsigned char * Source;
607    
608    if( GdcmFile.GetHeader()->HasLUT() )
609    {
610       size               = GdcmFile.GetImageDataSizeRaw();
611       Source             = (unsigned char*) GdcmFile.GetImageDataRaw();
612       unsigned char *Lut =                  GdcmFile.GetHeader()->GetLUTRGBA();
613
614       if(!this->LookupTable) 
615          this->LookupTable = vtkLookupTable::New();
616
617       this->LookupTable->SetNumberOfTableValues(256);
618       for (int tmp=0; tmp<256; tmp++)
619       {
620          this->LookupTable->SetTableValue(tmp,
621          (float)Lut[4*tmp+0]/255.0,
622          (float)Lut[4*tmp+1]/255.0,
623          (float)Lut[4*tmp+2]/255.0,
624          1);
625       }
626       this->LookupTable->SetRange(0,255);
627       vtkDataSetAttributes *a=this->GetOutput()->GetPointData();
628       a->GetScalars()->SetLookupTable(this->LookupTable);
629       free(Lut);
630    }
631    else
632    {
633       size        = GdcmFile.GetImageDataSize();
634       Source      = (unsigned char*)GdcmFile.GetImageData();
635    }
636    unsigned char * pSource     = Source; //pointer for later deletion
637    unsigned char * Destination = Dest + size - LineSize;
638
639    for (int plane = 0; plane < NumPlanes; plane++)
640    {
641       for (int line = 0; line < NumLines; line++)
642       {
643          // Copy one line at proper destination:
644          memcpy((void*)Destination, (void*)Source, LineSize);
645          Source      += LineSize;
646          Destination -= LineSize;
647          // Update progress related:
648          if (!(UpdateProgressCount%UpdateProgressTarget))
649          {
650             this->UpdateProgress(UpdateProgressCount/(50.0*UpdateProgressTarget));
651          }
652          UpdateProgressCount++;
653       }
654    }
655    //GetImageData allocate a (void*)malloc, remove it:
656    free(pSource);
657    return size;
658 }
659
660 //-----------------------------------------------------------------------------