Guest User

CH14 Colors

a guest
Jun 20th, 2011
1,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. /*
  2.  * Tuenti Contest
  3.  * Challenge 14 - Colors
  4.  * Author: Pedro Antonio Pardal Jimena
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <fcntl.h>
  11. #include <sys/mman.h>
  12. #include <sys/types.h>
  13. #include <string.h>
  14.  
  15. struct rgb24
  16. {
  17.     unsigned char b, g, r;
  18. } __attribute__((packed));
  19.  
  20. struct bmp
  21. {
  22.     /* BITMAPFILEHEADER */
  23.     uint16_t    bfType;
  24.     uint32_t    bfSize;
  25.     uint16_t    bfReserved1;
  26.     uint16_t    bfReserved2;
  27.     uint32_t    bfOffBits;
  28.    
  29.     /* BITMAPINFOHEADER */
  30.     uint32_t    biSize;
  31.     uint32_t    biWidth;
  32.     uint32_t    biHeight;
  33.     uint16_t    biPlanes;
  34.     uint16_t    biBitCount;
  35.     uint32_t    biCompression;
  36.     uint32_t    biSizeImage;
  37.     uint32_t    biXPelsPerMeter;
  38.     uint32_t    biYPelsPerMeter;
  39.     uint32_t    biClrUsed;
  40.     uint32_t    biClrImportant;
  41.    
  42.     struct rgb24 data[];
  43. } __attribute__((packed));
  44.  
  45. static struct bmp* read_image()
  46. {
  47.     int fd;
  48.     int longitud;
  49.     struct bmp *img;
  50.     struct stat stbuf;
  51.    
  52.     if ((fd = open("trabaja.bmp", O_RDONLY)) == -1)
  53.     {
  54.         perror("open");
  55.         exit(-1);
  56.     }
  57.    
  58.     if (fstat(fd, &stbuf) == -1) {
  59.         perror("fstat");
  60.         exit(-1);
  61.     }
  62.     longitud = stbuf.st_size;
  63.    
  64.     if ((img = (struct bmp *)mmap((caddr_t) 0, longitud, PROT_READ, MAP_SHARED,
  65.                                   fd, 0)) == MAP_FAILED)
  66.     {
  67.         perror("mmap");
  68.         exit(-1);
  69.     }
  70.    
  71.     close(fd);
  72.    
  73.     return img;
  74. }
  75.  
  76. static void close_image(struct bmp *img)
  77. {
  78.     munmap(img, img->bfSize);
  79. }
  80.  
  81. static int compute_sum(struct bmp *img, char color, int line)
  82. {
  83.     int i, base, sum;
  84.    
  85.     base = (img->biHeight - line - 1) * img->biWidth;
  86.     sum = 0;
  87.        
  88.     for (i = 0; i < img->biWidth; i++)
  89.     {
  90.         switch (color)
  91.         {
  92.             case 'R':
  93.                 sum += img->data[base + i].r;
  94.                 break;
  95.             case 'G':
  96.                 sum += img->data[base + i].g;
  97.                 break;
  98.             case 'B':
  99.                 sum += img->data[base + i].b;
  100.                 break;
  101.         }
  102.     }
  103.    
  104.     return sum + 1;
  105. }
  106.  
  107. #define BUFFER_SIZE 0x100
  108.  
  109. int main(int argc, char *argv[])
  110. {
  111.     struct bmp *img;
  112.     int i;
  113.     char buffer[BUFFER_SIZE];
  114.     char *tok;
  115.     char color;
  116.     int line;
  117.     int sum;
  118.    
  119.     img = read_image();
  120.    
  121.     while (fgets(buffer, BUFFER_SIZE, stdin) != NULL)
  122.     {
  123.         color = buffer[0];
  124.         line = atoi(&buffer[1]);
  125.         sum = compute_sum(img, color, line);
  126.        
  127.         fprintf(stdout, "%d\n", sum);
  128.     }
  129.    
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment