示例#1
0
main(int argc, char *argv[]){
  FILE *fp;
  gdImagePtr im, im2;
  int i,x,y,xsize,ysize,c;

  if(argc<7){
    fprintf(stderr, "[%s] compiled [%s/%s]\n", argv[0], __DATE__, __TIME__);
    fprintf(stderr, "Usage : %s x1 x2 y1 y2 in-gif out-gif [-blue]\n", argv[0]);
    exit(1);
  }
  if(argc>7){
    if(strcmp(argv[7], "-blue")==0) blueMode = 1;
  }

  x1 = atoi(argv[1]);
  x2 = atoi(argv[2]);
  y1 = atoi(argv[3]);
  y2 = atoi(argv[4]);
  infile  = argv[5];
  outfile = argv[6];

  fp = fopen(infile, "rb");
  if(!fp){
    fprintf(stderr, "Can't open [%s]\n", infile);
    exit(1);
  }
  im = gdImageCreateFromGif(fp);
  fclose(fp);

  xsize = gdImageSX(im);
  ysize = gdImageSY(im);

  im2 = gdImageCreate(xsize, ysize);

  if(blueMode){		// use bluish gradation for low index value
    int j;
    for(i=0; i<256; i++){
      j = (i-blueValue)*255/(255-blueValue); 	//////////////// blueValue --> 0, 255 --> 255
      j = (j<0)?0:j;
      j=  (j>255)?255:j;
      gdImageColorAllocate(im2, j, j, i);
    }
  } else {
    for(i=0; i<256; i++)
      gdImageColorAllocate(im2, i, i, i);
  }


  for(y=0; y<ysize; y++){
    for(x=0; x<xsize; x++){
      c = gdImageGetPixel(im, x, y);
      { int r,g,b;			// fixed 2001.07.09
	r = gdImageRed(im, c);
	g = gdImageGreen(im, c);
	b = gdImageBlue(im, c);
	c = ( g + g + r + b )/4;
      }
      indexMap(&c);
      gdImageSetPixel(im2, x, y, c);
    }
  }
  gdImageDestroy(im);

  fp = fopen(outfile, "wb");
  if(!fp){
    fprintf(stderr, "Can't open [%s]\n", outfile);
    exit(1);
  }
  gdImageGif(im2, fp);
  fclose(fp);
}
示例#2
0
文件: mandel.c 项目: JackDrogon/Study
int 
draw_mandel (char *filename,
              int    width, int height,
              double origin_real,
              double origin_imag,
              double range,
              double max_iterations)
{
    complex origin;
    int colors[100], color, white, x, y, i;
    FILE *out;
    gdImagePtr im_out;

    origin.r = origin_real;  /* Measured from top-left */
    origin.i = origin_imag;  
    if (!(out = fopen(filename, "wb"))) {
        fprintf(stderr, "File %s could not be opened\n");
            return 1;
    }
        
    im_out = gdImageCreate(width, height);
    /* Start from black, and increment r,g,b values to get different
       values of gray. */
    for (i = 0; i < 50; i++) {
            color = i * 4;
            colors[i] = gdImageColorAllocate(im_out, color,color,color);
    }
    white =        gdImageColorAllocate(im_out, 255,255,255);

    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            complex z, c ;
            int  iter;

           /* Convert every point on the canvas to an equivalent
            *  complex number, given the origin and the range. The
            *  range acts like an inverse zoom factor.*/
            c.r = origin.r + (double) x / (double) width * range;
            c.i = origin.i - (double) y / (double) height * range;

           /* Examine each point calculated above to see if 
            * repeated substitutions into an equation like
            * z(next) = z**z + z remains within a definite boundary.
            * If after a <max_iterations> iterations it still hasn't gone
            * beyond the white area, it belongs to the Mandelbrot set.
            * But if it does, we assign it a color depending on how
            * quickly it quit. So the points that don't belong to
            * the Mandelbrot set are the ones that give it its infinitely
            * recursive beauty. */
            color = white;
            z.r = z.i = 0.0; /* Starting point */
            for (iter = 0; iter < max_iterations; iter++) {
                double dist, new_real, new_imag;
                /*calculate  z = z^2 + c */
                /* If z = a + bi, z^2 => a^2 - b^2 + 2abi; */
                new_real = z.r * z.r - z.i * z.i + c.r;
                new_imag = 2 * z.r * z.i + c.i;
                z.r = new_real; z.i = new_imag;
                /* Pythagorean distance from 0,0 */
                dist = new_real * new_real + new_imag * new_imag; 
                if (dist >= 4) {
                    /* No point on the mandelbrot set is more than
                       2 units away from the origin. If it quits
                       the boundary, give that 'c' an interesting 
                       color, depending on how far the series wants
                       to jump out of its bounds */
                    color = colors[(int) dist % i];
                    break;
                }
            }
            gdImageSetPixel(im_out, x,y, color);

        }
    }
    gdImageGif(im_out,out);
    fclose(out);
    return 0;
}
示例#3
0
int main(int argc, char** argv)
{
  int i, j, count;
  FILE* f;

  char *prefix, *titleString, *temperature;

  int format; /* 0: PS  1: PNG  2: GIF  3: JPEG */
  int machine;

  char* buffer; /* for system() and fopen() */
  char* plotFile;

  /* functions to call - either PS or PNG */
  void (*init)();
  void (*title)(char*);
  void (*border)();
  void (*grid)();
  void (*plotDot)(int, int, double);
  void (*vertCenter)(char*, int);
  void (*horzCenter)(char*, int);
  void (*selection)(char*, int);

  g_filter = 0;
  g_grid = -1;
  g_dotSize = -1;
  g_top = g_left = g_size = -1;
  g_selectedI = g_selectedJ = -1;
  format = 0;
  machine = 0;
  titleString = NULL;
  g_cutoffValue = 0;
  getColor = getColorLogLog;
  g_epsilon = 0.01;
  temperature = "37";

  while ((count = getopt_long(argc, argv, "Vht:c:e:g:d:u:l:s:f:i:j:p:r:mo:", OPTIONS, NULL)) != -1)
    {
      if (count == 'V')
	version("hybrid-plot-ng");
      else if (count == 'h' || count == '?')
	{
	  puts("Usage: hybrid-plot-ng [options] <file prefix>");
	  puts("");
	  puts("Options:");
	  puts("-V, --version");
	  puts("-h, --help");
	  puts("-t, --temperature=<temperature>");
	  puts("-c, --colors=(linear | log | double) (defaults to double)");
	  puts("-e, --epsilon=<color epsilon> (defaults to .01)");
	  puts("-g, --grid=<grid spacing>");
	  puts("-d, --dot=<dot size>");
	  puts("-u, --top=<initial i>");
	  puts("-l, --left=<initial j>");
	  puts("-s, --size=<size of square>");
	  printf("-f, --format=(ps");
#if HAVE_GD_PNG
	  printf(" | png");
#endif
#if HAVE_GD_GIF
	  printf(" | gif");
#endif
#if HAVE_GD_JPEG
	  printf(" | jpeg");
#endif
	  puts(") (defaults to ps)");
	  puts("-i, --i=<selected i>");
	  puts("-j, --j=<selected j>");
	  puts("-p, --title=<plot title>");
	  puts("-r, --filter=(on | off) (defaults to off)");
	  puts("-o, --cutoff=<store cutoff>");
	  puts("");
	  puts("Report bugs to " PACKAGE_BUGREPORT);
	  return EXIT_SUCCESS;
	}
      else if (count == 't')
	temperature = optarg;
      else if (count == 'c')
	{
	  if (!strcmp(optarg, "linear"))
	    getColor = getColorLinear;
	  else if (!strcmp(optarg, "log"))
	    getColor = getColorLog;
	  else if (!strcmp(optarg, "double"))
	    getColor = getColorLogLog;
	}
      else if (count == 'e')
	g_epsilon = atof(optarg);
      else if (count == 'g')
	g_grid = atoi(optarg);
      else if (count == 'd')
	g_dotSize = atoi(optarg);
      else if (count == 'u')
	g_top = atoi(optarg);
      else if (count == 'l')
	g_left = atoi(optarg);
      else if (count == 's')
	g_size = atoi(optarg);
      else if (count == 'f')
	{
	  if (!strcmp(optarg, "ps"))
	    format = 0;
	  else if (!strcmp(optarg, "png"))
	    format = 1;
	  else if (!strcmp(optarg, "gif"))
	    format = 2;
	  else if (!strcmp(optarg, "jpeg"))
	    format = 3;
	}
      else if (count == 'i')
	g_selectedI = atoi(optarg);
      else if (count == 'j')
	g_selectedJ = atoi(optarg);
      else if (count == 'p')
	  {
	    titleString = xmalloc(strlen(optarg) + 1);
	    strcpy(titleString, optarg);
	  }
      else if (count == 'r')
	{
	  if (!strcmp(optarg, "on"))
	    g_filter = 1;
	  else if (!strcmp(optarg, "off"))
	    g_filter = 0;
	}
      else if (count == 'm')
	machine = 1;
      else if (count == 'o')
	g_cutoffValue = atof(optarg);
   }

  if (optind >= argc)
    {
      fputs("Error: no prefix specified\nRun 'hybrid-plot-ng -h' for help\n", stderr);
      return EXIT_FAILURE;
    }

  plotFile = xmalloc(strlen(argv[optind]) + 107);
  strcpy(plotFile, argv[optind]);

  for (i = 0; i <= strlen(argv[optind]); ++i)
    {
      if (argv[optind][i] == '-')
	{
	  g_ss = 0;
	  argv[optind][i] = 0;
	  break;
	}
      else if (argv[optind][i] == 0)
	{
	  g_ss = 1;
	  break;
	}
    }

  g_file1 = argv[optind];
  if (g_ss) /* from hybrid-ss */
    g_file2 = g_file1;
  else      /* from hybrid */
    g_file2 = argv[optind] + i + 1;

  if (g_ss)
    prefix = g_file1;
  else
    {
      prefix = xmalloc(strlen(g_file1) + 1 + strlen(g_file2) + 1);
      strcpy(prefix, g_file1);
      strcat(prefix, "-");
      strcat(prefix, g_file2);
    }

  if (!(f = fopen(g_file1, "rt")))
    {
      buffer = xmalloc(strlen(g_file1) + 5);
      strcpy(buffer, g_file1);
      strcat(buffer, ".seq");
      if (!(f = fopen(buffer, "rt")))
	{
	  perror(buffer);
	  return EXIT_FAILURE;
	}
      free(buffer);
    }
  input(f, &g_name1, &g_string1);
  fclose(f);
  if (!g_name1)
    g_name1 = g_file1;
  g_len1 = strlen(g_string1);

  if (g_ss)
    {
      g_name2 = g_name1;
      g_string2 = g_string1;
      g_len2 = g_len1;
    }
  else
    {
      if (!(f = fopen(g_file2, "rt")))
	{
	  buffer = xmalloc(strlen(g_file2) + 5);
	  strcpy(buffer, g_file2);
	  strcat(buffer, ".seq");
	  if (!(f = fopen(buffer, "rt")))
	    {
	      perror(buffer);
	      return EXIT_FAILURE;
	    }
	  free(buffer);
	}
      input(f, &g_name2, &g_string2);
      fclose(f);
      if (!g_name2)
	g_name2 = g_file2;;
      g_len2 = strlen(g_string2);
    }

  strcat(plotFile, ".");
  strcat(plotFile, temperature);
  strcat(plotFile, ".plot");

  if (!(f = fopen(plotFile, "rt")))
    {
      perror(plotFile);
      return EXIT_FAILURE;
    }
  g_scores = inputRecords(f);
  fclose(f);
  free(plotFile);

  if (!titleString)
    {
      titleString = xmalloc(1 + strlen(g_name1) + 7 + strlen(g_name2) + 5 + strlen(temperature) + 9);
      if (g_ss)
	sprintf(titleString, "'%s' at %s degrees", g_name1, temperature);
      else
	sprintf(titleString, "'%s' vs. '%s' at %s degrees", g_name1, g_name2, temperature);
    }

  if (g_top == -1 || g_left == -1 || g_size == -1)
    {
      g_top = g_left = 1;
      g_size = g_len1 > g_len2 ? g_len1 : g_len2;
      fixSize();
    }

  g_dotSpacing = (double) 484 / g_size;
  if (g_dotSize == -1)
    g_dotSize = roundInt(g_dotSpacing);
  if (g_dotSize < 1)
    g_dotSize = 1;
  if (g_grid < 0)
    {
      g_grid = g_size / 8;
      fixGrid();
    }
  if (g_grid)
    {
      g_labels = g_grid;
      fixLabels();
    }

  init = initPS;
  title = titlePS;
  border = borderPS;
  grid = gridPS;
  plotDot = plotDotPS;
  vertCenter = vertCenterPS;
  horzCenter = horzCenterPS;
  selection = selectionPS;
#if HAVE_GD
  if (format)
    {
      init = initPNG;
      title = titlePNG;
      border = borderPNG;
      grid = gridPNG;
      plotDot = plotDotPNG;
      vertCenter = vertCenterPNG;
      horzCenter = horzCenterPNG;
      selection = selectionPNG;
    }
#endif

  buffer = xmalloc(strlen(prefix) + 1 + strlen(temperature) + 5);
  strcpy(buffer, prefix);
  strcat(buffer, ".");
  strcat(buffer, temperature);
  if (format == 0)
    {
      strcat(buffer, ".ps");
      g_file = fopen(buffer, "wt");
    }
  else
    {
      if (format == 1)
	strcat(buffer, ".png");
      else if (format == 2)
	strcat(buffer, ".gif");
      else
	strcat(buffer, ".jpg");
      g_file = fopen(buffer, "wb");
#if HAVE_GD
      g_image = gdImageCreate(612, 612);
#endif
    }
  if (!g_file)
    {
      perror(buffer);
      return EXIT_FAILURE;
    }
  free(buffer);

  init();

  title(titleString);

  border();

  grid();

  for (i = 1; i <= g_len1; ++i)
    for (j = 1; j <= g_len2; ++j)
      if (g_scores[(i - 1) * g_len2 + j - 1] >= g_cutoffValue)
	if (g_filter == 0 || filter(i, j))
	  plotDot(i, j, g_scores[(i - 1) * g_len2 + j - 1]);

  if (g_selectedI > 0 && g_selectedJ > 0)
    {
      buffer = xmalloc(24);
      sprintf(buffer, "Selected: (%d-%c, %d-%c), %g",
	      g_selectedI, g_string1[g_selectedI - 1],
	      g_selectedJ, g_string2[g_selectedJ - 1],
	      g_scores[(g_selectedI - 1) * g_len2 + g_selectedJ - 1]);
      selection(buffer, g_gray);
    }

  if (format == 1)
#if HAVE_GD_PNG
    gdImagePng(g_image, g_file)
#endif
      ;
  else if (format == 2)
#if HAVE_GD_GIF
    gdImageGif(g_image, g_file)
#endif
      ;
  else if (format == 3)
#if HAVE_GD_JPEG
    gdImageJpeg(g_image, g_file, -1)
#endif
      ;

  fclose(g_file);

  if (machine)
    {
      /* save configuration */
      buffer = xmalloc(strlen(prefix) + 5);
      strcpy(buffer, prefix);
      strcat(buffer, ".cfg");
      if (!(f = fopen(buffer, "wt")))
	{
	  perror(buffer);
	  return EXIT_FAILURE;
	}
      free(buffer);
      fprintf(f, "%f\n", g_dotSpacing);
      fprintf(f, "%d\n", g_size);
      fclose(f);
    }

  return 0;
}
示例#4
0
/*------------------------------------------------------------*/
void
gifi_finish_output (void)
{
    int row, col, value, r, g, b, error, right, leftbelow, below;
    unsigned char *buffer, *buf;
    int *rcurr, *gcurr, *bcurr, *rnext, *gnext, *bnext, *swap;

    image_render();

    buffer = malloc (output_width * 3 * sizeof (unsigned char));
    rcurr = malloc ((output_width + 1) * sizeof (int));
    gcurr = malloc ((output_width + 1) * sizeof (int));
    bcurr = malloc ((output_width + 1) * sizeof (int));
    rnext = calloc (output_width + 1, sizeof (int));
    gnext = calloc (output_width + 1, sizeof (int));
    bnext = calloc (output_width + 1, sizeof (int));

    for (row = 0; row < output_height; row++) {

        swap = rnext;
        rnext = rcurr;
        rcurr = swap;
        swap = gnext;
        gnext = gcurr;
        gcurr = swap;
        swap = bnext;
        bnext = bcurr;
        bcurr = swap;
        for (col = 0; col < output_width; col++) {
            rnext[col] = 0;
            gnext[col] = 0;
            bnext[col] = 0;
        }

        glReadPixels (0, output_height - row - 1, output_width, 1,
                      GL_RGB, GL_UNSIGNED_BYTE, buffer);
        buf = buffer;
        for (col = 0; col < output_width; col++) {
            rcurr[col] += *buf++;
            gcurr[col] += *buf++;
            bcurr[col] += *buf++;
        }

        for (col = 0; col < output_width; col++) { /* error diffusion */

            value = rcurr[col];
            for (r = 0; 51 * (r + 1) - 26 < value; r++);
            error = value - r * 51;
            right = (7 * error) / 16;
            leftbelow = (3 * error) / 16;
            below = (5 * error) / 16;
            rcurr[col+1] += right;
            if (col > 0) rnext[col-1] += leftbelow;
            rnext[col] += below;
            rnext[col+1] = error - right - leftbelow - below;

            value = gcurr[col];
            for (g = 0; 51 * (g + 1) - 26 < value; g++);
            error = value - g * 51;
            right = (7 * error) / 16;
            leftbelow = (3 * error) / 16;
            below = (5 * error) / 16;
            gcurr[col+1] += right;
            if (col > 0) gnext[col-1] += leftbelow;
            gnext[col] += below;
            gnext[col+1] = error - right - leftbelow - below;

            value = bcurr[col];
            for (b = 0; 51 * (b + 1) - 26 < value; b++);
            error = value - b * 51;
            right = (7 * error) / 16;
            leftbelow = (3 * error) / 16;
            below = (5 * error) / 16;
            bcurr[col+1] += right;
            if (col > 0) bnext[col-1] += leftbelow;
            bnext[col] += below;
            bnext[col+1] = error - right - leftbelow - below;

            gdImageSetPixel (image, col, row, ((r * 6) + g) * 6 + b);
        }
    }

    free (rnext);
    free (gnext);
    free (bnext);
    free (rcurr);
    free (gcurr);
    free (bcurr);
    free (buffer);

    gdImageGif (image, outfile);
    gdImageDestroy (image);

    image_close();
}
示例#5
0
static void gd_end_graph(void)
{
    if (!im || external_surface)
        return;

    /*
     * Windows will do \n -> \r\n  translations on stdout unless told otherwise.
     */
#ifdef HAVE_SETMODE
#ifdef O_BINARY
    setmode(fileno(Output_file), O_BINARY);
#endif
#endif

    /* Only save the alpha channel in outputs that support it if
       the base color was transparent.   Otherwise everything
       was blended so there is no useful alpha info */
    if (im->trueColor) {
        if (is_format_truecolor_capable(Output_lang))
            gdImageSaveAlpha(im, TRUE);
        else
            gdImageTrueColorToPalette(im, 0, 256);
    }
    if (Output_lang == GD) {
        gdImageGd(im, Output_file);
#ifdef HAVE_LIBZ
    } else if (Output_lang == GD2) {
#define GD2_CHUNKSIZE 128
#define GD2_RAW 1
#define GD2_COMPRESSED 2
        gdImageGd2(im, Output_file, GD2_CHUNKSIZE, GD2_COMPRESSED);
#endif
#ifdef HAVE_GD_GIF
    } else if (Output_lang == GIF) {
        gdImageGif(im, Output_file);
#endif
#ifdef HAVE_GD_JPEG
    } else if (Output_lang == JPEG) {
        /*
         * Write IM to OUTFILE as a JFIF-formatted JPEG image, using
         * quality JPEG_QUALITY.  If JPEG_QUALITY is in the range
         * 0-100, increasing values represent higher quality but
         * also larger image size.  If JPEG_QUALITY is negative, the
         * IJG JPEG library's default quality is used (which should
         * be near optimal for many applications).  See the IJG JPEG
         * library documentation for more details.
         */
#define JPEG_QUALITY -1
        gdImageJpeg(im, Output_file, JPEG_QUALITY);
#endif
#ifdef HAVE_GD_PNG
    } else if (Output_lang == PNG) {
        gdImagePng(im, Output_file);
#endif
    } else if (Output_lang == WBMP) {
        /* Use black for the foreground color for the B&W wbmp image. */
        gdImageWBMP(im, black, Output_file);
#ifdef HAVE_GD_XPM
    } else if (Output_lang == XBM) {
        gdImageXbm(im, Output_file);
#endif
    }
    gd_freeusershapes();
    gdImageDestroy(im);
#ifdef MYTRACE
    fprintf(stderr, "gd_end_graph_to_file\n");
#endif
}