<commit_before>/*
  teem: <NAME>'s research software
  Copyright (C) 2002, 2001, 2000, 1999, 1998 University of Utah

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "unrrdu.h"
#include "privateUnrrdu.h"

/* bad bad bad Gordon */
extern void _nrrdGuessFormat(NrrdIO *io, const char *filename);
extern int _nrrdWriteDataHex (Nrrd *nrrd, NrrdIO *io);

int
unrrduFormatPlusParse(void *ptr, char *str, char err[AIR_STRLEN_HUGE]) {
  char me[]="unrrduParsePos";
  int *pos;

  if (!(ptr && str)) {
    sprintf(err, "%s: got NULL pointer", me);
    return 1;
  }
  pos = ptr;
  airToLower(str);
  if (!strcmp("eps", str)) {
    /* invent some unused value */
    pos[0] = 2*nrrdFormatLast;
  } else {
    pos[0] = airEnumVal(nrrdFormat, str);
    if (nrrdFormatUnknown == pos[0]) {
      sprintf(err, "%s: couldn't parse \"%s\" as format", me, str);
      return 1;
    }
  }
  return 0;
}

hestCB unrrduFormatPlusCB = {
  sizeof(int),
  "format",
  unrrduFormatPlusParse,
  NULL
};

int
unrrduEpsSave(char *out, NrrdIO *io, Nrrd *nout) {
  char me[]="unrrduEpsSave", err[AIR_STRLEN_MED];
  int color, sx, sy, fit;

  fit = nrrdFitsInFormat(nout, nrrdEncodingAscii, nrrdFormatPNM, AIR_TRUE);
  if (!fit) {
    sprintf(err, "%s: can't save image into EPS", me);
    biffMove(UNRRDU, err, NRRD); return 1;
  }
  color = (3 == fit);
  
  if (!( nrrdTypeUChar == nout->type )) {
    sprintf(err, "%s: can only save type %s data to EPS", me,
	    airEnumStr(nrrdType, nrrdTypeUChar));
    biffAdd(UNRRDU, err); return 1;
  }
  if (2 == nout->dim) {
    sx = nout->axis[0].size;
    sy = nout->axis[1].size;
  } else {
    sx = nout->axis[1].size;
    sy = nout->axis[2].size;
  }

  if (!(io->dataFile = fopen(out, "w"))) {
    sprintf(err, "%s: fopen(\"%s\", \"w\") failed: %s", me,
	    out, strerror(errno));
    biffAdd(UNRRDU, err); return 1;
  }
  fprintf(io->dataFile, "%%!PS-Adobe-2.0 EPSF-2.0\n");
  fprintf(io->dataFile, "%%%%Creator: unu\n");
  fprintf(io->dataFile, "%%%%Title: %s\n",
	  nout->content ? nout->content : NRRD_UNKNOWN);
  fprintf(io->dataFile, "%%%%Pages: 1\n");
  fprintf(io->dataFile, "%%%%BoundingBox: 0 0 %d %d\n", sx, sy);
  fprintf(io->dataFile, "%%%%EndComments\n");
  fprintf(io->dataFile, "%% linestr creates an empty string to hold one scanline\n");
  fprintf(io->dataFile, "/linestr %d string def\n", sx*(color ? 3 : 1));
  fprintf(io->dataFile, "%%%%EndProlog\n");
  fprintf(io->dataFile, "%%%%Page: 1 1\n");
  fprintf(io->dataFile, "gsave\n");
  fprintf(io->dataFile, "%d %d scale\n", sx, sy);
  fprintf(io->dataFile, "%d %d 8\n", sx, sy);
  fprintf(io->dataFile, "[%d 0 0 -%d 0 %d]\n", sx, sy, sy);
  fprintf(io->dataFile, "{currentfile linestr readhexstring pop} %s\n",
	  color ? "false 3 colorimage" : "image");
  _nrrdWriteDataHex(nout, io);
  fprintf(io->dataFile, "\n");
  fprintf(io->dataFile, "grestore\n");

  
  return 0;
}

