前面提高了一个将BMP左转的程序,右转其实也是类似的操作,就不写了,这节,我们来实现,将一张BMP图进行灰度处理,代码贴上:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define  RGB(r,g,b)    		((r+g+b)/3)

typedef  unsigned char  U8 ;
typedef  unsigned short U16 ;
typedef  unsigned int   U32 ; 

#pragma  pack(1)
struct bmp_header
{
	//bmp header
	U8  Signatue[2] ;   // B  M
	U32 FileSize ;     //文件大小
	U16 Reserv1 ;
	U16 Reserv2 ;
	U32 FileOffset ;   //文件头偏移量

	//DIB header
	U32 DIBHeaderSize ; //DIB头大小
	U32 ImageWidth   ;  //文件宽度
	U32 ImageHight   ;  //文件高度
	U16 Planes       ;
	U16 BPP          ;  //每个相素点的位数
	U32 Compression  ;
	U32 ImageSize    ;  //图文件大小
	U32 XPPM ;
	U32 YPPM ;
	U32 CCT ;
	U32 ICC ;
};
#pragma  pack()

int main(int argc , char **argv)
{
	if(argc != 4)
		return -1 ; 

	int fd ;
	int dest_fd ;
	fd = open(argv[1] , O_RDONLY);
	if(-1 == fd)
	{
		perror("open bmp file fail");
		return -2 ;
	}

	dest_fd = open( argv[2] , O_RDWR | O_CREAT|O_TRUNC , 0777);
	if(dest_fd < 0 )
	{
		perror("open rgb565 fail");
		return -3 ;
	}
	int value = atoi(argv[3]);

	struct bmp_header  header ; 

	int ret ; 

	ret = read(fd , &header , sizeof(struct bmp_header));

	printf(" Signatue[0]      : %c  \n " , header.Signatue[0]  );
	printf(" Signatue[1]      : %c  \n " , header.Signatue[1]  );
	printf(" FileSize         : %d  \n " , header.FileSize     );
	printf(" Reserv1          : %d  \n " , header.Reserv1      );
	printf(" Reserv2          : %d  \n " , header.Reserv2      );
	printf(" FileOffset       : %d  \n " , header.FileOffset   );
	printf(" DIBHeaderSize    : %d  \n " , header.DIBHeaderSize);
	printf(" ImageWidth       : %d  \n " , header.ImageWidth   );
	printf(" ImageHight       : %d  \n " , header.ImageHight   );
	printf(" Planes           : %d  \n " , header.Planes       );
	printf(" BPP              : %d  \n " , header.BPP          );
	printf(" Compression      : %d  \n " , header.Compression  );
	printf(" ImageSize        : %d  \n " , header.ImageSize    );
	printf(" XPPM             : %d  \n " , header.XPPM         );
	printf(" YPPM             : %d  \n " , header.YPPM         );
	printf(" CCT              : %d  \n " , header.CCT          );
	printf(" ICC              : %d  \n " , header.ICC          );

	char buffer[header.ImageSize] ; 

	read(fd , buffer , header.ImageSize);

	close(fd);

	//改变地方

	write(dest_fd , &header , sizeof(struct bmp_header));

	int row , col ;
	char *p = NULL ; 

	for(row = 0 ; row < 10 ; row++)
	{
		for(col = 0 ; col < 10 ; col++)
		{
			p = buffer + (row * 800 + col)*3 ;
			*p = 255;
			*(p+1)=255;
			*(p+2)=255;
		}
	}

	char data ;
	for(row = 0 ; row < 480  ; row++)
	{
		for(col = 0 ; col < 800 ; col++)
		{
			p =( buffer+(row*800 + col)*3);
			data = RGB((unsigned char)(*(p+2)) , (unsigned char)(*(p+1)) ,
			(unsigned char )(*(p)));
			if(data < value)
			{
			*p = 0;
			*(p+1)=0;
			*(p+2)=0;
			}
			else
			{
			*p = data;
			*(p+1)=data;
			*(p+2)=data;
			}
			write(dest_fd , p , 3);
		}
	}

	close(dest_fd);

	return 0 ;
}

linu下C语言之BMP图片操作编程(下)的更多相关文章

  1. linu下C语言之BMP图片操作编程(上)

    BMP文件格式,也被称为位图图像文件或与设备无关的位图文件格式(DIB)或者只是一个位图,是 一个光栅图形 图像文件格式使用 来存储位图,数字,图片,独立的显示设备. 微软已经定义了一个特定的表示颜色 ...

  2. linu下C语言之BMP图片操作编程(中)

    http://blog.csdn.net/morixinguan/article/details/50719472 关于BMP图的介绍之前已经说过了,最近要用到,又要重新开始学习. 现在实现一个让bm ...

  3. 【C】用C语言提取bmp图片像素,并进行K-means聚类分析——容易遇到的问题

    关于bmp图片的格式,网上有很多文章,具体可以参考百度百科,也有例子程序.这里只提要注意的问题. (1)结构体定义问题:首先按照百度百科介绍的定义了结构体,但是编译发现重定义BITMAPFILEHEA ...

  4. C语言实现BMP图片生成

    ## #include <stdio.h> #include <stdlib.h> #include <string.h> typedef unsigned cha ...

  5. Go语言基础之接口(面向对象编程下)

    1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...

  6. .NET Core 图片操作在 Linux/Docker 下的坑

    一.前言 .NET Core 目前更新到2.2了,但是直到现在在 .NET Core 本身依然不包括和图片有关的 Image.Bitmap 等类型.对于图片的操作在我们开发中很常见,比如:生成验证码. ...

  7. 位图文件格式及linux下C语言来操作位图文件

    说到图片,位图(Bitmap)当然是最简单的,它是Windows显示图片的基本格式,其文件扩展名为*.BMP.由于没有经过任何的压缩,故BMP图片往往很大.在Windows下,任何格式的图片文件都要转 ...

  8. 嵌入式linux------SDL移植(am335x下显示bmp图片)

    #include<stdio.h> #include "/usr/local/ffmpeg_arm/include/SDL/SDL.h" char *bmp_name[ ...

  9. bmp图片的有关操作

    读取bmp图片 并生成新的bmp图片 #include "stdafx.h"#include <windows.h>#include <cmath>#inc ...

随机推荐

  1. [BZOJ]3672 购票(Noi2014)

    革命尚未成功,同志还需努力. Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ ...

  2. 笔记7 AOP练习<有疑问>

    场景描述: 核心业务:举行一场古典音乐会. 周边功能:观众入场,关闭手机.落座,觉得音乐好听时鼓掌,觉都不好听则退票.(切面) 1.编写切点(切点用于准确定位应该在什么地方应用切面的通 知)----即 ...

  3. 在 TensorFlow 中实现文本分类的卷积神经网络

    在TensorFlow中实现文本分类的卷积神经网络 Github提供了完整的代码: https://github.com/dennybritz/cnn-text-classification-tf 在 ...

  4. 以太坊区块链Java(EthereumJ)学习笔记:概述

    本系列文章介绍以太坊区块链基于Java语言的解决方案.通过介绍EthereumJ定义的主要模块和Class,希望为大家学习和使用EthereumJ提供一些帮助. 整体架构 以太坊的Java解决方案主要 ...

  5. 通过接口标准化ABAP OO开发

    本文是对接口编程的讨论,希望能对年轻的开发者有所帮助. 要点: 通过接口对类方法进行更高层的抽象 接口使代码清晰易读 接口使你可以创建模拟对象(Mockup Object)以提高代码的可测试性 帮助实 ...

  6. css文本超出省略号

    终于完成了项目了,今天我就分享自己在项目中比较实用的一些功能的实现,第一个就是纯css文本超出省略号 /* 文本单行超出省略号 */ .textels { overflow: hidden; text ...

  7. Linux 管理软件

    公司的openfire先前运行在windows上的,但由于在windows上openfire内存机制问题,最多只能占用2GB内存,且时间稍微长久一些就会自动挂掉,用户无法登陆和连接,因此迁移到了Cen ...

  8. HTTP与TCP的关系

    一直比较想写TCP与HTTP之间的关系,HTTP报文是如何通过tcp发送的,HTTP报文形式内容如何. HTTP请求包含请求行,请求头,请求体 HTTP响应包含响应头,响应头,响应体 下面我准备通过J ...

  9. python脚本文件传参并通过token登录后爬取数据实例

    from bs4 import BeautifulSoup import requests import sys class Zabbix(object): def __init__(self, he ...

  10. U盘PE无人值守安装centOS6

    一.制作 1.需要用到的工具:老毛桃PX工具.系统ISO.一个8GU盘 老毛桃PE工具 http://laomaotao.net/ CentOS启动映像 http://mirrors.163.com/ ...