1 #include "../../include/Draw.h" 11 if( (ConfFile = fopen(
"Draw.conf",
"r")) == 0 ){
return;}
12 for(
int k=0;!(fgets(cLine,256,ConfFile)==NULL);k++){
13 if(1 == sscanf(cLine,
"Pre %lf",Val) )
15 else if(1 == sscanf(cLine,
"Passo %lf",Val))
17 else if(1 == sscanf(cLine,
"Box %lf",Val))
19 else if(1 == sscanf(cLine,
"Grid %lf",Val))
21 else if(3 == sscanf(cLine,
"Offset %lf %lf %lf",Val,Val+1,Val+2)){
22 xp = Val[0];yp = Val[1];zp = Val[2];
24 else if(3 == sscanf(cLine,
"Rotate %lf %lf %lf",Val,Val+1,Val+2)){
25 xa = Val[0];ya = Val[1];za = Val[2];
27 else if(3 == sscanf(cLine,
"Sun %lf %lf %lf",Val,Val+1,Val+2)){
28 xf = Val[0];yf = Val[1];zf = Val[2];
30 else if(3 == sscanf(cLine,
"Info %lf %lf %lf",Val,Val+1,Val+2)){
31 xi = Val[0];yi = Val[1];zi = Val[2];
33 else if(5 == sscanf(cLine,
"Legend %lf %lf %lf %lf %lf",Val,Val+1,Val+2,Val+3,Val+4)){
34 xLeg = Val[0];yLeg = Val[1];zLeg = Val[2];
dxLeg = Val[3];dyLeg = Val[4];
36 else if(3 == sscanf(cLine,
"Background %lf %lf %lf",Val,Val+1,Val+2)){
37 Rback = Val[0];Gback = Val[1];Bback = Val[2];
39 else if(1 == sscanf(cLine,
"Wheel %lf",Val)){
42 else if(3 == sscanf(cLine,
"GridEdge %lf %lf %lf",Val,Val+1,Val+2)){
57 uint16 photo=0, bps=0, spp=0, fillorder=0,resUnit=0;
58 uint32 ImWidth1=0,ImHeight1=0;
60 int imageOffset=0, result=0;
61 int stripMax=0, stripCount=0;
65 int bufferSize=0, count=0;
67 if((image = TIFFOpen(FileName,
"r")) == NULL){
68 fprintf(stderr,
"Could not open incoming image\n");
71 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &ImHeight1);
72 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &ImWidth1);
73 TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo);
74 TIFFGetField(image, TIFFTAG_FILLORDER, &fillorder);
75 TIFFGetField(image, TIFFTAG_XRESOLUTION, &xRes);
76 TIFFGetField(image, TIFFTAG_YRESOLUTION, &yRes);
77 TIFFGetField(image, TIFFTAG_RESOLUTIONUNIT, &resUnit);
78 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
79 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
80 stripSize = TIFFStripSize (image);
81 stripMax = TIFFNumberOfStrips (image);
83 bufferSize = TIFFNumberOfStrips (image) * stripSize;
84 ImHeight = (GLuint)ImHeight1;
86 NPixel =
ImWidth*ImHeight*Level;
89 buffer = (uint32 *)_TIFFmalloc(ImHeight1 * ImWidth1 *
sizeof (uint32));
91 fprintf(stderr,
"Could not allocate enough memory for the uncompressed image\n");
97 if(!TIFFReadRGBAImage(image,ImWidth1,ImHeight1,buffer,1)){
100 printf(
"The immage was not read\n");
101 throw "Corrupted TIFF file!";
103 for(
int h=0; h<ImHeight; h++) {
105 pixel[(h*ImWidth+w)*4+0] = buffer[(h*ImWidth+w)] & 0x000000ff;
106 pixel[(h*ImWidth+w)*4+1] = ((buffer[(h*ImWidth+w)] >> 8 ) & 0x0000ff);
107 pixel[(h*ImWidth+w)*4+2] = ((buffer[(h*ImWidth+w)] >> 16 ) & 0x00ff);
108 pixel[(h*ImWidth+w)*4+3] = ((buffer[(h*ImWidth+w)] >> 24 ) & 0xff);
118 sprintf(cImage,
"Image%05u.tif",
Step);
119 TIFF *tif = TIFFOpen( cImage,
"w");
121 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH,
ImWidth);
122 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ImHeight);
123 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
124 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
125 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
126 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
127 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4);
129 TIFFWriteEncodedStrip(tif, 0,
pixel,
ImWidth * ImHeight *4);
143 sprintf(cImage,
"Image%05u.tif",
Step);
144 TIFF *tif = TIFFOpen( cImage,
"w");
146 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH,
WinWidth);
147 TIFFSetField(tif, TIFFTAG_IMAGELENGTH,
WinHeight);
148 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
150 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
151 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
152 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4);
162 glReadPixels(0, 0,
WinWidth, WinHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);
163 char * MonaSoMare = (
char *) calloc(
WinWidth * WinHeight*4,
sizeof(
char));
167 MonaSoMare[(h*WinWidth+w)*4+l] = data[( (WinHeight-h)*WinWidth+w)*4 + l];
168 TIFFWriteEncodedStrip(tif, 0,MonaSoMare, WinWidth * WinHeight *4);
181 printf(
"libtiff not supported\n");
184 printf(
"libtiff not supported\n");
187 printf(
"libtiff not supported\n");
192 #include <pngwriter.h> 195 sprintf(cImage,
"Image%05u.png",
Step);
196 pngwriter Image(
ImWidth,ImHeight,1.0,cImage);
197 double Norm = 1./256.;
199 for(
int h=0;h<ImHeight;h++){
201 for(
int l=0;l<3;l++){
202 Color[l] = Norm*
pixel[(h*ImWidth+w)*4+l];
204 Image.plot(w,h,Color[0],Color[1],Color[2]);
215 int number_of_passes;
218 sprintf(cImage,
"Image%05u.png",
Step);
219 png_bytep * row_pointers = (png_bytep*) malloc(
sizeof(png_bytep) * ImHeight);
220 for (
int y=0; y<ImHeight; y++)
221 row_pointers[y] = (png_byte*) malloc(
ImWidth);
222 for (
int y=0; y<ImHeight; y++) {
223 png_byte* row = row_pointers[y];
224 for (
int x=0; x<
ImWidth; x++) {
225 png_byte* ptr = &(row[x*4]);
227 ptr[l] =
pixel[(y*ImWidth+x)*NLevel+l];
231 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
232 SigErr(!png_ptr,
"[write_png_file] png_create_write_struct failed");
233 info_ptr = png_create_info_struct(png_ptr);
234 SigErr(!info_ptr,
"[write_png_file] png_create_info_struct failed");
235 SigErr(setjmp(png_jmpbuf(png_ptr)),
"[read_png_file] Error during init_io");
236 png_set_sig_bytes(png_ptr, 8);
242 FILE *fp = fopen(cImage,
"wb");
243 SigErr(!fp,
"[write_png_file] File %s could not be opened for writing",cImage);
244 SigErr(setjmp(png_jmpbuf(png_ptr)),
"[write_png_file] Error during init_io");
247 png_init_io(png_ptr, fp);
259 SigErr(setjmp(png_jmpbuf(png_ptr)),
"[write_png_file] Error during writing header");
260 png_set_IHDR(png_ptr, info_ptr,
ImWidth, ImHeight,
261 bit_depth, color_type, PNG_INTERLACE_NONE,
262 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
263 png_write_info(png_ptr, info_ptr);
265 SigErr(setjmp(png_jmpbuf(png_ptr)),
"[write_png_file] Error during writing bytes");
266 png_write_image(png_ptr, row_pointers);
268 SigErr(setjmp(png_jmpbuf(png_ptr)),
"[write_png_file] Error during end of write");
269 png_write_end(png_ptr, NULL);
271 for (
int y=0; y<ImHeight; y++) free(row_pointers[y]);
278 printf(
"Pngwriter not installed\n");
281 printf(
"Pngwriter not installed\n");
288 if((File2Read = fopen(
"DrawScript.dr",
"r"))==0){
295 double *From = (
double *)calloc(3,
sizeof(
double));
296 double *To = (
double *)calloc(3,
sizeof(
double));
297 double *Hue = (
double *)calloc(5,
sizeof(
double));
298 char *cLine = (
char *)calloc(265,
sizeof(
char));
299 for(
int k=0;!(fgets(cLine,256,File2Read)==NULL);k++){
300 if(strncmp(cLine,
"Line",4)==0){
301 fgets(cLine,256,File2Read);
302 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3,Hue+4);
303 fgets(cLine,256,File2Read);
304 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
306 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
309 glNormal3f(0.,0.,1.);
310 glVertex3d(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
311 glVertex3d(To[0]*Expand[0],To[1]*Expand[1],To[2]*Expand[2]);
315 else if(strncmp(cLine,
"Curve",4)==0){
317 glBegin(GL_LINE_STRIP);
318 fgets(cLine,256,File2Read);
319 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
320 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
321 glNormal3f(0.,0.,1.);
324 fgetpos(File2Read,&FPos);
325 if(fgets(cLine,256,File2Read)==0)
break;
326 if(sscanf(cLine,
"%lf %lf %lf\n",From,From+1,From+2)!=3){
327 fsetpos(File2Read,&FPos);
330 glVertex3d(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
335 else if(strncmp(cLine,
"Polygon",4)==0){
338 fgets(cLine,256,File2Read);
339 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
340 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
341 glNormal3f(0.,0.,1.);
344 fgetpos(File2Read,&FPos);
345 if(fgets(cLine,256,File2Read)==0)
break;
346 if(sscanf(cLine,
"%lf %lf %lf\n",From,From+1,From+2)!=3){
347 fsetpos(File2Read,&FPos);
350 glVertex3d(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
355 else if(strncmp(cLine,
"Arrow",4)==0){
356 fgets(cLine,256,File2Read);
357 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3,Hue+4);
358 fgets(cLine,256,File2Read);
359 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
361 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
364 double Freccia[3] = {Expand[0],0.,0.};
366 for(
int d=0;d<3;d++){
367 Dist[d] = (To[d] - From[d])*Expand[d];
369 double Length1 = sqrt( QUAD(Dist[0]) + QUAD(Dist[1]) + QUAD(Dist[2]) );
370 double Length2 = sqrt( QUAD(Expand[0]) );
371 for(
int d=0;d<3;d++){
372 Axis[d] = .5*(Dist[d]/Length1 + Freccia[d]/Length2);
374 glTranslated(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
375 glRotatef(180.,Axis[0],Axis[1],Axis[2]);
376 glScaled(Length1/Length2,1.0,1.0);
377 glNormal3f(Dist[0],Dist[1],Dist[2]);
381 else if(strncmp(cLine,
"Sphere",6)==0){
382 fgets(cLine,256,File2Read);
383 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
384 fgets(cLine,256,File2Read);
385 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
387 glTranslated(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
388 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
389 glutSolidSphere(To[0]*Expand[0],(
int)To[1],(
int)To[2]);
392 else if(strncmp(cLine,
"Cylinder",7)==0){
393 fgets(cLine,256,File2Read);
394 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
395 fgets(cLine,256,File2Read);
396 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
398 glTranslated(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
399 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
400 glutSolidSphere(To[0]*Expand[0],(
int)To[1],(
int)To[2]);
403 else if(strncmp(cLine,
"Text",4)==0){
404 char *String = (
char *)calloc(265,
sizeof(
char));
405 fgets(cLine,256,File2Read);
406 for(
int c=0;c<strlen(cLine);c++)
407 String[c] = cLine[c];
409 fgets(cLine,256,File2Read);
410 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
411 fgets(cLine,256,File2Read);
412 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
414 glNormal3f(0.,0.,-1.);
415 glColor4f(Hue[0],Hue[1],Hue[2],Hue[3]);
416 glRasterPos3f(From[0]*Expand[0],From[1]*Expand[1],From[2]*Expand[2]);
417 for(
int i=0;i<strlen(String);i++)
418 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,String[i]);
423 else if(strncmp(cLine,
"Fog",3)==0){
425 GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR };
426 fgets(cLine,256,File2Read);
427 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
428 fgets(cLine,256,File2Read);
429 sscanf(cLine,
"%lf %lf %lf %lf %lf %lf\n",From,From+1,From+2,To,To+1,To+2);
430 GLuint fogfilter = From[0] > 2. ? 0 : (int) From[0];
431 GLfloat fogColor[4]= {Hue[0], Hue[1], Hue[2], Hue[3]};
432 glFogi(GL_FOG_MODE, fogMode[fogfilter]);
433 glFogfv(GL_FOG_COLOR, fogColor);
434 glFogf(GL_FOG_DENSITY, From[1]);
435 glHint(GL_FOG_HINT, GL_DONT_CARE);
436 glFogf(GL_FOG_START, From[2]*Expand[2]);
437 glFogf(GL_FOG_END, To[2]*Expand[2]);
440 else if(strncmp(cLine,
"Background",10)==0){
441 fgets(cLine,256,File2Read);
442 sscanf(cLine,
"%lf %lf %lf %lf\n",Hue,Hue+1,Hue+2,Hue+3);
443 glClearColor(Hue[0],Hue[1],Hue[2],Hue[3]);
GLfloat xp
Translation, wheel.
int GridEdge[3]
Number of lines per edge.
double InvScaleUn
Rescale the three orthogonal directions.
void ReadScript()
Reads and draw a script file.
GLuint Arrow
Refers to the list of a arrow.
GLfloat Rback
Background color.
int WritePngwriter()
Write a png file of the data in pixel (uses libpngwriter)
int Picture()
Write a tiff file of the data in pixel.
int WinHeight
Height of the window.
int gr
Puts/removes the grid.
int ImWidth
Width and height of the image.
int la
Puts/removes the box edges.
int OpenImage(const char *FileName)
Open a image to be store in pixel.
GLuint ScriptList
Refers to the list of the objects called by the script file.
GLfloat xLeg
Position of the legend.
int WritePng()
Write a png file of the data in pixel (not working)
int Step
Current step for the picture's name.
GLfloat xf
Orientation of the light.
GLfloat dxLeg
Width of the legend.
GLfloat xi
Position of the info string.
int WinWidth
Width of the window.
int NLevel
Levels of the images data (usually 4=RGBA)
GLubyte * pixel
Principal image (always allocated)
void ReadConf()
Reads and applies external configurations.
double GridStep
Finess of the grid.