#define INFO "Write nrrd with specific format, encoding, or endianness"
char *_unrrdu_saveInfoL =<commit_msg>allowed EPS files to save to stdout<commit_after>/*
  teem: <NAME>'s research software
  Copyright (C) 2002, 2001, 2000, 1999, 1998 University of Utah

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "unrrdu.h"
#include "privateUnrrdu.h"

/* bad bad bad Gordon */
extern void _nrrdGuessFormat(NrrdIO *io, const char *filename);
extern int _nrrdWriteDataHex (Nrrd *nrrd, NrrdIO *io);

int
unrrduFormatPlusParse(void *ptr, char *str, char err[AIR_STRLEN_HUGE]) {
  char me[]="unrrduParsePos";
  int *pos;

  if (!(ptr && str)) {
    sprintf(err, "%s: got NULL pointer", me);
    return 1;
  }
  pos = ptr;
  airToLower(str);
  if (!strcmp("eps", str)) {
    /* invent some unused value */
    pos[0] = 2*nrrdFormatLast;
  } else {
    pos[0] = airEnumVal(nrrdFormat, str);
    if (nrrdFormatUnknown == pos[0]) {
      sprintf(err, "%s: couldn't parse \"%s\" as format", me, str);
      return 1;
    }
  }
  return 0;
}

hestCB unrrduFormatPlusCB = {
  sizeof(int),
  "format",
  unrrduFormatPlusParse,
  NULL
};

int
unrrduEpsSave(char *out, NrrdIO *io, Nrrd *nout) {
  char me[]="unrrduEpsSave", err[AIR_STRLEN_MED];
  int color, sx, sy, fit;

  fit = nrrdFitsInFormat(nout, nrrdEncodingAscii, nrrdFormatPNM, AIR_TRUE);
  if (!fit) {
    sprintf(err, "%s: can't save image into EPS", me);
    biffMove(UNRRDU, err, NRRD); return 1;
  }
  color = (3 == fit);
  
  if (!( nrrdTypeUChar == nout->type )) {
    sprintf(err, "%s: can only save type %s data to EPS", me,
	    airEnumStr(nrrdType, nrrdTypeUChar));
    biffAdd(UNRRDU, err); return 1;
  }
  if (2 == nout->dim) {
    sx = nout->axis[0].size;
    sy = nout->axis[1].size;
  } else {
    sx = nout->axis[1].size;
    sy = nout->axis[2].size;
  }

  if (!strcmp("-", out)) {
    io->dataFile = stdout;
  } else {
    if (!(io->dataFile = fopen(out, "w"))) {
      sprintf(err, "%s: fopen(\"%s\", \"w\") failed: %s", me,
	      out, strerror(errno));
      biffAdd(UNRRDU, err); return 1;
    }
  }

  fprintf(io->dataFile, "%%!PS-Adobe-2.0 EPSF-2.0\n");
  fprintf(io->dataFile, "%%%%Creator: unu\n");
  fprintf(io->dataFile, "%%%%Title: %s\n",
	  nout->content ? nout->content : NRRD_UNKNOWN);
  fprintf(io->dataFile, "%%%%Pages: 1\n");
  fprintf(io->dataFile, "%%%%BoundingBox: 0 0 %d %d\n", sx, sy);
  fprintf(io->dataFile, "%%%%EndComments\n");
  fprintf(io->dataFile, "%% linestr creates an empty string to hold one scanline\n");
  fprintf(io->dataFile, "/linestr %d string def\n", sx*(color ? 3 : 1));
  fprintf(io->dataFile, "%%%%EndProlog\n");
  fprintf(io->dataFile, "%%%%Page: 1 1\n");
  fprintf(io->dataFile, "gsave\n");
  fprintf(io->dataFile, "%d %d scale\n", sx, sy);
  fprintf(io->dataFile, "%d %d 8\n", sx, sy);
  fprintf(io->dataFile, "[%d 0 0 -%d 0 %d]\n", sx, sy, sy);
  fprintf(io->dataFile, "{currentfile linestr readhexstring pop} %s\n",
	  color ? "false 3 colorimage" : "image");
  _nrrdWriteDataHex(nout, io);
  fprintf(io->dataFile, "\n");
  fprintf(io->dataFile, "grestore\n");

  
  return 0;
}

#define INFO "Write nrrd with specific format, encoding, or endianness"
char *_unrrdu_saveInfoL =
{'fraction_non_alphanumeric': 0.16262462947992454, 'fraction_numerical': 0.018593371059013743, 'mean_word_length': 3.701076630778974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5585951', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2801, 'n_words': 820}