Advertisement
Guest User

CH14 Colors

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