Advertisement
Titanyv47

Kursach

Dec 14th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <getopt.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #define BUFFER 100
  8. #pragma pack(push,1)
  9. typedef struct
  10. {
  11.     unsigned short signature;
  12.     unsigned int filesize;
  13.     unsigned short reserved1;
  14.     unsigned short reserved2;
  15.     unsigned int pixelArrOffset;
  16. } BitmapFileHeader;
  17.  
  18. typedef struct
  19. {
  20.     unsigned int headerSize;
  21.     unsigned int width;
  22.     unsigned int height;
  23.     unsigned short planes;
  24.     unsigned short bitsPerPixel;
  25.     unsigned int compression;
  26.     unsigned int imageSize;
  27.     unsigned int xPixelsPerMeter;
  28.     unsigned int yPixelsPerMeter;
  29.     unsigned int colorsInColorTable;
  30.     unsigned int importantColorCount;
  31. } BitmapInfoHeader;
  32.  
  33. typedef struct
  34. {
  35.     unsigned char b;
  36.     unsigned char g;
  37.     unsigned char r;
  38. } Rgb;
  39.  
  40. #pragma pack(pop)
  41.  
  42. typedef struct{
  43.     unsigned int X;
  44.     unsigned int Y;
  45.     unsigned int width_line;
  46.     Rgb color;
  47.  
  48. }Split;
  49.  
  50. typedef struct{
  51.     char* comanda;
  52.     Rgb arr1;
  53.     Rgb arr2;
  54.     char* path;
  55.     char colorrgb;
  56.     unsigned int value;
  57.     Split split;
  58. }Box;
  59.  
  60. static const char *optString1 = "m:c:s";
  61. static const char *optString2 = "Il:o:vh?";
  62. static const char *optString3 = "r:v:s";
  63. static const char *sep1 = "/";
  64.  
  65. void printFileHeader(BitmapFileHeader header){
  66.     printf("signature:\t%x (%hu)\n", header.signature, header.signature);
  67.     printf("filesize:\t%x (%u)\n", header.filesize, header.filesize);
  68.     printf("reserved1:\t%x (%hu)\n", header.reserved1, header.reserved1);
  69.     printf("reserved2:\t%x (%hu)\n", header.reserved2, header.reserved2);
  70.     printf("pixelArrOffset:\t%x (%u)\n", header.pixelArrOffset, header.pixelArrOffset);
  71. }
  72.  
  73. void printInfoHeader(BitmapInfoHeader header){
  74.     printf("headerSize:\t%x (%u)\n", header.headerSize, header.headerSize);
  75.     printf("width:     \t%x (%u)\n", header.width, header.width);
  76.     printf("height:    \t%x (%u)\n", header.height, header.height);
  77.     printf("planes:    \t%x (%hu)\n", header.planes, header.planes);
  78.     printf("bitsPerPixel:\t%x (%hu)\n", header.bitsPerPixel, header.bitsPerPixel);
  79.     printf("compression:\t%x (%u)\n", header.compression, header.compression);
  80.     printf("imageSize:\t%x (%u)\n", header.imageSize, header.imageSize);
  81.     printf("xPixelsPerMeter:\t%x (%u)\n", header.xPixelsPerMeter, header.xPixelsPerMeter);
  82.     printf("yPixelsPerMeter:\t%x (%u)\n", header.yPixelsPerMeter, header.yPixelsPerMeter);
  83.     printf("colorsInColorTable:\t%x (%u)\n", header.colorsInColorTable, header.colorsInColorTable);
  84.     printf("importantColorCount:\t%x (%u)\n", header.importantColorCount, header.importantColorCount);
  85. }
  86.  
  87. void del_arr(Rgb** arr,int H){
  88.     for(int i=0;i<H;i++){
  89.        free(arr[i]);
  90.     }
  91.     free(arr);
  92. }
  93.  
  94. void info_file(char* str){
  95.     FILE* fp;
  96.     if(!(fp=fopen(str, "rb"))){
  97.         printf("Не удалось открыть файл");
  98.         exit(1);
  99.     }
  100.     else{
  101.         BitmapFileHeader bmfh;
  102.         BitmapInfoHeader bmif;
  103.         fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
  104.         fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
  105.     printFileHeader(bmfh);
  106.     printInfoHeader(bmif);
  107.     }
  108. }
  109.  
  110. void color_swap(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,Rgb arr1,Rgb arr2,char* path){/*"Замена цвета"*/
  111.     unsigned int H = bmif.height;
  112.     unsigned int W = bmif.width;
  113.     FILE *ff;
  114.     if(!(ff = fopen(path, "wb"))) printf("\n----------Не удалось создать файл!-----------\n");
  115.     fwrite(&bmfh, 1, sizeof(BitmapFileHeader),ff);
  116.     fwrite(&bmif, 1, sizeof(BitmapInfoHeader),ff);
  117.     for(int i=0; i<H; i++){
  118.             for(int j=0; j<W; j++){
  119.                 if ((arr[i][j].r==arr1.r)&(arr[i][j].g==arr1.g)&(arr[i][j].b==arr1.b)){
  120.                     arr[i][j].r=arr2.r;
  121.                     arr[i][j].g=arr2.g;
  122.                     arr[i][j].b=arr2.b;    
  123.                 }
  124.         }
  125.         fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
  126.     }
  127.     fclose(ff);
  128.     for(int i=0; i<H; i++){
  129.             for(int j=0; j<W; j++){
  130.                 if ((arr[i][j].r==arr2.r)&(arr[i][j].g==arr2.g)&(arr[i][j].b==arr2.b)){
  131.                     arr[i][j].r==arr1.r;
  132.                     arr[i][j].g==arr1.g;
  133.                     arr[i][j].b==arr1.b;
  134.                 }
  135.         }
  136.     }  
  137.  
  138.  
  139. }
  140.  
  141.  
  142.  
  143. void filter_rgb_component(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,char component,int value,char* path){/*"фильтер rgb компонент"*/
  144.     unsigned int H = bmif.height;
  145.     unsigned int W = bmif.width;
  146.     FILE *ff;
  147.     if(!(ff = fopen(path, "wb"))) printf("\n----------Не удалось создать файл!-----------\n");
  148.     fwrite(&bmfh, 1, sizeof(BitmapFileHeader),ff);
  149.     fwrite(&bmif, 1, sizeof(BitmapInfoHeader),ff);
  150.  
  151.     switch(component){
  152.     case 'r':
  153.             for(int i=0; i<H; i++){
  154.                 for(int j=0; j<W; j++){
  155.                     arr[i][j].r=value;
  156.             }
  157.             fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
  158.         }
  159.             break;
  160.     case 'g':
  161.         for(int i=0; i<H; i++){
  162.                 for(int j=0; j<W; j++){
  163.                     arr[i][j].g=value;
  164.             }
  165.             fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
  166.         }
  167.             break;
  168.     case 'b':
  169.         for(int i=0; i<H; i++){
  170.                 for(int j=0; j<W; j++){
  171.                     arr[i][j].b=value;
  172.             }
  173.             fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
  174.         }
  175.             break;
  176.         default:
  177.              printf("Неверные значения!!!\n");  
  178.     }  
  179.  
  180.     fclose(ff);
  181. }
  182.  
  183. void info_author(){
  184.     printf("-----Имя: Семён-----\n-----Фамилия: Емельянов-----\n-----Дата рождения: 30.05.2001-----\n");
  185. }
  186.  
  187. void help_assistant(){
  188.     printf("---------Привет!Я помощник!--------\nВам доступны следующие команды:\ninfofile y.bmp- информация о файле\ninfoauthor-информация о авторе\nmycolor --m r/g/b --c r/g/b --s t.bmp y.bmp  - функция замены одного цвета на другой\nmyfilter --r a --v f --s t.bmp y.bmp - функцция замены rgb-компоненты\nmysplit --x a --y b --s t.bmp y.bmp - функцция разрезания изображения\n");
  189.  
  190. }
  191.  
  192. char* itoa(int number) {
  193.  
  194.     int i = number;
  195.     int num_len = 1;
  196.     int sym;
  197.  
  198.     while (i > 9) {
  199.  
  200.         i = i / 10;
  201.         num_len++;
  202.     }
  203.  
  204.     char* num_str = (char*)calloc(num_len + 1, sizeof(char));
  205.     num_str[num_len] = '\0';
  206.     i = number;
  207.  
  208.     for (int j = (num_len - 1); j > -1; j--) {
  209.  
  210.         sym = i % 10;
  211.  
  212.         if (sym == 0) num_str[j] = '0';
  213.         if (sym == 1) num_str[j] = '1';
  214.         if (sym == 2) num_str[j] = '2';
  215.         if (sym == 3) num_str[j] = '3';
  216.         if (sym == 4) num_str[j] = '4';
  217.         if (sym == 5) num_str[j] = '5';
  218.         if (sym == 6) num_str[j] = '6';
  219.         if (sym == 7) num_str[j] = '7';
  220.         if (sym == 8) num_str[j] = '8';
  221.         if (sym == 9) num_str[j] = '9';
  222.  
  223.         i = i / 10;
  224.     }
  225.  
  226.     return num_str;
  227. }
  228.  
  229. void image_splitting(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,Split split,char* path_out){/*"разрезание изображения"*/
  230.     char path[BUFFER];
  231.     char* ind;
  232.     FILE* file;
  233.     int W;
  234.     int H;
  235.     unsigned int H_s = bmif.height;
  236.     unsigned int W_s = bmif.width;
  237.     if (split.X != 0) W = bmif.width / split.X;
  238.     else { W = bmif.width;split.X = 1; }
  239.     if (split.Y != 0) H = bmif.height / split.Y;
  240.     else { H = bmif.height; split.Y = 1; }
  241.     bmif.width = W;
  242.     bmif.height = H;
  243.     int cout = 0;
  244.     for (int i = 0; i < split.Y; i++) {
  245.     for (int j = 0; j < split.X; j++) {
  246.  
  247.         ind = itoa(cout);
  248.            
  249.         strcpy(path, path_out);
  250.         strtok(path, ".");
  251.         strcat(path, ind); strcat(path, ".bmp");
  252.         free(ind);
  253.  
  254.         if (!(file = fopen(path, "wb"))) exit(1);
  255.  
  256.         fwrite(&bmfh, 1, sizeof(BitmapFileHeader), file);
  257.         fwrite(&bmif, 1, sizeof(BitmapInfoHeader), file);
  258.         for (int k = i * H; k < (i + 1) * H; k++) {
  259.             for (int l = j * W; l < (j + 1) * W; l++) fwrite(&arr[k][l], 1, sizeof(Rgb), file);
  260.             fwrite("\0", 1, (W * 3) % 4, file);
  261.         }
  262.  
  263.         fclose(file);cout++;
  264.     }
  265.     }
  266.     bmif.height=H_s;
  267.     bmif.width=W_s;
  268.  
  269.  
  270. }
  271.  
  272.  
  273. int main(int argc, char *argv[]){
  274.     if(argc==1){
  275.         printf("Введите -help или --h,чтобы узнать доступный функионал!!!\n");
  276.         return 0;
  277.     }
  278.  
  279.     Box box;
  280.     char strpath[BUFFER]="out.bmp";
  281.     box.arr1.r=255;box.arr1.g=255;box.arr1.b=255;
  282.     box.arr2.r=100;box.arr2.g=100;box.arr2.b=100;    
  283.     box.path = strpath;
  284.     box.colorrgb = 'b';
  285.     box.value = 0;
  286.     box.split.X = 1;
  287.     box.split.Y = 1;
  288.     box.split.width_line = 1;
  289.     box.split.color.r=255;box.split.color.g=255;box.split.color.b=255;
  290.     int optIdx;    
  291.     if(!(strcmp(argv[1],"--help"))||!(strcmp(argv[1],"-h"))){
  292.         help_assistant();
  293.         return 0;
  294.     }
  295.     if(!(strcmp(argv[1],"infoauthor"))||!(strcmp(argv[1],"-i"))){
  296.         info_author();
  297.         return 0;
  298.     }
  299.     if(!(strcmp(argv[1],"infofile"))){
  300.         info_file(argv[argc-1]);
  301.         return 0;
  302.     }
  303.    
  304.     int opt;
  305.     if(!(strcmp(argv[1],"mycolor"))){
  306.         char* str;
  307.         int count=0;
  308.         static struct option long_opt[] = {
  309.         {"mycolor", 1, 0, 'm'},
  310.         {"color", 1, 0, 'c'},
  311.         {"savefile",1, 0, 's'},
  312.         {0,0,0,0}
  313.         };
  314.  
  315.         FILE* fp;
  316.         if(!(fp=fopen(argv[argc-1], "rb"))){
  317.             printf("Не удалось открыть файл\n");
  318.             exit(1);
  319.         }
  320.         BitmapFileHeader bmfh;
  321.         BitmapInfoHeader bmif;
  322.         fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
  323.         fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
  324.         Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
  325.         for(int i=0; i<bmif.height; i++){
  326.         arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
  327.         fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
  328.         }  
  329.         while((opt=getopt_long(argc, argv,optString1, long_opt, &optIdx))!=-1){
  330.             switch(opt){
  331.             case 'm':
  332.             str=strtok(optarg,sep1);
  333.                     while(str!=NULL){
  334.                         if (count==0) box.arr1.r=atoi(str);
  335.                         if (count==1) box.arr1.g=atoi(str);
  336.                         if (count==2) box.arr1.b=atoi(str);
  337.                         count++;
  338.                 str=strtok(NULL,sep1);
  339.                     }
  340.                     count=0;
  341.                     break;
  342.                 case 'c':
  343.                     while(str!=NULL){
  344.                         if (count==0) box.arr2.r=atoi(str);
  345.                         if (count==1) box.arr2.g=atoi(str);
  346.                         if (count==2) box.arr2.b=atoi(str);
  347.                         count++;
  348.                 str=strtok(NULL,sep1);
  349.                     }
  350.                     count=0;
  351.                     break;
  352.                 case 's':
  353.                     box.path = optarg;
  354.                     break;
  355.                 default :
  356.                     printf("Неверно введена команда!!!\n");
  357.                     del_arr(arr,bmif.height);
  358.                     return -1;
  359.             }
  360.         }
  361.         color_swap(bmif,bmfh,arr,box.arr1,box.arr2,box.path);
  362.         del_arr(arr,bmif.height);
  363.         return 0;
  364.     }
  365.  
  366.  
  367.     if(!(strcmp(argv[1],"mysplit"))){
  368.         static struct option long_opt[] = {
  369.             {"x_argument", 1, 0, 'x'},
  370.             {"y_argument", 1, 0, 'y'},
  371.             {"savefile",1, 0, 's'},
  372.             {0,0,0,0}
  373.         };
  374.         FILE* fp;
  375.         if(!(fp=fopen(argv[argc-1], "rb"))){
  376.             printf("Не удалось открыть файл\n");
  377.             exit(1);
  378.         }
  379.         BitmapFileHeader bmfh;
  380.         BitmapInfoHeader bmif;
  381.         fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
  382.         fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
  383.         Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
  384.         for(int i=0; i<bmif.height; i++){
  385.         arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
  386.         fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
  387.         }
  388.         while((opt=getopt_long(argc, argv,optString3, long_opt, &optIdx))!=-1){
  389.             switch(opt){
  390.             case 'x':
  391.                     box.split.X=atoi(optarg);
  392.                     break;
  393.                 case 'y':
  394.             box.split.Y=atoi(optarg);
  395.                     break;
  396.                 case 's':
  397.                     box.path =optarg;
  398.                     break;
  399.                 default :
  400.                     printf("Неверно введена команда!!!\n");
  401.                     del_arr(arr,bmif.height);
  402.                     return -1;
  403.             }
  404.         }
  405.     image_splitting(bmif,bmfh,arr,box.split,box.path);
  406.         del_arr(arr,bmif.height);
  407.         return 0;
  408.     }
  409.  
  410.  
  411.     if(!(strcmp(argv[1],"myfilter"))){
  412.         static struct option long_opt[] = {
  413.         {"rgb", 1, 0, 'r'},
  414.         {"value", 1, 0, 'v'},
  415.         {"savefile",1, 0, 's'},
  416.         {0,0,0,0}
  417.         };
  418.         FILE* fp;
  419.         if(!(fp=fopen(argv[argc-1], "rb"))){
  420.             printf("Не удалось открыть файл\n");
  421.             exit(1);
  422.         }
  423.         BitmapFileHeader bmfh;
  424.         BitmapInfoHeader bmif;
  425.         fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
  426.         fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
  427.         Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
  428.         for(int i=0; i<bmif.height; i++){
  429.         arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
  430.         fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
  431.         }
  432.         while((opt=getopt_long(argc, argv,optString3, long_opt, &optIdx))!=-1){
  433.             switch(opt){
  434.             case 'r':
  435.                     box.colorrgb=*optarg;
  436.                     break;
  437.                 case 'v':
  438.             box.value=atoi(optarg);
  439.                     break;
  440.                 case 's':
  441.                     box.path =optarg;
  442.                     break;
  443.                 default :
  444.                     printf("Неверно введена команда!!!\n");
  445.                     del_arr(arr,bmif.height);
  446.                     return -1;
  447.             }
  448.         }
  449.     filter_rgb_component(bmif,bmfh,arr,box.colorrgb,box.value,box.path);
  450.         del_arr(arr,bmif.height);
  451.         return 0;
  452.     }
  453.     else{
  454.        printf("Неверно ввели команду!!!\n");
  455.        return 0;
  456.     }
  457.     return 0;
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement