LZ你好
我用了你的代码生成了DLL然后写了个测试程序。
测试程序如下:
- #pragma comment(lib, "Octree.lib")
-
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include "windows.h"
- #include "Octree.h"
- #include <math.h>
- #include <stdlib.h>
-
-
- using namespace std;
-
- bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, PALETTEITEM *pColorTable)
- {
-
- //如果位图数据指针为0,则没有数据传入,函数返回
-
- if(!imgBuf)
- return 0;
-
- //颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
-
- int colorTablesize=0;
-
- if(biBitCount==8)
- colorTablesize=1024;
-
- //待存储图像数据每行字节数为4的倍数
-
- int lineByte=(width * biBitCount/8+3)/4*4;
-
- //以二进制写的方式打开文件
-
- FILE *fp=fopen(bmpName,"wb");
-
- if(fp==0)
- return 0;
-
- //申请位图文件头结构变量,填写文件头信息
-
- BITMAPFILEHEADER fileHead;
-
- fileHead.bfType = 0x4D42;//bmp类型
-
- //bfSize是图像文件4个组成部分之和
-
- fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*height;
-
- fileHead.bfReserved1 = 0;
-
- fileHead.bfReserved2 = 0;
-
- //bfOffBits是图像文件前3个部分所需空间之和
-
- fileHead.bfOffBits=54+colorTablesize;
-
- //写文件头进文件
-
- fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);
-
- //申请位图信息头结构变量,填写信息头信息
-
- BITMAPINFOHEADER head;
-
- head.biBitCount=biBitCount;
-
- head.biClrImportant=0;
-
- head.biClrUsed=0;
-
- head.biCompression=0;
-
- head.biHeight=height;
-
- head.biPlanes=1;
-
- head.biSize=40;
-
- head.biSizeImage=lineByte*height;
-
- head.biWidth=width;
-
- head.biXPelsPerMeter=0;
-
- head.biYPelsPerMeter=0;
-
- //写位图信息头进内存
-
- fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);
-
- //如果灰度图像,有颜色表,写入文件
-
- if(biBitCount==8)
- fwrite(pColorTable, sizeof(PALETTEITEM),256, fp);
-
- //写位图数据进文件
-
- fwrite(imgBuf, height*lineByte, 1, fp);
-
- //关闭文件
-
- fclose(fp);
-
- return 1;
-
- }
-
-
-
-
- int main()
- {
- //与BMP相关的定义
- BITMAPFILEHEADER bf;
- BITMAPINFOHEADER bi;
- byte bColor[3];
- LONG biWidth=601; /* 以像素为单位说明图像的宽度 */
- LONG biHeight=500; /* 以像素为单位说明图像的高度 *///100G的数据为1760
- DWORD biSizeImage;
- RGBQUAD *RGBquad; //调色板
- int RealUsedColor = 0; //说明图像实际用到的颜色数,如果biClrUsed为0则颜色数为2的biBitCount次方
-
- //打开输出的BMP文件
-
- //行方向上像素的字节数必须是4的整数倍
- unsigned long biFullWidth;
- biFullWidth=ceil(biWidth/4.)*4;//灰度图像
-
- FILE *fpIn;
- if (!(fpIn = fopen("test_in.bmp", "rb"))) {
- printf("can't create d:\testBMP.bmp");
- return 0;
- }
- FILE *fpout;
- if (!(fpout = fopen("test_out.bmp", "wb"))) {
- printf("can't create d:\testBMP.bmp");
- return 0;
- }
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, fpIn);
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, fpIn);
-
-
-
- //此处添加代码
- /*
- if(bi.biClrUsed == 0){RealUsedColor = 2^bi.biBitCount;}else{RealUsedColor = bi.biClrUsed;} //判断实际颜色数
- for(int i = 0; i < RealUsedColor; i++) //读入调色盘
- {
- fread(&RGBquad[i].rgbBlue, 1, sizeof(BYTE), fpIn);
- fread(&RGBquad[i].rgbGreen, 1, sizeof(BYTE), fpIn);
- fread(&RGBquad[i].rgbRed, 1, sizeof(BYTE), fpIn);
- fread(&RGBquad[i].rgbReserved, 1, sizeof(BYTE), fpIn);
- }
- cout<<RGBquad[0].rgbBlue;
- *///真彩色图不用调色盘
-
- if(bi.biBitCount == 8)//256色调色盘
- {
- RGBquad = new RGBQUAD[256];
- fread(RGBquad, sizeof(RGBQUAD), 256, fpIn);
- }
-
- int Datalen = ((bi.biWidth*bi.biBitCount/8)+3)/4*4*bi.biHeight;//读取位图数据
- BYTE *Bmpbuf = new BYTE[Datalen];
- fseek(fpIn, bf.bfOffBits,SEEK_SET);
- fread(Bmpbuf, 1, Datalen, fpIn);
-
- UINT uPitch = GetBitmapPitch(bi.biBitCount,bi.biWidth);
-
- PALETTEITEM *pPaletteOut = new PALETTEITEM[256];
- // BYTE *Bmpbuf = new BYTE[256*4];
-
- CreateOctreePaletteRGB888(Bmpbuf, bi.biWidth, bi.biHeight, uPitch, 256, 8, pPaletteOut);
- /*
- for(int i=0;i<1024;i = i+3)
- {
- Bmpbuf[i] = pPaletteOut[i/4].R;
- Bmpbuf[i+1] = pPaletteOut[i/4].G;
- Bmpbuf[i+2] = pPaletteOut[i/4].B;
- }
- */
-
- saveBmp("test_out.bmp", Bmpbuf, bi.biWidth, bi.biHeight, 8, pPaletteOut);
-
-
-
- return 0;
- }
-
-
-
-
-
-
复制代码
这是我的运行结果:
很显然没有达到预期目的啊,不知是楼主的程序问题还是我的读写问题啊?
附上我的测试图片:
test_in.rar
(321.85 KB, 下载次数: 5)
|