模糊的使用C位图效果 [英] Blur effect on bitmap using C

查看:256
本文介绍了模糊的使用C位图效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写程序的MP4应用模糊滤镜。我是从MP4 exctracting BMP文件的有关ffmpeg。
但模糊的结果是错误的。画面的某些部分已经被正确地模糊了,但其他部分有错误的颜色。

I'm writing program applying blur filter on mp4. I'm exctracting bmp files from mp4 with ffmpeg. But the blur result is wrong. Some parts of picture have been correctly blurred but other parts have wrong colors.

原始图像
http://i.imgur.com/XS4yqNd.jpg

模糊图像
http://i.imgur.com/IbcxxA4.jpg

这是code读取BMP,运用模糊和写入BMP文件。

This is the code for reading bmp, applying blur and writing to bmp file.

int blur(char* input, char *output) {

  //variable dec:
  FILE *fp,*out;
  bitmap_header* hp;
  int n,x,xx,y,yy,ile, avgR,avgB,avgG,B,G,R;
  char *data;
  int blurSize = 5;


  //Open input file:
  fp = fopen(input, "r");
  if(fp==NULL){
    //cleanup
  }

  //Read the input file headers:
  hp=(bitmap_header*)malloc(sizeof(bitmap_header));
  if(hp==NULL)
    return 3;

  n=fread(hp, sizeof(bitmap_header), 1, fp);
  if(n<1){
    //cleanup
  }
  //Read the data of the image:
  data = (char*)malloc(sizeof(char)*hp->bitmapsize);
  if(data==NULL){
    //cleanup
  }

  fseek(fp,sizeof(char)*hp->fileheader.dataoffset,SEEK_SET);
  n=fread(data,sizeof(char),hp->bitmapsize, fp);
  if(n<1){
    //cleanup
  }

for(xx = 0; xx < hp->width; xx++)
{
    for(yy = 0; yy < hp->height; yy++)
    {
        avgB = avgG = avgR = 0;
        ile = 0;

        for(x = xx; x < hp->width && x < xx + blurSize; x++)
        {


            for(y = yy; y < hp->height && y < yy + blurSize; y++)
            {
                avgB += data[x*3 + y*hp->width*3 + 0];
                avgG += data[x*3 + y*hp->width*3 + 1];
                avgR += data[x*3 + y*hp->width*3 + 2];
                ile++;
            }
        }

        avgB = avgB / ile;
        avgG = avgG / ile;
        avgR = avgR / ile;

        data[xx*3 + yy*hp->width*3 + 0] = avgB;
        data[xx*3 + yy*hp->width*3 + 1] = avgG;
        data[xx*3 + yy*hp->width*3 + 2] = avgR;
    }
}

    //Open output file:
  out = fopen(output, "wb");
  if(out==NULL){
    //cleanup
  }

  n=fwrite(hp,sizeof(char),sizeof(bitmap_header),out);
  if(n<1){
    //cleanup
  }
  fseek(out,sizeof(char)*hp->fileheader.dataoffset,SEEK_SET);
  n=fwrite(data,sizeof(char),hp->bitmapsize,out);
  if(n<1){
    //cleanup
  }

  fclose(fp);
  fclose(out);
  free(hp);
  free(data);
  return 0;
}

我不知道问题出在哪里。请帮助!

I don't know where is the problem. Please help!

推荐答案

这似乎字符签署您平台。因此所有的颜色值高于大 127 是相互preTED为负数( 256 比原来的值小于)。这会导致所有奇怪的色彩效果。

It seems char is signed on your platform. Because of this all color values bigger than 127 are interpreted as negative numbers (256 less than original value). This causes all weird color effects.

要纠正它,你可以声明数据无符号字符*

To correct it you can declare data as unsigned char*:

unsigned char *data;

这篇关于模糊的使用C位图效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