Panda3D

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