ClarkeRubber

chessboard.c

May 13th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. /*
  2.  *  chessboard.c
  3.  *  create a 512x512 BMP of a chessboard, with user specified size (in pixels)
  4.  *  of the black and white squares on the board.  bottom right square must be white.
  5.  *
  6.  *  Created by Richard Buckland on 14/04/11.
  7.  *  Licensed under Creative Commons BY 3.0.
  8.  *
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13.  
  14. #define BYTES_PER_PIXEL 3
  15. #define BITS_PER_PIXEL (BYTES_PER_PIXEL*8)
  16. #define NUMBER_PLANES 1
  17. #define PIX_PER_METRE 2835
  18. #define MAGIC_NUMBER 0x4d42
  19. #define NO_COMPRESSION 0
  20. #define OFFSET 54
  21. #define DIB_HEADER_SIZE 40
  22. #define NUM_COLORS 0
  23.  
  24. #define SIZE 512
  25. #define BMP_FILE "chess.bmp"
  26.  
  27. #define WHITE 255
  28. #define BLACK 000
  29.  
  30. typedef unsigned char  bits8;
  31. typedef unsigned short bits16;
  32. typedef unsigned int   bits32;
  33.  
  34. void writeHeader (FILE *file);
  35.  
  36. int main (int argc, char *argv[]) {
  37.  
  38.    // check that the types have the size i'm relying on here
  39.    assert (sizeof(bits8)  == 1);
  40.    assert (sizeof(bits16) == 2);
  41.    assert (sizeof(bits32) == 4);
  42.  
  43.    FILE *outputFile;
  44.    int squareSize;
  45.  
  46.    outputFile = fopen(BMP_FILE, "wb");
  47.    assert ((outputFile!=NULL) && "Cannot open file");
  48.  
  49.    writeHeader(outputFile);
  50.    printf("HEADER WRITTEN\n");
  51.  
  52.    printf ("Enter square size (must be a factor of %d): \n", SIZE);
  53.    scanf ("%d", &squareSize);
  54.    assert (SIZE % squareSize == 0);
  55.  
  56.  
  57.    int numBytes = (SIZE * SIZE * BYTES_PER_PIXEL);
  58.    int pos = 0;
  59.    bits8 byte = BLACK;
  60.    while (pos < numBytes) {
  61.       fwrite (&byte, sizeof byte, 1, outputFile);
  62.       if ((pos + 1) % (3*squareSize) == 0 && (pos + 1) % (3*squareSize * SIZE) != 0) {
  63.          if (byte == WHITE) {
  64.             byte = BLACK;
  65.          } else {
  66.             byte = WHITE;
  67.          }
  68.       }
  69.       pos++;
  70.    }
  71.  
  72.    fclose(outputFile);
  73.  
  74.    return EXIT_SUCCESS;
  75. }
  76.  
  77. void writeHeader (FILE *file) {
  78.    assert(sizeof (bits8) == 1);
  79.    assert(sizeof (bits16) == 2);
  80.    assert(sizeof (bits32) == 4);
  81.  
  82.    bits16 magicNumber = MAGIC_NUMBER;
  83.    fwrite (&magicNumber, sizeof magicNumber, 1, file);
  84.  
  85.    bits32 fileSize = OFFSET + (SIZE * SIZE * BYTES_PER_PIXEL);
  86.    fwrite (&fileSize, sizeof fileSize, 1, file);
  87.  
  88.    bits32 reserved = 0;
  89.    fwrite (&reserved, sizeof reserved, 1, file);
  90.  
  91.    bits32 offset = OFFSET;
  92.    fwrite (&offset, sizeof offset, 1, file);
  93.  
  94.    bits32 dibHeaderSize = DIB_HEADER_SIZE;
  95.    fwrite (&dibHeaderSize, sizeof dibHeaderSize, 1, file);
  96.  
  97.    bits32 width = SIZE;
  98.    fwrite (&width, sizeof width, 1, file);
  99.  
  100.    bits32 height = SIZE;
  101.    fwrite (&height, sizeof height, 1, file);
  102.  
  103.    bits16 planes = NUMBER_PLANES;
  104.    fwrite (&planes, sizeof planes, 1, file);
  105.  
  106.    bits16 bitsPerPixel = BITS_PER_PIXEL;
  107.    fwrite (&bitsPerPixel, sizeof bitsPerPixel, 1, file);
  108.  
  109.    bits32 compression = NO_COMPRESSION;
  110.    fwrite (&compression, sizeof compression, 1, file);
  111.  
  112.    bits32 imageSize = (SIZE * SIZE * BYTES_PER_PIXEL);
  113.    fwrite (&imageSize, sizeof imageSize, 1, file);
  114.  
  115.    bits32 hResolution = PIX_PER_METRE;
  116.    fwrite (&hResolution, sizeof hResolution, 1, file);
  117.  
  118.    bits32 vResolution = PIX_PER_METRE;
  119.    fwrite (&vResolution, sizeof vResolution, 1, file);
  120.  
  121.    bits32 numColors = NUM_COLORS;
  122.    fwrite (&numColors, sizeof numColors, 1, file);
  123.  
  124.    bits32 importantColors = NUM_COLORS;
  125.    fwrite (&importantColors, sizeof importantColors, 1, file);
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment