File: cgif_example.c

package info (click to toggle)
cgif 0.5.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 440 kB
  • sloc: ansic: 3,943; python: 45; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 2,518 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "cgif.h"

#define WIDTH  1024
#define HEIGHT 1024

/* Small helper functions to initialize GIF- and frame-configuration */
static void initGIFConfig(CGIF_Config* pConfig, char* path, uint16_t width, uint16_t height, uint8_t* pPalette, uint16_t numColors) {
  memset(pConfig, 0, sizeof(CGIF_Config));
  pConfig->width                   = width;
  pConfig->height                  = height;
  pConfig->pGlobalPalette          = pPalette;
  pConfig->numGlobalPaletteEntries = numColors;
  pConfig->path                    = path;
}
static void initFrameConfig(CGIF_FrameConfig* pConfig, uint8_t* pImageData) {
  memset(pConfig, 0, sizeof(CGIF_FrameConfig));
  pConfig->pImageData = pImageData;
}

/* This is an example code that creates a GIF-image with random pixels. */
int main(void) {
  CGIF*          pGIF;                                          // struct containing the GIF
  CGIF_Config     gConfig;                                        // global configuration parameters for the GIF
  CGIF_FrameConfig   fConfig;                                     // configuration parameters for a frame
  uint8_t*      pImageData;                                     // image data (an array of color-indices)
  uint8_t       aPalette[] = {0xFF, 0x00, 0x00,                 // red
                              0x00, 0xFF, 0x00,                 // green
                              0x00, 0x00, 0xFF};                // blue
  uint16_t numColors = 3;                                        // number of colors in aPalette (up to 256 possible)

  // initialize the GIF-configuration and create a new GIF
  initGIFConfig(&gConfig, "example_cgif.gif", WIDTH, HEIGHT, aPalette, numColors);
  pGIF = cgif_newgif(&gConfig);

  // create image frame with stripe pattern
  pImageData = malloc(WIDTH * HEIGHT);                          // allocate memory for image data
  for (int i = 0; i < (WIDTH * HEIGHT); ++i) {                  // Generate pattern
    pImageData[i] = (unsigned char)((i % WIDTH)/4 % numColors); // stripe pattern (4 pixels per stripe)
  }

  // add frame to GIF
  initFrameConfig(&fConfig, pImageData);                         // initialize the frame-configuration
  cgif_addframe(pGIF, &fConfig);                                 // add a new frame to the GIF
  free(pImageData);                                              // free image data when frame is added

  // close GIF and free allocated space
  cgif_close(pGIF);
  return 0;
}