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

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