Panda3D
pnm-image-filter-core.cxx
1 // Filename: pnm-image-filter-core.cxx
2 // Created by:
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 // We map X and Y to A and B, because we might change our minds about which
16 // is dominant, and we map get/set functions for the channel in question to
17 // GETVAL/SETVAL.
18 
19 
20 static void
21 FUNCTION_NAME(IMAGETYPE &dest, const IMAGETYPE &source,
22  float width, FilterFunction *make_filter, int channel) {
23  if (!dest.is_valid() || !source.is_valid()) {
24  return;
25  }
26 
27  // First, set up a 2-d column-major matrix of StoreTypes, big enough to hold
28  // the image xelvals scaled in the A direction only. This will hold the
29  // adjusted xel data from our first pass.
30 
31  typedef StoreType *StoreTypeP;
32  StoreType **matrix = (StoreType **)PANDA_MALLOC_ARRAY(dest.ASIZE() * sizeof(StoreType *));
33 
34  int a, b;
35 
36  for (a=0; a<dest.ASIZE(); a++) {
37  matrix[a] = (StoreType *)PANDA_MALLOC_ARRAY(source.BSIZE() * sizeof(StoreType));
38  }
39 
40  // First, scale the image in the A direction.
41  float scale;
42  StoreType *temp_source, *temp_dest;
43 
44  scale = (float)dest.ASIZE() / (float)source.ASIZE();
45  temp_source = (StoreType *)PANDA_MALLOC_ARRAY(source.ASIZE() * sizeof(StoreType));
46  temp_dest = (StoreType *)PANDA_MALLOC_ARRAY(dest.ASIZE() * sizeof(StoreType));
47 
48  WorkType *filter;
49  float filter_width;
50 
51  make_filter(scale, width, filter, filter_width);
52 
53  for (b = 0; b < source.BSIZE(); b++) {
54  for (a = 0; a < source.ASIZE(); a++) {
55  temp_source[a] = (StoreType)(source_max * source.GETVAL(a, b, channel));
56  }
57 
58  filter_row(temp_dest, dest.ASIZE(),
59  temp_source, source.ASIZE(),
60  scale,
61  filter, filter_width);
62 
63  for (a = 0; a < dest.ASIZE(); a++) {
64  matrix[a][b] = temp_dest[a];
65  }
66  }
67 
68  PANDA_FREE_ARRAY(temp_source);
69  PANDA_FREE_ARRAY(temp_dest);
70  PANDA_FREE_ARRAY(filter);
71 
72  // Now, scale the image in the B direction.
73  scale = (float)dest.BSIZE() / (float)source.BSIZE();
74  temp_dest = (StoreType *)PANDA_MALLOC_ARRAY(dest.BSIZE() * sizeof(StoreType));
75 
76  make_filter(scale, width, filter, filter_width);
77 
78  for (a = 0; a < dest.ASIZE(); a++) {
79  filter_row(temp_dest, dest.BSIZE(),
80  matrix[a], source.BSIZE(),
81  scale,
82  filter, filter_width);
83 
84  for (b = 0; b < dest.BSIZE(); b++) {
85  dest.SETVAL(a, b, channel, (float)temp_dest[b]/(float)source_max);
86  }
87  }
88 
89  PANDA_FREE_ARRAY(temp_dest);
90  PANDA_FREE_ARRAY(filter);
91 
92  // Now, clean up our temp matrix and go home!
93 
94  for (a = 0; a < dest.ASIZE(); a++) {
95  PANDA_FREE_ARRAY(matrix[a]);
96  }
97  PANDA_FREE_ARRAY(matrix);
98 }
99