Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- FILE* fileIn;
- FILE* fileOut;
- double pi = 3.1415926535897932384626433832795;
- double sigma = 0.6;
- void inputCheck(int argc, char* argv[])
- {
- if (argc != 4 || !(strcmp(argv[2], "Averaging") == 0 || strcmp(argv[2], "Gauss3") == 0 || strcmp(argv[2], "Gauss5") == 0 || strcmp(argv[2], "Sobel") == 0
- || strcmp(argv[2], "SobelX") == 0 || strcmp(argv[2], "SobelY") == 0 || strcmp(argv[2], "ColorWB") == 0) || fopen_s(&fileIn, argv[1], "rb") != 0)
- {
- printf("Invalid input. Try again.");
- exit(0);
- }
- }
- #pragma pack(push, 1)
- struct BITMAPFILEHEADER
- {
- unsigned short bfType;
- unsigned int bfSize;
- unsigned short bfReserved1;
- unsigned short bfReserved2;
- unsigned int bfOffBits;
- };
- struct BITMAPINFOHEADER {
- unsigned int biSize;
- unsigned int biWidth;
- unsigned int biHeight;
- unsigned short biPlanes;
- unsigned short biBitCount;
- unsigned int biCompression;
- unsigned int biSizeImage;
- unsigned int biXPelsPerMeter;
- unsigned int biYPelsPerMeter;
- unsigned int biClrUsed;
- unsigned int biClrImportant;
- };
- #pragma pack(pop)
- void convolution(unsigned char* bitMapImage, int height, int width, char* mode)
- {
- if (strcmp(mode, "ColorWB") == 0)
- {
- for (int i = 0; i < height * width; i++)
- {
- unsigned char result = (299 * bitMapImage[i * 3] + 587 * bitMapImage[i * 3 + 1] + 114 * bitMapImage[i * 3 + 2]) / 1000;
- bitMapImage[i * 3] = result;
- bitMapImage[i * 3 + 1] = result;
- bitMapImage[i * 3 + 2] = result;
- }
- return;
- }
- unsigned char* bitMapImageCopy = (unsigned char*)malloc(3 * height * width * sizeof(unsigned char));
- for (int i = 0; i < 3 * height * width; i++)
- bitMapImageCopy[i] = bitMapImage[i];
- int* steps = (int*)malloc(18 * sizeof(int));
- for (int i = -1; i <= 1; i++)
- for (int j = -1; j <= 1; j++)
- {
- steps[(i + 1) * 9 + j + 1] = i;
- steps[(i + 1) * 9 + j + 2] = j;
- }
- double* matrix = (double*)malloc(9 * sizeof(double));
- int size = 1;
- if (strcmp(mode, "Gauss5") == 0)
- {
- steps = (int*)realloc(steps, 50 * sizeof(int));
- printf("asfgd\n");
- for (int i = -2; i <= 2; i++)
- for (int j = -2; j <= 2; j++)
- {
- steps[(i + 2) * 25 + j + 1] = i;
- steps[(i + 2) * 25 + j + 2] = j;
- }
- size = 2;
- matrix = (double*)realloc(matrix, 25 * sizeof(double));
- for (int x = -2; x < 2 + 1; x++)
- for (int y = -2; y < 2 + 1; y++)
- matrix[(x + 2) * 5 + y + 2] = 1 / sqrt(2 * pi * sigma) * exp(-(x * x + y * y) / (2 * sigma * sigma));
- }
- else if (strcmp(mode, "Averaging") == 0)
- {
- double source[9] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
- for (int i = 0; i < 9; i++)
- matrix[i] = source[i];
- }
- else if (strcmp(mode, "Gauss3") == 0)
- {
- for (int x = -1; x < 1 + 1; x++)
- for (int y = -1; y < 1 + 1; y++)
- matrix[(x + 1) * 3 + y + 1] = 1 / sqrt(2 * pi * sigma) * exp(-(x * x + y * y) / (2 * sigma * sigma));
- }
- else if (strcmp(mode, "SobelX") == 0)
- {
- int source[9] = { -1, 0, 1, -2, 0, 2, -1, 0, 1 };
- for (int i = 0; i < 9; i++)
- matrix[i] = source[i];
- }
- else if (strcmp(mode, "SobelY") == 0)
- {
- int source[9] = { -1, -2, -1, 0, 0, 0, 1, 2, 1 };
- for (int i = 0; i < 9; i++)
- matrix[i] = source[i];
- }
- for (int i = 0; i < 9; i++)
- printf("%f ", matrix[i]);
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- {
- double result[3] = { 0, 0, 0 };
- double divisor = 0;
- for (int step = 0; step < (2 * size + 1) * (2 * size + 1); step++)
- if (i + steps[step * 2] >= 0 && i + steps[step * 2] < height && j + steps[step * 2 + 1] >= 0 && j + steps[step * 2 + 1] < width)
- {
- 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];
- 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];
- 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];
- divisor += matrix[(steps[step * 2] + 1) * (2 * size + 1) + steps[step * 2 + 1] + 1];
- }
- bitMapImageCopy[(i * width + j) * 3] = (unsigned char)(result[0] / divisor);
- bitMapImageCopy[(i * width + j) * 3 + 1] = (unsigned char)(result[1] / divisor);
- bitMapImageCopy[(i * width + j) * 3 + 2] = (unsigned char)(result[2] / divisor);
- //printf("%f\n", divisor);
- }
- if (strcmp(mode, "SobelX") == 0 || strcmp(mode, "SobelY") == 0)
- {
- for (int i = 0; i < height * width * 3; i++)
- bitMapImage[i] = abs(bitMapImageCopy[i]) > 400 ? 255 : 0;
- }
- else
- {
- for (int i = 0; i < height * width * 3; i++)
- bitMapImage[i] = bitMapImageCopy[i];
- }
- free(steps);
- free(matrix);
- free(bitMapImageCopy);
- }
- int main()
- {
- //inputCheck(argc, argv);
- //Source C : \Users\David\source\repos\Task8\Task8\fileIn.bmp ColorWB C : \Users\David\source\repos\Task8\Task8\fileOut.bmp
- fopen_s(&fileIn, "fileIn.bmp", "rb");
- fopen_s(&fileOut, "fileOut.bmp", "wb");
- struct BITMAPFILEHEADER bitMapFileHeader;
- struct BITMAPINFOHEADER bitMapInfoHeader;
- fread(&bitMapFileHeader, sizeof(bitMapFileHeader), 1, fileIn);
- fread(&bitMapInfoHeader, sizeof(bitMapInfoHeader), 1, fileIn);
- unsigned char* bitMapImage = (unsigned char*)malloc(bitMapInfoHeader.biSizeImage);
- fseek(fileIn, bitMapFileHeader.bfOffBits, SEEK_SET);
- fread(bitMapImage, 1, bitMapInfoHeader.biSizeImage, fileIn);
- convolution(bitMapImage, bitMapInfoHeader.biHeight, bitMapInfoHeader.biWidth, "Gauss3");
- fwrite(&bitMapFileHeader, sizeof(bitMapFileHeader), 1, fileOut);
- fwrite(&bitMapInfoHeader, sizeof(bitMapInfoHeader), 1, fileOut);
- for (int i = 0; i < bitMapInfoHeader.biSizeImage; i++)
- fwrite(&bitMapImage[i], 1, 1, fileOut);
- printf("The program worked successfully");
- free(bitMapImage);
- fclose(fileIn);
- fclose(fileOut);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement