Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. const n=100;
  5. #pragma pack(push, 1)
  6.  
  7. #define DEBUG 1
  8. #if DEBUG
  9. #define LOG(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
  10. #else
  11. #define LOG(...)
  12. #endif
  13.  
  14. typedef struct BmpHeader {
  15.     uint16_t signature;
  16.     uint32_t file_zize, reserved, offset;
  17. } BmpHeader;
  18.  
  19. typedef struct BmpInfoHeader {
  20.     BmpHeader bh;
  21.     uint32_t size, width, height;
  22.     uint16_t planes,bpp;
  23.     uint32_t compression, image_size, unused[2], colors, important;
  24. } BmpInfoHeader;
  25.  
  26. typedef  struct rgb {
  27.     union {
  28.         struct {
  29.             uint8_t b, g, r;
  30.         };
  31.         uint8_t  start;
  32.     };
  33. } rgb;
  34.  
  35. #pragma pack(pop)
  36.  
  37.  
  38.  
  39. int main(int argc, char *argv[]) {
  40.     if (argc < 3) {
  41.         printf("%s <bmp in> <bmp out>\n", argv[0]);
  42.         exit(1);
  43.     }
  44.  
  45.     //char *tmpEnd;
  46.     //int brighten = strtol(argv[1], &tmpEnd, 10);
  47.     //LOG("____________________________________\nPercentage: %d", brighten);
  48.     FILE *bmf = fopen(argv[1], "rb");
  49.     //FILE *bmpout = fopen(argv[3], "wb");
  50.     //LOG("Input file name: %s\nOutput file name: %s\n", argv[2], argv[3]);
  51.  
  52.     BmpInfoHeader bih;
  53.     fread(&bih, sizeof bih, 1, bmf);
  54.     BmpHeader bh = bih.bh;
  55.  
  56.  
  57.     if (bih.size != 40 || bih.planes != 1 || bh.reserved != 0 || bih.compression > 2) {
  58.         printf("BMP file corrupted");
  59.         exit(2);
  60.     }
  61.     if (bih.bpp != 24) {
  62.         printf("bpp %hd not supported\n", bih.bpp);
  63.         exit(3);
  64.     }
  65.  
  66.     LOG("size  %d, offset  0x%x\n", bih.bh.file_zize, bih.bh.offset);
  67.     LOG("bpp  %hd, compression  %ld, colors  %d\n", bih.bpp,bih.compression, bih.colors);
  68.     fseek(bmf, bih.bh.offset, SEEK_SET);
  69.  
  70.  
  71.  
  72.   FILE *temp=fopen(argv[1],"rb");
  73.  
  74.         unsigned char info[54];
  75.         fread(info, sizeof(unsigned char), 54, temp); // read the 54-byte header
  76.  
  77.         // extract image height and width from header
  78.         int width = *(int*)&info[18];
  79.         int height = *(int*)&info[22];
  80.  
  81.         int size = 3 * width * height;
  82.         unsigned char data [size]; // allocate 3 bytes per pixel
  83.         fread(data, sizeof(unsigned char), size, temp); // read the rest of the data at once
  84.  
  85.         FILE *bmout=fopen(argv[2],"wb");
  86.         //fwrite(&bih, sizeof bih, 1, bmout);
  87.         for(int i = 0; i < size; i += 3)
  88.         {
  89.             unsigned char tmp = data[i];
  90.             data[i] = data[i+2];
  91.             data[i+2] = tmp;
  92.         }
  93.  
  94.  
  95.    /*
  96.     FILE *bmout=fopen(argv[2],"wb");
  97.     fwrite(&bih, sizeof bih, 1, bmout);
  98.     for (int i=0;i<bih.width*bih.height;i++)
  99.     {
  100.         rgb pixel;
  101.         fread(&pixel,sizeof pixel, 1, bmf);//1 15 57
  102.         if(pixel.g+n<bih.width)
  103.             pixel.r = pixel.g + n;
  104.         else
  105.             pixel.r=bih.width;
  106.         for(int j=0;j<3;j++)
  107.         {
  108.             uint8_t *subpix=&pixel.start+j;
  109.  
  110.  
  111.         }
  112.         fwrite(&pixel,sizeof pixel,1,bmout);
  113.  
  114.     }
  115.     fclose(bmout);*/
  116.     fclose(bmf);
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement