mightymandel v16

GPU-based Mandelbrot set explorer

parse_ppm.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include <assert.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <mpfr.h>
11 
12 #include "parse.h"
13 #include "parse_ppm.h"
14 
15 bool parse_ppm(const char *source, int length, mpfr_t cx, mpfr_t cy, mpfr_t cz) {
16  (void) length;
17  assert(source);
18  char *source2 = strdup(source);
19  assert(source2);
20  char *s = source2;
21  char *sx = 0;
22  char *sy = 0;
23  char *sz = 0;
24  char *line = parse_line(&s);
25  if (0 == strcmp("P6", line)) {
26  line = parse_line(&s);
27  int len = strlen(line);
28  if (len > 20) {
29  // the total length of a line is surely long enough for each of its
30  // shorter substrings
31  sx = (char *) malloc(len);
32  sy = (char *) malloc(len);
33  sz = (char *) malloc(len);
34  if (3 == sscanf(line, "# mightymandel %s + %s i @ %s", sx, sy, sz)) {
35  mpfr_set_prec(cz, 53);
36  mpfr_set_str(cz, sz, 10, MPFR_RNDN);
37  if (! radius_is_valid(cz)) {
38  free(sx);
39  free(sy);
40  free(sz);
41  free(source2);
42  return false;
43  }
44  mpfr_prec_t p = precision_for_radius(cz);
45  mpfr_set_prec(cx, p);
46  mpfr_set_prec(cy, p);
47  mpfr_set_str(cx, sx, 10, MPFR_RNDN);
48  mpfr_set_str(cy, sy, 10, MPFR_RNDN);
49  free(sx);
50  free(sy);
51  free(sz);
52  free(source2);
53  return true;
54  } else {
55  free(sx);
56  free(sy);
57  free(sz);
58  free(source2);
59  return false;
60  }
61  } else {
62  free(source2);
63  return false;
64  }
65  } else {
66  free(source2);
67  return false;
68  }
69 }