Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
SoImageWriter.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26#ifdef G4VIS_BUILD_OI_DRIVER
27
28/*----------------------------HEPVis----------------------------------------*/
29/* */
30/* Node: SoImageWriter */
31/* Author: Guy Barrand */
32/* */
33/*--------------------------------------------------------------------------*/
34
35// this :
37
38#include <Inventor/errors/SoDebugError.h>
39#include <Inventor/elements/SoViewportRegionElement.h>
40#include <Inventor/actions/SoGLRenderAction.h>
41
42#include <HEPVis/SbGL.h>
43#include <HEPVis/SbPainterPS.h>
44//#include <HEPVis/SbGIF.h>
45
46#include <stdlib.h>
47
48typedef struct {
49 unsigned char red;
50 unsigned char green;
51 unsigned char blue;
52} Pixel;
53typedef unsigned char Uchar;
54
55//static void getImagePixels(int,int,float*,int&,
56// Uchar*&,Uchar*&,Uchar*&,Uchar*&);
57
58static int sWidth = 0;
59static int sHeight = 0;
60static float* sImage = 0;
61static int getRGB(unsigned int,unsigned int,double&,double&,double&);
62
63SO_NODE_SOURCE(SoImageWriter)
64//////////////////////////////////////////////////////////////////////////////
66)
67//////////////////////////////////////////////////////////////////////////////
68//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
69{
70 static bool first = true;
71 if (first) {
72 first = false;
73 SO_NODE_INIT_CLASS(SoImageWriter,SoNode,"Node");
74 }
75}
76//////////////////////////////////////////////////////////////////////////////
78)
79:fEnabled(FALSE)
80,fStatus(FALSE)
81//////////////////////////////////////////////////////////////////////////////
82//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
83{
84 SO_NODE_CONSTRUCTOR(SoImageWriter);
85 //SO_NODE_ADD_FIELD(format,(POST_SCRIPT));
86 SO_NODE_ADD_FIELD(fileName,("out.ps"));
87
88 //SO_NODE_DEFINE_ENUM_VALUE(Format,POST_SCRIPT);
89 //SO_NODE_DEFINE_ENUM_VALUE(Format,GIF);
90
91 //SO_NODE_SET_SF_ENUM_TYPE(format,Format);
92}
93//////////////////////////////////////////////////////////////////////////////
95)
96//////////////////////////////////////////////////////////////////////////////
97//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
98{
99}
100//////////////////////////////////////////////////////////////////////////////
102)
103//////////////////////////////////////////////////////////////////////////////
104//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
105{
106 fEnabled = TRUE;
107}
108//////////////////////////////////////////////////////////////////////////////
110)
111//////////////////////////////////////////////////////////////////////////////
112//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
113{
114 fEnabled = FALSE;
115}
116//////////////////////////////////////////////////////////////////////////////
118) const
119//////////////////////////////////////////////////////////////////////////////
120//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
121{
122 return fStatus;
123}
124//////////////////////////////////////////////////////////////////////////////
126 SoGLRenderAction* aAction
127)
128//////////////////////////////////////////////////////////////////////////////
129//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
130{
131 fStatus = FALSE;
132 //printf("debug : SoImageWriter::GLRender : enabled : %d\n",fEnabled);
133 if(!fEnabled) return;
134 SbViewportRegion vpr = SoViewportRegionElement::get(aAction->getState());
135 const SbVec2s& win = vpr.getWindowSize();
136 int w = win[0];
137 int h = win[1];
138 if((w*h)<=0) {
139 SoDebugError::postInfo("SoImageWriter::GLRender","null area window !");
140 return;
141 }
142
143 int x = 0;
144 int y = 0;
145 int s = 3 * w * h;
146 float* image = new float[s];
147 if(!image) return;
148
149 //printf("debug : SoImageWriter::GLRender : %d %d %d %d\n",x,y,w,h);
150
151 //glReadPixels(x,y,w,h,GL_RGB,GL_UNSIGNED_BYTE,image); Don't work !
152 glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image);
153
154 //Format fm = (Format)format.getValue();
155 //if(fm==GIF) {
156/*
157 FILE* file = fopen(fileName.getValue().getString(),"wb");
158 if(!file) {
159 SoDebugError::postInfo("SoImageWriter::GLRender",
160 "can't open file \"%s\".",fileName.getValue().getString());
161 } else {
162 int coln;
163 Uchar* rs;
164 Uchar* gs;
165 Uchar* bs;
166 Uchar* data;
167 getImagePixels(w,h,image,coln,rs,gs,bs,data);
168
169 SbGIF::putBytesInStream(file,data,w,h,coln,rs,gs,bs);
170
171 delete [] data;
172
173 if(rs) free(rs);
174 if(gs) free(gs);
175 if(bs) free(bs);
176
177 fclose(file);
178
179 fStatus = TRUE;
180 }
181 } else {
182*/
183
184 SbPainterPS painterPS;
185 painterPS.openFileForWriting(fileName.getValue().getString());
186 if(!painterPS.getStream()) {
187 SoDebugError::postInfo("SoImageWriter::GLRender",
188 "can't open file \"%s\".",fileName.getValue().getString());
189 } else {
190 painterPS.setWindowSize(w,h);
191 //painterPS.setBitsPerPixel(8);
192 painterPS.setBitsPerPixel(4);
193 painterPS.beginTraversal();
194 painterPS.clearColorBuffer(1.,1.,1.);
195
196 sWidth = w;
197 sHeight = h;
198 sImage = image;
199 painterPS.putImageInStream((unsigned int)w,(unsigned int)h,getRGB);
200
201 painterPS.endTraversal();
202
203 painterPS.closeStream();
204
205 fStatus = TRUE;
206 }
207 //}
208 delete [] image;
209
210}
211/*
212//////////////////////////////////////////////////////////////////////////////
213void getImagePixels (
214 int aWidth
215,int aHeight
216,float* aImage
217,int& aColorn
218,Uchar*& aReds
219,Uchar*& aGreens
220,Uchar*& aBlues
221,Uchar*& aData
222)
223//////////////////////////////////////////////////////////////////////////////
224//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
225{
226 aColorn = 0;
227 aReds = 0;
228 aGreens = 0;
229 aBlues = 0;
230 aData = 0;
231 if( (aWidth * aHeight) <=0) return;
232 int size = 256;
233 Uchar* rs = (Uchar*)malloc(size * sizeof(Uchar));
234 Uchar* gs = (Uchar*)malloc(size * sizeof(Uchar));
235 Uchar* bs = (Uchar*)malloc(size * sizeof(Uchar));
236 Uchar* data = new Uchar[aWidth * aHeight];
237 if( !rs || !gs || !bs || !data ) {
238 if(rs) free(rs);
239 if(gs) free(gs);
240 if(bs) free(bs);
241 delete [] data;
242 return;
243 }
244 int pixeln = 0;
245 int row,col;
246 Uchar red,green,blue;
247 Uchar ored = 0,ogreen = 0,oblue = 0;
248 float* pimag = aImage;
249 Uchar* pdata = 0;
250 Uchar index = 0;
251 int status = 0;
252 for(row=0;row<aHeight;row++) {
253 pdata = data + (aHeight - 1 - row) * aWidth;
254 for(col=0;col<aWidth;col++){
255 red = (Uchar)(255 * (*pimag));pimag++;
256 green = (Uchar)(255 * (*pimag));pimag++;
257 blue = (Uchar)(255 * (*pimag));pimag++;
258 //printf("debug : %d %d : %d %d %d\n",row,col,red,green,blue);
259 if( (pixeln==0) || (red!=ored) || (green!=ogreen) || (blue!=oblue) ){
260 // Search exact color :
261 int found = 0;
262 for(int count=0;count<pixeln;count++){
263 if( (red==rs[count]) && (green==gs[count]) && (blue==bs[count]) ){
264 found = 1;
265 index = count;
266 break;
267 }
268 }
269 if(found==0){
270 if(pixeln>=256) {
271 // We can't store more than 256 on an Uchar.
272 // Search closest color :
273 int dr,dg,db;
274 int PRECISION = 20;
275 int closest = 0;
276 for(int count=0;count<pixeln;count++){
277 dr = red - rs[count];dr = dr<0 ? -dr : dr;
278 dg = green - gs[count];dg = dg<0 ? -dg : dg;
279 db = blue - bs[count];db = db<0 ? -db : db;
280 if( (dr<=PRECISION) && (dg<=PRECISION) && (db<=PRECISION) ){
281 closest = 1;
282 index = count;
283 break;
284 }
285 }
286 if(closest==0) {
287 index = 0;
288 status = 1;
289 }
290 } else {
291 if(pixeln>=size){
292 size += 256;
293 rs = (Uchar*)realloc(rs,size * sizeof(Uchar));
294 gs = (Uchar*)realloc(gs,size * sizeof(Uchar));
295 bs = (Uchar*)realloc(bs,size * sizeof(Uchar));
296 if( !rs || !gs || !bs ) {
297 if(rs) free(rs);
298 if(gs) free(gs);
299 if(bs) free(bs);
300 delete [] data;
301 return;
302 }
303 }
304 //printf("debug : SoImageWriter pixeln %d : %d %d %d\n",
305 // pixeln,red,green,blue);
306 rs[pixeln] = red;
307 gs[pixeln] = green;
308 bs[pixeln] = blue;
309 index = pixeln;
310 pixeln++;
311 }
312 }
313 }
314 *pdata = index;
315 pdata++;
316 ored = red;
317 ogreen = green;
318 oblue = blue;
319 }
320 }
321 if(status==1)
322 printf("SoImageWriter : more than 256 colors in picture ; some colors approximated.\n");
323 aColorn = pixeln;
324 aReds = rs;
325 aGreens = gs;
326 aBlues = bs;
327 aData = data;
328}
329*/
330//////////////////////////////////////////////////////////////////////////////
331int getRGB(
332 unsigned int aX
333,unsigned int aY
334,double& aRed
335,double& aGreen
336,double& aBlue
337)
338//////////////////////////////////////////////////////////////////////////////
339// OpenGL image is from down to up.
340// PS image is up to down.
341//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
342{
343 float* pimag = sImage + 3 * (sWidth * (sHeight - 1 - aY) + aX);
344 aRed = *pimag;pimag++;
345 aGreen = *pimag;pimag++;
346 aBlue = *pimag;pimag++;
347 return 1;
348}
349
350#endif
#define TRUE
Definition: Globals.hh:27
#define FALSE
Definition: Globals.hh:23
void endTraversal()
void beginTraversal()
void putImageInStream(unsigned int, unsigned int, GetRGB_Function)
void setBitsPerPixel(int)
void clearColorBuffer(float, float, float)
void openFileForWriting(const char *)
void * getStream()
void closeStream()
void setWindowSize(int, int)
virtual void GLRender(SoGLRenderAction *)
virtual ~SoImageWriter()
static void initClass()
SbBool getStatus() const
SoSFString fileName
Definition: SoImageWriter.h:50