MISR Toolkit  1.5.1
MtkResampleNearestNeighbor.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkResampleNearestNeighbor =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrReProject.h"
18 #include "MisrError.h"
19 #include "MisrUtil.h"
20 #include <string.h>
21 #include <math.h>
22 
39  MTKt_DataBuffer srcbuf,
40  MTKt_DataBuffer linebuf,
41  MTKt_DataBuffer samplebuf,
42  MTKt_DataBuffer *resampbuf )
43 {
44  MTKt_status status; /* Return status */
45  MTKt_status status_code; /* Return code of this function */
47  /* Resampled data buffer structure */
48  int lsrc; /* Source line index */
49  int ssrc; /* Source sample index */
50  int srcoff; /* Data pointer offset into src buffer */
51  int srcnum; /* Number of elements in src buffer */
52  int ldest; /* Destination line index */
53  int sdest; /* Destination sample index */
54  int destoff; /* Data pointer offset into dest buffer */
55  int destnum; /* Number of elements in dest buffer */
56 
57  /* Check line and sample buffer sizes */
58  if (linebuf.nline != samplebuf.nline)
60  if (linebuf.nsample != samplebuf.nsample)
62 
63  /* Check line and sample buffer datatypes */
64  if (linebuf.datatype != MTKe_float)
66  if (samplebuf.datatype != MTKe_float)
68 
69  /* --------------------------------------------------- */
70  /* Allocate buffers of the latitude and longitude data */
71  /* --------------------------------------------------- */
72 
73  status = MtkDataBufferAllocate(linebuf.nline, linebuf.nsample,
74  srcbuf.datatype, &destbuf);
75  MTK_ERR_COND_JUMP(status);
76 
77  /* -------------------------------------------------- */
78  /* Resample source data buffer using nearest neighbor */
79  /* -------------------------------------------------- */
80 
81  srcnum = srcbuf.nline * srcbuf.nsample;
82  destnum = destbuf.nline * destbuf.nsample;
83 
84  for (ldest=0; ldest < destbuf.nline; ldest++) {
85  for (sdest=0; sdest < destbuf.nsample; sdest++) {
86  lsrc = (int)floorf(linebuf.data.f[ldest][sdest] + 0.5);
87  ssrc = (int)floorf(samplebuf.data.f[ldest][sdest] + 0.5);
88  srcoff = lsrc * srcbuf.nsample + ssrc;
89  destoff = ldest * destbuf.nsample + sdest;
90  if (destoff >= 0 && destoff < destnum && srcoff >= 0 && srcoff < srcnum) {
91  memcpy((void *)((char *)destbuf.dataptr + (destoff * destbuf.datasize)),
92  (void *)((char *)srcbuf.dataptr + (srcoff * srcbuf.datasize)),
93  destbuf.datasize);
94  }
95  }
96  }
97 
98  *resampbuf = destbuf;
99 
100  return MTK_SUCCESS;
101 ERROR_HANDLE:
102  MtkDataBufferFree(&destbuf);
103  return status_code;
104 }
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 MtkResampleNearestNeighbor(MTKt_DataBuffer srcbuf, MTKt_DataBuffer linebuf, MTKt_DataBuffer samplebuf, MTKt_DataBuffer *resampbuf)
Perform nearest neighbor resampling.
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_DataType datatype
Definition: MisrUtil.h:102
#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