bm暗暗p.rar

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 4KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 26
    下载次数
  • 2005-06-01 01:42
    上传日期
这是一个对bmp格式文件图象读取的源程序,可以用它高效实现编码,注释详细,可读性好.
bm暗暗p.rar
  • bmp
  • bmp.c
    8.1KB
  • bmp.h
    5.2KB
  • www.pudn.com.txt
    218B
内容介绍
/************************************************************************* * * File: bmp.c * Author: Jianxin Li * * Copyright (C) 2003 Jianxin Li * contact the author (ljx97@126.com) to determine copyright restrictions. * In general, permission to copy is granted for research purposes as * long as this copyright notice appears in the header. * * Description: This file provides an implementation of bmp reading and writing * * Log: Version 1.0 2002/03/06 Initial version. * Version 1.1 2002/10/23 Fix some bugs. * Version 1.2 2003/01/26 Add some functions of image. * Version 1.3 2004/01/16 Remove some functions, simplify something. * * * Todo: * * Note: Define DATA_TYPE_INT to obtain differnt data types. * When CompNum = 1, only use one data buffer -- gray. When * CompNum = 3, use three data buffer -- DataA, DataB, DataC. * DataA is for B component, DataB is for G component, and DataC is * for R component. * ************************************************************************/ #include "bmp.h" IMAGE *BmpRead(FILE* InBmp) { int n, x, y; int byte, count, mask, type, compression; int width, height, BitCount, CompNum, ColorNum; int LineBytes, BytesRead, ColorIndex; unsigned char r, g, b; unsigned char ColorMap[3 * 256]; unsigned char PadZeros[4], BmpHeader[54]; IMAGE *image; fread(BmpHeader, sizeof(unsigned char), 54, InBmp); type = (BmpHeader[1] << 8) + BmpHeader[0]; width = (BmpHeader[19] << 8) + BmpHeader[18]; height = (BmpHeader[23] << 8) + BmpHeader[22]; BitCount = (BmpHeader[29] << 8) + BmpHeader[28]; compression = (BmpHeader[31] << 8) + BmpHeader[32]; LineBytes = (width * BitCount + 31) / 32 * 4; if (type != ((unsigned short)('M' << 8) | 'B')) { printf("the file maybe is not a bmp image.\n"); return NULL; } if (compression != 0) { printf("can not read a compressed bmp.\n"); return NULL; } if (BitCount > 8 && BitCount != 24) { printf("can not read a %d bit bmp.\n", BitCount); return NULL; } if (BitCount == 24) { CompNum = 3; if ((image = ImageAlloc(height, width, CompNum)) == NULL) { printf("fail to allocate image.\n"); return NULL; } for (y = height - 1; y >= 0; y--) { for (x = 0; x < width; x++) { fread(&b, sizeof(unsigned char), 1, InBmp); fread(&g, sizeof(unsigned char), 1, InBmp); fread(&r, sizeof(unsigned char), 1, InBmp); image->DataA[y * width + x] = b; image->DataB[y * width + x] = g; image->DataC[y * width + x] = r; } fread(PadZeros, sizeof(unsigned char), (LineBytes - width * 3), InBmp); } } else if (BitCount <= 8) { CompNum = 1; ColorNum = 1 << BitCount; for (n = 0; n < ColorNum; n++) { fread(&b, sizeof(unsigned char), 1, InBmp); fread(&g, sizeof(unsigned char), 1, InBmp); fread(&r, sizeof(unsigned char), 1, InBmp); fread(PadZeros, sizeof(unsigned char), 1, InBmp); ColorMap[n * 3 + 0] = b; ColorMap[n * 3 + 1] = g; ColorMap[n * 3 + 2] = r; if (b != g || b != r || g != r) CompNum = 3; } if ((image = ImageAlloc(height, width, CompNum)) == NULL) { printf("fail to allocate image.\n"); return NULL; } count = 0; BytesRead = 0; mask = (1 << BitCount) - 1; for (y = height - 1; y >= 0; y--) { for (x = 0; x < width; x++) { if (count <= 0) { count = 8; BytesRead += 1; fread(&byte, sizeof(unsigned char), 1, InBmp); } count -= BitCount; ColorIndex = (byte >> count) & mask; if (CompNum == 1) { image->gray[y * width + x] = ColorMap[ColorIndex * 3 + 0]; } else if (CompNum == 3) { image->DataA[y * width + x] = ColorMap[ColorIndex * 3 + 0]; image->DataB[y * width + x] = ColorMap[ColorIndex * 3 + 1]; image->DataC[y * width + x] = ColorMap[ColorIndex * 3 + 2]; } } if ((4 - BytesRead % 4) != 4) { fread(PadZeros, sizeof(unsigned char), (4 - BytesRead % 4), InBmp); } count = 0; BytesRead = 0; } } return image; } void BmpWrite(FILE* OutBmp, IMAGE *image) { int x, y; int height, width, BitCount, OffBits; int LineBytes, PadBytes, BmpBytes, ImageBytes; IMGDATATYPE TempB, TempG, TempR, TempGray; unsigned char b, g, r, gray; unsigned char BmpHeader[54], PadZeros[4] = {0, 0, 0, 0}; height = image->height; width = image->width; BitCount = 24; LineBytes = (width * BitCount + 31) / 32 * 4; ImageBytes = LineBytes * height; OffBits = 54 + ((BitCount == 24) ? 0 : ((1 << BitCount) * 4)); BmpBytes = OffBits + ImageBytes; PadBytes = LineBytes - width * 3; memset(BmpHeader, 0 , 54); *(BmpHeader + 0) = 'B'; *(BmpHeader + 1) = 'M'; *(BmpHeader + 2) = (unsigned char)(BmpBytes); *(BmpHeader + 3) = (unsigned char)(BmpBytes >> 8); *(BmpHeader + 4) = (unsigned char)(BmpBytes >> 16); *(BmpHeader + 5) = (unsigned char)(BmpBytes >> 32); *(BmpHeader + 10) = (unsigned char)(OffBits); *(BmpHeader + 11) = (unsigned char)(OffBits >> 8); *(BmpHeader + 14) = 40; *(BmpHeader + 18) = (unsigned char)(width); *(BmpHeader + 19) = (unsigned char)(width >> 8); *(BmpHeader + 22) = (unsigned char)(height); *(BmpHeader + 23) = (unsigned char)(height >> 8); *(BmpHeader + 26) = 1; *(BmpHeader + 28) = (unsigned char)(BitCount); *(BmpHeader + 34) = (unsigned char)(ImageBytes); *(BmpHeader + 35) = (unsigned char)(ImageBytes >> 8); *(BmpHeader + 36) = (unsigned char)(ImageBytes >> 16); *(BmpHeader + 37) = (unsigned char)(ImageBytes >> 32); fwrite(BmpHeader, sizeof(char), 54, OutBmp); if (image->CompNum == 1) { for (y = height - 1; y >= 0; y--) { for (x = 0; x < width ; x++) { TempGray = image->gray[y * width + x]; TempGray = (IMGDATATYPE)ROUND(TempGray); gray = (unsigned char)BOUND(TempGray, 0, 255); fwrite(&gray, sizeof(unsigned char), 1, OutBmp); fwrite(&gray, sizeof(unsigned char), 1, OutBmp); fwrite(&gray, sizeof(unsigned char), 1, OutBmp); } fwrite(PadZeros, sizeof(unsigned char), PadBytes, OutBmp); } } else if (image->CompNum == 3) { for (y = height - 1; y >= 0; y--) { for (x = 0; x <width ; x++) { TempB = image->DataA[y * width + x]; TempG = image->DataB[y * width + x]; TempR = image->DataC[y * width + x]; TempB = (IMGDATATYPE)ROUND(TempB); TempG = (IMGDATATYPE)ROUND(TempG); TempR = (IMGDATATYPE)ROUND(TempR); b = (unsigned char)BOUND(TempB, 0, 255); g = (unsigned char)BOUND(TempG, 0, 255); r = (unsigned char)BOUND(TempR, 0, 255); fwrite(&b, sizeof(unsigned char), 1, OutBmp); fwrite(&g, sizeof(unsigned char), 1, OutBmp); fwrite(&r, sizeof(unsigned char), 1, OutBmp); } fwrite(PadZeros, sizeof(unsigned char), PadBytes, OutBmp); } } } IMAGE* ImageAlloc(int height, int width, int CompNum) { IMAGE* image; if ((image = (IMAGE *) malloc (sizeof(IMAGE))) == NULL) { printf("fail to allocate memory image.\n"); return NULL; } if (CompNum == 1) { if ((image->gray = (IMGDATATYPE *) calloc (height * width, sizeof(IMGDATATYPE))) == NULL) { printf("fail to allocate image->gray.\n"); return NULL; } } else if (CompNum == 3) { if ((image->DataA = (IMGDATATYPE *) calloc (height * width, sizeof(IMGDATATYPE))) == NULL) { printf("fail to allocate image->DataA.\n"); return NULL; } if ((image->DataB = (IMGDATATYPE *) calloc (height * width, sizeof(IMGDATATYPE))) == NULL) { printf("fail to allocate image->DataB.\n"); return NULL; } if ((image->DataC = (IMGDATATYPE *) calloc (height * width, sizeof(IMGDATATYPE))) == NULL) { printf("fail to allocate image->DataC.\n"); return NULL; } } image->CompNum = CompNu
评论
    相关推荐
    • BMP.rar
      windows bmp图像文件处理相关类,以及DIB类
    • bmp.rar
      学习摄像头,BMP编码/JPEG编码和文件系统的使用,实现一个简单的照相机.
    • ShowBMP.rar
      使用C++编码实现在 vc6.0中bmp图片的显示
    • 字符集编码开发总结-BMP查看器
      NULL 博文链接:https://explorer.iteye.com/blog/436529
    • C实现将BMP文件转base64编码
      基于C/C++实现的将标准BMP格式图片,转换成标准base64编码文件
    • bmp-js:用于node.js的纯JavaScript BMP编码器和解码器
      适用于node.js的纯JavaScript Bmp编码器和解码器 支持所有位解码(1、4、8、16、24、32)和24位编码。 安装 npm install bmp-js 如何使用 解码BMP var bmp = require ( "bmp-js" ) ; var bmpBuffer = fs . ...
    • Huff:BMP文件的霍夫曼编码
      “ huff.c”为BMP图像实现了霍夫曼编码算法。 测试需要实现生成新BMP图像的功能,因此我决定将其作为程序的第二功能。 安装 包含一个Makefile,其中包含必要的标志。 它可以在Windows,MacOS和任何Linux发行版上运行...
    • bmp文件处理
      bmp图像处理成数组存储,使用C++编码
    • BMP图片编码x264文件
      使用FFMpeg可以很方便的对音视频进行编码,并且写文件。 下面的代码是将5幅1280*720大小的图片进行编码,并且写到文件中
    • bmp2rle:使用游程编码对位图 (bmp) 文件进行编码-开源
      使用行程编码(RLE)对位图(bmp)文件进行编码,就像 RLE4 和 RLE8,但扩展到 RLE16、RLE24 和 RLE32。 通常,该项目会用于嵌入式系统的GUI,使图像文件更小,并节省IO 带宽。 如果GUI支持JPG或PNG,压缩率比RLE好...