typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
char* stretch_percent_16to8(const char *inFilename)//将uint16转化为uint8
{
string s(inFilename);
size_t index = s.find_first_of(".", 0);
string newstr = s.substr(0, index);
string ss = newstr + "ceshi.tif";//生成缓存,我这里是将原来数据和现在生成数据分开了
GDALAllRegister();//注册
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
int src_height = 0;
int src_width = 0;
GDALDataset *poIn = (GDALDataset *)GDALOpen(inFilename, GA_ReadOnly);
GDALDataType eDataType = poIn->GetRasterBand(1)->GetRasterDataType();
if (eDataType == 1){//如果是常规电脑能打开的图
char* filename = new char[strlen(inFilename) + 1];
strcpy_s(filename, strlen(inFilename) + 1, inFilename);
return filename;//如果是Byte就直接显示
}
else if (eDataType == 6){//如果是遥感影像
char*dstFilename;
const int len = ss.length();
dstFilename = new char[len + 1];
strcpy_s(dstFilename, len + 1, ss.c_str());
fstream _file;
_file.open(dstFilename, ios::in);
if (!_file){
src_width = poIn->GetRasterXSize();
src_height = poIn->GetRasterYSize();
int InBands = poIn->GetRasterCount();
double adfInGeoTransform[6] = { 0 };
const char *pszWKT = NULL;
poIn->GetGeoTransform(adfInGeoTransform);
pszWKT = poIn->GetProjectionRef();
GDALDriver *poDriver = (GDALDriver *)GDALGetDriverByName("GTiff");
GDALDataset *poOutputDS = poDriver->Create(dstFilename, src_width, src_height, InBands, GDT_Byte, NULL);
poOutputDS->SetGeoTransform(adfInGeoTransform);
poOutputDS->SetProjection(pszWKT);
for (int iBand = 0; iBand < InBands; iBand++)
{
float32_t *srcData = (float32_t *)malloc(sizeof(float32_t)*src_width * src_height * 1);
memset(srcData, 0, sizeof(float32_t)* 1 * src_width * src_height);
float src_max = 1.0*(*srcData), src_min = 1.0*(*srcData);
poIn->GetRasterBand(iBand + 1)->RasterIO(GF_Read, 0, 0, src_width, src_height, srcData + 0 * src_width * src_height, src_width, src_height, GDT_Float32, 0, 0);
for (int src_row = 0; src_row < src_height; src_row++)
{
for (int src_col = 0; src_col < src_width; src_col++)
{
float32_t src_temVal = *(srcData + src_row * src_width + src_col);
if (src_temVal > src_max)
src_max = src_temVal;
if (src_temVal < src_min)
src_min = src_temVal;
}
}
for (int src_row = 0; src_row < src_height; src_row++)
{
uint8_t *dstData = (uint8_t*)malloc(sizeof(uint8_t)*src_width);
memset(dstData, 0, sizeof(uint8_t)*src_width);
for (int src_col = 0; src_col < src_width; src_col++)
{
float32_t src_temVal = *(srcData + src_row * src_width + src_col);
*(dstData + src_col) = round((255 - 0)*(src_temVal - src_min) / (src_max - src_min) + 0);
}
poOutputDS->GetRasterBand(iBand + 1)->RasterIO(GF_Write, 0, src_row, src_width, 1, dstData, src_width, 1, GDT_Byte, 0, 0);
free(dstData);
}
free(srcData);
}
GDALClose(poIn);
GDALClose(poOutputDS);
}
return dstFilename;
}
else{//如果不是,目前只能做16转化为8
char*dstFilename;
const int len = ss.length();
dstFilename = new char[len + 1];
strcpy_s(dstFilename, len + 1, ss.c_str());
fstream _file;
_file.open(dstFilename, ios::in);
if (!_file)
{
src_width = poIn->GetRasterXSize();
src_height = poIn->GetRasterYSize();
int InBands = poIn->GetRasterCount();
double adfInGeoTransform[6] = { 0 };
const char *pszWKT = NULL;
poIn->GetGeoTransform(adfInGeoTransform);
pszWKT = poIn->GetProjectionRef();
GDALDriver *poDriver = (GDALDriver *)GDALGetDriverByName("GTiff");
GDALDataset *poOutputDS = poDriver->Create(dstFilename, src_width, src_height, InBands, GDT_Byte, NULL);
poOutputDS->SetGeoTransform(adfInGeoTransform);
poOutputDS->SetProjection(pszWKT);
for (int iBand = 0; iBand < InBands; iBand++)
{
uint16_t *srcData = (uint16_t *)malloc(sizeof(uint16_t)*src_width * src_height * 1);
memset(srcData, 0, sizeof(uint16_t)* 1 * src_width * src_height);
int src_max = 0, src_min = 65500;
poIn->GetRasterBand(iBand + 1)->RasterIO(GF_Read, 0, 0, src_width, src_height, srcData + 0 * src_width * src_height, src_width, src_height, GDT_UInt16, 0, 0);
for (int src_row = 0; src_row < src_height; src_row++)
{
for (int src_col = 0; src_col < src_width; src_col++)
{
uint16_t src_temVal = *(srcData + src_row * src_width + src_col);
if (src_temVal > src_max)
src_max = src_temVal;
if (src_temVal < src_min)
src_min = src_temVal;
}
}
double *numb_pix = (double *)malloc(sizeof(double)*(src_max + 1));
memset(numb_pix, 0, sizeof(double)* (src_max + 1));
for (int src_row = 0; src_row < src_height; src_row++)
{
for (int src_col = 0; src_col < src_width; src_col++)
{
uint16_t src_temVal = *(srcData + src_row * src_width + src_col);
*(numb_pix + src_temVal) += 1;
}
}
double *frequency_val = (double *)malloc(sizeof(double)*(src_max + 1));
memset(frequency_val, 0.0, sizeof(double)*(src_max + 1));
for (int val_i = 0; val_i <= src_max; val_i++)
{
*(frequency_val + val_i) = *(numb_pix + val_i) / double(src_width * src_height);
}
double *accumlt_frequency_val = (double*)malloc(sizeof(double)*(src_max + 1));
memset(accumlt_frequency_val, 0.0, sizeof(double)*(src_max + 1));
for (int val_i = 0; val_i <= src_max; val_i++)
{
for (int val_j = 0; val_j < val_i; val_j++)
{
*(accumlt_frequency_val + val_i) += *(frequency_val + val_j);
}
}
int minVal = 0, maxVal = 0;//统计像素两端截断值
for (int val_i = 1; val_i < src_max; val_i++)
{
double acc_fre_temVal0 = *(frequency_val + 0);
double acc_fre_temVal = *(accumlt_frequency_val + val_i);
if ((acc_fre_temVal - acc_fre_temVal0) > 0.0015)
{
minVal = val_i;
break;
}
}
for (int val_i = src_max - 1; val_i > 0; val_i--)
{
double acc_fre_temVal0 = *(accumlt_frequency_val + src_max);
double acc_fre_temVal = *(accumlt_frequency_val + val_i);
if (acc_fre_temVal < (acc_fre_temVal0 - 0.00012))
{
maxVal = val_i;
break;
}
}
for (int src_row = 0; src_row < src_height; src_row++)
{
uint8_t *dstData = (uint8_t*)malloc(sizeof(uint8_t)*src_width);
memset(dstData, 0, sizeof(uint8_t)*src_width);
for (int src_col = 0; src_col < src_width; src_col++)
{
uint16_t src_temVal = *(srcData + src_row * src_width + src_col);
double stre_temVal = (src_temVal - minVal) / double(maxVal - minVal);
if (src_temVal < minVal)
{
*(dstData + src_col) = (src_temVal)*(20.0 / double(minVal));
}
else if (src_temVal > maxVal)
{
stre_temVal = uint8_t((src_temVal - src_min) / double(src_max - src_min));
*(dstData + src_col) = 254;
}
else
*(dstData + src_col) = pow(stre_temVal, 0.7) * 250;
}
poOutputDS->GetRasterBand(iBand + 1)->RasterIO(GF_Write, 0, src_row, src_width, 1, dstData, src_width, 1, GDT_Byte, 0, 0);
free(dstData);
}
free(numb_pix);
free(frequency_val);
free(accumlt_frequency_val);
free(srcData);
}
GDALClose(poIn);
GDALClose(poOutputDS);
}
return dstFilename;//返回缓存的名字
}
}