2022-02-27 21:37:51 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2022-12-28 23:01:50 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2020-07-20 22:10:04 +00:00
|
|
|
using namespace std;
|
2022-02-27 21:37:51 +00:00
|
|
|
using namespace vips;
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2023-03-09 01:36:39 +00:00
|
|
|
char *Mirror(string type, string *outType, char *BufferData,
|
|
|
|
size_t BufferLength, ArgumentMap Arguments, size_t *DataSize) {
|
2022-12-26 06:28:56 +00:00
|
|
|
bool vertical = GetArgumentWithFallback<bool>(Arguments, "vertical", false);
|
|
|
|
bool first = GetArgumentWithFallback<bool>(Arguments, "first", false);
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
VOption *options = VImage::option()->set("access", "sequential");
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
VImage in =
|
|
|
|
VImage::new_from_buffer(BufferData, BufferLength, "",
|
2023-03-09 01:36:39 +00:00
|
|
|
type == "gif" ? options->set("n", -1) : options)
|
2022-12-26 06:28:56 +00:00
|
|
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
2022-12-28 23:01:50 +00:00
|
|
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
VImage out;
|
2021-02-25 21:09:53 +00:00
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
if (vertical) {
|
2023-03-09 01:36:39 +00:00
|
|
|
if (type == "gif") {
|
2022-12-26 06:28:56 +00:00
|
|
|
// once again, libvips gif handling is both a blessing and a curse
|
|
|
|
vector<VImage> img;
|
|
|
|
int pageHeight = vips_image_get_page_height(in.get_image());
|
|
|
|
int nPages = vips_image_get_n_pages(in.get_image());
|
|
|
|
bool isOdd = pageHeight % 2;
|
|
|
|
for (int i = 0; i < nPages; i++) {
|
|
|
|
int x = (i * pageHeight) + (first ? 0 : (pageHeight / 2));
|
|
|
|
VImage cropped = in.crop(0, x, in.width(), pageHeight / 2);
|
2022-02-27 21:37:51 +00:00
|
|
|
VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
|
2022-12-26 06:28:56 +00:00
|
|
|
VImage final = VImage::arrayjoin(
|
|
|
|
{first ? cropped : flipped, first ? flipped : cropped},
|
|
|
|
VImage::option()->set("across", 1));
|
|
|
|
img.push_back(final);
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2022-12-26 06:28:56 +00:00
|
|
|
out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
|
|
|
out.set(VIPS_META_PAGE_HEIGHT, pageHeight - (isOdd ? 1 : 0));
|
2022-02-27 21:37:51 +00:00
|
|
|
} else {
|
2022-12-26 06:28:56 +00:00
|
|
|
VImage cropped = in.extract_area(0, 0, in.width(), in.height() / 2);
|
|
|
|
VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
|
|
|
|
out = VImage::arrayjoin({cropped, flipped},
|
|
|
|
VImage::option()->set("across", 1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (first) {
|
|
|
|
VImage cropped = in.extract_area(0, 0, in.width() / 2, in.height());
|
|
|
|
VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
|
|
|
|
out = VImage::arrayjoin({cropped, flipped});
|
|
|
|
} else {
|
|
|
|
int size = in.width() / 2;
|
|
|
|
VImage cropped = in.extract_area(size, 0, size, in.height());
|
|
|
|
VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
|
|
|
|
out = VImage::arrayjoin({flipped, cropped});
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2022-06-28 03:50:53 +00:00
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
void *buf;
|
2023-03-09 01:36:39 +00:00
|
|
|
out.write_to_buffer(("." + *outType).c_str(), &buf, DataSize);
|
2022-12-26 06:28:56 +00:00
|
|
|
|
|
|
|
return (char *)buf;
|
2021-12-30 21:32:39 +00:00
|
|
|
}
|