FreeImage库在C++与C#中的简单使用

FreeImage库在C++与C#中的简单使用


1.在C++中读图,合并图片

  分别将FreeImage库解压包中的“.h”,".lib",".dll"文件加入编译器vc的“include”,"lib","bin"文件夹中,在新建的C++文件中申明头文件“FreeImage.h”并包含FreeImage的lib库(#pragma comment(lib,"FreeImage.lib")),这之后便可以在VC中使用FreeImage库了。
  
#include<stdio.h>
#include<FreeImage.h> 
#include<iostream>
#include<math.h>
#pragma comment(lib,"FreeImage.lib")
using namespace std;

int main(int argc,char* argv)
{ 
	char* imageFile="church01.jpg";
	//初始化
	FreeImage_Initialise(TRUE);
	//读取图像
	FIBITMAP * bitmap=FreeImage_Load(FIF_JPEG,imageFile,JPEG_DEFAULT);
	if(bitmap) cout<<"Image Load successful!"<<endl;
	//得到图像相关信息
	FREE_IMAGE_TYPE img_type=FreeImage_GetImageType(bitmap);
	int width=FreeImage_GetWidth(bitmap);
	int height=FreeImage_GetHeight(bitmap);
	int bytespp = FreeImage_GetLine(bitmap)/ width; //单位宽度所含字节数
	int pitch=FreeImage_GetPitch(bitmap); //行扫描宽度
	int ByteWidth = FreeImage_GetLine(bitmap);
	int bpp=FreeImage_GetBPP(bitmap);//图像位深

	cout<<"Image Type: "<<img_type<<endl;
	cout<<"Height: "<<height<<"  Width: "<<width<<endl;
	cout<<"bytespp:"<<bytespp<<endl;
	cout<<"Pitch:" <<pitch<<endl<<"ByteWidth: "<<ByteWidth<<endl;
	cout<<"bpp:"<<bpp<<endl;

<span style="white-space:pre">	</span>//Copy3张图片做,获取3张图片数据首位指针
	//指针数组的声明
	FIBITMAP **src=NULL;
	BYTE * bits[3]={};
	src=new FIBITMAP *[3];
	for(int i=0;i<3;i++)
	{
		src[i]=FreeImage_Copy(bitmap,0,0,width,height); 
		bits[i]=(BYTE*) FreeImage_GetBits(src[i]);
	}
	//分配输出图片空间,获取输出图片数据首位指针
	FIBITMAP * dst=FreeImage_Allocate(3*width,3*height,bpp);
    <span style="white-space:pre">	</span>BYTE * bits_dst=FreeImage_GetBits(dst);

//数据变化
	for(int h=0;h<3;h++)
	{
		for(int i=0;i<height;i++)//在遍历高度的同时设置输入及每一行中每张图片的数据首位指针
			{
				BYTE * pixel_dst1=bits_dst;
				BYTE * pixel_dst2=bits_dst+3*FreeImage_GetWidth(bitmap);
				BYTE * pixel_dst3=bits_dst+2*3*FreeImage_GetWidth(bitmap); 
				BYTE * pixel_src =bits[h];

				for(int j=0;j<width;j++)
					{
						pixel_dst1[FI_RGBA_RED]=pixel_src[FI_RGBA_RED];
						pixel_dst1[FI_RGBA_GREEN]=pixel_src[FI_RGBA_GREEN];
						pixel_dst1[FI_RGBA_BLUE]=pixel_src[FI_RGBA_BLUE];
						pixel_dst2[FI_RGBA_RED]=pixel_src[FI_RGBA_RED];
						pixel_dst2[FI_RGBA_GREEN]=pixel_src[FI_RGBA_GREEN];
						pixel_dst2[FI_RGBA_BLUE]=pixel_src[FI_RGBA_BLUE];
						pixel_dst3[FI_RGBA_RED]=pixel_src[FI_RGBA_RED];
						pixel_dst3[FI_RGBA_GREEN]=pixel_src[FI_RGBA_GREEN];
						pixel_dst3[FI_RGBA_BLUE]=pixel_src[FI_RGBA_BLUE];
						pixel_dst1+=3;
						pixel_dst2+=3;
						pixel_dst3+=3;
						pixel_src+=3;					 
					}
				//指针换行设置
				bits_dst+=3*pitch;
				bits[h]+=pitch;
			}
	}

</pre><pre name="code" class="cpp">	//保存图像
	FreeImage_Save(FIF_PNG,dst,"church01_dst.jpg",0);
	//释放图像空间
	delete[] src;
	if(bitmap)  FreeImage_Unload(bitmap);
	getchar();
	
}

9合1效果图:
  


此处9合1操作代码很丑,效率不高,不能直视,使用起来也有限制性(图片位深需为24位),读入图片类型可通过FreeImage函数参数修改,未严格考虑内存配置和释放问题(还在努力学习中),以后有时间再做修改。


2.在C#中的简单使用

  相关的配置工作参见Rana博文 点击打开链接,在此只写出贴出9合1转换的核心代码:

                int copycount = Width * Height * 4;   
                IntPtr Bits = FreeImage_GetBits(Dib);
                byte[] line = new byte[Width * Height * 4];
                System.Runtime.InteropServices.Marshal.Copy(Bits, line, 0, copycount);

                byte[][] btList = { new byte[]{}, new byte[]{}, new byte[]{},
                                  new byte[]{},new byte[]{},new byte[]{},
                                  new byte[]{},new byte[]{},new byte[]{}};
                for (int i = 0; i < 9; i++)
                {
                    btList[i] = line;
                }

                byte[] camfusion = new byte[Height * Width * 4 * 9];

                for (int i = 0; i < height1; i++)
                {
                    //Buffer.BlockCopy(btList[0],i*width*4,camfusion,3*i*width*4,width*4);
                    Buffer.BlockCopy(btList[0], i * width1 * 4, camfusion, 3 * i * width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[1], i * width1 * 4, camfusion, 3 * i * width1 * 4 + width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[2], i * width1 * 4, camfusion, 3 * i * width1 * 4 + 2 * width1 * 4, width1 * 4);

                    Buffer.BlockCopy(btList[3], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 + 3 * i * width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[4], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 + 3 * i * width1 * 4 + width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[5], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 + 3 * i * width1 * 4 + 2 * width1 * 4, width1 * 4);

                    Buffer.BlockCopy(btList[6], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 * 2 + 3 * i * width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[7], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 * 2 + 3 * i * width1 * 4 + width1 * 4, width1 * 4);
                    Buffer.BlockCopy(btList[8], i * width1 * 4, camfusion, width1 * height1 * 3 * 4 * 2 + 3 * i * width1 * 4 + 2 * width1 * 4, width1 * 4);
                } 








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值