CompressWorker(string inputFile, string outputFile, int width, int height) : CNCSFile(), imageWidth(width), imageHeight(height) { // Fill out the band information structure NCSFileBandInfo bandInfo[IMAGE_BANDS]; bandInfo[0].nBits = 8; bandInfo[0].bSigned = FALSE; bandInfo[0].szDesc = "Red"; bandInfo[1].nBits = 8; bandInfo[1].bSigned = FALSE; bandInfo[1].szDesc = "Green"; bandInfo[2].nBits = 8; bandInfo[2].bSigned = FALSE; bandInfo[2].szDesc = "Blue"; // bandInfo[3].nBits = 8; // bandInfo[3].bSigned = FALSE; // bandInfo[3].szDesc = "Alpha"; // Fill out the file information structure NCSFileViewFileInfoEx fileInfo; fileInfo.nSizeX = width; fileInfo.nSizeY = height; fileInfo.nBands = IMAGE_BANDS; fileInfo.nCompressionRate = 20; fileInfo.eCellSizeUnits = ECW_CELL_UNITS_METERS; fileInfo.fCellIncrementX = 1000; fileInfo.fCellIncrementY = 1000; fileInfo.fOriginX = 0; fileInfo.fOriginY = 0; fileInfo.szDatum = "RAW"; fileInfo.szProjection = "RAW"; fileInfo.fCWRotationDegrees = 0; fileInfo.eColorSpace = NCSCS_sRGB; fileInfo.eCellType = NCSCT_UINT8; fileInfo.pBands = bandInfo; CNCSError error = SetFileInfo(fileInfo); string errorMessage = error.GetErrorMessage(); // Open file for output char* fileNameC = new char[1000]; strncpy(fileNameC, outputFile.c_str(), 1000); NCSError errorCode = Open(fileNameC, false, true); if(errorCode != NCS_SUCCESS) { string errorMessage = string(FormatErrorText(errorCode)); cout << "NCS: " << errorMessage << endl; } delete[] fileNameC; // Open the input file input = CreateFile(inputFile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); inputBuffer = new unsigned char[imageWidth * IMAGE_BANDS]; }
INT WINAPI WinMain( HINSTANCE , HINSTANCE, LPSTR , INT ) { int imageWidth = 43200; int imageHeight = 21600; string inputFile = "earth_diffuse_1km_shaded_RGB.raw"; string outputFile = "earth_diffuse_1km_shaded.ecw"; cout << "Input file: " << inputFile << " Output file: " << outputFile << endl; CNCSFile::SetKeySize(); CompressWorker worker(inputFile, outputFile, imageWidth, imageHeight); CNCSError error = worker.Write(); string errorMessage = error.GetErrorMessage(); }
GDALAsyncReader* ECWDataset::BeginAsyncReader( int nXOff, int nYOff, int nXSize, int nYSize, void *pBuf, int nBufXSize, int nBufYSize, GDALDataType eBufType, int nBandCount, int* panBandMap, int nPixelSpace, int nLineSpace, int nBandSpace, char **papszOptions) { int i; /* -------------------------------------------------------------------- */ /* Provide default packing if needed. */ /* -------------------------------------------------------------------- */ if( nPixelSpace == 0 ) nPixelSpace = GDALGetDataTypeSize(eBufType) / 8; if( nLineSpace == 0 ) nLineSpace = nPixelSpace * nBufXSize; if( nBandSpace == 0 ) nBandSpace = nLineSpace * nBufYSize; /* -------------------------------------------------------------------- */ /* Do a bit of validation. */ /* -------------------------------------------------------------------- */ if( nXSize < 1 || nYSize < 1 || nBufXSize < 1 || nBufYSize < 1 ) { CPLDebug( "GDAL", "BeginAsyncReader() skipped for odd window or buffer size.\n" " Window = (%d,%d)x%dx%d\n" " Buffer = %dx%d\n", nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize ); return NULL; } if( nXOff < 0 || nXOff > INT_MAX - nXSize || nXOff + nXSize > nRasterXSize || nYOff < 0 || nYOff > INT_MAX - nYSize || nYOff + nYSize > nRasterYSize ) { ReportError( CE_Failure, CPLE_IllegalArg, "Access window out of range in RasterIO(). Requested\n" "(%d,%d) of size %dx%d on raster of %dx%d.", nXOff, nYOff, nXSize, nYSize, nRasterXSize, nRasterYSize ); return NULL; } if( nBandCount <= 0 || nBandCount > nBands ) { ReportError( CE_Failure, CPLE_IllegalArg, "Invalid band count" ); return NULL; } if( panBandMap != NULL ) { for( i = 0; i < nBandCount; i++ ) { if( panBandMap[i] < 1 || panBandMap[i] > nBands ) { ReportError( CE_Failure, CPLE_IllegalArg, "panBandMap[%d] = %d, this band does not exist on dataset.", i, panBandMap[i] ); return NULL; } } } /* -------------------------------------------------------------------- */ /* Create the corresponding async reader. */ /* -------------------------------------------------------------------- */ ECWAsyncReader *poReader = new ECWAsyncReader(); poReader->poDS = this; poReader->nXOff = nXOff; poReader->nYOff = nYOff; poReader->nXSize = nXSize; poReader->nYSize = nYSize; poReader->pBuf = pBuf; poReader->nBufXSize = nBufXSize; poReader->nBufYSize = nBufYSize; poReader->eBufType = eBufType; poReader->nBandCount = nBandCount; poReader->panBandMap = (int *) CPLCalloc(sizeof(int),nBandCount); if( panBandMap != NULL ) { memcpy( poReader->panBandMap, panBandMap, sizeof(int) * nBandCount ); } else { for( i = 0; i < nBandCount; i++ ) poReader->panBandMap[i] = i + 1; } poReader->nPixelSpace = nPixelSpace; poReader->nLineSpace = nLineSpace; poReader->nBandSpace = nBandSpace; /* -------------------------------------------------------------------- */ /* Create a new view for this request. */ /* -------------------------------------------------------------------- */ poReader->poFileView = OpenFileView( GetDescription(), true, poReader->bUsingCustomStream ); if( poReader->poFileView == NULL ) { delete poReader; return NULL; } poReader->poFileView->SetClientData( poReader ); poReader->poFileView->SetRefreshCallback( ECWAsyncReader::RefreshCB ); /* -------------------------------------------------------------------- */ /* Issue a corresponding SetView command. */ /* -------------------------------------------------------------------- */ std::vector<UINT32> anBandIndices; NCSError eNCSErr; CNCSError oErr; for( i = 0; i < nBandCount; i++ ) anBandIndices.push_back( panBandMap[i] - 1 ); oErr = poReader->poFileView->SetView( nBandCount, &(anBandIndices[0]), nXOff, nYOff, nXOff + nXSize - 1, nYOff + nYSize - 1, nBufXSize, nBufYSize ); eNCSErr = oErr.GetErrorNumber(); if( eNCSErr != NCS_SUCCESS ) { delete poReader; CPLError( CE_Failure, CPLE_AppDefined, "%s", NCSGetErrorText(eNCSErr) ); return NULL; } return poReader; }
GDALAsyncReader* ECWDataset::BeginAsyncReader( int nXOff, int nYOff, int nXSize, int nYSize, void *pBuf, int nBufXSize, int nBufYSize, GDALDataType eBufType, int nBandCount, int* panBandMap, int nPixelSpace, int nLineSpace, int nBandSpace, char **papszOptions) { /* -------------------------------------------------------------------- */ /* Provide default packing if needed. */ /* -------------------------------------------------------------------- */ if( nPixelSpace == 0 ) nPixelSpace = GDALGetDataTypeSize(eBufType) / 8; if( nLineSpace == 0 ) nLineSpace = nPixelSpace * nBufXSize; if( nBandSpace == 0 ) nBandSpace = nLineSpace * nBufYSize; /* -------------------------------------------------------------------- */ /* We should do a bit of validation first - perhaps add later. */ /* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */ /* Create the corresponding async reader. */ /* -------------------------------------------------------------------- */ ECWAsyncReader *poReader = new ECWAsyncReader(); poReader->poDS = this; poReader->nXOff = nXOff; poReader->nYOff = nYOff; poReader->nXSize = nXSize; poReader->nYSize = nYSize; poReader->pBuf = pBuf; poReader->nBufXSize = nBufXSize; poReader->nBufYSize = nBufYSize; poReader->eBufType = eBufType; poReader->nBandCount = nBandCount; poReader->panBandMap = (int *) CPLCalloc(sizeof(int),nBandCount); memcpy( poReader->panBandMap, panBandMap, sizeof(int) * nBandCount ); poReader->nPixelSpace = nPixelSpace; poReader->nLineSpace = nLineSpace; poReader->nBandSpace = nBandSpace; /* -------------------------------------------------------------------- */ /* Create a new view for this request. */ /* -------------------------------------------------------------------- */ poReader->poFileView = OpenFileView( GetDescription(), true, poReader->bUsingCustomStream ); if( poReader->poFileView == NULL ) { delete poReader; return NULL; } poReader->poFileView->SetClientData( poReader ); poReader->poFileView->SetRefreshCallback( ECWAsyncReader::RefreshCB ); /* -------------------------------------------------------------------- */ /* Issue a corresponding SetView command. */ /* -------------------------------------------------------------------- */ std::vector<UINT32> anBandIndices; int i; NCSError eNCSErr; CNCSError oErr; for( i = 0; i < nBandCount; i++ ) anBandIndices.push_back( panBandMap[i] - 1 ); oErr = poReader->poFileView->SetView( nBandCount, &(anBandIndices[0]), nXOff, nYOff, nXOff + nXSize - 1, nYOff + nYSize - 1, nBufXSize, nBufYSize ); eNCSErr = oErr.GetErrorNumber(); if( eNCSErr != NCS_SUCCESS ) { delete poReader; CPLError( CE_Failure, CPLE_AppDefined, "%s", NCSGetErrorText(eNCSErr) ); return NULL; } return poReader; }
CNCSError ECWDEMCompressor::Compress(ImageFormatECWDEM *NewIFECW, RasterBounds *RBounds, char *pDstFile, UINT32 ImgWidth, UINT32 ImgHeight, char Depth, UINT16 nRate) { //NCSFileViewFileInfoEx has basic info about the file - dimensions, bands, //georeferencing information etc. NCSFileViewFileInfoEx *pDstInfo = (NCSFileViewFileInfoEx *)NCSMalloc(sizeof(NCSFileViewFileInfoEx), true); NCSFileViewFileInfoEx DstInfo = *pDstInfo; CNCSError Error; IFECW = NewIFECW; DstInfo.nSizeX = ImgWidth; DstInfo.nSizeY = ImgHeight; switch (Depth) { case 8: DstInfo.eCellType = NCSCT_UINT8; break; case 16: DstInfo.eCellType = NCSCT_UINT16; break; //case 28: DstInfo.eCellType = NCSCT_IEEE4; break; case 28: DstInfo.eCellType = NCSCT_UINT32; break; } // switch DstInfo.nBands = 1; DstInfo.nCompressionRate = nRate; DstInfo.eColorSpace = NCSCS_GREYSCALE; #ifdef WCS_BUILD_VNS if (RBounds && RBounds->FetchCoordSys()) { IFECW->CreateECWGeoRefNewAPI(RBounds->FetchCoordSys(), &DstInfo, RBounds); } // if #endif // WCS_BUILD_VNS //DstInfo.szProjection = "RAW"; //DstInfo.szDatum = "RAW"; //DstInfo.eCellSizeUnits = ECW_CELL_UNITS_METERS; //DstInfo.fCellIncrementX = 1.0; //DstInfo.fCellIncrementY = 1.0; //DstInfo.fOriginX = 0.0; //DstInfo.fOriginY = 0.0; DstInfo.pBands = (NCSFileBandInfo *)(new NCSFileBandInfo[DstInfo.nBands]); DstInfo.pBands[0].nBits = Depth; DstInfo.pBands[0].bSigned = false; DstInfo.pBands[0].szDesc = "Elevation"; //Call SetFileInfo to establish the file information we are going to //use for compression. The parameters used are those from the NCSFileViewFileInfoEx //struct we have populated using metadata derived from our input raster. Error = SetFileInfo(DstInfo); Error = Open(pDstFile, false, true); if (Error == NCS_SUCCESS) { #ifdef WCS_BUILD_JP2BOX_SUPPORT JP2UUID3DNBox RangeEquivs(NewIFECW->HighElev, NewIFECW->LowElev, (unsigned long int)NewIFECW->MaxValue, 0); AddBox(&RangeEquivs); #endif // WCS_BUILD_JP2BOX_SUPPORT Error = Write(); if (Error == NCS_SUCCESS) fprintf(stdout,"Finished compression\n"); else if (Error == NCS_USER_CANCELLED_COMPRESSION) fprintf(stdout,"Compression cancelled\n"); else fprintf(stdout,"Error during compression: %s\n",Error.GetErrorMessage()); Error = Close(true); } // if return(Error); } // ECWDEMCompressor::Compress