Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- struct Bitmap{
- int width;
- int height;
- unsigned char * pixels;
- };
- unsigned char header[54];
- struct Bitmap obraz, newImg;
- void readBmp(char * fileName)
- {
- FILE *file = fopen(fileName, "rb");
- fread(header,sizeof(unsigned char),54,file);
- obraz.width = *(int*)&header[18];
- obraz.height = *(int*)&header[22];
- int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
- obraz.pixels = (unsigned char *)malloc(N);
- fread(obraz.pixels,sizeof(unsigned char *),N,file);
- fclose(file);
- }
- unsigned char medianaCpu(unsigned char * mask){
- for (int i = 0; i < 8 ; i++){
- for (int j = 0; j <7; j++){
- int sort = 1;
- if(mask[j] > mask[j+1]){
- int temp = mask[j+1];
- mask[j+1] = mask[j];
- mask[j] = temp;
- sort = 0;
- }
- if(j == 7 && sort == 1){
- return mask[4];
- }
- }
- }
- // for(int i = 0;i<9;i++){
- // printf("%d\n",mask[i] );
- // }
- return mask[4];
- }
- void filtrMed(){
- unsigned char maskR[9];
- unsigned char maskG[9];
- unsigned char maskB[9];
- newImg.width = obraz.width;
- newImg.height = obraz.height;
- printf("%d\n",newImg.width );
- printf("%d\n",newImg.height );
- int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
- newImg.pixels = (unsigned char *)malloc(N);
- for(int y = 0;y<obraz.height - 2; y++ ){
- for(int x = 0;x<obraz.width - 2;x++){
- int i = 0;
- for(int maskY = y; maskY<y+3;maskY++){
- for(int maskX = x;maskX<x+3;maskX++){
- maskR[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 0];
- maskG[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 1];
- maskB[i] = obraz.pixels[(maskY*obraz.width*3) + (maskX * 3) + 2];
- i++;
- }
- }
- newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+0] = medianaCpu(maskR);
- //printf("%d\n",newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+0]);
- newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+1] = medianaCpu(maskG);
- newImg.pixels[(y+1)*obraz.width * 3 + (x+1)*3+2] = medianaCpu(maskB);
- }
- }
- }
- void saveBmp(char * fileName){
- FILE * file = fopen(fileName , "wb");
- int N = ((((obraz.width * obraz.height) + 31 ) & ~31)/8) * obraz.height;
- fwrite(header,sizeof(char),54,file);
- fwrite(newImg.pixels,sizeof(char),N,file);
- fclose(file);
- }
- int main(int argc, char** argv){
- //struct Bitmap obraz;
- unsigned char temp[9] = {0,1,19,34,50,2,4,5,6};
- readBmp("pkg.bmp");
- //struct Bitmap newImg;
- filtrMed();
- for(int i = 0;i<1000;i++){
- printf("%c\n",newImg.pixels[i] );
- }
- saveBmp("pkg_new.bmp");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement