GDAL中除了读取各种类型的图像外,也可以实现对各种图像的保存操作,具体实现测试代码如下:

int test_gadl_GDALDataset_write()
{
	const char* image_name = "E:/GitCode/GDAL_Test/test_images/1.jpg";

	{ // write bgr: CreateCopy
		GDALAllRegister();

		const char* pszFormat = "jpeg"; //"bmp";// "png";//"GTiff";
		GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
		if (!poDriver) {
			fprintf(stderr, "get driver by name failed\n");
			return -1;
		}

		char** papszMetadata = poDriver->GetMetadata();;
		if (CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATE, FALSE))
			fprintf(stderr, "Driver %s supports Create() method.\n", pszFormat);
		if (CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATECOPY, FALSE))
			fprintf(stderr, "Driver %s supports CreateCopy() method.\n", pszFormat);
		if (!CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATECOPY, FALSE)) {
			fprintf(stderr, "Driver %s don't supports CreateCopy() method.\n", pszFormat);
			return -1;
		}

		GDALDataset* poDataset = (GDALDataset*)GDALOpen(image_name, GA_ReadOnly);
		if (poDataset == nullptr) {
			std::cout << "input image error" << std::endl;
			return -1;
		}

		const char* pszDstFilename = "E:/GitCode/GDAL_Test/test_images/ret_1.jpg";
		// All drivers that support creating new files support the CreateCopy() method, but only a few support the Create() method
		GDALDataset* poDstDS = poDriver->CreateCopy(pszDstFilename, poDataset, FALSE, nullptr, nullptr, nullptr);
		if (!poDstDS) {
			std::cout << "create copy failed" << std::endl;
			return -1;
		}

		GDALClose((GDALDatasetH)poDataset);
		GDALClose((GDALDatasetH)poDstDS);
	}

	{ // write: bgr<->rgb
		GDALAllRegister();

		const char* pszFormat = "GTiff";
		GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
		if (!poDriver) {
			fprintf(stderr, "get driver by name failed\n");
			return -1;
		}

		char** papszMetadata = poDriver->GetMetadata();;
		if (!CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATE, FALSE)) {
			fprintf(stderr, "Driver %s don't supports Create() method.\n", pszFormat);
			return -1;
		}

		GDALDataset* poDataset = (GDALDataset*)GDALOpen(image_name, GA_ReadOnly);
		if (poDataset == nullptr) {
			std::cout << "input image error" << std::endl;
			return -1;
		}

		int width = poDataset->GetRasterXSize();
		int height = poDataset->GetRasterYSize();
		int band_count = poDataset->GetRasterCount();
		GDALDataType gdal_data_type = poDataset->GetRasterBand(1)->GetRasterDataType();

		int pBandMap[3] = { 1, 2, 3 };
		unsigned char* pData = new unsigned char[width * height * band_count]; //RRRR...BBBB...GGGG...
		poDataset->RasterIO(GF_Read, 0, 0, width, height, pData, width, height, gdal_data_type, band_count, pBandMap, 0, 0, 0);

		char** papszOptions = nullptr;
		papszOptions = CSLSetNameValue(papszOptions, "INTERLEAVE", "BAND");
		const char* pszDstFilename = "E:/GitCode/GDAL_Test/test_images/ret_2.jpg";
		GDALDataset* poDstDS = poDriver->Create(pszDstFilename, width, height, band_count, gdal_data_type, papszOptions);
		if (!poDstDS) {
			std::cout << "create copy failed" << std::endl;
			return -1;
		}

		int pBandMap_[3] = { 3, 2, 1 };
		poDstDS->RasterIO(GF_Write, 0, 0, width, height, pData, width, height, gdal_data_type, band_count, pBandMap_, 0, 0, 0);

		GDALClose((GDALDatasetH)poDataset);
		GDALClose((GDALDatasetH)poDstDS);
		delete[] pData;
	}

	{ // write: bgr->b, single channel
		GDALAllRegister();

		const char* pszFormat = "GTiff";
		GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
		if (!poDriver) {
			fprintf(stderr, "get driver by name failed\n");
			return -1;
		}

		char** papszMetadata = poDriver->GetMetadata();;
		if (!CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATE, FALSE)) {
			fprintf(stderr, "Driver %s don't supports Create() method.\n", pszFormat);
			return -1;
		}

		GDALDataset* poDataset = (GDALDataset*)GDALOpen(image_name, GA_ReadOnly);
		if (poDataset == nullptr) {
			std::cout << "input image error" << std::endl;
			return -1;
		}

		int width = poDataset->GetRasterXSize();
		int height = poDataset->GetRasterYSize();
		int band_count = poDataset->GetRasterCount();
		GDALDataType gdal_data_type = poDataset->GetRasterBand(1)->GetRasterDataType();

		unsigned char* pData = new unsigned char[width * height];
		GDALRasterBand* poBand = poDataset->GetRasterBand(2); // from 1 to GetRasterCount()
		poBand->RasterIO(GF_Read, 0, 0, width, height, pData, width, height, gdal_data_type, 0, 0, 0);

		char** papszOptions = nullptr;
		papszOptions = CSLSetNameValue(papszOptions, "INTERLEAVE", "BAND");
		const char* pszDstFilename = "E:/GitCode/GDAL_Test/test_images/ret_3.jpg";
		GDALDataset* poDstDS = poDriver->Create(pszDstFilename, width, height, 1, gdal_data_type, papszOptions);
		if (!poDstDS) {
			std::cout << "create copy failed" << std::endl;
			return -1;
		}

		int bandMap { 1 };
		poDstDS->RasterIO(GF_Write, 0, 0, width, height, pData, width, height, gdal_data_type, 1, &bandMap, 0, 0, 0);

		GDALClose((GDALDatasetH)poDataset);
		GDALClose((GDALDatasetH)poDstDS);
		delete[] pData;
	}

	{ // write: crop image
		GDALAllRegister();

		const char* pszFormat = "GTiff";
		GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
		if (!poDriver) {
			fprintf(stderr, "get driver by name failed\n");
			return -1;
		}

		char** papszMetadata = poDriver->GetMetadata();;
		if (!CSLFetchBoolean(papszMetadata, GDAL_DCAP_CREATE, FALSE)) {
			fprintf(stderr, "Driver %s don't supports Create() method.\n", pszFormat);
			return -1;
		}

		GDALDataset* poDataset = (GDALDataset*)GDALOpen(image_name, GA_ReadOnly);
		if (poDataset == nullptr) {
			std::cout << "input image error" << std::endl;
			return -1;
		}

		int width = poDataset->GetRasterXSize() / 2;
		int height = poDataset->GetRasterYSize() / 2;
		int band_count = poDataset->GetRasterCount();
		GDALDataType gdal_data_type = poDataset->GetRasterBand(1)->GetRasterDataType();

		unsigned char* pData = new unsigned char[width  * height * band_count];
		int pBandMap[3] = { 1, 2, 3 };
		poDataset->RasterIO(GF_Read, 0, 0, width, height, pData, width, height, gdal_data_type, band_count, pBandMap, 0, 0, 0);

		const char* pszDstFilename = "E:/GitCode/GDAL_Test/test_images/ret_4.jpg";
		GDALDataset* poDstDS = poDriver->Create(pszDstFilename, width, height, band_count, gdal_data_type, nullptr);
		if (!poDstDS) {
			std::cout << "create copy failed" << std::endl;
			return -1;
		}

		poDstDS->RasterIO(GF_Write, 0, 0, width, height, pData, width, height, gdal_data_type, band_count, pBandMap, 0, 0, 0);

		GDALClose((GDALDatasetH)poDataset);
		GDALClose((GDALDatasetH)poDstDS);
		delete[] pData;
	}

	return 0;
}

