GDAL创建JPEG图像

void OversceneBuilder::CreateJpgDS(GDALDatasetH pCreateCopyDS, LPCTSTR strOutputFileName) { GDALDriverH hDriver; GDALDatasetH hDstDS; /* -------------------------------------------------------------------- */ /* Find the output driver. */ /* -------------------------------------------------------------------- */ hDriver = GDALGetDriverByName( m_strFormat ); if( hDriver == NULL ) { return ; } /* -------------------------------------------------------------------- */ /* Create the output file. */ /* -------------------------------------------------------------------- */ char **papszCreateOptions = NULL; CString strCreateOptionJPG; strCreateOptionJPG.Format("QUALITY=%d", m_nJpgQuality); char *JPGCreatOptions[2]; JPGCreatOptions[0] = strCreateOptionJPG.GetBuffer(1); JPGCreatOptions[1] = NULL; //strOutputFileName.Format("%s_%06d_%06d", (LPCTSTR)m_strFileName, yDiv, xDiv); if (m_strFormat == "JPEG") { papszCreateOptions = JPGCreatOptions; } hDstDS = GDALCreateCopy( hDriver, strOutputFileName, pCreateCopyDS, FALSE, papszCreateOptions, NULL, NULL ); if( hDstDS == NULL ) return ; GDALClose(hDstDS); return ; } BOOL CZOOMDlg::ConvertToJpeg(CString strInImg,CString strOutImg,int x ,int y) { GDALAllRegister(); GDALDatasetH hSrcDS = GDALOpen(strInImg, GA_ReadOnly); GDALRasterBandH hSrcDSband = GDALGetRasterBand(hSrcDS,1); GDALDataType eWorkingType = GDALGetRasterDataType(hSrcDSband); int nSrcWidth = GDALGetRasterXSize(hSrcDS); int nSrcHeight = GDALGetRasterYSize(hSrcDS); GDALDatasetH hVrtDS; GDALDriverH hDriver = GDALGetDriverByName("MEM"); hVrtDS = GDALCreate( hDriver, "", nSrcWidth, nSrcHeight, GDALGetRasterCount(hSrcDS), eWorkingType, NULL ); //分配抽取后容纳数据的缓冲区 int nWordSize = GDALGetDataTypeSize(eWorkingType)/8; int nBandSize = nWordSize * nSrcWidth * nSrcHeight; void *pDstBuffer = new byte[ nBandSize * GDALGetRasterCount(hVrtDS) ]; memset(pDstBuffer, 0, nBandSize * GDALGetRasterCount(hVrtDS)); int nBandCount = GDALGetRasterCount(hVrtDS); int *panDstBands = new int[nBandCount]; for (int i = 0; i < nBandCount; i++) { panDstBands[i] = i+1; } //打开数据集 CPLErr eErr = CE_None; if (hSrcDS != NULL) { eErr = GDALDatasetRasterIO( hSrcDS, GF_Read, 0, 0, nSrcWidth, nSrcHeight, pDstBuffer, nSrcWidth, nSrcHeight, eWorkingType, nBandCount, panDstBands, 0, 0, 0 ); } //写入目标数据集 eErr = GDALDatasetRasterIO( hVrtDS, GF_Write, 0, 0, nSrcWidth, nSrcHeight, pDstBuffer, nSrcWidth, nSrcHeight, eWorkingType, nBandCount, panDstBands, 0, 0, 0 ); GDALClose(hSrcDS); // GDALClose(hVrtDS); delete []panDstBands; delete []pDstBuffer; GDALDatasetH hDstDS; /* -------------------------------------------------------------------- */ /* Find the output driver. */ /* -------------------------------------------------------------------- */ GDALDriverH hDriverjpg; hDriverjpg = GDALGetDriverByName("JPEG"); if( hDriverjpg == NULL ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Create the output file. */ /* -------------------------------------------------------------------- */ char **papszCreateOptions = NULL; int m_nJpgQuality = 80; CString strCreateOptionJPG; strCreateOptionJPG.Format("QUALITY=%d", m_nJpgQuality); char *JPGCreatOptions[2]; JPGCreatOptions[0] = strCreateOptionJPG.GetBuffer(1); JPGCreatOptions[1] = NULL; CString m_strFormat = "JPEG"; //strOutputFileName.Format("%s_%06d_%06d", (LPCTSTR)m_strFileName, yDiv, xDiv); if (m_strFormat == "JPEG") { papszCreateOptions = JPGCreatOptions; } hDstDS = GDALCreateCopy( hDriverjpg, strOutImg, hVrtDS, FALSE, papszCreateOptions, NULL, NULL ); if( hDstDS == NULL ) return FALSE; GDALClose(hDstDS); return TRUE; }

你可能感兴趣的:(GDAL相关应用)