Salome HOME
1d5eb76ff0030be6f7611eab1adcdc25ba503837
[modules/gui.git] / src / SVTK / SVTK_ImageWriter.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SVTK_ImageWriter.h"
23
24 #include <QSemaphore>
25
26 #include <vtkImageData.h>
27 #include <vtkImageClip.h>
28 #include <vtkJPEGWriter.h>
29 #include <vtkSmartPointer.h>
30
31 #ifdef _DEBUG_
32 static int MYDEBUG = 0;
33 #else
34 static int MYDEBUG = 0;
35 #endif
36
37
38 //----------------------------------------------------------------------------
39 SVTK_ImageWriter
40 ::SVTK_ImageWriter(QSemaphore* theSemaphore,
41                    vtkImageData* theImageData,
42                    const std::string& theName,
43                    int theProgressive,
44                    int theQuality):
45   mySemaphore(theSemaphore),
46   myImageData(theImageData),
47   myName(theName),
48   myProgressive(theProgressive),
49   myQuality(theQuality),
50   myConstraint16Flag(true)
51 {}
52
53 //----------------------------------------------------------------------------
54 SVTK_ImageWriter
55 ::~SVTK_ImageWriter()
56 {
57   if(MYDEBUG) cout<<"SVTK_ImageWriter::~SVTK_ImageWriter - this = "<<this<<endl;
58 }
59
60
61 //----------------------------------------------------------------------------
62 void
63 SVTK_ImageWriter
64 ::run()
65 {
66   vtkJPEGWriter *aWriter = vtkJPEGWriter::New();
67   vtkImageData *anImageData = myImageData;
68   vtkSmartPointer<vtkImageClip> anImageClip;
69   //
70   if(myConstraint16Flag){ 
71     int uExtent[6];
72     myImageData->UpdateInformation();
73     myImageData->GetUpdateExtent(uExtent);
74     unsigned int width = uExtent[1] - uExtent[0] + 1;
75     unsigned int height = uExtent[3] - uExtent[2] + 1;
76     width = (width / 16) * 16;
77     height= (height / 16) * 16;
78     uExtent[1] = uExtent[0] + width - 1;
79     uExtent[3] = uExtent[2] + height - 1;
80     //
81     anImageClip = vtkImageClip::New();
82     anImageClip->Delete();
83
84     anImageClip->SetInput(myImageData);
85     anImageClip->SetOutputWholeExtent(uExtent);
86     anImageClip->ClipDataOn();
87     anImageData = anImageClip->GetOutput();
88   }
89   //
90   aWriter->WriteToMemoryOff();
91   aWriter->SetFileName(myName.c_str());
92   aWriter->SetQuality(myQuality);
93   aWriter->SetProgressive(myProgressive);
94   aWriter->SetInput(anImageData);
95   aWriter->Write();
96
97   aWriter->Delete();
98   myImageData->Delete();
99
100   if(MYDEBUG) cout<<"SVTK_ImageWriter::run "<<
101                 "- this = "<<this<<
102                 //"; total = "<<mySemaphore->total()<<
103                 "; available = "<<mySemaphore->available()<<endl;
104   mySemaphore->release();
105 }
106