File: flip.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (133 lines) | stat: -rw-r--r-- 3,160 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include	<string>
#include	<math.h>
#include	<stdio.h>
#include	"gr.hh"
#include	"extern.hh"
#include	"image_ex.hh"

bool            flipCmd();
bool            flip_gridCmd(void);
bool            flip_imageCmd(void);

bool
flipCmd()
{
    if (_nword != 3) {
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    if (!strcmp(_word[1], "grid"))
	return flip_gridCmd();
    else if (!strcmp(_word[1], "image"))
	return flip_imageCmd();
    else
	return false;
}

bool
flip_gridCmd()
{
    register int    i, j;
    int             width, width_half, height, height_half;
    double          swap;
    bool            do_x = true;
    if (_nword != 3) {
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    if (!strcmp(_word[2], "x"))
	do_x = true;
    else if (!strcmp(_word[2], "y"))
	do_x = false;
    else {
	demonstrate_command_usage();
	err("Can only flip with respect to `x' or `y'");
	return false;
    }
    if (!_grid_exists) {
	err("No grid exists yet\n");
	return false;
    }
    // Flip the grid.
    width = _num_xmatrix_data;
    width_half = width / 2;
    height = _num_ymatrix_data;
    height_half = height / 2;
    if (do_x) {
	for (i = 0; i < width_half; i++)
	    for (j = 0; j < height; j++) {
		int             i_swap = width - i - 1;
		swap = _f_xy(i, j);
		_f_xy(i, j) = _f_xy(i_swap, j);
		_f_xy(i_swap, j) = swap;
		bool swap_legit = _legit_xy(i, j);
		_legit_xy(i, j) = _legit_xy(i_swap, j);
		_legit_xy(i_swap, j) = swap_legit;
	    }
    } else {
	for (i = 0; i < width; i++)
	    for (j = 0; j < height_half; j++) {
		int             j_swap = height - j - 1;
		swap = _f_xy(i, j);
		_f_xy(i, j) = _f_xy(i, j_swap);
		_f_xy(i, j_swap) = swap;
		bool swap_legit = _legit_xy(i, j);
		_legit_xy(i, j) = _legit_xy(i, j_swap);
		_legit_xy(i, j_swap) = swap_legit;
	    }
    }
    return true;
}

bool
flip_imageCmd()
{
    register int    i, j, width, width_half, height, height_half;
    register unsigned char swap;
    bool            do_x = true;
    if (_nword != 3) {
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    if (!strcmp(_word[2], "x"))
	do_x = true;
    else if (!strcmp(_word[2], "y"))
	do_x = false;
    else {
	demonstrate_command_usage();
	err("Can only flip with respect to `x' or `y'");
	return false;
    }
    if (!image_exists()) {
	err("No image exists yet\n");
	return false;
    }
    /*
     * Flip the image.
     */
    width = _image.ras_width;
    width_half = width / 2;
    height = _image.ras_height;
    height_half = height / 2;
    if (do_x) {
	for (i = 0; i < width; i++)
	    for (j = 0; j < height_half; j++) {
		int             jj = height - j - 1;
		swap = *(_image.image + j * width + i);
		*(_image.image + j * width + i) = *(_image.image + jj * width + i);
		*(_image.image + jj * width + i) = swap;
	    }
    } else {
	for (i = 0; i < width_half; i++)
	    for (j = 0; j < height; j++) {
		int             ii = width - i - 1;
		swap = *(_image.image + j * width + i);
		*(_image.image + j * width + i) = *(_image.image + j * width + ii);
		*(_image.image + j * width + ii) = swap;
	    }
    }
    return true;
}