Advertisement
Guest User

Untitled

a guest
Feb 20th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define BICUBIC_KERNEL_SIZE 4
  6.  
  7. double cubic(double x) {
  8. double absx = fabs(x);
  9. if (absx <= 1) {
  10. return 1 - 2 * pow(absx, 2) + pow(absx, 3);
  11. } else if (absx < 2) {
  12. return 4 - 8 * absx + 5 * pow(absx, 2) - pow(absx, 3);
  13. } else {
  14. return 0;
  15. }
  16. }
  17.  
  18. double bicubic(double coeffs[BICUBIC_KERNEL_SIZE][BICUBIC_KERNEL_SIZE], double x, double y) {
  19. double result = 0;
  20. for (int i = 0; i < BICUBIC_KERNEL_SIZE; i++) {
  21. for (int j = 0; j < BICUBIC_KERNEL_SIZE; j++) {
  22. double cx = cubic(x - (double)i);
  23. double cy = cubic(y - (double)j);
  24. result += cx * cy * coeffs[i][j];
  25. }
  26. }
  27. return result;
  28. }
  29.  
  30. void rescale_image(unsigned char *input_image, int input_width, int input_height, unsigned char *output_image, int output_width, int output_height) {
  31. double x_ratio = (double)input_width / (double)output_width;
  32. double y_ratio = (double)input_height / (double)output_height;
  33. double coeffs[BICUBIC_KERNEL_SIZE][BICUBIC_KERNEL_SIZE];
  34. int x, y;
  35. double x_diff, y_diff;
  36. int pixel_value;
  37.  
  38. // Iterate over each pixel in the output image
  39. for (int i = 0; i < output_height; i++) {
  40. for (int j = 0; j < output_width; j++) {
  41. // Compute the coordinates in the input image
  42. double x_coord = ((double)j + 0.5) * x_ratio - 0.5;
  43. double y_coord = ((double)i + 0.5) * y_ratio - 0.5;
  44.  
  45. // Compute the integer and fractional parts of the coordinates
  46. x = (int)x_coord;
  47. y = (int)y_coord;
  48. x_diff = x_coord - x;
  49. y_diff = y_coord - y;
  50.  
  51. // Compute the bicubic coefficients
  52. for (int k = -1; k <= 2; k++) {
  53. for (int l = -1; l <= 2; l++) {
  54. int row = y + k;
  55. int col = x + l;
  56.  
  57. // Handle edge cases
  58. if (row < 0) {
  59. row = 0;
  60. } else if (row >= input_height) {
  61. row = input_height - 1;
  62. }
  63. if (col < 0) {
  64. col = 0;
  65. } else if (col >= input_width) {
  66. col = input_width - 1;
  67. }
  68.  
  69. // Get the pixel value at the current position
  70. pixel_value = *(input_image + row * input_width + col);
  71.  
  72. // Compute the bicubic coefficient
  73. coeffs[k+1][l+1] = (double)pixel_value;
  74. }
  75. }
  76.  
  77. // Compute the new pixel value using bicubic interpolation
  78. pixel_value = (int)round(bic
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement