Advertisement
Guest User

pkg

a guest
Dec 9th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6.  
  7.  
  8. struct Bitmap{
  9. int width;
  10. int height;
  11. unsigned char * pixels;
  12. };
  13. unsigned char header[54];
  14.  
  15. struct Bitmap obraz, newImg;
  16.  
  17. void readBmp(char * fileName)
  18. {
  19.  
  20. FILE *file = fopen(fileName, "rb");
  21. fread(header,sizeof(unsigned char),54,file);
  22.  
  23.  
  24. obraz.width = *(int*)&header[18];
  25. obraz.height = *(int*)&header[22];
  26. int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
  27. obraz.pixels = (unsigned char *)malloc(N);
  28. fread(obraz.pixels,sizeof(unsigned char *),N,file);
  29.  
  30.  
  31. fclose(file);
  32. }
  33.  
  34.  
  35. unsigned char medianaCpu(unsigned char * mask){
  36.  
  37. for (int i = 0; i < 8 ; i++){
  38. for (int j = 0; j <7; j++){
  39. int sort = 1;
  40. if(mask[j] > mask[j+1]){
  41. int temp = mask[j+1];
  42. mask[j+1] = mask[j];
  43. mask[j] = temp;
  44. sort = 0;
  45. }
  46. if(j == 7 && sort == 1){
  47. return mask[4];
  48. }
  49. }
  50. }
  51. // for(int i = 0;i<9;i++){
  52. // printf("%d\n",mask[i] );
  53. // }
  54. return mask[4];
  55. }
  56.  
  57.  
  58. void filtrMed(){
  59. unsigned char maskR[9];
  60. unsigned char maskG[9];
  61. unsigned char maskB[9];
  62. newImg.width = obraz.width;
  63. newImg.height = obraz.height;
  64. printf("%d\n",newImg.width );
  65. printf("%d\n",newImg.height );
  66. int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
  67. newImg.pixels = (unsigned char *)malloc(N);
  68.  
  69. for(int y = 0;y<obraz.height - 2; y++ ){
  70. for(int x = 0;x<obraz.width - 2;x++){
  71. int i = 0;
  72. for(int maskY = y; maskY<y+3;maskY++){
  73. for(int maskX = x;maskX<x+3;maskX++){
  74. maskR[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 0];
  75. maskG[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 1];
  76. maskB[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 2];
  77. i++;
  78. }
  79. }
  80. newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+0] = medianaCpu(maskR);
  81. //printf("%d\n",newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+0]);
  82. newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+1] = medianaCpu(maskG);
  83. newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+2] = medianaCpu(maskB);
  84. }
  85. }
  86.  
  87.  
  88. }
  89.  
  90. void saveBmp(char * fileName){
  91.  
  92. FILE * file = fopen(fileName , "wb");
  93. int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
  94. fwrite(header,sizeof(char),54,file);
  95. fwrite(newImg.pixels,sizeof(char),N,file);
  96. fclose(file);
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. int main(int argc, char** argv){
  108.  
  109. //struct Bitmap obraz;
  110.  
  111. unsigned char temp[9] = {0,1,19,34,50,2,4,5,6};
  112.  
  113. readBmp("pkg.bmp");
  114. //struct Bitmap newImg;
  115. filtrMed();
  116. for(int i = 0;i<1000;i++){
  117. printf("%c\n",newImg.pixels[i] );
  118. }
  119. saveBmp("pkg_new.bmp");
  120.  
  121.  
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement