NRT_BOOL writeFile(nrt_Uint32 x0, nrt_Uint32 y0, nrt_Uint32 x1, nrt_Uint32 y1, nrt_Uint8 *buf, nrt_Uint64 bufSize, const char* prefix, nrt_Error *error) { NRT_BOOL rc = NRT_SUCCESS; char filename[NRT_MAX_PATH]; nrt_IOHandle outHandle; NRT_SNPRINTF(filename, NRT_MAX_PATH, "%s-raw-%d_%d__%d_%d.out", prefix, x0, y0, x1, y1); outHandle = nrt_IOHandle_create(filename, NRT_ACCESS_WRITEONLY, NRT_CREATE, error); if (NRT_INVALID_HANDLE(outHandle)) { goto CATCH_ERROR; } if (!nrt_IOHandle_write(outHandle, (const char *) buf, bufSize, error)) { goto CATCH_ERROR; } printf("Wrote file: %s\n", filename); goto CLEANUP; CATCH_ERROR: { rc = NRT_FAILURE; } CLEANUP: { if (!NRT_INVALID_HANDLE(outHandle)) nrt_IOHandle_close(outHandle); } return rc; }
NRTPROT(void) nrt_Utils_geographicLonToCharArray(int degrees, int minutes, double seconds, char *buffer8) { char dir = 'E'; int wtf = 0; if (degrees < 0) { dir = 'W'; degrees *= -1; } /* Round seconds. */ seconds += 0.5; /* Ensure seconds and minutes are still within valid range. */ if (seconds >= 60.0) { seconds -= 60.0; if (++minutes >= 60) { minutes -= 60; ++degrees; } } NRT_SNPRINTF(buffer8, 9, "%03d%02d%02d%c", degrees, minutes, (int) seconds, dir); }
NRTPROT(void *) nrt_Debug_malloc(const char *file, int line, size_t sz) { /* The pointer to allocate */ void *p; FILE *f; char name[512]; #ifndef WIN32 NRT_SNPRINTF(name, 512, "%s.%d", NRT_MEM_LOG, getpid()); #else /* This can easily be modified to use GetCurrentProcessId() */ strcpy(name, NRT_MEM_LOG); #endif f = fopen(name, "a"); assert(f); fprintf(f, "REQUEST: malloc\t[%d]\n", sz); p = malloc(sz); fprintf(f, "\tMALLOC\t%p\t%d\t%s\t%d\n", p, sz, file, line); fclose(f); return p; }
NRT_BOOL writeFile(j2k_Container *container, nrt_Uint32 tileX, nrt_Uint32 tileY, nrt_Uint8 *buf, nrt_Uint32 bufSize, nrt_Error *error) { NRT_BOOL rc = NRT_SUCCESS; char filename[NRT_MAX_PATH]; nrt_IOHandle outHandle; NRT_SNPRINTF(filename, NRT_MAX_PATH, "raw-%d_%d__%dx%d.out", tileX, tileY, j2k_Container_getTileWidth(container, error), j2k_Container_getTileHeight(container, error)); outHandle = nrt_IOHandle_create(filename, NRT_ACCESS_WRITEONLY, NRT_CREATE, error); if (NRT_INVALID_HANDLE(outHandle)) { goto CATCH_ERROR; } if (!nrt_IOHandle_write(outHandle, (const char *) buf, bufSize, error)) { goto CATCH_ERROR; } printf("Wrote file: %s\n", filename); goto CLEANUP; CATCH_ERROR: { rc = NRT_FAILURE; } CLEANUP: { if (!NRT_INVALID_HANDLE(outHandle)) nrt_IOHandle_close(outHandle); } return rc; }
NRTPROT(void) nrt_Debug_free(const char *file, int line, void *ptr) { FILE *f; char name[512]; #ifndef WIN32 NRT_SNPRINTF(name, 512, "%s.%d", NRT_MEM_LOG, getpid()); #else /* This can easily be modified to use GetCurrentProcessId() */ strcpy(name, NRT_MEM_LOG); #endif f = fopen(name, "a"); assert(f); fprintf(f, "REQUEST: free\t[%p]\n", ptr); free(ptr); fprintf(f, "\tFREE\t%s\t%d\n", file, line); fclose(f); }
int main(int argc, char **argv) { int rc = 0; int argIt = 0, i = 0, num = 0, dump = 0; char *fname = NULL; nrt_Error error; nrt_IOInterface *io = NULL; nitf_Reader *reader = NULL; nitf_Record *record = NULL; nrt_Uint8 *buf = NULL; for (argIt = 1; argIt < argc; ++argIt) { if (strcmp(argv[argIt], "--dump") == 0) dump = 1; else if (!fname) { fname = argv[argIt]; } } if (!fname) { nrt_Error_init(&error, "Usage: [--x0 --y0 --x1 --y1] <j2k-file>", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (nitf_Reader_getNITFVersion(fname) == NITF_VER_UNKNOWN) { nrt_Error_init(&error, "This file does not appear to be a valid NITF", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (!(io = nrt_IOHandleAdapter_open(fname, NRT_ACCESS_READONLY, NRT_OPEN_EXISTING, &error))) goto CATCH_ERROR; if (!(reader = nitf_Reader_construct(&error))) goto CATCH_ERROR; if (!(record = nitf_Reader_readIO(reader, io, &error))) goto CATCH_ERROR; num = nitf_Record_getNumImages(record, &error); if (num > 0) { nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); for (i = 0; nitf_ListIterator_notEqualTo(&iter, &end); ++i) { nitf_ImageSegment *segment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); nitf_ImageSubheader *subheader = segment->subheader; if (strcmp(subheader->imageCompression->raw, "C8") == 0) { j2k_Reader *j2kReader = NULL; j2k_Container *container = NULL; nrt_Uint32 cmpIt, nComponents; printf("Image %d contains J2K compressed data\n", (i + 1)); printf("Offset: %d\n", segment->imageOffset); if (!nrt_IOInterface_seek(io, segment->imageOffset, NRT_SEEK_SET, &error)) goto CATCH_ERROR; if (!(j2kReader = j2k_Reader_openIO(io, &error))) goto CATCH_ERROR; if (!(container = j2k_Reader_getContainer(j2kReader, &error))) goto CATCH_ERROR; printf("grid width:\t%d\n", j2k_Container_getGridWidth(container, &error)); printf("grid height:\t%d\n", j2k_Container_getGridHeight(container, &error)); printf("tile width:\t%d\n", j2k_Container_getTileWidth(container, &error)); printf("tile height:\t%d\n", j2k_Container_getTileHeight(container, &error)); printf("x tiles:\t%d\n", j2k_Container_getTilesX(container, &error)); printf("y tiles:\t%d\n", j2k_Container_getTilesY(container, &error)); printf("image type:\t%d\n", j2k_Container_getImageType(container, &error)); nComponents = j2k_Container_getNumComponents(container, &error); printf("components:\t%d\n", nComponents); for(cmpIt = 0; cmpIt < nComponents; ++cmpIt) { j2k_Component* c = j2k_Container_getComponent(container, cmpIt, &error); printf("===component %d===\n", (cmpIt + 1)); printf("width:\t\t%d\n", j2k_Component_getWidth(c, &error)); printf("height:\t\t%d\n", j2k_Component_getHeight(c, &error)); printf("precision:\t%d\n", j2k_Component_getPrecision(c, &error)); printf("x0:\t\t%d\n", j2k_Component_getOffsetX(c, &error)); printf("y0:\t\t%d\n", j2k_Component_getOffsetY(c, &error)); printf("x separation:\t%d\n", j2k_Component_getSeparationX(c, &error)); printf("y separation:\t%d\n", j2k_Component_getSeparationY(c, &error)); printf("signed:\t\t%d\n", j2k_Component_isSigned(c, &error)); } if (dump) { char namePrefix[NRT_MAX_PATH]; nrt_Uint32 width, height; nrt_Uint64 bufSize; if (buf) { NRT_FREE(buf); buf = NULL; } width = j2k_Container_getWidth(container, &error); height = j2k_Container_getWidth(container, &error); if ((bufSize = j2k_Reader_readRegion(j2kReader, 0, 0, width, height, &buf, &error)) == 0) { goto CATCH_ERROR; } NRT_SNPRINTF(namePrefix, NRT_MAX_PATH, "image-%d", (i + 1)); if (!writeFile(0, 0, width, height, buf, bufSize, namePrefix, &error)) { goto CATCH_ERROR; } } } nitf_ListIterator_increment(&iter); } } goto CLEANUP; CATCH_ERROR: { nrt_Error_print(&error, stdout, "Exiting..."); rc = 1; } CLEANUP: { if (reader) nitf_Reader_destruct(&reader); if (record) nitf_Record_destruct(&record); if (io) nrt_IOInterface_destruct(&io); } return rc; }
NRTPROT(void) nrt_Utils_decimalLonToCharArray(double decimal, char *buffer8) { NRT_SNPRINTF(buffer8, 9, "%+08.3f", decimal); }
NRTPROT(void) nrt_Utils_decimalLatToCharArray(double decimal, char *buffer7) { NRT_SNPRINTF(buffer7, 8, "%+07.3f", decimal); }
NRTAPI(NRT_BOOL) nrt_DateTime_formatMillis(double millis, const char *format, char *outBuf, size_t maxSize, nrt_Error * error) { time_t timeInSeconds; double fractSeconds; struct tm t; char *newFmtString = NULL; const char *endString = NULL; size_t begStringLen = 0; size_t formatLength; size_t startIndex = 0; size_t i, j; NRT_BOOL found = 0; timeInSeconds = (time_t) (millis / 1000); t = *gmtime(&timeInSeconds); fractSeconds = (millis / 1000.0) - timeInSeconds; /* Search for "%...S" string */ formatLength = strlen(format); for (i = 0; i < formatLength && !found; ++i) { if (format[i] == '%') { startIndex = i; for (j = startIndex + 1; j < formatLength; ++j) { if (format[j] == '%') { break; } if (format[j] == 'S') { found = 1; formatLength = j - startIndex + 1; begStringLen = startIndex; endString = format + j + 1; } } } } /* If we found a "%...S" string, parse it */ /* to find out how many decimal places to use */ if (found) { int decimalPlaces = 0; /* Figure out how many decimal places we need... */ for (i = startIndex + 1; i < startIndex + (formatLength - 1); ++i) { if (format[i] == '.') { /* The digits that follow should be */ /* the number of decimal places */ sscanf(format + i + 1, "%d", &decimalPlaces); } } if (decimalPlaces > 0) { char buf[256]; size_t newFmtLen = 0; size_t bufIdx = 0; size_t endStringLen = endString ? strlen(endString) : 0; newFmtLen = begStringLen + 1; newFmtString = (char *) NRT_MALLOC(newFmtLen); if (!newFmtString) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } memset(newFmtString, 0, newFmtLen); if (begStringLen > 0) { /* do the first part of the format */ strncpy(newFmtString, format, begStringLen); if (strftime(outBuf, maxSize, newFmtString, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } bufIdx = strlen(outBuf); } /* do the seconds - separately */ memset(buf, 0, 256); if (strftime(buf, 256, "%S", &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } /* tack it on the end */ strcpy(outBuf + bufIdx, buf); bufIdx = strlen(outBuf); memset(buf, 0, 256); NRT_SNPRINTF(buf, 256, "%.*f", decimalPlaces, fractSeconds); if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } /* tack on the fractional seconds - spare the leading 0 */ strcpy(outBuf + bufIdx, buf + 1); bufIdx = strlen(outBuf); if (endStringLen > 0) { /* tack on the end part */ memset(buf, 0, 256); if (strftime(buf, 256, endString, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } strcpy(outBuf + bufIdx, buf); } } } if (newFmtString == NULL) { if (strftime (outBuf, maxSize, newFmtString != NULL ? newFmtString : format, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", newFmtString != NULL ? newFmtString : format); goto CATCH_ERROR; } } else NRT_FREE(newFmtString); return NRT_SUCCESS; CATCH_ERROR: if (newFmtString) NRT_FREE(newFmtString); return NRT_FAILURE; }