int imFileFormatECW::Open(const char* file_name) { NCSError eError = NCScbmOpenFileView((char*)file_name, &this->pNCSFileView, NULL); if (eError != NCS_SUCCESS) { if (eError == NCS_FILE_OPEN_ERROR || eError == NCS_FILE_NOT_FOUND || eError == NCS_FILE_INVALID) return IM_ERR_OPEN; else if (eError == NCS_FILE_OPEN_FAILED) return IM_ERR_FORMAT; else return IM_ERR_ACCESS; } NCSFileType fileType = NCScbmGetFileType(this->pNCSFileView); if (fileType == NCS_FILE_ECW) strcpy(this->compression, "ECW"); else if (fileType == NCS_FILE_JP2) strcpy(this->compression, "JPEG-2000"); else return IM_ERR_COMPRESS; this->image_count = 1; return IM_ERR_NONE; }
/* * Class: com_ermapper_ecw_JNCSFile * Method: ECWOpen * Signature: (Ljava/lang/String;Z)I */ JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWOpen (JNIEnv *pEnv, jobject JNCSFile, jstring Filename, jboolean bProgressive) { const char *pFilename = (*pEnv)->GetStringUTFChars(pEnv, Filename, (jboolean *)NULL); NCSFileView *pNCSFileView = NULL; NCSFileViewSetInfo *pNCSFileViewSetInfo = NULL; NCSError nError; if (bProgressive) { nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NCSJNIRefreshCallback); } else { nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NULL); } if (NCS_FAILED(nError)) { // Return the short filename, since people dont like diplaying the full ecwp url char *pProtocol, *pHost, *pECWFilename, *pShortFileName; int nProtocolLength, nHostLength, nFilenameLength; NCSecwNetBreakdownUrl((char *)pFilename, &pProtocol, &nProtocolLength, &pHost, &nHostLength, &pECWFilename, &nFilenameLength); if (pECWFilename && nFilenameLength > 0 && (strstr(pECWFilename, "/") || strstr(pECWFilename, "\\"))) { int len = strlen(pECWFilename), index = 0; for (index=len; index > 0; index--) { pShortFileName = &pECWFilename[index]; if (pECWFilename[index] == '\\' || pECWFilename[index] == '/') { pShortFileName ++; break; } } } else { pShortFileName = (char *)pFilename; } NCSFormatErrorText(NCS_FILE_OPEN_FAILED, pShortFileName, NCSGetLastErrorText(nError)); return NCS_FILE_OPEN_FAILED; } else { NCSJNIInfo *pJNIInfo = NULL; char *pMimeType = NULL; NCSFileViewFileInfo *pNCSFileInfo = NULL; nError = NCSCreateJNIInfoStruct(pEnv, JNCSFile, pNCSFileView, &pJNIInfo); NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo); // Set the properties in the actual Java object (*pEnv)->SetIntField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDWidth, (jint)pNCSFileInfo->nSizeX ); (*pEnv)->SetIntField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDHeight, (jint)pNCSFileInfo->nSizeY ); (*pEnv)->SetIntField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDNumberOfBands, (jint)pNCSFileInfo->nBands); (*pEnv)->SetDoubleField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDCompressionRate, (jdouble)pNCSFileInfo->nCompressionRate ); (*pEnv)->SetDoubleField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementX, (jdouble)pNCSFileInfo->fCellIncrementX ); (*pEnv)->SetDoubleField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementY, (jdouble)pNCSFileInfo->fCellIncrementY ); (*pEnv)->SetIntField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDCellSizeUnits, (jint)pNCSFileInfo->eCellSizeUnits); (*pEnv)->SetDoubleField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDOriginX, (jdouble)pNCSFileInfo->fOriginX ); (*pEnv)->SetDoubleField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDOriginY, (jdouble)pNCSFileInfo->fOriginY ); (*pEnv)->SetObjectField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDDatum, (*pEnv)->NewStringUTF(pEnv, pNCSFileInfo->szDatum)); (*pEnv)->SetObjectField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDProjection, (*pEnv)->NewStringUTF(pEnv, pNCSFileInfo->szProjection)); (*pEnv)->SetObjectField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDFilename, Filename); (*pEnv)->SetBooleanField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDIsOpen, JNI_TRUE); (*pEnv)->SetLongField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer, (jlong)pJNIInfo); (*pEnv)->SetIntField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDFileType, (jint)NCScbmGetFileType(pNCSFileView) ); pMimeType = NCScbmGetFileMimeType(pNCSFileView); if( pMimeType ) { (*pEnv)->SetObjectField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDFileMimeType, (*pEnv)->NewStringUTF(pEnv, pMimeType) ); NCSFree( pMimeType ); pMimeType = NULL; } else { (*pEnv)->SetObjectField(pEnv, JNCSFile, pGlobalJNCSFieldIDs->jIDFileMimeType, NULL ); } } (*pEnv)->ReleaseStringUTFChars(pEnv, Filename, (const char *)pFilename); NCScbmGetViewInfo(pNCSFileView, &pNCSFileViewSetInfo); pNCSFileViewSetInfo->pClientData = (void *)(*pEnv)->NewGlobalRef(pEnv, JNCSFile); return NCS_SUCCESS; }
int test_openview( char *szInputFilename, BOOLEAN bRandomReads, BOOLEAN bReportTime ) { NCSFileView *pNCSFileView; NCSFileViewFileInfo *pNCSFileInfo; NCSError eError = NCS_SUCCESS; UINT8 **p_p_output_line = NULL; UINT8 *p_output_buffer = NULL; UINT32 x_size, y_size, number_x, number_y; UINT32 start_x, start_y, end_x, end_y; UINT32 line; int regions; double total_pixels = 0.0; UINT32 band; UINT32 nBands; UINT32 *band_list = NULL; /* list of individual bands to read, may be subset of actual bands */ clock_t start_time, mark_time; printf("ECW READ EXAMPLE\n"); start_time = mark_time = clock(); /* ** Open the input NCSFileView */ eError = NCScbmOpenFileView(szInputFilename, &pNCSFileView, NULL); if (eError != NCS_SUCCESS) { printf("Could not open view for file:%s\n",szInputFilename); printf("Error = %s\n", NCSGetErrorText(eError)); return(1); } NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo); x_size = pNCSFileInfo->nSizeX; y_size = pNCSFileInfo->nSizeY; nBands = pNCSFileInfo->nBands; printf("Input file is [%ld x %ld by %d bands]\n", (long)x_size, (long)y_size, nBands); // Have to set up the band list. Compatible with ER Mapper's method. // In this example we always request all bands. band_list = (UINT32 *) malloc(sizeof(UINT32) * nBands); if( !band_list ) { printf("Error - unable to malloc band list\n"); NCScbmCloseFileView(pNCSFileView); return(1); } for( band = 0; band < nBands; band++ ) band_list[band] = band; // All of image start_x = 0; start_y = 0; end_x = x_size - 1; end_y = y_size - 1; number_x = x_size; number_y = y_size; // do many region reads as tests if( bRandomReads ) regions = 10; else regions = 100; // regions = 5000; while( regions -- ) { { // work out a random region to read number_x = number_y = 512; // define a random region and size start_x = (UINT32) (RAND() * x_size); end_x = (UINT32) (RAND() * (x_size - start_x)) + start_x; start_y = (UINT32) (RAND() * y_size); end_y = (UINT32) (RAND() * (y_size - start_y)) + start_y; // make a square view area if( (end_x - start_x) < MAX_WINDOW ) { end_x = start_x + MAX_WINDOW; if( end_x >= x_size ) { // [01] start_x = x_size - MAX_WINDOW; end_x = x_size-1; } } if( (end_y - start_y) < MAX_WINDOW ) { end_y = start_y + MAX_WINDOW; if( end_y >= y_size ) { // [01] start_y = y_size - MAX_WINDOW; end_y = y_size-1; } } if( (end_x - start_x) < (end_y - start_y) ) end_y = start_y + (end_x - start_x); else end_x = start_x + (end_y - start_y); number_x = number_y = MAX_WINDOW; } printf("Region %4d: [%d,%d] to [%d,%d] for [%d,%d]\n", regions, start_x, start_y, end_x, end_y, number_x, number_y); eError = NCScbmSetFileView(pNCSFileView, nBands, band_list, start_x, start_y, end_x, end_y, number_x, number_y); if( eError != NCS_SUCCESS) { printf("Error while setting file view to %d bands, TL[%d,%d] BR[%d,%d], Window size [%d,%d]\n", nBands, start_x, start_y, end_x, end_y, number_x, number_y); printf("Error = %s\n", NCSGetErrorText(eError)); NCScbmCloseFileView(pNCSFileView); free(band_list); return(1); } p_output_buffer = (UINT8 *) malloc( sizeof(UINT8) * number_x * nBands); p_p_output_line = (UINT8 **) malloc( sizeof(UINT8 *) * nBands); if( !p_p_output_line || !p_output_buffer) { printf("Malloc error for output buffers\n"); NCScbmCloseFileView(pNCSFileView); free(band_list); if( p_p_output_line ) free((char *) p_p_output_line); if( p_output_buffer ) free((char *) p_output_buffer); return(1); } for(band = 0; band < nBands; band++ ) p_p_output_line[band] = p_output_buffer + (band * number_x); /* ** Read each line of the compressed file */ for( line = 0; line < number_y; line++ ) { NCSEcwReadStatus eReadStatus; eReadStatus = NCScbmReadViewLineBIL( pNCSFileView, p_p_output_line); if (eReadStatus != NCSECW_READ_OK) { printf("Read line error at line %d\n",line); printf("Status code = %d\n", eReadStatus); NCScbmCloseFileView(pNCSFileView); free(band_list); free((char *) p_p_output_line); free((char *) p_output_buffer); return(1); } } free((char *) p_p_output_line); free((char *) p_output_buffer); if( bReportTime ) { double pixels, seconds, pixels_per_second, seconds_per_megapixel; // time for this run seconds = ((double) (clock() - mark_time)) / (double) CLOCKS_PER_SEC; if( seconds == 0 ) seconds = 0.01; pixels = (double) number_x * (double) number_y; pixels_per_second = pixels / seconds; seconds_per_megapixel = seconds / (pixels / 1000000.0); printf("Region time:%5.2lf sec.%5.2lf Mpixels/sec. %4.2lf secs/Mpixel (%6.2lf Mpixels)\n", seconds, pixels_per_second / 1000000, seconds_per_megapixel, (pixels/1000000)); // cumulative time mark_time = clock(); seconds = ((double) (mark_time - start_time)) / (double) CLOCKS_PER_SEC; if( seconds == 0 ) seconds = 0.001; total_pixels += pixels; pixels_per_second = total_pixels / seconds; seconds_per_megapixel = seconds / (total_pixels / 1000000.0); } } if( bReportTime ) { double seconds, pixels_per_second, seconds_per_megapixel; // cumulative time mark_time = clock(); seconds = ((double) (mark_time - start_time)) / (double) CLOCKS_PER_SEC; if( seconds == 0 ) seconds = 0.001; pixels_per_second = total_pixels / seconds; seconds_per_megapixel = seconds / (total_pixels / 1000000.0); printf("ALL time:%5.2lf sec.%5.2lf Mpixels/sec. %4.2lf secs/Mpixel (%6.2lf Mpixels)\n", seconds, pixels_per_second / 1000000, seconds_per_megapixel, total_pixels / 1000000); } // Make the second argument below TRUE if this is the last view of the file and you // want the file closed, otherwise it will be kept open in the cache. This can // sometimes be an problem when writing plugins. // NCScbmCloseFileViewEx(pNCSFileView, FALSE); free(band_list); return(0); }
int main(int argc, char **argv) { NCSEcwCompressClient *pClient; char *szInputFilename; char *szOutputFilename; NCSFileView *pNCSFileView; NCSFileViewFileInfo *pNCSFileInfo; UINT8 *pReadBuffer; UINT8 **ppInputBandBufferArray; NCSError eError; INT32 nBand; IEEE4 fTargetCompressionOverride = 0.0; // Check user has specified a file on command line if (argc < 3 || argc > 4) { ReportError( "Usage: %s InputFilename OutputFilename [Target Compression]", argv[0]); exit(1); } else { szInputFilename = argv[1]; szOutputFilename = argv[2]; if (argc == 4) { fTargetCompressionOverride = (float)atof(argv[3]); } } // Initialize the ECW JPEG 2000 SDK libraries NCSecwInit(); // Open the input file eError = NCScbmOpenFileView(szInputFilename, &pNCSFileView, NULL); if (eError != NCS_SUCCESS) { ReportError("Could not open view for file:%s, Error = %s", szInputFilename, NCSGetErrorText(eError)); exit(1); } // Get input file information eError = NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo); if (eError != NCS_SUCCESS) { ReportError("Could not get file information for file:%s, Error = %s", szInputFilename, NCSGetErrorText(eError)); exit(1); } // Set up the input band list and set the view { INT32 *pBandList; pBandList = (UINT32 *)malloc(sizeof(UINT32) * pNCSFileInfo->nBands); if (pBandList == NULL) { ReportError("Unable to malloc band list."); // NOTREACHED } for (nBand = 0; nBand < pNCSFileInfo->nBands; nBand++) { pBandList[nBand] = nBand; } // Set the view we want to read (the whole file) eError = NCScbmSetFileView( pNCSFileView, pNCSFileInfo->nBands, pBandList, 0, 0, pNCSFileInfo->nSizeX-1, pNCSFileInfo->nSizeY-1, pNCSFileInfo->nSizeX, pNCSFileInfo->nSizeY); if (eError != NCS_SUCCESS) { ReportError("Error setting file view: %s", NCSGetErrorText(eError)); exit(1); } } // The read function wants an array of band buffers to store // the data in. We allocate one block of memory and then point // set up the band points to point to different locations within // it. Alternatively, we could allocate a separate buffer for each // band. pReadBuffer = (UINT8 *)malloc(sizeof(UINT8) * pNCSFileInfo->nSizeX * pNCSFileInfo->nBands); if (pReadBuffer == NULL) { ReportError("Could not malloc read buffer"); exit(1); } ppInputBandBufferArray = (UINT8 **)malloc(sizeof(UINT8 *) * pNCSFileInfo->nBands); if (ppInputBandBufferArray == NULL) { ReportError("Could not malloc band buffer array"); exit(1); } for (nBand = 0; nBand < pNCSFileInfo->nBands; nBand++) { ppInputBandBufferArray[nBand] = pReadBuffer + (nBand * pNCSFileInfo->nSizeX * sizeof(UINT8)); } // Now set up the output side of the task. First we need to // allocate and set up a client structure for the compression if (pClient = NCSEcwCompressAllocClient()) { NCSError eError; ReadInfo CompressReadInfo; // output filename strcpy(pClient->szOutputFilename, szOutputFilename); // Set up the target compression if (fTargetCompressionOverride != 0.0) { // User has specified a compression pClient->fTargetCompression = fTargetCompressionOverride; } else { // Use the same compression it was originally compressed with pClient->fTargetCompression = (float)pNCSFileInfo->nCompressionRate; } // Set up the input dimensions pClient->nInOutSizeX = pNCSFileInfo->nSizeX; pClient->nInOutSizeY = pNCSFileInfo->nSizeY; pClient->nInputBands = pNCSFileInfo->nBands; // Currently there isn't a way to find out what type of file we have, // so we make a guess based on the # of input bands. if(pClient->nInputBands == 1) { pClient->eCompressFormat = COMPRESS_UINT8; } else if(pClient->nInputBands == 3) { pClient->eCompressFormat = COMPRESS_RGB; } else { pClient->eCompressFormat = COMPRESS_MULTI; } // Set up optional projection information pClient->fCellIncrementX = pNCSFileInfo->fCellIncrementX; pClient->fCellIncrementY = pNCSFileInfo->fCellIncrementY; pClient->fOriginX = pNCSFileInfo->fOriginX; pClient->fOriginY = pNCSFileInfo->fOriginY; pClient->eCellSizeUnits = pNCSFileInfo->eCellSizeUnits; strcpy(pClient->szDatum, pNCSFileInfo->szDatum); strcpy(pClient->szProjection, pNCSFileInfo->szProjection); // Specify the callbacks and client data ptr pClient->pReadCallback = ReadCallback; pClient->pStatusCallback = StatusCallback; pClient->pCancelCallback = CancelCallback; // Set up client data for read callback CompressReadInfo.pNCSFileView = pNCSFileView; CompressReadInfo.ppInputBandBufferArray = ppInputBandBufferArray; CompressReadInfo.nPercentComplete = 0; CompressReadInfo.pErrorBuffer[0] = '\0'; pClient->pClientData = (void *)&CompressReadInfo; // Open the compression eError = NCSEcwCompressOpen(pClient, FALSE); if (eError != NCS_SUCCESS) { ReportError("NCSCompressOpen failed: %s", NCSGetErrorText(eError)); } else { // Opened OK, now do the compression eError = NCSEcwCompress(pClient); if (eError == NCS_COULDNT_READ_INPUT_LINE) { ReportError("Compression error: %s (%s)", NCSGetErrorText(eError), CompressReadInfo.pErrorBuffer); } else if (eError != NCS_SUCCESS) { ReportError("Compression error: %s", NCSGetErrorText(eError)); } else { // Compressed ok // Close the compression eError = NCSEcwCompressClose(pClient); if (eError != NCS_SUCCESS) { ReportError("NCSEcwCompressClose failed: %s", NCSGetErrorText(eError)); } // output stats fprintf(stdout, "Target ratio: %.1lf\r\n" "Actual ratio: %.1lf\r\n" #ifdef WIN32 "Output size: %I64d bytes\r\n" #else "Output size: %lld bytes\r\n" #endif "Time taken: %.1lf seconds\r\n" "Data Rate: %.1lf MB/s\r\n", pClient->fTargetCompression, pClient->fActualCompression, pClient->nOutputSize, pClient->fCompressionSeconds, pClient->fCompressionMBSec); fflush(stdout); } } // Free client NCSEcwCompressFreeClient(pClient); } else { // Couldn't alloc client structure ReportError("NCSEcwCompressAllocClient() failed!"); } // Shutdown library resources NCSecwShutdown(); fprintf(stdout, "Hit Enter to exit"); fflush(stdout); fgetc(stdin); return(0); }