Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. FILE* fileIn;
  8. FILE* fileOut;
  9.  
  10. double pi = 3.1415926535897932384626433832795;
  11. double sigma = 0.6;
  12.  
  13. void inputCheck(int argc, char* argv[])
  14. {
  15.     if (argc != 4 || !(strcmp(argv[2], "Averaging") == 0 || strcmp(argv[2], "Gauss3") == 0 || strcmp(argv[2], "Gauss5") == 0 || strcmp(argv[2], "Sobel") == 0
  16.         || strcmp(argv[2], "SobelX") == 0 || strcmp(argv[2], "SobelY") == 0 || strcmp(argv[2], "ColorWB") == 0) || fopen_s(&fileIn, argv[1], "rb") != 0)
  17.     {
  18.         printf("Invalid input. Try again.");
  19.         exit(0);
  20.     }
  21. }
  22.  
  23. #pragma pack(push, 1)
  24. struct BITMAPFILEHEADER
  25. {
  26.     unsigned short  bfType;
  27.     unsigned int bfSize;
  28.     unsigned short bfReserved1;
  29.     unsigned short bfReserved2;
  30.     unsigned int bfOffBits;
  31. };
  32.  
  33. struct BITMAPINFOHEADER {
  34.     unsigned int biSize;
  35.     unsigned int  biWidth;
  36.     unsigned int  biHeight;
  37.     unsigned short  biPlanes;
  38.     unsigned short  biBitCount;
  39.     unsigned int biCompression;
  40.     unsigned int biSizeImage;
  41.     unsigned int  biXPelsPerMeter;
  42.     unsigned int  biYPelsPerMeter;
  43.     unsigned int biClrUsed;
  44.     unsigned int biClrImportant;
  45. };
  46. #pragma pack(pop)
  47.  
  48. void convolution(unsigned char* bitMapImage, int height, int width, char* mode)
  49. {
  50.     if (strcmp(mode, "ColorWB") == 0)
  51.     {
  52.         for (int i = 0; i < height * width; i++)
  53.         {
  54.             unsigned char result = (299 * bitMapImage[i * 3] + 587 * bitMapImage[i * 3 + 1] + 114 * bitMapImage[i * 3 + 2]) / 1000;
  55.             bitMapImage[i * 3] = result;
  56.             bitMapImage[i * 3 + 1] = result;
  57.             bitMapImage[i * 3 + 2] = result;
  58.         }
  59.         return;
  60.     }
  61.  
  62.     unsigned char* bitMapImageCopy = (unsigned char*)malloc(3 * height * width * sizeof(unsigned char));
  63.     for (int i = 0; i < 3 * height * width; i++)
  64.         bitMapImageCopy[i] = bitMapImage[i];
  65.  
  66.     int* steps = (int*)malloc(18 * sizeof(int));
  67.  
  68.     for (int i = -1; i <= 1; i++)
  69.         for (int j = -1; j <= 1; j++)
  70.         {
  71.             steps[(i + 1) * 9 + j + 1] = i;
  72.             steps[(i + 1) * 9 + j + 2] = j;
  73.         }
  74.  
  75.     double* matrix = (double*)malloc(9 * sizeof(double));
  76.     int size = 1;
  77.     if (strcmp(mode, "Gauss5") == 0)
  78.     {
  79.         steps = (int*)realloc(steps, 50 * sizeof(int));
  80.         printf("asfgd\n");
  81.         for (int i = -2; i <= 2; i++)
  82.             for (int j = -2; j <= 2; j++)
  83.             {
  84.                 steps[(i + 2) * 25 + j + 1] = i;
  85.                 steps[(i + 2) * 25 + j + 2] = j;
  86.             }
  87.  
  88.         size = 2;
  89.  
  90.         matrix = (double*)realloc(matrix, 25 * sizeof(double));
  91.         for (int x = -2; x < 2 + 1; x++)
  92.             for (int y = -2; y < 2 + 1; y++)
  93.                 matrix[(x + 2) * 5 + y + 2] = 1 / sqrt(2 * pi * sigma) * exp(-(x * x + y * y) / (2 * sigma * sigma));
  94.     }
  95.     else if (strcmp(mode, "Averaging") == 0)
  96.     {
  97.         double source[9] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  98.         for (int i = 0; i < 9; i++)
  99.             matrix[i] = source[i];
  100.     }
  101.     else if (strcmp(mode, "Gauss3") == 0)
  102.     {
  103.         for (int x = -1; x < 1 + 1; x++)
  104.             for (int y = -1; y < 1 + 1; y++)
  105.                 matrix[(x + 1) * 3 + y + 1] = 1 / sqrt(2 * pi * sigma) * exp(-(x * x + y * y) / (2 * sigma * sigma));
  106.     }
  107.     else if (strcmp(mode, "SobelX") == 0)
  108.     {
  109.         int source[9] = { -1, 0, 1, -2, 0, 2, -1, 0, 1 };
  110.         for (int i = 0; i < 9; i++)
  111.             matrix[i] = source[i];
  112.     }
  113.     else if (strcmp(mode, "SobelY") == 0)
  114.     {
  115.         int source[9] = { -1, -2, -1, 0, 0, 0, 1, 2, 1 };
  116.         for (int i = 0; i < 9; i++)
  117.             matrix[i] = source[i];
  118.     }
  119.  
  120.     for (int i = 0; i < 9; i++)
  121.         printf("%f ", matrix[i]);
  122.  
  123.     for (int i = 0; i < height; i++)
  124.         for (int j = 0; j < width; j++)
  125.         {
  126.             double result[3] = { 0, 0, 0 };
  127.             double divisor = 0;
  128.  
  129.             for (int step = 0; step < (2 * size + 1) * (2 * size + 1); step++)
  130.                 if (i + steps[step * 2] >= 0 && i + steps[step * 2] < height && j + steps[step * 2 + 1] >= 0 && j + steps[step * 2 + 1] < width)
  131.                 {
  132.                     result[0] += bitMapImage[((i + steps[step * 2]) * width + j + steps[step * 2 + 1]) * 3] * matrix[(steps[step * 2] + 1) * (2 * size + 1) + steps[step * 2 + 1] + 1];
  133.                     result[1] += bitMapImage[((i + steps[step * 2]) * width + j + steps[step * 2 + 1]) * 3 + 1] * matrix[(steps[step * 2] + 1) * (2 * size + 1) + steps[step * 2 + 1] + 1];
  134.                     result[2] += bitMapImage[((i + steps[step * 2]) * width + j + steps[step * 2 + 1]) * 3 + 2] * matrix[(steps[step * 2] + 1) * (2 * size + 1) + steps[step * 2 + 1] + 1];
  135.                     divisor += matrix[(steps[step * 2] + 1) * (2 * size + 1) + steps[step * 2 + 1] + 1];
  136.                 }
  137.  
  138.             bitMapImageCopy[(i * width + j) * 3] = (unsigned char)(result[0] / divisor);
  139.             bitMapImageCopy[(i * width + j) * 3 + 1] = (unsigned char)(result[1] / divisor);
  140.             bitMapImageCopy[(i * width + j) * 3 + 2] = (unsigned char)(result[2] / divisor);
  141.             //printf("%f\n", divisor);
  142.         }
  143.  
  144.     if (strcmp(mode, "SobelX") == 0 || strcmp(mode, "SobelY") == 0)
  145.     {
  146.         for (int i = 0; i < height * width * 3; i++)
  147.             bitMapImage[i] = abs(bitMapImageCopy[i]) > 400 ? 255 : 0;
  148.     }
  149.     else
  150.     {
  151.         for (int i = 0; i < height * width * 3; i++)
  152.             bitMapImage[i] = bitMapImageCopy[i];
  153.     }
  154.  
  155.     free(steps);
  156.     free(matrix);
  157.     free(bitMapImageCopy);
  158. }
  159.  
  160. int main()
  161. {
  162.     //inputCheck(argc, argv);
  163.     //Source C : \Users\David\source\repos\Task8\Task8\fileIn.bmp ColorWB C : \Users\David\source\repos\Task8\Task8\fileOut.bmp
  164.     fopen_s(&fileIn, "fileIn.bmp", "rb");
  165.     fopen_s(&fileOut, "fileOut.bmp", "wb");
  166.  
  167.     struct BITMAPFILEHEADER bitMapFileHeader;
  168.     struct BITMAPINFOHEADER bitMapInfoHeader;
  169.  
  170.     fread(&bitMapFileHeader, sizeof(bitMapFileHeader), 1, fileIn);
  171.     fread(&bitMapInfoHeader, sizeof(bitMapInfoHeader), 1, fileIn);
  172.  
  173.     unsigned char* bitMapImage = (unsigned char*)malloc(bitMapInfoHeader.biSizeImage);
  174.  
  175.     fseek(fileIn, bitMapFileHeader.bfOffBits, SEEK_SET);
  176.     fread(bitMapImage, 1, bitMapInfoHeader.biSizeImage, fileIn);
  177.  
  178.     convolution(bitMapImage, bitMapInfoHeader.biHeight, bitMapInfoHeader.biWidth, "Gauss3");
  179.  
  180.     fwrite(&bitMapFileHeader, sizeof(bitMapFileHeader), 1, fileOut);
  181.     fwrite(&bitMapInfoHeader, sizeof(bitMapInfoHeader), 1, fileOut);
  182.  
  183.     for (int i = 0; i < bitMapInfoHeader.biSizeImage; i++)
  184.         fwrite(&bitMapImage[i], 1, 1, fileOut);
  185.  
  186.     printf("The program worked successfully");
  187.     free(bitMapImage);
  188.     fclose(fileIn);
  189.     fclose(fileOut);
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement