MISR Toolkit  1.5.1
MtkDownsample.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkDownsample =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2008, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrRegression.h"
18 #include "MisrUtil.h"
19 #include <stdlib.h>
20 #include <math.h>
21 
37  const MTKt_DataBuffer *Source,
38  const MTKt_DataBuffer *Source_mask,
39  int Size_factor,
40  MTKt_DataBuffer *Result,
41  MTKt_DataBuffer *Result_mask
42 )
43 {
44  MTKt_status status_code; /* Return status of this function */
45  MTKt_status status; /* Return status of called routines. */
47  /* Downsampled result. */
48  MTKt_DataBuffer result_mask_tmp = MTKT_DATABUFFER_INIT;
49  /* Valid mask for result. */
50  int number_line_out; /* Number of lines in output grid.*/
51  int number_sample_out; /* Number of samples in output grid.*/
52  int iline; /* Loop iterator. */
53  int isample; /* Loop iterator. */
54 
55  /* ------------------------------------------------------------------ */
56  /* Argument check: Source == NULL */
57  /* Source->nline < 1 */
58  /* Source->nsample < 1 */
59  /* Source->datatype = MTKe_float */
60  /* ------------------------------------------------------------------ */
61 
62  if (Source == NULL) {
63  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Source == NULL");
64  }
65  if (Source->nline < 1) {
66  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nline < 1");
67  }
68  if (Source->nsample < 1) {
69  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nsample < 1");
70  }
71  if (Source->datatype != MTKe_float) {
72  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->datatype != MTKe_float");
73  }
74 
75  /* ------------------------------------------------------------------ */
76  /* Argument check: Source_mask == NULL */
77  /* Source_mask->nline != Source->nline */
78  /* Source_mask->nsample != Source->nsample */
79  /* Source_mask->datatype = MTKe_uint8 */
80  /* ------------------------------------------------------------------ */
81 
82  if (Source_mask == NULL) {
83  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Source_mask == NULL");
84  }
85  if (Source_mask->nline != Source->nline) {
86  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->nline != Source->nline");
87  }
88  if (Source_mask->nsample != Source->nsample) {
89  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->nsample != Source->nsample");
90  }
91  if (Source_mask->datatype != MTKe_uint8) {
92  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->datatype != MTKe_uint8");
93  }
94 
95  /* ------------------------------------------------------------------ */
96  /* Argument check: */
97  /* Size_factor < 1 */
98  /* Source->nline % Size_factor != 0 */
99  /* Source->nsample % Size_factor != 0 */
100  /* ------------------------------------------------------------------ */
101 
102  if (Size_factor < 1) {
103  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Size_factor < 1");
104  }
105  if (Source->nline % Size_factor != 0) {
106  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nline % Size_factor != 0");
107  }
108  if (Source->nsample % Size_factor != 0) {
109  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nsample % Size_factor != 0");
110  }
111 
112  /* ------------------------------------------------------------------ */
113  /* Argument check: Result == NULL */
114  /* ------------------------------------------------------------------ */
115 
116  if (Result == NULL) {
117  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Result == NULL");
118  }
119 
120  /* ------------------------------------------------------------------ */
121  /* Argument check: Result_mask == NULL */
122  /* ------------------------------------------------------------------ */
123 
124  if (Result_mask == NULL) {
125  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Result_mask == NULL");
126  }
127 
128  /* ------------------------------------------------------------------ */
129  /* Determine size of output array. */
130  /* ------------------------------------------------------------------ */
131 
132  number_line_out = Source->nline / Size_factor;
133  number_sample_out = Source->nsample / Size_factor;
134 
135  /* ------------------------------------------------------------------ */
136  /* Allocate memory for result */
137  /* ------------------------------------------------------------------ */
138 
139  status = MtkDataBufferAllocate(number_line_out, number_sample_out,
140  MTKe_float, &result_tmp);
141  MTK_ERR_COND_JUMP(status);
142 
143  status = MtkDataBufferAllocate(number_line_out, number_sample_out,
144  MTKe_uint8, &result_mask_tmp);
145  MTK_ERR_COND_JUMP(status);
146 
147  /* ------------------------------------------------------------------ */
148  /* For each location in result grid... */
149  /* ------------------------------------------------------------------ */
150 
151  for (iline = 0; iline < number_line_out ; iline++) {
152  for (isample = 0; isample < number_sample_out; isample++) {
153  int rline_start, rsample_start;
154  int rline_end, rsample_end;
155  int rline, rsample;
156  int count;
157  double sum = 0;
158  rline_start = iline * Size_factor;
159  rsample_start = isample * Size_factor;
160  rline_end = rline_start + Size_factor;
161  rsample_end = rsample_start + Size_factor;
162  count = 0;
163 
164  /* ------------------------------------------------------------------ */
165  /* Calculate average of source pixels at this location. */
166  /* ------------------------------------------------------------------ */
167 
168  for (rline = rline_start ; rline < rline_end ; rline++) {
169  for (rsample = rsample_start ; rsample < rsample_end ; rsample++) {
170  if (Source_mask->data.u8[rline][rsample]) {
171  sum += Source->data.f[rline][rsample];
172  count++;
173  }
174  }
175  }
176 
177  if (count > 0) {
178  result_tmp.data.f[iline][isample] = sum / count;
179  result_mask_tmp.data.u8[iline][isample] = 1;
180  } else {
181  result_mask_tmp.data.u8[iline][isample] = 0;
182  }
183 
184  /* ------------------------------------------------------------------ */
185  /* End loop for each location in result grid. */
186  /* ------------------------------------------------------------------ */
187 
188  }
189  }
190 
191  /* ------------------------------------------------------------------ */
192  /* Return. */
193  /* ------------------------------------------------------------------ */
194 
195  *Result = result_tmp;
196  *Result_mask = result_mask_tmp;
197  return MTK_SUCCESS;
198 
199 ERROR_HANDLE:
200  MtkDataBufferFree(&result_tmp);
201  MtkDataBufferFree(&result_mask_tmp);
202  return status_code;
203 }
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
MTKt_DataBufferType data
Definition: MisrUtil.h:104
MTKt_status MtkDownsample(const MTKt_DataBuffer *Source, const MTKt_DataBuffer *Source_mask, int Size_factor, MTKt_DataBuffer *Result, MTKt_DataBuffer *Result_mask)
Downsample data by averaging pixels.
Definition: MtkDownsample.c:36
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_DataType datatype
Definition: MisrUtil.h:102
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
#define MTK_ERR_CODE_MSG_JUMP(code, msg)
Definition: MisrError.h:181
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51