BRL-CAD
fb2pix.c
Go to the documentation of this file.
1 /* F B 2 P I X . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1986-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  *
20  */
21 /** @file libged/fb2pix.c
22  *
23  * fb2pix writes a framebuffer image to a .pix file.
24  *
25  */
26 
27 #include "common.h"
28 
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 
32 #include "bu/getopt.h"
33 #include "fb.h"
34 #include "fbserv_obj.h"
35 
36 #include "pkg.h"
37 #include "ged.h"
38 
39 
40 char *file_name;
41 FILE *outfp;
42 
43 static int crunch = 0; /* Color map crunch? */
44 static int inverse = 0; /* Draw upside-down */
45 int screen_height; /* input height */
46 int screen_width; /* input width */
47 
48 
49 static int
50 get_args(int argc, char **argv)
51 {
52  int c;
53 
54  bu_optind = 1;
55  while ((c = bu_getopt(argc, argv, "ciF:s:w:n:h?")) != -1) {
56  switch (c) {
57  case 'c':
58  crunch = 1;
59  break;
60  case 'i':
61  inverse = 1;
62  break;
63  case 's':
64  /* square size */
66  break;
67  case 'w':
68  screen_width = atoi(bu_optarg);
69  break;
70  case 'n':
71  screen_height = atoi(bu_optarg);
72  break;
73 
74  default: /* 'h' '?' */
75  return 0;
76  }
77  }
78 
79  if (bu_optind >= argc) {
80  if (isatty(fileno(stdout)))
81  return 0;
82  file_name = "-";
83  outfp = stdout;
84  } else {
85  file_name = argv[bu_optind];
86  if ((outfp = fopen(file_name, "wb")) == NULL) {
87  fprintf(stderr,
88  "fb-pix: cannot open \"%s\" for writing\n",
89  file_name);
90  return 0;
91  }
92  (void)bu_fchmod(fileno(outfp), 0444);
93  }
94 
95  if (argc > ++bu_optind)
96  fprintf(stderr, "fb-pix: excess argument(s) ignored\n");
97 
98  return 1; /* OK */
99 }
100 
101 
102 int
103 ged_fb2pix(struct ged *gedp, int argc, const char *argv[])
104 {
105  int ret;
106  char usage[] = "Usage: fb-pix [-h -i -c] \n\
107  [-s squaresize] [-w width] [-n height] [file.pix]\n";
108 
112  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
113 
114  /* initialize result */
115  bu_vls_trunc(gedp->ged_result_str, 0);
116 
117  /* must be wanting help */
118  if (argc == 1) {
119  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
120  return GED_HELP;
121  }
122 
123  screen_height = screen_width = 512; /* Defaults */
124 
125  if (!get_args(argc, (char **)argv)) {
126  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
127  return GED_HELP;
128  }
129 
130  setmode(fileno(stdout), O_BINARY);
131 
132  ret = fb_write_fp(gedp->ged_fbsp->fbs_fbp, outfp,
134  crunch, inverse, gedp->ged_result_str);
135 
136  if (outfp != stdout)
137  fclose(outfp);
138 
139  if (ret == BRLCAD_OK)
140  return GED_OK;
141 
142  return GED_ERROR;
143 }
144 
145 
146 /*
147  * Local Variables:
148  * mode: C
149  * tab-width: 8
150  * indent-tabs-mode: t
151  * c-file-style: "stroustrup"
152  * End:
153  * ex: shiftwidth=4 tabstop=8
154  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int fb_write_fp(fb *ifp, FILE *fp, int req_width, int req_height, int crunch, int inverse, struct bu_vls *result)
Definition: fb_generic.c:601
int bu_fchmod(int fd, unsigned long pmode)
Definition: fchmod.c:131
Definition: ged.h:338
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
FILE * outfp
Definition: fb2pix.c:41
struct fbserv_obj * ged_fbsp
Definition: ged.h:362
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
#define GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define GED_CHECK_FBSERV(_gedp, _flags)
Definition: ged.h:150
#define BRLCAD_OK
Definition: defines.h:71
int get_args(int argc, char **argv, FILE **ifp, FILE **ofp, double *angle)
Definition: rot.c:67
struct bu_vls * ged_result_str
Definition: ged.h:357
#define GED_CHECK_FBSERV_FBP(_gedp, _flags)
Definition: ged.h:160
int screen_height
Definition: fb2pix.c:45
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
char * file_name
Definition: fb2pix.c:40
int screen_width
Definition: fb2pix.c:46
int ged_fb2pix(struct ged *gedp, int argc, const char *argv[])
Definition: fb2pix.c:103