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
GDAL中通过GDALDriver类的Create函数实现图像的保存的更多相关文章
- C++中怎么获取类的成员函数的函数指针?
用一个实际代码来说明. class A { public: staticvoid staticmember(){cout<<"static"<<endl;} ...
- VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)
转自:http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html ①需要一个别人写好的类CWebPage,将其对于的两个文件Web ...
- CImageList类Create函数参数解析
前面提到了CImageList类的Create(...)函数,虽然MSDN上已经有所解释,但仍有网友问到参数的具体含义,下面就我的理解,对参数进行一次轻量级的剖析 函数原型(其他重载函数请参看msdn ...
- Cocos2d-x Lua中Sprite精灵类
精灵类是Sprite,它的类图如下图所示. Sprite类图 Sprite类直接继承了Node类,具有Node基本特征.此外,我们还可以看到Sprite类的子类有:PhysicsSprite和Skin ...
- 【转载】C++中的基类与派生类
转自:http://www.cnblogs.com/sujz/articles/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的 ...
- C++中的基类与派生类
派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的成员被派生类继承后,保持原来的状态 private 基类的public和protected的成员被派生类继 ...
- C++中的基类和派生类
转载自:http://www.cnblogs.com/sujz/archive/2011/05/12/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public ...
- Oracle数据库中调用Java类开发存储过程、函数的方法
Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日 浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...
- javascript中如何让类工厂和构造函数变成同一个函数
我们知道在js中可以用一个函数来定义对象的类,该函数称之为对象的构造函数,我们在需要create对象的时候直接调用这个构造函数即可: var Man = funciton(name){ this.na ...
随机推荐
- SpringMVC整合FreeMarker实例
FreeMarker作为模板引擎,是比较常用的. FreeMarker官方文档地址为:https://freemarker.apache.org/ 现在浏览器或者翻译工具这么多,对于英文方面,我想大多 ...
- mail发邮件报错 "send-mail: fatal: parameter inet_interfaces: no local interface found for ::1"
发送邮件: [root@itfswelog123]# echo '测试邮件标题' | mail -s "数据库挂啦.挂啦.起床啦 " xx@163.com 出现异常: [r ...
- PAT——1046. 划拳
划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...
- MongoDB简易
一 安装 1.下载 $ brew install mongodb 2.启动 $ mongod --config /usr/local/etc/mongod.conf 3.连接 $ mongo 二 ...
- 一篇很好的解释了.Net Core, .Net Framework, .Net standard library, Xamarin 之间关系的文章 (转载)
Introducing .NET Standard In my last post, I talked about how we want to make porting to .NET Core e ...
- canvas转img,blob相互转换
摘自:https://www.cnblogs.com/jyuf/p/7251591.html 函数都比较简单,直接看就ok了 /*----------------------------------- ...
- iframe空白
优酷网页上复制的通用代码用来播放视频,浏览器和谷歌模拟器正常,但是发布后手机上打开一片空白,一直以为是苹果手机不支持iframe,最后发现是由于iframe播放链接是http的,而我们网页是https ...
- sql中查询某月某年内的记录
假设表结构:用户名,日期,上班时间,下班时间.8月份记录:select * from 表名 where month(日期)=8 and 用户名 = '小张'8月份迟到早退次数:select sum(i ...
- (Les16 执行数据库恢复)-控制文件恢复
测试丢失所有控制文件恢复[20180517] rman target / show all; configure channel 1 device type disk format ' ...
- Redis集群入门
官方文章: https://redis.io/topics/cluster-tutorial#redis-cluster-configuration-parameters 本文永久地址: https: ...