Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. const n=150 ;
  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.             uint8_t b, g, r;
  28.  
  29. } rgb;
  30.  
  31. #pragma pack(pop)
  32.  
  33.  
  34. rgb **arraymake(uint32_t width, uint32_t height)
  35. {
  36.     rgb **pixels = (rgb **)malloc (height * sizeof(rgb *));
  37.     for (uint32_t  row = 0; row < height; ++row)
  38.     {
  39.         pixels[row] = (rgb *)malloc(width * sizeof(rgb));
  40.     }
  41.  
  42.     return pixels;
  43. }
  44.  
  45. int main(int argc, char *argv[]) {
  46.     if (argc < 3) {
  47.         printf("%s <bmp in> <bmp out>\n", argv[0]);
  48.         exit(1);
  49.     }
  50.  
  51.  
  52.     FILE *bmf = fopen(argv[1], "rb");
  53.     FILE *bmout = fopen(argv[2], "wb");
  54.  
  55.  
  56.     BmpInfoHeader bih;
  57.     fread(&bih, sizeof bih, 1, bmf);
  58.     BmpHeader bh = bih.bh;
  59.  
  60.  
  61.     if (bih.size != 40 || bih.planes != 1 || bh.reserved != 0 || bih.compression > 2) {
  62.         printf("BMP file corrupted");
  63.         exit(2);
  64.     }
  65.     if (bih.bpp != 24) {
  66.         printf("bpp %hd not supported\n", bih.bpp);
  67.         exit(3);
  68.     }
  69.     fseek(bmf, bih.bh.offset, SEEK_SET);
  70.     fwrite(&bih, sizeof(bih), 1, bmout);
  71.  
  72.     //PixelData PD;
  73.     //PD.arr=arraymake(bih.width, bih.height);
  74.    /* PD.arr = (rgb **) malloc(bih.height * sizeof(rgb *));
  75.     for (int row = 0; row < bih.height; ++row)
  76.     {
  77.         PD.arr[row] = (rgb *) malloc(bih.width * sizeof(rgb));
  78.     }
  79. */
  80.     rgb PD[bih.height][bih.width];
  81.  
  82.  
  83.     for (uint32_t  i = 0; i < bih.height; i++) {
  84.         for (uint32_t  j = 0; j < bih.width; j++) {
  85.             fread(&PD[i][j], sizeof(rgb), 1, bmf);
  86.         }
  87.     }
  88.     for (uint32_t  i = 0; i < bih.height ; i++) {
  89.         for (uint32_t  j = 0; j < (bih.width); j++) {
  90.  
  91.                int32_t  temp = j - n;
  92.                if (temp > bih.width)
  93.                    temp=bih.width-1;
  94.                if (temp <= 0)
  95.                    temp=0;/*if (temp>0)
  96.                        temp = bih.width+temp;
  97.                    else
  98.                        temp=bih.width-temp;
  99. */
  100.  
  101.             rgb temp_pix_second;
  102.             rgb temp_pix_third;
  103.             temp_pix_second.b=PD[i][j].b;
  104.             temp_pix_second.g=PD[i][j].g;
  105.             if (temp==0)
  106.                 temp_pix_second.r=(uint8_t)0;
  107.             else{
  108.                 temp_pix_second.r = PD[i][temp].r;
  109.                 PD[i][temp].r=(uint8_t)0;
  110.             }
  111.             temp_pix_third=temp_pix_second;
  112.  
  113.             /*if (PD[i][j].r==(uint8_t)0)
  114.                 temp_pix_third.r=(uint8_t)0;
  115.             else
  116.                 temp_pix_third.r=PD[i][temp].r;
  117.             */
  118.             fwrite(&temp_pix_third, sizeof(rgb),1,bmout);
  119.         }
  120.     }
  121.  
  122.   /*  for (uint32_t  i = 0; i < bih.height ; i++) {
  123.         for (uint32_t  j = 0; j < bih.width ; j++) {
  124.             fwrite(&PD.arr[i][j], sizeof(rgb),1,bmout);
  125.         }
  126.     }
  127. */
  128.  
  129.   //FILE *temp = fopen(argv[1],"rb");
  130. /*
  131.         unsigned char info[54];
  132.         fread(info, sizeof(unsigned char), 54, temp); // read the 54-byte header
  133.  
  134.         // extract image height and width from header
  135.         int width = *(int*)&info[18];
  136.         int height = *(int*)&info[22];
  137.  
  138.         int size = 3 * width * height;
  139.         unsigned char data[size];
  140.        //unsigned char **data= new unsigned char [size]="\0"; // allocate 3 bytes per pixel
  141.        for (int ii=0;ii<size;ii++)
  142.         fread(data, sizeof(unsigned char), 1, temp); // read the rest of the data at once
  143.  
  144.         FILE *bmout=fopen(argv[2],"wb");
  145.         fwrite(&bih, sizeof bih, 1, bmout);
  146.  
  147.  
  148.     for (int i=0;i<bih.height;i++)
  149.         {
  150.             for (int j=0;j<bih.width; j++)
  151.             {
  152.                 data[3*(i*bih.width+j)+2]=data[3*(i*bih.width+j)+1]+n;
  153.                 fwrite(&data[3*(i*bih.width+j)],sizeof(unsigned char),1,bmout);
  154.                 fwrite(&data[3*(i*bih.width+j)+1],sizeof(unsigned char),1,bmout);
  155.                 fwrite(&data[3*(i*bih.width+j)+2],sizeof(unsigned char),1,bmout);
  156.             }
  157.         }
  158. */
  159.    /*
  160.     FILE *bmout=fopen(argv[2],"wb");
  161.     fwrite(&bih, sizeof bih, 1, bmout);
  162.     for (int i=0;i<bih.width*bih.height;i++)
  163.     {
  164.         rgb pixel;
  165.         fread(&pixel,sizeof pixel, 1, bmf);//1 15 57
  166.         if(pixel.g+n<bih.width)
  167.             pixel.r = pixel.g + n;
  168.         else
  169.             pixel.r=bih.width;
  170.         for(int j=0;j<3;j++)
  171.         {
  172.             uint8_t *subpix=&pixel.start+j;
  173.  
  174.  
  175.         }
  176.         fwrite(&pixel,sizeof pixel,1,bmout);
  177.  
  178.     }
  179.     */
  180.  
  181.     fclose(bmout);
  182.     fclose(bmf);
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement