tiff灰度图转换bmp灰度图

 
bmpFormat.h

--------------------------------------------

#ifndef _BMPTEST_H_
#define _BMPTEST_H_

#include <stdio.h>

      typedef unsigned char BYTE;
      typedef unsigned short WORD;

      typedef struct {
            long imageSize;
            long blank;
            long startPostition;
      }BmpHead;

      typedef struct {
            long length;
            long width;
            long height;
            WORD colorPlane;
            WORD bitColor;
            long zipFormat;
            long readSize;
            long xPels;
            long yPels;
            long colorUse;
            long colorImportant;
      }InfoHead;

      typedef struct {
            BYTE rgbBlue;
            BYTE rgbGreen;
            BYTE rgbRed;
            BYTE rgbReserved;
      }RGBMixPlate;

      #endif

--------------------------------------------

     create.c

--------------------------------------------

#include "bmpFormat.h"
#include <tiffio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
      //tiff文件涉及变量
      TIFF *image;                                       //tiff文件指针
      uint32 width, height;                           //图片的宽度和高度
      char *buffer;                                      //保存tiff文件中的图像灰度信息缓存
      tsize_t stripSize;                                  //tiff中图像的条大小
      unsigned long bufferSize;                     //缓存大小
      int stripMax, stripCount;                       //图像总共的条数和读条信息时的控制变量
      unsigned long imageOffset, result;         //读文件信息时的控制变量和每次读的结果

      //循环控制变量

      unsigned int i;
      unsigned int j;
  
      //bmp文件涉及变量
      BmpHead bmphead;        //bmp文件头(不包括固定信息)

      InfoHead infohead;         //bmp信息头
      RGBMixPlate *color;         //调色板
      BYTE *index;                //图像数据在调色板中的索引值
      FILE *bmp;                    //bmp文件指针
      char bm[2]={'B','M'};     //bmp文件头中的一块固定信息,表示这是一张BMP图片

      // 打开tiff图片

      if((image = TIFFOpen(argv[1], "r")) == NULL){
            fprintf(stderr, "Could not open incoming image\n");
            return -1;
      }

      //获取图片的宽度和高度

      TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
      TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
  
      stripSize = TIFFStripSize (image);   //每条的大小
      stripMax = TIFFNumberOfStrips (image);     //一共有多少条
      imageOffset = 0;                             //后面读取文件信息时使用的偏移量
  
      //根据文件信息申请相应的数据空间
      bufferSize = TIFFNumberOfStrips (image) * stripSize;
      if((buffer = (char *) malloc(bufferSize)) == NULL)
      {
            fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");
            return -1;
      }

      //读取文件中的灰度信息
      for (stripCount = 0; stripCount < stripMax; stripCount++)
      {
            if((result = TIFFReadEncodedStrip (image, stripCount,buffer + imageOffset,stripSize)) == -1)
                  {
                        fprintf(stderr, "Read error on input strip number %d\n", stripCount);
                        return -1;
                  }
            imageOffset += result;
      }
  

      //bmp文件头成员赋值
      bmphead.blank=0;                                              //保留字,为0
      bmphead.imageSize=14+40+4*256+width*height; //图片的大小=文件头大小+信息头大小+调色板大小+实际图片大小
      bmphead.startPostition=14+40+4*256;                 //图片实际数据相对于文件头的偏移量

      //bmp信息头成员赋值(可参见BMP文件解析每个成员的定义)

      infohead.length=40;
      infohead.width=width;
      infohead.height=height;
      infohead.colorPlane=1;
      infohead.bitColor=8;
      infohead.zipFormat=0;
      infohead.readSize=width*height;
      infohead.xPels=400;
      infohead.yPels=300;
      infohead.colorUse=0;
      infohead.colorImportant=0;

      //申请调色板和数据域空间

      color=(RGBMixPlate *)malloc(sizeof(RGBMixPlate)*256);
      index=(BYTE *)malloc(sizeof(BYTE)*infohead.readSize);

      //生成256色灰度调色板

      for(i=0;i<256;i++)
      {
            color[i].rgbBlue=i;
            color[i].rgbGreen=i;
            color[i].rgbRed=i;
            color[i].rgbReserved=0;
      }
  

      //256色灰度bmp的数据信息实际就是该灰度在相应灰度值的调色板中的颜色
      for(i=0;i<height;i++)
            for(j=0;j<width;j++)
                  index[i*width+j]=buffer[(height-1-i)*width+j];     //tiff图片是从左上到右下,bmp图片是从左下到右上

  

      //创建bmp图片
      bmp=fopen(argv[2],"wb");
      if(bmp==NULL)
      {
            printf("open new file error\n");
            return -1;
      }

      //写入相应的数据
      fwrite(bm,1,2,bmp);
      fwrite(&bmphead,1,12,bmp);
      fwrite(&infohead,1,40,bmp);
      fwrite(color,1,256*sizeof(color),bmp);
      fwrite(index,1,infohead.readSize,bmp);
      fclose(bmp);
  

      //释放有关资源
      free(buffer);
      free(color);
      free(index);
      TIFFClose(image);

      fclose(bmp);
      return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值