0
|
1 #include "wm.h"
|
|
2 #include "gray.h"
|
|
3
|
|
4 gray **alloc_grays_8x8() {
|
|
5 return alloc_grays(8, 8);
|
|
6 }
|
|
7
|
|
8 gray **alloc_grays(int cols, int rows) {
|
|
9 gray **p;
|
|
10 int i;
|
|
11
|
|
12 p = (gray **)malloc(rows * sizeof(gray *));
|
|
13 if (!p) {
|
|
14 #ifdef DEBUG
|
|
15 fprintf(stderr, "alloc_grays(): malloc() failed\n");
|
|
16 exit(1);
|
|
17 #else
|
|
18 return NULL;
|
|
19 #endif
|
|
20 }
|
|
21
|
|
22 p[0] = (gray *)malloc(rows * cols * sizeof(gray));
|
|
23 if (!p[0]) {
|
|
24 #ifdef DEBUG
|
|
25 fprintf(stderr, "alloc_grays(): malloc() failed\n");
|
|
26 exit(1);
|
|
27 #else
|
|
28 free(p);
|
|
29 return NULL;
|
|
30 #endif
|
|
31 }
|
|
32
|
|
33 for (i = 1; i < rows; i++) {
|
|
34 p[i] = &(p[0][i * cols]);
|
|
35 }
|
|
36
|
|
37 return p;
|
|
38 }
|
|
39
|
|
40 void free_grays(gray **grays) {
|
|
41 free(grays[0]);
|
|
42 free(grays);
|
|
43 }
|
|
44
|
|
45 void copy_grays_to_block(gray ** block_grays, gray ** image_grays, int c, int r, int w, int h) {
|
|
46 int i, j;
|
|
47
|
|
48 #ifdef DEBUG
|
|
49 if (!image_grays) {
|
|
50 fprintf(stderr, "copy_grays_to_block(): NULL image pixels\n");
|
|
51 }
|
|
52 if (!block_grays) {
|
|
53 fprintf(stderr, "copy_grays_to_block(): NULL block pixels\n");
|
|
54 }
|
|
55 if (w <= 0 || h <= 0 || c < 0 || r < 0) {
|
|
56 fprintf(stderr, "copy_grays_to_block(): block dimension out of range\n");
|
|
57 }
|
|
58 #endif
|
|
59
|
|
60 for (i = 0; i < w; i++) {
|
|
61 for (j = 0; j < h; j++)
|
|
62 block_grays[j][i] = image_grays[r + j][c + i];
|
|
63 }
|
|
64 }
|
|
65
|
|
66 void copy_grays_from_block(gray ** image_grays, gray ** block_grays, int
|
|
67 c, int r, int w, int h) {
|
|
68 int i, j;
|
|
69
|
|
70 #ifdef DEBUG
|
|
71 if (!image_grays) {
|
|
72 fprintf(stderr, "copy_grays_from_block(): NULL image pixels\n");
|
|
73 }
|
|
74 if (!block_grays) {
|
|
75 fprintf(stderr, "copy_grays_from_block(): NULL block pixels\n");
|
|
76 }
|
|
77 if (w <= 0 || h <= 0 || c < 0 || r < 0) {
|
|
78 fprintf(stderr, "copy_grays_from_block(): block dimension out of range\n");
|
|
79 }
|
|
80 #endif
|
|
81
|
|
82 for (i = 0; i < w; i++) {
|
|
83 for (j = 0; j < h; j++)
|
|
84 image_grays[r + j][c + i] = block_grays[j][i];
|
|
85 }
|
|
86 }
|
|
87
|
|
88 void print_grays(gray **grays, int c, int r, int w, int h) {
|
|
89 int i, j;
|
|
90 gray *p;
|
|
91
|
|
92 #ifdef DEBUG
|
|
93 if (!grays) {
|
|
94 fprintf(stderr, "print_grays(): NULL pixels\n");
|
|
95 }
|
|
96 if (w <= 0 || h <= 0 || c < 0 || r < 0) {
|
|
97 fprintf(stderr, "print_grays(): block dimension out of range\n");
|
|
98 }
|
|
99 #endif
|
|
100
|
|
101 for (j = r; j < r + h; j++) {
|
|
102 p = &grays[j][c];
|
|
103 for (i = 0; i < w; i++)
|
|
104 fprintf(stderr, "%3d ", *(p++));
|
|
105 fprintf(stderr, "\n");
|
|
106 }
|
|
107 }
|
|
108
|
|
109 void print_grays_8x8(gray **grays) {
|
|
110 int i, j;
|
|
111
|
|
112 for (i = 0; i < 8; i++) {
|
|
113 for (j = 0; j < 8; j++)
|
|
114 fprintf(stderr, "%3d ", grays[i][j]);
|
|
115 fprintf(stderr, "\n");
|
|
116 }
|
|
117 }
|