summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/blurscope/scopedisplayer.cpp
blob: 6b107327c994488d72c95afcf77e2db47314d558 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*****************************************************************

Copyright (c) 2001 Charles Samuels <[email protected]>
              2001 Neil Stevens <[email protected]>
              With code stolen from Paul Harrison <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIAB\ILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#include "sdlscope.h"

#if 0
#define TEST() cout << "At line "  << __LINE__ << endl
#else
#define TEST() if(0)
#endif

#define output ((unsigned char*)data)	
template <class Type>
inline void Bitmap<Type>::addPixel(int x, int y, int br1, int br2)
{
	if (x < 0 || x >= width || y < 0 || y >= height) return;

	register unsigned char *p = output+x*2+y*width*2;
	if (p[0] < 255-br1) p[0] += br1; else p[0] = 255;
	if (p[1] < 255-br2) p[1] += br2; else p[1] = 255;
}

template <class Type>
void Bitmap<Type>::addVertLine(int x, int y1, int y2, int br1, int br2)
{
	if(y1 < y2)
	{
		for(int y = y1; y <= y2; y++)
			addPixel(x,y, br1, br2);
	}
	else if(y2 < y1)
	{
		for(int y = y2; y <= y1; y++)
			addPixel(x, y, br1, br2);
	}
	else
	{
		addPixel(x, y1, br1, br2);
	}
}

template <class Type>
void Bitmap<Type>::fadeStar()
{
	register unsigned long *ptr = (unsigned long*)output;
	int i = width*height*2/4;
	do
	{
		if (*ptr)
			*ptr -= ((*ptr & 0xf0f0f0f0ul) >> 4) + ((*ptr & 0xe0e0e0e0ul) >> 5);
		ptr++;
	} while(--i > 0);
}

SDLView::SDLView(int in) : mFd(in), outputBmp(0), fullscreen(false)
{
	surface = 0;
	width = 320;
	height = width*6/8;
	outputBmp.size(width, height);
	TEST();
	
	TEST();
	fcntl(mFd, F_SETFL, fcntl(mFd, F_GETFL) & ~O_NONBLOCK);
	TEST();


	float *data = new float[width];
	TEST();

	startVideo();
	setupPalette();
	TEST();

	while(true)
	{
		checkInput();

		if(!surface) exit(0);

		int bytestoread = width * sizeof(float);
		int pos = 0;
		while(pos < bytestoread)
		{
			char *d = (char *)data;
			int r = read(mFd, d + pos, bytestoread - pos);
			if(r > 0) pos += r;
			else if (r==0) exit(0);
		}

		outputBmp.fadeStar();
		outputBmp.fadeStar();

		float *d = data;
		float *end = data + width;
		float heightHalf = height / 4.0;
		int y = height / 2;
		int x=0;
		int oldy=(int)(*d * heightHalf);
		while(d <= end)
		{
			int amp = (int)(*d * heightHalf);
			amp+=y;
			
			outputBmp.addVertLine(x, oldy, amp, 255, 255);
			oldy=amp;
			
			d++;
			x++;
		}
		tqrepaint();
	}
}

static SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
{
	SDL_Surface *screen;

	// Set the video mode
	screen = SDL_SetVideoMode(w, h, bpp, flags);
	return screen;
}

void SDLView::startVideo()
{
	if(surface)
	{
		SDL_FreeSurface(surface);
		surface = 0;
	}

	Uint32 videoflags;

	/* Initialize SDL */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
		exit(0);

	SDL_WM_SetCaption("BlurScope","blurscope");

	/* See if we try to get a hardware colormap */
	videoflags = SDL_SWSURFACE | (fullscreen?SDL_FULLSCREEN:0);

	surface = CreateScreen(width, height, 8, videoflags);
	if (!surface)
		exit(0);
	
	SDL_ShowCursor(0);

	if(!surface)
		SDL_Quit();

	SDL_WM_SetCaption("BlurScope", 0);
	SDL_ShowCursor(0);

}

void SDLView::setupPalette(double)
{
#define BOUND(x) ((x) > 255 ? 255 : (x))
#define PEAKIFY(x) int(BOUND((x) - (x)*(255-(x))/255/2))
#define MAX(x,y) ((x) > (y) ? (x) : (y))

	int redMax=136;
	int greenMax=136;
	int blueMax=255;
	
	SDL_Color sdlPalette[256];

	for(int i=0;i<256;i++)
	{
		// i
		// 255 136
		sdlPalette[i].r = i*redMax/255;
		sdlPalette[i].g = i*greenMax/255;
		sdlPalette[i].b = i*blueMax/255;
	}

	SDL_SetColors(surface, sdlPalette, 0, 256);

#undef BOUND
#undef PEAKIFY
#undef MAX
}

void SDLView::checkInput()
{
	SDL_Event myEvent;

	while(SDL_PollEvent(&myEvent))
	{
		switch(myEvent.type)
		{
		case SDL_KEYDOWN:
			switch(myEvent.key.keysym.sym)
			{
			case SDLK_SPACE:
//				fullscreen ^= true;
//				startVideo();
				break;
			default:
				break;
			}
			break;

		case SDL_QUIT:
			exit(0);
			break;
		}
	}
}

#define output2 ((unsigned char*)outputBmp.data)

void SDLView::tqrepaint()
{
	SDL_LockSurface(surface);
	TEST();

	register unsigned long *ptr2 = (unsigned long*)output2;
	unsigned long *ptr1 = (unsigned long*)( surface->pixels );
	int i = width*height/4;
	TEST();

	do {
		// Asger Alstrup Nielsen's ([email protected])
		// optimized 32 bit screen loop
		register unsigned int const r1 = *(ptr2++);
		register unsigned int const r2 = *(ptr2++);

		//if (r1 || r2) {
#ifdef LITTLEENDIAN
		register unsigned int const v =
			((r1 & 0x000000f0ul) >> 4)
			| ((r1 & 0x0000f000ul) >> 8)
			| ((r1 & 0x00f00000ul) >> 12)
			| ((r1 & 0xf0000000ul) >> 16);
		*(ptr1++) = v |
			( ((r2 & 0x000000f0ul) << 12)
			  | ((r2 & 0x0000f000ul) << 8)
			  | ((r2 & 0x00f00000ul) << 4)
			  | ((r2 & 0xf0000000ul)));
#else
		register unsigned int const v =
			((r2 & 0x000000f0ul) >> 4)
			| ((r2 & 0x0000f000ul) >> 8)
			| ((r2 & 0x00f00000ul) >> 12)
			| ((r2 & 0xf0000000ul) >> 16);
		*(ptr1++) = v |
			( ((r1 & 0x000000f0ul) << 12)
			  | ((r1 & 0x0000f000ul) << 8)
			  | ((r1 & 0x00f00000ul) << 4)
			  | ((r1 & 0xf0000000ul)));
#endif
		//} else ptr1++;
	} while (--i);
	TEST();

	SDL_UnlockSurface(surface);
	SDL_UpdateRect(surface, 0, 0, 0, 0);
	TEST();

}

SDLView::~SDLView()
{
	::close(mFd);
}