GitHub:https://github.com/fengbingchun/GDAL_Test

GDAL中通过GDALDriver类的Create函数实现图像的保存的更多相关文章

  1. C++中怎么获取类的成员函数的函数指针?

    用一个实际代码来说明. class A { public: staticvoid staticmember(){cout<<"static"<<endl;} ...

  2. VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)

    转自:http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html ①需要一个别人写好的类CWebPage,将其对于的两个文件Web ...

  3. CImageList类Create函数参数解析

    前面提到了CImageList类的Create(...)函数,虽然MSDN上已经有所解释,但仍有网友问到参数的具体含义,下面就我的理解,对参数进行一次轻量级的剖析 函数原型(其他重载函数请参看msdn ...

  4. Cocos2d-x Lua中Sprite精灵类

    精灵类是Sprite,它的类图如下图所示. Sprite类图 Sprite类直接继承了Node类,具有Node基本特征.此外,我们还可以看到Sprite类的子类有:PhysicsSprite和Skin ...

  5. 【转载】C++中的基类与派生类

    转自:http://www.cnblogs.com/sujz/articles/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的 ...

  6. C++中的基类与派生类

    派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的成员被派生类继承后,保持原来的状态 private 基类的public和protected的成员被派生类继 ...

  7. C++中的基类和派生类

    转载自:http://www.cnblogs.com/sujz/archive/2011/05/12/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public ...

  8. Oracle数据库中调用Java类开发存储过程、函数的方法

    Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日  浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...

  9. javascript中如何让类工厂和构造函数变成同一个函数

    我们知道在js中可以用一个函数来定义对象的类,该函数称之为对象的构造函数,我们在需要create对象的时候直接调用这个构造函数即可: var Man = funciton(name){ this.na ...

随机推荐

  1. ClassNotFoundException: INameEnvironment

    springboot兼容jsp启动是报错,ClassNotFoundException: INameEnvironment 降低springboot版本即可,原版本1.5.10,降为1.5.2可使用

  2. oracle exp 无法导出空表

    oracle exp 无法导出空表   select 'alter table '|| a.table_name ||' allocate extent;' from user_tables a wh ...

  3. windows C++ new/delete内存大小

    转载自:https://blog.csdn.net/will_hsbsch/article/details/21124055 windows 上,但使用C++语言new一块内存,用指针P指向这块内存, ...

  4. LCTF wp简单复现

    1.T4lk 1s ch34p,sh0w m3 the sh31l 代码如下: <?php $SECRET = `../read_secret`; $SANDBOX = "../dat ...

  5. CNN识别验证码1

    之前学习python的时候,想尝试用requests实现自动登陆,但是现在网站登陆都会有验证码保护,主要是为了防止暴力破解,任意用户注册.最近接触深度学习,cnn能够进行图像识别,能够进行验证码识别. ...

  6. mysql备份数据库脚本

    mysqldump.exe -uroot -proot mydb > D:\backup_script\bak-tmp\mydb.sql 备注:把mysql的bin下的mysqldump.exe ...

  7. 《metasploit渗透测试魔鬼训练营》学习笔记第七章--社会工程学

    五.社工工程学     5.1社会工程系框架          5.1.1信息搜集                 maltego是一个高度自动化的信息搜集工具,集成在BT5中,如果国内网络环境使用时 ...

  8. Microsoft visual c++ 14.0 is required问题

    错误信息: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Too ...

  9. I、Q信号是如何产生的,I、Q信号复用的作用

    接收机在中频部分实现模数变换和采样,采样后的信号和数字域的同频相乘,就可以得到基带的I.Q分量.在无线接口传输时,每一种使用特定的载波频率.码(扩频码和扰码)以及载波相对相位(I或Q)的信道都可以理解 ...

  10. 《AngularJS即学即用》读书笔记(一)

    最近在学习angularJS,就买了一本<AngularJS即学即用>作为自己的入门书籍,到目前为止看了两章的内容,感觉这本书还是不错的,东西讲的浅显易懂.之所以写这篇文章,一是督促自己能 ...