ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-5.01.tar.gz
[xscreensaver] / hacks / bumps.c
1 /* -*- mode: C; tab-width: 4 -*-
2  * Bumps, Copyright (c) 2002, 2006 Shane Smit <CodeWeaver@DigitalLoom.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or
10  * implied warranty.
11  *
12  * Module: "bumps.c"
13  * Tab Size: 4
14  *
15  * Description:
16  *  This is typical bump-mapping.  The actual bump map is generated by a screen
17  *  grab.  The light source is represented by a spotlight of random color. This
18  *  spotlight randomly traverses the bump map in a sinus pattern.
19  *
20  *  Essentially, it 3D-izes your desktop, based on color intensity.
21  *
22  * Modification History:
23  *  [10/01/99] - Shane Smit: Creation
24  *  [10/08/99] - Shane Smit: Port to C. (Ick)
25  *  [03/08/02] - Shane Smit: New movement code.
26  *  [09/12/02] - Shane Smit: MIT-SHM XImages.
27  *                                                       Thanks to Kennett Galbraith <http://www.Alpha-II.com/>
28  *                                                       for code optimization.
29  */
30
31 #include "bumps.h"
32
33 static void SetPalette(Display *, SBumps *, XWindowAttributes * );
34 static void InitBumpMap(Display *, SBumps *, XWindowAttributes * );
35 static void InitBumpMap_2(Display *, SBumps *);
36 static void SoftenBumpMap( SBumps * );
37
38
39
40
41 /* This function pointer will point to the appropriate PutPixel*() function below. */
42 static void (*MyPutPixel)( int8_ *, uint32_ );
43
44 static void PutPixel32( int8_ *pData, uint32_ pixel )
45 {
46         *(uint32_ *)pData = pixel;
47 }
48
49 static void PutPixel24( int8_ *pData, uint32_ pixel )
50 {
51         pData[ 2 ] = ( pixel & 0x00FF0000 ) >> 16;
52         pData[ 1 ] = ( pixel & 0x0000FF00 ) >> 8;
53         pData[ 0 ] = ( pixel & 0x000000FF );
54 }
55
56 static void PutPixel16( int8_ *pData, uint32_ pixel )
57 {
58         *(uint16_ *)pData = (uint16_)pixel;
59 }
60
61 static void PutPixel8( int8_ *pData, uint32_ pixel )
62 {
63         *(uint8_ *)pData = (uint8_)pixel;
64 }
65
66 /* Creates the light map, which is a circular image... going from black around the edges
67  * to white in the center. */
68 static void CreateSpotLight( SSpotLight *pSpotLight, uint16_ iDiameter, uint16_ nColorCount )
69 {
70         double nDist;
71         int16_ iDistX, iDistY;
72         uint8_ *pLOffset;
73         
74         pSpotLight->nFalloffDiameter = iDiameter;
75         pSpotLight->nFalloffRadius = pSpotLight->nFalloffDiameter / 2;
76         pSpotLight->nLightDiameter = iDiameter / 2;
77         pSpotLight->nLightRadius = pSpotLight->nLightDiameter / 2;
78 #ifdef VERBOSE
79         printf( "%s: Falloff Diameter: %d\n", progclass, pSpotLight->nFalloffDiameter );
80         printf( "%s: Spot Light Diameter: %d\n", progclass, pSpotLight->nLightDiameter );
81 #endif
82
83         pSpotLight->aLightMap = malloc( pSpotLight->nLightDiameter * pSpotLight->nLightDiameter * sizeof(uint8_) );
84
85         pLOffset = pSpotLight->aLightMap;
86         for( iDistY=-pSpotLight->nLightRadius; iDistY<pSpotLight->nLightRadius; ++iDistY )
87         {
88                 for( iDistX=-pSpotLight->nLightRadius; iDistX<pSpotLight->nLightRadius; ++iDistX )
89                 {
90                         nDist = sqrt( pow( iDistX+0.5F, 2 ) + pow( iDistY+0.5F, 2 ) );
91                         if( nDist / pSpotLight->nLightRadius <= 1.0f )
92                                 *pLOffset = (uint8_)(nColorCount - ( ( nDist / pSpotLight->nLightRadius ) * ( nColorCount - 1 ) ));
93                         else
94                                 *pLOffset = 0;
95
96                         ++pLOffset;
97                 }
98         }
99                 
100         /* Initialize movement variables.       */
101         pSpotLight->nAccelX = 0;
102         pSpotLight->nAccelY = 0;
103         pSpotLight->nVelocityX = ( RANDOM() % 2 ) ? pSpotLight->nVelocityMax : -pSpotLight->nVelocityMax;
104         pSpotLight->nVelocityY = ( RANDOM() % 2 ) ? pSpotLight->nVelocityMax : -pSpotLight->nVelocityMax;
105 }
106
107
108 /* Calculates the position of the spot light on the screen. */
109 static void CalcLightPos( SBumps *pBumps )
110 {
111         SSpotLight *pSpotLight = &pBumps->SpotLight;
112         float nGravity;
113
114         /* X */
115         if( pSpotLight->nXPos < pSpotLight->nFalloffRadius )                                                    nGravity = 1.0f;
116         else if( pSpotLight->nXPos > pBumps->iWinWidth - pSpotLight->nFalloffRadius )   nGravity = -1.0f;
117         else                                                                                                                                                    nGravity = ( ( RANDOM() % 201 ) / 100.0f ) - 1.0f;
118                 
119         pSpotLight->nAccelX += nGravity * ( pSpotLight->nAccelMax / 5.0f );
120         if( pSpotLight->nAccelX < -pSpotLight->nAccelMax )              pSpotLight->nAccelX = -pSpotLight->nAccelMax;
121         else if( pSpotLight->nAccelX > pSpotLight->nAccelMax )  pSpotLight->nAccelX = pSpotLight->nAccelMax;
122
123         pSpotLight->nVelocityX += pSpotLight->nAccelX;
124         if( pSpotLight->nVelocityX < -pSpotLight->nVelocityMax )                pSpotLight->nVelocityX = -pSpotLight->nVelocityMax;
125         else if( pSpotLight->nVelocityX > pSpotLight->nVelocityMax )    pSpotLight->nVelocityX = pSpotLight->nVelocityMax;
126
127         pSpotLight->nXPos += pSpotLight->nVelocityX;
128
129         /* Y */
130         if( pSpotLight->nYPos < pSpotLight->nFalloffRadius )                                                            nGravity = 1.0f;
131         else if( pSpotLight->nYPos > pBumps->iWinHeight - pSpotLight->nFalloffRadius )  nGravity = -1.0f;
132         else                                                                                                                                                    nGravity = ( ( RANDOM() % 201 ) / 100.0f ) - 1.0f;
133                 
134         pSpotLight->nAccelY += nGravity * ( pSpotLight->nAccelMax / 5.0f );
135         if( pSpotLight->nAccelY < -pSpotLight->nAccelMax )              pSpotLight->nAccelY = -pSpotLight->nAccelMax;
136         else if( pSpotLight->nAccelY > pSpotLight->nAccelMax )  pSpotLight->nAccelY = pSpotLight->nAccelMax;
137
138         pSpotLight->nVelocityY += pSpotLight->nAccelY;
139         if( pSpotLight->nVelocityY < -pSpotLight->nVelocityMax )                pSpotLight->nVelocityY = -pSpotLight->nVelocityMax;
140         else if( pSpotLight->nVelocityY > pSpotLight->nVelocityMax )    pSpotLight->nVelocityY = pSpotLight->nVelocityMax;
141
142         pSpotLight->nYPos += pSpotLight->nVelocityY;
143 }
144
145
146 /* Main initialization function. */
147 static void CreateBumps( SBumps *pBumps, Display *dpy, Window NewWin )
148 {
149         XWindowAttributes XWinAttribs;
150         XGCValues GCValues;
151         int32_ nGCFlags;
152         uint16_ iDiameter;
153
154         /* Make size and velocity a function of window size, so it appears the same at 100x60 as it does in 3200x1200. */
155         XGetWindowAttributes( dpy, NewWin, &XWinAttribs );
156         pBumps->iWinWidth = XWinAttribs.width;
157         pBumps->iWinHeight = XWinAttribs.height;
158         pBumps->SpotLight.nXPos = XWinAttribs.width / 2.0f;
159         pBumps->SpotLight.nYPos = XWinAttribs.height / 2.0f;
160         pBumps->SpotLight.nVelocityMax = ( ( XWinAttribs.width < XWinAttribs.height ) ? XWinAttribs.width : XWinAttribs.height ) / 140.0f;
161         pBumps->SpotLight.nAccelMax = pBumps->SpotLight.nVelocityMax / 10.0f;
162         pBumps->dpy = dpy;
163         pBumps->Win = NewWin;
164         pBumps->pXImage = NULL;
165         
166         iDiameter = ( ( pBumps->iWinWidth < pBumps->iWinHeight ) ? pBumps->iWinWidth : pBumps->iWinHeight ) / 2;
167
168     /* jwz: sometimes we get tearing if this lands on the wrong bounaary;
169        constraining it to be a multiple of 8 seems to fix it. */
170     iDiameter = ((iDiameter+7)/8)*8;
171
172 #ifdef HAVE_XSHM_EXTENSION
173         pBumps->bUseShm = get_boolean_resource(dpy,  "useSHM", "Boolean" );
174
175         if( pBumps->bUseShm )
176         {
177                 pBumps->pXImage = create_xshm_image( pBumps->dpy, XWinAttribs.visual, XWinAttribs.depth,
178                                                                                          ZPixmap, NULL, &pBumps->XShmInfo, iDiameter, iDiameter );
179                 if( !pBumps->pXImage )
180                 {
181                         fprintf( stderr, "%s: Unable to create XShmImage.\n", progname );
182                         pBumps->bUseShm = False;
183                 }
184         }
185 #endif /* HAVE_XSHM_EXTENSION */
186         if( !pBumps->pXImage )
187         {
188                 pBumps->pXImage = XCreateImage( pBumps->dpy, XWinAttribs.visual, XWinAttribs.depth, 
189                                                                         ZPixmap, 0, NULL, iDiameter, iDiameter, BitmapPad( pBumps->dpy ), 0 );
190                 pBumps->pXImage->data = malloc( pBumps->pXImage->bytes_per_line * pBumps->pXImage->height * sizeof(int8_) );
191         }
192
193         /* For speed, access the XImage data directly using my own PutPixel routine. */
194         switch( pBumps->pXImage->bits_per_pixel )
195         {
196                 case 32:
197                         pBumps->bytesPerPixel = 4;
198                         MyPutPixel = PutPixel32;
199                         break;
200                 
201                 case 24:
202                         pBumps->bytesPerPixel = 3;
203                         MyPutPixel = PutPixel24;
204                         break;
205
206                 case 16:
207                         pBumps->bytesPerPixel = 2;
208                         MyPutPixel = PutPixel16;
209                         break;
210
211                 case 8:
212                         pBumps->bytesPerPixel = 1;
213                         MyPutPixel = PutPixel8;
214                         break;
215
216                 default:
217                         fprintf( stderr, "%s: Unknown XImage depth.", progname );
218 #ifdef HAVE_XSHM_EXTENSION
219                         if( pBumps->bUseShm )
220                                 destroy_xshm_image( pBumps->dpy, pBumps->pXImage, &pBumps->XShmInfo );
221                         else
222 #endif /* HAVE_XSHM_EXTENSION */
223                                 XDestroyImage( pBumps->pXImage );
224                         exit( 1 );
225         }
226         
227         GCValues.function = GXcopy;
228         GCValues.subwindow_mode = IncludeInferiors;
229         nGCFlags = GCFunction;
230         if( use_subwindow_mode_p( XWinAttribs.screen, pBumps->Win ) ) /* See grabscreen.c */
231                 nGCFlags |= GCSubwindowMode;
232         pBumps->GraphicsContext = XCreateGC( pBumps->dpy, pBumps->Win, nGCFlags, &GCValues );
233         
234         SetPalette(dpy, pBumps, &XWinAttribs );
235         CreateSpotLight( &pBumps->SpotLight, iDiameter, pBumps->nColorCount );
236         InitBumpMap(dpy, pBumps, &XWinAttribs );
237 }
238
239
240 /* Creates a specialized phong shade palette. */
241 static void SetPalette(Display *dpy, SBumps *pBumps, XWindowAttributes *pXWinAttribs )
242 {
243         XColor BaseColor;
244         XColor Color;
245         char *sColor;                   /* Spotlight Color */
246         int16_ iColor;
247         
248         sColor = get_string_resource(dpy,  "color", "Color" );
249
250         BaseColor.red = RANDOM() % 0xFFFF; 
251         BaseColor.green = RANDOM() % 0xFFFF;
252         BaseColor.blue = RANDOM() % 0xFFFF;
253         
254         /* Make one color full intesity to avoid dark spotlights.       */
255         switch( RANDOM() % 3 )
256         {
257                 case 0: BaseColor.red   = 0xFFFF;       break;
258                 case 1: BaseColor.green = 0xFFFF;       break;
259                 case 2: BaseColor.blue  = 0xFFFF;       break;
260         }
261
262         if( strcasecmp( sColor, "random" ) && !XParseColor( pBumps->dpy, pXWinAttribs->colormap, sColor, &BaseColor ) )
263                 fprintf( stderr, "%s: color %s not found in database. Choosing random...\n", progname, sColor );
264
265 #ifdef VERBOSE
266         printf( "%s: Spotlight color is <%d,%d,%d> RGB.\n", progclass, BaseColor.red, BaseColor.green, BaseColor.blue );
267 #endif  /*  VERBOSE */
268
269         pBumps->nColorCount = get_integer_resource(dpy,  "colorcount", "Integer" );
270         if( pBumps->nColorCount < 2 )   pBumps->nColorCount = 2;
271         if( pBumps->nColorCount > 128 ) pBumps->nColorCount = 128;
272
273         pBumps->aColors = malloc( pBumps->nColorCount * sizeof(uint32_ ) );
274
275         /* Creates a phong shade:                 / BaseColor  \                               Index/ColorCount 
276          *                                                      PhongShade = | ------------ | Index + ( 65535 - BaseColor )^ 
277          *                                                                                \ ColorCount /                                                                                                */
278         pBumps->nColorCount--;
279         for( iColor=0; iColor<=pBumps->nColorCount; iColor++ )
280         {
281                 Color.red   = (uint16_)( ( ( BaseColor.red   / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.red,   iColor/(double)pBumps->nColorCount ) );
282                 Color.green = (uint16_)( ( ( BaseColor.green / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.green, iColor/(double)pBumps->nColorCount ) );
283                 Color.blue  = (uint16_)( ( ( BaseColor.blue  / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.blue,  iColor/(double)pBumps->nColorCount ) );
284
285                 if( !XAllocColor( pBumps->dpy, pXWinAttribs->colormap, &Color ) )
286                 {
287                         XFreeColors( pBumps->dpy, pXWinAttribs->colormap, pBumps->aColors, iColor, 0 );
288                         free( pBumps->aColors );
289                         pBumps->aColors = malloc( pBumps->nColorCount * sizeof(uint32_) );
290                         pBumps->nColorCount--;
291                         iColor = -1;
292                 }
293                 else
294                         pBumps->aColors[ iColor ] = Color.pixel;
295         }
296         pBumps->nColorCount++;
297
298 #ifdef VERBOSE
299         printf( "%s: Allocated %d colors.\n", progclass, pBumps->nColorCount );
300 #endif  /*  VERBOSE */
301
302         XSetWindowBackground( pBumps->dpy, pBumps->Win, pBumps->aColors[ 0 ] );
303 }
304
305
306 /* Grabs the current contents of the window to use an intensity-based bump map. */
307 static void InitBumpMap(Display *dpy, SBumps *pBumps, XWindowAttributes *pXWinAttribs )
308 {
309         pBumps->xColors = (XColor*)malloc( pBumps->iWinWidth * sizeof(XColor) );
310
311     if (pBumps->source) abort();
312     pBumps->source = XCreatePixmap(pBumps->dpy, pBumps->Win,
313                                    pXWinAttribs->width, pXWinAttribs->height,
314                                    pXWinAttribs->depth);
315   pBumps->img_loader = load_image_async_simple (0, pXWinAttribs->screen,
316                                             pBumps->Win, pBumps->source, 0, 0);
317 }
318
319 static void InitBumpMap_2(Display *dpy, SBumps *pBumps)
320 {
321         XImage *pScreenImage;
322         XColor *pColor;
323         uint8_ nSoften;
324         uint16_ iWidth, iHeight;
325         uint32_ nAverager;
326         uint16_ *pBump;
327         uint16_ maxHeight;
328         double softenMultiplier = 1.0f;
329         BOOL bInvert = (BOOL)get_boolean_resource(dpy,  "invert", "Boolean" );
330     XWindowAttributes XWinAttribs;
331     XGetWindowAttributes( pBumps->dpy, pBumps->Win, &XWinAttribs );
332
333         pScreenImage = XGetImage( pBumps->dpy, pBumps->source, 0, 0, 
334                               pBumps->iWinWidth, pBumps->iWinHeight,
335                               ~0L, ZPixmap );
336     XFreePixmap (pBumps->dpy, pBumps->source);
337     pBumps->source = 0;
338
339         XSetWindowBackground( pBumps->dpy, pBumps->Win, pBumps->aColors[ 0 ] );
340         XClearWindow (pBumps->dpy, pBumps->Win);
341         XSync (pBumps->dpy, 0);
342
343         pBumps->aBumpMap = malloc( pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_) );
344         
345         nSoften = get_integer_resource(dpy,  "soften", "Integer" );
346         while( nSoften-- )
347                 softenMultiplier *= 1.0f + ( 1.0f / 3.0f );     /* Softening takes the max height down, so scale up to compensate. */
348         maxHeight = pBumps->SpotLight.nLightRadius * softenMultiplier;
349         nAverager = maxHeight ? ( 3 * 0xFFFF ) / maxHeight : 0;
350
351         pBump = pBumps->aBumpMap;
352         if( bInvert )   /* Funny, it's actually the 'else' that inverts the bump map... */
353         {
354                 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
355                 {
356                         pColor = pBumps->xColors;
357                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
358                                 (pColor++)->pixel = XGetPixel( pScreenImage, iWidth, iHeight );
359
360                         XQueryColors( pBumps->dpy, XWinAttribs.colormap, pBumps->xColors, pBumps->iWinWidth );
361
362                         pColor = pBumps->xColors;
363                         for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pColor, ++pBump )
364                           *pBump = ( nAverager ? ( pColor->red + pColor->green + pColor->blue ) / nAverager : 0 );
365                 }
366         }
367         else
368         {
369                 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
370                 {
371                         pColor = pBumps->xColors;
372                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
373                                 (pColor++)->pixel = XGetPixel( pScreenImage, iWidth, iHeight );
374
375                         XQueryColors( pBumps->dpy, XWinAttribs.colormap, pBumps->xColors, pBumps->iWinWidth );
376         
377                         pColor = pBumps->xColors;
378                         for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pColor, ++pBump )
379                           *pBump = ( maxHeight - ( nAverager ? ( pColor->red + pColor->green + pColor->blue ) / nAverager : 0 ) );
380                 }
381         }
382
383         XDestroyImage( pScreenImage );
384
385         nSoften = get_integer_resource(dpy,  "soften", "Integer" );
386 #ifdef VERBOSE
387         if( nSoften )   printf( "%s: Softening Bump Map %d time(s)...\n", progclass, nSoften );
388 #endif
389         while( nSoften-- )
390                 SoftenBumpMap( pBumps );
391
392         free( pBumps->xColors );
393     pBumps->xColors = 0;
394 }
395
396 /* Soften the bump map.  This is to avoid pixelated-looking ridges.
397  * |-----|-----|-----|
398  * |  0% |12.5%|  0% |  The adjacent pixels are averaged together
399  * |-----|-----|-----|  first.  Then than value is averaged with
400  * |12.5%| 50% |12.5%|  the pixel is question. This essentially weights
401  * |-----|-----|-----|  each pixel as shown on the left.
402  * |  0% |12.5%|  0% |
403  * |-----|-----|-----|
404  */
405 static void SoftenBumpMap( SBumps *pBumps )
406 {
407         uint16_ *pOffset, *pTOffset;
408         uint32_ nHeight;
409         uint32_ iWidth, iHeight;
410         uint16_ *aTempBuffer = malloc( pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_) );
411
412         pOffset = pBumps->aBumpMap;
413         pTOffset = aTempBuffer;
414         for( iHeight=pBumps->iWinHeight; iHeight; --iHeight )
415         {
416                 for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pOffset, ++pTOffset )
417                 {
418                         if( iHeight==pBumps->iWinHeight || iHeight==1 ||
419                                 iWidth==pBumps->iWinWidth || iWidth==1 )
420                         {
421                                 *pTOffset = 0;
422                                 continue;
423                         }
424
425                         nHeight = pOffset[ -pBumps->iWinWidth ];
426                         nHeight += pOffset[ 1 ];
427                         nHeight += pOffset[ pBumps->iWinWidth ];
428                         nHeight += pOffset[ -1 ];
429                         nHeight >>= 2;
430                         nHeight += pOffset[ 0 ];
431                         nHeight >>= 1;
432                         *pTOffset = nHeight;
433                 }
434         }                                               
435
436         memcpy( pBumps->aBumpMap, aTempBuffer, pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_) );
437         free( aTempBuffer );
438 }
439
440
441 /* This is where we slap down some pixels... */
442 static void Execute( SBumps *pBumps )
443 {
444         int32_ nLightXPos, nLightYPos;
445         int32_ iScreenX, iScreenY;
446         int32_ iLightX, iLightY;
447         uint16_ *pBOffset;
448         int8_ *pDOffset;
449         int32_ nX, nY;
450         uint16_ nColor;
451         int32_ nLightOffsetFar = pBumps->SpotLight.nFalloffDiameter - pBumps->SpotLight.nLightRadius;
452
453         CalcLightPos( pBumps );
454         
455         /* Offset to upper left hand corner. */
456         nLightXPos = pBumps->SpotLight.nXPos - pBumps->SpotLight.nFalloffRadius;
457         nLightYPos = pBumps->SpotLight.nYPos - pBumps->SpotLight.nFalloffRadius;
458         
459         for( iScreenY=nLightYPos, iLightY=-pBumps->SpotLight.nLightRadius; iLightY<nLightOffsetFar; ++iScreenY, ++iLightY )
460         {
461                 if( iScreenY < 0 )                                                      continue;
462                 else if( iScreenY >= pBumps->iWinHeight )       break;
463
464     /* warning: pointer targets in assignment differ in signedness
465        Should pDOffset be a int8?  I can't tell.  -jwz, 22-Jul-2003 */
466                 pDOffset = (int8_ *) &pBumps->pXImage->data[ (iLightY+pBumps->SpotLight.nLightRadius) * pBumps->pXImage->bytes_per_line ];
467                 pBOffset = pBumps->aBumpMap + ( iScreenY * pBumps->iWinWidth ) + nLightXPos;
468                 for( iScreenX=nLightXPos, iLightX=-pBumps->SpotLight.nLightRadius; iLightX<nLightOffsetFar; ++iScreenX, ++iLightX, ++pBOffset, pDOffset+=pBumps->bytesPerPixel )
469                 {
470                         if( iScreenX < 0 )                                                      continue;
471                         else if( iScreenX >= pBumps->iWinWidth )        break;
472                         else if( iScreenY == 0 || iScreenY >= pBumps->iWinHeight-2 ||
473                                          iScreenX == 0 || iScreenX >= pBumps->iWinWidth-2 )
474                         {
475                                 MyPutPixel( pDOffset, pBumps->aColors[ 0 ] );
476                                 continue;
477                         }
478
479                         /* That's right folks, all the magic of bump mapping occurs in these two lines.  (kinda disappointing, isn't it?) */
480                         nX = ( pBOffset[ 1 ] - pBOffset[ 0 ] ) + iLightX;
481                         nY = ( pBOffset[ pBumps->iWinWidth ] - pBOffset[ 0 ] ) + iLightY;
482
483                         if( nX<0 || nX>=pBumps->SpotLight.nLightDiameter
484                          || nY<0 || nY>=pBumps->SpotLight.nLightDiameter )
485                         {
486                                 MyPutPixel( pDOffset, pBumps->aColors[ 0 ] );
487                                 continue;
488                         }
489                                 
490                         nColor = pBumps->SpotLight.aLightMap[ ( nY * pBumps->SpotLight.nLightDiameter ) + nX ];
491                         MyPutPixel( pDOffset, pBumps->aColors[ nColor ] );
492                 }
493         }       
494
495         /* Allow the spotlight to go *slightly* off the screen by clipping the XImage. */
496         iLightX = iLightY = 0;  /* Use these for XImages X and Y now.   */
497         nX = nY = pBumps->SpotLight.nFalloffDiameter;   /* Use these for XImage width and height now.   */
498         if( nLightXPos < 0 )
499         {
500                 iLightX = -nLightXPos;
501                 nX -= iLightX;
502                 nLightXPos = 0;
503         }
504         else if( nLightXPos + nX >= pBumps->iWinWidth )
505         {
506                 nX -= ( nLightXPos + nX ) - pBumps->iWinWidth;
507         }
508         
509         if( nLightYPos < 0 )
510         {
511                 iLightY = -nLightYPos;
512                 nY -= iLightY;
513                 nLightYPos = 0;
514         }
515         else if( nLightYPos + nY >= pBumps->iWinHeight )
516         {
517                 nY -= ( nLightYPos + nY ) - pBumps->iWinHeight;
518         }
519         
520 #ifdef HAVE_XSHM_EXTENSION
521         if( pBumps->bUseShm )
522                 XShmPutImage( pBumps->dpy, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, iLightX, iLightY, nLightXPos, nLightYPos,
523                                           nX, nY, False);
524         else
525 #endif /* HAVE_XSHM_EXTENSION */
526                 XPutImage( pBumps->dpy, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, iLightX, iLightY, nLightXPos, nLightYPos,
527                                    nX, nY );
528 }
529
530
531 static void DestroySpotLight( SSpotLight *pSpotLight ) { free( pSpotLight->aLightMap ); }
532
533 /* Clean up */
534 static void DestroyBumps( SBumps *pBumps )
535 {
536         DestroySpotLight( &pBumps->SpotLight );
537         free( pBumps->aColors );
538         free( pBumps->aBumpMap );
539 #ifdef HAVE_XSHM_EXTENSION
540         if( pBumps->bUseShm )
541                 destroy_xshm_image( pBumps->dpy, pBumps->pXImage, &pBumps->XShmInfo );
542         else
543 #endif /* HAVE_XSHM_EXTENSION */
544                 XDestroyImage( pBumps->pXImage );
545 }
546
547
548 /* All messages to the screensaver are processed here. */
549 static void *
550 bumps_init (Display *dpy, Window Win)
551 {
552         SBumps *Bumps = (SBumps *) calloc (1, sizeof(SBumps));
553
554 #ifdef VERBOSE
555         time_t Time = time( NULL );
556         uint16_ iFrame = 0;
557 #endif  /*  VERBOSE */
558         
559         CreateBumps( Bumps, dpy, Win );
560         Bumps->delay = get_integer_resource(dpy,  "delay", "Integer" );
561     return Bumps;
562 }
563
564 static unsigned long
565 bumps_draw (Display *dpy, Window window, void *closure)
566 {
567   SBumps *Bumps = (SBumps *) closure;
568
569   if (Bumps->img_loader)   /* still loading */
570     {
571       Bumps->img_loader = load_image_async_simple (Bumps->img_loader, 0, 0, 0, 0, 0);
572       if (! Bumps->img_loader)  /* just finished */
573         InitBumpMap_2(dpy, Bumps);
574       return Bumps->delay;
575     }
576
577
578
579   Execute( Bumps );
580
581 #ifdef VERBOSE
582   iFrame++;
583   if( Time - time( NULL ) )
584     {
585       printf( "FPS: %d\n", iFrame );
586       Time = time( NULL );
587       iFrame = 0;
588     }
589 #endif  /*  VERBOSE */
590
591   return Bumps->delay;
592 }
593
594 static void
595 bumps_reshape (Display *dpy, Window window, void *closure, 
596                  unsigned int w, unsigned int h)
597 {
598 }
599
600 static Bool
601 bumps_event (Display *dpy, Window window, void *closure, XEvent *event)
602 {
603   return False;
604 }
605
606 static void
607 bumps_free (Display *dpy, Window window, void *closure)
608 {
609   SBumps *Bumps = (SBumps *) closure;
610   DestroyBumps( Bumps );
611 }
612
613
614 XSCREENSAVER_MODULE ("Bumps", bumps)
615
616 /* vim: ts=4
617  */