BRL-CAD
fb_rect.c
Go to the documentation of this file.
1 /* F B _ R E C T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1997-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library 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 library 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 /** @addtogroup fb */
21 /** @{ */
22 /** @file fb_rect.c
23  *
24  * Subroutines to simulate the fb_readrect() and fb_writerect()
25  * capabilities for displays that do not presently handle it.
26  *
27  */
28 /** @} */
29 
30 #include "common.h"
31 
32 #include <stdio.h>
33 
34 #include "bu/color.h"
35 #include "bu/log.h"
36 #include "fb_private.h"
37 #include "fb.h"
38 
39 
40 /*
41  * A routine to simulate the effect of fb_readrect() when a
42  * particular display does not handle it.
43  */
44 int
45 fb_sim_readrect(fb *ifp, int xmin, int ymin, int width, int height, unsigned char *pp)
46 {
47  register int y;
48  register int tot;
49  int got;
50 
51  tot = 0;
52  for (y=ymin; y < ymin+height; y++) {
53  got = fb_read(ifp, xmin, y, pp, (size_t)width);
54  if (got < 0) {
55  fb_log("fb_sim_readrect() y=%d unexpected EOF\n", y);
56  break;
57  }
58  tot += got;
59  if (got != width) {
60  fb_log("fb_sim_readrect() y=%d, read of %d got %d pixels, aborting\n",
61  y, width, got);
62  break;
63  }
64  pp += width * sizeof(RGBpixel);
65  }
66  return tot;
67 }
68 
69 
70 /*
71  * A routine to simulate the effect of fb_writerect() when a
72  * particular display does not handle it.
73  *
74  * Returns number of pixels actually written.
75  * Clipping to the screen may reduce the total if caller was sloppy.
76  */
77 int
78 fb_sim_writerect(fb *ifp, int xmin, int ymin, int width, int height, const unsigned char *pp)
79 {
80  register int y;
81  register int tot;
82  int got;
83  size_t xlen;
84 
85  xlen = width;
86  if (xmin + width > fb_getwidth(ifp))
87  xlen = fb_getwidth(ifp) - xmin;
88 
89  tot = 0;
90  for (y=ymin; y < ymin+height; y++) {
91  got = fb_write(ifp, xmin, y, pp, xlen);
92  tot += got;
93  if (got != (int)xlen) break;
94  pp += width * sizeof(RGBpixel);
95  }
96  return tot;
97 }
98 
99 
100 #define SIMBUF_SIZE (24*1024)
101 int
102 fb_sim_bwreadrect(fb *ifp, int xmin, int ymin, int width, int height, unsigned char *pp)
103 {
104  register int y;
105  register int tot;
106  int got;
107  unsigned char buf[SIMBUF_SIZE*3];
108 
109  if (width > SIMBUF_SIZE) {
110  fb_log("fb_sim_bwreadrect() width of %d exceeds internal buffer, aborting\n", width);
111  return -SIMBUF_SIZE; /* FAIL */
112  }
113 
114  tot = 0;
115  for (y=ymin; y < ymin+height; y++) {
116  register int x;
117 
118  got = fb_read(ifp, xmin, y, buf, (size_t)width);
119 
120  /* Extract green chan */
121  for (x=0; x < width; x++)
122  *pp++ = buf[x*3+GRN];
123 
124  tot += got;
125  if (got != width) break;
126  }
127  return tot;
128 }
129 
130 
131 int
132 fb_sim_bwwriterect(fb *ifp, int xmin, int ymin, int width, int height, const unsigned char *pp)
133 {
134  register int y;
135  register int tot;
136  int got;
137  size_t xlen;
138  unsigned char buf[SIMBUF_SIZE];
139 
140  if (width > SIMBUF_SIZE) {
141  fb_log("fb_sim_bwwriterect() width of %d exceeds internal buffer, aborting\n", width);
142  return -SIMBUF_SIZE; /* FAIL */
143  }
144 
145  xlen = width;
146  if (xmin + width > fb_getwidth(ifp))
147  xlen = fb_getwidth(ifp) - xmin;
148 
149  tot = 0;
150  for (y=ymin; y < ymin+height; y++) {
151  register int x;
152  register unsigned char *bp;
153 
154  /* Copy monochrome (b&w) intensity into all three chans */
155  bp = buf;
156  for (x=0; x < width; x++) {
157  register unsigned char c = *pp++;
158  bp[0] = c;
159  bp[1] = c;
160  bp[2] = c;
161  bp += 3;
162  }
163 
164  got = fb_write(ifp, xmin, y, buf, xlen);
165  tot += got;
166  if (got != (int)xlen) break;
167  }
168  return tot;
169 }
170 
171 
172 /*
173  * Local Variables:
174  * mode: C
175  * tab-width: 8
176  * indent-tabs-mode: t
177  * c-file-style: "stroustrup"
178  * End:
179  * ex: shiftwidth=4 tabstop=8
180  */
unsigned char RGBpixel[3]
Definition: fb.h:73
void fb_log(const char *fmt,...) _BU_ATTR_PRINTF12
Definition: fb_log.c:42
Header file for the BRL-CAD common definitions.
int fb_sim_writerect(fb *ifp, int xmin, int ymin, int width, int height, const unsigned char *pp)
Definition: fb_rect.c:78
ustring width
unsigned char * bp
Definition: rot.c:56
int fb_getwidth(fb *ifp)
Definition: fb_generic.c:231
ssize_t fb_write(fb *ifp, int x, int y, const unsigned char *pp, size_t count)
Definition: fb_generic.c:276
int fb_sim_bwreadrect(fb *ifp, int xmin, int ymin, int width, int height, unsigned char *pp)
Definition: fb_rect.c:102
int fb_sim_readrect(fb *ifp, int xmin, int ymin, int width, int height, unsigned char *pp)
Definition: fb_rect.c:45
int fb_sim_bwwriterect(fb *ifp, int xmin, int ymin, int width, int height, const unsigned char *pp)
Definition: fb_rect.c:132
A frame-buffer IO structure.
Definition: fb_private.h:80
#define GRN
Definition: color.h:40
#define SIMBUF_SIZE
Definition: fb_rect.c:100
ssize_t fb_read(fb *ifp, int x, int y, unsigned char *pp, size_t count)
Definition: fb_generic.c:272