Advertisement
Aveneid

Untitled

Oct 12th, 2020
2,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define STB_IMAGE_IMPLEMENTATION
  4. #include "stb/stb_image.h"
  5.  
  6. #define STB_IMAGE_WRITE_IMPLEMENTATION
  7. #include "stb/stb_image_write.h"
  8.  
  9.  
  10. unsigned char filter[] = {1,2,1,
  11.                 2,4,2,
  12.                 1,2,1
  13.                };
  14. using namespace std;
  15. unsigned char* makeStuff(unsigned char *img, int w,int h, int ch)
  16. {
  17.     size_t s = (w)*(h)*ch;
  18.     unsigned char *res = (unsigned char *)malloc(s);
  19.  
  20. unsigned char p1,p2,p3,p4,p5,p6,p7,p8,p9;
  21.     for(int x=1; x<h; x++)
  22.     {
  23.         for(int y=1; y<w; y++)
  24.         {
  25.             for(int c=0; c<ch; c++)
  26.             {
  27.                 p1 = (img[ch*( (x-1) *w  +  (y-1))  +c]*filter[0]);
  28.                 p2 = (img[ch*( (x)   *w  +  (y-1))  +c]*filter[1]);
  29.                 p3 = (img[ch*( (x+1) *w  +  (y-1))  +c]*filter[2]);
  30.  
  31.                 p4 = (img[ch*( (x-1) *w  +  (y))    +c]*filter[3]);
  32.                 p5 = (img[ch*( (x)   *w  +  (y))    +c]*filter[4]);
  33.                 p6 = (img[ch*( (x+1) *w  +  (y))    +c]*filter[5]);
  34.  
  35.                 p7 = (img[ch*( (x-1) *w  +  (y+1))  +c]*filter[6]);
  36.                 p8 = (img[ch*( (x)   *w  +  (y+1))  +c]*filter[7]);
  37.                 p9 = (img[ch*( (x+1) *w  +  (y+1))  +c]*filter[8]);
  38.  
  39.                 res[ch*(x*w+y)+c] = ((p1+p2+p3+p4+p5+p6+p7+p8+p9))/16;
  40.             }
  41.  
  42.         }
  43.     }
  44.     return res;
  45. }
  46.  
  47. int main()
  48. {
  49.     string path = "t.jpg";
  50.     int w,h,ch,gch;
  51.     unsigned char *image = stbi_load(path.c_str(),&w,&h,&ch,0);
  52.  
  53.     unsigned char *im = makeStuff(image,w,h,ch);
  54.  
  55.     stbi_write_jpg("t2.jpg",w,h,ch,im,100);
  56.  
  57.     stbi_image_free(image);
  58.  
  59.     return 0;
  60. }
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement