/* $Id: cpl_image_bpm.c,v 1.38 2006/11/22 14:53:16 yjung Exp $ * * This file is part of the ESO Common Pipeline Library * Copyright (C) 2001-2004 European Southern Observatory * * 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; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that 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 */ /* * $Author: yjung $ * $Date: 2006/11/22 14:53:16 $ * $Revision: 1.38 $ * $Name: $ */ #ifdef HAVE_CONFIG_H #include #endif /*----------------------------------------------------------------------------- Includes -----------------------------------------------------------------------------*/ #include #include #include #include #include #include "cpl_image_bpm.h" #include "cpl_mask.h" #include "cpl_tools.h" #include "cpl_image_defs.h" /*----------------------------------------------------------------------------*/ /** * @defgroup cpl_image_bpm Bad pixels map handling functions * * This module provides functions to handle bad pixels maps stored in images. * * The provided functions allow the user to set or reset the bad pixels in * an image, or to get various informations on those bad pixels. * * @par Synopsis: * @code * #include "cpl_image_bpm.h" * @endcode */ /*----------------------------------------------------------------------------*/ /**@{*/ /*----------------------------------------------------------------------------- Function codes -----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ /** @brief Test if a pixel is good or bad @param im the input image @param x the x pixel position in the image (first pixel is 1) @param y the y pixel position in the image (first pixel is 1) @return 1 if the pixel is bad, 0 if the pixel is good, negative on error. Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL - CPL_ERROR_ACCESS_OUT_OF_RANGE if the specified position is out of the image */ /*----------------------------------------------------------------------------*/ int cpl_image_is_rejected( const cpl_image * im, int x, int y) { cpl_binary val ; /* Test entries */ cpl_ensure(im != NULL, CPL_ERROR_NULL_INPUT, -1); cpl_ensure(x >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE, -2); cpl_ensure(y >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE, -3); cpl_ensure(x <= im->nx, CPL_ERROR_ACCESS_OUT_OF_RANGE, -4); cpl_ensure(y <= im->ny, CPL_ERROR_ACCESS_OUT_OF_RANGE, -5); /* Get the pixel info */ if (im->bpm != NULL) { val = cpl_mask_get(im->bpm, x, y) ; if (val == CPL_BINARY_0) return 0 ; else return 1 ; } return 0 ; } /*----------------------------------------------------------------------------*/ /** @brief Count the number of bad pixels declared in an image @param im the input image @return the number of bad pixels or -1 if the input image is NULL Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL */ /*----------------------------------------------------------------------------*/ int cpl_image_count_rejected(const cpl_image * im) { /* Test entries */ cpl_ensure(im!=NULL, CPL_ERROR_NULL_INPUT, -1) ; if (im->bpm != NULL) return cpl_mask_count(im->bpm) ; return 0 ; } /*----------------------------------------------------------------------------*/ /** @brief Set a pixel as bad in an image @param im the input image @param x the x pixel position in the image (first pixel is 1) @param y the y pixel position in the image (first pixel is 1) @return the #_cpl_error_code_ or CPL_ERROR_NONE Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL - CPL_ERROR_ACCESS_OUT_OF_RANGE if the specified position is out of the image */ /*----------------------------------------------------------------------------*/ cpl_error_code cpl_image_reject( cpl_image * im, int x, int y) { cpl_binary * pbpm ; /* Test entries */ cpl_ensure_code(im != NULL, CPL_ERROR_NULL_INPUT); cpl_ensure_code(x >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(y >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(x <= im->nx, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(y <= im->ny, CPL_ERROR_ACCESS_OUT_OF_RANGE); /* Create the bad pixels mask if empty */ if (im->bpm == NULL) im->bpm = cpl_mask_new(im->nx, im->ny) ; /* Get the access to the data */ pbpm = cpl_mask_get_data(im->bpm) ; /* Set the bad pixel */ pbpm[(x-1) + (y-1) * im->nx] = CPL_BINARY_1 ; /* Return */ return CPL_ERROR_NONE ; } /*----------------------------------------------------------------------------*/ /** @brief Set a pixel as good in an image @param im the input image @param x the x pixel position in the image (first pixel is 1) @param y the y pixel position in the image (first pixel is 1) @return the #_cpl_error_code_ or CPL_ERROR_NONE Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL - CPL_ERROR_ACCESS_OUT_OF_RANGE if the specified position is out of the image */ /*----------------------------------------------------------------------------*/ cpl_error_code cpl_image_accept( cpl_image * im, int x, int y) { cpl_binary * pbpm ; /* Test entries */ cpl_ensure_code(im != NULL, CPL_ERROR_NULL_INPUT); cpl_ensure_code(x >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(y >= 1, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(x <= im->nx, CPL_ERROR_ACCESS_OUT_OF_RANGE); cpl_ensure_code(y <= im->ny, CPL_ERROR_ACCESS_OUT_OF_RANGE); /* If no badpixel map in the image, return with ok */ if (im->bpm==NULL) return CPL_ERROR_NONE ; /* Get the access to the data */ pbpm = cpl_mask_get_data(im->bpm) ; /* Set the good pixel */ pbpm[(x-1) + (y-1) * im->nx] = CPL_BINARY_0 ; return CPL_ERROR_NONE; } /*----------------------------------------------------------------------------*/ /** @brief Set all pixels in the image as good @param im the input image @return the #_cpl_error_code_ or CPL_ERROR_NONE Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL */ /*----------------------------------------------------------------------------*/ cpl_error_code cpl_image_accept_all(cpl_image * im) { /* Test entries */ cpl_ensure_code(im !=NULL, CPL_ERROR_NULL_INPUT); if (im->bpm != NULL) { cpl_mask_delete(im->bpm) ; im->bpm = NULL ; } return CPL_ERROR_NONE ; } /*----------------------------------------------------------------------------*/ /** @brief Set the bad pixels in an image as defined in a mask @param im the input image @param map the mask defining the bad pixels @return the #_cpl_error_code_ or CPL_ERROR_NONE The bad pixel map contained in the image is overwritten/lost. Possible #_cpl_error_code_ set in this function: - CPL_ERROR_NULL_INPUT if the input image or the input map is NULL - CPL_ERROR_ILLEGAL_INPUT if the image and the map have different sizes */ /*----------------------------------------------------------------------------*/ cpl_error_code cpl_image_reject_from_mask( cpl_image * im, const cpl_mask * map) { /* Test entries */ cpl_ensure_code(im, CPL_ERROR_NULL_INPUT) ; cpl_ensure_code(map, CPL_ERROR_NULL_INPUT) ; cpl_ensure_code(im->nx==cpl_mask_get_size_x(map) && im->ny==cpl_mask_get_size_y(map), CPL_ERROR_ILLEGAL_INPUT); /* Remove the previous bad pixel map */ if (im->bpm != NULL) cpl_mask_delete(im->bpm) ; im->bpm = cpl_mask_duplicate(map) ; return CPL_ERROR_NONE ; } /**@}*/