Skip to content
Snippets Groups Projects
CreateFile.cpp 7.51 KiB
Newer Older
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
// $Id: CreateFile.cpp,v 1.50 2003-11-08 04:08:20 geuzaine Exp $
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
// Copyright (C) 1997-2003 C. Geuzaine, J.-F. Remacle
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
// 
// Please report all bugs and problems to "gmsh@geuz.org".

#include "Gmsh.h"
#include "GmshUI.h"
#include "Mesh.h"
#include "OpenFile.h"
#include "Draw.h"
#include "Context.h"
#include "Options.h"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
extern Context_T CTX;
extern Mesh M;

#include "gl2ps.h"
#include "gl2gif.h"
#include "gl2jpeg.h"
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#include "gl2png.h"
#include "gl2ppm.h"
#include "gl2yuv.h"

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void FillBuffer(void)
{
  InitOpengl();
  ClearOpengl();
  Draw3d();
  Draw2d();
}
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void CreateOutputFile(char *name, int format)
{
  FILE *fp;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  GLint size3d, viewport[4];
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  char ext[256];
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  int len, res, oldformat, psformat, pssort, psoptions;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  if(!name || !strlen(name))
    return;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  for(int i = 0; i < 4; i++) viewport[i] = CTX.viewport[i];

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  switch (format) {
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_AUTO:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    for(len = strlen(name) - 1; len >= 0; len--) {
      if(name[len] == '.') {
        strcpy(ext, &name[len]);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
        break;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(len <= 0)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      strcpy(ext, "");

    if(!strcmp(ext, ".geo"))
      CreateOutputFile(name, FORMAT_GEO);
    else if(!strcmp(ext, ".opt"))
      CreateOutputFile(name, FORMAT_OPT);
    else if(!strcmp(ext, ".msh"))
      CreateOutputFile(name, FORMAT_MSH);
    else if(!strcmp(ext, ".unv"))
      CreateOutputFile(name, FORMAT_UNV);
    else if(!strcmp(ext, ".gif"))
      CreateOutputFile(name, FORMAT_GIF);
    else if(!strcmp(ext, ".jpg"))
      CreateOutputFile(name, FORMAT_JPEG);
    else if(!strcmp(ext, ".jpeg"))
      CreateOutputFile(name, FORMAT_JPEG);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".png"))
      CreateOutputFile(name, FORMAT_PNG);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".ps"))
      CreateOutputFile(name, FORMAT_PS);
    else if(!strcmp(ext, ".eps"))
      CreateOutputFile(name, FORMAT_EPS);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".pdf"))
      CreateOutputFile(name, FORMAT_PDF);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".tex"))
      CreateOutputFile(name, FORMAT_TEX);
    else if(!strcmp(ext, ".pstex"))
      CreateOutputFile(name, FORMAT_PSTEX);
    else if(!strcmp(ext, ".epstex"))
      CreateOutputFile(name, FORMAT_EPSTEX);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".pdftex"))
      CreateOutputFile(name, FORMAT_PDFTEX);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else if(!strcmp(ext, ".jpegtex"))
      CreateOutputFile(name, FORMAT_JPEGTEX);
    else if(!strcmp(ext, ".ppm"))
      CreateOutputFile(name, FORMAT_PPM);
    else if(!strcmp(ext, ".yuv"))
      CreateOutputFile(name, FORMAT_YUV);
    else if(!strcmp(ext, ".gref"))
      CreateOutputFile(name, FORMAT_GREF);
    else if(!strcmp(ext, ".Gref"))
      CreateOutputFile(name, FORMAT_GREF);
    else if(!strcmp(ext, ".wrl"))
      CreateOutputFile(name, FORMAT_VRML);
    else
      Msg(GERROR, "Unknown extension '%s' for automatic format detection",
          ext);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_GEO:
    Print_Geo(&M, name);
    break;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_OPT:
    Print_Options(0, GMSH_FULLRC, name);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_MSH:
  case FORMAT_UNV:
  case FORMAT_GREF:
  case FORMAT_VRML:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    Print_Mesh(&M, name, format);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_JPEG:
  case FORMAT_JPEGTEX:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_PNG:
  case FORMAT_PNGTEX:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(!(fp = fopen(name, "wb"))) {
      Msg(GERROR, "Unable to open file '%s'", name);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(format == FORMAT_JPEGTEX || format == FORMAT_PNGTEX){
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      CTX.print.gl_fonts = 0;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    }
    FillBuffer();
Christophe Geuzaine's avatar
Christophe Geuzaine committed
    CTX.print.gl_fonts = 1;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(format == FORMAT_JPEG || format == FORMAT_JPEGTEX){
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      create_jpeg(fp, viewport[2]-viewport[0], viewport[3]-viewport[1], CTX.print.jpeg_quality);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      Msg(INFO, "JPEG creation complete '%s'", name);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    else{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      create_png(fp, viewport[2]-viewport[0], viewport[3]-viewport[1], 100);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      Msg(INFO, "PNG creation complete '%s'", name);
    Msg(STATUS2, "Wrote '%s'", name);
    fclose(fp);
    break;

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_PPM:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_YUV:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_GIF:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(!(fp = fopen(name, "wb"))) {
      Msg(GERROR, "Unable to open file '%s'", name);
    FillBuffer();
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(format == FORMAT_PPM){
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      create_ppm(fp, viewport[2]-viewport[0], viewport[3]-viewport[1]);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      Msg(INFO, "PPM creation complete '%s'", name);
    }
    else if (format == FORMAT_YUV){
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      create_yuv(fp, viewport[2]-viewport[0], viewport[3]-viewport[1]);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      Msg(INFO, "YUV creation complete '%s'", name);
    }
    else{
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      create_gif(fp, viewport[2]-viewport[0], viewport[3]-viewport[1],
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		 CTX.print.gif_dither,
		 CTX.print.gif_sort,
		 CTX.print.gif_interlace,
		 CTX.print.gif_transparent,
		 UNPACK_RED(CTX.color.bg),
		 UNPACK_GREEN(CTX.color.bg), UNPACK_BLUE(CTX.color.bg));
      Msg(INFO, "GIF creation complete '%s'", name);
    }
    Msg(STATUS2, "Wrote '%s'", name);
    fclose(fp);
    break;

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_PS:
  case FORMAT_PSTEX:
  case FORMAT_EPS:
  case FORMAT_EPSTEX:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_PDF:
  case FORMAT_PDFTEX:
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(!(fp = fopen(name, "wb"))) {
      Msg(GERROR, "Unable to open file '%s'", name);
      return;
    }

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(format == FORMAT_PDF || format == FORMAT_PDFTEX){
      psformat = GL2PS_PDF;
    }
    else{
      if(format == FORMAT_PS || format == FORMAT_PSTEX)
	psformat = GL2PS_PS;
      else
	psformat = GL2PS_EPS;
    }

    pssort = (CTX.print.eps_quality == 1) ? GL2PS_SIMPLE_SORT : GL2PS_BSP_SORT;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    psoptions =
      GL2PS_SIMPLE_LINE_OFFSET | GL2PS_SILENT | 
      (CTX.print.eps_occlusion_culling ? GL2PS_OCCLUSION_CULL : 0) |
      (CTX.print.eps_best_root ? GL2PS_BEST_ROOT : 0) |
      (CTX.print.eps_background ? GL2PS_DRAW_BACKGROUND : 0) |
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      (CTX.print.eps_compress ? GL2PS_COMPRESS : 0) |
      (format == FORMAT_PSTEX ? GL2PS_NO_TEXT : 0) |
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      (format == FORMAT_EPSTEX ? GL2PS_NO_TEXT : 0) |
      (format == FORMAT_PDFTEX ? GL2PS_NO_TEXT : 0);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

    size3d = 0;
    res = GL2PS_OVERFLOW;
    while(res == GL2PS_OVERFLOW) {
      size3d += 2048 * 2048;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
      gl2psBeginPage(CTX.base_filename, "Gmsh", viewport, 
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
		     psformat, pssort, psoptions, GL_RGBA, 0, NULL, 
		     0, 0, 0, size3d, fp, name);
Christophe Geuzaine's avatar
Christophe Geuzaine committed
      CTX.print.gl_fonts = 0;
Christophe Geuzaine's avatar
Christophe Geuzaine committed
      CTX.print.gl_fonts = 1;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    if(psformat == GL2PS_PDF)
      Msg(INFO, "PDF creation complete '%s'", name);
    else
      Msg(INFO, "PS/EPS creation complete '%s'", name);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    break;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  case FORMAT_TEX:
    if(!(fp = fopen(name, "w"))) {
      Msg(GERROR, "Unable to open file '%s'", name);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    gl2psBeginPage(CTX.base_filename, "Gmsh", viewport,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
                   GL2PS_TEX, GL2PS_NO_SORT, GL2PS_SILENT, GL_RGBA, 0, NULL, 
		   0, 0, 0, 1000, fp, name);
    CTX.print.gl_fonts = 0;
    FillBuffer();
    CTX.print.gl_fonts = 1;
    res = gl2psEndPage();
    Msg(INFO, "TEX creation complete '%s'", name);
    Msg(STATUS2, "Wrote '%s'", name);
    fclose(fp);
    break;

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  default:
    Msg(WARNING, "Unknown print format");