Advertisement
Guest User

Untitled

a guest
Apr 27th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct _color_table {
  5.     unsigned char R;
  6.     unsigned char G;
  7.     unsigned char B;
  8. } _color_table;
  9.  
  10. int search_pattern(FILE *INput) {
  11.     char pattern[11] = { 0x06, 'c', 'o', 'l', 'o', 'r', 's', 0x00, 0x00, 0x40, 0x00 };
  12.     char get_pattern[11] = { 0 };
  13.  
  14.     while (fread(&get_pattern,sizeof(char),11,INput)) {
  15.         if (feof(INput)) {
  16.             break;
  17.         }
  18.         if (!strcmp(get_pattern,pattern)) {
  19.             return 1;
  20.         }
  21.         fseek(INput, -10, SEEK_CUR);
  22.     }
  23.     return 0;
  24. }
  25.  
  26. int main(int argc, char **argv) {
  27.     unsigned char BMP_HEADER[54] = { 0x42, 0x4D, 0x36, 0xC0, 0x00, 0x00, 0x00, 0x00,
  28.                                    0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
  29.                                    0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00,
  30.                                    0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
  31.                                    0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0xC4, 0x0E,
  32.                                    0x00, 0x00, 0xC4, 0x0E, 0x00, 0x00, 0x00, 0x00,
  33.                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  34.     _color_table color_table[] = {
  35.                                     { 0, 0, 0},             //  0 NONE
  36.                                     { 127, 178, 56 },       //  1 GRASS
  37.                                     { 247, 233, 163 },      //  2 SAND
  38.                                     { 199, 199, 199 },      //  3 WOOL
  39.                                     { 255, 0, 0 },          //  4 FIRE
  40.                                     { 160, 160, 255 },      //  5 ICE
  41.                                     { 167, 167, 167 },      //  6 METAL
  42.                                     { 0, 124, 0 },          //  7 PLANT
  43.                                     { 255, 255, 255 },      //  8 SNOW
  44.                                     { 164, 168, 184 },      //  9 CLAY
  45.                                     { 151, 109, 77 },       // 10 DIRT
  46.                                     { 112, 112, 112 },      // 11 STONE
  47.                                     { 64, 64, 255 },        // 12 WATER
  48.                                     { 143, 119, 72 },       // 13 WOOD
  49.                                     { 255, 252, 245 },      // 14 QUARTZ
  50.                                     { 216, 127, 51 },       // 15 COLOR_ORANGE
  51.                                     { 178, 76, 216 },       // 16 COLOR_MAGENTA
  52.                                     { 102, 153, 216 },      // 17 COLOR_LIGHT_BLUE
  53.                                     { 229, 229, 51 },       // 18 COLOR_YELLOW
  54.                                     { 127, 204, 25 },       // 19 COLOR_LIGHT_GREEN
  55.                                     { 242, 127, 165 },      // 20 COLOR_PING
  56.                                     { 76, 76, 76 },         // 21 COLOR_GRAY
  57.                                     { 153, 153, 153 },      // 22 COLOR_LIGHT_GRAY
  58.                                     { 76, 127, 153 },       // 23 COLOR_CYAN
  59.                                     { 127, 63, 178 },       // 24 COLOR_PURPLE
  60.                                     { 51, 76, 178 },        // 25 COLOR_BLUE
  61.                                     { 102, 76, 51 },        // 26 COLOR_BROWN
  62.                                     { 102, 127, 51 },       // 27 COLOR_GREEN
  63.                                     { 153, 51, 51 },        // 28 COLOR_RED
  64.                                     { 25, 25, 25 },         // 29 COLOR_BLACK
  65.                                     { 250, 238, 77 },       // 30 GOLD
  66.                                     { 92, 219, 213 },       // 31 DIAMOND
  67.                                     { 74, 128, 255 },       // 32 LAPIS
  68.                                     { 0, 217, 58 },         // 33 EMERALD
  69.                                     { 129, 86, 49 },        // 34 PODZOL
  70.                                     { 112, 2, 0 },          // 35 NETHER
  71.                                     { 209, 177, 161 },      // 36 TERRACOTTA_WHIE
  72.                                     { 159, 82, 36 },        // 37 TERRACOTTA_ORANGE
  73.                                     { 149, 87, 108 },       // 38 TERRACOTTA_MAGENTA
  74.                                     { 112, 108, 138 },      // 39 TERRACOTTA_LIGHT_BLUE
  75.                                     { 186, 133, 36 },       // 40 TERRACOTTA_YELLOW
  76.                                     { 103, 117, 53 },       // 41 TERRACOTTA_LIGHT_GREEN
  77.                                     { 160, 77, 78 },        // 42 TERRACOTTA_PINK
  78.                                     { 57, 41, 35 },         // 43 TERRACOTTA_GRAY
  79.                                     { 135, 107, 98 },       // 44 TERRACOTTA_LIGHT_GRAY
  80.                                     { 87, 92, 92 },         // 45 TERRACOTTA_CYAN
  81.                                     { 122, 73, 88 },        // 46 TERRACOTTA_PURPLE
  82.                                     { 76, 62, 92 },         // 47 TERRACOTTA_BLUE
  83.                                     { 76, 50, 35 },         // 48 TERRACOTTA_BROWN
  84.                                     { 76, 82, 42 },         // 49 TERRACOTTA_GREEN
  85.                                     { 142, 60, 46 },        // 50 TERRACOTTA_RED
  86.                                     { 37, 22, 16 }          // 51 TERRACOTTA_BLACK
  87.     };
  88.     unsigned char colors[16384] = { 0 };
  89.     FILE *INput = NULL;
  90.     FILE *OUTput = NULL;
  91.     unsigned int shade_ID = 0;
  92.     unsigned int base_color_ID = 0;
  93.     int i = 0;
  94.     int j = 0;
  95.     char output[256] = { 0 };
  96.  
  97.     strcpy(output, argv[1]);
  98.     strcat(output, ".bmp");
  99.  
  100.     INput = fopen(argv[1],"rb");
  101.     if (INput == NULL) {
  102.         printf("Could not open: %s\n",argv[1]);
  103.         return -1;
  104.     }
  105.  
  106.     if (search_pattern(INput)) {
  107.         printf("Pattern found\n");
  108.     }
  109.     else {
  110.         printf("Pattern not found\n");
  111.         fclose(INput);
  112.         return -1;
  113.     }
  114.     fread(&colors,sizeof(char),16384,INput);
  115.     fclose(INput);
  116.  
  117.     OUTput = fopen(output,"wb");
  118.     if (OUTput == NULL) {
  119.         printf("Could not open: %s\n",output);
  120.         return -1;
  121.     }
  122.     fwrite(BMP_HEADER, sizeof(char),54,OUTput);
  123.  
  124.     j = 127;
  125.     while (j >= 0) {
  126.     i = 128*j;
  127.         while (i < 128*(j+1)){
  128.  
  129.             base_color_ID = colors[i]/4;
  130.             shade_ID = colors[i]%4;
  131.             switch (shade_ID) {
  132.                 case 0 :
  133.                     fputc((color_table[base_color_ID].B*180)/255, OUTput);
  134.                     fputc((color_table[base_color_ID].G*180)/255, OUTput);
  135.                     fputc((color_table[base_color_ID].R*180)/255, OUTput);
  136.                     break;
  137.                 case 1 :
  138.                     fputc((color_table[base_color_ID].B*220)/255, OUTput);
  139.                     fputc((color_table[base_color_ID].G*220)/255, OUTput);
  140.                     fputc((color_table[base_color_ID].R*220)/255, OUTput);
  141.                     break;
  142.                 case 2 :
  143.                     fputc((color_table[base_color_ID].B*255)/255, OUTput);
  144.                     fputc((color_table[base_color_ID].G*255)/255, OUTput);
  145.                     fputc((color_table[base_color_ID].R*255)/255, OUTput);
  146.                     break;
  147.                 case 3 :
  148.                     fputc((color_table[base_color_ID].B*135)/255, OUTput);
  149.                     fputc((color_table[base_color_ID].G*135)/255, OUTput);
  150.                     fputc((color_table[base_color_ID].R*135)/255, OUTput);
  151.                     break;
  152.  
  153.             }
  154.              i++;
  155.         }
  156.         j--;
  157.     }
  158.     fclose(OUTput);
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement