File: renderer-window.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (196 lines) | stat: -rw-r--r-- 5,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (c) 1993-1995 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
static const char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/renderer-window.cc,v 1.1.1.1 1998/02/18 18:03:42 ucacsva Exp $ (LBL)";

#include <stdlib.h>
#include "vw.h"
#include "renderer-window.h"
#include "color.h"

WindowRenderer::WindowRenderer(VideoWindow* w, int decimation) :
	BlockRenderer(decimation == 422 ? FT_YUV_422 : FT_YUV_411),
	window_(w),
	image_(0),
	ww_(w->width()),
	wh_(w->height()),
	scale_(0),
	outw_(0),
	outh_(0),
	color_(0),
	decimation_(decimation)
{
}

WindowRenderer::~WindowRenderer()
{
	/*
	 * Clear out the image so that the VideoWindow class does
	 * not try to use it before another window gets attached
	 * and replaces the image.
	 */
	window_->setimage(0);
	delete image_;
}

static inline int
distance(int a, int b)
{
	a -= b;
	return (a >= 0 ? a : -a);
}

void WindowRenderer::compute_scale(int w, int h)
{
	width_ = w;
	height_ = h;
	framesize_ = w * h;

	/*
	 * Choose a good scale factor.  We scale up or down
	 * by factors of two.  Find the closest scale so that
	 * the input geometry matches to the desired window size.
	 * On a mismatch, the image will be centered in the window.
	 */
	scale_ = 0;
	int d = distance(ww_, width_);
	int t = distance(ww_, width_ << 1);
	if (t < d) {
		outw_ = width_ << 1;
		outh_ = height_ << 1;
		scale_ = -1;
	} else {
		/*XXX this should be a loop */
		t = distance(ww_, width_ >> 1);
		if (t < d) {
			d = t;
			scale_ = 1;
		}
		t = distance(ww_, width_ >> 2);
		if (t < d) {
			d = t;
			scale_ = 2;
		}
		t = distance(ww_, width_ >> 3);
		if (t < d)
			scale_ = 3;

		outw_ = width_ >> scale_;
		outh_ = height_ >> scale_;
	}
}

void WindowRenderer::sync() const
{
	window_->complete();
}

void WindowRenderer::push(const u_char*, int miny, int maxy, int minx, int maxx) const
{
	if (scale_ >= 0) {
		miny >>= scale_;
		maxy >>= scale_;
		minx >>= scale_;
		maxx >>= scale_;
	} else {
		miny <<= -scale_;
		maxy <<= -scale_;
		minx <<= -scale_;
		maxx <<= -scale_;
	}
	window_->render(image_, miny, maxy, minx, maxx);
}

/*
 * Tell subclass that we've change decimation/color state
 * so it can reconfigure with different rendering parameters.
 */
void WindowRenderer::doupdate()
{
	/*
	 * Subclasses assume that output width is a multiple of 4.
	 * If not, disable the renderer.  (This is a pathological
	 * case that should only happen with garbage streams.
	 * In theory, it could happen with non-standard video sources
	 * like X screen captures; XXX deal with this when things break.)
	 */
	if (outw_ & 3)
		disable();
	else
		update();
}

void WindowRenderer::resize(int w, int h)
{
	int outw = outw_;
	int outh = outh_;
	compute_scale(w, h);
	/*
	 * Doing a resize can change the output window size
	 * (but it's not likely because this size is mainly
	 * dependent on the size of the window).
	 */
	if (outw != outw_ || outh != outh_) {
		delete image_;
		window_->setimage(0);
		alloc_image();
		/*XXX*/
		window_->damage();
		window_->redraw();
	}
	doupdate();
}

void WindowRenderer::setcolor(int c)
{
	color_ = c;
	doupdate();
}

void WindowRenderer::dither_null(const u_char*, u_int, u_int,
				 u_int, u_int) const
{
}

WindowDitherer::WindowDitherer(VideoWindow* vw, int decimation)
	: WindowRenderer(vw, decimation)
{
}

void WindowDitherer::alloc_image()
{
	StandardVideoImage* p;
	p = StandardVideoImage::allocate(window_->tkwin(), outw_, outh_);
	pixbuf_ = p->pixbuf();
	image_ = p;
}