BRL-CAD
pix2fb.c
Go to the documentation of this file.
1 /* P I X 2 F B . 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/pix2fb.c
22  *
23  * Program to take bottom-up pixel files and send them to a framebuffer.
24  *
25  */
26 
27 #include "common.h"
28 
29 #include <stdlib.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_WINSOCK_H
37 # include <winsock.h>
38 #endif
39 
40 #include "bu/getopt.h"
41 #include "fb.h"
42 #include "fbserv_obj.h"
43 
44 #include "pkg.h"
45 #include "ged.h"
46 
47 
48 static int multiple_lines = 0; /* Streamlined operation */
49 
50 static char *file_name;
51 static int infd;
52 
53 static int fileinput = 0; /* file or pipe on input? */
54 static int autosize = 0; /* !0 to autosize input */
55 
56 static size_t file_width = 512; /* default input width */
57 static size_t file_height = 512;/* default input height */
58 static int scr_width = 0; /* screen tracks file if not given */
59 static int scr_height = 0;
60 static int file_xoff, file_yoff;
61 static int scr_xoff, scr_yoff;
62 static int clear = 0;
63 static int zoom = 0;
64 static int inverse = 0; /* Draw upside-down */
65 static int one_line_only = 0; /* insist on 1-line writes */
66 static int pause_sec = 0; /* Pause that many seconds before
67  closing the FB and exiting */
68 
69 static char usage[] = "\
70 Usage: pix-fb [-a -h -i -c -z -1] [-m #lines] [-F framebuffer]\n\
71  [-s squarefilesize] [-w file_width] [-n file_height]\n\
72  [-x file_xoff] [-y file_yoff] [-X scr_xoff] [-Y scr_yoff]\n\
73  [-S squarescrsize] [-W scr_width] [-N scr_height] [-p seconds]\n\
74  [file.pix]\n";
75 
76 static int
77 get_args(int argc, char **argv)
78 {
79  int c;
80 
81  bu_optind = 1;
82  while ((c = bu_getopt(argc, argv, "1m:aiczF:p:s:w:n:x:y:X:Y:S:W:N:h?")) != -1) {
83  switch (c) {
84  case '1':
85  one_line_only = 1;
86  break;
87  case 'm':
88  multiple_lines = atoi(bu_optarg);
89  break;
90  case 'a':
91  autosize = 1;
92  break;
93  case 'i':
94  inverse = 1;
95  break;
96  case 'c':
97  clear = 1;
98  break;
99  case 'z':
100  zoom = 1;
101  break;
102  case 's':
103  /* square file size */
104  file_height = file_width = atoi(bu_optarg);
105  autosize = 0;
106  break;
107  case 'w':
108  file_width = atoi(bu_optarg);
109  autosize = 0;
110  break;
111  case 'n':
112  file_height = atoi(bu_optarg);
113  autosize = 0;
114  break;
115  case 'x':
116  file_xoff = atoi(bu_optarg);
117  break;
118  case 'y':
119  file_yoff = atoi(bu_optarg);
120  break;
121  case 'X':
122  scr_xoff = atoi(bu_optarg);
123  break;
124  case 'Y':
125  scr_yoff = atoi(bu_optarg);
126  break;
127  case 'S':
128  scr_height = scr_width = atoi(bu_optarg);
129  break;
130  case 'W':
131  scr_width = atoi(bu_optarg);
132  break;
133  case 'N':
134  scr_height = atoi(bu_optarg);
135  break;
136  case 'p':
137  pause_sec=atoi(bu_optarg);
138  break;
139 
140  default: /* 'h' '?' */
141  return 0;
142  }
143  }
144 
145  if (bu_optind >= argc) {
146  if (isatty(fileno(stdin)))
147  return 0;
148  file_name = "-";
149  infd = 0;
150  } else {
151  file_name = argv[bu_optind];
152  if ((infd = open(file_name, 0)) < 0) {
153  perror(file_name);
154  fprintf(stderr,
155  "pix-fb: cannot open \"%s\" for reading\n",
156  file_name);
157  bu_exit(1, NULL);
158  }
159  setmode(infd, O_BINARY);
160  fileinput++;
161  }
162 
163  if (argc > ++bu_optind)
164  fprintf(stderr, "pix-fb: excess argument(s) ignored\n");
165 
166  return 1; /* OK */
167 }
168 
169 
170 int
171 ged_pix2fb(struct ged *gedp, int argc, const char *argv[])
172 {
173  int ret;
174 
178  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
179 
180  /* initialize result */
181  bu_vls_trunc(gedp->ged_result_str, 0);
182 
183  /* must be wanting help */
184  if (argc == 1) {
185  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
186  return GED_HELP;
187  }
188 
189  if (!get_args(argc, (char **)argv)) {
190  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
191  return GED_HELP;
192  }
193 
194  ret = fb_read_fd(gedp->ged_fbsp->fbs_fbp, infd,
195  file_width, file_height,
196  file_xoff, file_yoff,
197  scr_width, scr_height,
198  scr_xoff, scr_yoff,
199  fileinput, file_name, one_line_only, multiple_lines,
200  autosize, inverse, clear, zoom,
201  gedp->ged_result_str);
202 
203  if (infd != 0)
204  close(infd);
205 
206  sleep(pause_sec);
207 
208  if (ret == BRLCAD_OK)
209  return GED_OK;
210 
211  return GED_ERROR;
212 }
213 
214 
215 /*
216  * Local Variables:
217  * mode: C
218  * tab-width: 8
219  * indent-tabs-mode: t
220  * c-file-style: "stroustrup"
221  * End:
222  * ex: shiftwidth=4 tabstop=8
223  */
#define GED_OK
Definition: ged.h:55
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
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 fb_read_fd(fb *ifp, int fd, int file_width, int file_height, int file_xoff, int file_yoff, int scr_width, int scr_height, int scr_xoff, int scr_yoff, int fileinput, char *file_name, int one_line_only, int multiple_lines, int autosize, int inverse, int clear, int zoom, struct bu_vls *result)
Definition: fb_generic.c:686
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
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
#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
HIDDEN int zoom(struct ged *gedp, double sf)
Definition: zoom.c:26
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 ged_pix2fb(struct ged *gedp, int argc, const char *argv[])
Definition: pix2fb.c:171