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