int open_asf_data(const char *filename, const char *band, int multilook, meta_parameters *meta, ClientInterface *client) { ReadAsfClientInfo *info = MALLOC(sizeof(ReadAsfClientInfo)); info->is_rgb = FALSE; info->band_gs = info->band_r = info->band_g = info->band_b = 0; info->ml = multilook; // special hack for Avnir data! if (!band && strcmp_case(meta->general->sensor_name, "AVNIR") == 0 && meta->general->band_count >= 3) { // no band was specifed -- show true color (3,2,1) asfPrintStatus("Avnir data: defaulting to TRUE color -- " "Red=3, Green=2, Blue=1\n"); band = "03,02,01"; } if (band) { char *r, *b, *g; if (split3(band, &r, &g, &b, ',')) { // Looks like we were given 3 bands -- so, we are doing rgb info->band_r = get_band_number(meta->general->bands, meta->general->band_count, r); if (info->band_r < 0) asfPrintWarning("Red band '%s' not found.\n", r); else asfPrintStatus("Red band is band #%d: %s\n", info->band_r+1, r); info->band_g = get_band_number(meta->general->bands, meta->general->band_count, g); if (info->band_g < 0) asfPrintWarning("Green band '%s' not found.\n", g); else asfPrintStatus("Green band is band #%d: %s\n", info->band_g+1, g); info->band_b = get_band_number(meta->general->bands, meta->general->band_count, b); if (info->band_b < 0) asfPrintWarning("Blue band '%s' not found.\n", b); else asfPrintStatus("Blue band is band #%d: %s\n", info->band_b+1, b); if (info->band_r < 0 && info->band_g < 0 && info->band_b < 0) { // none of the bands were found return FALSE; } info->is_rgb = TRUE; FREE(r); FREE(g); FREE(b); set_bands_rgb(info->band_r, info->band_g, info->band_b); } else { // Single band name given info->band_gs = get_band_number(meta->general->bands, meta->general->band_count, (char*)band); if (info->band_gs < 0) { asfPrintWarning("Band '%s' not found.\n", band); return FALSE; } else { asfPrintStatus("Reading band #%d: %s\n", info->band_gs+1, band); } set_bands_greyscale(info->band_gs); } } info->fp = fopen(filename, "rb"); if (!info->fp) { asfPrintWarning("Failed to open ASF Internal file %s: %s\n", filename, strerror(errno)); return FALSE; } client->read_client_info = info; client->read_fn = read_asf_client; client->thumb_fn = get_asf_thumbnail_data; client->free_fn = free_asf_client_info; if (meta->general->data_type == ASF_BYTE) client->data_type = info->is_rgb ? RGB_BYTE : GREYSCALE_BYTE; else client->data_type = info->is_rgb ? RGB_FLOAT : GREYSCALE_FLOAT; return TRUE; }
int open_asf_data(const char *filename, const char *band, int multilook, meta_parameters *meta, ClientInterface *client) { ReadAsfClientInfo *info = MALLOC(sizeof(ReadAsfClientInfo)); info->is_rgb = FALSE; info->band_gs = info->band_r = info->band_g = info->band_b = 0; info->ml = multilook; // special hack for Avnir data! if (!band && strcmp_case(meta->general->sensor_name, "AVNIR") == 0 && meta->general->band_count >= 3) { // no band was specifed -- show true color (3,2,1) asfPrintStatus("Avnir data: defaulting to TRUE color -- " "Red=3, Green=2, Blue=1\n"); band = "03,02,01"; } if (band) { char *r, *b, *g; if (split3(band, &r, &g, &b, ',')) { // Looks like we were given 3 bands -- so, we are doing rgb info->band_r = get_band_number(meta->general->bands, meta->general->band_count, r); if (info->band_r < 0) asfPrintWarning("Red band '%s' not found.\n", r); else asfPrintStatus("Red band is band #%d: %s\n", info->band_r+1, r); info->band_g = get_band_number(meta->general->bands, meta->general->band_count, g); if (info->band_g < 0) asfPrintWarning("Green band '%s' not found.\n", g); else asfPrintStatus("Green band is band #%d: %s\n", info->band_g+1, g); info->band_b = get_band_number(meta->general->bands, meta->general->band_count, b); if (info->band_b < 0) asfPrintWarning("Blue band '%s' not found.\n", b); else asfPrintStatus("Blue band is band #%d: %s\n", info->band_b+1, b); if (info->band_r < 0 && info->band_g < 0 && info->band_b < 0) { // none of the bands were found return FALSE; } info->is_rgb = TRUE; FREE(r); FREE(g); FREE(b); set_bands_rgb(info->band_r, info->band_g, info->band_b); } else { // Single band name given info->band_gs = get_band_number(meta->general->bands, meta->general->band_count, (char*)band); if (info->band_gs < 0) { asfPrintWarning("Band '%s' not found.\n", band); return FALSE; } else { asfPrintStatus("Reading band #%d: %s\n", info->band_gs+1, band); } set_bands_greyscale(info->band_gs); } } info->fp = fopen(filename, "rb"); if (!info->fp) { asfPrintWarning("Failed to open ASF Internal file %s: %s\n", filename, strerror(errno)); return FALSE; } client->read_client_info = info; client->read_fn = read_asf_client; client->thumb_fn = get_asf_thumbnail_data; client->free_fn = free_asf_client_info; if (meta->general->data_type == ASF_BYTE) client->data_type = info->is_rgb ? RGB_BYTE : GREYSCALE_BYTE; else client->data_type = info->is_rgb ? RGB_FLOAT : GREYSCALE_FLOAT; // special sanity checks for ASF data if (meta->general->line_count == 0 || meta->general->sample_count == 0) { asfPrintStatus("Line count: %d\n", meta->general->line_count); asfPrintStatus("Sample count: %d\n", meta->general->sample_count); asfPrintWarning("Line count or sample count is 0.\n"); return FALSE; } long long sz = fileSize(filename); int mult = (info->is_rgb ? 3 : 1) * (meta->general->data_type == ASF_BYTE ? 1 : 4); long long expected_sz = meta->general->line_count * meta->general->sample_count * mult; if (sz < expected_sz) { asfPrintWarning("File is too short! Truncating lines...\n"); int orig = meta->general->line_count; asfPrintStatus("Original line count: %d\n", orig); meta->general->line_count = sz / mult / meta->general->sample_count; asfPrintStatus("Truncated line count (calculated from file size): %d\n", meta->general->line_count); asfPrintWarning("Truncated %d lines!\n", orig - meta->general->line_count); } return TRUE; }