| 1 |
#include <stdbool.h> |
| 2 |
#include <stdint.h> |
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "netpbm/mallocvar.h" |
| 6 |
#include "netpbm/shhopt.h" |
| 7 |
#include "netpbm/pam.h" |
| 8 |
|
| 9 |
#include "limits_pamtris.h" |
| 10 |
#include "framebuffer.h" |
| 11 |
#include "boundaries.h" |
| 12 |
#include "input.h" |
| 13 |
|
| 14 |
#define MAX_METRICS 8192 |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
static int |
| 19 |
parse_command_line(int * const argc_ptr, |
| 20 |
const char ** const argv, |
| 21 |
int32_t * const width, |
| 22 |
int32_t * const height, |
| 23 |
int32_t * const maxval, |
| 24 |
int32_t * const num_attribs, |
| 25 |
char * const tupletype) { |
| 26 |
|
| 27 |
optEntry * option_def; |
| 28 |
optStruct3 opt; |
| 29 |
/* Instructions to pm_optParseOptions3 on how to parse our options */ |
| 30 |
unsigned int option_def_index; |
| 31 |
|
| 32 |
char * tupletype_tmp; |
| 33 |
|
| 34 |
unsigned int width_spec, height_spec, attribs_spec, tupletype_spec; |
| 35 |
unsigned int rgb_spec, grayscale_spec, maxval_spec; |
| 36 |
|
| 37 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 38 |
|
| 39 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 40 |
OPTENT3(0, "width", OPT_INT, width, &width_spec, 0); |
| 41 |
OPTENT3(0, "height", OPT_INT, height, &height_spec, 0); |
| 42 |
OPTENT3(0, "num_attribs", OPT_INT, num_attribs, &attribs_spec, 0); |
| 43 |
OPTENT3(0, "tupletype", OPT_STRING, &tupletype_tmp, &tupletype_spec, 0); |
| 44 |
OPTENT3(0, "rgb", OPT_FLAG, NULL, &rgb_spec, 0); |
| 45 |
OPTENT3(0, "grayscale", OPT_FLAG, NULL, &grayscale_spec, 0); |
| 46 |
OPTENT3(0, "maxval", OPT_INT, maxval, &maxval_spec, 0); |
| 47 |
|
| 48 |
opt.opt_table = option_def; |
| 49 |
opt.short_allowed = false; |
| 50 |
opt.allowNegNum = false; |
| 51 |
|
| 52 |
pm_optParseOptions3(argc_ptr, (char **)argv, opt, sizeof(opt), 0); |
| 53 |
|
| 54 |
if (!width_spec || !height_spec || (!attribs_spec && !(rgb_spec || grayscale_spec))) { |
| 55 |
pm_errormsg( |
| 56 |
"you must at least specify -width, -height and " |
| 57 |
"either -num_attribs, -rgb or -grayscale."); |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
if (rgb_spec + grayscale_spec + attribs_spec != 1) { |
| 63 |
pm_errormsg("you must provide either only -num_attribs, " |
| 64 |
"-rgb or -grayscale; not a combination of those."); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
if (*width < 1 || *width > MAX_METRICS) { |
| 70 |
pm_errormsg("invalid width."); |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
if (*height < 1 || *height > MAX_METRICS) { |
| 76 |
pm_errormsg("invalid height."); |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
if (maxval_spec) { |
| 82 |
if (*maxval < 1 || *maxval > PAM_OVERALL_MAXVAL) { |
| 83 |
pm_errormsg("invalid maxval."); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
} else { |
| 88 |
*maxval = 255; |
| 89 |
} |
| 90 |
|
| 91 |
if (rgb_spec) { |
| 92 |
*num_attribs = 3; |
| 93 |
set_tupletype("RGB_ALPHA", tupletype); |
| 94 |
} |
| 95 |
|
| 96 |
if (grayscale_spec) { |
| 97 |
*num_attribs = 1; |
| 98 |
set_tupletype("GRAYSCALE_ALPHA", tupletype); |
| 99 |
} |
| 100 |
|
| 101 |
if (*num_attribs < 1 || *num_attribs > MAX_NUM_ATTRIBS) { |
| 102 |
pm_errormsg("invalid number of generic attributes per vertex."); |
| 103 |
|
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
if (tupletype_spec) { |
| 108 |
if(rgb_spec || grayscale_spec) { |
| 109 |
pm_errormsg("you may not provide -tupletype together with " |
| 110 |
"-rgb or -grayscale."); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
if (!set_tupletype(tupletype_tmp, tupletype)) { |
| 116 |
pm_errormsg("warning: invalid tuple type; using empty string."); |
| 117 |
|
| 118 |
set_tupletype(NULL, tupletype); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
free(option_def); |
| 123 |
|
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
int |
| 130 |
main(int argc, const char ** argv) { |
| 131 |
|
| 132 |
framebuffer_info fbi; |
| 133 |
boundary_info bi; |
| 134 |
Input input; |
| 135 |
bool no_more_commands; |
| 136 |
|
| 137 |
pm_proginit(&argc, (const char**)argv); |
| 138 |
|
| 139 |
set_tupletype(NULL, fbi.outpam.tuple_type); |
| 140 |
|
| 141 |
if (!parse_command_line(&argc, |
| 142 |
argv, |
| 143 |
&fbi.width, |
| 144 |
&fbi.height, |
| 145 |
&fbi.maxval, |
| 146 |
&fbi.num_attribs, |
| 147 |
fbi.outpam.tuple_type)) { |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
if (!init_framebuffer(&fbi)) { |
| 152 |
pm_errormsg("out of memory."); |
| 153 |
|
| 154 |
return 3; |
| 155 |
} |
| 156 |
|
| 157 |
init_boundary_buffer(&bi, fbi.height); |
| 158 |
|
| 159 |
input_init(&input); |
| 160 |
|
| 161 |
for (no_more_commands = false; !no_more_commands; ) |
| 162 |
input_process_next_command(&input, &bi, &fbi, &no_more_commands); |
| 163 |
|
| 164 |
input_term(&input); |
| 165 |
free_boundary_buffer(&bi); |
| 166 |
free_framebuffer(&fbi); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
|