ocssw  1.0
/disk01/web/ocssw/build/src/libmeris/epr_band.c (r8218/r7671)
Go to the documentation of this file.
00001 /*
00002  * $Id: epr_band.c,v 1.2 2009-03-27 10:25:54 sabine Exp $
00003  *
00004  * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU General Public License as published by the
00008  * Free Software Foundation. This program is distributed in the hope it will
00009  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00010  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011  * See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  */
00017 
00018 #include <assert.h>
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <math.h>
00024 
00025 #include "epr_api.h"
00026 #include "epr_core.h"
00027 #include "epr_string.h"
00028 #include "epr_ptrarray.h"
00029 #include "epr_swap.h"
00030 #include "epr_field.h"
00031 #include "epr_record.h"
00032 #include "epr_param.h"
00033 #include "epr_dsd.h"
00034 #include "epr_msph.h"
00035 #include "epr_band.h"
00036 #include "epr_bitmask.h"
00037 
00038 #include "epr_dddb.h"
00039 
00043 EPR_SPtrArray* epr_create_band_ids(EPR_SProductId* product_id) {
00044     EPR_SBandId* band_id = NULL;
00045     EPR_SPtrArray* band_ids = NULL;
00046     char test_block[1024];
00047     int bt_index;
00048     int i;
00049     const struct BandDescriptorTable* b_tables;
00050     int num_descr;
00051 
00052     if (product_id == NULL) {
00053         epr_set_err(e_err_null_pointer,
00054                     "epr_create_band_ids: product_id must not be NULL");
00055         return NULL;
00056     }
00057 
00058 
00059     /* @DDDB */
00060 
00061     b_tables = dddb_band_tables;
00062     bt_index = -1;
00063     for (i = 0; i < EPR_NUM_BAND_TABLES; i++) {
00064         const char* id = b_tables[i].name;
00065         if (strncmp(product_id->id_string, id, 10) == 0) {
00066             if (product_id->meris_iodd_version == 5) {
00067                 if (strcmp(id, "MER_RR__1P_IODD5") == 0 ||
00068                         strcmp(id, "MER_FR__1P_IODD5") == 0) {
00069                     bt_index = i;
00070                 }
00071             } else if (product_id->meris_iodd_version == 6) {
00072                 if (strcmp(id, "MER_RR__2P_IODD6") == 0 ||
00073                         strcmp(id, "MER_FR__2P_IODD6") == 0) {
00074                     bt_index = i;
00075                 }
00076             } else {
00077                 bt_index = i;
00078             }
00079         }
00080         if (bt_index != -1) {
00081             break;
00082         }
00083     }
00084     if (bt_index == -1) {
00085         epr_set_err(e_err_null_pointer,
00086                     "epr_create_band_ids: unknown product type");
00087         return NULL;
00088     }
00089 
00090     band_ids = epr_create_ptr_array(16);
00091     num_descr = b_tables[bt_index].num_descriptors;
00092     for (i = 0; i < num_descr; i++) {
00093 
00094         band_id = (EPR_SBandId*) calloc(1, sizeof (EPR_SBandId));
00095         if (band_id == NULL) {
00096             epr_set_err(e_err_out_of_memory,
00097                         "epr_create_band_ids: out of memory");
00098             return NULL;
00099         }
00100         band_id->magic = EPR_MAGIC_BAND_ID;
00101         band_id->product_id = product_id;
00102 
00103         /* 1: band_name */
00104         epr_assign_string(&band_id->band_name, b_tables[bt_index].descriptors[i].id);
00105         /* 2: dataset_name */
00106         band_id->dataset_ref = epr_get_ref_struct(product_id, b_tables[bt_index].descriptors[i].rec_name);
00107         if (band_id->dataset_ref.dataset_id == NULL) {
00108             epr_set_err(e_err_invalid_dataset_name,
00109                         "epr_create_band_ids: invalid dataset name in DDDB");
00110             epr_free_band_id(band_id);
00111             return NULL;
00112         }
00113         /* 3: sample_offset */
00114         band_id->sample_model = b_tables[bt_index].descriptors[i].sample_offset;
00115         /* 4: band_datatype */
00116         band_id->data_type = b_tables[bt_index].descriptors[i].type;
00117         /* 5: spectr_band_index*/
00118         band_id->spectr_band_index = b_tables[bt_index].descriptors[i].spectral_index;
00119         /* 6: scaling_method*/
00120         if (b_tables[bt_index].descriptors[i].scale_method == e_smid_non) {
00121             band_id->scaling_method = 0;
00122             band_id->scaling_offset = 0.0;
00123             band_id->scaling_factor = 1.0;
00124         } else {
00125             band_id->scaling_method = b_tables[bt_index].descriptors[i].scale_method;
00126             /* 7: scaling_offset*/
00127             strcpy (test_block, b_tables[bt_index].descriptors[i].scale_offset);
00128             if (test_block == NULL) {
00129                 band_id->scaling_offset = 0.0;
00130             } else {
00131                 float scaling_offset = (float)atof(test_block);
00132                 if (epr_numeral_suspicion(test_block) == 1) {
00133                     band_id->scaling_offset = scaling_offset;
00134                 } else {
00135                     scaling_offset = epr_get_scaling_params(product_id, test_block);
00136                     if (scaling_offset == -909.909) { /* @todo what an ugly return value. Eeeek!*/
00137                         epr_set_err(e_err_invalid_dataset_name,
00138                                     "epr_create_band_ids: invalid dataset name in dddb");
00139                         epr_free_band_id(band_id);
00140                         return NULL;
00141                     }
00142                     band_id->scaling_offset = scaling_offset;
00143                 }
00144             }
00145             /* 8: scaling_factor*/
00146             strcpy (test_block, b_tables[bt_index].descriptors[i].scale_factor);
00147             if (test_block == NULL) {
00148                 band_id->scaling_factor = 0.0;
00149             } else {
00150                 float scaling_factor = (float)atof(test_block);
00151                 if (epr_numeral_suspicion(test_block) == 1) {
00152                     band_id->scaling_factor = scaling_factor;
00153                 } else {
00154                     scaling_factor = epr_get_scaling_params(product_id, test_block);
00155                     if (scaling_factor == -909.909) { /* @todo what an ugly return value. Eeeek!*/
00156                         epr_set_err(e_err_invalid_dataset_name,
00157                                     "epr_create_band_ids: invalid dataset name in dddb");
00158                         epr_free_band_id(band_id);
00159                         return NULL;
00160                     }
00161                     band_id->scaling_factor = scaling_factor;
00162                 }
00163             }
00164         }
00165         /* 9: bit_expr*/
00166         epr_assign_string(&band_id->bm_expr, b_tables[bt_index].descriptors[i].bitmask_expr);
00167         /* 10: flags_definition_file*/
00168         if (b_tables[bt_index].descriptors[i].flag_coding_name != NULL) {
00169             band_id->flag_coding = epr_create_flag_coding(product_id, b_tables[bt_index].descriptors[i].flag_coding_name);
00170             if (band_id->flag_coding == NULL) {
00171                 epr_set_err(e_err_out_of_memory,
00172                             "epr_create_band_ids: out of memory");
00173                 epr_free_band_id(band_id);
00174                 return NULL;
00175             }
00176         } else {
00177             band_id->flag_coding = NULL;
00178         }
00179         /* 11: unit*/
00180         epr_assign_string(&band_id->unit, b_tables[bt_index].descriptors[i].unit);
00181         /* 12: description*/
00182         epr_assign_string(&band_id->description, b_tables[bt_index].descriptors[i].description);
00183 
00184         /* lines_flipped*/
00185         if (strncmp(product_id->id_string, EPR_ENVISAT_PRODUCT_MERIS, 3) == 0
00186                 || strncmp(product_id->id_string, EPR_ENVISAT_PRODUCT_AATSR, 3) == 0) {
00187             band_id->lines_mirrored = EPR_TRUE;
00188         } else {
00189             if (strncmp(product_id->id_string, EPR_ENVISAT_PRODUCT_ASAR, 3) == 0
00190                     && strncmp(product_id->id_string, "ASA_IMG", 7) != 0
00191                     && strncmp(product_id->id_string, "ASA_APG", 7) != 0) {
00192                 band_id->lines_mirrored = EPR_TRUE;
00193             } else {
00194                 if (strncmp(product_id->id_string, EPR_ENVISAT_PRODUCT_SAR, 3) == 0
00195                         && strncmp(product_id->id_string, "SAR_IMG", 7) != 0
00196                         && strncmp(product_id->id_string, "SAR_APG", 7) != 0) {
00197                     band_id->lines_mirrored = EPR_TRUE;
00198                 } else {
00199                     band_id->lines_mirrored = EPR_FALSE;
00200                 }
00201             }
00202         }
00203 
00204         epr_add_ptr_array_elem(band_ids, band_id);
00205     }
00206 
00207     return band_ids;
00208 }
00209 
00210 
00211 epr_uint epr_get_num_bands(EPR_SProductId* product_id) {
00212     epr_clear_err();
00213     if (!epr_check_api_init_flag()) {
00214         return 0;
00215     }
00216 
00217     if (product_id == NULL) {
00218         epr_set_err(e_err_null_pointer,
00219                     "epr_get_num_bands: product_id must not be NULL");
00220         return (epr_uint) -1;
00221     }
00222     return product_id->band_ids->length;
00223 }
00224 
00225 EPR_SBandId* epr_get_band_id_at(EPR_SProductId* product_id, epr_uint index) {
00226     EPR_SBandId* band_id = NULL;
00227 
00228     epr_clear_err();
00229 
00230     if (product_id == NULL) {
00231         epr_set_err(e_err_null_pointer,
00232                     "epr_get_band_id_at: product_id must not be NULL");
00233         return NULL;
00234     }
00235     if (index >= product_id->band_ids->length) {
00236         epr_set_err(e_err_index_out_of_range,
00237                     "epr_get_band_id_at: band index out of range");
00238         return NULL;
00239     }
00240 
00241     band_id = (EPR_SBandId*)epr_get_ptr_array_elem_at(product_id->band_ids, index);
00242     return band_id;
00243 }
00244 
00245 EPR_SBandId* epr_get_band_id(EPR_SProductId* product_id, const char* band_name) {
00246     EPR_SBandId* band_id = NULL;
00247     int num_bands, i;
00248 
00249     epr_clear_err();
00250 
00251     if (product_id == NULL) {
00252         epr_set_err(e_err_null_pointer,
00253                     "epr_get_band_id: product_id must not be NULL");
00254         return NULL;
00255     }
00256     if (band_name == NULL) {
00257         epr_set_err(e_err_null_pointer,
00258                     "epr_get_band_id: dataset_name must not be NULL");
00259         return NULL;
00260     }
00261 
00262     num_bands = epr_get_num_bands(product_id);
00263     for (i = 0; i < num_bands; i++) {
00264         band_id = epr_get_band_id_at(product_id, i);
00265         if (epr_equal_names(band_name, epr_get_band_name(band_id))) {
00266             return band_id;
00267         }
00268     }
00269     epr_set_err(e_err_invalid_band_name,
00270                 "epr_get_band_id: band not found");
00271     return NULL;
00272 }
00273 
00274 const char* epr_get_band_name(EPR_SBandId* band_id) {
00275     epr_clear_err();
00276 
00277     if (band_id == NULL) {
00278         epr_set_err(e_err_null_pointer,
00279                     "epr_get_band_name: band_id must not be NULL");
00280         return NULL;
00281     }
00282     return band_id->band_name;
00283 }
00284 
00292 void epr_free_band_id(EPR_SBandId* band_id) {
00293     if (band_id == NULL)
00294         return;
00295 
00296     band_id->dataset_ref.elem_index = -1;
00297     band_id->dataset_ref.field_index = -1;
00298     band_id->dataset_ref.dataset_id = NULL;
00299 
00300     epr_free_and_null_string(&band_id->band_name);
00301     epr_free_and_null_string(&band_id->bm_expr);
00302 
00303     epr_free_flag_coding(band_id->flag_coding);
00304     band_id->flag_coding = NULL;
00305 
00306     band_id->spectr_band_index = 0;
00307     band_id->scaling_offset  = 0;
00308     band_id->scaling_factor  = 0;
00309     band_id->data_type = e_tid_unknown;
00310 
00311     epr_free_and_null_string(&band_id->unit);
00312     epr_free_and_null_string(&band_id->description);
00313 
00314     band_id->lines_mirrored = EPR_FALSE;
00315 
00316     free(band_id);
00317 }
00318 
00319 
00328 float epr_get_scaling_params(EPR_SProductId* product_id,  const char* str) {
00329     EPR_SDatasetRef scal_fact;
00330     const EPR_SField* field = NULL;
00331     EPR_SRecord* record = NULL;
00332     float ziff;
00333 
00334     scal_fact = epr_get_ref_struct(product_id, str);
00335     if (scal_fact.dataset_id == NULL) {
00336         return (float)(-909.909);
00337     }
00338 
00339     /*'Scaling_Factor_GADS'*/
00340     record = epr_create_record(scal_fact.dataset_id);
00341     record = epr_read_record(scal_fact.dataset_id, 0, record);
00342 
00343     field = epr_get_field_at(record, scal_fact.field_index - 1);
00344     ziff = epr_get_field_elem_as_float(field, (epr_uint)(scal_fact.elem_index - 1));
00345 
00346     epr_free_record(record);
00347 
00348     return ziff;
00349 }
00350 
00351 
00360 float epr_get_scaling_factor(EPR_SProductId* product_id,  const char* str) {
00361     EPR_SDatasetRef scal_fact;
00362     const EPR_SField* field = NULL;
00363     EPR_SRecord* record = NULL;
00364     float ziff;
00365 
00366     scal_fact = epr_get_ref_struct(product_id, str);
00367     if (scal_fact.dataset_id == NULL) {
00368         return (float)(-909.909);
00369     }
00370 
00371     /*'Scaling_Factor_GADS'*/
00372     record = epr_create_record(scal_fact.dataset_id);
00373     record = epr_read_record(scal_fact.dataset_id, 0, record);
00374 
00375     field = epr_get_field_at(record, scal_fact.field_index - 1);
00376     ziff = epr_get_field_elem_as_float(field, (epr_uint)(scal_fact.elem_index - 1));
00377 
00378     epr_free_record(record);
00379 
00380     return ziff;
00381 }
00382 
00391 EPR_SDatasetRef epr_get_ref_struct(EPR_SProductId* product_id, const char* str) {
00392     EPR_SDatasetRef ref_struct;
00393     int pos = 0;
00394     char* stopstring;
00395     char* token;
00396 
00397     ref_struct.dataset_id = NULL;
00398     ref_struct.field_index = -1;
00399     ref_struct.elem_index = -1;
00400 
00401     token = epr_str_tok(str, ".", &pos);
00402 
00403     ref_struct.dataset_id = epr_get_dataset_id(product_id, token);
00404     if (ref_struct.dataset_id == NULL) {
00405         epr_free_and_null_string(&token);
00406         return ref_struct;
00407     }
00408     epr_free_and_null_string(&token);
00409 
00410     token = epr_str_tok(str, ".", &pos);
00411     if (token == NULL) {
00412         ref_struct.field_index = -1;
00413     } else {
00414         ref_struct.field_index = strtol(token, &stopstring, 10);
00415     }
00416     epr_free_and_null_string(&token);
00417 
00418     token = epr_str_tok(str, ".", &pos);
00419     if (token == NULL) {
00420         ref_struct.elem_index = -1;
00421     } else {
00422         ref_struct.elem_index = strtol(token, &stopstring, 10);
00423     }
00424     epr_free_and_null_string(&token);
00425 
00426     return ref_struct;
00427 }
00428 
00437 EPR_EScalingMethod epr_str_to_scaling_method(const char* str) {
00438     assert(str != NULL);
00439     if (epr_equal_names(str, "Linear_Scale"))
00440         return e_smid_lin;
00441     else if (epr_equal_names(str, "Log_Scale"))
00442         return e_smid_log;
00443     else
00444         return e_smid_non;
00445 }
00446 
00447 
00456 EPR_ESampleModel epr_str_to_sample_offset(const char* str) {
00457     assert(str != NULL);
00458     if (epr_equal_names(str, "1OF2"))
00459         return e_smod_1OF2;
00460     else if (epr_equal_names(str, "2OF2"))
00461         return e_smod_2OF2;
00462     else if (epr_equal_names(str, "3TOI"))
00463         return e_smod_3TOI;
00464     else if (epr_equal_names(str, "2TOF"))
00465         return e_smod_2TOF;
00466     else
00467         return e_smod_1OF1;
00468 }
00469 
00480 EPR_SRaster* epr_create_bitmask_raster(epr_uint source_width,
00481                                        epr_uint source_height,
00482                                        epr_uint source_step_x,
00483                                        epr_uint source_step_y) {
00484     return epr_create_raster(e_tid_uchar,
00485                              source_width,
00486                              source_height,
00487                              source_step_x,
00488                              source_step_y);
00489 }
00490 
00491 
00503 EPR_SRaster* epr_create_raster(EPR_EDataTypeId data_type,
00504                                epr_uint source_width,
00505                                epr_uint source_height,
00506                                epr_uint source_step_x,
00507                                epr_uint source_step_y) {
00508     EPR_SRaster* raster = NULL;
00509     epr_uint num_elems;
00510 
00511     epr_clear_err();
00512 
00513     if (data_type == e_tid_string ||
00514             data_type == e_tid_spare ||
00515             data_type == e_tid_time) {
00516         epr_set_err(e_err_illegal_data_type, "epr_create_raster: illegal data type");
00517         return NULL;
00518     }
00519 
00520     raster = (EPR_SRaster*) calloc(1, sizeof (EPR_SRaster));
00521     if (raster == NULL) {
00522         epr_set_err(e_err_out_of_memory, "epr_create_raster: out of memory");
00523         return NULL;
00524     }
00525 
00526 
00527     raster->magic         = EPR_MAGIC_RASTER;
00528     raster->data_type     = data_type;
00529     raster->elem_size     = epr_get_data_type_size(data_type);
00530     raster->source_height = source_height;
00531     raster->source_width  = source_width;
00532     raster->source_step_x = source_step_x;
00533     raster->source_step_y = source_step_y;
00534     raster->raster_width  = (source_width  - 1) / source_step_x + 1;
00535     raster->raster_height = (source_height - 1) / source_step_y + 1;
00536 
00537     num_elems = raster->raster_width * raster->raster_height;
00538 
00539     raster->buffer = calloc(raster->elem_size, num_elems);
00540     if (raster->buffer == NULL) {
00541         epr_free_raster(raster);
00542         epr_set_err(e_err_out_of_memory, "epr_create_raster: out of memory");
00543         return NULL;
00544     }
00545 
00546     return raster;
00547 }
00548 
00549 
00557 EPR_SRaster* epr_create_compatible_raster(EPR_SBandId* band_id,
00558         epr_uint source_width,
00559         epr_uint source_height,
00560         epr_uint source_step_x,
00561         epr_uint source_step_y) {
00562     epr_clear_err();
00563 
00564     if (band_id == NULL) {
00565         epr_set_err(e_err_invalid_band, "epr_create_raster: band_id must not be NULL");
00566         return NULL;
00567     }
00568     return epr_create_raster(band_id->data_type,
00569                              source_width,
00570                              source_height,
00571                              source_step_x,
00572                              source_step_y);
00573 }
00574 
00575 
00576 void epr_free_raster(EPR_SRaster* raster) {
00577     epr_clear_err();
00578 
00579     if (raster == NULL)
00580         return;
00581 
00582     raster->data_type = e_tid_unknown;
00583     raster->elem_size     = 0;
00584     raster->raster_height = 0;
00585     raster->raster_width  = 0;
00586     raster->source_height = 0;
00587     raster->source_width  = 0;
00588     raster->source_step_x = 0;
00589     raster->source_step_y = 0;
00590 
00591     if (raster->buffer != NULL) {
00592         free(raster->buffer);
00593         raster->buffer = NULL;
00594     }
00595 
00596     free(raster);
00597 }
00598 
00599 
00600 
00601 int epr_read_band_raster(EPR_SBandId* band_id,
00602                          int offset_x,
00603                          int offset_y,
00604                          EPR_SRaster* raster/*, EPR_SRaster** bitmask_raster*/) {
00605 
00606     EPR_SProductId* product_id = NULL;
00607     EPR_SDatasetId* dataset_id = NULL;
00608     char* rec_type;
00609 
00610     epr_clear_err();
00611 
00612     if (band_id == NULL) {
00613         epr_set_err(e_err_invalid_band,
00614                     "epr_read_band_raster: band_id must not be NULL");
00615         return epr_get_last_err_code();
00616     }
00617     if (band_id->data_type != raster->data_type) {
00618         epr_set_err(e_err_illegal_data_type,
00619                     "epr_read_band_raster: illegal raster data type");
00620         return epr_get_last_err_code();
00621     }
00622     if (raster->buffer == NULL) {
00623         epr_set_err(e_err_illegal_arg,
00624                     "epr_read_band_raster: raster->buffer must not be NULL");
00625         return epr_get_last_err_code();
00626     }
00627     if ((offset_x<0) || (offset_y<0) || (raster->raster_width<0) || (raster->raster_height<0) || (raster->source_step_x<0) || (raster->source_step_y<0)) {
00628         epr_set_err(e_err_invalid_value,
00629                     "epr_read_band_raster: all digit parameter must be positive");
00630         return epr_get_last_err_code();
00631     }
00632     /*  removed because the source_step_x can truly be greater than raster_width.
00633     if ((raster->source_step_x>raster->raster_width) || (raster->source_step_y>raster->raster_height)) {
00634         epr_set_err(e_err_invalid_value,
00635                 "epr_read_band_raster: too small raster sizes or large steps");
00636         return epr_get_last_err_code();
00637     }
00638     */
00639     product_id = band_id->product_id;
00640     dataset_id = band_id->dataset_ref.dataset_id;
00641     rec_type = dataset_id->dsd->ds_type;
00642     if (strcmp(rec_type, "M") == 0) {
00643         if (epr_read_band_measurement_data(band_id,
00644                                            offset_x,
00645                                            offset_y,
00646                                            raster) != 0) {
00647             epr_set_err(e_err_file_read_error,
00648                         "epr_read_band_raster: unsuccessfully reading band measurement data");
00649             return epr_get_last_err_code();
00650         }
00651         if (band_id->bm_expr != NULL) {
00652             EPR_SRaster* bm_raster;
00653 
00654             bm_raster = epr_create_raster(e_tid_uchar, /*was char*/
00655                                           raster->source_width,
00656                                           raster->source_height,
00657                                           raster->source_step_x,
00658                                           raster->source_step_y);
00659 
00660 
00661             epr_read_bitmask_raster(product_id,
00662                                             band_id->bm_expr,
00663                                             offset_x,
00664                                             offset_y,
00665                                             bm_raster);
00666 
00667             epr_zero_invalid_pixels(raster, bm_raster);
00668 
00669             epr_free_raster(bm_raster);
00670 
00671         }
00672     } else if (strcmp(rec_type, "A") == 0) {
00673         if (epr_read_band_annotation_data
00674                 (band_id, offset_x, offset_y, raster) == 1) {
00675             epr_set_err(e_err_file_read_error,
00676                         "epr_read_band_raster: unsuccessfully reading band annotation data");
00677             return epr_get_last_err_code();
00678         }
00679     } else {
00680         epr_set_err(e_err_invalid_value,
00681                     "epr_read_band_raster: illegat DS-TYPE; 'A' or'M' will be accepted");
00682         return epr_get_last_err_code();
00683     }
00684     return e_err_none;
00685 }
00686 
00697 int epr_read_band_measurement_data(EPR_SBandId* band_id,
00698                                    int offset_x,
00699                                    int offset_y,
00700                                    EPR_SRaster* raster) {
00701     EPR_SProductId* product_id = NULL;
00702     const EPR_SField* field = NULL;
00703     EPR_SFieldInfo* field_info = NULL;
00704     EPR_SDatasetId* dataset_id = NULL;
00705     EPR_SRecord* record = NULL;
00706     EPR_SRecord* sph_record = NULL;
00707     EPR_EDataTypeId band_datatype, datatype_id;
00708     EPR_ESampleModel band_smod;
00709     //epr_uint rec_size;
00710     epr_uint rec_numb;
00711     int iY, raster_pos, delta_raster_pos;
00712     int offset_x_mirrored = 0;
00713     epr_uint scan_line_length;
00714     EPR_FLineDecoder decode_func;
00715     epr_uint scene_width;
00716 
00717     product_id = band_id->product_id;
00718 
00719     if (strncmp(EPR_ENVISAT_PRODUCT_MERIS, product_id->id_string, 3) == 0) {
00720         sph_record = product_id->sph_record;
00721         field = epr_get_field(sph_record, "LINE_LENGTH");
00722         scan_line_length = epr_get_field_elem_as_uint(field, 0);
00723     } else if (strncmp(EPR_ENVISAT_PRODUCT_AATSR, product_id->id_string, 3) == 0) {
00724         scan_line_length = EPR_ATS_LINE_LENGTH;
00725     } else if (strncmp(EPR_ENVISAT_PRODUCT_ASAR, product_id->id_string, 3) == 0) {
00726         scan_line_length = epr_get_scene_width(product_id);
00727     } else if (strncmp(EPR_ENVISAT_PRODUCT_SAR, product_id->id_string, 3) == 0) {
00728         scan_line_length = epr_get_scene_width(product_id);
00729     } else {
00730         epr_set_err(e_err_illegal_arg,
00731                     "epr_read_band_measurement_data: scan line length unknown");
00732         return epr_get_last_err_code();
00733     }
00734 
00735     dataset_id = band_id->dataset_ref.dataset_id;
00736     /*the length of measurement record size*/
00737     //rec_size = dataset_id->dsd->dsr_size;
00738     /*the number of measurement records*/
00739     rec_numb = dataset_id->dsd->num_dsr;
00740     /*data type in the band*/
00741     band_datatype = band_id->data_type;
00742     /*data model in the band*/
00743     band_smod = band_id->sample_model;
00744     record = epr_create_record(dataset_id);
00745     field_info = (EPR_SFieldInfo*)epr_get_ptr_array_elem_at(record->info->field_infos, band_id->dataset_ref.field_index - 1);
00746     datatype_id = field_info->data_type_id;
00747 
00748     /* if the user raster (or part of) is outside bbox in source coordinates*/
00749     if (offset_x + raster->raster_width > (int)scan_line_length) {
00750         epr_set_err(e_err_illegal_arg,
00751                     "epr_read_band_measurement_data: raster x co-ordinates out of bounds");
00752         epr_free_record(record);
00753         return epr_get_last_err_code();
00754     }
00755     if (offset_y + raster->raster_height > (int)(rec_numb)) {
00756         epr_set_err(e_err_illegal_arg,
00757                     "epr_read_band_measurement_data: raster y co-ordinates out of bounds");
00758         epr_free_record(record);
00759         return epr_get_last_err_code();
00760     }
00761     raster_pos = 0;
00762     delta_raster_pos = (int)floor((raster->source_width - 1) / raster->source_step_x) + 1;
00763 
00764     /*select the correspondent function to scaling and transform data type*/
00765     decode_func = select_line_decode_function(band_datatype, band_smod, datatype_id);
00766     if (decode_func == NULL) {
00767         epr_set_err(e_err_illegal_data_type,
00768                     "epr_read_band_measurement_data: internal error: unknown data type");
00769         epr_free_record(record);
00770         return epr_get_last_err_code();
00771     }
00772 
00773     scene_width = band_id->product_id->scene_width;
00774     if (band_id->lines_mirrored) {
00775         offset_x_mirrored = (scene_width - 1) - (offset_x + raster->source_width - 1);
00776     } else {
00777         offset_x_mirrored = offset_x;
00778     }
00779 
00780     for (iY = offset_y; (epr_uint)iY < offset_y + raster->source_height; iY += raster->source_step_y ) {
00781 
00782         /*get the next record by the given name*/
00783         record = epr_read_record(dataset_id, iY, record);
00784         if (record == NULL) {
00785             return epr_get_last_err_code();
00786         }
00787         /*get the field at its number*/
00788         field = epr_get_field_at(record, band_id->dataset_ref.field_index - 1);
00789         /*get the scaled "line" of physical values*/
00790         decode_func(field->elems, band_id, offset_x_mirrored, raster->source_width, raster->source_step_x, raster->buffer, raster_pos);
00791         /*locate "data point" for the next "line"*/
00792         raster_pos += delta_raster_pos;
00793     }
00794 
00795     if (band_id->lines_mirrored) {
00796         if (band_datatype == e_tid_float) {
00797             mirror_float_array((float*)raster->buffer, raster->raster_width, raster->raster_height);
00798         } else if (band_datatype == e_tid_uchar || band_datatype == e_tid_char) {
00799             mirror_uchar_array((epr_uchar*)raster->buffer, raster->raster_width, raster->raster_height);
00800         } else if (band_datatype == e_tid_ushort || band_datatype == e_tid_short) {
00801             mirror_ushort_array((epr_ushort*)raster->buffer, raster->raster_width, raster->raster_height);
00802         } else if (band_datatype == e_tid_uint || band_datatype == e_tid_int) {
00803             mirror_uint_array((epr_uint*)raster->buffer, raster->raster_width, raster->raster_height);
00804         } else {
00805             epr_set_err(e_err_illegal_data_type,
00806                         "epr_read_band_measurement_data: internal error: unknown data type");
00807             epr_free_record(record);
00808             return epr_get_last_err_code();
00809         }
00810     }
00811 
00812     epr_free_record(record);
00813 
00814     return 0;
00815 }
00816 
00817 
00828 int epr_read_band_annotation_data(EPR_SBandId* band_id,
00829                                   int offset_x,
00830                                   int offset_y,
00831                                   EPR_SRaster* raster) {
00832     EPR_SProductId* product_id = NULL;
00833     const EPR_SField* field = NULL;
00834     const EPR_SField* field_beg = NULL;
00835     const EPR_SField* field_end = NULL;
00836     EPR_SFieldInfo* field_info = NULL;
00837     EPR_SDatasetId* dataset_id = NULL;
00838     EPR_SRecord* record = NULL;
00839     EPR_SRecord* record_beg = NULL;
00840     EPR_SRecord* record_end = NULL;
00841     EPR_SRecord* sph_record = NULL;
00842     EPR_EDataTypeId band_datatype = 0, datatype_id = 0;
00843     //EPR_ESampleModel band_smod = 0;
00844     epr_uint rec_numb = 0;
00845     epr_uint lines_per_tie_pt, samples_per_tie_pt, scan_line_length;
00846     int iY, raster_pos, delta_raster_pos;
00847     EPR_FArrayTransformer transform_array_func = NULL;
00848     int y_beg, y_end, y_beg_old, y_end_old;
00849     //int offset_x_mirrored = 0;
00850     epr_uint num_elems = 0;
00851     float y_mod = 0;
00852     float scan_offset_x = 0;
00853     float scan_offset_y = 0;
00854     void* line_beg_buffer = NULL;
00855     void* line_end_buffer = NULL;
00856 
00857     product_id = band_id->product_id;
00858 
00859     dataset_id = band_id->dataset_ref.dataset_id;
00860     /*the length of annotation record size*/
00861     //rec_size = dataset_id->dsd->dsr_size;
00862     /*the number of annotation records*/
00863     rec_numb = dataset_id->dsd->num_dsr;
00864     /*data type in the band*/
00865     band_datatype = band_id->data_type;
00866     /*data model in the band*/
00867     //band_smod = band_id->sample_model;
00868     record = epr_create_record(dataset_id);
00869     field_info = (EPR_SFieldInfo*)epr_get_ptr_array_elem_at(record->info->field_infos, band_id->dataset_ref.field_index - 1);
00870     datatype_id = field_info->data_type_id;
00871 
00872 
00873     /*find LINES_PER_TIE_PT & SAMPLES_PER_TIE_PT for different products*/
00874     if (strncmp(EPR_ENVISAT_PRODUCT_MERIS, product_id->id_string, 3) == 0) {
00875         /*elements number in the band (e.g.71)*/
00876         scan_offset_x = 0.0F; 
00877         scan_offset_y = 0.0F; 
00878         num_elems = field_info->num_elems;
00879         sph_record = product_id->sph_record;
00880         field = epr_get_field(sph_record, "LINES_PER_TIE_PT");
00881         lines_per_tie_pt = epr_get_field_elem_as_uint(field, 0);
00882         field = epr_get_field(sph_record, "SAMPLES_PER_TIE_PT");
00883         samples_per_tie_pt = epr_get_field_elem_as_uint(field, 0);
00884         field = epr_get_field(sph_record, "LINE_LENGTH");
00885         scan_line_length = epr_get_field_elem_as_uint(field, 0);
00886     } else if (strncmp(EPR_ENVISAT_PRODUCT_AATSR, product_id->id_string, 3) == 0) {
00887         scan_offset_y = 0.0F; 
00888         scan_line_length = EPR_ATS_LINE_LENGTH;
00889         lines_per_tie_pt = EPR_AATSR_LINES_PER_TIE_PT;
00890         num_elems = field_info->num_elems;
00891         if (num_elems == EPR_ATS_NUM_PER_POINT_ACROSS_LOCAT) {
00892             scan_offset_x = -19.0F;
00893             samples_per_tie_pt = 25;
00894         } else if (num_elems == EPR_ATS_NUM_PER_POINT_ACROSS_SOLAR) {
00895             scan_offset_x = 6.0F;
00896             samples_per_tie_pt = 50;
00897         } else {
00898             epr_set_err(e_err_invalid_value, "epr_read_band_annotation_data: internal error: illegal value for samples_per_tie_pt");
00899             epr_free_record(record);
00900             return epr_get_last_err_code();
00901         }
00902     } else if ((strncmp(EPR_ENVISAT_PRODUCT_ASAR, product_id->id_string, 3) == 0) ||
00903                (strncmp(EPR_ENVISAT_PRODUCT_SAR, product_id->id_string, 3) == 0)) {
00904         EPR_SDatasetId* dataset_id = NULL;
00905         epr_uint num_rec;
00906         scan_offset_x = 0.5F; /* @todo CHECK THIS FOR ASAR! */
00907         scan_offset_y = 0.5F;
00908         scan_line_length = epr_get_scene_width(product_id);
00909         samples_per_tie_pt = scan_line_length / (EPR_ASAR_NUM_PER_POINT_ACROSS_LOCAT - 1);
00910         dataset_id = epr_get_dataset_id(product_id, "GEOLOCATION_GRID_ADS");
00911         num_rec = epr_get_num_records(dataset_id);
00912         lines_per_tie_pt = epr_get_scene_height(product_id) / (num_rec - 1);
00913         num_elems = field_info->num_elems;
00914     } else {
00915         epr_set_err(e_err_illegal_arg,
00916                     "epr_read_band_annotation_data: unhandled ENVISAT product type");
00917         epr_free_record(record);
00918         return epr_get_last_err_code();
00919     }
00920 
00921     /*memory allocate for the increasingly begin tie point line*/
00922     line_beg_buffer = calloc(sizeof(float), num_elems);
00923     if (line_beg_buffer == NULL) {
00924         epr_set_err(e_err_out_of_memory, "epr_read_band_annotation_data: out of memory");
00925         epr_free_record(record);
00926         return epr_get_last_err_code();
00927     }
00928     /*memory allocate for the increasingly end tie point line*/
00929     line_end_buffer = calloc(sizeof(float), num_elems);
00930     if (line_end_buffer == NULL)  {
00931         epr_set_err(e_err_out_of_memory, "epr_read_band_annotation_data: out of memory");
00932         epr_free_record(record);
00933         free(line_beg_buffer);
00934         return epr_get_last_err_code();
00935     }
00936     /* if the user raster (or its part) is outside of orbit in source coordinates*/
00937     if (offset_x + raster->raster_width > (int)scan_line_length) {
00938         epr_set_err(e_err_illegal_arg,
00939                     "epr_read_band_data: raster x co-ordinates out of bounds");
00940         epr_free_record(record);
00941         free(line_beg_buffer);
00942         free(line_end_buffer);
00943         return epr_get_last_err_code();
00944     }
00945     if (offset_y + raster->raster_height > (int)(rec_numb * lines_per_tie_pt)) {
00946         epr_set_err(e_err_illegal_arg,
00947                     "epr_read_band_data: raster y co-ordinates out of bounds");
00948         epr_free_record(record);
00949         free(line_beg_buffer);
00950         free(line_end_buffer);
00951         return epr_get_last_err_code();
00952     }
00953     raster_pos = 0;
00954 
00955     delta_raster_pos = (int)floor((raster->source_width - 1) / raster->source_step_x) + 1;
00956 
00957     /*select the correspondent function to scaling and transform data type*/
00958     transform_array_func = select_transform_array_function(band_datatype, datatype_id);
00959     if (transform_array_func == NULL) {
00960         epr_set_err(e_err_illegal_data_type,
00961                     "epr_read_band_annotation_data: internal error: illegal data type");
00962         epr_free_record(record);
00963         free(line_beg_buffer);
00964         free(line_end_buffer);
00965         return epr_get_last_err_code();
00966     }
00967     y_beg_old = 9999;
00968     y_end_old = 9999;
00969 
00970     //if (band_id->lines_mirrored) {
00971     //    offset_x_mirrored = num_elems - (offset_x + raster->source_width - 1) - 1;
00972     //} else {
00973     //    offset_x_mirrored = offset_x;
00974     //}
00975 
00976     for (iY = offset_y; (epr_uint)iY < offset_y + raster->source_height; iY += raster->source_step_y ) {
00977 
00978         /*find the increasing neighbour begin and end tie point lines*/
00979         y_mod = ((float)iY - scan_offset_y) / lines_per_tie_pt;
00980         y_beg = (epr_uint)floor(y_mod);
00981 
00982         if (y_beg < 0) {
00983             y_beg = 0;
00984         }
00985         if ((epr_uint)y_beg > dataset_id->dsd->num_dsr - 2) {
00986             y_beg = dataset_id->dsd->num_dsr - 2;
00987         }
00988 
00989         y_mod -= y_beg;
00990         y_end = y_beg + 1;
00991 
00992         /*as long as between increasing neighbour tie point lines, not to change them*/
00993         if (y_beg_old != y_beg) {
00994             record_beg = epr_read_record(dataset_id, y_beg, record_beg);
00995             y_beg_old = y_beg;
00996         }
00997         if (y_end_old != y_end) {
00998             record_end = epr_read_record(dataset_id, y_end, record_end);
00999             y_end_old = y_end;
01000         }
01001 
01002         /*get the values for the increasing neighbour tie point lines*/
01003         field_beg = epr_get_field_at(record_beg, band_id->dataset_ref.field_index - 1);
01004         field_end = epr_get_field_at(record_end, band_id->dataset_ref.field_index - 1);
01005 
01006         /*transform and scale the values for the increasing neighbour tie point lines*/
01007         transform_array_func(field_beg->elems, band_id, line_beg_buffer, num_elems);
01008         transform_array_func(field_end->elems, band_id, line_end_buffer, num_elems);
01009 
01010         /*get the "line" of interpolated physical values from tie point data*/
01011         decode_tiepoint_band(line_beg_buffer, line_end_buffer,
01012                              samples_per_tie_pt, num_elems, band_id, offset_x, scan_offset_x, y_mod,
01013                              raster->source_width, raster->source_step_x, raster->buffer, raster_pos);
01014         /*locate "data point" for the next "line"*/
01015         raster_pos += delta_raster_pos;
01016     }
01017 
01018     if (strncmp(EPR_ENVISAT_PRODUCT_MERIS, product_id->id_string, 3) == 0) {
01019         mirror_float_array((float*)raster->buffer, raster->raster_width, raster->raster_height);
01020     } else {
01021         if (strncmp(EPR_ENVISAT_PRODUCT_AATSR, product_id->id_string, 3) == 0) {
01022             mirror_float_array((float*)raster->buffer, raster->raster_width, raster->raster_height);
01023         } else {
01024             if (strncmp(EPR_ENVISAT_PRODUCT_ASAR, product_id->id_string, 3) == 0
01025                     && strncmp(product_id->id_string, "ASA_IMG", 7) != 0
01026                     && strncmp(product_id->id_string, "ASA_APG", 7) != 0) {
01027                 mirror_float_array((float*)raster->buffer, raster->raster_width, raster->raster_height);
01028             } else {
01029                 if (strncmp(EPR_ENVISAT_PRODUCT_SAR, product_id->id_string, 3) == 0
01030                         && strncmp(product_id->id_string, "SAR_IMG", 7) != 0
01031                         && strncmp(product_id->id_string, "SAR_APG", 7) != 0) {
01032                     mirror_float_array((float*)raster->buffer, raster->raster_width, raster->raster_height);
01033                 }
01034             }
01035         }
01036     }
01037 
01038     epr_free_record(record_beg);
01039     epr_free_record(record_end);
01040     epr_free_record(record);
01041     free(line_beg_buffer);
01042     free(line_end_buffer);
01043     return 0;
01044 }
01045 
01046 
01047 epr_uint epr_get_raster_elem_size(const EPR_SRaster* raster) {
01048     if (raster == NULL) {
01049         epr_set_err(e_err_invalid_raster, "epr_get_raster_elem_size: raster must not be NULL");
01050         return 0;
01051     }
01052     return raster->elem_size;
01053 }
01054 
01055 void* epr_get_raster_elem_addr(const EPR_SRaster* raster, epr_uint offset) {
01056     if (raster == NULL) {
01057         epr_set_err(e_err_invalid_raster, "epr_get_raster_elem_addr: raster must not be NULL");
01058         return 0;
01059     }
01060     return ((epr_uchar*) raster->buffer) + epr_get_raster_elem_size(raster) * offset;
01061 }
01062 
01063 void* epr_get_raster_pixel_addr(const EPR_SRaster* raster, epr_uint x, epr_uint y) {
01064     if (raster == NULL) {
01065         epr_set_err(e_err_invalid_raster, "epr_get_raster_pixel_addr: raster must not be NULL");
01066         return 0;
01067     }
01068     return epr_get_raster_elem_addr(raster, y * raster->raster_width + x);
01069 }
01070 
01071 void* epr_get_raster_line_addr(const EPR_SRaster* raster, epr_uint y) {
01072     if (raster == NULL) {
01073         epr_set_err(e_err_invalid_raster, "epr_get_raster_line_addr: raster must not be NULL");
01074         return 0;
01075     }
01076     return epr_get_raster_elem_addr(raster, y * raster->raster_width);
01077 }
01078 
01079 epr_uint epr_get_raster_width(EPR_SRaster* raster) {
01080     if (raster == NULL) {
01081         epr_set_err(e_err_invalid_raster, "epr_get_raster_width: raster must not be NULL");
01082         return 0;
01083     }
01084     return raster->raster_width;
01085 }
01086 
01087 epr_uint epr_get_raster_height(EPR_SRaster* raster) {
01088     if (raster == NULL) {
01089         epr_set_err(e_err_invalid_raster, "epr_get_raster_height: raster must not be NULL");
01090         return 0;
01091     }
01092     return raster->raster_height;
01093 }
01094 
01095 /******************************************************************/
01096 EPR_FLineDecoder select_line_decode_function(EPR_EDataTypeId band_tid,
01097         EPR_ESampleModel band_smod,
01098         EPR_EDataTypeId raw_tid) {
01099     EPR_FLineDecoder decode_func;
01100     if ((band_tid == e_tid_char || band_tid == e_tid_uchar)
01101             && band_smod == e_smod_1OF1
01102             && (raw_tid == e_tid_char || raw_tid == e_tid_uchar))
01103         decode_func = decode_line_uchar_1_of_1_to_uchar;
01104     else if ((band_tid == e_tid_char || band_tid == e_tid_uchar)
01105              && band_smod == e_smod_1OF2
01106              && (raw_tid == e_tid_char || raw_tid == e_tid_uchar))
01107         decode_func = decode_line_uchar_1_of_2_to_uchar;
01108     else if ((band_tid == e_tid_char || band_tid == e_tid_uchar)
01109              && band_smod == e_smod_2OF2
01110              && (raw_tid == e_tid_char || raw_tid == e_tid_uchar))
01111         decode_func = decode_line_uchar_2_of_2_to_uchar;
01112     else if ((band_tid == e_tid_short || band_tid == e_tid_ushort)
01113              && band_smod == e_smod_1OF1
01114              && (raw_tid == e_tid_short || raw_tid == e_tid_ushort))
01115         decode_func = decode_line_ushort_1_of_1_to_ushort;
01116     else if (band_tid == e_tid_float
01117              && band_smod == e_smod_1OF1
01118              && raw_tid == e_tid_uchar)
01119         decode_func = decode_line_uchar_1_of_1_to_float;
01120     else if (band_tid == e_tid_float
01121              && band_smod == e_smod_1OF1
01122              && raw_tid == e_tid_char)
01123         decode_func = decode_line_char_1_of_1_to_float;
01124     else if (band_tid == e_tid_float
01125              && band_smod == e_smod_1OF1
01126              && raw_tid == e_tid_ushort)
01127         decode_func = decode_line_ushort_1_of_1_to_float;
01128     else if (band_tid == e_tid_float
01129              && band_smod == e_smod_1OF1
01130              && raw_tid == e_tid_short)
01131         decode_func = decode_line_short_1_of_1_to_float;
01132     else if (band_tid == e_tid_float
01133              && band_smod == e_smod_1OF2
01134              && raw_tid == e_tid_short)
01135         decode_func = decode_line_short_1_of_2_to_float;
01136     else if (band_tid == e_tid_float
01137              && band_smod == e_smod_2OF2
01138              && raw_tid == e_tid_short)
01139         decode_func = decode_line_short_2_of_2_to_float;
01140     else if (band_tid == e_tid_float
01141              && band_smod == e_smod_1OF2
01142              && raw_tid == e_tid_uchar)
01143         decode_func = decode_line_uchar_1_of_2_to_float;
01144     else if (band_tid == e_tid_float
01145              && band_smod == e_smod_2OF2
01146              && raw_tid == e_tid_uchar)
01147         decode_func = decode_line_uchar_2_of_2_to_float;
01148     else if (band_tid == e_tid_float
01149              && band_smod == e_smod_2TOF
01150              && raw_tid == e_tid_uchar)
01151         decode_func = decode_line_uchar_2_to_f_to_float;
01152     else if (band_tid == e_tid_uint
01153              && band_smod == e_smod_3TOI
01154              && raw_tid == e_tid_uchar)
01155         decode_func = decode_line_uchar_3_to_i_to_uint;
01156     else {
01157         return NULL;
01158     }
01159     return decode_func;
01160 }
01161 
01162 
01163 EPR_FArrayTransformer select_transform_array_function(EPR_EDataTypeId band_tid,
01164         EPR_EDataTypeId raw_tid) {
01165     EPR_FArrayTransformer transform_array_func;
01166     if (band_tid == e_tid_float && raw_tid == e_tid_short)
01167         transform_array_func = transform_array_short_to_float;
01168     else if (band_tid == e_tid_float && raw_tid == e_tid_ushort)
01169         transform_array_func = transform_array_ushort_to_float;
01170     else if (band_tid == e_tid_float && raw_tid == e_tid_int)
01171         transform_array_func = transform_array_int_to_float;
01172     else if (band_tid == e_tid_float && raw_tid == e_tid_uint)
01173         transform_array_func = transform_array_uint_to_float;
01174     else {
01175         return NULL;
01176     }
01177     return transform_array_func;
01178 }
01179 
01180 
01181 void decode_line_uchar_1_of_1_to_float(void* source_array,
01182                                        EPR_SBandId* band_id,
01183                                        int offset_x,
01184                                        int raster_width,
01185                                        int step_x,
01186                                        void* raster_buffer,
01187                                        int raster_pos) {
01188     int x, x1, x2;
01189     epr_uchar* sa = (epr_uchar*) source_array;
01190     float* buf = (float*) raster_buffer;
01191 
01192     x1 = offset_x;
01193     x2 = x1 + raster_width - 1;
01194 
01195     if (band_id->scaling_method == e_smid_log) {
01196         for (x = x1; x <= x2; x += step_x)  {
01197             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[x]);
01198         }
01199     } else if (band_id->scaling_method == e_smid_lin) {
01200         for (x = x1; x <= x2; x += step_x)  {
01201             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[x];
01202         }
01203     } else {
01204         for (x = x1; x <= x2; x += step_x)  {
01205             buf[raster_pos++] = sa[x];
01206         }
01207     }
01208 }
01209 
01210 
01211 void decode_line_char_1_of_1_to_float(void* source_array,
01212                                       EPR_SBandId* band_id,
01213                                       int offset_x,
01214                                       int raster_width,
01215                                       int step_x,
01216                                       void* raster_buffer,
01217                                       int raster_pos) {
01218     int x, x1, x2;
01219     char* sa = (char*) source_array;
01220     float* buf = (float*) raster_buffer;
01221 
01222     x1 = offset_x;
01223     x2 = x1 + raster_width - 1;
01224 
01225     if (band_id->scaling_method == e_smid_log) {
01226         for (x = x1; x <= x2; x += step_x)  {
01227             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[x]);
01228         }
01229     } else if (band_id->scaling_method == e_smid_lin) {
01230         for (x = x1; x <= x2; x += step_x)  {
01231             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[x];
01232         }
01233     } else {
01234         for (x = x1; x <= x2; x += step_x) {
01235             buf[raster_pos++] = sa[x];
01236         }
01237     }
01238 }
01239 
01240 
01241 void decode_line_ushort_1_of_1_to_float(void* source_array,
01242                                         EPR_SBandId* band_id,
01243                                         int offset_x,
01244                                         int raster_width,
01245                                         int step_x,
01246                                         void* raster_buffer,
01247                                         int raster_pos) {
01248     int x, x1, x2;
01249     epr_ushort* sa = (epr_ushort*) source_array;
01250     float* buf = (float*) raster_buffer;
01251 
01252     x1 = offset_x;
01253     x2 = x1 + raster_width - 1;
01254 
01255     if (band_id->scaling_method == e_smid_log) {
01256         for (x = x1; x <= x2; x += step_x) {
01257             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[x]);
01258         }
01259     } else if (band_id->scaling_method == e_smid_lin) {
01260         for (x = x1; x <= x2; x += step_x) {
01261             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[x];
01262         }
01263     } else {
01264         for (x = x1; x <= x2; x += step_x) {
01265             buf[raster_pos++] = sa[x];
01266         }
01267     }
01268 }
01269 
01270 
01271 void decode_line_short_1_of_1_to_float(void* source_array,
01272                                        EPR_SBandId* band_id,
01273                                        int offset_x,
01274                                        int raster_width,
01275                                        int step_x,
01276                                        void* raster_buffer,
01277                                        int raster_pos) {
01278     int x, x1, x2;
01279     short* sa = (short*) source_array;
01280     float* buf = (float*) raster_buffer;
01281 
01282     x1 = offset_x;
01283     x2 = x1 + raster_width - 1;
01284 
01285     if (band_id->scaling_method == e_smid_log) {
01286         for (x = x1; x <= x2; x += step_x)  {
01287             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[x]);
01288         }
01289     } else if (band_id->scaling_method == e_smid_lin) {
01290         for (x = x1; x <= x2; x += step_x)  {
01291             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[x];
01292         }
01293     } else {
01294         for (x = x1; x <= x2; x += step_x)  {
01295             buf[raster_pos++] = sa[x];
01296         }
01297     }
01298 }
01299 
01300 void decode_line_short_1_of_2_to_float(void* source_array,
01301                                        EPR_SBandId* band_id,
01302                                        int offset_x,
01303                                        int raster_width,
01304                                        int step_x,
01305                                        void* raster_buffer,
01306                                        int raster_pos) {
01307     int x, x1, x2;
01308     short* sa = (short*) source_array;
01309     float* buf = (float*) raster_buffer;
01310 
01311     x1 = offset_x;
01312     x2 = x1 + raster_width - 1;
01313 
01314     if (band_id->scaling_method == e_smid_log) {
01315         for (x = x1; x <= x2; x += step_x)  {
01316             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[2 * x]);
01317         }
01318     } else if (band_id->scaling_method == e_smid_lin) {
01319         for (x = x1; x <= x2; x += step_x)  {
01320             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[2 * x];
01321         }
01322     } else {
01323         for (x = x1; x <= x2; x += step_x)  {
01324             buf[raster_pos++] = (float)(sa[2 * x]);
01325         }
01326     }
01327 }
01328 
01329 
01330 void decode_line_short_2_of_2_to_float(void* source_array,
01331                                        EPR_SBandId* band_id,
01332                                        int offset_x,
01333                                        int raster_width,
01334                                        int step_x,
01335                                        void* raster_buffer,
01336                                        int raster_pos) {
01337     int x, x1, x2;
01338     short* sa = (short*) source_array;
01339     float* buf = (float*) raster_buffer;
01340 
01341     x1 = offset_x;
01342     x2 = x1 + raster_width - 1;
01343 
01344     if (band_id->scaling_method == e_smid_log) {
01345         for (x = x1; x <= x2; x += step_x) {
01346             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * sa[2 * x + 1]);
01347         }
01348     } else if (band_id->scaling_method == e_smid_lin) {
01349         for (x = x1; x <= x2; x += step_x)  {
01350             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * sa[2 * x + 1];
01351         }
01352     } else {
01353         for (x = x1; x <= x2; x += step_x)  {
01354             buf[raster_pos++] = (float)(sa[2 * x + 1]);
01355         }
01356     }
01357 }
01358 
01359 
01360 void decode_line_uchar_1_of_1_to_uchar(void* source_array,
01361                                        EPR_SBandId* band_id,
01362                                        int offset_x,
01363                                        int raster_width,
01364                                        int step_x,
01365                                        void* raster_buffer,
01366                                        int raster_pos) {
01367     int x, x1, x2;
01368     epr_uchar* sa = (epr_uchar*) source_array;
01369     epr_uchar* buf = (epr_uchar*) raster_buffer;
01370 
01371     x1 = offset_x;
01372     x2 = x1 + raster_width - 1;
01373 
01374     for (x = x1; x <= x2; x += step_x)  {
01375         buf[raster_pos++] = sa[x];
01376     }
01377 }
01378 
01379 void decode_line_uchar_1_of_2_to_uchar(void* source_array,
01380                                        EPR_SBandId* band_id,
01381                                        int offset_x,
01382                                        int raster_width,
01383                                        int step_x,
01384                                        void* raster_buffer,
01385                                        int raster_pos) {
01386     int x, x1, x2;
01387     epr_uchar* sa = (epr_uchar*) source_array;
01388     epr_uchar* buf = (epr_uchar*) raster_buffer;
01389 
01390     x1 = offset_x;
01391     x2 = x1 + raster_width - 1;
01392 
01393     for (x = x1; x <= x2; x += step_x)  {
01394         buf[raster_pos++] = sa[2 * x];
01395     }
01396 }
01397 
01398 
01399 void decode_line_uchar_2_of_2_to_uchar(void* source_array,
01400                                        EPR_SBandId* band_id,
01401                                        int offset_x,
01402                                        int raster_width,
01403                                        int step_x,
01404                                        void* raster_buffer,
01405                                        int raster_pos) {
01406     int x, x1, x2;
01407     epr_uchar* sa = (epr_uchar*) source_array;
01408     epr_uchar* buf = (epr_uchar*) raster_buffer;
01409 
01410     x1 = offset_x;
01411     x2 = x1 + raster_width - 1;
01412 
01413     for (x = x1; x <= x2; x += step_x)  {
01414         buf[raster_pos++] = sa[2 * x + 1];
01415     }
01416 }
01417 
01418 void decode_line_ushort_1_of_1_to_ushort(void* source_array,
01419         EPR_SBandId* band_id,
01420         int offset_x,
01421         int raster_width,
01422         int step_x,
01423         void* raster_buffer,
01424         int raster_pos) {
01425     int x, x1, x2;
01426     epr_ushort* sa = (epr_ushort*) source_array;
01427     epr_ushort* buf = (epr_ushort*) raster_buffer;
01428 
01429     x1 = offset_x;
01430     x2 = x1 + raster_width - 1;
01431 
01432     for (x = x1; x <= x2; x += step_x)  {
01433         buf[raster_pos++] = sa[x];
01434     }
01435 }
01436 
01437 void decode_line_uchar_2_to_f_to_float(void* source_array,
01438                                        EPR_SBandId* band_id,
01439                                        int offset_x,
01440                                        int raster_width,
01441                                        int step_x,
01442                                        void* raster_buffer,
01443                                        int raster_pos) {
01444     int x, x1, x2, shi;
01445     epr_uchar* sa = (epr_uchar*) source_array;
01446     float* buf = (float*) raster_buffer;
01447 
01448     x1 = offset_x;
01449     x2 = x1 + raster_width - 1;
01450 
01451     if (band_id->scaling_method == e_smid_log) {
01452         for (x = x1; x <= x2; x += step_x)  {
01453             shi = (((sa[2 * x] & 0xff)) | ((sa[2 * x + 1] & 0xff) << 8)) & 0xffff;
01454             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * shi);
01455         }
01456     } else if (band_id->scaling_method == e_smid_lin) {
01457         for (x = x1; x <= x2; x += step_x)  {
01458             shi = (((sa[2 * x] & 0xff)) | ((sa[2 * x + 1] & 0xff) << 8)) & 0xffff;
01459             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * shi;
01460         }
01461     } else {
01462         for (x = x1; x <= x2; x += step_x)  {
01463             shi = (((sa[2 * x] & 0xff)) | ((sa[2 * x + 1] & 0xff) << 8)) & 0xffff;
01464             buf[raster_pos++] = (float)shi;
01465         }
01466     }
01467 }
01468 
01469 void decode_line_uchar_1_of_2_to_float(void* source_array,
01470                                        EPR_SBandId* band_id,
01471                                        int offset_x,
01472                                        int raster_width,
01473                                        int step_x,
01474                                        void* raster_buffer,
01475                                        int raster_pos) {
01476     int x, x1, x2;
01477     epr_uchar* sa = (epr_uchar*) source_array;
01478     float* buf = (float*) raster_buffer;
01479 
01480     x1 = offset_x;
01481     x2 = x1 + raster_width - 1;
01482 
01483     if (band_id->scaling_method == e_smid_log) {
01484         for (x = x1; x <= x2; x += step_x)  {
01485             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * (sa[2 * x] & 0xff));
01486         }
01487     } else if (band_id->scaling_method == e_smid_lin) {
01488         for (x = x1; x <= x2; x += step_x)  {
01489             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * (sa[2 * x] & 0xff);
01490         }
01491     } else {
01492         for (x = x1; x <= x2; x += step_x)  {
01493             buf[raster_pos++] = (float)(sa[2 * x] & 0xff);
01494         }
01495     }
01496 }
01497 
01498 
01499 
01500 void decode_line_uchar_2_of_2_to_float(void* source_array,
01501                                        EPR_SBandId* band_id,
01502                                        int offset_x,
01503                                        int raster_width,
01504                                        int step_x,
01505                                        void* raster_buffer,
01506                                        int raster_pos) {
01507     int x, x1, x2;
01508     epr_uchar* sa = (epr_uchar*) source_array;
01509     float* buf = (float*) raster_buffer;
01510 
01511     x1 = offset_x;
01512     x2 = x1 + raster_width - 1;
01513 
01514     if (band_id->scaling_method == e_smid_log) {
01515         for (x = x1; x <= x2; x += step_x) {
01516             buf[raster_pos++] = (float)pow(10, band_id->scaling_offset + band_id->scaling_factor * (sa[2 * x + 1] & 0xff));
01517         }
01518     } else if (band_id->scaling_method == e_smid_lin) {
01519         for (x = x1; x <= x2; x += step_x)  {
01520             buf[raster_pos++] = band_id->scaling_offset + band_id->scaling_factor * (sa[2 * x + 1] & 0xff);
01521         }
01522     } else {
01523         for (x = x1; x <= x2; x += step_x)  {
01524             buf[raster_pos++] = (float)(sa[2 * x + 1] & 0xff);
01525         }
01526     }
01527 }
01528 
01529 void decode_line_uchar_3_to_i_to_uint(void* source_array,
01530                                        EPR_SBandId* band_id,
01531                                        int offset_x,
01532                                        int raster_width,
01533                                        int step_x,
01534                                        void* raster_buffer,
01535                                        int raster_pos) {
01536     int x, x1, x2, n;
01537     epr_uchar* sa = (epr_uchar*) source_array;
01538     epr_uint* buf = (epr_uint*) raster_buffer;
01539 
01540     x1 = offset_x;
01541     x2 = x1 + raster_width - 1;
01542 
01543     for (x = x1; x <= x2; x += step_x)  {
01544         n = 3 * x;
01545         buf[raster_pos++] = (sa[n] & 0xff) << 16 | ((sa[n+1] & 0xff) << 8) | ((sa[n+2] & 0xff) );
01546     }
01547 }
01548 
01565 void decode_tiepoint_band(float* sa_beg,
01566                           float* sa_end,
01567                           epr_uint samples_per_tie_pt,
01568                           epr_uint num_elems,
01569                           EPR_SBandId* band_id,
01570                           int offset_x,
01571                           float scan_offset_x,
01572                           float y_mod,
01573                           int raster_width,
01574                           int s_x,
01575                           float* raster_buffer,
01576                           int raster_pos) {
01577 
01578     int ix;
01579     float x_mod;
01580     epr_uint x_knot;
01581     int inti_flag = 0;
01582     int intersection = 0;
01583     float circle;
01584     float half_circle;
01585     float null_point;
01586 
01587     circle = EPR_LONGI_ABS_MAX - EPR_LONGI_ABS_MIN;
01588     half_circle = 0.5F * circle;
01589     null_point = 0.5F * (EPR_LONGI_ABS_MAX + EPR_LONGI_ABS_MIN);
01590 
01591     if (strncmp(band_id->band_name, EPR_LONGI_BAND_NAME, strlen(EPR_LONGI_BAND_NAME)) == 0) {
01592         inti_flag = 1;
01593     } else {
01594         inti_flag = 0;
01595     }
01596 
01597     for (ix = offset_x; ix < offset_x + raster_width; ix += s_x) {
01598         x_mod = (ix - scan_offset_x) / samples_per_tie_pt;
01599         if (x_mod >= 0.0F) {
01600             x_knot = (epr_uint)floor(x_mod);
01601             if (x_knot >= num_elems - 1) {
01602                 x_knot = num_elems - 2;
01603             }
01604         } else {
01605             x_knot = (epr_uint)0;
01606         }
01607         x_mod -= x_knot;
01608 
01609         if (inti_flag == 1) {
01610             if (fabs((float)(sa_beg[x_knot + 1] - sa_beg[x_knot])) > half_circle ||
01611                     fabs((float)(sa_beg[x_knot] - sa_end[x_knot])) > half_circle ||
01612                     fabs((float)(sa_end[x_knot] - sa_end[x_knot + 1])) > half_circle ||
01613                     fabs((float)(sa_end[x_knot + 1] - sa_beg[x_knot + 1])) > half_circle) {
01614                 intersection = 1;
01615                 if (sa_beg[x_knot] < (float)null_point) {
01616                     sa_beg[x_knot] += circle;
01617                 }
01618                 if (sa_beg[x_knot + 1] < (float)null_point) {
01619                     sa_beg[x_knot + 1] += circle;
01620                 }
01621                 if (sa_end[x_knot] < (float)null_point) {
01622                     sa_end[x_knot] += circle;
01623                 }
01624                 if (sa_end[x_knot + 1] < (float)null_point) {
01625                     sa_end[x_knot + 1] += circle;
01626                 }
01627             }
01628         } else {
01629             intersection = 0;
01630         }
01631 
01632         raster_buffer[raster_pos] = epr_interpolate2D(x_mod, y_mod,
01633                                     sa_beg[x_knot], sa_beg[x_knot + 1],
01634                                     sa_end[x_knot], sa_end[x_knot + 1]);
01635 
01636         if (inti_flag == 1 &&
01637                 intersection == 1 &&
01638                 raster_buffer[raster_pos] > EPR_LONGI_ABS_MAX) {
01639             raster_buffer[raster_pos] -= circle;
01640         }
01641 
01642         raster_pos++;
01643     }
01644 }
01645 
01646 
01647 float epr_interpolate2D(float wi, float wj, float x00, float x10, float x01, float x11) {
01648     return x00 + wi * (x10 - x00) + wj * (x01 - x00) + wi * wj * (x11 + x00 - x01 - x10);
01649 }
01650 
01651 
01652 void transform_array_short_to_float (void* sourceArray,
01653                                      EPR_SBandId* band_id,
01654                                      float* raster_buffer,
01655                                      epr_uint nel) {
01656     epr_uint ix;
01657     short* sa = (short*) sourceArray;
01658 
01659     for (ix = 0; ix < nel; ix ++) {
01660         raster_buffer[ix] = band_id->scaling_offset + band_id->scaling_factor * sa[ix];
01661     }
01662 }
01663 
01664 void transform_array_ushort_to_float (void* sourceArray,
01665                                       EPR_SBandId* band_id,
01666                                       float* raster_buffer,
01667                                       epr_uint nel) {
01668     epr_uint ix;
01669     epr_ushort* sa = (epr_ushort*) sourceArray;
01670 
01671     for (ix = 0; ix < nel; ix ++) {
01672         raster_buffer[ix] = band_id->scaling_offset + band_id->scaling_factor * sa[ix];
01673     }
01674 }
01675 
01676 void transform_array_int_to_float (void* sourceArray,
01677                                     EPR_SBandId* band_id,
01678                                     float* raster_buffer,
01679                                     epr_uint nel) {
01680     epr_uint ix;
01681     int* sa = (int*) sourceArray;
01682 
01683     for (ix = 0; ix < nel; ix ++) {
01684         raster_buffer[ix] = band_id->scaling_offset + band_id->scaling_factor * sa[ix];
01685     }
01686 }
01687 
01688 void transform_array_uint_to_float (void* sourceArray,
01689                                      EPR_SBandId* band_id,
01690                                      float* raster_buffer,
01691                                      epr_uint nel) {
01692     epr_uint ix;
01693     epr_uint* sa = (epr_uint*) sourceArray;
01694 
01695     for (ix = 0; ix < nel; ix ++) {
01696         raster_buffer[ix] = band_id->scaling_offset + band_id->scaling_factor * sa[ix];
01697     }
01698 }
01699 
01700 void mirror_float_array(float* raster_buffer, epr_uint raster_width, epr_uint raster_height) {
01701     epr_uint w, h, pol_w, offset;
01702     float tmp;
01703     pol_w = raster_width / 2;
01704 
01705     for (h = 0; h < raster_height; h ++) {
01706         for (w = 0; w < pol_w; w ++) {
01707             offset = h * raster_width;
01708             tmp = raster_buffer[w + offset];
01709             raster_buffer[w + offset] = raster_buffer[raster_width - 1 - w + offset];
01710             raster_buffer[raster_width - 1 - w + offset] = tmp;
01711         }
01712     }
01713 }
01714 
01715 void mirror_uchar_array(epr_uchar* raster_buffer, epr_uint raster_width, epr_uint raster_height) {
01716     epr_uint w, h, pol_w, offset;
01717     epr_uchar tmp;
01718     pol_w = raster_width / 2;
01719 
01720     for (h = 0; h < raster_height; h ++) {
01721         for (w = 0; w < pol_w; w ++) {
01722             offset = h * raster_width;
01723             tmp = raster_buffer[w + offset];
01724             raster_buffer[w + offset] = raster_buffer[raster_width - 1 - w + offset];
01725             raster_buffer[raster_width - 1 - w + offset] = tmp;
01726         }
01727     }
01728 }
01729 
01730 void mirror_ushort_array(epr_ushort* raster_buffer, epr_uint raster_width, epr_uint raster_height) {
01731     epr_uint w, h, pol_w, offset;
01732     epr_ushort tmp;
01733     pol_w = raster_width / 2;
01734 
01735     for (h = 0; h < raster_height; h ++) {
01736         for (w = 0; w < pol_w; w ++) {
01737             offset = h * raster_width;
01738             tmp = raster_buffer[w + offset];
01739             raster_buffer[w + offset] = raster_buffer[raster_width - 1 - w + offset];
01740             raster_buffer[raster_width - 1 - w + offset] = tmp;
01741         }
01742     }
01743 }
01744 
01745 void mirror_uint_array(epr_uint* raster_buffer, epr_uint raster_width, epr_uint raster_height) {
01746     epr_uint w, h, pol_w, offset;
01747     epr_uint tmp;
01748     pol_w = raster_width / 2;
01749 
01750     for (h = 0; h < raster_height; h ++) {
01751         for (w = 0; w < pol_w; w ++) {
01752             offset = h * raster_width;
01753             tmp = raster_buffer[w + offset];
01754             raster_buffer[w + offset] = raster_buffer[raster_width - 1 - w + offset];
01755             raster_buffer[raster_width - 1 - w + offset] = tmp;
01756         }
01757     }
01758 }
01759 
01760 void epr_zero_invalid_pixels(EPR_SRaster* raster, EPR_SRaster* bm_raster) {
01761 
01762     epr_uchar* bm_pixels;
01763     epr_uint bm_pos = 0;
01764     epr_uint bm_len = raster->raster_width * raster->raster_height;
01765 
01766     assert(bm_raster->data_type == e_tid_char
01767            || bm_raster->data_type == e_tid_uchar);
01768 
01769 
01770     bm_pixels = (epr_uchar*) bm_raster->buffer;
01771     switch (raster->data_type) {
01772         case e_tid_char:
01773         case e_tid_uchar: {
01774             char* pixels = (char*) raster->buffer;
01775             for (bm_pos = 0; bm_pos < bm_len; bm_pos++) {
01776                 if (bm_pixels[bm_pos] == 0) {
01777                     pixels[bm_pos] = 0;
01778                 }
01779             }
01780         }
01781         break;
01782         case e_tid_short:
01783         case e_tid_ushort: {
01784             short* pixels = (short*) raster->buffer;
01785             for (bm_pos = 0; bm_pos < bm_len; bm_pos++) {
01786                 if (bm_pixels[bm_pos] == 0) {
01787                     pixels[bm_pos] = 0;
01788                 }
01789             }
01790         }
01791         break;
01792         case e_tid_int:
01793         case e_tid_uint: {
01794             int* pixels = (int*) raster->buffer;
01795             for (bm_pos = 0; bm_pos < bm_len; bm_pos++) {
01796                 if (bm_pixels[bm_pos] == 0) {
01797                     pixels[bm_pos] = 0;
01798                 }
01799             }
01800         }
01801         break;
01802         case e_tid_float: {
01803             float* pixels = (float*) raster->buffer;
01804             for (bm_pos = 0; bm_pos < bm_len; bm_pos++) {
01805                 if (bm_pixels[bm_pos] == 0) {
01806                     pixels[bm_pos] = 0.0F;
01807                 }
01808             }
01809         }
01810         break;
01811         case e_tid_double: {
01812             double* pixels = (double*) raster->buffer;
01813             for (bm_pos = 0; bm_pos < bm_len; bm_pos++) {
01814                 if (bm_pixels[bm_pos] == 0) {
01815                     pixels[bm_pos] = 0.0;
01816                 }
01817             }
01818         }
01819         break;
01820         default: {}
01821         break;
01822     }
01823 }