comparison Meerwald-dir/dwt_util.c @ 24:9f20bce6184e v0.7

move directories, support netpbm 11
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Fri, 20 Dec 2024 13:08:59 +0100
parents Meerwald/dwt_util.c@be303a3f5ea8
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "dwt_util.h"
3 #include <ctype.h>
4
5 void copy_coeffs_from_dwt(double ** block_coeffs, double ** dwt_coeffs,
6 int level, int band, int width, int height) {
7 int i, j;
8 int size = width >> level;
9 int h = (band > 2) ? size : 0;
10 int w = (band & 1) ? 0 : size;
11
12 for (i = 0; i < size; i++)
13 for (j = 0; j < size; j++)
14 block_coeffs[i][j] = dwt_coeffs[h + i][w + j];
15 }
16
17 void copy_coeffs_to_dwt(double ** dwt_coeffs, double ** block_coeffs,
18 int level, int band, int width, int height) {
19 int i, j;
20 int size = width >> level;
21 int h = (band > 2) ? size : 0;
22 int w = (band & 1) ? 0 : size;
23
24 for (i = 0; i < size; i++)
25 for (j = 0; j < size; j++)
26 dwt_coeffs[h + i][w + j] = block_coeffs[i][j];
27 }
28
29 char *subband_name(int type) {
30 switch (type) {
31 case LL: return "LL";
32 case HL: return "HL";
33 case LH: return "LH";
34 case HH: return "HH";
35 default: return "XX";
36 }
37 }
38
39 int subband_in_list(char *list, int type, int level) {
40 return 1;
41 }
42
43 int subband_wp_in_list(char *list, char *name) {
44 return 1;
45 }
46
47 int calc_subband_wp_level(char *name){
48 return strlen(name);
49 }
50
51 void calc_subband_location(int cols, int rows, int type, int level, int *col, int *row) {
52 *col = *row = 0;
53
54 if (level <= 0 || level > find_deepest_level(cols, rows) - 1) return;
55
56 switch (type) {
57 case LL:
58 break;
59 case HL:
60 *col = 0;
61 *row = rows >> level;
62 break;
63 case LH:
64 *col = cols >> level;
65 *row = 0;
66 break;
67 case HH:
68 *col = cols >> level;
69 *row = rows >> level;
70 break;
71 default:
72 break;
73 }
74 }
75
76 void calc_subband_wp_location(int cols, int rows, char *name, int *col, int *row) {
77 char *p = name;
78 int level = 0;
79 *col = *row = 0;
80
81 while (*p) {
82 level++;
83 switch (toupper(*p)) {
84 case 'A':
85 break;
86 case 'H':
87 *col += (cols >> level);
88 break;
89 case 'V':
90 *row += (rows >> level);
91 break;
92 case 'D':
93 *col += (cols >> level);
94 *row += (rows >> level);
95 break;
96 default:
97 break;
98 }
99 p++;
100 }
101 }
102
103 Pixel *get_dwt_data(Image_tree dwt, int level, int type) {
104 return get_dwt_image(dwt, level, type)->data;
105 }
106
107 Image get_dwt_image(Image_tree dwt, int level, int type) {
108 return get_dwt_subband(dwt, level, type)->image;
109 }
110
111 Image_tree get_dwt_subband(Image_tree dwt, int level, int type) {
112 while (--level)
113 dwt = dwt->coarse;
114
115 switch (type) {
116 case LL:
117 return dwt->coarse;
118 case HL:
119 return dwt->vertical;
120 case LH:
121 return dwt->horizontal;
122 case HH:
123 return dwt->diagonal;
124 }
125
126 return NULL;
127 }
128
129 Pixel get_dwt_coeff(Image_tree dwt, int level, int type, int coeff) {
130 return get_dwt_data(dwt, level, type)[coeff];
131 }
132
133 Pixel get_dwt_location(Image_tree dwt, int level, int type, int col, int row) {
134 return get_pixel(get_dwt_image(dwt, level, type), col, row);
135 }
136
137 static void calc__subband(Image_tree p, Image_tree q, double *min, double *max, double *error) {
138 int i;
139
140 if (!p || !q) return;
141
142 *error = 0;
143 *min = *max = fabs(p->image->data[0] - q->image->data[0]);
144 for (i = 0; i < p->image->size; i++) {
145 double diff = fabs(p->image->data[i] - q->image->data[i]);
146
147 *error += sqr(diff);
148 if (diff < *min) *min = diff;
149 if (diff > *max) *max = diff;
150 }
151 }
152
153 void calc_subband(Image_tree p, Image_tree q, int type, double *min, double *max, double *error) {
154 calc__subband(p, q, min, max, error);
155 }
156
157 void calc_subband_wp(Image_tree p, Image_tree q, char *name, double *min, double *max, double *error) {
158 calc__subband(p, q, min, max, error);
159 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.