Files
wmaker/wrlib/flip.c
T

164 lines
3.4 KiB
C
Raw Normal View History

2014-05-21 11:59:48 +07:00
/* flip.c - image flip
*
* Raster graphics library
*
* Copyright (c) 2014 Window Maker Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "config.h"
2014-05-21 11:59:48 +07:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2014-05-21 11:59:48 +07:00
#include <X11/Xlib.h>
2014-05-21 11:59:48 +07:00
#include "wraster.h"
#include "rotate.h"
#include "wr_i18n.h"
2014-05-21 11:59:48 +07:00
static RImage *r_flip_vertically(RImage *source);
static RImage *r_flip_horizontally(RImage *source);
/* Flip an image in the direction(s) specified */
RImage *RFlipImage(RImage *source, int mode)
{
/* Security */
if (source == NULL)
return NULL;
switch (mode & (RVerticalFlip | RHorizontalFlip)) {
case RHorizontalFlip:
return r_flip_horizontally(source);
case RVerticalFlip:
return r_flip_vertically(source);
case RHorizontalFlip | RVerticalFlip:
return wraster_rotate_image_180(source);
default:
return RRetainImage(source);
}
}
RImage *r_flip_vertically(RImage *source)
2014-05-21 11:59:48 +07:00
{
RImage *target;
2014-05-21 11:59:48 +07:00
int nwidth, nheight;
int x, y;
nwidth = source->width;
nheight = source->height;
2014-05-21 11:59:48 +07:00
target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
if (!target)
2014-05-21 11:59:48 +07:00
return NULL;
if (source->format == RRGBFormat) {
2014-05-26 22:42:44 +08:00
unsigned char *optr, *nptr;
optr = source->data;
nptr = target->data + 3 * (nwidth * nheight - nwidth);
2014-05-26 22:42:44 +08:00
for (y = 0; y < nheight; y++) {
for (x = 0; x < nwidth; x++) {
nptr[0] = optr[0];
nptr[1] = optr[1];
nptr[2] = optr[2];
optr += 3;
nptr += 3;
2014-05-21 11:59:48 +07:00
}
nptr -= (nwidth * 3) * 2;
2014-05-26 22:42:44 +08:00
}
} else {
unsigned char *optr, *nptr;
optr = source->data;
nptr = target->data + 4 * (nwidth * nheight - nwidth);
2014-05-26 22:42:44 +08:00
for (y = 0; y < nheight; y++) {
for (x = 0; x < nwidth; x++) {
nptr[0] = optr[0];
nptr[1] = optr[1];
nptr[2] = optr[2];
nptr[3] = optr[3];
optr += 4;
nptr += 4;
2014-05-21 11:59:48 +07:00
}
nptr -= (nwidth * 4) * 2;
2014-05-21 11:59:48 +07:00
}
2014-05-26 22:42:44 +08:00
}
return target;
2014-05-21 11:59:48 +07:00
}
RImage *r_flip_horizontally(RImage *source)
2014-05-21 11:59:48 +07:00
{
RImage *target;
2014-05-21 11:59:48 +07:00
int nwidth, nheight;
int x, y;
nwidth = source->width;
nheight = source->height;
2014-05-21 11:59:48 +07:00
target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
if (!target)
2014-05-21 11:59:48 +07:00
return NULL;
if (source->format == RRGBFormat) {
2014-05-26 22:42:44 +08:00
unsigned char *optr, *nptr;
2014-05-21 11:59:48 +07:00
optr = source->data;
nptr = target->data + 3 * (nwidth - 1);
2014-05-21 11:59:48 +07:00
2014-05-26 22:42:44 +08:00
for (y = nheight; y; y--) {
for (x = 0; x < nwidth; x++) {
nptr[0] = optr[0];
nptr[1] = optr[1];
nptr[2] = optr[2];
2014-05-21 11:59:48 +07:00
2014-05-26 22:42:44 +08:00
optr += 3;
nptr -= 3;
2014-05-21 11:59:48 +07:00
}
nptr += (nwidth * 3) * 2;
2014-05-26 22:42:44 +08:00
}
} else {
unsigned char *optr, *nptr;
2014-05-21 11:59:48 +07:00
optr = source->data;
nptr = target->data + 4 * (nwidth - 1);
2014-05-21 11:59:48 +07:00
2014-05-26 22:42:44 +08:00
for (y = nheight; y; y--) {
for (x = 0; x < nwidth; x++) {
nptr[0] = optr[0];
nptr[1] = optr[1];
nptr[2] = optr[2];
nptr[3] = optr[3];
2014-05-21 11:59:48 +07:00
2014-05-26 22:42:44 +08:00
optr += 4;
nptr -= 4;
2014-05-21 11:59:48 +07:00
}
nptr += (nwidth * 4) * 2;
2014-05-21 11:59:48 +07:00
}
2014-05-26 22:42:44 +08:00
}
return target;
2014-05-21 11:59:48 +07:00
}