|
| 1 | +// |
| 2 | +// gif-h-demo.cpp |
| 3 | +// by Charlie Tangora |
| 4 | +// Public domain. |
| 5 | +// Email me : ctangora -at- gmail -dot- com |
| 6 | +// |
| 7 | +// Shows an example usage of gif.h |
| 8 | +// |
| 9 | + |
| 10 | +#include "gif.h" |
| 11 | + |
| 12 | +#include <math.h> |
| 13 | + |
| 14 | +const int width = 256; |
| 15 | +const int height = 256; |
| 16 | +uint8_t image[ width * height * 4 ]; |
| 17 | + |
| 18 | +void SetPixel( int xx, int yy, uint8_t red, uint8_t grn, uint8_t blu ) |
| 19 | +{ |
| 20 | + uint8_t* pixel = &image[(yy*width+xx)*4]; |
| 21 | + pixel[0] = red; |
| 22 | + pixel[1] = blu; |
| 23 | + pixel[2] = grn; |
| 24 | + pixel[3] = 255; // no alpha for this demo |
| 25 | +} |
| 26 | + |
| 27 | +void SetPixelFloat( int xx, int yy, float fred, float fgrn, float fblu ) |
| 28 | +{ |
| 29 | + // convert float to unorm |
| 30 | + uint8_t red = (uint8_t)roundf( 255.0f * fred ); |
| 31 | + uint8_t grn = (uint8_t)roundf( 255.0f * fgrn ); |
| 32 | + uint8_t blu = (uint8_t)roundf( 255.0f * fblu ); |
| 33 | + |
| 34 | + SetPixel( xx, yy, red, grn, blu ); |
| 35 | +} |
| 36 | + |
| 37 | +int main(int argc, const char * argv[]) |
| 38 | +{ |
| 39 | + const char* filename = "./MyGif.gif"; |
| 40 | + if( argc > 1 ) |
| 41 | + { |
| 42 | + filename = argv[1]; |
| 43 | + } |
| 44 | + |
| 45 | + // Create a gif |
| 46 | + GifWriter writer = {}; |
| 47 | + GifBegin( &writer, filename, width, height, 2, 8, true ); |
| 48 | + |
| 49 | + for( int frame=0; frame<256; ++frame ) |
| 50 | + { |
| 51 | + |
| 52 | + // Make an image, somehow |
| 53 | + // this is the default shadertoy - credit to shadertoy.com |
| 54 | + float tt = frame * 3.14159f * 2 / 255.0f; |
| 55 | + for( int yy=0; yy<height; ++yy ) |
| 56 | + { |
| 57 | + for( int xx=0; xx<width; ++xx ) |
| 58 | + { |
| 59 | + float fx = xx / (float)width; |
| 60 | + float fy = yy / (float)height; |
| 61 | + |
| 62 | + float red = 0.5f + 0.5f * cosf(tt+fx); |
| 63 | + float grn = 0.5f + 0.5f * cosf(tt+fy+2.f); |
| 64 | + float blu = 0.5f + 0.5f * cosf(tt+fx+4.f); |
| 65 | + |
| 66 | + SetPixelFloat( xx, yy, red, grn, blu ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + // Write the frame to the gif |
| 72 | + printf( "Writing frame %d...\n", frame ); |
| 73 | + GifWriteFrame( &writer, image, width, height, 2, 8, true ); |
| 74 | + } |
| 75 | + |
| 76 | + // Write EOF |
| 77 | + GifEnd( &writer ); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments