HAWKI Pipeline Reference Manual  1.8.12
hawki_image.c
1 /* $Id: hawki_image.c,v 1.3 2012/12/04 09:17:04 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2012/12/04 09:17:04 $
24  * $Revision: 1.3 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <math.h>
37 #include <string.h>
38 #include <cpl.h>
39 #include <cpl_mask.h>
40 #include <cpl_matrix.h>
41 
42 #include "hawki_mask.h"
43 
44 /*----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------*/
50 
68 (cpl_image * target,
69  const cpl_image * from,
70  cpl_size target_shift_x,
71  cpl_size target_shift_y)
72 {
73 
74  cpl_size inter_x1;
75  cpl_size inter_x2;
76  cpl_size inter_y1;
77  cpl_size inter_y2;
78 
79  cpl_size from_x1 = 0;
80  cpl_size from_x2 = cpl_image_get_size_x(from);
81  cpl_size from_y1 = 0;
82  cpl_size from_y2 = cpl_image_get_size_y(from);
83 
84  cpl_size target_x1 = target_shift_x;
85  cpl_size target_x2 = target_shift_x + cpl_image_get_size_x(target);
86  cpl_size target_y1 = target_shift_y;
87  cpl_size target_y2 = target_shift_y + cpl_image_get_size_y(target);
88 
89  /* Check entries */
90  cpl_ensure_code(target != NULL, CPL_ERROR_NULL_INPUT);
91  cpl_ensure_code(from != NULL, CPL_ERROR_NULL_INPUT);
92  cpl_ensure_code(cpl_image_get_type(target) == cpl_image_get_type(from),
93  CPL_ERROR_TYPE_MISMATCH);
94 
95  /* Compute intersection */
96  inter_x1 = CX_MAX(from_x1, target_x1);
97  inter_x2 = CX_MIN(from_x2, target_x2);
98  inter_y1 = CX_MAX(from_y1, target_y1);
99  inter_y2 = CX_MIN(from_y2, target_y2);
100 
101  if(inter_x2 > inter_x1 && inter_y2 > inter_y1)
102  {
103  const void * from_data;
104  void * target_data;
105  int iy;
106  size_t pixel_size = cpl_type_get_sizeof(cpl_image_get_type(from));
107  cpl_size from_nx = cpl_image_get_size_x(from);
108  cpl_size target_nx = cpl_image_get_size_y(target);
109  cpl_size memcopy_size;
110 
111  memcopy_size = (inter_x2 - inter_x1) * pixel_size;
112 
113  from_data = cpl_image_get_data_const(from);
114  target_data = cpl_image_get_data(target);
115 
116  for(iy=inter_y1; iy<inter_y2; ++iy)
117  {
118  const void * from_p;
119  void * target_p;
120 
121  from_p = from_data + (inter_x1 + iy * from_nx) * pixel_size;
122  target_p = target_data +
123  (inter_x1 - target_shift_x +
124  (iy - target_shift_y) * target_nx) * pixel_size;
125  memcpy(target_p, from_p, memcopy_size);
126  }
127  }
128  /* If the if is not executed is because intersection is empty.
129  * Nothing to copy */
130 
131  return CPL_ERROR_NONE;
132 }
133