Improve flip_image() performance (x10 from old impl, x2 from new slow impl)

The old impl was using get/put_pixel(), the new slow one is using the
get/put_pixel_fast(), and the new default flip_image() is using just
raw pointers.

Added some utilites like random_image() for testing purposes, and
DOC_DISPATCH_BY_COLOR_MODE() macros to avoid switch/case for each
color mode. In a future these might use generic lambdas.
This commit is contained in:
David Capello 2023-08-01 09:32:39 -03:00
parent 2e1d50bf33
commit 413288a014
8 changed files with 354 additions and 26 deletions

View File

@ -13,6 +13,7 @@ add_library(doc-lib
algorithm/floodfill.cpp
algorithm/modify_selection.cpp
algorithm/polygon.cpp
algorithm/random_image.cpp
algorithm/resize_image.cpp
algorithm/rotate.cpp
algorithm/rotsprite.cpp

View File

@ -0,0 +1,63 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/algorithm/flip_image.h"
#include "doc/image.h"
#include <benchmark/benchmark.h>
using namespace doc;
void BM_FlipSlow(benchmark::State& state) {
const auto pf = (PixelFormat)state.range(0);
const int w = state.range(1);
const int h = state.range(2);
const auto ft = (doc::algorithm::FlipType)state.range(3);
std::unique_ptr<Image> img(Image::create(pf, w, h));
while (state.KeepRunning()) {
algorithm::flip_image_slow(img.get(), img->bounds(), ft);
}
}
void BM_FlipRawPtr(benchmark::State& state) {
const auto pf = (PixelFormat)state.range(0);
const int w = state.range(1);
const int h = state.range(2);
const auto ft = (doc::algorithm::FlipType)state.range(3);
std::unique_ptr<Image> img(Image::create(pf, w, h));
while (state.KeepRunning()) {
algorithm::flip_image(img.get(), img->bounds(), ft);
}
}
#define DEFARGS() \
->Args({ IMAGE_RGB, 8192, 8192, doc::algorithm::FlipHorizontal }) \
->Args({ IMAGE_RGB, 8192, 8192, doc::algorithm::FlipVertical }) \
->Args({ IMAGE_GRAYSCALE, 8192, 8192, doc::algorithm::FlipHorizontal }) \
->Args({ IMAGE_GRAYSCALE, 8192, 8192, doc::algorithm::FlipVertical }) \
->Args({ IMAGE_INDEXED, 8192, 8192, doc::algorithm::FlipHorizontal }) \
->Args({ IMAGE_INDEXED, 8192, 8192, doc::algorithm::FlipVertical }) \
->Args({ IMAGE_BITMAP, 8192, 8192, doc::algorithm::FlipHorizontal }) \
->Args({ IMAGE_BITMAP, 8192, 8192, doc::algorithm::FlipVertical }) \
->Args({ IMAGE_TILEMAP, 8192, 8192, doc::algorithm::FlipHorizontal }) \
->Args({ IMAGE_TILEMAP, 8192, 8192, doc::algorithm::FlipVertical })
BENCHMARK(BM_FlipSlow)
DEFARGS()
->Unit(benchmark::kMicrosecond)
->UseRealTime();
BENCHMARK(BM_FlipRawPtr)
DEFARGS()
->Unit(benchmark::kMicrosecond)
->UseRealTime();
BENCHMARK_MAIN();

View File

@ -1,4 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -10,41 +11,47 @@
#include "doc/algorithm/flip_image.h"
#include "gfx/rect.h"
#include "doc/dispatch.h"
#include "doc/image.h"
#include "doc/image_impl.h"
#include "doc/mask.h"
#include "doc/primitives.h"
#include "doc/primitives_fast.h"
#include "gfx/rect.h"
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
namespace doc {
namespace algorithm {
void flip_image(Image* image, const gfx::Rect& bounds, FlipType flipType)
template<typename ImageTraits>
void flip_image_with_put_pixel_fast_templ(Image* image, const gfx::Rect& bounds, FlipType flipType)
{
switch (flipType) {
case FlipHorizontal:
for (int y=bounds.y; y<bounds.y+bounds.h; ++y) {
int u = bounds.x+bounds.w-1;
for (int y=bounds.y; y<bounds.y2(); ++y) {
int u = bounds.x2()-1;
for (int x=bounds.x; x<bounds.x+bounds.w/2; ++x, --u) {
uint32_t c1 = get_pixel(image, x, y);
uint32_t c2 = get_pixel(image, u, y);
put_pixel(image, x, y, c2);
put_pixel(image, u, y, c1);
uint32_t c1 = get_pixel_fast<ImageTraits>(image, x, y);
uint32_t c2 = get_pixel_fast<ImageTraits>(image, u, y);
put_pixel_fast<ImageTraits>(image, x, y, c2);
put_pixel_fast<ImageTraits>(image, u, y, c1);
}
}
break;
case FlipVertical: {
int v = bounds.y+bounds.h-1;
int v = bounds.y2()-1;
for (int y=bounds.y; y<bounds.y+bounds.h/2; ++y, --v) {
for (int x=bounds.x; x<bounds.x+bounds.w; ++x) {
uint32_t c1 = get_pixel(image, x, y);
uint32_t c2 = get_pixel(image, x, v);
put_pixel(image, x, y, c2);
put_pixel(image, x, v, c1);
for (int x=bounds.x; x<bounds.x2(); ++x) {
uint32_t c1 = get_pixel_fast<ImageTraits>(image, x, y);
uint32_t c2 = get_pixel_fast<ImageTraits>(image, x, v);
put_pixel_fast<ImageTraits>(image, x, y, c2);
put_pixel_fast<ImageTraits>(image, x, v, c1);
}
}
break;
@ -52,7 +59,65 @@ void flip_image(Image* image, const gfx::Rect& bounds, FlipType flipType)
}
}
void flip_image_with_mask(Image* image, const Mask* mask, FlipType flipType, int bgcolor)
template<typename ImageTraits>
void flip_image_with_rawptr_templ(Image* image, const gfx::Rect& bounds, FlipType flipType)
{
using address_t = typename ImageTraits::address_t;
switch (flipType) {
case FlipHorizontal:
for (int y=bounds.y; y<bounds.y2(); ++y) {
const int n = bounds.w/2;
auto l = (address_t)image->getPixelAddress(bounds.x, y);
auto r = (address_t)image->getPixelAddress(bounds.x2()-1, y);
for (int x=0; x<n; ++x, ++l, --r) {
std::swap(*l, *r);
}
}
break;
case FlipVertical: {
const int n = bounds.w;
int v = bounds.y2()-1;
for (int y=bounds.y; y<bounds.y+bounds.h/2; ++y, --v) {
auto t = (address_t)image->getPixelAddress(bounds.x, y);
auto b = (address_t)image->getPixelAddress(bounds.x, v);
for (int x=0; x<n; ++x, ++t, ++b) {
std::swap(*t, *b);
}
}
break;
}
}
}
void flip_image_slow(Image* image, const gfx::Rect& bounds, FlipType flipType)
{
DOC_DISPATCH_BY_COLOR_MODE(
image->colorMode(),
flip_image_with_put_pixel_fast_templ,
image, bounds, flipType);
}
void flip_image(Image* image, const gfx::Rect& bounds, FlipType flipType)
{
// Use get/put_pixel_fast for IMAGE_BITMAP as we cannot use the
// rawptr to iterate through bits.
if (image->colorMode() == ColorMode::BITMAP) {
return flip_image_with_put_pixel_fast_templ<BitmapTraits>(image, bounds, flipType);
}
DOC_DISPATCH_BY_COLOR_MODE_EXCLUDE_BITMAP(
image->colorMode(),
flip_image_with_rawptr_templ,
image, bounds, flipType);
}
template<typename ImageTraits>
void flip_image_with_mask_templ(Image* image, const Mask* mask, FlipType flipType, int bgcolor)
{
gfx::Rect bounds = mask->bounds();
@ -61,16 +126,19 @@ void flip_image_with_mask(Image* image, const Mask* mask, FlipType flipType, int
case FlipHorizontal: {
std::unique_ptr<Image> originalRow(Image::create(image->pixelFormat(), bounds.w, 1));
for (int y=bounds.y; y<bounds.y+bounds.h; ++y) {
for (int y=bounds.y; y<bounds.y2(); ++y) {
// Copy the current row.
originalRow->copy(image, gfx::Clip(0, 0, bounds.x, y, bounds.w, 1));
int u = bounds.x+bounds.w-1;
for (int x=bounds.x; x<bounds.x+bounds.w; ++x, --u) {
int u = bounds.x2()-1;
for (int x=bounds.x; x<bounds.x2(); ++x, --u) {
if (mask->containsPoint(x, y)) {
put_pixel(image, u, y, get_pixel(originalRow.get(), x-bounds.x, 0));
put_pixel_fast<ImageTraits>(
image, u, y,
get_pixel_fast<ImageTraits>(originalRow.get(), x-bounds.x, 0));
if (!mask->containsPoint(u, y))
put_pixel(image, x, y, bgcolor);
put_pixel_fast<ImageTraits>(image, x, y, bgcolor);
}
}
}
@ -80,16 +148,19 @@ void flip_image_with_mask(Image* image, const Mask* mask, FlipType flipType, int
case FlipVertical: {
std::unique_ptr<Image> originalCol(Image::create(image->pixelFormat(), 1, bounds.h));
for (int x=bounds.x; x<bounds.x+bounds.w; ++x) {
for (int x=bounds.x; x<bounds.x2(); ++x) {
// Copy the current column.
originalCol->copy(image, gfx::Clip(0, 0, x, bounds.y, 1, bounds.h));
int v = bounds.y+bounds.h-1;
for (int y=bounds.y; y<bounds.y+bounds.h; ++y, --v) {
int v = bounds.y2()-1;
for (int y=bounds.y; y<bounds.y2(); ++y, --v) {
if (mask->containsPoint(x, y)) {
put_pixel(image, x, v, get_pixel(originalCol.get(), 0, y-bounds.y));
put_pixel_fast<ImageTraits>(
image, x, v,
get_pixel_fast<ImageTraits>(originalCol.get(), 0, y-bounds.y));
if (!mask->containsPoint(x, v))
put_pixel(image, x, y, bgcolor);
put_pixel_fast<ImageTraits>(image, x, y, bgcolor);
}
}
}
@ -99,5 +170,13 @@ void flip_image_with_mask(Image* image, const Mask* mask, FlipType flipType, int
}
}
void flip_image_with_mask(Image* image, const Mask* mask, FlipType flipType, int bgcolor)
{
DOC_DISPATCH_BY_COLOR_MODE(
image->colorMode(),
flip_image_with_mask_templ,
image, mask, flipType, bgcolor);
}
} // namespace algorithm
} // namespace doc

View File

@ -1,4 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
// Copyright (c) 2001-2014 David Capello
//
// This file is released under the terms of the MIT license.
@ -17,8 +18,12 @@ namespace doc {
namespace algorithm {
// Flips the rectangular region specified in the "bounds" parameter.
// Different implementation to flip a rectangular region specified
// in the "bounds" parameter.
// flip_image: uses raw pointers
// flip_image_slow: uses get/put_pixel_fast
void flip_image(Image* image, const gfx::Rect& bounds, FlipType flipType);
void flip_image_slow(Image* image, const gfx::Rect& bounds, FlipType flipType);
// Flips an irregular region specified by the "mask". The
// "bgcolor" is used to clear areas that aren't covered by a

View File

@ -0,0 +1,58 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "gtest/gtest.h"
#include "doc/algorithm/flip_image.h"
#include "doc/algorithm/random_image.h"
#include "doc/image.h"
#include "doc/image_ref.h"
#include "doc/primitives.h"
using namespace doc;
using namespace gfx;
TEST(Flip, Image)
{
for (auto pf : { IMAGE_RGB, IMAGE_GRAYSCALE, IMAGE_INDEXED, IMAGE_BITMAP, IMAGE_TILEMAP }) {
for (int h=2; h<200; h+=5) {
for (int w=2; w<200; w+=5) {
ImageRef a(Image::create(pf, w, h));
doc::algorithm::random_image(a.get());
// random_image() shouldn't generate a plain image
//ASSERT_FALSE(is_plain_image(a.get(), get_pixel(a.get(), 0, 0)));
ImageRef b(Image::createCopy(a.get()));
ImageRef c(Image::createCopy(a.get()));
ASSERT_TRUE(is_same_image(b.get(), c.get()));
for (auto ft : { doc::algorithm::FlipHorizontal,
doc::algorithm::FlipVertical }) {
doc::algorithm::flip_image(b.get(), b->bounds(), ft);
doc::algorithm::flip_image_slow(c.get(), c->bounds(), ft);
ASSERT_TRUE(is_same_image(b.get(), c.get()))
<< "Pixel format=" << pf << " Size=" << w << "x" << h
<< "\nFlip type=" << ft;
doc::algorithm::flip_image(b.get(), b->bounds(), ft);
doc::algorithm::flip_image_slow(c.get(), c->bounds(), ft);
ASSERT_TRUE(is_same_image(a.get(), b.get()));
ASSERT_TRUE(is_same_image(a.get(), c.get()));
}
}
}
}
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,55 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/algorithm/random_image.h"
#include "doc/dispatch.h"
#include "doc/image.h"
#include "doc/image_impl.h"
#include <random>
namespace doc {
namespace algorithm {
template<typename ImageTraits>
void random_image_templ(Image* image)
{
using pixel_t = typename ImageTraits::pixel_t;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<color_t> dist(ImageTraits::min_value,
ImageTraits::max_value);
doc::transform_image<ImageTraits>(
image,
[&](pixel_t) -> pixel_t {
color_t v = dist(gen);
if constexpr (ImageTraits::color_mode == ColorMode::RGB) {
v = rgba_seta(v, 255);
}
else if constexpr (ImageTraits::color_mode == ColorMode::GRAYSCALE) {
v = graya_seta(v, 255);
}
return v;
});
}
void random_image(Image* image)
{
DOC_DISPATCH_BY_COLOR_MODE(
image->colorMode(),
random_image_templ,
image);
}
} // namespace algorithm
} // namespace doc

View File

@ -0,0 +1,21 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DOC_ALGORITHM_RANDOM_IMAGE_H_INCLUDED
#define DOC_ALGORITHM_RANDOM_IMAGE_H_INCLUDED
#pragma once
namespace doc {
class Image;
namespace algorithm {
void random_image(Image* image);
}
}
#endif

46
src/doc/dispatch.h Normal file
View File

@ -0,0 +1,46 @@
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DOC_DISPATCH_H_INCLUDED
#define DOC_DISPATCH_H_INCLUDED
#pragma once
#include "doc/color_mode.h"
#include "doc/image_traits.h"
// Calls the given func<ImageTraits>() with the parameters "..." for
// each color mode implementation (RGB, Grayscale, etc.) depending on
// the given "colorMode".
#define DOC_DISPATCH_BY_COLOR_MODE(colorMode, func, ...) \
switch (colorMode) { \
case doc::ColorMode::RGB: \
return func<doc::RgbTraits>(__VA_ARGS__); \
case doc::ColorMode::GRAYSCALE: \
return func<doc::GrayscaleTraits>(__VA_ARGS__); \
case doc::ColorMode::INDEXED: \
return func<doc::IndexedTraits>(__VA_ARGS__); \
case doc::ColorMode::BITMAP: \
return func<doc::BitmapTraits>(__VA_ARGS__); \
case doc::ColorMode::TILEMAP: \
return func<doc::TilemapTraits>(__VA_ARGS__); \
}
#define DOC_DISPATCH_BY_COLOR_MODE_EXCLUDE_BITMAP(colorMode, func, ...) \
switch (colorMode) { \
case doc::ColorMode::RGB: \
return func<doc::RgbTraits>(__VA_ARGS__); \
case doc::ColorMode::GRAYSCALE: \
return func<doc::GrayscaleTraits>(__VA_ARGS__); \
case doc::ColorMode::INDEXED: \
return func<doc::IndexedTraits>(__VA_ARGS__); \
case doc::ColorMode::TILEMAP: \
return func<doc::TilemapTraits>(__VA_ARGS__); \
case doc::ColorMode::BITMAP: \
ASSERT(false); \
break; \
}
#endif