From http://www.jwz.org/xscreensaver/xscreensaver-5.30.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
32 #include <math.h>
33 #include <stdint.h>
34 #include "screenhack.h"
35
36 #ifdef HAVE_XSHM_EXTENSION
37 #include "xshm.h"
38 #endif /* HAVE_XSHM_EXTENSION */
39
40
41 /* Defines: */
42 /* #define VERBOSE */
43 #define RANDOM() ((int) (random() & 0X7FFFFFFFL))
44
45 typedef unsigned char   BOOL;
46
47
48 /* Globals: */
49
50 static const char *bumps_defaults [] = {
51   ".background: black",
52   ".foreground: white",
53   "*fpsSolid:   true",
54   "*color:              random",
55   "*colorcount: 64",
56   "*delay:              30000",
57   "*duration:   120",
58   "*soften:             1",
59   "*invert:             FALSE",
60 #ifdef __sgi    /* really, HAVE_READ_DISPLAY_EXTENSION */
61   "*visualID:   Best",
62 #endif
63 #ifdef HAVE_XSHM_EXTENSION
64   "*useSHM:             True",
65 #endif /* HAVE_XSHM_EXTENSION */
66 #ifdef USE_IPHONE
67   "*ignoreRotation: True",
68   "*rotateImages:   True",
69 #endif
70   0
71 };
72
73 static XrmOptionDescRec bumps_options [] = {
74   { "-color",           ".color",               XrmoptionSepArg, 0 },
75   { "-colorcount",      ".colorcount",  XrmoptionSepArg, 0 },
76   { "-duration",        ".duration",    XrmoptionSepArg, 0 },
77   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
78   { "-soften",          ".soften",              XrmoptionSepArg, 0 },
79   { "-invert",          ".invert",              XrmoptionNoArg, "TRUE" },
80 #ifdef HAVE_XSHM_EXTENSION
81   { "-shm",                     ".useSHM",              XrmoptionNoArg, "True" },
82   { "-no-shm",          ".useSHM",              XrmoptionNoArg, "False" },
83 #endif /* HAVE_XSHM_EXTENSION */
84
85   { 0, 0, 0, 0 }
86 };
87
88
89 /* This structure handles everything to do with the spotlight, and is designed to be
90  * a member of TBumps. */
91 typedef struct
92 {
93         uint8_t *aLightMap;
94         uint16_t nFalloffDiameter, nFalloffRadius;
95         uint16_t nLightDiameter, nLightRadius;
96         float nAccelX, nAccelY;
97         float nAccelMax;
98         float nVelocityX, nVelocityY;
99         float nVelocityMax;
100         float nXPos, nYPos;
101 } SSpotLight;
102
103
104 /* The entire program's operation is contained within this structure. */
105 typedef struct
106 {
107         /* XWindows specific variables. */
108         Display *dpy;
109         Window Win;
110         Screen *screen;
111         Pixmap source;
112         GC GraphicsContext;
113         XColor *xColors;
114         unsigned long *aColors;
115         XImage *pXImage;
116 #ifdef HAVE_XSHM_EXTENSION
117         XShmSegmentInfo XShmInfo;
118         Bool    bUseShm;
119 #endif /* HAVE_XSHM_EXTENSION */
120
121         uint8_t nColorCount;                            /* Number of colors used. */
122         uint8_t bytesPerPixel;
123         uint16_t iWinWidth, iWinHeight;
124         uint16_t *aBumpMap;                             /* The actual bump map. */
125         SSpotLight SpotLight;
126
127         int delay;
128         int duration;
129         time_t start_time;
130
131         async_load_state *img_loader;
132 } SBumps;
133
134
135 static void SetPalette(Display *, SBumps *, XWindowAttributes * );
136 static void InitBumpMap(Display *, SBumps *, XWindowAttributes * );
137 static void InitBumpMap_2(Display *, SBumps *);
138 static void SoftenBumpMap( SBumps * );
139
140
141
142
143 /* This function pointer will point to the appropriate PutPixel*() function below. */
144 static void (*MyPutPixel)( int8_t *, uint32_t );
145
146 static void PutPixel32( int8_t *pData, uint32_t pixel )
147 {
148         *(uint32_t *)pData = pixel;
149 }
150
151 static void PutPixel24( int8_t *pData, uint32_t pixel )
152 {
153         pData[ 2 ] = ( pixel & 0x00FF0000 ) >> 16;
154         pData[ 1 ] = ( pixel & 0x0000FF00 ) >> 8;
155         pData[ 0 ] = ( pixel & 0x000000FF );
156 }
157
158 static void PutPixel16( int8_t *pData, uint32_t pixel )
159 {
160         *(uint16_t *)pData = (uint16_t)pixel;
161 }
162
163 static void PutPixel8( int8_t *pData, uint32_t pixel )
164 {
165         *(uint8_t *)pData = (uint8_t)pixel;
166 }
167
168 /* Creates the light map, which is a circular image... going from black around the edges
169  * to white in the center. */
170 static void CreateSpotLight( SSpotLight *pSpotLight, uint16_t iDiameter, uint16_t nColorCount )
171 {
172         double nDist;
173         int16_t iDistX, iDistY;
174         uint8_t *pLOffset;
175         
176         pSpotLight->nFalloffDiameter = iDiameter;
177         pSpotLight->nFalloffRadius = pSpotLight->nFalloffDiameter / 2;
178         pSpotLight->nLightDiameter = iDiameter / 2;
179         pSpotLight->nLightRadius = pSpotLight->nLightDiameter / 2;
180 #ifdef VERBOSE
181         printf( "%s: Falloff Diameter: %d\n", progclass, pSpotLight->nFalloffDiameter );
182         printf( "%s: Spot Light Diameter: %d\n", progclass, pSpotLight->nLightDiameter );
183 #endif
184
185         pSpotLight->aLightMap = malloc( pSpotLight->nLightDiameter * pSpotLight->nLightDiameter * sizeof(uint8_t) );
186
187         pLOffset = pSpotLight->aLightMap;
188         for( iDistY=-pSpotLight->nLightRadius; iDistY<pSpotLight->nLightRadius; ++iDistY )
189         {
190                 for( iDistX=-pSpotLight->nLightRadius; iDistX<pSpotLight->nLightRadius; ++iDistX )
191                 {
192                         nDist = sqrt( pow( iDistX+0.5F, 2 ) + pow( iDistY+0.5F, 2 ) );
193                         if( nDist / pSpotLight->nLightRadius <= 1.0f )
194                                 *pLOffset = (uint8_t)(nColorCount - ( ( nDist / pSpotLight->nLightRadius ) * ( nColorCount - 1 ) ));
195                         else
196                                 *pLOffset = 0;
197
198                         ++pLOffset;
199                 }
200         }
201                 
202         /* Initialize movement variables.       */
203         pSpotLight->nAccelX = 0;
204         pSpotLight->nAccelY = 0;
205         pSpotLight->nVelocityX = ( RANDOM() % 2 ) ? pSpotLight->nVelocityMax : -pSpotLight->nVelocityMax;
206         pSpotLight->nVelocityY = ( RANDOM() % 2 ) ? pSpotLight->nVelocityMax : -pSpotLight->nVelocityMax;
207 }
208
209
210 /* Calculates the position of the spot light on the screen. */
211 static void CalcLightPos( SBumps *pBumps )
212 {
213         SSpotLight *pSpotLight = &pBumps->SpotLight;
214         float nGravity;
215
216         /* X */
217         if( pSpotLight->nXPos < pSpotLight->nFalloffRadius )                                                    nGravity = 1.0f;
218         else if( pSpotLight->nXPos > pBumps->iWinWidth - pSpotLight->nFalloffRadius )   nGravity = -1.0f;
219         else                                                                                                                                                    nGravity = ( ( RANDOM() % 201 ) / 100.0f ) - 1.0f;
220                 
221         pSpotLight->nAccelX += nGravity * ( pSpotLight->nAccelMax / 5.0f );
222         if( pSpotLight->nAccelX < -pSpotLight->nAccelMax )              pSpotLight->nAccelX = -pSpotLight->nAccelMax;
223         else if( pSpotLight->nAccelX > pSpotLight->nAccelMax )  pSpotLight->nAccelX = pSpotLight->nAccelMax;
224
225         pSpotLight->nVelocityX += pSpotLight->nAccelX;
226         if( pSpotLight->nVelocityX < -pSpotLight->nVelocityMax )                pSpotLight->nVelocityX = -pSpotLight->nVelocityMax;
227         else if( pSpotLight->nVelocityX > pSpotLight->nVelocityMax )    pSpotLight->nVelocityX = pSpotLight->nVelocityMax;
228
229         pSpotLight->nXPos += pSpotLight->nVelocityX;
230
231         /* Y */
232         if( pSpotLight->nYPos < pSpotLight->nFalloffRadius )                                                            nGravity = 1.0f;
233         else if( pSpotLight->nYPos > pBumps->iWinHeight - pSpotLight->nFalloffRadius )  nGravity = -1.0f;
234         else                                                                                                                                                    nGravity = ( ( RANDOM() % 201 ) / 100.0f ) - 1.0f;
235                 
236         pSpotLight->nAccelY += nGravity * ( pSpotLight->nAccelMax / 5.0f );
237         if( pSpotLight->nAccelY < -pSpotLight->nAccelMax )              pSpotLight->nAccelY = -pSpotLight->nAccelMax;
238         else if( pSpotLight->nAccelY > pSpotLight->nAccelMax )  pSpotLight->nAccelY = pSpotLight->nAccelMax;
239
240         pSpotLight->nVelocityY += pSpotLight->nAccelY;
241         if( pSpotLight->nVelocityY < -pSpotLight->nVelocityMax )                pSpotLight->nVelocityY = -pSpotLight->nVelocityMax;
242         else if( pSpotLight->nVelocityY > pSpotLight->nVelocityMax )    pSpotLight->nVelocityY = pSpotLight->nVelocityMax;
243
244         pSpotLight->nYPos += pSpotLight->nVelocityY;
245 }
246
247
248 /* Main initialization function. */
249 static void CreateBumps( SBumps *pBumps, Display *dpy, Window NewWin )
250 {
251         XWindowAttributes XWinAttribs;
252         XGCValues GCValues;
253         int32_t nGCFlags;
254         uint16_t iDiameter;
255
256         /* Make size and velocity a function of window size, so it appears the same at 100x60 as it does in 3200x1200. */
257         XGetWindowAttributes( dpy, NewWin, &XWinAttribs );
258         pBumps->iWinWidth = XWinAttribs.width;
259         pBumps->iWinHeight = XWinAttribs.height;
260         pBumps->SpotLight.nXPos = XWinAttribs.width / 2.0f;
261         pBumps->SpotLight.nYPos = XWinAttribs.height / 2.0f;
262         pBumps->SpotLight.nVelocityMax = ( ( XWinAttribs.width < XWinAttribs.height ) ? XWinAttribs.width : XWinAttribs.height ) / 140.0f;
263         pBumps->SpotLight.nAccelMax = pBumps->SpotLight.nVelocityMax / 10.0f;
264         pBumps->dpy = dpy;
265         pBumps->Win = NewWin;
266     pBumps->screen = XWinAttribs.screen;
267         pBumps->pXImage = NULL;
268         
269         iDiameter = ( ( pBumps->iWinWidth < pBumps->iWinHeight ) ? pBumps->iWinWidth : pBumps->iWinHeight ) / 2;
270
271     /* jwz: sometimes we get tearing if this lands on the wrong bounaary;
272        constraining it to be a multiple of 8 seems to fix it. */
273     iDiameter = ((iDiameter+7)/8)*8;
274
275 #ifdef HAVE_XSHM_EXTENSION
276         pBumps->bUseShm = get_boolean_resource(dpy,  "useSHM", "Boolean" );
277
278         if( pBumps->bUseShm )
279         {
280                 pBumps->pXImage = create_xshm_image( pBumps->dpy, XWinAttribs.visual, XWinAttribs.depth,
281                                                                                          ZPixmap, NULL, &pBumps->XShmInfo, iDiameter, iDiameter );
282                 if( !pBumps->pXImage )
283                 {
284                         fprintf( stderr, "%s: Unable to create XShmImage.\n", progname );
285                         pBumps->bUseShm = False;
286                 }
287         }
288 #endif /* HAVE_XSHM_EXTENSION */
289         if( !pBumps->pXImage )
290         {
291                 pBumps->pXImage = XCreateImage( pBumps->dpy, XWinAttribs.visual, XWinAttribs.depth, 
292                                                                         ZPixmap, 0, NULL, iDiameter, iDiameter, BitmapPad( pBumps->dpy ), 0 );
293                 pBumps->pXImage->data = malloc( pBumps->pXImage->bytes_per_line * pBumps->pXImage->height * sizeof(int8_t) );
294         }
295
296         /* For speed, access the XImage data directly using my own PutPixel routine. */
297         switch( pBumps->pXImage->bits_per_pixel )
298         {
299                 case 32:
300                         pBumps->bytesPerPixel = 4;
301                         MyPutPixel = PutPixel32;
302                         break;
303                 
304                 case 24:
305                         pBumps->bytesPerPixel = 3;
306                         MyPutPixel = PutPixel24;
307                         break;
308
309                 case 16:
310                         pBumps->bytesPerPixel = 2;
311                         MyPutPixel = PutPixel16;
312                         break;
313
314                 case 8:
315                         pBumps->bytesPerPixel = 1;
316                         MyPutPixel = PutPixel8;
317                         break;
318
319                 default:
320                         fprintf( stderr, "%s: Unknown XImage depth.", progname );
321 #ifdef HAVE_XSHM_EXTENSION
322                         if( pBumps->bUseShm )
323                                 destroy_xshm_image( pBumps->dpy, pBumps->pXImage, &pBumps->XShmInfo );
324                         else
325 #endif /* HAVE_XSHM_EXTENSION */
326                                 XDestroyImage( pBumps->pXImage );
327                         exit( 1 );
328         }
329         
330         GCValues.function = GXcopy;
331         GCValues.subwindow_mode = IncludeInferiors;
332         nGCFlags = GCFunction;
333         if( use_subwindow_mode_p( XWinAttribs.screen, pBumps->Win ) ) /* See grabscreen.c */
334                 nGCFlags |= GCSubwindowMode;
335         pBumps->GraphicsContext = XCreateGC( pBumps->dpy, pBumps->Win, nGCFlags, &GCValues );
336         
337         SetPalette(dpy, pBumps, &XWinAttribs );
338         CreateSpotLight( &pBumps->SpotLight, iDiameter, pBumps->nColorCount );
339         InitBumpMap(dpy, pBumps, &XWinAttribs );
340 }
341
342
343 /* Creates a specialized phong shade palette. */
344 static void SetPalette(Display *dpy, SBumps *pBumps, XWindowAttributes *pXWinAttribs )
345 {
346         XColor BaseColor;
347         XColor Color;
348         char *sColor;                   /* Spotlight Color */
349         int16_t iColor;
350         
351         sColor = get_string_resource(dpy,  "color", "Color" );
352
353         BaseColor.red = RANDOM() % 0xFFFF; 
354         BaseColor.green = RANDOM() % 0xFFFF;
355         BaseColor.blue = RANDOM() % 0xFFFF;
356         
357         /* Make one color full intesity to avoid dark spotlights.       */
358         switch( RANDOM() % 3 )
359         {
360                 case 0: BaseColor.red   = 0xFFFF;       break;
361                 case 1: BaseColor.green = 0xFFFF;       break;
362                 case 2: BaseColor.blue  = 0xFFFF;       break;
363         }
364
365         if( strcasecmp( sColor, "random" ) && !XParseColor( pBumps->dpy, pXWinAttribs->colormap, sColor, &BaseColor ) )
366                 fprintf( stderr, "%s: color %s not found in database. Choosing random...\n", progname, sColor );
367
368 #ifdef VERBOSE
369         printf( "%s: Spotlight color is <%d,%d,%d> RGB.\n", progclass, BaseColor.red, BaseColor.green, BaseColor.blue );
370 #endif  /*  VERBOSE */
371
372         pBumps->nColorCount = get_integer_resource(dpy,  "colorcount", "Integer" );
373         if( pBumps->nColorCount < 2 )   pBumps->nColorCount = 2;
374         if( pBumps->nColorCount > 128 ) pBumps->nColorCount = 128;
375
376         pBumps->aColors = malloc( pBumps->nColorCount * sizeof(unsigned long) );
377
378         /* Creates a phong shade:                 / BaseColor  \                               Index/ColorCount 
379          *                                                      PhongShade = | ------------ | Index + ( 65535 - BaseColor )^ 
380          *                                                                                \ ColorCount /                                                                                                */
381         pBumps->nColorCount--;
382         for( iColor=0; iColor<=pBumps->nColorCount; iColor++ )
383         {
384                 Color.red   = (uint16_t)( ( ( BaseColor.red   / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.red,   iColor/(double)pBumps->nColorCount ) );
385                 Color.green = (uint16_t)( ( ( BaseColor.green / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.green, iColor/(double)pBumps->nColorCount ) );
386                 Color.blue  = (uint16_t)( ( ( BaseColor.blue  / (double)pBumps->nColorCount ) * iColor ) + pow( 0xFFFF - BaseColor.blue,  iColor/(double)pBumps->nColorCount ) );
387
388                 if( !XAllocColor( pBumps->dpy, pXWinAttribs->colormap, &Color ) )
389                 {
390                         XFreeColors( pBumps->dpy, pXWinAttribs->colormap, pBumps->aColors, iColor, 0 );
391                         free( pBumps->aColors );
392                         pBumps->aColors = malloc( pBumps->nColorCount * sizeof(unsigned long) );
393                         pBumps->nColorCount--;
394                         iColor = -1;
395                 }
396                 else
397                         pBumps->aColors[ iColor ] = Color.pixel;
398         }
399         pBumps->nColorCount++;
400
401 #ifdef VERBOSE
402         printf( "%s: Allocated %d colors.\n", progclass, pBumps->nColorCount );
403 #endif  /*  VERBOSE */
404
405         XSetWindowBackground( pBumps->dpy, pBumps->Win, pBumps->aColors[ 0 ] );
406 }
407
408
409 /* Grabs the current contents of the window to use an intensity-based bump map. */
410 static void InitBumpMap(Display *dpy, SBumps *pBumps, XWindowAttributes *pXWinAttribs )
411 {
412         pBumps->xColors = (XColor*)malloc( pBumps->iWinWidth * sizeof(XColor) );
413
414     if (pBumps->source) abort();
415     pBumps->source = XCreatePixmap(pBumps->dpy, pBumps->Win,
416                                    pXWinAttribs->width, pXWinAttribs->height,
417                                    pXWinAttribs->depth);
418   pBumps->img_loader = load_image_async_simple (0, pXWinAttribs->screen,
419                                             pBumps->Win, pBumps->source, 0, 0);
420 }
421
422 static void InitBumpMap_2(Display *dpy, SBumps *pBumps)
423 {
424         XImage *pScreenImage;
425         XColor *pColor;
426         uint8_t nSoften;
427         uint16_t iWidth, iHeight;
428         uint32_t nAverager;
429         uint16_t        *pBump;
430         uint16_t maxHeight;
431         double softenMultiplier = 1.0f;
432         BOOL bInvert = (BOOL)get_boolean_resource(dpy,  "invert", "Boolean" );
433     XWindowAttributes XWinAttribs;
434     XGetWindowAttributes( pBumps->dpy, pBumps->Win, &XWinAttribs );
435
436     pBumps->start_time = time ((time_t) 0);
437
438         pScreenImage = XGetImage( pBumps->dpy, pBumps->source, 0, 0, 
439                               pBumps->iWinWidth, pBumps->iWinHeight,
440                               ~0L, ZPixmap );
441 /*    XFreePixmap (pBumps->dpy, pBumps->source);
442     pBumps->source = 0;*/
443
444         XSetWindowBackground( pBumps->dpy, pBumps->Win, pBumps->aColors[ 0 ] );
445         XClearWindow (pBumps->dpy, pBumps->Win);
446         XSync (pBumps->dpy, 0);
447
448         pBumps->aBumpMap = malloc( pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_t) );
449         
450         nSoften = get_integer_resource(dpy,  "soften", "Integer" );
451         while( nSoften-- )
452                 softenMultiplier *= 1.0f + ( 1.0f / 3.0f );     /* Softening takes the max height down, so scale up to compensate. */
453         maxHeight = pBumps->SpotLight.nLightRadius * softenMultiplier;
454         nAverager = maxHeight ? ( 3 * 0xFFFF ) / maxHeight : 0;
455
456         pBump = pBumps->aBumpMap;
457         if( bInvert )   /* Funny, it's actually the 'else' that inverts the bump map... */
458         {
459                 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
460                 {
461                         pColor = pBumps->xColors;
462                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
463                                 (pColor++)->pixel = XGetPixel( pScreenImage, iWidth, iHeight );
464
465                         XQueryColors( pBumps->dpy, XWinAttribs.colormap, pBumps->xColors, pBumps->iWinWidth );
466
467                         pColor = pBumps->xColors;
468                         for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pColor, ++pBump )
469                           *pBump = ( nAverager ? ( pColor->red + pColor->green + pColor->blue ) / nAverager : 0 );
470                 }
471         }
472         else
473         {
474                 for( iHeight=0; iHeight<pBumps->iWinHeight; iHeight++ )
475                 {
476                         pColor = pBumps->xColors;
477                         for( iWidth=0; iWidth<pBumps->iWinWidth; iWidth++ )
478                                 (pColor++)->pixel = XGetPixel( pScreenImage, iWidth, iHeight );
479
480                         XQueryColors( pBumps->dpy, XWinAttribs.colormap, pBumps->xColors, pBumps->iWinWidth );
481         
482                         pColor = pBumps->xColors;
483                         for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pColor, ++pBump )
484                           *pBump = ( maxHeight - ( nAverager ? ( pColor->red + pColor->green + pColor->blue ) / nAverager : 0 ) );
485                 }
486         }
487
488         XDestroyImage( pScreenImage );
489
490         nSoften = get_integer_resource(dpy,  "soften", "Integer" );
491 #ifdef VERBOSE
492         if( nSoften )   printf( "%s: Softening Bump Map %d time(s)...\n", progclass, nSoften );
493 #endif
494         while( nSoften-- )
495                 SoftenBumpMap( pBumps );
496
497 /*      free( pBumps->xColors );
498     pBumps->xColors = 0;*/
499 }
500
501 /* Soften the bump map.  This is to avoid pixelated-looking ridges.
502  * |-----|-----|-----|
503  * |  0% |12.5%|  0% |  The adjacent pixels are averaged together
504  * |-----|-----|-----|  first.  Then than value is averaged with
505  * |12.5%| 50% |12.5%|  the pixel is question. This essentially weights
506  * |-----|-----|-----|  each pixel as shown on the left.
507  * |  0% |12.5%|  0% |
508  * |-----|-----|-----|
509  */
510 static void SoftenBumpMap( SBumps *pBumps )
511 {
512         uint16_t *pOffset, *pTOffset;
513         uint32_t nHeight;
514         uint32_t iWidth, iHeight;
515         uint16_t *aTempBuffer = malloc( pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_t) );
516
517         pOffset = pBumps->aBumpMap;
518         pTOffset = aTempBuffer;
519         for( iHeight=pBumps->iWinHeight; iHeight; --iHeight )
520         {
521                 for( iWidth=pBumps->iWinWidth; iWidth; --iWidth, ++pOffset, ++pTOffset )
522                 {
523                         if( iHeight==pBumps->iWinHeight || iHeight==1 ||
524                                 iWidth==pBumps->iWinWidth || iWidth==1 )
525                         {
526                                 *pTOffset = 0;
527                                 continue;
528                         }
529
530                         nHeight = pOffset[ -pBumps->iWinWidth ];
531                         nHeight += pOffset[ 1 ];
532                         nHeight += pOffset[ pBumps->iWinWidth ];
533                         nHeight += pOffset[ -1 ];
534                         nHeight >>= 2;
535                         nHeight += pOffset[ 0 ];
536                         nHeight >>= 1;
537                         *pTOffset = nHeight;
538                 }
539         }                                               
540
541         memcpy( pBumps->aBumpMap, aTempBuffer, pBumps->iWinWidth * pBumps->iWinHeight * sizeof(uint16_t) );
542         free( aTempBuffer );
543 }
544
545
546 /* This is where we slap down some pixels... */
547 static void Execute( SBumps *pBumps )
548 {
549         int32_t nLightXPos, nLightYPos;
550         int32_t iScreenX, iScreenY;
551         int32_t iLightX, iLightY;
552         uint16_t *pBOffset;
553         int8_t *pDOffset;
554         int32_t nX, nY;
555         uint16_t nColor;
556         int32_t nLightOffsetFar = pBumps->SpotLight.nFalloffDiameter - pBumps->SpotLight.nLightRadius;
557
558         CalcLightPos( pBumps );
559         
560         /* Offset to upper left hand corner. */
561         nLightXPos = pBumps->SpotLight.nXPos - pBumps->SpotLight.nFalloffRadius;
562         nLightYPos = pBumps->SpotLight.nYPos - pBumps->SpotLight.nFalloffRadius;
563         
564         for( iScreenY=nLightYPos, iLightY=-pBumps->SpotLight.nLightRadius; iLightY<nLightOffsetFar; ++iScreenY, ++iLightY )
565         {
566                 if( iScreenY < 0 )                                                      continue;
567                 else if( iScreenY >= pBumps->iWinHeight )       break;
568
569     /* warning: pointer targets in assignment differ in signedness
570        Should pDOffset be a int8?  I can't tell.  -jwz, 22-Jul-2003 */
571                 pDOffset = (int8_t *) &pBumps->pXImage->data[ (iLightY+pBumps->SpotLight.nLightRadius) * pBumps->pXImage->bytes_per_line ];
572                 pBOffset = pBumps->aBumpMap + ( iScreenY * pBumps->iWinWidth ) + nLightXPos;
573                 for( iScreenX=nLightXPos, iLightX=-pBumps->SpotLight.nLightRadius; iLightX<nLightOffsetFar; ++iScreenX, ++iLightX, ++pBOffset, pDOffset+=pBumps->bytesPerPixel )
574                 {
575                         if( iScreenX < 0 )                                                      continue;
576                         else if( iScreenX >= pBumps->iWinWidth )        break;
577                         else if( iScreenY == 0 || iScreenY >= pBumps->iWinHeight-2 ||
578                                          iScreenX == 0 || iScreenX >= pBumps->iWinWidth-2 )
579                         {
580                                 MyPutPixel( pDOffset, pBumps->aColors[ 0 ] );
581                                 continue;
582                         }
583
584                         /* That's right folks, all the magic of bump mapping occurs in these two lines.  (kinda disappointing, isn't it?) */
585                         nX = ( pBOffset[ 1 ] - pBOffset[ 0 ] ) + iLightX;
586                         nY = ( pBOffset[ pBumps->iWinWidth ] - pBOffset[ 0 ] ) + iLightY;
587
588                         if( nX<0 || nX>=pBumps->SpotLight.nLightDiameter
589                          || nY<0 || nY>=pBumps->SpotLight.nLightDiameter )
590                         {
591                                 MyPutPixel( pDOffset, pBumps->aColors[ 0 ] );
592                                 continue;
593                         }
594                                 
595                         nColor = pBumps->SpotLight.aLightMap[ ( nY * pBumps->SpotLight.nLightDiameter ) + nX ];
596                         MyPutPixel( pDOffset, pBumps->aColors[ nColor ] );
597                 }
598         }       
599
600         /* Allow the spotlight to go *slightly* off the screen by clipping the XImage. */
601         iLightX = iLightY = 0;  /* Use these for XImages X and Y now.   */
602         nX = nY = pBumps->SpotLight.nFalloffDiameter;   /* Use these for XImage width and height now.   */
603         if( nLightXPos < 0 )
604         {
605                 iLightX = -nLightXPos;
606                 nX -= iLightX;
607                 nLightXPos = 0;
608         }
609         else if( nLightXPos + nX >= pBumps->iWinWidth )
610         {
611                 nX -= ( nLightXPos + nX ) - pBumps->iWinWidth;
612         }
613         
614         if( nLightYPos < 0 )
615         {
616                 iLightY = -nLightYPos;
617                 nY -= iLightY;
618                 nLightYPos = 0;
619         }
620         else if( nLightYPos + nY >= pBumps->iWinHeight )
621         {
622                 nY -= ( nLightYPos + nY ) - pBumps->iWinHeight;
623         }
624         
625 #ifdef HAVE_XSHM_EXTENSION
626         if( pBumps->bUseShm )
627                 XShmPutImage( pBumps->dpy, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, iLightX, iLightY, nLightXPos, nLightYPos,
628                                           nX, nY, False);
629         else
630 #endif /* HAVE_XSHM_EXTENSION */
631                 XPutImage( pBumps->dpy, pBumps->Win, pBumps->GraphicsContext, pBumps->pXImage, iLightX, iLightY, nLightXPos, nLightYPos,
632                                    nX, nY );
633 }
634
635
636 static void DestroySpotLight( SSpotLight *pSpotLight ) { free( pSpotLight->aLightMap ); }
637
638 /* Clean up */
639 static void DestroyBumps( SBumps *pBumps )
640 {
641         DestroySpotLight( &pBumps->SpotLight );
642         free( pBumps->aColors );
643         free( pBumps->aBumpMap );
644 #ifdef HAVE_XSHM_EXTENSION
645         if( pBumps->bUseShm )
646                 destroy_xshm_image( pBumps->dpy, pBumps->pXImage, &pBumps->XShmInfo );
647         else
648 #endif /* HAVE_XSHM_EXTENSION */
649                 XDestroyImage( pBumps->pXImage );
650 }
651
652
653 /* All messages to the screensaver are processed here. */
654 static void *
655 bumps_init (Display *dpy, Window Win)
656 {
657         SBumps *Bumps = (SBumps *) calloc (1, sizeof(SBumps));
658
659 #ifdef VERBOSE
660         time_t Time = time( NULL );
661         uint16_t iFrame = 0;
662 #endif  /*  VERBOSE */
663         
664         CreateBumps( Bumps, dpy, Win );
665         Bumps->delay = get_integer_resource(dpy,  "delay", "Integer" );
666     Bumps->duration = get_integer_resource (dpy, "duration", "Seconds");
667     if (Bumps->delay < 0) Bumps->delay = 0;
668     if (Bumps->duration < 1) Bumps->duration = 1;
669     Bumps->start_time = time ((time_t) 0);
670     return Bumps;
671 }
672
673 static unsigned long
674 bumps_draw (Display *dpy, Window window, void *closure)
675 {
676   SBumps *Bumps = (SBumps *) closure;
677
678   if (Bumps->img_loader)   /* still loading */
679     {
680       Bumps->img_loader = load_image_async_simple (Bumps->img_loader, 0, 0, 0, 0, 0);
681       if (! Bumps->img_loader)  /* just finished */
682         InitBumpMap_2(dpy, Bumps);
683       return Bumps->delay;
684     }
685
686   if (!Bumps->img_loader &&
687       Bumps->start_time + Bumps->duration < time ((time_t) 0)) {
688     Bumps->img_loader = load_image_async_simple (0, Bumps->screen,
689                                                  Bumps->Win, Bumps->source, 
690                                                  0, 0);
691   }
692
693   Execute( Bumps );
694
695 #ifdef VERBOSE
696   iFrame++;
697   if( Time - time( NULL ) )
698     {
699       printf( "FPS: %d\n", iFrame );
700       Time = time( NULL );
701       iFrame = 0;
702     }
703 #endif  /*  VERBOSE */
704
705   return Bumps->delay;
706 }
707
708 static void
709 bumps_reshape (Display *dpy, Window window, void *closure, 
710                  unsigned int w, unsigned int h)
711 {
712 }
713
714 static Bool
715 bumps_event (Display *dpy, Window window, void *closure, XEvent *event)
716 {
717   SBumps *Bumps = (SBumps *) closure;
718   if (screenhack_event_helper (dpy, window, event))
719     {
720       Bumps->start_time = 0;
721       return True;
722     }
723
724   return False;
725 }
726
727 static void
728 bumps_free (Display *dpy, Window window, void *closure)
729 {
730   SBumps *Bumps = (SBumps *) closure;
731   DestroyBumps( Bumps );
732 }
733
734
735 XSCREENSAVER_MODULE ("Bumps", bumps)
736
737 /* vim: ts=4
738  */