⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epr_bitmask.c

📁 Insar图像处理软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * $Id: epr_bitmask.c,v 1.1.1.1 2003/03/05 17:36:43 hartmut Exp $ * * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation. This program is distributed in the hope it will * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#include <ctype.h>#include "epr_api.h"#include "epr_core.h"#include "epr_string.h"#include "epr_ptrarray.h"#include "epr_swap.h"#include "epr_field.h"#include "epr_record.h"#include "epr_param.h"#include "epr_dsd.h"#include "epr_msph.h"#include "epr_band.h"#include "epr_bitmask.h"#include "epr_dddb.h"void epr_resolve_bm_ref(EPR_SBmEvalContext* context, EPR_SBmTerm* term);EPR_SBmEvalContext* epr_create_bm_eval_context(EPR_SProductId* product_id,                                                int offset_x,                                                int offset_y,                                                EPR_SRaster* bitmask_raster){    EPR_SBmEvalContext* context;        context = (EPR_SBmEvalContext*) calloc(1, sizeof (EPR_SBmEvalContext));    if (context == NULL) {        epr_set_err(e_err_out_of_memory,                     "epr_create_bm_eval_context: out of memory");        return NULL;    }    context->product_id = product_id;    context->offset_x = offset_x;    context->offset_y = offset_y;    context->bitmask_raster = bitmask_raster;    context->flag_band_ids = epr_create_ptr_array(4);    context->flag_rasters = epr_create_ptr_array(4);    return context;}void epr_free_bm_eval_context(EPR_SBmEvalContext* context) {    EPR_SRaster* flag_raster;    uint flag_index;    if (context == NULL) {        return;    }        if (context->flag_band_ids != NULL) {        for (flag_index = 0; flag_index < context->flag_band_ids->length; flag_index++) {            /*              * Note that the release of band ID's is handled by the epr_close_product() function,             * The rasters need to be released, because they are internally used by this             * context.             */            flag_raster = (EPR_SRaster*)context->flag_rasters->elems[flag_index];            epr_free_raster(flag_raster);        }        epr_free_ptr_array(context->flag_band_ids);        epr_free_ptr_array(context->flag_rasters);    }    free(context);}/** * Reads bit-mask pixels of the given product for the given bit-mask expression  * for the given region offset and raster. * * @param product_id the product ID * @param bm_expr the bit-mask expression * @param offset_x X-coordinate in pixel co-ordinates (zero-based) of the upper right raster corner to be searched for * @param offset_y Y-coordinate in pixel co-ordinates (zero-based) of the upper right raster corner to be searched for * @param raster the instance to the buffer information was used * * @return zero for success, an error code otherwise */int epr_read_bitmask_raster(EPR_SProductId* product_id,                             const char* bm_expr,                             int offset_x,                             int offset_y,                             EPR_SRaster* raster){    EPR_SBmEvalContext* context;    EPR_SBmTerm* term;    int x, y, x1, x2, y1, y2, sx, sy;    uint pos;    uchar* bm_buffer = NULL;    EPR_EErrCode errcode;    epr_clear_err();    if (raster->data_type != e_tid_uchar && raster->data_type != e_tid_char) {        epr_set_err(e_err_illegal_data_type,             "epr_read_bitmask_raster: illegal raster datatype; must be 'char' or 'uchar'");        return e_err_illegal_data_type;     }    bm_buffer = (uchar*) raster->buffer;    if (bm_buffer == NULL) {         epr_set_err(e_err_out_of_memory,              "epr_read_bitmask_raster: false memory allocation for a raster buffer");        return e_err_out_of_memory;     }    context = epr_create_bm_eval_context(product_id, offset_x, offset_y, raster);    if (context == NULL) {         epr_set_err(e_err_illegal_arg,              "epr_read_bitmask_raster: the context cannot be created");        return e_err_illegal_arg;     }    term = epr_parse_bm_expr_str(bm_expr);    if (term == NULL) {         epr_set_err(e_err_illegal_arg,              "epr_read_bitmask_raster: the term was not build");        return e_err_illegal_arg;     }    x1 = offset_x;    y1 = offset_y;    x2 = x1 + raster->source_width - 1;    y2 = y1 + raster->source_height - 1;    sx = raster->source_step_x;    sy = raster->source_step_y;    pos = 0;    epr_clear_err();    errcode = epr_get_last_err_code();    for (y = y1; y <= y2; y += sy) {        for (x = x1; x <= x2; x += sx) {            bm_buffer[pos] = (uchar) epr_eval_bm_term(context, term, x, y);            pos++;            errcode = epr_get_last_err_code();            if (errcode != 0) {                break;            }        }        if (errcode != 0) {            break;        }    }    epr_free_bm_term(term);    epr_free_bm_eval_context(context);    return errcode;}/** * Evaluates the given bitmask expression. * * @param term the bitmask term * @param x the pixel's x co-ordinate * @param y the pixel's y co-ordinate */boolean epr_eval_bm_term(EPR_SBmEvalContext* context, EPR_SBmTerm* term, int x, int y) {        if (term == NULL) {        return FALSE;    }    switch (term->op_code) {    case BMT_REF:		{			EPR_SRaster* flag_raster = term->op.ref.flag_raster;            ulong flag_mask = term->op.ref.flag_mask;            if (flag_raster == NULL) {                epr_resolve_bm_ref(context, term);                flag_raster = term->op.ref.flag_raster;                flag_mask = term->op.ref.flag_mask;                if (flag_raster == NULL) {                    return FALSE;                }            }            assert(flag_raster != NULL);            assert(flag_mask != FLAG_MASK_NOT_COMPUTED);            return (epr_get_pixel_as_ulong(flag_raster, x, y) & flag_mask) != 0;        }    case BMT_AND:		{			if (!epr_eval_bm_term(context, term->op.binary.arg1, x, y)) {                return FALSE;			}            return epr_eval_bm_term(context, term->op.binary.arg2, x, y);		}    case BMT_OR:		{            if (epr_eval_bm_term(context, term->op.binary.arg1, x, y)) {                return TRUE;			}            return epr_eval_bm_term(context, term->op.binary.arg2, x, y);        }    case BMT_NOT:		{            return !epr_eval_bm_term(context, term->op.unary.arg, x, y);        }    default:        assert(0);        return FALSE;    }}void epr_resolve_bm_ref(EPR_SBmEvalContext* context, EPR_SBmTerm* term) {    const char* band_name = term->op.ref.band_name;    const char* flag_name = term->op.ref.flag_name;    EPR_SBandId* flag_band_id = NULL;    uint flag_band_index = (uint) -1;    uint band_index = 0;    uint num_bands = context->flag_band_ids->length;    EPR_SRaster* flag_raster = NULL;    ulong flag_mask = FLAG_MASK_NOT_COMPUTED;    /* Find the corresponding flag_band_id for band_name */    for (band_index = 0; band_index < num_bands; band_index++) {        flag_band_id = (EPR_SBandId*) context->flag_band_ids->elems[band_index];        if (epr_equal_names(band_name, flag_band_id->band_name)) {            flag_band_index = band_index;            break;        }    }    /* flag_band_id found? */    if (flag_band_index != (uint) -1) {        /* Yes, found: get flag_band_id  and the corresponding raster */        flag_band_id = (EPR_SBandId*)(context->flag_band_ids->elems[flag_band_index]);         flag_raster = (EPR_SRaster*)(context->flag_rasters->elems[flag_band_index]);     } else {        /* Not found: get flag_band_id from product and load the corresponding raster */        flag_band_id = epr_get_band_id(context->product_id, band_name);        if (flag_band_id != NULL) {            flag_raster = epr_create_compatible_raster(flag_band_id,                                             context->bitmask_raster->source_width,                                             context->bitmask_raster->source_height,                                             context->bitmask_raster->source_step_x,                                             context->bitmask_raster->source_step_y);            epr_read_band_raster(flag_band_id,                                  context->offset_x,                                  context->offset_y,                                  flag_raster);            /* register flag_band_id and flag_raster for later use */            epr_add_ptr_array_elem(context->flag_band_ids, flag_band_id);            epr_add_ptr_array_elem(context->flag_rasters, flag_raster);        } else {            epr_set_err(e_err_flag_not_found, "flags band not found");            return;        }    }        /* Now, compute flag_mask */    /* Does the band have a flag coding? */    if (flag_band_id->flag_coding != NULL) {        /* Yes, now find flag definition for flag_name */        EPR_SFlagDef* flag_def = NULL;        uint flag_def_index;        for (flag_def_index = 0; flag_def_index < flag_band_id->flag_coding->length; flag_def_index++) {            flag_def = (EPR_SFlagDef*) flag_band_id->flag_coding->elems[flag_def_index];            if (epr_equal_names(flag_def->name, flag_name)) {                int bit_index = flag_def->bit_index;                flag_mask = 1 << bit_index;                break;            }        }    }    if (flag_mask == FLAG_MASK_NOT_COMPUTED) {        epr_set_err(e_err_flag_not_found, "flag not found");    }        term->op.ref.flag_mask = flag_mask;    term->op.ref.flag_raster = flag_raster;}/** * Parses the bit-mask expression given as character string. * * @param bm_expr the bit-mask expression given as character string * @return the bit-mask term tree representing the bit-mask expression * @throws BitmaskExpressionParseException if the given code could not be epr_parse'd * @throws IOException if an I/O error occurs */EPR_SBmTerm* epr_parse_bm_expr_str(const char* bm_expr) {        EPR_SBmTerm* term;    EPR_SParseInfo parse_info;        parse_info.bm_expr = bm_expr;    parse_info.bm_expr_pos = 0;    parse_info.pushed_back = FALSE;    parse_info.token = NULL;    parse_info.err_message = NULL;    term = epr_parse_bm_expr(&parse_info, FALSE);        epr_free_string(parse_info.token);    parse_info.token = NULL;    if (epr_is_bm_expr_error(&parse_info)) {        char tmp[256] = {"bitmap-expression error: "};        strcat(tmp, parse_info.err_message);        epr_set_err(e_err_invalid_value, tmp);    }    return term;}EPR_SPtrArray* epr_create_flag_coding(EPR_SProductId* product_id, const char* flag_coding_name){	int num_descr;    int i;    const struct FlagDescriptorTable* fc_tables;    int fct_index;    EPR_SPtrArray* flag_coding = NULL;	if (product_id == NULL) {            epr_set_err(e_err_null_pointer,                     "epr_create_flag_coding: product_id must not be NULL");        return NULL;    }    /* @DDDB */    fc_tables = dddb_flag_coding_tables;    fct_index = -1;    for (i = 0; i < EPR_NUM_FLAG_CODING_TABLES; i++) {         const char* id = fc_tables[i].name;        if (epr_equal_names(id, flag_coding_name)) {            fct_index = i;            break;        }    }    if (fct_index == -1) {        epr_set_err(e_err_null_pointer,                     "epr_create_flag_coding: unknown flag coding");        return NULL;    }    flag_coding = epr_create_ptr_array(16);    num_descr = fc_tables[fct_index].num_descriptors;	for (i = 0; i < num_descr; i++) {		EPR_SFlagDef* flag_def = (EPR_SFlagDef*) calloc(1, sizeof (EPR_SFlagDef));		if (flag_def == NULL) {			epr_set_err(e_err_out_of_memory, 						"epr_create_flag_coding: out of memory");			return NULL;		}        /* 1: flag_name */        epr_assign_string(&flag_def->name, fc_tables[fct_index].descriptors[i].id);        if (flag_def->name == NULL) {            epr_set_err(e_err_out_of_memory, "epr_get_flag_coding: out of memory");            epr_free_flag_def(flag_def);            return NULL;        }        /* 2: dataset_name */        flag_def->bit_index = (uint)fc_tables[fct_index].descriptors[i].bit_index;         /* 3: sample_offset */        epr_assign_string(&flag_def->description, fc_tables[fct_index].descriptors[i].description);		epr_add_ptr_array_elem(flag_coding, flag_def);	}    return flag_coding;}void epr_free_flag_coding(EPR_SPtrArray* flag_coding){    EPR_SFlagDef* flag_def = NULL;    uint flag_index;    if (flag_coding == NULL) {        return;    }        for (flag_index = 0; flag_index < flag_coding->length; flag_index++)    {        flag_def = (EPR_SFlagDef*) flag_coding->elems[flag_index];        epr_free_flag_def(flag_def);        flag_def = NULL;    }    epr_free_ptr_array(flag_coding);}EPR_SFlagDef* epr_create_flag_def(){    EPR_SFlagDef* flag_def = NULL;    flag_def = (EPR_SFlagDef*) calloc(1, sizeof (EPR_SFlagDef));    if (flag_def == NULL) {        epr_set_err(e_err_out_of_memory,                     "epr_create_flag_def: out of memory");        return NULL;    }    flag_def->magic = EPR_MAGIC_FLAG_DEF;    return flag_def;}void epr_free_flag_def(EPR_SFlagDef* flag_def){    if (flag_def == NULL)         return;    epr_free_string(flag_def->name);    flag_def->name = NULL;    flag_def->bit_index = 0;    epr_free_string(flag_def->description);    flag_def->description = NULL;    free(flag_def);}EPR_SBmTerm* epr_parse_bm_expr(EPR_SParseInfo* parse_info, boolean term_required) {    return epr_parse_bm_OR_expr(parse_info, term_required);}EPR_SBmTerm* epr_parse_bm_OR_expr(EPR_SParseInfo* parse_info, boolean term_required) {    EPR_SBmTerm* term1 = epr_parse_bm_AND_expr(parse_info, term_required);    if (term1 == NULL) {        return NULL;    }    while (!epr_is_bm_expr_error(parse_info)) {        epr_next_bm_expr_token(parse_info);        if (epr_is_bm_OR_keyword(parse_info) || epr_is_bm_OR_operator(parse_info)) {            EPR_SBmTerm* term2 = epr_parse_bm_OR_expr(parse_info, TRUE);            term1 = epr_create_bm_OR_term(term1, term2);        } else {            epr_push_back_bm_expr_token(parse_info);            break;        }    }    return term1;}EPR_SBmTerm* epr_parse_bm_AND_expr(EPR_SParseInfo* parse_info, boolean term_required) {    EPR_SBmTerm* term1 = epr_parse_bm_unary_expr(parse_info, term_required);    if (term1 == NULL) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -