You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.1KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <getopt.h>
  5. #include <unistd.h>
  6. #include "gblur.h"
  7. void print_help() {
  8. printf("This program performs gaussian blur on given image using sigma parameter.\n"
  9. "\n"
  10. "Usage:\n"
  11. "\tgblur -i input_file [-o output_file] [-s radius] [-h]\n"
  12. "Options:\n"
  13. "\t-i: path to file with input RGB JPEG image.\n"
  14. "\t-o: path to file where blurred RGB JPEG image will be recorded. If no path will be specified, "
  15. "input file will be overwritten!\n"
  16. "\t-s: radius of blur. Default is 5.\n"
  17. "\t-h: show this help.\n"
  18. "\n"
  19. "Example of usage:"
  20. "\tgblur -i /home/user/Images/img.jpg -o /home/user/Images/blurred_img.jpg -s 5\n");
  21. }
  22. int main(int argc, char *argv[]) {
  23. int opt;
  24. double sigma = 0.0;
  25. char *input_filename = NULL;
  26. char *out_filename = NULL;
  27. // Parsing passed arguments
  28. while ((opt = getopt(argc, argv, "i:o:s:h")) != -1) {
  29. switch(opt) {
  30. case 'i':
  31. input_filename = optarg;
  32. break;
  33. case 'o':
  34. out_filename = optarg;
  35. break;
  36. case 's':
  37. sigma = atof(optarg);
  38. break;
  39. case 'h':
  40. print_help();
  41. exit(0);
  42. break;
  43. case ':':
  44. printf("Option needs a value\n");
  45. break;
  46. case '?':
  47. printf("Unknown option\n");
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. if (input_filename) {
  54. if (access(input_filename, R_OK) == -1) {
  55. fprintf(stderr, "Can not read file %s\n", input_filename);
  56. exit(1);
  57. }
  58. } else {
  59. fprintf(stderr, "Please specify input filename!\n");
  60. print_help();
  61. exit(1);
  62. }
  63. if (sigma == 0.0) {
  64. printf("Value of sigma is 0, so using default value: 5.0\n");
  65. sigma = 5.0;
  66. }
  67. FILE *infile;
  68. if ((infile = fopen(input_filename, "rb")) == NULL) {
  69. fprintf(stderr, "Can not open %s\n", input_filename);
  70. exit(1);
  71. }
  72. struct image_data * original_image = decompress_image(infile);
  73. fclose(infile);
  74. if (original_image == NULL) {
  75. fprintf(stderr, "Failed to decompress image %s\n", input_filename);
  76. exit(1);
  77. }
  78. // If no output file were specified, use original image
  79. FILE *outfile;
  80. if (out_filename == NULL) {
  81. out_filename = input_filename;
  82. }
  83. if ((outfile = fopen(out_filename, "wb")) == NULL) {
  84. fprintf(stderr, "Can not open %s\n", out_filename);
  85. exit(1);
  86. }
  87. struct image_data * blurred_image;
  88. blurred_image = malloc(sizeof(struct image_data));
  89. if (blurred_image == NULL) {
  90. fprintf(stderr, "Failed to allocate memory!\n");
  91. destroy_image_data(original_image);
  92. exit(1);
  93. }
  94. blurred_image->x = original_image->x;
  95. blurred_image->y = original_image->y;
  96. blurred_image->num_channels = original_image->num_channels;
  97. size_t data_size = blurred_image->x
  98. * blurred_image->y
  99. * blurred_image->num_channels;
  100. blurred_image->pixels = (unsigned char *)malloc(data_size);
  101. int8_t blur_result;
  102. blur_result = gaussian_blur(original_image, blurred_image, sigma);
  103. if (blur_result != 0) {
  104. fprintf(stderr, "Failed to blur image!\n");
  105. destroy_image_data(original_image);
  106. destroy_image_data(blurred_image);
  107. exit(1);
  108. }
  109. int8_t compress_result;
  110. compress_result = compress_image(blurred_image, outfile, 90);
  111. fclose(outfile);
  112. if (compress_result != 0) {
  113. fprintf(stderr, "Failed to write blurred image to file!\n");
  114. destroy_image_data(original_image);
  115. destroy_image_data(blurred_image);
  116. exit(1);
  117. }
  118. destroy_image_data(original_image);
  119. destroy_image_data(blurred_image);
  120. return 0;
  121. }