GDAL中通过GDALDriver类的Create函数实现图像的保存

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值