Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <string.h>
- #include <getopt.h>
- #define BUFFER 100
- #pragma pack(push,1)
- typedef struct
- {
- unsigned short signature;
- unsigned int filesize;
- unsigned short reserved1;
- unsigned short reserved2;
- unsigned int pixelArrOffset;
- } BitmapFileHeader;
- typedef struct
- {
- unsigned int headerSize;
- unsigned int width;
- unsigned int height;
- unsigned short planes;
- unsigned short bitsPerPixel;
- unsigned int compression;
- unsigned int imageSize;
- unsigned int xPixelsPerMeter;
- unsigned int yPixelsPerMeter;
- unsigned int colorsInColorTable;
- unsigned int importantColorCount;
- } BitmapInfoHeader;
- typedef struct
- {
- unsigned char b;
- unsigned char g;
- unsigned char r;
- } Rgb;
- #pragma pack(pop)
- typedef struct{
- unsigned int X;
- unsigned int Y;
- unsigned int width_line;
- Rgb color;
- }Split;
- typedef struct{
- char* comanda;
- Rgb arr1;
- Rgb arr2;
- char* path;
- char colorrgb;
- unsigned int value;
- Split split;
- }Box;
- static const char *optString1 = "m:c:s";
- static const char *optString2 = "Il:o:vh?";
- static const char *optString3 = "r:v:s";
- static const char *sep1 = "/";
- void printFileHeader(BitmapFileHeader header){
- printf("signature:\t%x (%hu)\n", header.signature, header.signature);
- printf("filesize:\t%x (%u)\n", header.filesize, header.filesize);
- printf("reserved1:\t%x (%hu)\n", header.reserved1, header.reserved1);
- printf("reserved2:\t%x (%hu)\n", header.reserved2, header.reserved2);
- printf("pixelArrOffset:\t%x (%u)\n", header.pixelArrOffset, header.pixelArrOffset);
- }
- void printInfoHeader(BitmapInfoHeader header){
- printf("headerSize:\t%x (%u)\n", header.headerSize, header.headerSize);
- printf("width: \t%x (%u)\n", header.width, header.width);
- printf("height: \t%x (%u)\n", header.height, header.height);
- printf("planes: \t%x (%hu)\n", header.planes, header.planes);
- printf("bitsPerPixel:\t%x (%hu)\n", header.bitsPerPixel, header.bitsPerPixel);
- printf("compression:\t%x (%u)\n", header.compression, header.compression);
- printf("imageSize:\t%x (%u)\n", header.imageSize, header.imageSize);
- printf("xPixelsPerMeter:\t%x (%u)\n", header.xPixelsPerMeter, header.xPixelsPerMeter);
- printf("yPixelsPerMeter:\t%x (%u)\n", header.yPixelsPerMeter, header.yPixelsPerMeter);
- printf("colorsInColorTable:\t%x (%u)\n", header.colorsInColorTable, header.colorsInColorTable);
- printf("importantColorCount:\t%x (%u)\n", header.importantColorCount, header.importantColorCount);
- }
- void del_arr(Rgb** arr,int H){
- for(int i=0;i<H;i++){
- free(arr[i]);
- }
- free(arr);
- }
- void info_file(char* str){
- FILE* fp;
- if(!(fp=fopen(str, "rb"))){
- printf("Не удалось открыть файл");
- exit(1);
- }
- else{
- BitmapFileHeader bmfh;
- BitmapInfoHeader bmif;
- fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
- fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
- printFileHeader(bmfh);
- printInfoHeader(bmif);
- }
- }
- void color_swap(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,Rgb arr1,Rgb arr2,char* path){/*"Замена цвета"*/
- unsigned int H = bmif.height;
- unsigned int W = bmif.width;
- FILE *ff;
- if(!(ff = fopen(path, "wb"))) printf("\n----------Не удалось создать файл!-----------\n");
- fwrite(&bmfh, 1, sizeof(BitmapFileHeader),ff);
- fwrite(&bmif, 1, sizeof(BitmapInfoHeader),ff);
- for(int i=0; i<H; i++){
- for(int j=0; j<W; j++){
- if ((arr[i][j].r==arr1.r)&(arr[i][j].g==arr1.g)&(arr[i][j].b==arr1.b)){
- arr[i][j].r=arr2.r;
- arr[i][j].g=arr2.g;
- arr[i][j].b=arr2.b;
- }
- }
- fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
- }
- fclose(ff);
- for(int i=0; i<H; i++){
- for(int j=0; j<W; j++){
- if ((arr[i][j].r==arr2.r)&(arr[i][j].g==arr2.g)&(arr[i][j].b==arr2.b)){
- arr[i][j].r==arr1.r;
- arr[i][j].g==arr1.g;
- arr[i][j].b==arr1.b;
- }
- }
- }
- }
- void filter_rgb_component(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,char component,int value,char* path){/*"фильтер rgb компонент"*/
- unsigned int H = bmif.height;
- unsigned int W = bmif.width;
- FILE *ff;
- if(!(ff = fopen(path, "wb"))) printf("\n----------Не удалось создать файл!-----------\n");
- fwrite(&bmfh, 1, sizeof(BitmapFileHeader),ff);
- fwrite(&bmif, 1, sizeof(BitmapInfoHeader),ff);
- switch(component){
- case 'r':
- for(int i=0; i<H; i++){
- for(int j=0; j<W; j++){
- arr[i][j].r=value;
- }
- fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
- }
- break;
- case 'g':
- for(int i=0; i<H; i++){
- for(int j=0; j<W; j++){
- arr[i][j].g=value;
- }
- fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
- }
- break;
- case 'b':
- for(int i=0; i<H; i++){
- for(int j=0; j<W; j++){
- arr[i][j].b=value;
- }
- fwrite(arr[i], 1, W * sizeof(Rgb) + (W * 3) % 4, ff);
- }
- break;
- default:
- printf("Неверные значения!!!\n");
- }
- fclose(ff);
- }
- void info_author(){
- printf("-----Имя: Семён-----\n-----Фамилия: Емельянов-----\n-----Дата рождения: 30.05.2001-----\n");
- }
- void help_assistant(){
- 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");
- }
- char* itoa(int number) {
- int i = number;
- int num_len = 1;
- int sym;
- while (i > 9) {
- i = i / 10;
- num_len++;
- }
- char* num_str = (char*)calloc(num_len + 1, sizeof(char));
- num_str[num_len] = '\0';
- i = number;
- for (int j = (num_len - 1); j > -1; j--) {
- sym = i % 10;
- if (sym == 0) num_str[j] = '0';
- if (sym == 1) num_str[j] = '1';
- if (sym == 2) num_str[j] = '2';
- if (sym == 3) num_str[j] = '3';
- if (sym == 4) num_str[j] = '4';
- if (sym == 5) num_str[j] = '5';
- if (sym == 6) num_str[j] = '6';
- if (sym == 7) num_str[j] = '7';
- if (sym == 8) num_str[j] = '8';
- if (sym == 9) num_str[j] = '9';
- i = i / 10;
- }
- return num_str;
- }
- void image_splitting(BitmapInfoHeader bmif,BitmapFileHeader bmfh,Rgb** arr,Split split,char* path_out){/*"разрезание изображения"*/
- char path[BUFFER];
- char* ind;
- FILE* file;
- int W;
- int H;
- unsigned int H_s = bmif.height;
- unsigned int W_s = bmif.width;
- if (split.X != 0) W = bmif.width / split.X;
- else { W = bmif.width;split.X = 1; }
- if (split.Y != 0) H = bmif.height / split.Y;
- else { H = bmif.height; split.Y = 1; }
- bmif.width = W;
- bmif.height = H;
- int cout = 0;
- for (int i = 0; i < split.Y; i++) {
- for (int j = 0; j < split.X; j++) {
- ind = itoa(cout);
- strcpy(path, path_out);
- strtok(path, ".");
- strcat(path, ind); strcat(path, ".bmp");
- free(ind);
- if (!(file = fopen(path, "wb"))) exit(1);
- fwrite(&bmfh, 1, sizeof(BitmapFileHeader), file);
- fwrite(&bmif, 1, sizeof(BitmapInfoHeader), file);
- for (int k = i * H; k < (i + 1) * H; k++) {
- for (int l = j * W; l < (j + 1) * W; l++) fwrite(&arr[k][l], 1, sizeof(Rgb), file);
- fwrite("\0", 1, (W * 3) % 4, file);
- }
- fclose(file);cout++;
- }
- }
- bmif.height=H_s;
- bmif.width=W_s;
- }
- int main(int argc, char *argv[]){
- if(argc==1){
- printf("Введите -help или --h,чтобы узнать доступный функионал!!!\n");
- return 0;
- }
- Box box;
- char strpath[BUFFER]="out.bmp";
- box.arr1.r=255;box.arr1.g=255;box.arr1.b=255;
- box.arr2.r=100;box.arr2.g=100;box.arr2.b=100;
- box.path = strpath;
- box.colorrgb = 'b';
- box.value = 0;
- box.split.X = 1;
- box.split.Y = 1;
- box.split.width_line = 1;
- box.split.color.r=255;box.split.color.g=255;box.split.color.b=255;
- int optIdx;
- if(!(strcmp(argv[1],"--help"))||!(strcmp(argv[1],"-h"))){
- help_assistant();
- return 0;
- }
- if(!(strcmp(argv[1],"infoauthor"))||!(strcmp(argv[1],"-i"))){
- info_author();
- return 0;
- }
- if(!(strcmp(argv[1],"infofile"))){
- info_file(argv[argc-1]);
- return 0;
- }
- int opt;
- if(!(strcmp(argv[1],"mycolor"))){
- char* str;
- int count=0;
- static struct option long_opt[] = {
- {"mycolor", 1, 0, 'm'},
- {"color", 1, 0, 'c'},
- {"savefile",1, 0, 's'},
- {0,0,0,0}
- };
- FILE* fp;
- if(!(fp=fopen(argv[argc-1], "rb"))){
- printf("Не удалось открыть файл\n");
- exit(1);
- }
- BitmapFileHeader bmfh;
- BitmapInfoHeader bmif;
- fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
- fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
- Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
- for(int i=0; i<bmif.height; i++){
- arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
- fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
- }
- while((opt=getopt_long(argc, argv,optString1, long_opt, &optIdx))!=-1){
- switch(opt){
- case 'm':
- str=strtok(optarg,sep1);
- while(str!=NULL){
- if (count==0) box.arr1.r=atoi(str);
- if (count==1) box.arr1.g=atoi(str);
- if (count==2) box.arr1.b=atoi(str);
- count++;
- str=strtok(NULL,sep1);
- }
- count=0;
- break;
- case 'c':
- while(str!=NULL){
- if (count==0) box.arr2.r=atoi(str);
- if (count==1) box.arr2.g=atoi(str);
- if (count==2) box.arr2.b=atoi(str);
- count++;
- str=strtok(NULL,sep1);
- }
- count=0;
- break;
- case 's':
- box.path = optarg;
- break;
- default :
- printf("Неверно введена команда!!!\n");
- del_arr(arr,bmif.height);
- return -1;
- }
- }
- color_swap(bmif,bmfh,arr,box.arr1,box.arr2,box.path);
- del_arr(arr,bmif.height);
- return 0;
- }
- if(!(strcmp(argv[1],"mysplit"))){
- static struct option long_opt[] = {
- {"x_argument", 1, 0, 'x'},
- {"y_argument", 1, 0, 'y'},
- {"savefile",1, 0, 's'},
- {0,0,0,0}
- };
- FILE* fp;
- if(!(fp=fopen(argv[argc-1], "rb"))){
- printf("Не удалось открыть файл\n");
- exit(1);
- }
- BitmapFileHeader bmfh;
- BitmapInfoHeader bmif;
- fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
- fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
- Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
- for(int i=0; i<bmif.height; i++){
- arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
- fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
- }
- while((opt=getopt_long(argc, argv,optString3, long_opt, &optIdx))!=-1){
- switch(opt){
- case 'x':
- box.split.X=atoi(optarg);
- break;
- case 'y':
- box.split.Y=atoi(optarg);
- break;
- case 's':
- box.path =optarg;
- break;
- default :
- printf("Неверно введена команда!!!\n");
- del_arr(arr,bmif.height);
- return -1;
- }
- }
- image_splitting(bmif,bmfh,arr,box.split,box.path);
- del_arr(arr,bmif.height);
- return 0;
- }
- if(!(strcmp(argv[1],"myfilter"))){
- static struct option long_opt[] = {
- {"rgb", 1, 0, 'r'},
- {"value", 1, 0, 'v'},
- {"savefile",1, 0, 's'},
- {0,0,0,0}
- };
- FILE* fp;
- if(!(fp=fopen(argv[argc-1], "rb"))){
- printf("Не удалось открыть файл\n");
- exit(1);
- }
- BitmapFileHeader bmfh;
- BitmapInfoHeader bmif;
- fread(&bmfh,1,sizeof(BitmapFileHeader),fp);
- fread(&bmif,1,sizeof(BitmapInfoHeader),fp);
- Rgb **arr = malloc(bmif.height*sizeof(Rgb*));
- for(int i=0; i<bmif.height; i++){
- arr[i] = malloc(bmif.width * sizeof(Rgb) + (bmif.width*3)%4);
- fread(arr[i],1,bmif.width * sizeof(Rgb) + (bmif.width*3)%4,fp);
- }
- while((opt=getopt_long(argc, argv,optString3, long_opt, &optIdx))!=-1){
- switch(opt){
- case 'r':
- box.colorrgb=*optarg;
- break;
- case 'v':
- box.value=atoi(optarg);
- break;
- case 's':
- box.path =optarg;
- break;
- default :
- printf("Неверно введена команда!!!\n");
- del_arr(arr,bmif.height);
- return -1;
- }
- }
- filter_rgb_component(bmif,bmfh,arr,box.colorrgb,box.value,box.path);
- del_arr(arr,bmif.height);
- return 0;
- }
- else{
- printf("Неверно ввели команду!!!\n");
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement