correct an old bug link
[imager.git] / flip.im
CommitLineData
619d5d1c 1#define IMAGER_NO_CONTEXT
e41cfe8f
TC
2#include "imager.h"
3
4static void flip_h(i_img *im);
5static void flip_v(i_img *im);
6static void flip_hv(i_img *im);
7
8#define XAXIS 0
9#define YAXIS 1
10#define XYAXIS 2
11
12/*
13=item i_flipxy(im, axis)
14
15Flips the image inplace around the axis specified.
16Returns 0 if parameters are invalid.
17
18 im - Image pointer
19 axis - 0 = x, 1 = y, 2 = both
20
21=cut
22*/
23
24undef_int
25i_flipxy(i_img *im, int direction) {
619d5d1c 26 dIMCTXim(im);
e41cfe8f
TC
27 i_clear_error();
28
619d5d1c 29 im_log((aIMCTX, 1, "i_flipxy(im %p, direction %d)\n", im, direction ));
e41cfe8f 30
e41cfe8f
TC
31 switch (direction) {
32 case XAXIS: /* Horizontal flip */
33 flip_h(im);
34 break;
35
36 case YAXIS: /* Vertical flip */
37 flip_v(im);
38 break;
39
40 case XYAXIS: /* Horizontal and Vertical flip */
41 flip_hv(im);
42 break;
43
44 default:
619d5d1c
TC
45 im_log((aIMCTX, 1, "i_flipxy: direction is invalid\n" ));
46 im_push_errorf(aIMCTX, 0, "direction %d invalid", direction);
e41cfe8f 47 return 0;
e41cfe8f
TC
48 }
49 return 1;
50}
51
52static void
53flip_row_pal(i_palidx *row, i_img_dim width) {
54 i_palidx tmp;
55 i_palidx *leftp = row;
56 i_palidx *rightp = row + width - 1;
57
58 while (leftp < rightp) {
59 tmp = *leftp;
60 *leftp = *rightp;
61 *rightp = tmp;
62 ++leftp;
63 --rightp;
64 }
65}
66
67#code
68
69static void
70IM_SUFFIX(flip_row)(IM_COLOR *row, i_img_dim width) {
71 IM_COLOR tmp;
72 IM_COLOR *leftp = row;
73 IM_COLOR *rightp = row + width - 1;
74
75 while (leftp < rightp) {
76 tmp = *leftp;
77 *leftp = *rightp;
78 *rightp = tmp;
79 ++leftp;
80 --rightp;
81 }
82}
83
84#/code
85
86static void
87flip_h(i_img *im) {
8d14daab 88 i_img_dim y;
e41cfe8f
TC
89 if (im->type == i_palette_type) {
90 i_palidx *line = mymalloc(im->xsize * sizeof(i_palidx));
91 for (y = 0; y < im->ysize; ++y) {
92 i_gpal(im, 0, im->xsize, y, line);
93 flip_row_pal(line, im->xsize);
94 i_ppal(im, 0, im->xsize, y, line);
95 }
96 myfree(line);
97 }
98 else {
99#code im->bits == i_8_bits
100 IM_COLOR *line = mymalloc(im->xsize * sizeof(IM_COLOR));
101 for (y = 0; y < im->ysize; ++y) {
102 IM_GLIN(im, 0, im->xsize, y, line);
103 IM_SUFFIX(flip_row)(line, im->xsize);
104 IM_PLIN(im, 0, im->xsize, y, line);
105 }
106 myfree(line);
107#/code
108 }
109}
110
111static void
112flip_v(i_img *im) {
8d14daab
TC
113 i_img_dim topy = 0;
114 i_img_dim boty = im->ysize - 1;
e41cfe8f
TC
115 if (im->type == i_palette_type) {
116 i_palidx *top_line = mymalloc(im->xsize * sizeof(i_palidx));
117 i_palidx *bot_line = mymalloc(im->xsize * sizeof(i_palidx));
118 while (topy < boty) {
119 i_gpal(im, 0, im->xsize, topy, top_line);
120 i_gpal(im, 0, im->xsize, boty, bot_line);
121 i_ppal(im, 0, im->xsize, topy, bot_line);
122 i_ppal(im, 0, im->xsize, boty, top_line);
123 ++topy;
124 --boty;
125 }
126 myfree(bot_line);
127 myfree(top_line);
128 }
129 else {
130#code im->bits == i_8_bits
131 IM_COLOR *top_line = mymalloc(im->xsize * sizeof(IM_COLOR));
132 IM_COLOR *bot_line = mymalloc(im->xsize * sizeof(IM_COLOR));
133 while (topy < boty) {
134 IM_GLIN(im, 0, im->xsize, topy, top_line);
135 IM_GLIN(im, 0, im->xsize, boty, bot_line);
136 IM_PLIN(im, 0, im->xsize, topy, bot_line);
137 IM_PLIN(im, 0, im->xsize, boty, top_line);
138 ++topy;
139 --boty;
140 }
141 myfree(top_line);
142 myfree(bot_line);
143#/code
144 }
145}
146
147static void
148flip_hv(i_img *im) {
8d14daab
TC
149 i_img_dim topy = 0;
150 i_img_dim boty = im->ysize - 1;
e41cfe8f
TC
151 if (im->type == i_palette_type) {
152 i_palidx *top_line = mymalloc(im->xsize * sizeof(i_palidx));
153 i_palidx *bot_line = mymalloc(im->xsize * sizeof(i_palidx));
154 while (topy < boty) {
155 i_gpal(im, 0, im->xsize, topy, top_line);
156 i_gpal(im, 0, im->xsize, boty, bot_line);
157 flip_row_pal(top_line, im->xsize);
158 flip_row_pal(bot_line, im->xsize);
159 i_ppal(im, 0, im->xsize, topy, bot_line);
160 i_ppal(im, 0, im->xsize, boty, top_line);
161 ++topy;
162 --boty;
163 }
164 myfree(bot_line);
165 myfree(top_line);
166 }
167 else {
168#code im->bits == i_8_bits
169 IM_COLOR *top_line = mymalloc(im->xsize * sizeof(IM_COLOR));
170 IM_COLOR *bot_line = mymalloc(im->xsize * sizeof(IM_COLOR));
171 while (topy < boty) {
172 IM_GLIN(im, 0, im->xsize, topy, top_line);
173 IM_GLIN(im, 0, im->xsize, boty, bot_line);
174 IM_SUFFIX(flip_row)(top_line, im->xsize);
175 IM_SUFFIX(flip_row)(bot_line, im->xsize);
176 IM_PLIN(im, 0, im->xsize, topy, bot_line);
177 IM_PLIN(im, 0, im->xsize, boty, top_line);
178 ++topy;
179 --boty;
180 }
181 if (topy == boty) {
182 IM_GLIN(im, 0, im->xsize, topy, top_line);
183 IM_SUFFIX(flip_row)(top_line, im->xsize);
184 IM_PLIN(im, 0, im->xsize, topy, top_line);
185 }
186 myfree(top_line);
187 myfree(bot_line);
188#/code
189 }
190}