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