Coverity: fix wmsetbg string not null terminated
[wmaker-crm.git] / wrlib / flip.c
blobb884ab58a8827e51e2cbeb1c19ce498c98384da3
1 /* flip.c - image flip
3 * Raster graphics library
5 * Copyright (c) 2014 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <X11/Xlib.h>
31 #include "wraster.h"
32 #include "rotate.h"
33 #include "wr_i18n.h"
36 static RImage *r_flip_vertically(RImage *source);
37 static RImage *r_flip_horizontally(RImage *source);
39 /* Flip an image in the direction(s) specified */
40 RImage *RFlipImage(RImage *source, int mode)
42 /* Security */
43 if (source == NULL)
44 return NULL;
46 switch (mode & (RVerticalFlip | RHorizontalFlip)) {
47 case RHorizontalFlip:
48 return r_flip_horizontally(source);
50 case RVerticalFlip:
51 return r_flip_vertically(source);
53 case RHorizontalFlip | RVerticalFlip:
54 return wraster_rotate_image_180(source);
56 default:
57 return RRetainImage(source);
61 RImage *r_flip_vertically(RImage *source)
63 RImage *target;
64 int nwidth, nheight;
65 int x, y;
67 nwidth = source->width;
68 nheight = source->height;
70 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
71 if (!target)
72 return NULL;
74 if (source->format == RRGBFormat) {
75 unsigned char *optr, *nptr;
77 optr = source->data;
78 nptr = target->data + 3 * (nwidth * nheight - nwidth);
80 for (y = 0; y < nheight; y++) {
81 for (x = 0; x < nwidth; x++) {
82 nptr[0] = optr[0];
83 nptr[1] = optr[1];
84 nptr[2] = optr[2];
86 optr += 3;
87 nptr += 3;
89 nptr -= (nwidth * 3) * 2;
91 } else {
92 unsigned char *optr, *nptr;
94 optr = source->data;
95 nptr = target->data + 4 * (nwidth * nheight - nwidth);
97 for (y = 0; y < nheight; y++) {
98 for (x = 0; x < nwidth; x++) {
99 nptr[0] = optr[0];
100 nptr[1] = optr[1];
101 nptr[2] = optr[2];
102 nptr[3] = optr[3];
104 optr += 4;
105 nptr += 4;
107 nptr -= (nwidth * 4) * 2;
110 return target;
113 RImage *r_flip_horizontally(RImage *source)
115 RImage *target;
116 int nwidth, nheight;
117 int x, y;
119 nwidth = source->width;
120 nheight = source->height;
122 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
123 if (!target)
124 return NULL;
126 if (source->format == RRGBFormat) {
127 unsigned char *optr, *nptr;
129 optr = source->data;
130 nptr = target->data + 3 * (nwidth - 1);
132 for (y = nheight; y; y--) {
133 for (x = 0; x < nwidth; x++) {
134 nptr[0] = optr[0];
135 nptr[1] = optr[1];
136 nptr[2] = optr[2];
138 optr += 3;
139 nptr -= 3;
141 nptr += (nwidth * 3) * 2;
143 } else {
144 unsigned char *optr, *nptr;
146 optr = source->data;
147 nptr = target->data + 4 * (nwidth - 1);
149 for (y = nheight; y; y--) {
150 for (x = 0; x < nwidth; x++) {
151 nptr[0] = optr[0];
152 nptr[1] = optr[1];
153 nptr[2] = optr[2];
154 nptr[3] = optr[3];
156 optr += 4;
157 nptr -= 4;
159 nptr += (nwidth * 4) * 2;
162 return target;